2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
28 e83f12a6 2023-02-12 op #include <sha2.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
45 53ccebc2 2019-07-30 stsp #include "got_version.h"
46 9f7d7167 2018-04-29 stsp #include "got_error.h"
47 80ddbec8 2018-04-29 stsp #include "got_object.h"
48 80ddbec8 2018-04-29 stsp #include "got_reference.h"
49 80ddbec8 2018-04-29 stsp #include "got_repository.h"
50 80ddbec8 2018-04-29 stsp #include "got_diff.h"
51 511a516b 2018-05-19 stsp #include "got_opentemp.h"
52 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
53 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
54 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
55 a70480e0 2018-06-23 stsp #include "got_blame.h"
56 c2db6724 2019-01-04 stsp #include "got_privsep.h"
57 1dd54920 2019-05-11 stsp #include "got_path.h"
58 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
70 9f7d7167 2018-04-29 stsp #ifndef nitems
71 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 9f7d7167 2018-04-29 stsp struct tog_cmd {
75 c2301be8 2018-04-30 stsp const char *name;
76 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
77 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
80 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
84 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
85 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
90 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
91 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
93 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
94 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
95 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
96 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
97 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
98 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
101 d6b05b5b 2018-08-04 stsp enum tog_view_type {
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
103 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
104 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
105 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
106 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
107 ec2a9698 2022-09-15 mark TOG_VIEW_HELP
110 ec2a9698 2022-09-15 mark /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
111 ec2a9698 2022-09-15 mark enum tog_keymap_type {
112 ec2a9698 2022-09-15 mark TOG_KEYMAP_KEYS = -2,
113 ec2a9698 2022-09-15 mark TOG_KEYMAP_GLOBAL,
114 ec2a9698 2022-09-15 mark TOG_KEYMAP_DIFF,
115 ec2a9698 2022-09-15 mark TOG_KEYMAP_LOG,
116 ec2a9698 2022-09-15 mark TOG_KEYMAP_BLAME,
117 ec2a9698 2022-09-15 mark TOG_KEYMAP_TREE,
118 ec2a9698 2022-09-15 mark TOG_KEYMAP_REF,
119 ec2a9698 2022-09-15 mark TOG_KEYMAP_HELP
122 9b058f45 2022-06-30 mark enum tog_view_mode {
123 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
124 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
125 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
128 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
130 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
132 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
133 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
134 ba4f502b 2018-08-04 stsp struct got_object_id *id;
135 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
138 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
139 ba4f502b 2018-08-04 stsp struct commit_queue {
140 ba4f502b 2018-08-04 stsp int ncommits;
141 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
144 f26dddb7 2019-11-08 stsp struct tog_color {
145 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
146 6d17833f 2019-11-08 stsp regex_t regex;
147 6d17833f 2019-11-08 stsp short colorpair;
149 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
151 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
152 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
153 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
155 cc488aa7 2022-01-23 stsp static const struct got_error *
156 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
157 cc488aa7 2022-01-23 stsp struct got_reference* re2)
159 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
160 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
161 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
163 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
164 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
165 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
166 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
168 cc488aa7 2022-01-23 stsp return NULL;
169 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
171 cc488aa7 2022-01-23 stsp return NULL;
174 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
175 cc488aa7 2022-01-23 stsp return NULL;
178 11b20872 2019-11-08 stsp static const struct got_error *
179 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
181 51a10b52 2020-12-26 stsp const struct got_error *err;
183 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
184 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
187 51a10b52 2020-12-26 stsp return err;
189 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
193 51a10b52 2020-12-26 stsp static void
194 51a10b52 2020-12-26 stsp tog_free_refs(void)
196 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
197 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
198 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
200 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
203 51a10b52 2020-12-26 stsp static const struct got_error *
204 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
205 11b20872 2019-11-08 stsp int idx, short color)
207 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
208 11b20872 2019-11-08 stsp struct tog_color *tc;
209 11b20872 2019-11-08 stsp int regerr = 0;
211 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
212 11b20872 2019-11-08 stsp return NULL;
214 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
216 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
217 11b20872 2019-11-08 stsp if (tc == NULL)
218 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
219 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
220 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
221 11b20872 2019-11-08 stsp if (regerr) {
222 11b20872 2019-11-08 stsp static char regerr_msg[512];
223 11b20872 2019-11-08 stsp static char err_msg[512];
224 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
225 11b20872 2019-11-08 stsp sizeof(regerr_msg));
226 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
227 11b20872 2019-11-08 stsp regerr_msg);
228 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
230 11b20872 2019-11-08 stsp return err;
232 11b20872 2019-11-08 stsp tc->colorpair = idx;
233 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
234 11b20872 2019-11-08 stsp return NULL;
237 11b20872 2019-11-08 stsp static void
238 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
240 11b20872 2019-11-08 stsp struct tog_color *tc;
242 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
243 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
244 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
245 11b20872 2019-11-08 stsp regfree(&tc->regex);
250 336075a4 2022-06-25 op static struct tog_color *
251 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
253 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
255 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
256 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
260 11b20872 2019-11-08 stsp return NULL;
264 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
267 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
269 11b20872 2019-11-08 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
271 11b20872 2019-11-08 stsp return COLOR_YELLOW;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
273 11b20872 2019-11-08 stsp return COLOR_GREEN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
275 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
277 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
279 91b8c405 2020-01-25 stsp return COLOR_CYAN;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
281 11b20872 2019-11-08 stsp return COLOR_GREEN;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
283 11b20872 2019-11-08 stsp return COLOR_GREEN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
285 11b20872 2019-11-08 stsp return COLOR_CYAN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
287 11b20872 2019-11-08 stsp return COLOR_YELLOW;
288 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
289 6458efa5 2020-11-24 stsp return COLOR_GREEN;
290 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
291 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
294 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
295 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
301 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
303 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
305 11b20872 2019-11-08 stsp if (val == NULL)
306 11b20872 2019-11-08 stsp return default_color_value(envvar);
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
309 11b20872 2019-11-08 stsp return COLOR_BLACK;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
311 11b20872 2019-11-08 stsp return COLOR_RED;
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
313 11b20872 2019-11-08 stsp return COLOR_GREEN;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
315 11b20872 2019-11-08 stsp return COLOR_YELLOW;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
317 11b20872 2019-11-08 stsp return COLOR_BLUE;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
319 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
321 11b20872 2019-11-08 stsp return COLOR_CYAN;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
323 11b20872 2019-11-08 stsp return COLOR_WHITE;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
327 11b20872 2019-11-08 stsp return default_color_value(envvar);
330 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
331 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
332 3dbaef42 2020-11-24 stsp const char *label1, *label2;
333 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
334 f9d37699 2022-06-28 stsp int fd1, fd2;
335 c7d5c43c 2022-08-04 mark int lineno;
336 15a087fe 2019-02-21 stsp int first_displayed_line;
337 15a087fe 2019-02-21 stsp int last_displayed_line;
339 15a087fe 2019-02-21 stsp int diff_context;
340 3dbaef42 2020-11-24 stsp int ignore_whitespace;
341 64453f7e 2020-11-21 stsp int force_text_diff;
342 15a087fe 2019-02-21 stsp struct got_repository *repo;
343 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
344 fe621944 2020-11-10 stsp size_t nlines;
345 f44b1f58 2020-02-02 tracey int matched_line;
346 f44b1f58 2020-02-02 tracey int selected_line;
348 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
349 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
352 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
353 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
355 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
356 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
357 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
358 1a76625f 2018-10-22 stsp int commits_needed;
359 fb280deb 2021-08-30 stsp int load_all;
360 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
361 568eae95 2022-09-11 mark struct commit_queue *real_commits;
362 1a76625f 2018-10-22 stsp const char *in_repo_path;
363 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
364 1a76625f 2018-10-22 stsp struct got_repository *repo;
365 74467cc8 2022-06-15 stsp int *pack_fds;
366 1a76625f 2018-10-22 stsp int log_complete;
367 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
368 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
369 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
370 13add988 2019-10-15 stsp int *searching;
371 13add988 2019-10-15 stsp int *search_next_done;
372 13add988 2019-10-15 stsp regex_t *regex;
373 568eae95 2022-09-11 mark int *limiting;
374 568eae95 2022-09-11 mark int limit_match;
375 568eae95 2022-09-11 mark regex_t *limit_regex;
376 568eae95 2022-09-11 mark struct commit_queue *limit_commits;
379 1a76625f 2018-10-22 stsp struct tog_log_view_state {
380 568eae95 2022-09-11 mark struct commit_queue *commits;
381 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
382 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
383 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
384 568eae95 2022-09-11 mark struct commit_queue real_commits;
385 b01e7d3b 2018-08-04 stsp int selected;
386 b01e7d3b 2018-08-04 stsp char *in_repo_path;
387 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
388 b672a97a 2020-01-27 stsp int log_branches;
389 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
390 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
391 1a76625f 2018-10-22 stsp sig_atomic_t quit;
392 1a76625f 2018-10-22 stsp pthread_t thread;
393 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
394 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
395 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
396 11b20872 2019-11-08 stsp struct tog_colors colors;
397 10aab77f 2022-07-19 op int use_committer;
398 568eae95 2022-09-11 mark int limit_view;
399 568eae95 2022-09-11 mark regex_t limit_regex;
400 568eae95 2022-09-11 mark struct commit_queue limit_commits;
403 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
404 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
405 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
406 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
407 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
408 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
409 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
410 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
411 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
412 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
413 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
414 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
415 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
416 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
417 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
419 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
420 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
421 e9424729 2018-08-04 stsp int nlines;
423 e9424729 2018-08-04 stsp struct tog_view *view;
424 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
429 e9424729 2018-08-04 stsp const char *path;
430 e9424729 2018-08-04 stsp struct got_repository *repo;
431 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
432 e9424729 2018-08-04 stsp int *complete;
433 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
434 fc06ba56 2019-08-22 stsp void *cancel_arg;
437 e9424729 2018-08-04 stsp struct tog_blame {
439 be659d10 2020-11-18 stsp off_t filesize;
440 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
441 6fcac457 2018-11-19 stsp int nlines;
442 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
443 e9424729 2018-08-04 stsp pthread_t thread;
444 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
445 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
446 e9424729 2018-08-04 stsp const char *path;
447 0ae84acc 2022-06-15 tracey int *pack_fds;
450 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
451 7cbe629d 2018-08-04 stsp int first_displayed_line;
452 7cbe629d 2018-08-04 stsp int last_displayed_line;
453 7cbe629d 2018-08-04 stsp int selected_line;
454 c0f61fa4 2022-07-11 mark int last_diffed_line;
455 7cbe629d 2018-08-04 stsp int blame_complete;
458 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
459 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
460 e5a0f69f 2018-08-18 stsp char *path;
461 7cbe629d 2018-08-04 stsp struct got_repository *repo;
462 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
463 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
464 e9424729 2018-08-04 stsp struct tog_blame blame;
465 6c4c42e0 2019-06-24 stsp int matched_line;
466 11b20872 2019-11-08 stsp struct tog_colors colors;
469 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
470 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
471 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
472 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
473 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
474 ad80ab7b 2018-08-04 stsp int selected;
477 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
479 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
480 ad80ab7b 2018-08-04 stsp char *tree_label;
481 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
482 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
483 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
484 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
486 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
487 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
488 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
489 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
490 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
491 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
492 6458efa5 2020-11-24 stsp struct tog_colors colors;
495 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
496 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
497 6458efa5 2020-11-24 stsp struct got_reference *ref;
501 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
503 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
504 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
506 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
507 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
508 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
509 6458efa5 2020-11-24 stsp struct got_repository *repo;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
511 bddb1296 2019-11-08 stsp struct tog_colors colors;
514 ec2a9698 2022-09-15 mark struct tog_help_view_state {
516 ec2a9698 2022-09-15 mark off_t *line_offsets;
517 ec2a9698 2022-09-15 mark size_t nlines;
518 ec2a9698 2022-09-15 mark int lineno;
519 ec2a9698 2022-09-15 mark int first_displayed_line;
520 ec2a9698 2022-09-15 mark int last_displayed_line;
522 ec2a9698 2022-09-15 mark int matched_line;
523 ec2a9698 2022-09-15 mark int selected_line;
525 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
528 ec2a9698 2022-09-15 mark #define GENERATE_HELP \
529 ec2a9698 2022-09-15 mark KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
530 ec2a9698 2022-09-15 mark KEY_("H F1", "Open view-specific help (double tap for all help)"), \
531 ec2a9698 2022-09-15 mark KEY_("k C-p Up", "Move cursor or page up one line"), \
532 ec2a9698 2022-09-15 mark KEY_("j C-n Down", "Move cursor or page down one line"), \
533 ec2a9698 2022-09-15 mark KEY_("C-b b PgUp", "Scroll the view up one page"), \
534 ec2a9698 2022-09-15 mark KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
535 ec2a9698 2022-09-15 mark KEY_("C-u u", "Scroll the view up one half page"), \
536 ec2a9698 2022-09-15 mark KEY_("C-d d", "Scroll the view down one half page"), \
537 0b3f028d 2023-01-09 mark KEY_("g", "Go to line N (default: first line)"), \
538 0b3f028d 2023-01-09 mark KEY_("Home =", "Go to the first line"), \
539 0b3f028d 2023-01-09 mark KEY_("G", "Go to line N (default: last line)"), \
540 0b3f028d 2023-01-09 mark KEY_("End *", "Go to the last line"), \
541 ec2a9698 2022-09-15 mark KEY_("l Right", "Scroll the view right"), \
542 ec2a9698 2022-09-15 mark KEY_("h Left", "Scroll the view left"), \
543 ec2a9698 2022-09-15 mark KEY_("$", "Scroll view to the rightmost position"), \
544 ec2a9698 2022-09-15 mark KEY_("0", "Scroll view to the leftmost position"), \
545 ec2a9698 2022-09-15 mark KEY_("-", "Decrease size of the focussed split"), \
546 ec2a9698 2022-09-15 mark KEY_("+", "Increase size of the focussed split"), \
547 ec2a9698 2022-09-15 mark KEY_("Tab", "Switch focus between views"), \
548 ec2a9698 2022-09-15 mark KEY_("F", "Toggle fullscreen mode"), \
549 ec2a9698 2022-09-15 mark KEY_("/", "Open prompt to enter search term"), \
550 ec2a9698 2022-09-15 mark KEY_("n", "Find next line/token matching the current search term"), \
551 ec2a9698 2022-09-15 mark KEY_("N", "Find previous line/token matching the current search term"),\
552 16a2d4f0 2022-09-19 stsp KEY_("q", "Quit the focussed view; Quit help screen"), \
553 ec2a9698 2022-09-15 mark KEY_("Q", "Quit tog"), \
555 fd6badaa 2022-09-23 stsp KEYMAP_("Log view", TOG_KEYMAP_LOG), \
556 ec2a9698 2022-09-15 mark KEY_("< ,", "Move cursor up one commit"), \
557 ec2a9698 2022-09-15 mark KEY_("> .", "Move cursor down one commit"), \
558 ec2a9698 2022-09-15 mark KEY_("Enter", "Open diff view of the selected commit"), \
559 ec2a9698 2022-09-15 mark KEY_("B", "Reload the log view and toggle display of merged commits"), \
560 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
561 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the repository from the selected" \
562 ec2a9698 2022-09-15 mark " commit"), \
563 ec2a9698 2022-09-15 mark KEY_("@", "Toggle between displaying author and committer name"), \
564 ec2a9698 2022-09-15 mark KEY_("&", "Open prompt to enter term to limit commits displayed"), \
565 ec2a9698 2022-09-15 mark KEY_("C-g Backspace", "Cancel current search or log operation"), \
566 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload the log view with new commits in the repository"), \
568 fd6badaa 2022-09-23 stsp KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
569 ec2a9698 2022-09-15 mark KEY_("K < ,", "Display diff of next line in the file/log entry"), \
570 ec2a9698 2022-09-15 mark KEY_("J > .", "Display diff of previous line in the file/log entry"), \
571 ec2a9698 2022-09-15 mark KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
572 ec2a9698 2022-09-15 mark KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
573 ec2a9698 2022-09-15 mark " data"), \
574 ec2a9698 2022-09-15 mark KEY_("(", "Go to the previous file in the diff"), \
575 ec2a9698 2022-09-15 mark KEY_(")", "Go to the next file in the diff"), \
576 ec2a9698 2022-09-15 mark KEY_("{", "Go to the previous hunk in the diff"), \
577 ec2a9698 2022-09-15 mark KEY_("}", "Go to the next hunk in the diff"), \
578 ec2a9698 2022-09-15 mark KEY_("[", "Decrease the number of context lines"), \
579 ec2a9698 2022-09-15 mark KEY_("]", "Increase the number of context lines"), \
580 ec2a9698 2022-09-15 mark KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
582 fd6badaa 2022-09-23 stsp KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
583 ec2a9698 2022-09-15 mark KEY_("Enter", "Display diff view of the selected line's commit"), \
584 ec2a9698 2022-09-15 mark KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
585 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the currently selected annotated line"), \
586 ec2a9698 2022-09-15 mark KEY_("C", "Reload view with the previously blamed commit"), \
587 ec2a9698 2022-09-15 mark KEY_("c", "Reload view with the version of the file found in the" \
588 ec2a9698 2022-09-15 mark " selected line's commit"), \
589 ec2a9698 2022-09-15 mark KEY_("p", "Reload view with the version of the file found in the" \
590 ec2a9698 2022-09-15 mark " selected line's parent commit"), \
592 fd6badaa 2022-09-23 stsp KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
593 ec2a9698 2022-09-15 mark KEY_("Enter", "Enter selected directory or open blame view of the" \
594 ec2a9698 2022-09-15 mark " selected file"), \
595 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the selected entry"), \
596 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
597 ec2a9698 2022-09-15 mark KEY_("i", "Show object IDs for all tree entries"), \
598 ec2a9698 2022-09-15 mark KEY_("Backspace", "Return to the parent directory"), \
600 fd6badaa 2022-09-23 stsp KEYMAP_("Ref view", TOG_KEYMAP_REF), \
601 ec2a9698 2022-09-15 mark KEY_("Enter", "Display log view of the selected reference"), \
602 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the selected reference"), \
603 ec2a9698 2022-09-15 mark KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
604 ec2a9698 2022-09-15 mark KEY_("m", "Toggle display of last modified date for each reference"), \
605 ec2a9698 2022-09-15 mark KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
606 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload view with all repository references")
608 ec2a9698 2022-09-15 mark struct tog_key_map {
609 ec2a9698 2022-09-15 mark const char *keys;
610 ec2a9698 2022-09-15 mark const char *info;
611 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
615 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
617 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
618 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
619 669b5ffa 2018-10-07 stsp * there is enough screen estate.
621 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
622 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
624 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
625 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
626 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
628 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
629 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
631 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
633 cc3c9aac 2018-08-01 stsp struct tog_view {
634 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
635 26ed57b2 2018-05-19 stsp WINDOW *window;
636 26ed57b2 2018-05-19 stsp PANEL *panel;
637 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
638 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
639 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
640 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
641 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
642 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
643 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
644 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
645 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
647 669b5ffa 2018-10-07 stsp struct tog_view *parent;
648 669b5ffa 2018-10-07 stsp struct tog_view *child;
651 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
652 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
653 e78dc838 2020-12-04 stsp * between parent and child.
654 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
655 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
656 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
657 e78dc838 2020-12-04 stsp * situations.
659 e78dc838 2020-12-04 stsp int focus_child;
661 9b058f45 2022-06-30 mark enum tog_view_mode mode;
662 5dc9f4bc 2018-08-04 stsp /* type-specific state */
663 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
665 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
666 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
667 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
668 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
669 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
670 ec2a9698 2022-09-15 mark struct tog_help_view_state help;
673 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
674 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
675 e78dc838 2020-12-04 stsp struct tog_view *, int);
676 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
677 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
678 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
680 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
681 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
682 eaf2c13e 2022-09-17 mark void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
683 eaf2c13e 2022-09-17 mark int **, int **, int **, int **);
684 c0c4acc8 2021-01-24 stsp int search_started;
685 60493ae3 2019-06-20 stsp int searching;
686 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
687 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
688 60493ae3 2019-06-20 stsp int search_next_done;
689 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
690 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
691 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
692 1803e47f 2019-06-22 stsp regex_t regex;
693 41605754 2020-11-12 stsp regmatch_t regmatch;
694 3f6c6614 2023-01-23 mark const char *action;
697 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
698 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
699 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
700 78756c87 2020-11-24 stsp struct got_repository *);
701 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
702 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
703 e78dc838 2020-12-04 stsp struct tog_view *, int);
704 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
705 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
706 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
707 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
708 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
709 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
711 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
712 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
713 78756c87 2020-11-24 stsp const char *, const char *, int);
714 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
715 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
716 e78dc838 2020-12-04 stsp struct tog_view *, int);
717 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
718 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
719 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
720 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
722 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
723 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
724 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
725 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
726 e78dc838 2020-12-04 stsp struct tog_view *, int);
727 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
728 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
729 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
730 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
731 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
733 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
734 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
735 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
736 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
737 e78dc838 2020-12-04 stsp struct tog_view *, int);
738 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
739 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
740 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
742 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
743 6458efa5 2020-11-24 stsp struct got_repository *);
744 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
745 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
746 e78dc838 2020-12-04 stsp struct tog_view *, int);
747 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
748 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
749 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
751 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
752 eaf2c13e 2022-09-17 mark struct tog_view *);
753 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
754 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
755 eaf2c13e 2022-09-17 mark struct tog_view *, int);
756 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
757 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
758 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
759 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
760 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
762 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
763 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
764 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
765 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
766 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
768 25791caa 2018-10-24 stsp static void
769 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
771 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
774 83baff54 2019-08-12 stsp static void
775 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
777 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
780 61266923 2020-01-14 stsp static void
781 61266923 2020-01-14 stsp tog_sigcont(int signo)
783 61266923 2020-01-14 stsp tog_sigcont_received = 1;
786 2497f032 2022-05-31 stsp static void
787 2497f032 2022-05-31 stsp tog_sigint(int signo)
789 2497f032 2022-05-31 stsp tog_sigint_received = 1;
792 2497f032 2022-05-31 stsp static void
793 2497f032 2022-05-31 stsp tog_sigterm(int signo)
795 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
799 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
801 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
802 f044c841 2022-10-24 stsp tog_sigint_received || tog_sigterm_received);
805 e5a0f69f 2018-08-18 stsp static const struct got_error *
806 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
808 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
810 669b5ffa 2018-10-07 stsp if (view->child) {
811 5629093a 2022-07-11 stsp child_err = view_close(view->child);
812 669b5ffa 2018-10-07 stsp view->child = NULL;
814 e5a0f69f 2018-08-18 stsp if (view->close)
815 e5a0f69f 2018-08-18 stsp err = view->close(view);
816 ea5e7bb5 2018-08-01 stsp if (view->panel)
817 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
818 ea5e7bb5 2018-08-01 stsp if (view->window)
819 ea5e7bb5 2018-08-01 stsp delwin(view->window);
820 ea5e7bb5 2018-08-01 stsp free(view);
821 5629093a 2022-07-11 stsp return err ? err : child_err;
824 ea5e7bb5 2018-08-01 stsp static struct tog_view *
825 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
826 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
828 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
830 ea5e7bb5 2018-08-01 stsp if (view == NULL)
831 ea5e7bb5 2018-08-01 stsp return NULL;
833 d6b05b5b 2018-08-04 stsp view->type = type;
834 f7d12f7e 2018-08-01 stsp view->lines = LINES;
835 f7d12f7e 2018-08-01 stsp view->cols = COLS;
836 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
837 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
838 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
839 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
840 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
841 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
842 96a765a8 2018-08-04 stsp view_close(view);
843 ea5e7bb5 2018-08-01 stsp return NULL;
845 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
846 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
847 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
848 96a765a8 2018-08-04 stsp view_close(view);
849 ea5e7bb5 2018-08-01 stsp return NULL;
852 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
853 ea5e7bb5 2018-08-01 stsp return view;
857 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
859 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
861 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
864 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
866 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
868 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
871 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
873 5c60c32a 2018-10-18 stsp static const struct got_error *
874 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
876 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
878 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
879 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
880 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
882 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
883 9b058f45 2022-06-30 mark view->begin_x = 0;
884 571ccd73 2022-07-19 mark } else if (!view->resized) {
885 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
886 3c1dfe12 2022-07-08 mark view->cols > 119)
887 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
889 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
890 9b058f45 2022-06-30 mark view->begin_y = 0;
892 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
893 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
894 5c60c32a 2018-10-18 stsp view->lines = LINES;
895 5c60c32a 2018-10-18 stsp view->cols = COLS;
896 5c60c32a 2018-10-18 stsp err = view_resize(view);
898 5c60c32a 2018-10-18 stsp return err;
900 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
901 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
903 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
904 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
906 5c60c32a 2018-10-18 stsp return NULL;
909 5c60c32a 2018-10-18 stsp static const struct got_error *
910 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
912 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
914 5c60c32a 2018-10-18 stsp view->begin_x = 0;
915 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
916 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
917 5c60c32a 2018-10-18 stsp view->ncols = COLS;
918 5c60c32a 2018-10-18 stsp view->lines = LINES;
919 5c60c32a 2018-10-18 stsp view->cols = COLS;
920 5c60c32a 2018-10-18 stsp err = view_resize(view);
922 5c60c32a 2018-10-18 stsp return err;
924 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
925 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
927 5c60c32a 2018-10-18 stsp return NULL;
931 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
933 5c60c32a 2018-10-18 stsp return view->parent == NULL;
937 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
939 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
943 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
945 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
949 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
951 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
952 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
955 9b058f45 2022-06-30 mark static void
956 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
958 9b058f45 2022-06-30 mark PANEL *panel;
959 9b058f45 2022-06-30 mark const struct tog_view *view_above;
961 9b058f45 2022-06-30 mark if (view->parent)
962 9b058f45 2022-06-30 mark return view_border(view->parent);
964 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
965 9b058f45 2022-06-30 mark if (panel == NULL)
968 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
969 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
970 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
971 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
972 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
974 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
975 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
978 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
979 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
980 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
981 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
982 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
984 4d8c2215 2018-08-19 stsp static const struct got_error *
985 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
987 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
988 9b058f45 2022-06-30 mark int dif, nlines, ncols;
990 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
992 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
993 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
995 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
996 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
997 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
999 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1001 4dd27a72 2022-06-29 stsp if (view->child) {
1002 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
1004 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1005 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1006 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1007 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1008 0dbbbe90 2022-06-17 op ncols = COLS;
1010 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1011 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1012 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1014 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1016 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1018 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1019 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1022 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1023 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1024 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1027 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1029 9b058f45 2022-06-30 mark return err;
1030 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1031 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1033 9b058f45 2022-06-30 mark return err;
1035 9b058f45 2022-06-30 mark view_border(view);
1036 9b058f45 2022-06-30 mark update_panels();
1037 9b058f45 2022-06-30 mark doupdate();
1038 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1039 9b058f45 2022-06-30 mark nlines = view->nlines;
1041 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1042 0dbbbe90 2022-06-17 op ncols = COLS;
1044 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1045 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1047 571ccd73 2022-07-19 mark return err;
1050 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1051 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1052 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1053 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1054 0dbbbe90 2022-06-17 op wclear(view->window);
1056 0dbbbe90 2022-06-17 op view->nlines = nlines;
1057 0dbbbe90 2022-06-17 op view->ncols = ncols;
1058 0dbbbe90 2022-06-17 op view->lines = LINES;
1059 0dbbbe90 2022-06-17 op view->cols = COLS;
1061 5c60c32a 2018-10-18 stsp return NULL;
1064 571ccd73 2022-07-19 mark static const struct got_error *
1065 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1067 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1068 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1069 6fe51fee 2022-07-22 mark int n = 0;
1071 6fe51fee 2022-07-22 mark if (s->selected_entry)
1072 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1075 571ccd73 2022-07-19 mark * Request commits to account for the increased
1076 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1078 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1079 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1080 571ccd73 2022-07-19 mark err = request_log_commits(view);
1083 571ccd73 2022-07-19 mark return err;
1086 d9a7ab53 2022-07-11 mark static void
1087 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1089 d9a7ab53 2022-07-11 mark if (n == 0)
1092 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1093 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1094 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1096 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1097 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1098 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1099 d9a7ab53 2022-07-11 mark view->offset -= n;
1101 d9a7ab53 2022-07-11 mark view->offset = 0;
1105 3c1dfe12 2022-07-08 mark static const struct got_error *
1106 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1108 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1109 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1111 3c1dfe12 2022-07-08 mark if (view->parent)
1112 3c1dfe12 2022-07-08 mark v = view->parent;
1116 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1117 3c1dfe12 2022-07-08 mark return NULL;
1119 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1121 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1122 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1123 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1124 3c1dfe12 2022-07-08 mark if (view->parent)
1125 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1127 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1128 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1129 3c1dfe12 2022-07-08 mark view->count = 0;
1130 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1131 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1132 3c1dfe12 2022-07-08 mark view->count = 0;
1133 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1135 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1136 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1137 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1138 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1140 3c1dfe12 2022-07-08 mark return err;
1141 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1143 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1144 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1145 3c1dfe12 2022-07-08 mark if (view->parent)
1146 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1148 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1149 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1150 3c1dfe12 2022-07-08 mark view->count = 0;
1151 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1152 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1153 3c1dfe12 2022-07-08 mark view->count = 0;
1154 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1156 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1159 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1160 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1161 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1162 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1164 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1166 3c1dfe12 2022-07-08 mark return err;
1167 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1169 3c1dfe12 2022-07-08 mark return err;
1171 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1172 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1174 3c1dfe12 2022-07-08 mark return err;
1177 6fe51fee 2022-07-22 mark if (v->resize)
1178 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1179 6fe51fee 2022-07-22 mark else if (v->child->resize)
1180 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1182 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1184 3c1dfe12 2022-07-08 mark return err;
1187 3c1dfe12 2022-07-08 mark static void
1188 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1190 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1192 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1193 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1196 669b5ffa 2018-10-07 stsp static const struct got_error *
1197 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1199 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1201 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1202 669b5ffa 2018-10-07 stsp return NULL;
1204 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1205 669b5ffa 2018-10-07 stsp view->child = NULL;
1206 669b5ffa 2018-10-07 stsp return err;
1209 0dbbbe90 2022-06-17 op static const struct got_error *
1210 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1212 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1214 669b5ffa 2018-10-07 stsp view->child = child;
1215 669b5ffa 2018-10-07 stsp child->parent = view;
1217 3c1dfe12 2022-07-08 mark err = view_resize(view);
1219 3c1dfe12 2022-07-08 mark return err;
1221 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1222 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1224 3c1dfe12 2022-07-08 mark return err;
1227 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1228 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1230 136e2bd4 2022-07-23 mark static const struct got_error *
1231 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1232 136e2bd4 2022-07-23 mark enum tog_view_type request)
1234 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1235 136e2bd4 2022-07-23 mark const struct got_error *err;
1236 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1238 136e2bd4 2022-07-23 mark *requested = NULL;
1240 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1241 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1243 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1245 136e2bd4 2022-07-23 mark return err;
1247 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1248 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1249 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1251 136e2bd4 2022-07-23 mark return err;
1254 136e2bd4 2022-07-23 mark view->focussed = 0;
1255 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1256 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1257 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1258 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1260 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1261 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1262 136e2bd4 2022-07-23 mark err = view_close_child(view);
1264 136e2bd4 2022-07-23 mark return err;
1265 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1267 136e2bd4 2022-07-23 mark return err;
1268 136e2bd4 2022-07-23 mark view->focus_child = 1;
1270 136e2bd4 2022-07-23 mark *requested = new_view;
1272 136e2bd4 2022-07-23 mark return NULL;
1275 34bc9ec9 2019-02-22 stsp static void
1276 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1278 25791caa 2018-10-24 stsp int cols, lines;
1279 25791caa 2018-10-24 stsp struct winsize size;
1281 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1282 25791caa 2018-10-24 stsp cols = 80; /* Default */
1283 25791caa 2018-10-24 stsp lines = 24;
1285 25791caa 2018-10-24 stsp cols = size.ws_col;
1286 25791caa 2018-10-24 stsp lines = size.ws_row;
1288 25791caa 2018-10-24 stsp resize_term(lines, cols);
1291 2b49a8ae 2019-06-22 stsp static const struct got_error *
1292 602eda79 2023-02-09 mark view_search_start(struct tog_view *view, int fast_refresh)
1294 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1295 9b058f45 2022-06-30 mark struct tog_view *v = view;
1296 2b49a8ae 2019-06-22 stsp char pattern[1024];
1299 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1300 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1301 c0c4acc8 2021-01-24 stsp view->searching = 0;
1302 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1304 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1306 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1307 2b49a8ae 2019-06-22 stsp return NULL;
1309 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1310 9b058f45 2022-06-30 mark v = view->child;
1311 9d0feb8b 2022-12-27 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1312 9d0feb8b 2022-12-27 mark v = view->parent;
1314 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1315 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1317 092a9f9c 2022-12-27 mark nodelay(v->window, FALSE); /* block for search term input */
1318 2b49a8ae 2019-06-22 stsp nocbreak();
1320 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1321 9b058f45 2022-06-30 mark wrefresh(v->window);
1324 092a9f9c 2022-12-27 mark nodelay(v->window, TRUE);
1325 602eda79 2023-02-09 mark if (!fast_refresh)
1326 602eda79 2023-02-09 mark halfdelay(10);
1327 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1328 2b49a8ae 2019-06-22 stsp return NULL;
1330 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1331 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1332 7c32bd05 2019-06-22 stsp if (err) {
1333 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1334 7c32bd05 2019-06-22 stsp return err;
1336 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1337 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1338 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1339 2b49a8ae 2019-06-22 stsp view->search_next(view);
1342 2b49a8ae 2019-06-22 stsp return NULL;
1345 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1346 d2366e29 2022-07-07 mark static const struct got_error *
1347 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1349 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1350 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1352 d2366e29 2022-07-07 mark if (view->parent)
1353 d2366e29 2022-07-07 mark v = view->parent;
1357 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1358 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1360 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1362 7532ccda 2022-07-11 mark if (!v->child)
1363 7532ccda 2022-07-11 mark return NULL;
1364 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1365 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1367 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1368 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1369 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1370 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1371 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1374 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1375 d2366e29 2022-07-07 mark v->ncols = COLS;
1376 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1377 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1379 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1381 d2366e29 2022-07-07 mark return err;
1383 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1384 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1385 d2366e29 2022-07-07 mark v->focus_child = 1;
1387 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1389 d2366e29 2022-07-07 mark return err;
1390 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1392 d2366e29 2022-07-07 mark return err;
1394 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1395 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1396 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1397 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1399 279d2047 2022-07-29 stsp return err;
1400 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1402 279d2047 2022-07-29 stsp return err;
1404 7532ccda 2022-07-11 mark offset_selection_up(v);
1405 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1407 dff91825 2022-07-22 mark if (v->resize)
1408 dff91825 2022-07-22 mark err = v->resize(v, 0);
1409 dff91825 2022-07-22 mark else if (v->child->resize)
1410 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1412 d2366e29 2022-07-07 mark return err;
1416 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1417 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1419 640cd7ff 2022-06-22 mark static int
1420 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1422 9b058f45 2022-06-30 mark struct tog_view *v = view;
1423 9b058f45 2022-06-30 mark int x, n = 0;
1425 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1426 9b058f45 2022-06-30 mark v = view->child;
1427 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1428 9b058f45 2022-06-30 mark v = view->parent;
1430 640cd7ff 2022-06-22 mark view->count = 0;
1431 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1432 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1433 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1434 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1435 9b058f45 2022-06-30 mark waddch(v->window, ':');
1438 9b058f45 2022-06-30 mark x = getcurx(v->window);
1439 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1440 9b058f45 2022-06-30 mark waddch(v->window, c);
1441 9b058f45 2022-06-30 mark wrefresh(v->window);
1445 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1446 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1448 640cd7ff 2022-06-22 mark if (n >= 9999999)
1449 640cd7ff 2022-06-22 mark n = 9999999;
1451 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1452 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1454 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1455 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1460 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1461 640cd7ff 2022-06-22 mark view->count = n;
1466 3f6c6614 2023-01-23 mark static void
1467 3f6c6614 2023-01-23 mark action_report(struct tog_view *view)
1469 3f6c6614 2023-01-23 mark struct tog_view *v = view;
1471 3f6c6614 2023-01-23 mark if (view_is_hsplit_top(view))
1472 3f6c6614 2023-01-23 mark v = view->child;
1473 3f6c6614 2023-01-23 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1474 3f6c6614 2023-01-23 mark v = view->parent;
1476 3f6c6614 2023-01-23 mark wmove(v->window, v->nlines - 1, 0);
1477 3f6c6614 2023-01-23 mark wclrtoeol(v->window);
1478 3f6c6614 2023-01-23 mark wprintw(v->window, ":%s", view->action);
1479 3f6c6614 2023-01-23 mark wrefresh(v->window);
1482 3f6c6614 2023-01-23 mark * Clear action status report. Only clear in blame view
1483 3f6c6614 2023-01-23 mark * once annotating is complete, otherwise it's too fast.
1485 3f6c6614 2023-01-23 mark if (view->type == TOG_VIEW_BLAME) {
1486 3f6c6614 2023-01-23 mark if (view->state.blame.blame_complete)
1487 3f6c6614 2023-01-23 mark view->action = NULL;
1489 3f6c6614 2023-01-23 mark view->action = NULL;
1492 0cf4efb1 2018-09-29 stsp static const struct got_error *
1493 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1494 602eda79 2023-02-09 mark struct tog_view_list_head *views, int fast_refresh)
1496 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1497 669b5ffa 2018-10-07 stsp struct tog_view *v;
1498 1a76625f 2018-10-22 stsp int ch, errcode;
1500 e5a0f69f 2018-08-18 stsp *new = NULL;
1502 3f6c6614 2023-01-23 mark if (view->action)
1503 3f6c6614 2023-01-23 mark action_report(view);
1505 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1506 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1507 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1508 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1509 640cd7ff 2022-06-22 mark view->count = 0;
1512 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1513 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1514 82954512 2020-02-03 stsp if (errcode)
1515 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1516 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1517 3da8ef85 2021-09-21 stsp sched_yield();
1518 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1519 82954512 2020-02-03 stsp if (errcode)
1520 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1521 82954512 2020-02-03 stsp "pthread_mutex_lock");
1522 60493ae3 2019-06-20 stsp view->search_next(view);
1523 60493ae3 2019-06-20 stsp return NULL;
1526 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1527 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1528 1a76625f 2018-10-22 stsp if (errcode)
1529 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1530 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1531 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1533 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1534 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1535 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1536 a6d37fac 2022-07-03 mark view->count = 0;
1538 a6d37fac 2022-07-03 mark ch = view->ch;
1540 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1541 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1542 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1544 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1545 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1546 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1547 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1548 1a76625f 2018-10-22 stsp if (errcode)
1549 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1551 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1552 25791caa 2018-10-24 stsp tog_resizeterm();
1553 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1554 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1555 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1556 25791caa 2018-10-24 stsp err = view_resize(v);
1558 25791caa 2018-10-24 stsp return err;
1559 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1561 25791caa 2018-10-24 stsp return err;
1562 cdfcfb03 2020-12-06 stsp if (v->child) {
1563 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1565 cdfcfb03 2020-12-06 stsp return err;
1566 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1567 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1569 cdfcfb03 2020-12-06 stsp return err;
1570 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1571 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1573 3c1dfe12 2022-07-08 mark return err;
1579 e5a0f69f 2018-08-18 stsp switch (ch) {
1582 ec2a9698 2022-09-15 mark case KEY_F(1):
1583 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1584 ec2a9698 2022-09-15 mark err = view->reset(view);
1586 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1589 640cd7ff 2022-06-22 mark view->count = 0;
1590 1e37a5c2 2019-05-12 jcs if (view->child) {
1591 e78dc838 2020-12-04 stsp view->focussed = 0;
1592 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1593 e78dc838 2020-12-04 stsp view->focus_child = 1;
1594 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1595 e78dc838 2020-12-04 stsp view->focussed = 0;
1596 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1597 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1598 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1599 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1600 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1603 9b058f45 2022-06-30 mark return err;
1605 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1606 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1608 9b058f45 2022-06-30 mark return err;
1613 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1614 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1615 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1616 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1620 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1622 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1623 9970f7fc 2020-12-03 stsp view->dying = 1;
1629 640cd7ff 2022-06-22 mark view->count = 0;
1630 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1631 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1633 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1634 e78dc838 2020-12-04 stsp view->focussed = 0;
1635 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1636 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1638 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1640 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1644 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1645 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1647 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1648 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1649 e78dc838 2020-12-04 stsp view->focussed = 1;
1650 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1652 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1653 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1654 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1656 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1660 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1664 6fe51fee 2022-07-22 mark if (view->resize) {
1665 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1669 9b058f45 2022-06-30 mark if (view->parent)
1670 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1672 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1675 3c1dfe12 2022-07-08 mark view->count = 0;
1676 d2366e29 2022-07-07 mark err = switch_split(view);
1679 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1682 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1684 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1687 640cd7ff 2022-06-22 mark view->count = 0;
1688 60493ae3 2019-06-20 stsp if (view->search_start)
1689 602eda79 2023-02-09 mark view_search_start(view, fast_refresh);
1691 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1695 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1696 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1697 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1698 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1699 60493ae3 2019-06-20 stsp view->search_next(view);
1701 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1704 3f6c6614 2023-01-23 mark if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1705 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1706 3f6c6614 2023-01-23 mark view->action = "Patience diff algorithm";
1708 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1709 3f6c6614 2023-01-23 mark view->action = "Myers diff algorithm";
1711 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1712 917d79a7 2022-07-01 stsp if (v->reset) {
1713 917d79a7 2022-07-01 stsp err = v->reset(v);
1715 917d79a7 2022-07-01 stsp return err;
1717 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1718 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1720 917d79a7 2022-07-01 stsp return err;
1725 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1729 e5a0f69f 2018-08-18 stsp return err;
1733 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1735 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1736 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1738 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1740 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1743 669b5ffa 2018-10-07 stsp return view->focussed;
1746 bcbd79e2 2018-08-19 stsp static const struct got_error *
1747 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1749 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1750 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1751 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1752 d2366e29 2022-07-07 mark char *mode;
1753 fd823528 2018-10-22 stsp int fast_refresh = 10;
1754 1a76625f 2018-10-22 stsp int done = 0, errcode;
1756 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1757 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1758 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1760 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1762 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1763 1a76625f 2018-10-22 stsp if (errcode)
1764 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1766 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1767 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1769 1004088d 2018-09-29 stsp view->focussed = 1;
1770 878940b7 2018-09-29 stsp err = view->show(view);
1772 0cf4efb1 2018-09-29 stsp return err;
1773 0cf4efb1 2018-09-29 stsp update_panels();
1774 0cf4efb1 2018-09-29 stsp doupdate();
1775 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1776 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1777 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1778 388b24d6 2023-01-23 mark if (fast_refresh && --fast_refresh == 0)
1779 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1781 602eda79 2023-02-09 mark err = view_input(&new_view, &done, view, &views, fast_refresh);
1785 dc2c3344 2023-01-23 mark if (view->dying && view == TAILQ_FIRST(&views) &&
1786 dc2c3344 2023-01-23 mark TAILQ_NEXT(view, entry) == NULL)
1788 dc2c3344 2023-01-23 mark if (done) {
1789 dc2c3344 2023-01-23 mark struct tog_view *v;
1792 dc2c3344 2023-01-23 mark * When we quit, scroll the screen up a single line
1793 dc2c3344 2023-01-23 mark * so we don't lose any information.
1795 dc2c3344 2023-01-23 mark TAILQ_FOREACH(v, &views, entry) {
1796 dc2c3344 2023-01-23 mark wmove(v->window, 0, 0);
1797 dc2c3344 2023-01-23 mark wdeleteln(v->window);
1798 dc2c3344 2023-01-23 mark wnoutrefresh(v->window);
1799 dc2c3344 2023-01-23 mark if (v->child && !view_is_fullscreen(v)) {
1800 dc2c3344 2023-01-23 mark wmove(v->child->window, 0, 0);
1801 dc2c3344 2023-01-23 mark wdeleteln(v->child->window);
1802 dc2c3344 2023-01-23 mark wnoutrefresh(v->child->window);
1805 dc2c3344 2023-01-23 mark doupdate();
1808 9970f7fc 2020-12-03 stsp if (view->dying) {
1809 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1811 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1812 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1814 e78dc838 2020-12-04 stsp else if (view->parent)
1815 669b5ffa 2018-10-07 stsp prev = view->parent;
1817 e78dc838 2020-12-04 stsp if (view->parent) {
1818 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1819 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1820 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1821 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1822 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1825 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1826 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1828 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1830 9970f7fc 2020-12-03 stsp err = view_close(view);
1832 e5a0f69f 2018-08-18 stsp goto done;
1834 e78dc838 2020-12-04 stsp view = NULL;
1835 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1836 e78dc838 2020-12-04 stsp if (v->focussed)
1839 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1840 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1842 e78dc838 2020-12-04 stsp view = prev;
1843 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1844 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1845 e78dc838 2020-12-04 stsp tog_view_list_head);
1847 e78dc838 2020-12-04 stsp if (view) {
1848 e78dc838 2020-12-04 stsp if (view->focus_child) {
1849 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1850 e78dc838 2020-12-04 stsp view = view->child;
1852 e78dc838 2020-12-04 stsp view->focussed = 1;
1856 bcbd79e2 2018-08-19 stsp if (new_view) {
1857 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1858 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1859 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1860 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1862 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1863 86c66b02 2018-10-18 stsp err = view_close(v);
1865 86c66b02 2018-10-18 stsp goto done;
1868 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1869 fed7eaa8 2018-10-24 stsp view = new_view;
1871 dc2c3344 2023-01-23 mark if (view && !done) {
1872 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1873 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1874 e78dc838 2020-12-04 stsp view = view->child;
1876 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1877 e78dc838 2020-12-04 stsp view = view->parent;
1879 e78dc838 2020-12-04 stsp show_panel(view->panel);
1880 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1881 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1882 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1883 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1885 1a76625f 2018-10-22 stsp goto done;
1887 669b5ffa 2018-10-07 stsp err = view->show(view);
1889 1a76625f 2018-10-22 stsp goto done;
1890 669b5ffa 2018-10-07 stsp if (view->child) {
1891 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1893 1a76625f 2018-10-22 stsp goto done;
1895 1a76625f 2018-10-22 stsp update_panels();
1896 1a76625f 2018-10-22 stsp doupdate();
1900 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1901 5629093a 2022-07-11 stsp const struct got_error *close_err;
1902 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1903 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1904 5629093a 2022-07-11 stsp close_err = view_close(view);
1905 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1906 5629093a 2022-07-11 stsp err = close_err;
1909 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1910 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1911 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1913 e5a0f69f 2018-08-18 stsp return err;
1916 4ed7e80c 2018-05-20 stsp __dead static void
1917 9f7d7167 2018-04-29 stsp usage_log(void)
1920 c70c5802 2018-08-01 stsp fprintf(stderr,
1921 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1922 9f7d7167 2018-04-29 stsp getprogname());
1926 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1927 80ddbec8 2018-04-29 stsp static const struct got_error *
1928 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1930 00dfcb92 2018-06-11 stsp char *vis = NULL;
1931 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1933 963b370f 2018-05-20 stsp *ws = NULL;
1934 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1935 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1936 00dfcb92 2018-06-11 stsp int vislen;
1937 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1938 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1940 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1941 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1943 00dfcb92 2018-06-11 stsp return err;
1944 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1945 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1946 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1947 a7f50699 2018-06-11 stsp goto done;
1951 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1952 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1953 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1954 a7f50699 2018-06-11 stsp goto done;
1957 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1958 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1960 00dfcb92 2018-06-11 stsp free(vis);
1961 963b370f 2018-05-20 stsp if (err) {
1962 963b370f 2018-05-20 stsp free(*ws);
1963 963b370f 2018-05-20 stsp *ws = NULL;
1964 963b370f 2018-05-20 stsp *wlen = 0;
1966 963b370f 2018-05-20 stsp return err;
1969 145b6838 2022-06-16 stsp static const struct got_error *
1970 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1972 145b6838 2022-06-16 stsp char *dst;
1973 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1975 145b6838 2022-06-16 stsp *ptr = NULL;
1976 145b6838 2022-06-16 stsp n = len = strlen(src);
1977 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1978 145b6838 2022-06-16 stsp if (dst == NULL)
1979 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1981 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1982 145b6838 2022-06-16 stsp const char c = src[idx];
1984 145b6838 2022-06-16 stsp if (c == '\t') {
1985 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1988 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1989 6e1c41ad 2022-06-16 mark if (p == NULL) {
1990 6e1c41ad 2022-06-16 mark free(dst);
1991 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1996 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1999 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
2003 145b6838 2022-06-16 stsp dst[sz] = '\0';
2004 145b6838 2022-06-16 stsp *ptr = dst;
2005 145b6838 2022-06-16 stsp return NULL;
2009 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
2010 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
2011 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
2015 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2017 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
2019 4e4a9ac8 2022-06-17 op if (n == 0) {
2020 4e4a9ac8 2022-06-17 op *rcol = cols;
2024 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
2025 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
2026 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2028 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
2030 4e4a9ac8 2022-06-17 op if (width == -1) {
2032 4e4a9ac8 2022-06-17 op wline[i] = L'.';
2035 4e4a9ac8 2022-06-17 op if (cols + width > n)
2037 4e4a9ac8 2022-06-17 op cols += width;
2040 4e4a9ac8 2022-06-17 op *rcol = cols;
2045 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
2046 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
2047 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2049 ccda2f4d 2022-06-16 stsp static const struct got_error *
2050 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2051 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2053 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2055 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2056 145b6838 2022-06-16 stsp char *exstr = NULL;
2057 963b370f 2018-05-20 stsp size_t wlen;
2058 4e4a9ac8 2022-06-17 op int i, scrollx;
2060 963b370f 2018-05-20 stsp *wlinep = NULL;
2061 b700b5d6 2018-07-10 stsp *widthp = 0;
2063 145b6838 2022-06-16 stsp if (expand) {
2064 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2066 145b6838 2022-06-16 stsp return err;
2069 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2070 145b6838 2022-06-16 stsp free(exstr);
2072 963b370f 2018-05-20 stsp return err;
2074 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2076 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2077 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2080 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2081 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2085 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2086 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2088 b700b5d6 2018-07-10 stsp if (widthp)
2089 b700b5d6 2018-07-10 stsp *widthp = cols;
2090 ccda2f4d 2022-06-16 stsp if (scrollxp)
2091 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2093 963b370f 2018-05-20 stsp free(wline);
2095 963b370f 2018-05-20 stsp *wlinep = wline;
2096 963b370f 2018-05-20 stsp return err;
2099 8b473291 2019-02-21 stsp static const struct got_error*
2100 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2101 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2103 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2104 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2106 8b473291 2019-02-21 stsp const char *name;
2108 8b473291 2019-02-21 stsp *refs_str = NULL;
2110 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2111 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2112 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2115 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2116 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2118 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2119 8b473291 2019-02-21 stsp name += 5;
2120 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2121 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2123 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2124 8b473291 2019-02-21 stsp name += 6;
2125 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2126 8b473291 2019-02-21 stsp name += 8;
2127 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2128 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2131 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2134 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2135 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2136 5d844a1e 2019-08-13 stsp if (err) {
2137 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2138 48cae60d 2020-09-22 stsp free(ref_id);
2141 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2142 5d844a1e 2019-08-13 stsp err = NULL;
2143 5d844a1e 2019-08-13 stsp tag = NULL;
2146 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2147 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2148 48cae60d 2020-09-22 stsp free(ref_id);
2150 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2151 52b5abe1 2019-08-13 stsp if (cmp != 0)
2153 8b473291 2019-02-21 stsp s = *refs_str;
2154 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2155 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2156 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2158 8b473291 2019-02-21 stsp *refs_str = NULL;
2164 8b473291 2019-02-21 stsp return err;
2167 963b370f 2018-05-20 stsp static const struct got_error *
2168 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2169 27a741e5 2019-09-11 stsp int col_tab_align)
2171 e6b8b890 2020-12-29 naddy char *smallerthan;
2173 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2174 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2175 5813d178 2019-03-09 stsp author = smallerthan + 1;
2176 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2177 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2178 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2181 5813d178 2019-03-09 stsp static const struct got_error *
2182 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2183 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2184 8fdc79fe 2020-12-01 naddy int author_display_cols)
2186 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2187 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2188 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2189 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2190 5813d178 2019-03-09 stsp char *author = NULL;
2191 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2192 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2193 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2194 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2195 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2196 ccb26ccd 2018-11-05 stsp struct tm tm;
2197 45d799e2 2018-12-23 stsp time_t committer_time;
2198 11b20872 2019-11-08 stsp struct tog_color *tc;
2200 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2201 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2202 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2203 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2204 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2206 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2207 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2209 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2210 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2212 11b20872 2019-11-08 stsp wattr_on(view->window,
2213 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2214 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2216 11b20872 2019-11-08 stsp wattr_off(view->window,
2217 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2218 27a741e5 2019-09-11 stsp col = limit;
2219 b39d25c7 2018-07-10 stsp if (col > avail)
2220 b39d25c7 2018-07-10 stsp goto done;
2222 6570a66d 2019-11-08 stsp if (avail >= 120) {
2223 6570a66d 2019-11-08 stsp char *id_str;
2224 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2226 6570a66d 2019-11-08 stsp goto done;
2227 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2229 11b20872 2019-11-08 stsp wattr_on(view->window,
2230 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2231 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2233 11b20872 2019-11-08 stsp wattr_off(view->window,
2234 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2235 6570a66d 2019-11-08 stsp free(id_str);
2237 6570a66d 2019-11-08 stsp if (col > avail)
2238 6570a66d 2019-11-08 stsp goto done;
2241 10aab77f 2022-07-19 op if (s->use_committer)
2242 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2244 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2245 5813d178 2019-03-09 stsp if (author == NULL) {
2246 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2247 80ddbec8 2018-04-29 stsp goto done;
2249 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2251 bb737323 2018-05-20 stsp goto done;
2252 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2254 11b20872 2019-11-08 stsp wattr_on(view->window,
2255 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2256 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2257 bb737323 2018-05-20 stsp col += author_width;
2258 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2259 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2261 bb737323 2018-05-20 stsp author_width++;
2264 f0f62654 2022-09-08 mark wattr_off(view->window,
2265 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2266 9c2eaf34 2018-05-20 stsp if (col > avail)
2267 9c2eaf34 2018-05-20 stsp goto done;
2269 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2271 6d9fbc00 2018-04-29 stsp goto done;
2272 bb737323 2018-05-20 stsp logmsg = logmsg0;
2273 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2275 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2276 bb737323 2018-05-20 stsp if (newline)
2277 bb737323 2018-05-20 stsp *newline = '\0';
2278 ccda2f4d 2022-06-16 stsp limit = avail - col;
2279 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2280 4d1f6af3 2022-06-17 op limit--; /* for the border */
2281 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2282 ccda2f4d 2022-06-16 stsp limit, col, 1);
2284 29688b02 2022-06-16 stsp goto done;
2285 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2286 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2287 27a741e5 2019-09-11 stsp while (col < avail) {
2288 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2292 80ddbec8 2018-04-29 stsp free(logmsg0);
2293 bb737323 2018-05-20 stsp free(wlogmsg);
2294 5813d178 2019-03-09 stsp free(author);
2295 bb737323 2018-05-20 stsp free(wauthor);
2296 80ddbec8 2018-04-29 stsp free(line);
2297 80ddbec8 2018-04-29 stsp return err;
2300 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2301 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2302 899d86c2 2018-05-10 stsp struct got_object_id *id)
2304 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2305 e15c42de 2022-09-05 op struct got_object_id *dup;
2307 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2308 80ddbec8 2018-04-29 stsp if (entry == NULL)
2309 899d86c2 2018-05-10 stsp return NULL;
2311 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2312 e15c42de 2022-09-05 op if (dup == NULL) {
2313 e15c42de 2022-09-05 op free(entry);
2314 e15c42de 2022-09-05 op return NULL;
2317 e15c42de 2022-09-05 op entry->id = dup;
2318 99db9666 2018-05-07 stsp entry->commit = commit;
2319 899d86c2 2018-05-10 stsp return entry;
2322 99db9666 2018-05-07 stsp static void
2323 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2325 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2327 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2328 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2329 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2330 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2331 e15c42de 2022-09-05 op free(entry->id);
2332 99db9666 2018-05-07 stsp free(entry);
2335 99db9666 2018-05-07 stsp static void
2336 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2338 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2339 99db9666 2018-05-07 stsp pop_commit(commits);
2342 c4972b91 2018-05-07 stsp static const struct got_error *
2343 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2344 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2346 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2347 13add988 2019-10-15 stsp regmatch_t regmatch;
2348 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2350 13add988 2019-10-15 stsp *have_match = 0;
2352 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2354 13add988 2019-10-15 stsp return err;
2356 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2358 13add988 2019-10-15 stsp goto done;
2360 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2361 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2362 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2363 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2364 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2365 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2366 13add988 2019-10-15 stsp *have_match = 1;
2368 13add988 2019-10-15 stsp free(id_str);
2369 13add988 2019-10-15 stsp free(logmsg);
2370 13add988 2019-10-15 stsp return err;
2373 13add988 2019-10-15 stsp static const struct got_error *
2374 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2376 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2379 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2380 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2381 1a76625f 2018-10-22 stsp * while updating the display.
2384 d9787ed8 2022-09-10 op struct got_object_id id;
2385 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2386 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2387 568eae95 2022-09-11 mark int limit_match = 0;
2388 1a76625f 2018-10-22 stsp int errcode;
2390 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2391 4e0d2870 2020-12-07 naddy NULL, NULL);
2395 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2398 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2399 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2400 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2404 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2405 1a76625f 2018-10-22 stsp if (errcode) {
2406 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2407 13add988 2019-10-15 stsp "pthread_mutex_lock");
2411 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2412 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2413 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2415 568eae95 2022-09-11 mark if (*a->limiting) {
2416 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2417 568eae95 2022-09-11 mark a->limit_regex);
2421 568eae95 2022-09-11 mark if (limit_match) {
2422 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2424 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2425 568eae95 2022-09-11 mark entry->commit, entry->id);
2426 568eae95 2022-09-11 mark if (matched == NULL) {
2427 568eae95 2022-09-11 mark err = got_error_from_errno(
2428 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2431 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2432 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2434 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2435 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2436 568eae95 2022-09-11 mark matched, entry);
2437 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2441 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2442 568eae95 2022-09-11 mark * have found a match, and that it should be
2443 568eae95 2022-09-11 mark * counted as a new entry for the view.
2445 568eae95 2022-09-11 mark a->limit_match = limit_match;
2448 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2449 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2450 7c1452c1 2020-03-26 stsp int have_match;
2451 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2455 568eae95 2022-09-11 mark if (*a->limiting) {
2456 568eae95 2022-09-11 mark if (limit_match && have_match)
2457 568eae95 2022-09-11 mark *a->search_next_done =
2458 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2459 568eae95 2022-09-11 mark } else if (have_match)
2460 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2463 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2464 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2465 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2466 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2469 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2471 9ba79e04 2018-06-11 stsp return err;
2474 2b779855 2020-12-05 naddy static void
2475 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2477 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2478 2b779855 2020-12-05 naddy int ncommits = 0;
2480 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2481 2b779855 2020-12-05 naddy while (entry) {
2482 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2483 2b779855 2020-12-05 naddy s->selected_entry = entry;
2486 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2487 2b779855 2020-12-05 naddy ncommits++;
2491 0553a4e3 2018-04-30 stsp static const struct got_error *
2492 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2494 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2495 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2496 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2497 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2498 60493ae3 2019-06-20 stsp int width;
2499 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2500 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2501 8b473291 2019-02-21 stsp char *refs_str = NULL;
2502 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2503 11b20872 2019-11-08 stsp struct tog_color *tc;
2504 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2506 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2507 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2509 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2510 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2511 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2512 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2514 ecb28ae0 2018-07-16 stsp return err;
2515 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2516 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2517 d2075bf3 2020-12-25 stsp if (refs) {
2518 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2519 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2521 d2075bf3 2020-12-25 stsp goto done;
2525 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2526 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2528 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2529 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2530 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2531 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2532 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2533 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2534 8f4ed634 2020-03-26 stsp goto done;
2537 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2538 568eae95 2022-09-11 mark const char *limit_str = NULL;
2540 f9686aa5 2020-03-27 stsp if (view->searching) {
2541 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2542 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2543 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2544 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2545 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2546 f9686aa5 2020-03-27 stsp search_str = "searching...";
2549 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2550 568eae95 2022-09-11 mark limit_str = "no matches found";
2552 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2553 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2554 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2555 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2556 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2557 8f4ed634 2020-03-26 stsp goto done;
2561 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2562 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2563 87411fa9 2022-06-16 stsp "........................................",
2564 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2566 1a76625f 2018-10-22 stsp header = NULL;
2567 1a76625f 2018-10-22 stsp goto done;
2569 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2570 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2571 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2572 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2573 1a76625f 2018-10-22 stsp header = NULL;
2574 1a76625f 2018-10-22 stsp goto done;
2576 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2578 1a76625f 2018-10-22 stsp goto done;
2580 2814baeb 2018-08-01 stsp werase(view->window);
2582 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2583 a3404814 2018-09-02 stsp wstandout(view->window);
2584 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2586 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2587 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2588 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2589 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2593 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2594 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2595 a3404814 2018-09-02 stsp wstandend(view->window);
2596 ecb28ae0 2018-07-16 stsp free(wline);
2597 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2598 1a76625f 2018-10-22 stsp goto done;
2600 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2601 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2602 5813d178 2019-03-09 stsp ncommits = 0;
2603 145b6838 2022-06-16 stsp view->maxx = 0;
2604 5813d178 2019-03-09 stsp while (entry) {
2605 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2606 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2607 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2608 5813d178 2019-03-09 stsp int width;
2609 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2611 10aab77f 2022-07-19 op if (s->use_committer)
2612 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2614 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2615 5813d178 2019-03-09 stsp if (author == NULL) {
2616 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2617 5813d178 2019-03-09 stsp goto done;
2619 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2620 27a741e5 2019-09-11 stsp date_display_cols);
2621 5813d178 2019-03-09 stsp if (author_cols < width)
2622 5813d178 2019-03-09 stsp author_cols = width;
2623 5813d178 2019-03-09 stsp free(wauthor);
2624 5813d178 2019-03-09 stsp free(author);
2625 a310d9c3 2022-07-21 florian if (err)
2626 a310d9c3 2022-07-21 florian goto done;
2627 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2629 145b6838 2022-06-16 stsp goto done;
2630 145b6838 2022-06-16 stsp msg = msg0;
2631 145b6838 2022-06-16 stsp while (*msg == '\n')
2633 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2634 29688b02 2022-06-16 stsp *eol = '\0';
2635 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2636 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2638 29688b02 2022-06-16 stsp goto done;
2639 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2640 145b6838 2022-06-16 stsp free(msg0);
2641 29688b02 2022-06-16 stsp free(wmsg);
2642 7ca04879 2019-10-19 stsp ncommits++;
2643 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2646 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2647 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2648 867c6645 2018-07-10 stsp ncommits = 0;
2649 899d86c2 2018-05-10 stsp while (entry) {
2650 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2652 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2653 2814baeb 2018-08-01 stsp wstandout(view->window);
2654 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2655 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2656 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2657 2814baeb 2018-08-01 stsp wstandend(view->window);
2659 60493ae3 2019-06-20 stsp goto done;
2660 0553a4e3 2018-04-30 stsp ncommits++;
2661 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2662 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2665 9b058f45 2022-06-30 mark view_border(view);
2667 1a76625f 2018-10-22 stsp free(id_str);
2668 8b473291 2019-02-21 stsp free(refs_str);
2669 1a76625f 2018-10-22 stsp free(ncommits_str);
2670 1a76625f 2018-10-22 stsp free(header);
2671 80ddbec8 2018-04-29 stsp return err;
2674 07b55e75 2018-05-10 stsp static void
2675 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2677 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2678 07b55e75 2018-05-10 stsp int nscrolled = 0;
2680 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2681 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2684 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2685 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2686 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2687 07b55e75 2018-05-10 stsp if (entry) {
2688 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2689 07b55e75 2018-05-10 stsp nscrolled++;
2694 aa075928 2018-05-10 stsp static const struct got_error *
2695 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2697 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2698 5e224a3e 2019-02-22 stsp int errcode;
2700 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2702 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2703 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2704 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2705 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2706 7aafa0d1 2019-02-22 stsp if (errcode)
2707 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2708 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2711 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2712 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2714 7c1452c1 2020-03-26 stsp if (!wait)
2717 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2718 ffe38506 2020-12-01 naddy show_log_view(view);
2719 7c1452c1 2020-03-26 stsp update_panels();
2720 7c1452c1 2020-03-26 stsp doupdate();
2722 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2723 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2724 82954512 2020-02-03 stsp if (errcode)
2725 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2726 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2728 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2729 ffe38506 2020-12-01 naddy show_log_view(view);
2730 7c1452c1 2020-03-26 stsp update_panels();
2731 7c1452c1 2020-03-26 stsp doupdate();
2734 5e224a3e 2019-02-22 stsp return NULL;
2737 5e224a3e 2019-02-22 stsp static const struct got_error *
2738 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2740 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2741 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2743 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2744 2525dccb 2022-07-13 mark return NULL;
2746 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2747 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2748 9b058f45 2022-06-30 mark view->nscrolled = 0;
2750 9b058f45 2022-06-30 mark return err;
2753 9b058f45 2022-06-30 mark static const struct got_error *
2754 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2756 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2757 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2758 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2759 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2761 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2762 5e224a3e 2019-02-22 stsp return NULL;
2764 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2765 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2766 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2768 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2770 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2771 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2772 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2774 5e224a3e 2019-02-22 stsp return err;
2778 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2779 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2782 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2783 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
2785 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2786 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2788 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2789 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2791 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2792 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2794 9b058f45 2022-06-30 mark view->nscrolled = 0;
2796 dd0a52c1 2018-05-20 stsp return err;
2799 cd0acaa7 2018-05-20 stsp static const struct got_error *
2800 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2801 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2802 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2804 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2805 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2806 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2808 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2809 15a94983 2018-12-23 stsp if (diff_view == NULL)
2810 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2812 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2813 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2814 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2815 e5a0f69f 2018-08-18 stsp if (err == NULL)
2816 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2817 cd0acaa7 2018-05-20 stsp return err;
2820 80ddbec8 2018-04-29 stsp static const struct got_error *
2821 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2822 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2824 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2826 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2827 941e9f74 2019-05-21 stsp if (parent == NULL)
2828 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2830 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2831 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2832 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2833 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2834 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2835 941e9f74 2019-05-21 stsp s->tree = subtree;
2836 941e9f74 2019-05-21 stsp s->selected = 0;
2837 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2838 941e9f74 2019-05-21 stsp return NULL;
2841 941e9f74 2019-05-21 stsp static const struct got_error *
2842 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2843 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2845 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2846 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2847 941e9f74 2019-05-21 stsp const char *p;
2848 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2850 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2852 941e9f74 2019-05-21 stsp while (*p) {
2853 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2854 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2855 56e0773d 2019-11-28 stsp char *te_name;
2857 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2860 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2861 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2862 941e9f74 2019-05-21 stsp if (slash == NULL)
2863 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2865 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2866 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2867 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2870 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2871 56e0773d 2019-11-28 stsp if (te == NULL) {
2872 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2873 56e0773d 2019-11-28 stsp free(te_name);
2876 56e0773d 2019-11-28 stsp free(te_name);
2877 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2879 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2880 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2882 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2883 941e9f74 2019-05-21 stsp if (slash)
2884 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2886 941e9f74 2019-05-21 stsp subpath = strdup(path);
2887 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2888 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2892 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2897 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2898 941e9f74 2019-05-21 stsp free(tree_id);
2902 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2903 941e9f74 2019-05-21 stsp if (err) {
2904 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2907 941e9f74 2019-05-21 stsp if (slash == NULL)
2909 941e9f74 2019-05-21 stsp free(subpath);
2910 941e9f74 2019-05-21 stsp subpath = NULL;
2911 941e9f74 2019-05-21 stsp p = slash;
2914 941e9f74 2019-05-21 stsp free(subpath);
2915 1a76625f 2018-10-22 stsp return err;
2918 61266923 2020-01-14 stsp static const struct got_error *
2919 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2920 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2921 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2923 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2924 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2925 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2927 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2928 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2929 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2931 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2933 55cccc34 2020-02-20 stsp return err;
2934 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2936 55cccc34 2020-02-20 stsp *new_view = tree_view;
2938 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2939 55cccc34 2020-02-20 stsp return NULL;
2941 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2944 55cccc34 2020-02-20 stsp static const struct got_error *
2945 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2947 61266923 2020-01-14 stsp sigset_t sigset;
2948 61266923 2020-01-14 stsp int errcode;
2950 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2951 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2953 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2954 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2955 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2956 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2957 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2958 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2959 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2960 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2961 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2963 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2964 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2965 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2967 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2968 61266923 2020-01-14 stsp if (errcode)
2969 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2971 61266923 2020-01-14 stsp return NULL;
2974 1a76625f 2018-10-22 stsp static void *
2975 1a76625f 2018-10-22 stsp log_thread(void *arg)
2977 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2978 1a76625f 2018-10-22 stsp int errcode = 0;
2979 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2980 1a76625f 2018-10-22 stsp int done = 0;
2983 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2984 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2986 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2987 5629093a 2022-07-11 stsp if (errcode) {
2988 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2989 61266923 2020-01-14 stsp return (void *)err;
2992 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2993 5629093a 2022-07-11 stsp if (err) {
2994 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2995 5629093a 2022-07-11 stsp goto done;
2998 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2999 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3000 5629093a 2022-07-11 stsp if (errcode) {
3001 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
3002 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
3003 5629093a 2022-07-11 stsp goto done;
3005 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3006 1a76625f 2018-10-22 stsp if (err) {
3007 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3008 5629093a 2022-07-11 stsp goto done;
3009 1a76625f 2018-10-22 stsp err = NULL;
3011 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
3012 568eae95 2022-09-11 mark if (*a->limiting) {
3013 568eae95 2022-09-11 mark if (a->limit_match)
3014 568eae95 2022-09-11 mark a->commits_needed--;
3016 568eae95 2022-09-11 mark a->commits_needed--;
3019 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3020 3abe8080 2019-04-10 stsp if (errcode) {
3021 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3022 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3023 5629093a 2022-07-11 stsp goto done;
3024 3abe8080 2019-04-10 stsp } else if (*a->quit)
3026 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
3027 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3028 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
3029 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3030 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
3031 568eae95 2022-09-11 mark *a->first_displayed_entry =
3032 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
3033 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
3036 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3037 7c1452c1 2020-03-26 stsp if (errcode) {
3038 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3039 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3040 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3041 5629093a 2022-07-11 stsp goto done;
3045 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3047 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3048 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3049 7c1452c1 2020-03-26 stsp &tog_mutex);
3050 5629093a 2022-07-11 stsp if (errcode) {
3051 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3052 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3053 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3054 5629093a 2022-07-11 stsp goto done;
3056 21355643 2020-12-06 stsp if (*a->quit)
3061 3abe8080 2019-04-10 stsp a->log_complete = 1;
3062 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3063 5629093a 2022-07-11 stsp if (errcode)
3064 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3066 5629093a 2022-07-11 stsp if (err) {
3067 5629093a 2022-07-11 stsp tog_thread_error = 1;
3068 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3070 1a76625f 2018-10-22 stsp return (void *)err;
3073 1a76625f 2018-10-22 stsp static const struct got_error *
3074 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3076 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3077 1a76625f 2018-10-22 stsp int errcode;
3079 1a76625f 2018-10-22 stsp if (s->thread) {
3080 1a76625f 2018-10-22 stsp s->quit = 1;
3081 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3082 1a76625f 2018-10-22 stsp if (errcode)
3083 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3084 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3085 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3086 1a76625f 2018-10-22 stsp if (errcode)
3087 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3088 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3089 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3090 1a76625f 2018-10-22 stsp if (errcode)
3091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3092 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3093 1a76625f 2018-10-22 stsp if (errcode)
3094 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3095 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3096 1a76625f 2018-10-22 stsp s->thread = NULL;
3099 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3100 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3101 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3104 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3105 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3106 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3107 74467cc8 2022-06-15 stsp if (err == NULL)
3108 74467cc8 2022-06-15 stsp err = pack_err;
3109 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3112 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3113 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3114 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3117 5629093a 2022-07-11 stsp return err ? err : thread_err;
3120 9343a5fb 2018-06-23 stsp static const struct got_error *
3121 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3123 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3124 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3125 276b94a1 2020-11-13 naddy int errcode;
3127 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3129 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3130 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3131 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3133 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3134 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3135 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3137 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3138 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3139 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3140 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3141 1a76625f 2018-10-22 stsp free(s->start_id);
3142 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3143 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3144 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3145 1a76625f 2018-10-22 stsp return err;
3149 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3150 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3151 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3152 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3153 568eae95 2022-09-11 mark * works with very slight change.
3155 1a76625f 2018-10-22 stsp static const struct got_error *
3156 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3158 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3159 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3160 568eae95 2022-09-11 mark struct tog_view *v = view;
3161 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3162 568eae95 2022-09-11 mark char pattern[1024];
3165 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3166 568eae95 2022-09-11 mark v = view->child;
3167 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3168 568eae95 2022-09-11 mark v = view->parent;
3170 568eae95 2022-09-11 mark /* Get the pattern */
3171 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3172 568eae95 2022-09-11 mark wclrtoeol(v->window);
3173 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3174 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3175 568eae95 2022-09-11 mark nocbreak();
3177 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3180 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3181 568eae95 2022-09-11 mark if (ret == ERR)
3182 568eae95 2022-09-11 mark return NULL;
3184 568eae95 2022-09-11 mark if (*pattern == '\0') {
3186 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3187 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3189 568eae95 2022-09-11 mark if (!s->limit_view)
3190 568eae95 2022-09-11 mark return NULL;
3193 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3194 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3195 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3196 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3197 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3198 568eae95 2022-09-11 mark * the queue.
3200 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3201 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3202 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3203 568eae95 2022-09-11 mark s->selected = 0;
3204 568eae95 2022-09-11 mark s->limit_view = 0;
3206 568eae95 2022-09-11 mark return NULL;
3209 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3210 568eae95 2022-09-11 mark return NULL;
3212 568eae95 2022-09-11 mark s->limit_view = 1;
3214 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3215 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3216 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3217 568eae95 2022-09-11 mark s->selected_entry = NULL;
3218 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3220 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3221 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3222 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3224 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3225 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3226 568eae95 2022-09-11 mark int have_match = 0;
3228 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3229 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3231 568eae95 2022-09-11 mark return err;
3233 568eae95 2022-09-11 mark if (have_match) {
3234 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3236 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3237 568eae95 2022-09-11 mark entry->id);
3238 568eae95 2022-09-11 mark if (matched == NULL) {
3239 568eae95 2022-09-11 mark err = got_error_from_errno(
3240 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3243 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3244 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3246 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3247 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3248 568eae95 2022-09-11 mark matched, entry);
3249 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3253 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3254 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3255 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3256 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3257 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3258 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3260 568eae95 2022-09-11 mark return err;
3263 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3264 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3265 568eae95 2022-09-11 mark s->selected = 0;
3267 568eae95 2022-09-11 mark return NULL;
3270 568eae95 2022-09-11 mark static const struct got_error *
3271 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3273 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3275 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3276 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3277 60493ae3 2019-06-20 stsp return NULL;
3280 60493ae3 2019-06-20 stsp static const struct got_error *
3281 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3283 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3284 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3285 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3287 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3288 f9686aa5 2020-03-27 stsp show_log_view(view);
3289 f9686aa5 2020-03-27 stsp update_panels();
3290 f9686aa5 2020-03-27 stsp doupdate();
3292 96e2b566 2019-07-08 stsp if (s->search_entry) {
3293 13add988 2019-10-15 stsp int errcode, ch;
3294 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3295 13add988 2019-10-15 stsp if (errcode)
3296 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3297 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3298 13add988 2019-10-15 stsp ch = wgetch(view->window);
3299 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3300 13add988 2019-10-15 stsp if (errcode)
3301 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3302 13add988 2019-10-15 stsp "pthread_mutex_lock");
3303 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3304 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3305 678cbce5 2019-07-28 stsp return NULL;
3307 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3308 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3310 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3311 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3312 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3314 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3315 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3316 f704b35c 2022-06-18 stsp * might have changed.
3318 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3319 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3321 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3324 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3327 60493ae3 2019-06-20 stsp while (1) {
3328 13add988 2019-10-15 stsp int have_match = 0;
3330 60493ae3 2019-06-20 stsp if (entry == NULL) {
3331 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3332 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3333 f9967bca 2020-03-27 stsp view->search_next_done =
3334 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3335 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3336 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3337 f801134a 2019-06-25 stsp return NULL;
3340 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3341 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3342 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3344 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3345 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3348 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3349 13add988 2019-10-15 stsp &view->regex);
3352 13add988 2019-10-15 stsp if (have_match) {
3353 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3354 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3358 96e2b566 2019-07-08 stsp s->search_entry = entry;
3359 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3360 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3362 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3365 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3366 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3367 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3368 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3370 60493ae3 2019-06-20 stsp return err;
3373 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3374 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3376 60493ae3 2019-06-20 stsp return err;
3381 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3383 60493ae3 2019-06-20 stsp return NULL;
3386 60493ae3 2019-06-20 stsp static const struct got_error *
3387 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3388 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3389 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3391 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3392 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3393 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3394 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3395 1a76625f 2018-10-22 stsp int errcode;
3397 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3398 f135c941 2020-02-20 stsp free(s->in_repo_path);
3399 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3400 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3401 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3404 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3405 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3406 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3407 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3409 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3410 568eae95 2022-09-11 mark s->limit_view = 0;
3411 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3413 fb2756b9 2018-08-04 stsp s->repo = repo;
3414 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3415 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3416 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3417 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3418 9cd7cbd1 2020-12-07 stsp goto done;
3421 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3422 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3423 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3424 5036bf37 2018-09-24 stsp goto done;
3426 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3427 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3429 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3430 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3431 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3432 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3434 11b20872 2019-11-08 stsp goto done;
3435 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3436 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3437 11b20872 2019-11-08 stsp if (err) {
3438 11b20872 2019-11-08 stsp free_colors(&s->colors);
3439 11b20872 2019-11-08 stsp goto done;
3441 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3442 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3443 11b20872 2019-11-08 stsp if (err) {
3444 11b20872 2019-11-08 stsp free_colors(&s->colors);
3445 11b20872 2019-11-08 stsp goto done;
3449 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3450 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3451 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3452 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3453 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3454 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3456 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3457 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3459 74467cc8 2022-06-15 stsp goto done;
3461 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3462 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3463 0ae84acc 2022-06-15 tracey if (err)
3464 0ae84acc 2022-06-15 tracey goto done;
3465 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3466 b672a97a 2020-01-27 stsp !s->log_branches);
3468 1a76625f 2018-10-22 stsp goto done;
3469 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3470 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3472 c5b78334 2020-01-12 stsp goto done;
3474 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3475 1a76625f 2018-10-22 stsp if (errcode) {
3476 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3477 1a76625f 2018-10-22 stsp goto done;
3479 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3480 7c1452c1 2020-03-26 stsp if (errcode) {
3481 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3482 7c1452c1 2020-03-26 stsp goto done;
3485 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3486 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3487 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3488 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3489 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3490 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3491 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3492 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3493 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3494 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3495 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3496 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3497 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3498 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3499 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3500 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3501 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3504 1a76625f 2018-10-22 stsp close_log_view(view);
3505 ba4f502b 2018-08-04 stsp return err;
3508 e5a0f69f 2018-08-18 stsp static const struct got_error *
3509 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3511 f2f6d207 2020-11-24 stsp const struct got_error *err;
3512 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3514 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3515 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3516 2b380cc8 2018-10-24 stsp &s->thread_args);
3517 2b380cc8 2018-10-24 stsp if (errcode)
3518 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3519 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3520 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3522 f2f6d207 2020-11-24 stsp return err;
3526 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3529 b880cc75 2022-06-30 mark static void
3530 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3532 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3534 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3536 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3537 568eae95 2022-09-11 mark view->count = 0;
3539 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3541 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3543 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3544 b880cc75 2022-06-30 mark --s->selected;
3546 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3548 b880cc75 2022-06-30 mark select_commit(s);
3552 b880cc75 2022-06-30 mark static const struct got_error *
3553 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3555 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3556 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3557 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3559 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3560 568eae95 2022-09-11 mark return NULL;
3562 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3563 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3564 b880cc75 2022-06-30 mark return NULL;
3566 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3567 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3569 631e7531 2022-08-12 mark if (!page) {
3570 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3571 b880cc75 2022-06-30 mark ++s->selected;
3573 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3574 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3575 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3578 631e7531 2022-08-12 mark s->selected = 0;
3579 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3580 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3581 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3582 631e7531 2022-08-12 mark if (entry == NULL)
3584 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3585 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3587 631e7531 2022-08-12 mark if (n > 0)
3588 631e7531 2022-08-12 mark s->selected = n - 1;
3590 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3591 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3592 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3593 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3595 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3598 b880cc75 2022-06-30 mark return err;
3601 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3602 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3604 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3605 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3606 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3607 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3610 b880cc75 2022-06-30 mark select_commit(s);
3612 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3613 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3614 b880cc75 2022-06-30 mark view->count = 0;
3616 b880cc75 2022-06-30 mark return NULL;
3619 9b058f45 2022-06-30 mark static void
3620 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3625 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3626 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3627 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3628 7532ccda 2022-07-11 mark else if (view->resized_y)
3629 7532ccda 2022-07-11 mark *y = view->resized_y;
3631 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3632 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3633 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3634 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3635 7532ccda 2022-07-11 mark else if (view->resized_x)
3636 7532ccda 2022-07-11 mark *x = view->resized_x;
3638 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3642 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3643 e5a0f69f 2018-08-18 stsp static const struct got_error *
3644 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3646 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3648 9b058f45 2022-06-30 mark view->nlines = y;
3649 d2366e29 2022-07-07 mark view->ncols = COLS;
3650 9b058f45 2022-06-30 mark err = view_resize(view);
3652 9b058f45 2022-06-30 mark return err;
3654 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3656 9b058f45 2022-06-30 mark return err;
3659 94b80cfa 2022-08-01 mark static const struct got_error *
3660 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3662 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3663 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3664 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3666 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3667 374f69dd 2022-08-13 stsp return NULL;
3669 94b80cfa 2022-08-01 mark g = view->gline;
3670 94b80cfa 2022-08-01 mark view->gline = 0;
3672 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3673 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3674 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3675 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3676 94b80cfa 2022-08-01 mark select_commit(s);
3677 94b80cfa 2022-08-01 mark return NULL;
3680 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3681 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3682 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3683 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3684 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3686 94b80cfa 2022-08-01 mark return err;
3687 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3688 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3690 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3691 94b80cfa 2022-08-01 mark s->selected = g - 1;
3693 94b80cfa 2022-08-01 mark select_commit(s);
3694 94b80cfa 2022-08-01 mark return NULL;
3698 d627976f 2023-02-01 mark static void
3699 d627976f 2023-02-01 mark horizontal_scroll_input(struct tog_view *view, int ch)
3702 d627976f 2023-02-01 mark switch (ch) {
3703 d627976f 2023-02-01 mark case KEY_LEFT:
3705 d627976f 2023-02-01 mark view->x -= MIN(view->x, 2);
3706 d627976f 2023-02-01 mark if (view->x <= 0)
3707 d627976f 2023-02-01 mark view->count = 0;
3709 d627976f 2023-02-01 mark case KEY_RIGHT:
3711 d627976f 2023-02-01 mark if (view->x + view->ncols / 2 < view->maxx)
3712 d627976f 2023-02-01 mark view->x += 2;
3714 d627976f 2023-02-01 mark view->count = 0;
3717 d627976f 2023-02-01 mark view->x = 0;
3720 d627976f 2023-02-01 mark view->x = MAX(view->maxx - view->ncols / 2, 0);
3721 d627976f 2023-02-01 mark view->count = 0;
3728 9b058f45 2022-06-30 mark static const struct got_error *
3729 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3731 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3732 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3733 631e7531 2022-08-12 mark int eos, nscroll;
3735 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3736 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3737 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3738 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3739 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3740 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3743 11edf34c 2022-08-12 mark return err;
3746 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3747 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3748 0dca135e 2022-06-30 mark --eos; /* border */
3750 94b80cfa 2022-08-01 mark if (view->gline)
3751 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3753 528dedf3 2021-08-30 stsp switch (ch) {
3755 568eae95 2022-09-11 mark err = limit_log_view(view);
3758 1e37a5c2 2019-05-12 jcs s->quit = 1;
3762 145b6838 2022-06-16 stsp case KEY_RIGHT:
3764 145b6838 2022-06-16 stsp case KEY_LEFT:
3766 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
3769 1e37a5c2 2019-05-12 jcs case KEY_UP:
3772 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3773 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3777 912a3f79 2021-08-30 j case KEY_HOME:
3778 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3779 640cd7ff 2022-06-22 mark view->count = 0;
3781 83cc4199 2022-06-13 stsp case CTRL('u'):
3783 83cc4199 2022-06-13 stsp nscroll /= 2;
3784 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3785 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3786 a4292ac5 2019-05-12 jcs case CTRL('b'):
3788 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3791 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3794 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3795 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3798 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3799 3f6c6614 2023-01-23 mark view->action = s->use_committer ?
3800 3f6c6614 2023-01-23 mark "show committer" : "show commit author";
3804 912a3f79 2021-08-30 j case KEY_END: {
3805 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3806 912a3f79 2021-08-30 j * traverse them all. */
3807 640cd7ff 2022-06-22 mark view->count = 0;
3808 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3809 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3810 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3811 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3812 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3815 80b7e8da 2022-06-11 stsp case CTRL('d'):
3817 83cc4199 2022-06-13 stsp nscroll /= 2;
3818 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3819 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3820 61417565 2022-06-20 mark case CTRL('f'):
3821 48bb96f0 2022-06-20 naddy case 'f':
3823 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3825 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3826 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3827 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3828 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3829 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3830 2b779855 2020-12-05 naddy select_commit(s);
3831 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3832 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3833 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3834 568eae95 2022-09-11 mark s->commits->ncommits;
3835 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3838 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3839 49b24ee5 2022-07-03 mark case '\r':
3840 640cd7ff 2022-06-22 mark view->count = 0;
3841 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3843 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3846 640cd7ff 2022-06-22 mark view->count = 0;
3847 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3849 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3851 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3852 21355643 2020-12-06 stsp case CTRL('l'):
3854 640cd7ff 2022-06-22 mark view->count = 0;
3855 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3856 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3858 21355643 2020-12-06 stsp err = stop_log_thread(s);
3860 74cfe85e 2020-10-20 stsp return err;
3861 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3862 21355643 2020-12-06 stsp char *parent_path;
3863 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3865 21355643 2020-12-06 stsp return err;
3866 21355643 2020-12-06 stsp free(s->in_repo_path);
3867 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3868 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3869 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3870 21355643 2020-12-06 stsp struct got_object_id *start_id;
3871 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3872 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3873 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3874 ea371198 2022-11-17 stsp if (err) {
3875 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
3876 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
3877 ea371198 2022-11-17 stsp return err;
3878 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
3879 ea371198 2022-11-17 stsp free(s->head_ref_name);
3880 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
3881 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
3882 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3883 ea371198 2022-11-17 stsp &tog_refs, s->repo);
3885 ea371198 2022-11-17 stsp return err;
3887 21355643 2020-12-06 stsp free(s->start_id);
3888 21355643 2020-12-06 stsp s->start_id = start_id;
3889 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3890 21355643 2020-12-06 stsp } else /* 'B' */
3891 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3893 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3894 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3896 b0dd8d27 2022-06-16 stsp return err;
3898 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3899 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3900 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3902 21355643 2020-12-06 stsp return err;
3903 51a10b52 2020-12-26 stsp tog_free_refs();
3904 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3906 ca51c541 2020-12-07 stsp return err;
3907 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3908 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3910 d01904d4 2019-06-25 stsp return err;
3911 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3912 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3914 b672a97a 2020-01-27 stsp return err;
3915 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3916 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3917 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3918 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3919 21355643 2020-12-06 stsp s->selected_entry = NULL;
3920 21355643 2020-12-06 stsp s->selected = 0;
3921 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3922 21355643 2020-12-06 stsp s->quit = 0;
3923 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3924 dfee752e 2022-06-18 op s->matched_entry = NULL;
3925 dfee752e 2022-06-18 op s->search_entry = NULL;
3926 01a7bcaf 2022-07-22 mark view->offset = 0;
3929 640cd7ff 2022-06-22 mark view->count = 0;
3930 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3933 640cd7ff 2022-06-22 mark view->count = 0;
3937 80ddbec8 2018-04-29 stsp return err;
3940 4ed7e80c 2018-05-20 stsp static const struct got_error *
3941 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3943 c2db6724 2019-01-04 stsp const struct got_error *error;
3945 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3946 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3947 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3949 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3950 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3952 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3953 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3955 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3956 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3958 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3959 c2db6724 2019-01-04 stsp if (error != NULL)
3960 c2db6724 2019-01-04 stsp return error;
3962 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3963 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3965 c2db6724 2019-01-04 stsp return NULL;
3968 a915003a 2019-02-05 stsp static void
3969 a915003a 2019-02-05 stsp init_curses(void)
3972 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3973 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3974 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3976 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3977 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3978 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3979 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3980 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3982 a915003a 2019-02-05 stsp initscr();
3984 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3987 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3988 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3989 a915003a 2019-02-05 stsp curs_set(0);
3990 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3991 6d17833f 2019-11-08 stsp start_color();
3992 6d17833f 2019-11-08 stsp use_default_colors();
3996 c2db6724 2019-01-04 stsp static const struct got_error *
3997 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3998 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4000 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4002 f135c941 2020-02-20 stsp if (argc == 0) {
4003 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4004 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4005 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4006 f135c941 2020-02-20 stsp return NULL;
4009 f135c941 2020-02-20 stsp if (worktree) {
4010 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4013 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4015 f135c941 2020-02-20 stsp return err;
4016 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4017 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4018 bfd61697 2020-11-14 stsp p) == -1) {
4019 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4020 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4024 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4026 f135c941 2020-02-20 stsp return err;
4029 f135c941 2020-02-20 stsp static const struct got_error *
4030 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4032 80ddbec8 2018-04-29 stsp const struct got_error *error;
4033 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4034 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4035 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4036 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4037 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4038 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4039 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4040 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4041 04cc582a 2018-08-01 stsp struct tog_view *view;
4042 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4044 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4045 80ddbec8 2018-04-29 stsp switch (ch) {
4047 b672a97a 2020-01-27 stsp log_branches = 1;
4050 80ddbec8 2018-04-29 stsp start_commit = optarg;
4053 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4054 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4055 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4059 17020d27 2019-03-07 stsp usage_log();
4060 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4064 80ddbec8 2018-04-29 stsp argc -= optind;
4065 80ddbec8 2018-04-29 stsp argv += optind;
4067 f135c941 2020-02-20 stsp if (argc > 1)
4068 f135c941 2020-02-20 stsp usage_log();
4070 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4071 0ae84acc 2022-06-15 tracey if (error != NULL)
4072 0ae84acc 2022-06-15 tracey goto done;
4074 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4075 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4076 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4077 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4078 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4079 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4080 c156c7a4 2020-12-18 stsp goto done;
4081 a1fbf39a 2019-08-11 stsp if (worktree)
4082 6962eb72 2020-02-20 stsp repo_path =
4083 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4085 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4086 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4087 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4088 c156c7a4 2020-12-18 stsp goto done;
4092 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4093 80ddbec8 2018-04-29 stsp if (error != NULL)
4094 ecb28ae0 2018-07-16 stsp goto done;
4096 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4097 f135c941 2020-02-20 stsp repo, worktree);
4098 f135c941 2020-02-20 stsp if (error)
4099 f135c941 2020-02-20 stsp goto done;
4101 f135c941 2020-02-20 stsp init_curses();
4103 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4104 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4105 c02c541e 2019-03-29 stsp if (error)
4106 c02c541e 2019-03-29 stsp goto done;
4108 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4109 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4110 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4111 87670572 2020-12-26 naddy if (error)
4112 87670572 2020-12-26 naddy goto done;
4115 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4116 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4117 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4118 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4119 d8f38dc4 2020-12-05 stsp if (error)
4120 d8f38dc4 2020-12-05 stsp goto done;
4121 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4123 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4124 d8f38dc4 2020-12-05 stsp if (error == NULL)
4125 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4126 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4127 d8f38dc4 2020-12-05 stsp goto done;
4128 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4129 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4130 d8f38dc4 2020-12-05 stsp if (error)
4131 d8f38dc4 2020-12-05 stsp goto done;
4134 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4135 04cc582a 2018-08-01 stsp if (view == NULL) {
4136 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4137 04cc582a 2018-08-01 stsp goto done;
4139 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4140 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4141 ba4f502b 2018-08-04 stsp if (error)
4142 ba4f502b 2018-08-04 stsp goto done;
4143 2fc00ff4 2019-08-31 stsp if (worktree) {
4144 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4145 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4146 2fc00ff4 2019-08-31 stsp worktree = NULL;
4148 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4150 f135c941 2020-02-20 stsp free(in_repo_path);
4151 ecb28ae0 2018-07-16 stsp free(repo_path);
4152 ecb28ae0 2018-07-16 stsp free(cwd);
4153 899d86c2 2018-05-10 stsp free(start_id);
4154 d8f38dc4 2020-12-05 stsp free(label);
4156 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4157 1d0f4054 2021-06-17 stsp if (repo) {
4158 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4159 1d0f4054 2021-06-17 stsp if (error == NULL)
4160 1d0f4054 2021-06-17 stsp error = close_err;
4162 ec142235 2019-03-07 stsp if (worktree)
4163 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4164 0ae84acc 2022-06-15 tracey if (pack_fds) {
4165 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4166 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4167 0ae84acc 2022-06-15 tracey if (error == NULL)
4168 0ae84acc 2022-06-15 tracey error = pack_err;
4170 51a10b52 2020-12-26 stsp tog_free_refs();
4171 80ddbec8 2018-04-29 stsp return error;
4174 4ed7e80c 2018-05-20 stsp __dead static void
4175 9f7d7167 2018-04-29 stsp usage_diff(void)
4178 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4179 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4183 6d17833f 2019-11-08 stsp static int
4184 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4185 41605754 2020-11-12 stsp regmatch_t *regmatch)
4187 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4190 336075a4 2022-06-25 op static struct tog_color *
4191 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4193 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4195 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4196 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4197 6d17833f 2019-11-08 stsp return tc;
4200 6d17833f 2019-11-08 stsp return NULL;
4203 4ed7e80c 2018-05-20 stsp static const struct got_error *
4204 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4205 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4207 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4208 44a87665 2022-06-16 stsp char *exstr = NULL;
4209 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4210 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4211 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4212 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4214 41605754 2020-11-12 stsp *wtotal = 0;
4216 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4217 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4219 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4221 44a87665 2022-06-16 stsp return err;
4223 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4224 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4225 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4226 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4227 44a87665 2022-06-16 stsp goto done;
4229 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4230 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4231 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4232 1853e0f4 2022-06-16 stsp goto done;
4234 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4235 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4236 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4237 1853e0f4 2022-06-16 stsp goto done;
4240 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4241 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4242 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4244 1853e0f4 2022-06-16 stsp goto done;
4245 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4247 1853e0f4 2022-06-16 stsp free(wline);
4248 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4249 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4251 1853e0f4 2022-06-16 stsp goto done;
4252 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4253 1853e0f4 2022-06-16 stsp wlimit -= width;
4254 1853e0f4 2022-06-16 stsp *wtotal += width;
4257 41605754 2020-11-12 stsp if (wlimit > 0) {
4258 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4259 1853e0f4 2022-06-16 stsp size_t wlen;
4261 1853e0f4 2022-06-16 stsp free(wline);
4262 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4263 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4265 1853e0f4 2022-06-16 stsp goto done;
4266 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4267 1853e0f4 2022-06-16 stsp while (i < wlen) {
4268 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4269 1853e0f4 2022-06-16 stsp if (width == -1) {
4270 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4271 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4272 1853e0f4 2022-06-16 stsp goto done;
4274 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4276 61417565 2022-06-20 mark w += width;
4279 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4280 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4281 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4282 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4283 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4284 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4285 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4289 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4290 1853e0f4 2022-06-16 stsp free(wline);
4291 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4292 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4293 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4294 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4296 1853e0f4 2022-06-16 stsp goto done;
4297 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4299 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4300 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4302 1853e0f4 2022-06-16 stsp goto done;
4303 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4305 1853e0f4 2022-06-16 stsp *wtotal += width2;
4308 145b6838 2022-06-16 stsp free(wline);
4309 44a87665 2022-06-16 stsp free(exstr);
4310 1853e0f4 2022-06-16 stsp free(seg0);
4311 1853e0f4 2022-06-16 stsp free(seg1);
4312 1853e0f4 2022-06-16 stsp free(seg2);
4313 1853e0f4 2022-06-16 stsp return err;
4316 94b80cfa 2022-08-01 mark static int
4317 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4319 94b80cfa 2022-08-01 mark FILE *f = NULL;
4320 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4322 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4323 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4325 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4326 ec2a9698 2022-09-15 mark selected = first;
4327 ec2a9698 2022-09-15 mark eof = &s->eof;
4329 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4330 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4332 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4333 94b80cfa 2022-08-01 mark selected = first;
4334 94b80cfa 2022-08-01 mark eof = &s->eof;
4336 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4337 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4339 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4340 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4341 94b80cfa 2022-08-01 mark eof = &s->eof;
4342 94b80cfa 2022-08-01 mark f = s->blame.f;
4346 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4347 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4349 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4350 94b80cfa 2022-08-01 mark rewind(f);
4352 94b80cfa 2022-08-01 mark *first = 1;
4353 94b80cfa 2022-08-01 mark *lineno = 0;
4354 94b80cfa 2022-08-01 mark *nprinted = 0;
4358 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4359 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4360 94b80cfa 2022-08-01 mark view->gline = 0;
4365 41605754 2020-11-12 stsp static const struct got_error *
4366 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4368 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4369 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4370 61e69b96 2018-05-20 stsp const struct got_error *err;
4371 c7d5c43c 2022-08-04 mark int nprinted = 0;
4372 b304db33 2018-05-20 stsp char *line;
4373 826082fe 2020-12-10 stsp size_t linesize = 0;
4374 826082fe 2020-12-10 stsp ssize_t linelen;
4375 61e69b96 2018-05-20 stsp wchar_t *wline;
4376 e0b650dd 2018-05-20 stsp int width;
4377 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4378 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4379 fe621944 2020-11-10 stsp off_t line_offset;
4381 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4382 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4383 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4384 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4386 f7d12f7e 2018-08-01 stsp werase(view->window);
4388 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4389 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4391 a3404814 2018-09-02 stsp if (header) {
4392 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4393 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4394 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4396 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4397 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4398 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4400 135a2da0 2020-11-11 stsp free(line);
4402 a3404814 2018-09-02 stsp return err;
4404 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4405 a3404814 2018-09-02 stsp wstandout(view->window);
4406 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4407 e54cc94a 2020-11-11 stsp free(wline);
4408 e54cc94a 2020-11-11 stsp wline = NULL;
4409 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4410 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4411 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4412 a3404814 2018-09-02 stsp wstandend(view->window);
4414 a3404814 2018-09-02 stsp if (max_lines <= 1)
4415 a3404814 2018-09-02 stsp return NULL;
4416 a3404814 2018-09-02 stsp max_lines--;
4419 89f1a395 2020-12-01 naddy s->eof = 0;
4420 145b6838 2022-06-16 stsp view->maxx = 0;
4421 826082fe 2020-12-10 stsp line = NULL;
4422 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4423 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4424 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4426 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4427 826082fe 2020-12-10 stsp if (linelen == -1) {
4428 826082fe 2020-12-10 stsp if (feof(s->f)) {
4429 826082fe 2020-12-10 stsp s->eof = 1;
4432 826082fe 2020-12-10 stsp free(line);
4433 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4436 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4438 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4440 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4441 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4443 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4444 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4445 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4446 1853e0f4 2022-06-16 stsp if (err) {
4447 1853e0f4 2022-06-16 stsp free(line);
4448 1853e0f4 2022-06-16 stsp return err;
4450 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4451 1853e0f4 2022-06-16 stsp free(wline);
4452 1853e0f4 2022-06-16 stsp wline = NULL;
4454 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4455 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4456 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4457 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4459 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4460 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4461 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4462 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4463 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4464 41605754 2020-11-12 stsp if (err) {
4465 41605754 2020-11-12 stsp free(line);
4466 41605754 2020-11-12 stsp return err;
4470 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4471 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4472 969c159c 2022-06-16 stsp if (err) {
4473 969c159c 2022-06-16 stsp free(line);
4474 969c159c 2022-06-16 stsp return err;
4476 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4477 969c159c 2022-06-16 stsp free(wline);
4478 41605754 2020-11-12 stsp wline = NULL;
4480 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4481 94b80cfa 2022-08-01 mark /* highlight full gline length */
4482 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4483 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4485 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4486 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4489 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4490 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4491 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4493 826082fe 2020-12-10 stsp free(line);
4494 fe621944 2020-11-10 stsp if (nprinted >= 1)
4495 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4496 89f1a395 2020-12-01 naddy (nprinted - 1);
4498 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4500 9b058f45 2022-06-30 mark view_border(view);
4502 89f1a395 2020-12-01 naddy if (s->eof) {
4503 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4504 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4505 c3e9aa98 2019-05-13 jcs nprinted++;
4508 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4509 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4511 c3e9aa98 2019-05-13 jcs return err;
4514 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4515 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4516 e54cc94a 2020-11-11 stsp free(wline);
4517 e54cc94a 2020-11-11 stsp wline = NULL;
4518 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4521 26ed57b2 2018-05-19 stsp return NULL;
4524 abd2672a 2018-12-23 stsp static char *
4525 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4527 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4528 09867e48 2019-08-13 stsp char *p, *s;
4530 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4531 09867e48 2019-08-13 stsp if (tm == NULL)
4532 09867e48 2019-08-13 stsp return NULL;
4533 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4534 09867e48 2019-08-13 stsp if (s == NULL)
4535 09867e48 2019-08-13 stsp return NULL;
4536 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4538 abd2672a 2018-12-23 stsp *p = '\0';
4542 4ed7e80c 2018-05-20 stsp static const struct got_error *
4543 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4544 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4546 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4548 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4549 fe621944 2020-11-10 stsp if (p == NULL)
4550 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4551 c7d5c43c 2022-08-04 mark *lines = p;
4552 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4553 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4554 fe621944 2020-11-10 stsp (*nlines)++;
4556 fe621944 2020-11-10 stsp return NULL;
4559 fe621944 2020-11-10 stsp static const struct got_error *
4560 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4561 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4563 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4564 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4565 1f3405c9 2023-01-17 mark size_t i, r;
4567 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4568 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4570 1f3405c9 2023-01-17 mark for (;;) {
4571 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4572 1f3405c9 2023-01-17 mark if (r == 0) {
4573 1f3405c9 2023-01-17 mark if (ferror(src))
4574 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4575 1f3405c9 2023-01-17 mark if (feof(src))
4578 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4579 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4583 1f3405c9 2023-01-17 mark * The diff driver initialises the first line at offset zero when the
4584 1f3405c9 2023-01-17 mark * array isn't prepopulated, skip it; we already have it in *d_lines.
4586 1f3405c9 2023-01-17 mark for (i = 1; i < s_nlines; ++i)
4587 1f3405c9 2023-01-17 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4589 1f3405c9 2023-01-17 mark --s_nlines;
4591 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4592 1f3405c9 2023-01-17 mark if (p == NULL) {
4593 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4594 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4597 1f3405c9 2023-01-17 mark *d_lines = p;
4599 1f3405c9 2023-01-17 mark memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4600 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4602 1f3405c9 2023-01-17 mark return NULL;
4605 1f3405c9 2023-01-17 mark static const struct got_error *
4606 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4607 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4608 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4609 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4611 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4612 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4613 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4614 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4615 45d799e2 2018-12-23 stsp time_t committer_time;
4616 45d799e2 2018-12-23 stsp const char *author, *committer;
4617 8b473291 2019-02-21 stsp char *refs_str = NULL;
4618 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4619 fe621944 2020-11-10 stsp off_t outoff = 0;
4622 8b473291 2019-02-21 stsp if (refs) {
4623 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4625 8b473291 2019-02-21 stsp return err;
4628 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4630 abd2672a 2018-12-23 stsp return err;
4632 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4633 15a94983 2018-12-23 stsp if (err) {
4634 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4635 15a94983 2018-12-23 stsp goto done;
4638 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4640 fe621944 2020-11-10 stsp goto done;
4642 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4643 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4644 fe621944 2020-11-10 stsp if (n < 0) {
4645 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4646 abd2672a 2018-12-23 stsp goto done;
4648 fe621944 2020-11-10 stsp outoff += n;
4649 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4651 fe621944 2020-11-10 stsp goto done;
4653 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4654 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4655 fe621944 2020-11-10 stsp if (n < 0) {
4656 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4657 abd2672a 2018-12-23 stsp goto done;
4659 fe621944 2020-11-10 stsp outoff += n;
4660 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4662 fe621944 2020-11-10 stsp goto done;
4664 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4665 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4666 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4667 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4668 fe621944 2020-11-10 stsp if (n < 0) {
4669 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4670 fe621944 2020-11-10 stsp goto done;
4672 fe621944 2020-11-10 stsp outoff += n;
4673 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4674 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4676 fe621944 2020-11-10 stsp goto done;
4678 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4679 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4680 1a99e0b4 2023-01-07 stsp if (datestr) {
4681 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4682 fe621944 2020-11-10 stsp if (n < 0) {
4683 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4684 fe621944 2020-11-10 stsp goto done;
4686 fe621944 2020-11-10 stsp outoff += n;
4687 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4688 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4690 fe621944 2020-11-10 stsp goto done;
4692 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4693 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4694 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4695 9f98ca05 2021-09-24 stsp int pn = 1;
4696 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4697 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4698 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4700 9f98ca05 2021-09-24 stsp goto done;
4701 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4702 9f98ca05 2021-09-24 stsp if (n < 0) {
4703 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4704 9f98ca05 2021-09-24 stsp goto done;
4706 9f98ca05 2021-09-24 stsp outoff += n;
4707 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4708 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4710 9f98ca05 2021-09-24 stsp goto done;
4711 9f98ca05 2021-09-24 stsp free(id_str);
4712 9f98ca05 2021-09-24 stsp id_str = NULL;
4716 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4718 5943eee2 2019-08-13 stsp goto done;
4719 fe621944 2020-11-10 stsp s = logmsg;
4720 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4721 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4722 fe621944 2020-11-10 stsp if (n < 0) {
4723 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4724 fe621944 2020-11-10 stsp goto done;
4726 fe621944 2020-11-10 stsp outoff += n;
4727 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4728 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4730 fe621944 2020-11-10 stsp goto done;
4733 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
4734 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4735 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
4737 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4738 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4739 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
4740 fe621944 2020-11-10 stsp if (n < 0) {
4741 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4742 fe621944 2020-11-10 stsp goto done;
4744 fe621944 2020-11-10 stsp outoff += n;
4745 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4746 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4748 fe621944 2020-11-10 stsp goto done;
4751 0208f208 2020-05-05 stsp fputc('\n', outfile);
4753 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4755 5191b70b 2023-01-07 mark goto done;
4757 5191b70b 2023-01-07 mark n = fprintf(outfile,
4758 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4759 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4760 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4761 5191b70b 2023-01-07 mark if (n < 0) {
4762 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
4763 5191b70b 2023-01-07 mark goto done;
4765 5191b70b 2023-01-07 mark outoff += n;
4766 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4768 5191b70b 2023-01-07 mark goto done;
4770 5191b70b 2023-01-07 mark fputc('\n', outfile);
4772 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4774 abd2672a 2018-12-23 stsp free(id_str);
4775 5943eee2 2019-08-13 stsp free(logmsg);
4776 8b473291 2019-02-21 stsp free(refs_str);
4777 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4778 7510f233 2020-08-09 stsp if (err) {
4779 c7d5c43c 2022-08-04 mark free(*lines);
4780 c7d5c43c 2022-08-04 mark *lines = NULL;
4781 ae6a6978 2020-08-09 stsp *nlines = 0;
4783 fe621944 2020-11-10 stsp return err;
4786 abd2672a 2018-12-23 stsp static const struct got_error *
4787 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4789 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4790 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
4791 15a94983 2018-12-23 stsp int obj_type;
4792 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
4793 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
4795 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
4797 c7d5c43c 2022-08-04 mark free(s->lines);
4798 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4799 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4800 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4801 fe621944 2020-11-10 stsp s->nlines = 0;
4803 511a516b 2018-05-19 stsp f = got_opentemp();
4804 48ae06ee 2018-10-18 stsp if (f == NULL) {
4805 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4806 48ae06ee 2018-10-18 stsp goto done;
4808 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
4809 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
4810 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
4811 1f3405c9 2023-01-17 mark goto done;
4813 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4814 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4815 fb43ecf1 2019-02-11 stsp goto done;
4819 15a94983 2018-12-23 stsp if (s->id1)
4820 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4822 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4824 15a94983 2018-12-23 stsp goto done;
4826 15a94983 2018-12-23 stsp switch (obj_type) {
4827 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4828 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4829 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4830 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4831 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4834 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4835 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4836 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4837 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4838 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
4840 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4841 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4842 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4843 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4844 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4845 1f3405c9 2023-01-17 mark size_t nlines = 0;
4846 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
4847 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
4848 1f3405c9 2023-01-17 mark &changed_paths,
4849 1f3405c9 2023-01-17 mark s->ignore_whitespace,
4850 1f3405c9 2023-01-17 mark s->force_text_diff,
4851 1f3405c9 2023-01-17 mark tog_diff_algo
4854 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
4855 1f3405c9 2023-01-17 mark if (lines == NULL) {
4856 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
4857 1f3405c9 2023-01-17 mark goto done;
4860 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
4861 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
4862 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4863 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
4864 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4868 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4870 3ffacbe1 2020-02-02 tracey goto done;
4871 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4872 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4873 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4874 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4875 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
4876 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4877 f44b1f58 2020-02-02 tracey if (err)
4878 f44b1f58 2020-02-02 tracey goto done;
4879 f44b1f58 2020-02-02 tracey } else {
4880 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4881 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4882 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4883 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4884 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4885 5191b70b 2023-01-07 mark s->ignore_whitespace,
4886 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4887 f44b1f58 2020-02-02 tracey if (err)
4888 f44b1f58 2020-02-02 tracey goto done;
4893 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4895 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4896 1f3405c9 2023-01-17 mark lines, nlines);
4900 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4904 1f3405c9 2023-01-17 mark free(lines);
4905 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4906 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4907 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4908 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4909 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
4910 48ae06ee 2018-10-18 stsp return err;
4913 f5215bb9 2019-02-22 stsp static void
4914 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4916 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4917 f5215bb9 2019-02-22 stsp update_panels();
4918 f5215bb9 2019-02-22 stsp doupdate();
4921 f44b1f58 2020-02-02 tracey static const struct got_error *
4922 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4924 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4926 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4927 f44b1f58 2020-02-02 tracey return NULL;
4930 eaf2c13e 2022-09-17 mark static void
4931 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4932 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4934 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4936 eaf2c13e 2022-09-17 mark *f = s->f;
4937 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4938 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4939 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4940 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4941 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4942 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4945 ec2a9698 2022-09-15 mark static const struct got_error *
4946 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4948 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4950 f44b1f58 2020-02-02 tracey int lineno;
4951 cb713507 2022-06-16 stsp char *line = NULL;
4952 826082fe 2020-12-10 stsp size_t linesize = 0;
4953 826082fe 2020-12-10 stsp ssize_t linelen;
4954 ec2a9698 2022-09-15 mark off_t *line_offsets;
4955 ec2a9698 2022-09-15 mark size_t nlines = 0;
4956 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4958 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4959 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4960 eaf2c13e 2022-09-17 mark "view search not supported");
4961 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4962 ec2a9698 2022-09-15 mark &match, &selected);
4964 f44b1f58 2020-02-02 tracey if (!view->searching) {
4965 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4966 f44b1f58 2020-02-02 tracey return NULL;
4969 ec2a9698 2022-09-15 mark if (*match) {
4970 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4971 ec2a9698 2022-09-15 mark lineno = *match + 1;
4973 ec2a9698 2022-09-15 mark lineno = *match - 1;
4975 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4977 f44b1f58 2020-02-02 tracey while (1) {
4978 f44b1f58 2020-02-02 tracey off_t offset;
4980 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4981 ec2a9698 2022-09-15 mark if (*match == 0) {
4982 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4986 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4987 f44b1f58 2020-02-02 tracey lineno = 1;
4989 ec2a9698 2022-09-15 mark lineno = nlines;
4992 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
4993 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
4994 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
4995 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
4996 f44b1f58 2020-02-02 tracey free(line);
4997 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4999 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
5000 cb713507 2022-06-16 stsp if (linelen != -1) {
5001 cb713507 2022-06-16 stsp char *exstr;
5002 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5005 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5006 cb713507 2022-06-16 stsp &view->regmatch)) {
5007 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5008 ec2a9698 2022-09-15 mark *match = lineno;
5009 cb713507 2022-06-16 stsp free(exstr);
5012 cb713507 2022-06-16 stsp free(exstr);
5014 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5015 f44b1f58 2020-02-02 tracey lineno++;
5017 f44b1f58 2020-02-02 tracey lineno--;
5019 826082fe 2020-12-10 stsp free(line);
5021 ec2a9698 2022-09-15 mark if (*match) {
5022 ec2a9698 2022-09-15 mark *first = *match;
5023 ec2a9698 2022-09-15 mark *selected = 1;
5026 145b6838 2022-06-16 stsp return err;
5029 48ae06ee 2018-10-18 stsp static const struct got_error *
5030 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
5032 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
5033 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5035 b72706c3 2022-06-01 stsp free(s->id1);
5036 b72706c3 2022-06-01 stsp s->id1 = NULL;
5037 b72706c3 2022-06-01 stsp free(s->id2);
5038 b72706c3 2022-06-01 stsp s->id2 = NULL;
5039 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
5040 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5041 b72706c3 2022-06-01 stsp s->f = NULL;
5042 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5043 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5044 b72706c3 2022-06-01 stsp s->f1 = NULL;
5045 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5046 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5047 b72706c3 2022-06-01 stsp s->f2 = NULL;
5048 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5049 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5050 f9d37699 2022-06-28 stsp s->fd1 = -1;
5051 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5052 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5053 f9d37699 2022-06-28 stsp s->fd2 = -1;
5054 c7d5c43c 2022-08-04 mark free(s->lines);
5055 c7d5c43c 2022-08-04 mark s->lines = NULL;
5056 b72706c3 2022-06-01 stsp s->nlines = 0;
5057 b72706c3 2022-06-01 stsp return err;
5060 b72706c3 2022-06-01 stsp static const struct got_error *
5061 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5062 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5063 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5064 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
5066 48ae06ee 2018-10-18 stsp const struct got_error *err;
5067 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5069 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
5070 f9d37699 2022-06-28 stsp s->fd1 = -1;
5071 f9d37699 2022-06-28 stsp s->fd2 = -1;
5073 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5074 15a94983 2018-12-23 stsp int type1, type2;
5075 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5077 15a94983 2018-12-23 stsp return err;
5078 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5080 15a94983 2018-12-23 stsp return err;
5082 15a94983 2018-12-23 stsp if (type1 != type2)
5083 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5085 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5086 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5087 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5088 f44b1f58 2020-02-02 tracey s->repo = repo;
5089 f44b1f58 2020-02-02 tracey s->id1 = id1;
5090 f44b1f58 2020-02-02 tracey s->id2 = id2;
5091 3dbaef42 2020-11-24 stsp s->label1 = label1;
5092 3dbaef42 2020-11-24 stsp s->label2 = label2;
5094 15a94983 2018-12-23 stsp if (id1) {
5095 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5096 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5097 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5099 5465d566 2020-02-01 tracey s->id1 = NULL;
5101 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5102 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5103 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5104 b72706c3 2022-06-01 stsp goto done;
5107 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5108 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5109 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5110 a00719e9 2022-06-17 stsp goto done;
5113 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5114 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5115 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5116 b72706c3 2022-06-01 stsp goto done;
5119 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5120 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5121 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5122 f9d37699 2022-06-28 stsp goto done;
5125 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5126 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5127 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5128 f9d37699 2022-06-28 stsp goto done;
5131 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5132 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5133 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5134 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5135 5465d566 2020-02-01 tracey s->repo = repo;
5137 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5140 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5141 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5142 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5143 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5144 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5145 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5146 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5147 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5148 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5149 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5150 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5151 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5152 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5153 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5154 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5155 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5156 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5157 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5158 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5159 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5160 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5161 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5162 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5163 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5164 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5165 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5166 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5167 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5168 b72706c3 2022-06-01 stsp goto done;
5172 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5173 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5174 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5175 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5177 5465d566 2020-02-01 tracey err = create_diff(s);
5179 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5180 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5181 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5182 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5183 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5184 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5185 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5188 b72706c3 2022-06-01 stsp close_diff_view(view);
5189 e5a0f69f 2018-08-18 stsp return err;
5192 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5193 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5195 a3404814 2018-09-02 stsp const struct got_error *err;
5196 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5197 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5198 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5200 a3404814 2018-09-02 stsp if (s->id1) {
5201 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5203 a3404814 2018-09-02 stsp return err;
5204 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5206 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5208 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5210 a3404814 2018-09-02 stsp return err;
5211 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5213 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5214 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5215 a3404814 2018-09-02 stsp free(id_str1);
5216 a3404814 2018-09-02 stsp free(id_str2);
5217 a3404814 2018-09-02 stsp return err;
5219 a3404814 2018-09-02 stsp free(id_str1);
5220 a3404814 2018-09-02 stsp free(id_str2);
5222 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5223 267bb3b8 2021-08-01 stsp free(header);
5224 267bb3b8 2021-08-01 stsp return err;
5227 15a087fe 2019-02-21 stsp static const struct got_error *
5228 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5229 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5231 d7a04538 2019-02-21 stsp const struct got_error *err;
5232 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5233 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5234 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5236 15a087fe 2019-02-21 stsp free(s->id2);
5237 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5238 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5239 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5241 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5243 d7a04538 2019-02-21 stsp return err;
5244 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5245 15a087fe 2019-02-21 stsp free(s->id1);
5246 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5247 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5248 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5249 15a087fe 2019-02-21 stsp return NULL;
5252 0cf4efb1 2018-09-29 stsp static const struct got_error *
5253 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5255 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5257 917d79a7 2022-07-01 stsp view->count = 0;
5258 917d79a7 2022-07-01 stsp wclear(view->window);
5259 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5260 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5261 917d79a7 2022-07-01 stsp s->matched_line = 0;
5262 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5263 917d79a7 2022-07-01 stsp return create_diff(s);
5266 c7d5c43c 2022-08-04 mark static void
5267 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5269 c7d5c43c 2022-08-04 mark int start, i;
5271 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5273 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5274 c7d5c43c 2022-08-04 mark if (i == 0)
5275 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5276 c7d5c43c 2022-08-04 mark if (--i == start)
5277 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5280 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5281 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5284 c7d5c43c 2022-08-04 mark static void
5285 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5287 c7d5c43c 2022-08-04 mark int start, i;
5289 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5291 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5292 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5294 c7d5c43c 2022-08-04 mark if (++i == start)
5295 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5298 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5299 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5302 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5303 c0f61fa4 2022-07-11 mark int, int, int);
5304 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5305 c0f61fa4 2022-07-11 mark int, int);
5307 917d79a7 2022-07-01 stsp static const struct got_error *
5308 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5310 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5311 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5312 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5313 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5314 826082fe 2020-12-10 stsp char *line = NULL;
5315 826082fe 2020-12-10 stsp size_t linesize = 0;
5316 826082fe 2020-12-10 stsp ssize_t linelen;
5317 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5319 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5321 e5a0f69f 2018-08-18 stsp switch (ch) {
5324 145b6838 2022-06-16 stsp case KEY_RIGHT:
5326 145b6838 2022-06-16 stsp case KEY_LEFT:
5328 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
5332 3f6c6614 2023-01-23 mark if (ch == 'a') {
5333 3f6c6614 2023-01-23 mark s->force_text_diff = !s->force_text_diff;
5334 3f6c6614 2023-01-23 mark view->action = s->force_text_diff ?
5335 3f6c6614 2023-01-23 mark "force ASCII text enabled" :
5336 3f6c6614 2023-01-23 mark "force ASCII text disabled";
5338 3f6c6614 2023-01-23 mark else if (ch == 'w') {
5339 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5340 3f6c6614 2023-01-23 mark view->action = s->ignore_whitespace ?
5341 3f6c6614 2023-01-23 mark "ignore whitespace enabled" :
5342 3f6c6614 2023-01-23 mark "ignore whitespace disabled";
5344 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5347 912a3f79 2021-08-30 j case KEY_HOME:
5348 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5349 640cd7ff 2022-06-22 mark view->count = 0;
5352 912a3f79 2021-08-30 j case KEY_END:
5353 640cd7ff 2022-06-22 mark view->count = 0;
5357 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5361 1e37a5c2 2019-05-12 jcs case KEY_UP:
5362 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5363 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5364 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5366 640cd7ff 2022-06-22 mark view->count = 0;
5368 83cc4199 2022-06-13 stsp case CTRL('u'):
5370 83cc4199 2022-06-13 stsp nscroll /= 2;
5371 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5372 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5373 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5375 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5376 640cd7ff 2022-06-22 mark view->count = 0;
5380 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5381 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5384 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5385 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5386 1e37a5c2 2019-05-12 jcs if (!s->eof)
5387 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5389 640cd7ff 2022-06-22 mark view->count = 0;
5391 83cc4199 2022-06-13 stsp case CTRL('d'):
5393 83cc4199 2022-06-13 stsp nscroll /= 2;
5394 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5395 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5396 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5399 640cd7ff 2022-06-22 mark if (s->eof) {
5400 640cd7ff 2022-06-22 mark view->count = 0;
5404 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5405 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5406 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5407 826082fe 2020-12-10 stsp if (linelen == -1) {
5408 826082fe 2020-12-10 stsp if (feof(s->f)) {
5409 826082fe 2020-12-10 stsp s->eof = 1;
5411 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5415 826082fe 2020-12-10 stsp free(line);
5418 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5421 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5424 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5427 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5430 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5431 1e37a5c2 2019-05-12 jcs s->diff_context--;
5432 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5433 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5434 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5435 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5436 27829c9e 2020-11-21 stsp s->nlines) {
5437 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5438 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5441 640cd7ff 2022-06-22 mark view->count = 0;
5444 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5445 1e37a5c2 2019-05-12 jcs s->diff_context++;
5446 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5447 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5448 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5450 640cd7ff 2022-06-22 mark view->count = 0;
5456 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5460 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5461 640cd7ff 2022-06-22 mark view->count = 0;
5464 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5466 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5467 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5468 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5470 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5471 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5474 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5476 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5479 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5482 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5483 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5484 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5486 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5487 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5488 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5490 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5491 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5494 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5496 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5498 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5499 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5500 c0f61fa4 2022-07-11 mark bs->selected_line);
5501 c0f61fa4 2022-07-11 mark if (id == NULL)
5504 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5507 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5511 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5512 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5513 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5514 145b6838 2022-06-16 stsp view->x = 0;
5516 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5517 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5520 640cd7ff 2022-06-22 mark view->count = 0;
5524 bcbd79e2 2018-08-19 stsp return err;
5527 4ed7e80c 2018-05-20 stsp static const struct got_error *
5528 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5530 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5531 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5532 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5533 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5534 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5535 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5536 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5537 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5538 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5539 3dbaef42 2020-11-24 stsp const char *errstr;
5540 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5541 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5543 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5544 26ed57b2 2018-05-19 stsp switch (ch) {
5546 64453f7e 2020-11-21 stsp force_text_diff = 1;
5549 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5551 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5552 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5553 5a20d08d 2022-02-09 op errstr, errstr);
5555 09b5bff8 2020-02-23 naddy case 'r':
5556 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5557 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5558 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5560 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5563 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5566 17020d27 2019-03-07 stsp usage_diff();
5567 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5571 26ed57b2 2018-05-19 stsp argc -= optind;
5572 26ed57b2 2018-05-19 stsp argv += optind;
5574 26ed57b2 2018-05-19 stsp if (argc == 0) {
5575 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5576 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5577 15a94983 2018-12-23 stsp id_str1 = argv[0];
5578 15a94983 2018-12-23 stsp id_str2 = argv[1];
5580 26ed57b2 2018-05-19 stsp usage_diff();
5582 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5583 0ae84acc 2022-06-15 tracey if (error)
5584 0ae84acc 2022-06-15 tracey goto done;
5586 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5587 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5588 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5589 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5590 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5591 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5592 c156c7a4 2020-12-18 stsp goto done;
5593 a273ac94 2020-02-23 naddy if (worktree)
5594 a273ac94 2020-02-23 naddy repo_path =
5595 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5597 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5598 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5599 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5600 c156c7a4 2020-12-18 stsp goto done;
5604 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5605 eb6600df 2019-01-04 stsp if (error)
5606 eb6600df 2019-01-04 stsp goto done;
5608 a273ac94 2020-02-23 naddy init_curses();
5610 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5611 51a10b52 2020-12-26 stsp if (error)
5612 51a10b52 2020-12-26 stsp goto done;
5614 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5615 26ed57b2 2018-05-19 stsp if (error)
5616 26ed57b2 2018-05-19 stsp goto done;
5618 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5619 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5620 26ed57b2 2018-05-19 stsp if (error)
5621 26ed57b2 2018-05-19 stsp goto done;
5623 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5624 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5625 26ed57b2 2018-05-19 stsp if (error)
5626 26ed57b2 2018-05-19 stsp goto done;
5628 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5629 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5630 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5631 ea5e7bb5 2018-08-01 stsp goto done;
5633 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5634 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5635 5dc9f4bc 2018-08-04 stsp if (error)
5636 5dc9f4bc 2018-08-04 stsp goto done;
5637 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5639 3dbaef42 2020-11-24 stsp free(label1);
5640 3dbaef42 2020-11-24 stsp free(label2);
5641 c02c541e 2019-03-29 stsp free(repo_path);
5642 a273ac94 2020-02-23 naddy free(cwd);
5643 1d0f4054 2021-06-17 stsp if (repo) {
5644 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5645 1d0f4054 2021-06-17 stsp if (error == NULL)
5646 1d0f4054 2021-06-17 stsp error = close_err;
5648 a273ac94 2020-02-23 naddy if (worktree)
5649 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5650 0ae84acc 2022-06-15 tracey if (pack_fds) {
5651 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5652 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5653 0ae84acc 2022-06-15 tracey if (error == NULL)
5654 0ae84acc 2022-06-15 tracey error = pack_err;
5656 51a10b52 2020-12-26 stsp tog_free_refs();
5657 26ed57b2 2018-05-19 stsp return error;
5660 4ed7e80c 2018-05-20 stsp __dead static void
5661 9f7d7167 2018-04-29 stsp usage_blame(void)
5664 87411fa9 2022-06-16 stsp fprintf(stderr,
5665 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5666 9f7d7167 2018-04-29 stsp getprogname());
5670 84451b3e 2018-07-10 stsp struct tog_blame_line {
5671 84451b3e 2018-07-10 stsp int annotated;
5672 84451b3e 2018-07-10 stsp struct got_object_id *id;
5675 4ed7e80c 2018-05-20 stsp static const struct got_error *
5676 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5678 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5679 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5680 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5681 84451b3e 2018-07-10 stsp const struct got_error *err;
5682 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5683 826082fe 2020-12-10 stsp char *line = NULL;
5684 826082fe 2020-12-10 stsp size_t linesize = 0;
5685 826082fe 2020-12-10 stsp ssize_t linelen;
5686 84451b3e 2018-07-10 stsp wchar_t *wline;
5687 27a741e5 2019-09-11 stsp int width;
5688 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5689 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5690 ab089a2a 2018-07-12 stsp char *id_str;
5691 11b20872 2019-11-08 stsp struct tog_color *tc;
5693 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5695 ab089a2a 2018-07-12 stsp return err;
5697 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5698 f7d12f7e 2018-08-01 stsp werase(view->window);
5700 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5701 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5702 ab089a2a 2018-07-12 stsp free(id_str);
5703 ab089a2a 2018-07-12 stsp return err;
5706 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5707 ab089a2a 2018-07-12 stsp free(line);
5708 2550e4c3 2018-07-13 stsp line = NULL;
5710 1cae65b4 2019-09-22 stsp return err;
5711 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5712 a3404814 2018-09-02 stsp wstandout(view->window);
5713 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5715 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5716 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5717 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5718 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5720 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5721 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5722 a3404814 2018-09-02 stsp wstandend(view->window);
5723 2550e4c3 2018-07-13 stsp free(wline);
5724 2550e4c3 2018-07-13 stsp wline = NULL;
5726 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5727 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5729 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5730 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5731 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5732 ab089a2a 2018-07-12 stsp free(id_str);
5733 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5735 ab089a2a 2018-07-12 stsp free(id_str);
5736 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5737 3f60a8ef 2018-07-10 stsp free(line);
5738 2550e4c3 2018-07-13 stsp line = NULL;
5740 3f60a8ef 2018-07-10 stsp return err;
5741 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5742 2550e4c3 2018-07-13 stsp free(wline);
5743 2550e4c3 2018-07-13 stsp wline = NULL;
5744 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5745 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5747 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5748 145b6838 2022-06-16 stsp view->maxx = 0;
5749 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5750 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5751 826082fe 2020-12-10 stsp if (linelen == -1) {
5752 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5753 826082fe 2020-12-10 stsp s->eof = 1;
5756 84451b3e 2018-07-10 stsp free(line);
5757 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5759 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5761 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5764 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5765 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5766 1853e0f4 2022-06-16 stsp if (err) {
5767 1853e0f4 2022-06-16 stsp free(line);
5768 1853e0f4 2022-06-16 stsp return err;
5770 1853e0f4 2022-06-16 stsp free(wline);
5771 1853e0f4 2022-06-16 stsp wline = NULL;
5772 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5774 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5775 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5777 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5778 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5779 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5780 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5781 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5782 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5783 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5784 8d0fe45a 2019-08-12 stsp char *id_str;
5785 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5786 87411fa9 2022-06-16 stsp blame_line->id);
5787 8d0fe45a 2019-08-12 stsp if (err) {
5788 8d0fe45a 2019-08-12 stsp free(line);
5789 8d0fe45a 2019-08-12 stsp return err;
5791 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5793 11b20872 2019-11-08 stsp wattr_on(view->window,
5794 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5795 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5797 11b20872 2019-11-08 stsp wattr_off(view->window,
5798 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5799 8d0fe45a 2019-08-12 stsp free(id_str);
5800 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5802 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5803 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5806 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5807 ee41ec32 2018-07-10 stsp prev_id = NULL;
5810 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5811 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5812 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5814 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5815 41605754 2020-11-12 stsp width = 9;
5816 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5817 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5818 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5819 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5820 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5821 41605754 2020-11-12 stsp if (err) {
5822 41605754 2020-11-12 stsp free(line);
5823 41605754 2020-11-12 stsp return err;
5825 41605754 2020-11-12 stsp width += 9;
5828 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5829 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5830 4cb9d869 2022-06-16 stsp if (err) {
5831 4cb9d869 2022-06-16 stsp free(line);
5832 4cb9d869 2022-06-16 stsp return err;
5834 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5835 a75b3e2d 2022-06-16 stsp width += 9;
5836 41605754 2020-11-12 stsp free(wline);
5837 41605754 2020-11-12 stsp wline = NULL;
5840 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5841 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5842 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5843 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5845 826082fe 2020-12-10 stsp free(line);
5846 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5848 9b058f45 2022-06-30 mark view_border(view);
5850 84451b3e 2018-07-10 stsp return NULL;
5853 84451b3e 2018-07-10 stsp static const struct got_error *
5854 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5855 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5857 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5858 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5859 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5860 1a76625f 2018-10-22 stsp int errcode;
5862 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5863 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5864 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5866 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5867 1a76625f 2018-10-22 stsp if (errcode)
5868 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5870 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5871 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5872 d68a0a7d 2018-07-10 stsp goto done;
5875 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5876 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5878 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5879 d68a0a7d 2018-07-10 stsp if (line->annotated)
5880 d68a0a7d 2018-07-10 stsp goto done;
5882 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5883 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5884 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5885 84451b3e 2018-07-10 stsp goto done;
5887 84451b3e 2018-07-10 stsp line->annotated = 1;
5889 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5890 1a76625f 2018-10-22 stsp if (errcode)
5891 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5892 84451b3e 2018-07-10 stsp return err;
5895 84451b3e 2018-07-10 stsp static void *
5896 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5898 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5899 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5900 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5901 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5902 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5904 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5905 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5906 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5908 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5909 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5910 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5911 e6e73e55 2022-06-30 tracey goto done;
5914 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5915 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5916 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5917 e6e73e55 2022-06-30 tracey goto done;
5919 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5920 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5921 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5922 e6e73e55 2022-06-30 tracey goto done;
5925 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5927 e6e73e55 2022-06-30 tracey goto done;
5929 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5930 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5931 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5932 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5933 fc06ba56 2019-08-22 stsp err = NULL;
5935 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5936 e6e73e55 2022-06-30 tracey if (errcode) {
5937 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5938 e6e73e55 2022-06-30 tracey goto done;
5941 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5942 1d0f4054 2021-06-17 stsp if (err == NULL)
5943 1d0f4054 2021-06-17 stsp err = close_err;
5944 c9beca56 2018-07-22 stsp ta->repo = NULL;
5945 c9beca56 2018-07-22 stsp *ta->complete = 1;
5947 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5948 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5949 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5952 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5953 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5954 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5955 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5956 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5957 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5958 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5959 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5961 18430de3 2018-07-10 stsp return (void *)err;
5964 245d91c1 2018-07-12 stsp static struct got_object_id *
5965 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5966 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5968 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5970 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5971 8d0fe45a 2019-08-12 stsp return NULL;
5973 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5974 c0f61fa4 2022-07-11 mark if (!line->annotated)
5975 c0f61fa4 2022-07-11 mark return NULL;
5977 c0f61fa4 2022-07-11 mark return line->id;
5980 c0f61fa4 2022-07-11 mark static struct got_object_id *
5981 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5982 c0f61fa4 2022-07-11 mark int lineno)
5984 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5986 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5987 c0f61fa4 2022-07-11 mark return NULL;
5989 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5990 245d91c1 2018-07-12 stsp if (!line->annotated)
5991 245d91c1 2018-07-12 stsp return NULL;
5993 245d91c1 2018-07-12 stsp return line->id;
5996 b880a918 2018-07-10 stsp static const struct got_error *
5997 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5999 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6002 245d91c1 2018-07-12 stsp if (blame->thread) {
6003 1a76625f 2018-10-22 stsp int errcode;
6004 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6005 1a76625f 2018-10-22 stsp if (errcode)
6006 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6007 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6008 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6009 1a76625f 2018-10-22 stsp if (errcode)
6010 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6011 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6012 1a76625f 2018-10-22 stsp if (errcode)
6013 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6014 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6015 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6016 245d91c1 2018-07-12 stsp err = NULL;
6017 245d91c1 2018-07-12 stsp blame->thread = NULL;
6019 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6020 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6021 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6022 1d0f4054 2021-06-17 stsp if (err == NULL)
6023 1d0f4054 2021-06-17 stsp err = close_err;
6024 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6026 245d91c1 2018-07-12 stsp if (blame->f) {
6027 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6028 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6029 245d91c1 2018-07-12 stsp blame->f = NULL;
6031 57670559 2018-12-23 stsp if (blame->lines) {
6032 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6033 57670559 2018-12-23 stsp free(blame->lines[i].id);
6034 57670559 2018-12-23 stsp free(blame->lines);
6035 57670559 2018-12-23 stsp blame->lines = NULL;
6037 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6038 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6039 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
6040 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6041 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
6042 0ae84acc 2022-06-15 tracey if (err == NULL)
6043 0ae84acc 2022-06-15 tracey err = pack_err;
6044 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
6046 245d91c1 2018-07-12 stsp return err;
6049 245d91c1 2018-07-12 stsp static const struct got_error *
6050 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6052 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6053 fc06ba56 2019-08-22 stsp int *done = arg;
6054 fc06ba56 2019-08-22 stsp int errcode;
6056 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6057 fc06ba56 2019-08-22 stsp if (errcode)
6058 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6059 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6061 fc06ba56 2019-08-22 stsp if (*done)
6062 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6064 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6065 fc06ba56 2019-08-22 stsp if (errcode)
6066 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6067 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6069 fc06ba56 2019-08-22 stsp return err;
6072 fc06ba56 2019-08-22 stsp static const struct got_error *
6073 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6075 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6076 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6077 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6078 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6079 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6080 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6081 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6082 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6083 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6085 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6086 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6088 15a94983 2018-12-23 stsp return err;
6090 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6091 eb81bc23 2022-06-28 tracey if (fd == -1) {
6092 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6093 eb81bc23 2022-06-28 tracey goto done;
6096 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6098 a44927cc 2022-04-07 stsp goto done;
6100 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6102 84451b3e 2018-07-10 stsp goto done;
6104 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6105 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6106 84451b3e 2018-07-10 stsp goto done;
6109 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6111 a70480e0 2018-06-23 stsp goto done;
6112 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6113 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6114 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6115 84451b3e 2018-07-10 stsp goto done;
6117 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6118 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6120 1fddf795 2021-01-20 stsp goto done;
6121 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6122 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6123 84451b3e 2018-07-10 stsp goto done;
6126 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6127 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6128 b02560ec 2019-08-19 stsp blame->nlines--;
6130 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6131 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6132 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6133 84451b3e 2018-07-10 stsp goto done;
6136 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6138 bd24772e 2018-07-11 stsp goto done;
6139 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6140 0ae84acc 2022-06-15 tracey pack_fds);
6141 0ae84acc 2022-06-15 tracey if (err)
6142 0ae84acc 2022-06-15 tracey goto done;
6144 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6145 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6146 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6147 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6148 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6149 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6150 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6151 245d91c1 2018-07-12 stsp goto done;
6153 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6155 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6156 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6157 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6158 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6159 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6160 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6161 a5388363 2020-12-01 naddy s->blame_complete = 0;
6163 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6164 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6165 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6166 f5a09613 2020-12-13 naddy s->selected_line = 1;
6168 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6171 a44927cc 2022-04-07 stsp if (commit)
6172 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6173 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6174 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6176 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6177 27d434c2 2018-09-15 stsp free(obj_id);
6179 245d91c1 2018-07-12 stsp stop_blame(blame);
6180 245d91c1 2018-07-12 stsp return err;
6183 245d91c1 2018-07-12 stsp static const struct got_error *
6184 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6185 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6187 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6188 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6190 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6192 c4843652 2019-08-12 stsp s->path = strdup(path);
6193 c4843652 2019-08-12 stsp if (s->path == NULL)
6194 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6196 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6197 c4843652 2019-08-12 stsp if (err) {
6198 c4843652 2019-08-12 stsp free(s->path);
6199 7cbe629d 2018-08-04 stsp return err;
6202 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6203 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6204 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6205 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6206 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6207 fb2756b9 2018-08-04 stsp s->repo = repo;
6208 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6209 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6211 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6212 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6213 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6214 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6216 11b20872 2019-11-08 stsp return err;
6219 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6220 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6221 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6222 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6223 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6224 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6225 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6227 a5388363 2020-12-01 naddy return run_blame(view);
6230 e5a0f69f 2018-08-18 stsp static const struct got_error *
6231 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6233 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6234 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6236 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6237 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6239 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6240 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6241 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6242 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6243 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6246 e5a0f69f 2018-08-18 stsp free(s->path);
6247 11b20872 2019-11-08 stsp free_colors(&s->colors);
6248 e5a0f69f 2018-08-18 stsp return err;
6251 7cbe629d 2018-08-04 stsp static const struct got_error *
6252 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6254 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6256 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6257 6c4c42e0 2019-06-24 stsp return NULL;
6260 eaf2c13e 2022-09-17 mark static void
6261 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6262 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6264 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6266 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6267 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6268 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6269 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6270 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6271 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6272 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6275 6c4c42e0 2019-06-24 stsp static const struct got_error *
6276 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6278 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6279 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6280 2b380cc8 2018-10-24 stsp int errcode;
6282 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6283 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6284 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6285 2b380cc8 2018-10-24 stsp if (errcode)
6286 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6288 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6291 51fe7530 2019-08-19 stsp if (s->blame_complete)
6292 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6294 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6296 9b058f45 2022-06-30 mark view_border(view);
6297 e5a0f69f 2018-08-18 stsp return err;
6300 e5a0f69f 2018-08-18 stsp static const struct got_error *
6301 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6302 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6304 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6305 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6307 05f04cdf 2022-07-20 mark *new_view = NULL;
6309 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6310 05f04cdf 2022-07-20 mark if (log_view == NULL)
6311 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6313 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6315 05f04cdf 2022-07-20 mark view_close(log_view);
6317 05f04cdf 2022-07-20 mark *new_view = log_view;
6319 05f04cdf 2022-07-20 mark return err;
6322 05f04cdf 2022-07-20 mark static const struct got_error *
6323 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6325 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6326 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6327 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6328 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6330 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6331 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6332 9b058f45 2022-06-30 mark --eos; /* border */
6334 e5a0f69f 2018-08-18 stsp switch (ch) {
6337 145b6838 2022-06-16 stsp case KEY_RIGHT:
6339 145b6838 2022-06-16 stsp case KEY_LEFT:
6341 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
6344 1e37a5c2 2019-05-12 jcs s->done = 1;
6346 4deef56f 2021-09-02 naddy case 'g':
6347 4deef56f 2021-09-02 naddy case KEY_HOME:
6348 4deef56f 2021-09-02 naddy s->selected_line = 1;
6349 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6350 640cd7ff 2022-06-22 mark view->count = 0;
6352 4deef56f 2021-09-02 naddy case 'G':
6353 4deef56f 2021-09-02 naddy case KEY_END:
6354 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6355 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6356 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6358 9b058f45 2022-06-30 mark s->selected_line = eos;
6359 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6361 640cd7ff 2022-06-22 mark view->count = 0;
6364 1e37a5c2 2019-05-12 jcs case KEY_UP:
6365 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6366 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6367 1e37a5c2 2019-05-12 jcs s->selected_line--;
6368 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6369 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6370 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6372 640cd7ff 2022-06-22 mark view->count = 0;
6374 83cc4199 2022-06-13 stsp case CTRL('u'):
6376 83cc4199 2022-06-13 stsp nscroll /= 2;
6377 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6378 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6379 ea025d1d 2020-02-22 naddy case CTRL('b'):
6381 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6382 640cd7ff 2022-06-22 mark if (view->count > 1)
6383 640cd7ff 2022-06-22 mark nscroll += nscroll;
6384 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6385 640cd7ff 2022-06-22 mark view->count = 0;
6388 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6389 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6391 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6394 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6395 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6396 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6397 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6398 1e37a5c2 2019-05-12 jcs s->selected_line++;
6399 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6400 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6402 640cd7ff 2022-06-22 mark view->count = 0;
6405 1e37a5c2 2019-05-12 jcs case 'p': {
6406 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6408 640cd7ff 2022-06-22 mark view->count = 0;
6409 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6410 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6411 1e37a5c2 2019-05-12 jcs if (id == NULL)
6413 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6414 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6415 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6416 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6417 1e37a5c2 2019-05-12 jcs int obj_type;
6418 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6419 1e37a5c2 2019-05-12 jcs s->repo, id);
6422 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6423 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6424 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6425 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6428 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6429 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6430 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6433 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6434 a44927cc 2022-04-07 stsp pcommit, s->path);
6435 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6436 e5a0f69f 2018-08-18 stsp if (err) {
6437 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6438 1e37a5c2 2019-05-12 jcs err = NULL;
6439 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6442 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6444 1e37a5c2 2019-05-12 jcs free(blob_id);
6445 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6446 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6447 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6450 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6451 d7b5a0e8 2022-04-20 stsp &pid->id);
6452 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6454 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6455 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6457 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6462 1e37a5c2 2019-05-12 jcs s->done = 1;
6463 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6464 1e37a5c2 2019-05-12 jcs s->done = 0;
6465 1e37a5c2 2019-05-12 jcs if (thread_err)
6467 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6468 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6469 a5388363 2020-12-01 naddy err = run_blame(view);
6474 61417565 2022-06-20 mark case 'C': {
6475 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6477 640cd7ff 2022-06-22 mark view->count = 0;
6478 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6479 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6481 1e37a5c2 2019-05-12 jcs s->done = 1;
6482 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6483 1e37a5c2 2019-05-12 jcs s->done = 0;
6484 1e37a5c2 2019-05-12 jcs if (thread_err)
6486 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6487 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6488 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6489 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6490 a5388363 2020-12-01 naddy err = run_blame(view);
6496 05f04cdf 2022-07-20 mark view->count = 0;
6497 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6498 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6499 136e2bd4 2022-07-23 mark if (s->id_to_log)
6500 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6502 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6503 1e37a5c2 2019-05-12 jcs case '\r': {
6504 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6505 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6506 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6508 640cd7ff 2022-06-22 mark view->count = 0;
6509 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6510 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6511 1e37a5c2 2019-05-12 jcs if (id == NULL)
6513 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6516 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6517 c0f61fa4 2022-07-11 mark if (*new_view) {
6518 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6519 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6522 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6524 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6525 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6527 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6528 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6529 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6530 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6531 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6535 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6536 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6537 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6539 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6542 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6543 c0f61fa4 2022-07-11 mark s->selected_line;
6544 c0f61fa4 2022-07-11 mark if (*new_view)
6545 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6546 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6547 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6548 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6553 e78dc838 2020-12-04 stsp view->focussed = 0;
6554 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6555 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6556 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6557 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6558 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6559 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6562 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6565 e78dc838 2020-12-04 stsp view->focus_child = 1;
6567 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6572 83cc4199 2022-06-13 stsp case CTRL('d'):
6574 83cc4199 2022-06-13 stsp nscroll /= 2;
6575 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6576 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6577 ea025d1d 2020-02-22 naddy case CTRL('f'):
6580 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6581 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6582 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6583 640cd7ff 2022-06-22 mark view->count = 0;
6586 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6587 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6588 83cc4199 2022-06-13 stsp s->selected_line +=
6589 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6590 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6592 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6593 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6595 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6596 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6598 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6599 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6600 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6601 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6605 640cd7ff 2022-06-22 mark view->count = 0;
6608 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6611 917d79a7 2022-07-01 stsp static const struct got_error *
6612 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6614 917d79a7 2022-07-01 stsp const struct got_error *err;
6615 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6617 917d79a7 2022-07-01 stsp view->count = 0;
6618 917d79a7 2022-07-01 stsp s->done = 1;
6619 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6620 917d79a7 2022-07-01 stsp s->done = 0;
6622 917d79a7 2022-07-01 stsp return err;
6623 917d79a7 2022-07-01 stsp return run_blame(view);
6626 a70480e0 2018-06-23 stsp static const struct got_error *
6627 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6629 a70480e0 2018-06-23 stsp const struct got_error *error;
6630 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6631 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6632 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6633 0587e10c 2020-07-23 stsp char *link_target = NULL;
6634 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6635 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6636 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6638 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6639 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6641 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6642 a70480e0 2018-06-23 stsp switch (ch) {
6644 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6647 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6648 69069811 2018-08-02 stsp if (repo_path == NULL)
6649 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6653 17020d27 2019-03-07 stsp usage_blame();
6654 a70480e0 2018-06-23 stsp /* NOTREACHED */
6658 a70480e0 2018-06-23 stsp argc -= optind;
6659 a70480e0 2018-06-23 stsp argv += optind;
6661 f135c941 2020-02-20 stsp if (argc != 1)
6662 a70480e0 2018-06-23 stsp usage_blame();
6664 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6665 0ae84acc 2022-06-15 tracey if (error != NULL)
6666 0ae84acc 2022-06-15 tracey goto done;
6668 69069811 2018-08-02 stsp if (repo_path == NULL) {
6669 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6670 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6671 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6672 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6673 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6674 c156c7a4 2020-12-18 stsp goto done;
6675 f135c941 2020-02-20 stsp if (worktree)
6676 eb41ed75 2019-02-05 stsp repo_path =
6677 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6679 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6680 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6681 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6682 c156c7a4 2020-12-18 stsp goto done;
6686 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6687 c02c541e 2019-03-29 stsp if (error != NULL)
6688 8e94dd5b 2019-01-04 stsp goto done;
6690 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6691 f135c941 2020-02-20 stsp worktree);
6692 c02c541e 2019-03-29 stsp if (error)
6693 92205607 2019-01-04 stsp goto done;
6695 f135c941 2020-02-20 stsp init_curses();
6697 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6698 51a10b52 2020-12-26 stsp if (error)
6699 51a10b52 2020-12-26 stsp goto done;
6701 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6702 eb41ed75 2019-02-05 stsp if (error)
6703 69069811 2018-08-02 stsp goto done;
6705 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6706 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6707 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6708 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6709 a70480e0 2018-06-23 stsp if (error != NULL)
6710 66b4983c 2018-06-23 stsp goto done;
6711 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6712 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6714 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6715 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6717 a19e88aa 2018-06-23 stsp if (error != NULL)
6718 8b473291 2019-02-21 stsp goto done;
6720 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6721 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6722 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6723 e1cd8fed 2018-08-01 stsp goto done;
6726 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6727 a44927cc 2022-04-07 stsp if (error)
6728 a44927cc 2022-04-07 stsp goto done;
6730 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6731 a44927cc 2022-04-07 stsp commit, repo);
6732 7cbe629d 2018-08-04 stsp if (error)
6733 7cbe629d 2018-08-04 stsp goto done;
6735 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6736 78756c87 2020-11-24 stsp commit_id, repo);
6737 0587e10c 2020-07-23 stsp if (error)
6738 0587e10c 2020-07-23 stsp goto done;
6739 12314ad4 2019-08-31 stsp if (worktree) {
6740 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6741 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6742 12314ad4 2019-08-31 stsp worktree = NULL;
6744 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6746 69069811 2018-08-02 stsp free(repo_path);
6747 f135c941 2020-02-20 stsp free(in_repo_path);
6748 0587e10c 2020-07-23 stsp free(link_target);
6749 69069811 2018-08-02 stsp free(cwd);
6750 a70480e0 2018-06-23 stsp free(commit_id);
6751 a44927cc 2022-04-07 stsp if (commit)
6752 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6753 eb41ed75 2019-02-05 stsp if (worktree)
6754 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6755 1d0f4054 2021-06-17 stsp if (repo) {
6756 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6757 1d0f4054 2021-06-17 stsp if (error == NULL)
6758 1d0f4054 2021-06-17 stsp error = close_err;
6760 0ae84acc 2022-06-15 tracey if (pack_fds) {
6761 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6762 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6763 0ae84acc 2022-06-15 tracey if (error == NULL)
6764 0ae84acc 2022-06-15 tracey error = pack_err;
6766 51a10b52 2020-12-26 stsp tog_free_refs();
6767 a70480e0 2018-06-23 stsp return error;
6770 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6771 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6773 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6774 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6775 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6776 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6777 0c6ad1bc 2022-09-09 mark char *index = NULL;
6778 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6779 d627976f 2023-02-01 mark int width, n, nentries, scrollx, i = 1;
6780 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6782 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6783 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6784 9b058f45 2022-06-30 mark --limit; /* border */
6786 f7d12f7e 2018-08-01 stsp werase(view->window);
6788 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6789 ffd1d5e5 2018-06-23 stsp return NULL;
6791 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6794 ffd1d5e5 2018-06-23 stsp return err;
6795 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6796 a3404814 2018-09-02 stsp wstandout(view->window);
6797 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6799 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6800 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6801 0c6ad1bc 2022-09-09 mark free(wline);
6802 0c6ad1bc 2022-09-09 mark wline = NULL;
6803 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6804 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6806 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6807 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6808 a3404814 2018-09-02 stsp wstandend(view->window);
6809 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6810 0c6ad1bc 2022-09-09 mark return NULL;
6812 df68a56b 2022-08-12 mark i += s->selected;
6813 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6814 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6815 df68a56b 2022-08-12 mark if (s->tree != s->root)
6816 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6818 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6819 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6820 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6821 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6822 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6823 0c6ad1bc 2022-09-09 mark free(index);
6825 ce52c690 2018-06-23 stsp return err;
6826 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6827 2550e4c3 2018-07-13 stsp free(wline);
6828 2550e4c3 2018-07-13 stsp wline = NULL;
6829 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6830 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6831 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6832 ffd1d5e5 2018-06-23 stsp return NULL;
6833 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6834 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6835 a1eca9bb 2018-06-23 stsp return NULL;
6837 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6838 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6839 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6840 0cf4efb1 2018-09-29 stsp if (view->focussed)
6841 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6842 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6844 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6845 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6846 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6847 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6848 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6849 ffd1d5e5 2018-06-23 stsp return NULL;
6853 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6856 d627976f 2023-02-01 mark view->maxx = 0;
6857 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6858 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6859 848d6979 2019-08-12 stsp const char *modestr = "";
6860 56e0773d 2019-11-28 stsp mode_t mode;
6862 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6863 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6865 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6866 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6867 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6869 638f9024 2019-05-13 stsp return got_error_from_errno(
6870 230a42bd 2019-05-11 jcs "got_object_id_str");
6872 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6873 63c5ca5d 2019-08-24 stsp modestr = "$";
6874 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6877 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6878 d86d3b18 2020-12-01 naddy te, s->repo);
6879 0d6c6ee3 2020-05-20 stsp if (err) {
6880 0d6c6ee3 2020-05-20 stsp free(id_str);
6881 0d6c6ee3 2020-05-20 stsp return err;
6883 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6884 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6885 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6887 848d6979 2019-08-12 stsp modestr = "@";
6889 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6890 848d6979 2019-08-12 stsp modestr = "/";
6891 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6892 848d6979 2019-08-12 stsp modestr = "*";
6893 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6894 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6895 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6896 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6897 1d13200f 2018-07-12 stsp free(id_str);
6898 0d6c6ee3 2020-05-20 stsp free(link_target);
6899 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6901 1d13200f 2018-07-12 stsp free(id_str);
6902 0d6c6ee3 2020-05-20 stsp free(link_target);
6904 d627976f 2023-02-01 mark /* use full line width to determine view->maxx */
6905 d627976f 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6906 ffd1d5e5 2018-06-23 stsp if (err) {
6907 ffd1d5e5 2018-06-23 stsp free(line);
6910 d627976f 2023-02-01 mark view->maxx = MAX(view->maxx, width);
6911 d627976f 2023-02-01 mark free(wline);
6912 d627976f 2023-02-01 mark wline = NULL;
6914 d627976f 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
6915 d627976f 2023-02-01 mark view->ncols, 0, 0);
6916 d627976f 2023-02-01 mark if (err) {
6917 d627976f 2023-02-01 mark free(line);
6920 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6921 0cf4efb1 2018-09-29 stsp if (view->focussed)
6922 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6923 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6925 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6927 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6928 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6929 d627976f 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
6931 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6932 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6933 b637f33f 2023-02-01 mark if (width < view->ncols)
6934 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6935 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6936 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6937 ffd1d5e5 2018-06-23 stsp free(line);
6938 2550e4c3 2018-07-13 stsp free(wline);
6939 2550e4c3 2018-07-13 stsp wline = NULL;
6941 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6942 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6943 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6947 ffd1d5e5 2018-06-23 stsp return err;
6950 ffd1d5e5 2018-06-23 stsp static void
6951 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6953 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6954 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6955 fa86c4bf 2020-11-29 stsp int i = 0;
6957 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6960 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6961 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6962 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6963 fa86c4bf 2020-11-29 stsp if (!isroot)
6964 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6967 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6968 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6972 9b058f45 2022-06-30 mark static const struct got_error *
6973 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6975 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6976 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6977 ffd1d5e5 2018-06-23 stsp int n = 0;
6979 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6980 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6981 694d3271 2020-12-01 naddy s->first_displayed_entry);
6983 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6985 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6986 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6987 94b80cfa 2022-08-01 mark if (last) {
6988 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6989 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6991 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6992 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6993 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6997 9b058f45 2022-06-30 mark return NULL;
7000 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7001 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7002 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7004 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7005 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7006 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7008 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7009 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7010 56e0773d 2019-11-28 stsp + 1 /* slash */;
7012 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7014 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7015 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7016 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7018 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7019 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7020 d9765a41 2018-06-23 stsp while (pt) {
7021 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7022 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7023 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7024 cb2ebc8a 2018-06-23 stsp goto done;
7026 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7027 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7028 cb2ebc8a 2018-06-23 stsp goto done;
7030 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7033 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7034 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7035 ce52c690 2018-06-23 stsp goto done;
7039 ce52c690 2018-06-23 stsp if (err) {
7040 ce52c690 2018-06-23 stsp free(*path);
7041 ce52c690 2018-06-23 stsp *path = NULL;
7043 ce52c690 2018-06-23 stsp return err;
7046 ce52c690 2018-06-23 stsp static const struct got_error *
7047 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7048 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7049 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7051 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7052 ce52c690 2018-06-23 stsp char *path;
7053 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7055 a0de39f3 2019-08-09 stsp *new_view = NULL;
7057 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7059 ce52c690 2018-06-23 stsp return err;
7061 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7062 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7063 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7064 83ce39e3 2019-08-12 stsp goto done;
7067 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7068 e5a0f69f 2018-08-18 stsp if (err) {
7069 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7070 fc06ba56 2019-08-22 stsp err = NULL;
7071 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7073 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7075 83ce39e3 2019-08-12 stsp free(path);
7076 69efd4c4 2018-07-18 stsp return err;
7079 69efd4c4 2018-07-18 stsp static const struct got_error *
7080 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7081 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7083 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7084 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7085 69efd4c4 2018-07-18 stsp char *path;
7087 a0de39f3 2019-08-09 stsp *new_view = NULL;
7089 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7090 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7091 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7093 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7095 69efd4c4 2018-07-18 stsp return err;
7097 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7100 e5a0f69f 2018-08-18 stsp view_close(log_view);
7102 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7103 cb2ebc8a 2018-06-23 stsp free(path);
7104 cb2ebc8a 2018-06-23 stsp return err;
7107 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7108 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7109 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7111 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7112 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7113 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7114 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7116 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7117 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7119 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7120 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7121 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7123 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7125 bc573f3b 2021-07-10 stsp goto done;
7128 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7129 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7130 bc573f3b 2021-07-10 stsp * closed on demand.
7132 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7133 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7135 bc573f3b 2021-07-10 stsp goto done;
7136 bc573f3b 2021-07-10 stsp s->tree = s->root;
7138 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7139 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7140 ffd1d5e5 2018-06-23 stsp goto done;
7142 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7143 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7144 ffd1d5e5 2018-06-23 stsp goto done;
7147 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7148 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7149 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7150 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7151 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7152 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7153 9cd7cbd1 2020-12-07 stsp goto done;
7156 fb2756b9 2018-08-04 stsp s->repo = repo;
7158 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7159 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7160 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7161 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7163 c0b01bdb 2019-11-08 stsp goto done;
7164 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7165 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7167 c0b01bdb 2019-11-08 stsp goto done;
7168 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7169 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7170 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7172 c0b01bdb 2019-11-08 stsp goto done;
7174 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7175 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7176 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7178 11b20872 2019-11-08 stsp goto done;
7180 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7181 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7183 c0b01bdb 2019-11-08 stsp goto done;
7186 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7187 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7188 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7189 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7190 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7192 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7193 bc573f3b 2021-07-10 stsp if (commit)
7194 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7196 bc573f3b 2021-07-10 stsp close_tree_view(view);
7197 ad80ab7b 2018-08-04 stsp return err;
7200 e5a0f69f 2018-08-18 stsp static const struct got_error *
7201 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7203 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7205 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7206 fb2756b9 2018-08-04 stsp free(s->tree_label);
7207 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7208 6484ec90 2018-09-29 stsp free(s->commit_id);
7209 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7210 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7211 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7212 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7213 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7214 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7215 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7216 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7217 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7218 ad80ab7b 2018-08-04 stsp free(parent);
7221 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7222 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7223 bc573f3b 2021-07-10 stsp if (s->root)
7224 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7225 7c32bd05 2019-06-22 stsp return NULL;
7228 7c32bd05 2019-06-22 stsp static const struct got_error *
7229 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7231 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7233 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7234 7c32bd05 2019-06-22 stsp return NULL;
7237 7c32bd05 2019-06-22 stsp static int
7238 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7240 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7242 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7246 7c32bd05 2019-06-22 stsp static const struct got_error *
7247 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7249 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7250 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7252 7c32bd05 2019-06-22 stsp if (!view->searching) {
7253 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7254 7c32bd05 2019-06-22 stsp return NULL;
7257 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7258 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7259 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7260 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7261 56e0773d 2019-11-28 stsp s->selected_entry);
7263 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7265 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7266 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7268 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7269 56e0773d 2019-11-28 stsp s->selected_entry);
7272 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7273 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7274 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7275 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7277 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7280 7c32bd05 2019-06-22 stsp while (1) {
7281 56e0773d 2019-11-28 stsp if (te == NULL) {
7282 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7283 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7284 ac66afb8 2019-06-24 stsp return NULL;
7286 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7287 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7289 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7292 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7293 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7294 56e0773d 2019-11-28 stsp s->matched_entry = te;
7298 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7299 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7301 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7304 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7305 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7306 7c32bd05 2019-06-22 stsp s->selected = 0;
7309 e5a0f69f 2018-08-18 stsp return NULL;
7312 ad80ab7b 2018-08-04 stsp static const struct got_error *
7313 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7315 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7316 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7317 e5a0f69f 2018-08-18 stsp char *parent_path;
7319 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7321 e5a0f69f 2018-08-18 stsp return err;
7323 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7324 e5a0f69f 2018-08-18 stsp free(parent_path);
7326 9b058f45 2022-06-30 mark view_border(view);
7327 e5a0f69f 2018-08-18 stsp return err;
7330 94b80cfa 2022-08-01 mark static const struct got_error *
7331 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7333 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7334 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7335 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7336 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7337 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7338 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7340 94b80cfa 2022-08-01 mark g = view->gline;
7341 94b80cfa 2022-08-01 mark view->gline = 0;
7343 94b80cfa 2022-08-01 mark if (g == 0)
7345 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7346 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7348 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7349 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7350 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7352 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7353 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7354 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7356 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7357 94b80cfa 2022-08-01 mark last += off;
7359 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7360 94b80cfa 2022-08-01 mark s->selected = g - first;
7361 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7364 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7365 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7369 94b80cfa 2022-08-01 mark if (i < g) {
7370 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7372 94b80cfa 2022-08-01 mark return err;
7373 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7374 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7375 94b80cfa 2022-08-01 mark first + s->selected < g &&
7376 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7377 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7378 94b80cfa 2022-08-01 mark first += off;
7379 94b80cfa 2022-08-01 mark s->selected = g - first;
7381 94b80cfa 2022-08-01 mark } else if (i > g)
7382 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7384 94b80cfa 2022-08-01 mark if (g < nlines &&
7385 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7386 94b80cfa 2022-08-01 mark s->selected = g - 1;
7388 94b80cfa 2022-08-01 mark return NULL;
7391 e5a0f69f 2018-08-18 stsp static const struct got_error *
7392 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7394 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7395 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7396 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7397 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7399 94b80cfa 2022-08-01 mark if (view->gline)
7400 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7402 e5a0f69f 2018-08-18 stsp switch (ch) {
7405 d627976f 2023-02-01 mark case KEY_RIGHT:
7407 d627976f 2023-02-01 mark case KEY_LEFT:
7409 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
7412 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7413 640cd7ff 2022-06-22 mark view->count = 0;
7416 640cd7ff 2022-06-22 mark view->count = 0;
7417 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7419 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7422 640cd7ff 2022-06-22 mark view->count = 0;
7423 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7425 e4526bf5 2021-09-03 naddy case 'g':
7427 e4526bf5 2021-09-03 naddy case KEY_HOME:
7428 e4526bf5 2021-09-03 naddy s->selected = 0;
7429 640cd7ff 2022-06-22 mark view->count = 0;
7430 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7431 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7432 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7434 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7436 e4526bf5 2021-09-03 naddy case 'G':
7438 9b058f45 2022-06-30 mark case KEY_END: {
7439 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7441 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7442 9b058f45 2022-06-30 mark --eos; /* border */
7443 e4526bf5 2021-09-03 naddy s->selected = 0;
7444 640cd7ff 2022-06-22 mark view->count = 0;
7445 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7446 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7447 e4526bf5 2021-09-03 naddy if (te == NULL) {
7448 ad055527 2022-07-16 mark if (s->tree != s->root) {
7449 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7454 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7455 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7457 e4526bf5 2021-09-03 naddy if (n > 0)
7458 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7462 1e37a5c2 2019-05-12 jcs case KEY_UP:
7463 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7464 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7465 1e37a5c2 2019-05-12 jcs s->selected--;
7468 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7469 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7470 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7471 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7472 640cd7ff 2022-06-22 mark view->count = 0;
7474 83cc4199 2022-06-13 stsp case CTRL('u'):
7476 83cc4199 2022-06-13 stsp nscroll /= 2;
7477 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7478 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7479 ea025d1d 2020-02-22 naddy case CTRL('b'):
7481 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7482 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7483 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7484 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7486 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7487 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7489 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7490 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7491 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7492 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7493 640cd7ff 2022-06-22 mark view->count = 0;
7496 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7497 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7498 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7499 1e37a5c2 2019-05-12 jcs s->selected++;
7502 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7503 640cd7ff 2022-06-22 mark == NULL) {
7504 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7505 640cd7ff 2022-06-22 mark view->count = 0;
7508 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7510 83cc4199 2022-06-13 stsp case CTRL('d'):
7512 83cc4199 2022-06-13 stsp nscroll /= 2;
7513 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7514 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7515 ea025d1d 2020-02-22 naddy case CTRL('f'):
7517 48bb96f0 2022-06-20 naddy case ' ':
7518 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7520 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7521 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7522 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7523 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7525 640cd7ff 2022-06-22 mark view->count = 0;
7528 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7530 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7532 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7533 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7534 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7535 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7536 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7537 640cd7ff 2022-06-22 mark view->count = 0;
7540 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7541 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7543 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7544 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7545 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7546 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7547 1e37a5c2 2019-05-12 jcs s->selected_entry =
7548 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7549 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7550 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7551 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7555 1e37a5c2 2019-05-12 jcs free(parent);
7556 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7557 56e0773d 2019-11-28 stsp s->selected_entry))) {
7558 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7559 640cd7ff 2022-06-22 mark view->count = 0;
7560 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7561 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7564 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7565 941e9f74 2019-05-21 stsp if (err) {
7566 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7569 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7570 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7572 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7573 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7574 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7575 640cd7ff 2022-06-22 mark view->count = 0;
7578 640cd7ff 2022-06-22 mark view->count = 0;
7582 ffd1d5e5 2018-06-23 stsp return err;
7585 ffd1d5e5 2018-06-23 stsp __dead static void
7586 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7589 87411fa9 2022-06-16 stsp fprintf(stderr,
7590 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7591 ffd1d5e5 2018-06-23 stsp getprogname());
7595 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7596 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7598 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7599 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7600 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7601 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7602 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7603 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7604 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7605 4e97c21c 2020-12-06 stsp char *label = NULL;
7606 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7607 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7609 5221c383 2018-08-01 stsp struct tog_view *view;
7610 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7612 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7613 ffd1d5e5 2018-06-23 stsp switch (ch) {
7615 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7618 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7619 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7620 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7624 e99e2d15 2020-11-24 naddy usage_tree();
7625 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7629 ffd1d5e5 2018-06-23 stsp argc -= optind;
7630 ffd1d5e5 2018-06-23 stsp argv += optind;
7632 55cccc34 2020-02-20 stsp if (argc > 1)
7633 e99e2d15 2020-11-24 naddy usage_tree();
7635 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7636 0ae84acc 2022-06-15 tracey if (error != NULL)
7637 0ae84acc 2022-06-15 tracey goto done;
7639 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7640 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7641 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7642 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7643 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7644 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7645 c156c7a4 2020-12-18 stsp goto done;
7646 55cccc34 2020-02-20 stsp if (worktree)
7647 52185f70 2019-02-05 stsp repo_path =
7648 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7650 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7651 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7652 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7653 c156c7a4 2020-12-18 stsp goto done;
7657 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7658 c02c541e 2019-03-29 stsp if (error != NULL)
7659 52185f70 2019-02-05 stsp goto done;
7661 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7662 55cccc34 2020-02-20 stsp repo, worktree);
7663 55cccc34 2020-02-20 stsp if (error)
7664 55cccc34 2020-02-20 stsp goto done;
7666 55cccc34 2020-02-20 stsp init_curses();
7668 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7669 c02c541e 2019-03-29 stsp if (error)
7670 52185f70 2019-02-05 stsp goto done;
7672 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7673 51a10b52 2020-12-26 stsp if (error)
7674 51a10b52 2020-12-26 stsp goto done;
7676 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7677 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7678 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7679 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7680 4e97c21c 2020-12-06 stsp if (error)
7681 4e97c21c 2020-12-06 stsp goto done;
7682 4e97c21c 2020-12-06 stsp head_ref_name = label;
7684 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7685 4e97c21c 2020-12-06 stsp if (error == NULL)
7686 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7687 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7688 4e97c21c 2020-12-06 stsp goto done;
7689 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7690 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7691 4e97c21c 2020-12-06 stsp if (error)
7692 4e97c21c 2020-12-06 stsp goto done;
7695 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7696 a44927cc 2022-04-07 stsp if (error)
7697 a44927cc 2022-04-07 stsp goto done;
7699 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7700 5221c383 2018-08-01 stsp if (view == NULL) {
7701 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7702 5221c383 2018-08-01 stsp goto done;
7704 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7705 ad80ab7b 2018-08-04 stsp if (error)
7706 ad80ab7b 2018-08-04 stsp goto done;
7707 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7708 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7709 d91faf3b 2020-12-01 naddy in_repo_path);
7710 55cccc34 2020-02-20 stsp if (error)
7711 55cccc34 2020-02-20 stsp goto done;
7714 55cccc34 2020-02-20 stsp if (worktree) {
7715 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7716 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7717 55cccc34 2020-02-20 stsp worktree = NULL;
7719 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7721 52185f70 2019-02-05 stsp free(repo_path);
7722 e4a0e26d 2020-02-20 stsp free(cwd);
7723 ffd1d5e5 2018-06-23 stsp free(commit_id);
7724 4e97c21c 2020-12-06 stsp free(label);
7726 486cd271 2020-12-06 stsp got_ref_close(ref);
7727 1d0f4054 2021-06-17 stsp if (repo) {
7728 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7729 1d0f4054 2021-06-17 stsp if (error == NULL)
7730 1d0f4054 2021-06-17 stsp error = close_err;
7732 0ae84acc 2022-06-15 tracey if (pack_fds) {
7733 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7734 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7735 0ae84acc 2022-06-15 tracey if (error == NULL)
7736 0ae84acc 2022-06-15 tracey error = pack_err;
7738 51a10b52 2020-12-26 stsp tog_free_refs();
7739 ffd1d5e5 2018-06-23 stsp return error;
7742 6458efa5 2020-11-24 stsp static const struct got_error *
7743 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7745 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7746 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7748 6458efa5 2020-11-24 stsp s->nrefs = 0;
7749 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7750 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7751 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7752 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7753 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7756 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7757 6458efa5 2020-11-24 stsp if (re == NULL)
7758 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7760 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7761 8924d611 2020-12-26 stsp if (re->ref == NULL)
7762 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7763 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7764 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7767 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7768 6458efa5 2020-11-24 stsp return NULL;
7772 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7774 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7776 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7777 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7778 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7779 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7784 6458efa5 2020-11-24 stsp static const struct got_error *
7785 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7787 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7788 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7790 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7791 6458efa5 2020-11-24 stsp s->repo = repo;
7793 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7794 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7796 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7798 6458efa5 2020-11-24 stsp return err;
7800 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7801 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7802 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7803 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7805 6458efa5 2020-11-24 stsp goto done;
7807 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7808 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7809 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7811 6458efa5 2020-11-24 stsp goto done;
7813 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7814 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7815 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7817 cc488aa7 2022-01-23 stsp goto done;
7819 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7820 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7821 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7823 6458efa5 2020-11-24 stsp goto done;
7826 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7827 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7828 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7829 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7830 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7833 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7834 6458efa5 2020-11-24 stsp return err;
7837 6458efa5 2020-11-24 stsp static const struct got_error *
7838 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7840 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7842 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7843 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7845 6458efa5 2020-11-24 stsp return NULL;
7848 6458efa5 2020-11-24 stsp static const struct got_error *
7849 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7850 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7852 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7853 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7854 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7855 6458efa5 2020-11-24 stsp int obj_type;
7857 c42c9805 2020-11-24 stsp *commit_id = NULL;
7859 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7861 6458efa5 2020-11-24 stsp return err;
7863 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7865 6458efa5 2020-11-24 stsp goto done;
7867 6458efa5 2020-11-24 stsp switch (obj_type) {
7868 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7869 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7871 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7872 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7874 6458efa5 2020-11-24 stsp goto done;
7875 c42c9805 2020-11-24 stsp free(obj_id);
7876 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7877 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7879 6458efa5 2020-11-24 stsp goto done;
7880 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7881 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7882 c42c9805 2020-11-24 stsp goto done;
7884 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7885 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7886 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7887 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7888 c42c9805 2020-11-24 stsp goto done;
7892 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7898 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7899 c42c9805 2020-11-24 stsp if (err) {
7900 c42c9805 2020-11-24 stsp free(*commit_id);
7901 c42c9805 2020-11-24 stsp *commit_id = NULL;
7903 c42c9805 2020-11-24 stsp return err;
7906 c42c9805 2020-11-24 stsp static const struct got_error *
7907 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7908 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7910 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7911 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7912 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7914 c42c9805 2020-11-24 stsp *new_view = NULL;
7916 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7917 c42c9805 2020-11-24 stsp if (err) {
7918 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7919 c42c9805 2020-11-24 stsp return err;
7921 c42c9805 2020-11-24 stsp return NULL;
7924 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7925 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7926 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7927 6458efa5 2020-11-24 stsp goto done;
7930 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7931 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7934 6458efa5 2020-11-24 stsp view_close(log_view);
7936 6458efa5 2020-11-24 stsp *new_view = log_view;
7937 c42c9805 2020-11-24 stsp free(commit_id);
7938 6458efa5 2020-11-24 stsp return err;
7941 ce5b7c56 2019-07-09 stsp static void
7942 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7944 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7945 34ba6917 2020-11-30 stsp int i = 0;
7947 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7950 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7951 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7952 34ba6917 2020-11-30 stsp if (re == NULL)
7954 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7955 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7959 9b058f45 2022-06-30 mark static const struct got_error *
7960 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7962 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7963 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7964 6458efa5 2020-11-24 stsp int n = 0;
7966 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7967 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7969 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7971 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7972 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7973 94b80cfa 2022-08-01 mark if (last) {
7974 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7975 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7977 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7978 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7979 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7983 9b058f45 2022-06-30 mark return NULL;
7986 6458efa5 2020-11-24 stsp static const struct got_error *
7987 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7989 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7991 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7992 6458efa5 2020-11-24 stsp return NULL;
7995 6458efa5 2020-11-24 stsp static int
7996 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7998 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8000 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8004 6458efa5 2020-11-24 stsp static const struct got_error *
8005 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8007 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8008 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8010 6458efa5 2020-11-24 stsp if (!view->searching) {
8011 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8012 6458efa5 2020-11-24 stsp return NULL;
8015 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8016 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8017 6458efa5 2020-11-24 stsp if (s->selected_entry)
8018 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8020 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8021 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8023 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8024 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8026 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8027 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8030 487cd7d2 2021-12-17 stsp if (s->selected_entry)
8031 487cd7d2 2021-12-17 stsp re = s->selected_entry;
8032 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
8033 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8035 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8038 6458efa5 2020-11-24 stsp while (1) {
8039 6458efa5 2020-11-24 stsp if (re == NULL) {
8040 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8041 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8042 6458efa5 2020-11-24 stsp return NULL;
8044 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8045 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8047 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8050 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8051 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8052 6458efa5 2020-11-24 stsp s->matched_entry = re;
8056 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8057 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8059 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8062 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8063 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8064 6458efa5 2020-11-24 stsp s->selected = 0;
8067 6458efa5 2020-11-24 stsp return NULL;
8070 6458efa5 2020-11-24 stsp static const struct got_error *
8071 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8073 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8074 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8075 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8076 6458efa5 2020-11-24 stsp char *line = NULL;
8077 6458efa5 2020-11-24 stsp wchar_t *wline;
8078 6458efa5 2020-11-24 stsp struct tog_color *tc;
8079 0ed6bc90 2023-02-01 mark int width, n, scrollx;
8080 6458efa5 2020-11-24 stsp int limit = view->nlines;
8082 6458efa5 2020-11-24 stsp werase(view->window);
8084 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8085 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8086 9b058f45 2022-06-30 mark --limit; /* border */
8088 6458efa5 2020-11-24 stsp if (limit == 0)
8089 6458efa5 2020-11-24 stsp return NULL;
8091 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8093 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8094 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8095 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8097 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8098 6458efa5 2020-11-24 stsp if (err) {
8099 6458efa5 2020-11-24 stsp free(line);
8100 6458efa5 2020-11-24 stsp return err;
8102 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8103 6458efa5 2020-11-24 stsp wstandout(view->window);
8104 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8105 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8106 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8107 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8108 6458efa5 2020-11-24 stsp wstandend(view->window);
8109 6458efa5 2020-11-24 stsp free(wline);
8110 6458efa5 2020-11-24 stsp wline = NULL;
8111 6458efa5 2020-11-24 stsp free(line);
8112 6458efa5 2020-11-24 stsp line = NULL;
8113 6458efa5 2020-11-24 stsp if (--limit <= 0)
8114 6458efa5 2020-11-24 stsp return NULL;
8117 0ed6bc90 2023-02-01 mark view->maxx = 0;
8118 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8119 6458efa5 2020-11-24 stsp char *line = NULL;
8120 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8122 b4996bee 2022-06-16 stsp if (s->show_date) {
8123 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8124 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8125 b4996bee 2022-06-16 stsp struct got_object_id *id;
8126 b4996bee 2022-06-16 stsp struct tm tm;
8129 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8131 b4996bee 2022-06-16 stsp return err;
8132 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8133 b4996bee 2022-06-16 stsp if (err) {
8134 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8136 b4996bee 2022-06-16 stsp return err;
8138 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8140 b4996bee 2022-06-16 stsp if (err) {
8142 b4996bee 2022-06-16 stsp return err;
8144 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8145 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8147 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8148 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8151 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8152 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8153 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8154 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8156 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8157 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8158 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8159 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8160 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8161 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8162 6458efa5 2020-11-24 stsp struct got_object_id *id;
8163 6458efa5 2020-11-24 stsp char *id_str;
8164 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8166 6458efa5 2020-11-24 stsp return err;
8167 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8168 6458efa5 2020-11-24 stsp if (err) {
8170 6458efa5 2020-11-24 stsp return err;
8172 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8173 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8174 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8176 6458efa5 2020-11-24 stsp free(id_str);
8177 6458efa5 2020-11-24 stsp return err;
8180 6458efa5 2020-11-24 stsp free(id_str);
8181 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8182 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8183 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8185 0ed6bc90 2023-02-01 mark /* use full line width to determine view->maxx */
8186 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8187 6458efa5 2020-11-24 stsp if (err) {
8188 6458efa5 2020-11-24 stsp free(line);
8189 6458efa5 2020-11-24 stsp return err;
8191 0ed6bc90 2023-02-01 mark view->maxx = MAX(view->maxx, width);
8192 0ed6bc90 2023-02-01 mark free(wline);
8193 0ed6bc90 2023-02-01 mark wline = NULL;
8195 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
8196 0ed6bc90 2023-02-01 mark view->ncols, 0, 0);
8197 0ed6bc90 2023-02-01 mark if (err) {
8198 0ed6bc90 2023-02-01 mark free(line);
8199 0ed6bc90 2023-02-01 mark return err;
8201 6458efa5 2020-11-24 stsp if (n == s->selected) {
8202 6458efa5 2020-11-24 stsp if (view->focussed)
8203 6458efa5 2020-11-24 stsp wstandout(view->window);
8204 6458efa5 2020-11-24 stsp s->selected_entry = re;
8206 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8208 6458efa5 2020-11-24 stsp wattr_on(view->window,
8209 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8210 0ed6bc90 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
8212 6458efa5 2020-11-24 stsp wattr_off(view->window,
8213 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8214 b637f33f 2023-02-01 mark if (width < view->ncols)
8215 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8216 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8217 6458efa5 2020-11-24 stsp wstandend(view->window);
8218 6458efa5 2020-11-24 stsp free(line);
8219 6458efa5 2020-11-24 stsp free(wline);
8220 6458efa5 2020-11-24 stsp wline = NULL;
8222 6458efa5 2020-11-24 stsp s->ndisplayed++;
8223 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8226 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8229 9b058f45 2022-06-30 mark view_border(view);
8230 6458efa5 2020-11-24 stsp return err;
8233 6458efa5 2020-11-24 stsp static const struct got_error *
8234 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8235 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8237 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8238 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8239 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8241 c42c9805 2020-11-24 stsp *new_view = NULL;
8243 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8244 c42c9805 2020-11-24 stsp if (err) {
8245 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8246 c42c9805 2020-11-24 stsp return err;
8248 c42c9805 2020-11-24 stsp return NULL;
8252 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8253 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8254 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8255 c42c9805 2020-11-24 stsp goto done;
8258 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8259 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8261 c42c9805 2020-11-24 stsp goto done;
8263 c42c9805 2020-11-24 stsp *new_view = tree_view;
8265 c42c9805 2020-11-24 stsp free(commit_id);
8266 c42c9805 2020-11-24 stsp return err;
8269 94b80cfa 2022-08-01 mark static const struct got_error *
8270 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8272 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8273 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8274 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8276 94b80cfa 2022-08-01 mark g = view->gline;
8277 94b80cfa 2022-08-01 mark view->gline = 0;
8279 94b80cfa 2022-08-01 mark if (g == 0)
8281 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8282 94b80cfa 2022-08-01 mark g = s->nrefs;
8284 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8285 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8286 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8287 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8288 94b80cfa 2022-08-01 mark return NULL;
8291 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8292 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8294 94b80cfa 2022-08-01 mark return err;
8295 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8296 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8297 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8298 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8299 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8300 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8302 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8303 94b80cfa 2022-08-01 mark s->selected = g - 1;
8305 94b80cfa 2022-08-01 mark return NULL;
8309 c42c9805 2020-11-24 stsp static const struct got_error *
8310 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8312 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8313 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8314 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8315 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8317 94b80cfa 2022-08-01 mark if (view->gline)
8318 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8320 6458efa5 2020-11-24 stsp switch (ch) {
8323 0ed6bc90 2023-02-01 mark case KEY_RIGHT:
8325 0ed6bc90 2023-02-01 mark case KEY_LEFT:
8327 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8330 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8331 640cd7ff 2022-06-22 mark view->count = 0;
8334 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8335 640cd7ff 2022-06-22 mark view->count = 0;
8338 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8339 3f6c6614 2023-01-23 mark view->action = s->sort_by_date ? "sort by date" : "sort by name";
8340 640cd7ff 2022-06-22 mark view->count = 0;
8341 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8342 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8343 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8346 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8347 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8348 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8351 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8352 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8354 6458efa5 2020-11-24 stsp case KEY_ENTER:
8355 6458efa5 2020-11-24 stsp case '\r':
8356 640cd7ff 2022-06-22 mark view->count = 0;
8357 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8359 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8362 640cd7ff 2022-06-22 mark view->count = 0;
8363 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8365 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8367 e4526bf5 2021-09-03 naddy case 'g':
8369 e4526bf5 2021-09-03 naddy case KEY_HOME:
8370 e4526bf5 2021-09-03 naddy s->selected = 0;
8371 640cd7ff 2022-06-22 mark view->count = 0;
8372 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8374 e4526bf5 2021-09-03 naddy case 'G':
8376 9b058f45 2022-06-30 mark case KEY_END: {
8377 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8379 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8380 9b058f45 2022-06-30 mark --eos; /* border */
8381 e4526bf5 2021-09-03 naddy s->selected = 0;
8382 640cd7ff 2022-06-22 mark view->count = 0;
8383 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8384 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8385 e4526bf5 2021-09-03 naddy if (re == NULL)
8387 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8388 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8390 e4526bf5 2021-09-03 naddy if (n > 0)
8391 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8395 6458efa5 2020-11-24 stsp case KEY_UP:
8396 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8397 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8398 6458efa5 2020-11-24 stsp s->selected--;
8401 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8402 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8403 640cd7ff 2022-06-22 mark view->count = 0;
8405 83cc4199 2022-06-13 stsp case CTRL('u'):
8407 83cc4199 2022-06-13 stsp nscroll /= 2;
8408 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8409 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8410 6458efa5 2020-11-24 stsp case CTRL('b'):
8412 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8413 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8414 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8415 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8416 640cd7ff 2022-06-22 mark view->count = 0;
8419 6458efa5 2020-11-24 stsp case KEY_DOWN:
8420 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8421 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8422 6458efa5 2020-11-24 stsp s->selected++;
8425 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8426 6458efa5 2020-11-24 stsp /* can't scroll any further */
8427 640cd7ff 2022-06-22 mark view->count = 0;
8430 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8432 83cc4199 2022-06-13 stsp case CTRL('d'):
8434 83cc4199 2022-06-13 stsp nscroll /= 2;
8435 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8436 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8437 6458efa5 2020-11-24 stsp case CTRL('f'):
8439 48bb96f0 2022-06-20 naddy case ' ':
8440 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8441 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8442 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8443 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8444 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8445 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8446 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8447 640cd7ff 2022-06-22 mark view->count = 0;
8450 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8452 6458efa5 2020-11-24 stsp case CTRL('l'):
8453 640cd7ff 2022-06-22 mark view->count = 0;
8454 8924d611 2020-12-26 stsp tog_free_refs();
8455 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8458 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8459 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8461 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8462 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8463 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8466 640cd7ff 2022-06-22 mark view->count = 0;
8470 6458efa5 2020-11-24 stsp return err;
8473 6458efa5 2020-11-24 stsp __dead static void
8474 6458efa5 2020-11-24 stsp usage_ref(void)
8477 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8478 6458efa5 2020-11-24 stsp getprogname());
8482 6458efa5 2020-11-24 stsp static const struct got_error *
8483 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8485 6458efa5 2020-11-24 stsp const struct got_error *error;
8486 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8487 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8488 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8490 6458efa5 2020-11-24 stsp struct tog_view *view;
8491 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8493 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8494 6458efa5 2020-11-24 stsp switch (ch) {
8496 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8497 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8498 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8502 e99e2d15 2020-11-24 naddy usage_ref();
8503 6458efa5 2020-11-24 stsp /* NOTREACHED */
8507 6458efa5 2020-11-24 stsp argc -= optind;
8508 6458efa5 2020-11-24 stsp argv += optind;
8510 6458efa5 2020-11-24 stsp if (argc > 1)
8511 e99e2d15 2020-11-24 naddy usage_ref();
8513 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8514 0ae84acc 2022-06-15 tracey if (error != NULL)
8515 0ae84acc 2022-06-15 tracey goto done;
8517 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8518 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8519 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8520 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8521 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8522 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8523 c156c7a4 2020-12-18 stsp goto done;
8524 6458efa5 2020-11-24 stsp if (worktree)
8525 6458efa5 2020-11-24 stsp repo_path =
8526 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8528 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8529 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8530 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8531 c156c7a4 2020-12-18 stsp goto done;
8535 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8536 6458efa5 2020-11-24 stsp if (error != NULL)
8537 6458efa5 2020-11-24 stsp goto done;
8539 6458efa5 2020-11-24 stsp init_curses();
8541 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8542 51a10b52 2020-12-26 stsp if (error)
8543 51a10b52 2020-12-26 stsp goto done;
8545 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8546 6458efa5 2020-11-24 stsp if (error)
8547 6458efa5 2020-11-24 stsp goto done;
8549 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8550 6458efa5 2020-11-24 stsp if (view == NULL) {
8551 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8552 6458efa5 2020-11-24 stsp goto done;
8555 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8556 6458efa5 2020-11-24 stsp if (error)
8557 6458efa5 2020-11-24 stsp goto done;
8559 6458efa5 2020-11-24 stsp if (worktree) {
8560 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8561 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8562 6458efa5 2020-11-24 stsp worktree = NULL;
8564 6458efa5 2020-11-24 stsp error = view_loop(view);
8566 6458efa5 2020-11-24 stsp free(repo_path);
8567 6458efa5 2020-11-24 stsp free(cwd);
8568 1d0f4054 2021-06-17 stsp if (repo) {
8569 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8570 1d0f4054 2021-06-17 stsp if (close_err)
8571 1d0f4054 2021-06-17 stsp error = close_err;
8573 0ae84acc 2022-06-15 tracey if (pack_fds) {
8574 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8575 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8576 0ae84acc 2022-06-15 tracey if (error == NULL)
8577 0ae84acc 2022-06-15 tracey error = pack_err;
8579 51a10b52 2020-12-26 stsp tog_free_refs();
8580 6458efa5 2020-11-24 stsp return error;
8583 ec2a9698 2022-09-15 mark static const struct got_error*
8584 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8585 ec2a9698 2022-09-15 mark const char *str)
8587 ec2a9698 2022-09-15 mark size_t len;
8589 ec2a9698 2022-09-15 mark if (win == NULL)
8590 ec2a9698 2022-09-15 mark win = stdscr;
8592 ec2a9698 2022-09-15 mark len = strlen(str);
8593 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8595 ec2a9698 2022-09-15 mark if (focus)
8596 ec2a9698 2022-09-15 mark wstandout(win);
8597 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8598 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8599 ec2a9698 2022-09-15 mark if (focus)
8600 ec2a9698 2022-09-15 mark wstandend(win);
8602 ec2a9698 2022-09-15 mark return NULL;
8605 136e2bd4 2022-07-23 mark static const struct got_error *
8606 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8610 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8611 ec2a9698 2022-09-15 mark if (p == NULL) {
8612 ec2a9698 2022-09-15 mark free(*line_offsets);
8613 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8614 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8617 ec2a9698 2022-09-15 mark *line_offsets = p;
8618 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8619 ec2a9698 2022-09-15 mark ++(*nlines);
8620 ec2a9698 2022-09-15 mark return NULL;
8623 ec2a9698 2022-09-15 mark static const struct got_error *
8624 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8628 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8629 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8630 ec2a9698 2022-09-15 mark size_t len = 1;
8632 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8635 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8636 ec2a9698 2022-09-15 mark if (t0 == NULL)
8637 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8639 ec2a9698 2022-09-15 mark len += strlen(t);
8640 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8641 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8643 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8646 ec2a9698 2022-09-15 mark return NULL;
8650 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8651 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8652 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8654 ec2a9698 2022-09-15 mark static const struct got_error *
8655 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8657 ec2a9698 2022-09-15 mark int n, len = width;
8659 ec2a9698 2022-09-15 mark if (km->keys) {
8660 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8661 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8662 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8664 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8665 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8667 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8669 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8670 ec2a9698 2022-09-15 mark if (t0 == NULL)
8671 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8673 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8674 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8675 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8676 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8677 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8678 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8679 ec2a9698 2022-09-15 mark if (n < 0) {
8681 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8683 ec2a9698 2022-09-15 mark first = 0;
8684 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8685 ec2a9698 2022-09-15 mark *off += n;
8689 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8690 ec2a9698 2022-09-15 mark if (n < 0)
8691 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8692 ec2a9698 2022-09-15 mark *off += n;
8694 ec2a9698 2022-09-15 mark return NULL;
8697 ec2a9698 2022-09-15 mark static const struct got_error *
8698 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8700 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8701 ec2a9698 2022-09-15 mark off_t off = 0;
8702 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8703 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8704 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8705 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8706 ec2a9698 2022-09-15 mark GENERATE_HELP
8707 ec2a9698 2022-09-15 mark #undef KEYMAP_
8708 ec2a9698 2022-09-15 mark #undef KEY_
8711 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8713 ec2a9698 2022-09-15 mark return err;
8715 ec2a9698 2022-09-15 mark n = nitems(km);
8716 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8718 ec2a9698 2022-09-15 mark return err;
8720 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8721 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8722 ec2a9698 2022-09-15 mark show = s->all;
8723 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8724 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8727 ec2a9698 2022-09-15 mark if (show) {
8728 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8730 ec2a9698 2022-09-15 mark return err;
8731 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8733 ec2a9698 2022-09-15 mark return err;
8736 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8738 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8739 ec2a9698 2022-09-15 mark return err;
8742 ec2a9698 2022-09-15 mark static const struct got_error *
8743 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8746 ec2a9698 2022-09-15 mark const struct got_error *err;
8748 ec2a9698 2022-09-15 mark free(s->line_offsets);
8749 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8750 ec2a9698 2022-09-15 mark s->nlines = 0;
8752 ec2a9698 2022-09-15 mark f = got_opentemp();
8753 ec2a9698 2022-09-15 mark if (f == NULL)
8754 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8757 ec2a9698 2022-09-15 mark err = format_help(s);
8759 ec2a9698 2022-09-15 mark return err;
8761 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8762 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8764 ec2a9698 2022-09-15 mark return NULL;
8767 ec2a9698 2022-09-15 mark static const struct got_error *
8768 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8770 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8771 ec2a9698 2022-09-15 mark return NULL;
8774 eaf2c13e 2022-09-17 mark static void
8775 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8776 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8778 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8780 eaf2c13e 2022-09-17 mark *f = s->f;
8781 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8782 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8783 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8784 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8785 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8786 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8789 ec2a9698 2022-09-15 mark static const struct got_error *
8790 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8792 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8793 ec2a9698 2022-09-15 mark const struct got_error *err;
8794 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8795 ec2a9698 2022-09-15 mark wchar_t *wline;
8796 ec2a9698 2022-09-15 mark char *line;
8797 ec2a9698 2022-09-15 mark ssize_t linelen;
8798 ec2a9698 2022-09-15 mark size_t linesz = 0;
8799 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8800 ec2a9698 2022-09-15 mark int eos = view->nlines;
8802 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8803 ec2a9698 2022-09-15 mark --eos; /* account for border */
8805 ec2a9698 2022-09-15 mark s->lineno = 0;
8806 ec2a9698 2022-09-15 mark rewind(s->f);
8807 ec2a9698 2022-09-15 mark werase(view->window);
8809 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8810 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8812 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8813 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8814 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8816 ec2a9698 2022-09-15 mark return err;
8817 ec2a9698 2022-09-15 mark if (eos <= 1)
8818 ec2a9698 2022-09-15 mark return NULL;
8819 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8822 ec2a9698 2022-09-15 mark s->eof = 0;
8823 ec2a9698 2022-09-15 mark view->maxx = 0;
8824 ec2a9698 2022-09-15 mark line = NULL;
8825 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8826 ec2a9698 2022-09-15 mark attr_t attr = 0;
8828 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8829 ec2a9698 2022-09-15 mark if (linelen == -1) {
8830 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8831 ec2a9698 2022-09-15 mark free(line);
8832 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8834 ec2a9698 2022-09-15 mark s->eof = 1;
8837 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8839 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8841 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8842 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8844 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8845 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8846 ec2a9698 2022-09-15 mark if (err) {
8847 ec2a9698 2022-09-15 mark free(line);
8848 ec2a9698 2022-09-15 mark return err;
8850 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8851 ec2a9698 2022-09-15 mark free(wline);
8852 ec2a9698 2022-09-15 mark wline = NULL;
8855 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8856 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8857 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8858 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8859 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8860 ec2a9698 2022-09-15 mark if (err) {
8861 ec2a9698 2022-09-15 mark free(line);
8862 ec2a9698 2022-09-15 mark return err;
8867 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8868 b637f33f 2023-02-01 mark view->x, view->ncols, 0, view->x ? 1 : 0);
8869 ec2a9698 2022-09-15 mark if (err) {
8870 ec2a9698 2022-09-15 mark free(line);
8871 ec2a9698 2022-09-15 mark return err;
8873 b637f33f 2023-02-01 mark waddwstr(view->window, &wline[skip]);
8874 ec2a9698 2022-09-15 mark free(wline);
8875 ec2a9698 2022-09-15 mark wline = NULL;
8877 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8878 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8879 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8881 b637f33f 2023-02-01 mark if (width < view->ncols)
8882 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8885 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8886 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8887 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8889 ec2a9698 2022-09-15 mark free(line);
8890 ec2a9698 2022-09-15 mark if (nprinted > 0)
8891 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8893 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8895 ec2a9698 2022-09-15 mark view_border(view);
8897 ec2a9698 2022-09-15 mark if (s->eof) {
8898 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8899 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8900 ec2a9698 2022-09-15 mark view->ncols - 1);
8901 ec2a9698 2022-09-15 mark if (rc == ERR)
8902 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8904 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8905 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8906 ec2a9698 2022-09-15 mark wstandout(view->window);
8907 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8908 ec2a9698 2022-09-15 mark view->ncols - 1);
8909 ec2a9698 2022-09-15 mark if (rc == ERR)
8910 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8911 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8912 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8913 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8914 ec2a9698 2022-09-15 mark if (rc == ERR)
8915 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8917 ec2a9698 2022-09-15 mark wstandend(view->window);
8920 ec2a9698 2022-09-15 mark return NULL;
8923 ec2a9698 2022-09-15 mark static const struct got_error *
8924 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8926 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8927 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8928 ec2a9698 2022-09-15 mark char *line = NULL;
8929 ec2a9698 2022-09-15 mark ssize_t linelen;
8930 ec2a9698 2022-09-15 mark size_t linesz = 0;
8931 ec2a9698 2022-09-15 mark int eos, nscroll;
8933 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8934 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8935 ec2a9698 2022-09-15 mark --eos; /* border */
8937 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8939 ec2a9698 2022-09-15 mark switch (ch) {
8942 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8944 ec2a9698 2022-09-15 mark case KEY_LEFT:
8946 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8949 ec2a9698 2022-09-15 mark case KEY_HOME:
8950 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8951 ec2a9698 2022-09-15 mark view->count = 0;
8954 ec2a9698 2022-09-15 mark case KEY_END:
8955 ec2a9698 2022-09-15 mark view->count = 0;
8956 ec2a9698 2022-09-15 mark if (s->eof)
8958 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8959 ec2a9698 2022-09-15 mark s->eof = 1;
8962 ec2a9698 2022-09-15 mark case KEY_UP:
8963 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8964 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8966 ec2a9698 2022-09-15 mark view->count = 0;
8968 ec2a9698 2022-09-15 mark case CTRL('u'):
8970 ec2a9698 2022-09-15 mark nscroll /= 2;
8971 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8972 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8973 ec2a9698 2022-09-15 mark case CTRL('b'):
8975 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8976 ec2a9698 2022-09-15 mark view->count = 0;
8979 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8980 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8983 ec2a9698 2022-09-15 mark case KEY_DOWN:
8984 ec2a9698 2022-09-15 mark case CTRL('n'):
8985 ec2a9698 2022-09-15 mark if (!s->eof)
8986 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8988 ec2a9698 2022-09-15 mark view->count = 0;
8990 ec2a9698 2022-09-15 mark case CTRL('d'):
8992 ec2a9698 2022-09-15 mark nscroll /= 2;
8993 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8994 ec2a9698 2022-09-15 mark case KEY_NPAGE:
8995 ec2a9698 2022-09-15 mark case CTRL('f'):
8998 ec2a9698 2022-09-15 mark if (s->eof) {
8999 ec2a9698 2022-09-15 mark view->count = 0;
9002 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
9003 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9004 ec2a9698 2022-09-15 mark s->first_displayed_line++;
9005 ec2a9698 2022-09-15 mark if (linelen == -1) {
9006 ec2a9698 2022-09-15 mark if (feof(s->f))
9007 ec2a9698 2022-09-15 mark s->eof = 1;
9009 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
9013 ec2a9698 2022-09-15 mark free(line);
9016 ec2a9698 2022-09-15 mark view->count = 0;
9020 ec2a9698 2022-09-15 mark return err;
9023 ec2a9698 2022-09-15 mark static const struct got_error *
9024 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
9026 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9028 ec2a9698 2022-09-15 mark free(s->line_offsets);
9029 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9030 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
9031 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9033 ec2a9698 2022-09-15 mark return NULL;
9036 ec2a9698 2022-09-15 mark static const struct got_error *
9037 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
9039 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9042 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
9043 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9045 ec2a9698 2022-09-15 mark wclear(view->window);
9046 ec2a9698 2022-09-15 mark view->count = 0;
9047 ec2a9698 2022-09-15 mark view->x = 0;
9048 ec2a9698 2022-09-15 mark s->all = !s->all;
9049 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9050 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9051 ec2a9698 2022-09-15 mark s->matched_line = 0;
9053 ec2a9698 2022-09-15 mark return create_help(s);
9056 ec2a9698 2022-09-15 mark static const struct got_error *
9057 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
9059 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9060 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9062 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
9063 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9064 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9065 ec2a9698 2022-09-15 mark s->selected_line = 1;
9067 ec2a9698 2022-09-15 mark view->show = show_help_view;
9068 ec2a9698 2022-09-15 mark view->input = input_help_view;
9069 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
9070 ec2a9698 2022-09-15 mark view->close = close_help_view;
9071 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
9072 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
9073 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
9075 ec2a9698 2022-09-15 mark err = create_help(s);
9076 ec2a9698 2022-09-15 mark return err;
9079 ec2a9698 2022-09-15 mark static const struct got_error *
9080 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9081 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
9083 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
9085 136e2bd4 2022-07-23 mark *new_view = NULL;
9087 136e2bd4 2022-07-23 mark switch (request) {
9088 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
9089 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9090 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9092 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9093 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9094 136e2bd4 2022-07-23 mark view, s->repo);
9096 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9097 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9099 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9100 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9101 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9103 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9104 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9107 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9108 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9110 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9111 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9112 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9113 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9114 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9115 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9116 136e2bd4 2022-07-23 mark &view->state.tree);
9117 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9118 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9119 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9120 136e2bd4 2022-07-23 mark view->state.ref.repo);
9122 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9123 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9125 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9126 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9127 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9128 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9129 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9130 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9131 136e2bd4 2022-07-23 mark view->state.log.repo);
9132 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9133 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9134 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9135 136e2bd4 2022-07-23 mark view->state.ref.repo);
9137 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9138 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9140 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9141 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9142 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9143 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9144 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9145 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9146 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9147 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9149 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9150 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9152 136e2bd4 2022-07-23 mark view_close(*new_view);
9154 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9155 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9156 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9157 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9158 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9160 ec2a9698 2022-09-15 mark view_close(*new_view);
9163 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9166 136e2bd4 2022-07-23 mark return err;
9170 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9171 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9173 6458efa5 2020-11-24 stsp static void
9174 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9176 9b058f45 2022-06-30 mark switch (view->type) {
9177 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9178 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9179 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9180 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9184 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9185 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9187 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9188 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9191 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9192 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9193 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9195 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9196 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9197 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9199 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9200 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9201 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9207 9b058f45 2022-06-30 mark view->offset = 0;
9211 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9212 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9214 9b058f45 2022-06-30 mark static const struct got_error *
9215 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9217 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9218 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9219 9b058f45 2022-06-30 mark int *selected = NULL;
9220 9b058f45 2022-06-30 mark int header, offset;
9222 9b058f45 2022-06-30 mark switch (view->type) {
9223 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9224 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9225 9b058f45 2022-06-30 mark header = 3;
9226 9b058f45 2022-06-30 mark scrolld = NULL;
9227 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9228 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9229 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9230 9b058f45 2022-06-30 mark s->selected_line -= offset;
9231 9b058f45 2022-06-30 mark view->offset = offset;
9235 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9236 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9237 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9238 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9239 9b058f45 2022-06-30 mark selected = &s->selected;
9242 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9243 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9244 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9245 9b058f45 2022-06-30 mark header = 3;
9246 9b058f45 2022-06-30 mark selected = &s->selected;
9249 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9250 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9251 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9252 9b058f45 2022-06-30 mark header = 5;
9253 9b058f45 2022-06-30 mark selected = &s->selected;
9257 9b058f45 2022-06-30 mark selected = NULL;
9258 9b058f45 2022-06-30 mark scrolld = NULL;
9259 9b058f45 2022-06-30 mark header = 0;
9263 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9264 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9265 9b058f45 2022-06-30 mark view->offset = offset;
9266 9b058f45 2022-06-30 mark if (scrolld && offset) {
9267 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9268 9b058f45 2022-06-30 mark *selected -= offset;
9272 9b058f45 2022-06-30 mark return err;
9275 9b058f45 2022-06-30 mark static void
9276 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9280 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9281 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9282 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9283 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9285 6879ba42 2020-10-01 naddy fputc('\n', fp);
9288 4ed7e80c 2018-05-20 stsp __dead static void
9289 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9291 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9293 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9294 6879ba42 2020-10-01 naddy getprogname());
9295 6879ba42 2020-10-01 naddy if (hflag) {
9296 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9297 6879ba42 2020-10-01 naddy list_commands(fp);
9299 6879ba42 2020-10-01 naddy exit(status);
9302 c2301be8 2018-04-30 stsp static char **
9303 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9305 ee85c5e8 2020-02-29 stsp va_list ap;
9306 c2301be8 2018-04-30 stsp char **argv;
9309 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9311 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9312 c2301be8 2018-04-30 stsp if (argv == NULL)
9313 c2301be8 2018-04-30 stsp err(1, "calloc");
9314 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9315 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9316 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9317 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9320 ee85c5e8 2020-02-29 stsp va_end(ap);
9321 c2301be8 2018-04-30 stsp return argv;
9325 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9326 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9327 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9328 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9329 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9331 ee85c5e8 2020-02-29 stsp static const struct got_error *
9332 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9334 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9335 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9336 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9337 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9338 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9339 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9340 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9341 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9342 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9344 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9345 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9346 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9348 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9349 0ae84acc 2022-06-15 tracey if (error != NULL)
9350 0ae84acc 2022-06-15 tracey goto done;
9352 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9353 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9354 ee85c5e8 2020-02-29 stsp goto done;
9356 ee85c5e8 2020-02-29 stsp if (worktree)
9357 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9359 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9360 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9361 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9362 ee85c5e8 2020-02-29 stsp goto done;
9365 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9366 ee85c5e8 2020-02-29 stsp if (error != NULL)
9367 ee85c5e8 2020-02-29 stsp goto done;
9369 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9370 ee85c5e8 2020-02-29 stsp repo, worktree);
9371 ee85c5e8 2020-02-29 stsp if (error)
9372 ee85c5e8 2020-02-29 stsp goto done;
9374 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9375 84de9106 2020-12-26 stsp if (error)
9376 84de9106 2020-12-26 stsp goto done;
9377 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9378 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9379 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9380 ee85c5e8 2020-02-29 stsp if (error)
9381 ee85c5e8 2020-02-29 stsp goto done;
9383 ee85c5e8 2020-02-29 stsp if (worktree) {
9384 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9385 ee85c5e8 2020-02-29 stsp worktree = NULL;
9388 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9389 a44927cc 2022-04-07 stsp if (error)
9390 a44927cc 2022-04-07 stsp goto done;
9392 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9393 ee85c5e8 2020-02-29 stsp if (error) {
9394 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9395 ee85c5e8 2020-02-29 stsp goto done;
9396 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9397 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9398 6879ba42 2020-10-01 naddy usage(1, 1);
9399 ee85c5e8 2020-02-29 stsp /* not reached */
9402 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9403 ee85c5e8 2020-02-29 stsp if (error)
9404 ee85c5e8 2020-02-29 stsp goto done;
9406 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9408 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9409 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9411 1d0f4054 2021-06-17 stsp if (repo) {
9412 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9413 1d0f4054 2021-06-17 stsp if (error == NULL)
9414 1d0f4054 2021-06-17 stsp error = close_err;
9416 a44927cc 2022-04-07 stsp if (commit)
9417 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9418 ee85c5e8 2020-02-29 stsp if (worktree)
9419 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9420 0ae84acc 2022-06-15 tracey if (pack_fds) {
9421 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9422 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9423 0ae84acc 2022-06-15 tracey if (error == NULL)
9424 0ae84acc 2022-06-15 tracey error = pack_err;
9427 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9428 ee85c5e8 2020-02-29 stsp free(commit_id);
9429 ee85c5e8 2020-02-29 stsp free(cwd);
9430 ee85c5e8 2020-02-29 stsp free(repo_path);
9431 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9432 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9434 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9435 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9436 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9438 87670572 2020-12-26 naddy tog_free_refs();
9439 ee85c5e8 2020-02-29 stsp return error;
9443 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9445 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9446 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9447 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9448 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9449 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9450 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9451 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9453 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9455 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9456 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9458 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9460 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9461 9f7d7167 2018-04-29 stsp switch (ch) {
9463 9f7d7167 2018-04-29 stsp hflag = 1;
9466 53ccebc2 2019-07-30 stsp Vflag = 1;
9469 6879ba42 2020-10-01 naddy usage(hflag, 1);
9470 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9474 9f7d7167 2018-04-29 stsp argc -= optind;
9475 9f7d7167 2018-04-29 stsp argv += optind;
9476 9814e6a3 2020-09-27 naddy optind = 1;
9477 c2301be8 2018-04-30 stsp optreset = 1;
9479 53ccebc2 2019-07-30 stsp if (Vflag) {
9480 53ccebc2 2019-07-30 stsp got_version_print_str();
9481 6879ba42 2020-10-01 naddy return 0;
9484 4010e238 2020-12-04 stsp #ifndef PROFILE
9485 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9486 4010e238 2020-12-04 stsp NULL) == -1)
9487 4010e238 2020-12-04 stsp err(1, "pledge");
9490 c2301be8 2018-04-30 stsp if (argc == 0) {
9491 f29d3e89 2018-06-23 stsp if (hflag)
9492 6879ba42 2020-10-01 naddy usage(hflag, 0);
9493 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9494 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9496 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9500 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9501 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9502 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9503 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9504 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9510 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9511 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9512 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9513 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9514 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9515 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9518 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9519 ee85c5e8 2020-02-29 stsp if (argc != 1)
9520 6879ba42 2020-10-01 naddy usage(0, 1);
9521 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9522 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9524 ee85c5e8 2020-02-29 stsp if (hflag)
9525 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9527 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9531 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9533 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9534 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9535 a2f4a359 2020-02-28 stsp free(cmd_argv);
9538 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9539 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9540 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9541 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9542 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9543 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);