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 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
108 9b058f45 2022-06-30 mark enum tog_view_mode {
109 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
110 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
111 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
114 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
118 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
119 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
120 ba4f502b 2018-08-04 stsp struct got_object_id *id;
121 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
124 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 ba4f502b 2018-08-04 stsp struct commit_queue {
126 ba4f502b 2018-08-04 stsp int ncommits;
127 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
130 f26dddb7 2019-11-08 stsp struct tog_color {
131 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
132 6d17833f 2019-11-08 stsp regex_t regex;
133 6d17833f 2019-11-08 stsp short colorpair;
135 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
137 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 cc488aa7 2022-01-23 stsp static const struct got_error *
142 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 cc488aa7 2022-01-23 stsp struct got_reference* re2)
145 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
146 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
147 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
149 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
150 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
154 cc488aa7 2022-01-23 stsp return NULL;
155 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
157 cc488aa7 2022-01-23 stsp return NULL;
160 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 cc488aa7 2022-01-23 stsp return NULL;
164 11b20872 2019-11-08 stsp static const struct got_error *
165 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
167 51a10b52 2020-12-26 stsp const struct got_error *err;
169 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
173 51a10b52 2020-12-26 stsp return err;
175 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
179 51a10b52 2020-12-26 stsp static void
180 51a10b52 2020-12-26 stsp tog_free_refs(void)
182 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
183 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
184 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
186 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
189 51a10b52 2020-12-26 stsp static const struct got_error *
190 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
191 11b20872 2019-11-08 stsp int idx, short color)
193 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
194 11b20872 2019-11-08 stsp struct tog_color *tc;
195 11b20872 2019-11-08 stsp int regerr = 0;
197 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
198 11b20872 2019-11-08 stsp return NULL;
200 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
202 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
203 11b20872 2019-11-08 stsp if (tc == NULL)
204 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
205 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
206 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 11b20872 2019-11-08 stsp if (regerr) {
208 11b20872 2019-11-08 stsp static char regerr_msg[512];
209 11b20872 2019-11-08 stsp static char err_msg[512];
210 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
211 11b20872 2019-11-08 stsp sizeof(regerr_msg));
212 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 11b20872 2019-11-08 stsp regerr_msg);
214 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
216 11b20872 2019-11-08 stsp return err;
218 11b20872 2019-11-08 stsp tc->colorpair = idx;
219 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
220 11b20872 2019-11-08 stsp return NULL;
223 11b20872 2019-11-08 stsp static void
224 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
226 11b20872 2019-11-08 stsp struct tog_color *tc;
228 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
229 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
230 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
231 11b20872 2019-11-08 stsp regfree(&tc->regex);
236 336075a4 2022-06-25 op static struct tog_color *
237 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
239 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
241 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
242 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
246 11b20872 2019-11-08 stsp return NULL;
250 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 11b20872 2019-11-08 stsp return COLOR_CYAN;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 11b20872 2019-11-08 stsp return COLOR_YELLOW;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 11b20872 2019-11-08 stsp return COLOR_GREEN;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 91b8c405 2020-01-25 stsp return COLOR_CYAN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 11b20872 2019-11-08 stsp return COLOR_GREEN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 11b20872 2019-11-08 stsp return COLOR_CYAN;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 11b20872 2019-11-08 stsp return COLOR_YELLOW;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_GREEN;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
278 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
280 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
287 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
289 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
291 11b20872 2019-11-08 stsp if (val == NULL)
292 11b20872 2019-11-08 stsp return default_color_value(envvar);
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
295 11b20872 2019-11-08 stsp return COLOR_BLACK;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
297 11b20872 2019-11-08 stsp return COLOR_RED;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
299 11b20872 2019-11-08 stsp return COLOR_GREEN;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
301 11b20872 2019-11-08 stsp return COLOR_YELLOW;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
303 11b20872 2019-11-08 stsp return COLOR_BLUE;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
305 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
307 11b20872 2019-11-08 stsp return COLOR_CYAN;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
309 11b20872 2019-11-08 stsp return COLOR_WHITE;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
313 11b20872 2019-11-08 stsp return default_color_value(envvar);
317 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
318 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
319 3dbaef42 2020-11-24 stsp const char *label1, *label2;
320 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
321 f9d37699 2022-06-28 stsp int fd1, fd2;
322 15a087fe 2019-02-21 stsp int first_displayed_line;
323 15a087fe 2019-02-21 stsp int last_displayed_line;
325 15a087fe 2019-02-21 stsp int diff_context;
326 3dbaef42 2020-11-24 stsp int ignore_whitespace;
327 64453f7e 2020-11-21 stsp int force_text_diff;
328 15a087fe 2019-02-21 stsp struct got_repository *repo;
329 bddb1296 2019-11-08 stsp struct tog_colors colors;
330 fe621944 2020-11-10 stsp size_t nlines;
331 f44b1f58 2020-02-02 tracey off_t *line_offsets;
332 f44b1f58 2020-02-02 tracey int matched_line;
333 f44b1f58 2020-02-02 tracey int selected_line;
335 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
336 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
339 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
342 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
343 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
344 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
345 1a76625f 2018-10-22 stsp int commits_needed;
346 fb280deb 2021-08-30 stsp int load_all;
347 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
348 1a76625f 2018-10-22 stsp struct commit_queue *commits;
349 1a76625f 2018-10-22 stsp const char *in_repo_path;
350 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
351 1a76625f 2018-10-22 stsp struct got_repository *repo;
352 74467cc8 2022-06-15 stsp int *pack_fds;
353 1a76625f 2018-10-22 stsp int log_complete;
354 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
355 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
356 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
357 13add988 2019-10-15 stsp int *searching;
358 13add988 2019-10-15 stsp int *search_next_done;
359 13add988 2019-10-15 stsp regex_t *regex;
362 1a76625f 2018-10-22 stsp struct tog_log_view_state {
363 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
365 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
366 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
367 b01e7d3b 2018-08-04 stsp int selected;
368 b01e7d3b 2018-08-04 stsp char *in_repo_path;
369 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
370 b672a97a 2020-01-27 stsp int log_branches;
371 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
372 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
373 1a76625f 2018-10-22 stsp sig_atomic_t quit;
374 1a76625f 2018-10-22 stsp pthread_t thread;
375 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
376 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
377 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
378 11b20872 2019-11-08 stsp struct tog_colors colors;
379 10aab77f 2022-07-19 op int use_committer;
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
384 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
385 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
388 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
389 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
390 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
391 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
392 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
394 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
395 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
396 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
398 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
399 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
400 e9424729 2018-08-04 stsp int nlines;
402 e9424729 2018-08-04 stsp struct tog_view *view;
403 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
407 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
408 e9424729 2018-08-04 stsp const char *path;
409 e9424729 2018-08-04 stsp struct got_repository *repo;
410 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
411 e9424729 2018-08-04 stsp int *complete;
412 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
413 fc06ba56 2019-08-22 stsp void *cancel_arg;
416 e9424729 2018-08-04 stsp struct tog_blame {
418 be659d10 2020-11-18 stsp off_t filesize;
419 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
420 6fcac457 2018-11-19 stsp int nlines;
421 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
422 e9424729 2018-08-04 stsp pthread_t thread;
423 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
424 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
425 e9424729 2018-08-04 stsp const char *path;
426 0ae84acc 2022-06-15 tracey int *pack_fds;
429 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
430 7cbe629d 2018-08-04 stsp int first_displayed_line;
431 7cbe629d 2018-08-04 stsp int last_displayed_line;
432 7cbe629d 2018-08-04 stsp int selected_line;
433 c0f61fa4 2022-07-11 mark int last_diffed_line;
434 7cbe629d 2018-08-04 stsp int blame_complete;
437 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
438 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
439 e5a0f69f 2018-08-18 stsp char *path;
440 7cbe629d 2018-08-04 stsp struct got_repository *repo;
441 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
442 e9424729 2018-08-04 stsp struct tog_blame blame;
443 6c4c42e0 2019-06-24 stsp int matched_line;
444 11b20872 2019-11-08 stsp struct tog_colors colors;
447 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
448 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
449 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
452 ad80ab7b 2018-08-04 stsp int selected;
455 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
457 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
458 ad80ab7b 2018-08-04 stsp char *tree_label;
459 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
460 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
461 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
462 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
465 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
466 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
467 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
468 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
469 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
470 6458efa5 2020-11-24 stsp struct tog_colors colors;
473 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
474 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
475 6458efa5 2020-11-24 stsp struct got_reference *ref;
479 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
481 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
482 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
483 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
486 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
487 6458efa5 2020-11-24 stsp struct got_repository *repo;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
489 bddb1296 2019-11-08 stsp struct tog_colors colors;
493 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
495 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
496 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
497 669b5ffa 2018-10-07 stsp * there is enough screen estate.
499 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
500 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
502 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
503 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
504 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
506 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
507 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
509 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
511 cc3c9aac 2018-08-01 stsp struct tog_view {
512 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
513 26ed57b2 2018-05-19 stsp WINDOW *window;
514 26ed57b2 2018-05-19 stsp PANEL *panel;
515 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
516 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
517 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
518 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
519 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
520 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
521 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
522 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
524 669b5ffa 2018-10-07 stsp struct tog_view *parent;
525 669b5ffa 2018-10-07 stsp struct tog_view *child;
528 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
529 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
530 e78dc838 2020-12-04 stsp * between parent and child.
531 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
532 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
533 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
534 e78dc838 2020-12-04 stsp * situations.
536 e78dc838 2020-12-04 stsp int focus_child;
538 9b058f45 2022-06-30 mark enum tog_view_mode mode;
539 5dc9f4bc 2018-08-04 stsp /* type-specific state */
540 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
542 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
543 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
544 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
545 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
546 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
549 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
550 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
551 e78dc838 2020-12-04 stsp struct tog_view *, int);
552 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
553 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
554 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
556 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
557 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
558 c0c4acc8 2021-01-24 stsp int search_started;
559 60493ae3 2019-06-20 stsp int searching;
560 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
561 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
562 60493ae3 2019-06-20 stsp int search_next_done;
563 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
564 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
565 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
566 1803e47f 2019-06-22 stsp regex_t regex;
567 41605754 2020-11-12 stsp regmatch_t regmatch;
570 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
571 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
572 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
573 78756c87 2020-11-24 stsp struct got_repository *);
574 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
575 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
576 e78dc838 2020-12-04 stsp struct tog_view *, int);
577 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
579 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
580 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
582 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
583 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
584 78756c87 2020-11-24 stsp const char *, const char *, int);
585 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
587 e78dc838 2020-12-04 stsp struct tog_view *, int);
588 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
590 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
593 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
594 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
595 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
596 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
597 e78dc838 2020-12-04 stsp struct tog_view *, int);
598 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
600 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
603 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
604 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
605 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
606 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
607 e78dc838 2020-12-04 stsp struct tog_view *, int);
608 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
609 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
613 6458efa5 2020-11-24 stsp struct got_repository *);
614 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
615 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
616 e78dc838 2020-12-04 stsp struct tog_view *, int);
617 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
618 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
621 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
622 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
623 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
624 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
625 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
627 25791caa 2018-10-24 stsp static void
628 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
630 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
633 83baff54 2019-08-12 stsp static void
634 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
636 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
639 61266923 2020-01-14 stsp static void
640 61266923 2020-01-14 stsp tog_sigcont(int signo)
642 61266923 2020-01-14 stsp tog_sigcont_received = 1;
645 2497f032 2022-05-31 stsp static void
646 2497f032 2022-05-31 stsp tog_sigint(int signo)
648 2497f032 2022-05-31 stsp tog_sigint_received = 1;
651 2497f032 2022-05-31 stsp static void
652 2497f032 2022-05-31 stsp tog_sigterm(int signo)
654 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
658 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
660 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
661 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
664 e5a0f69f 2018-08-18 stsp static const struct got_error *
665 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
667 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
669 669b5ffa 2018-10-07 stsp if (view->child) {
670 5629093a 2022-07-11 stsp child_err = view_close(view->child);
671 669b5ffa 2018-10-07 stsp view->child = NULL;
673 e5a0f69f 2018-08-18 stsp if (view->close)
674 e5a0f69f 2018-08-18 stsp err = view->close(view);
675 ea5e7bb5 2018-08-01 stsp if (view->panel)
676 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
677 ea5e7bb5 2018-08-01 stsp if (view->window)
678 ea5e7bb5 2018-08-01 stsp delwin(view->window);
679 ea5e7bb5 2018-08-01 stsp free(view);
680 5629093a 2022-07-11 stsp return err ? err : child_err;
683 ea5e7bb5 2018-08-01 stsp static struct tog_view *
684 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
685 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
687 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
689 ea5e7bb5 2018-08-01 stsp if (view == NULL)
690 ea5e7bb5 2018-08-01 stsp return NULL;
692 d6b05b5b 2018-08-04 stsp view->type = type;
693 f7d12f7e 2018-08-01 stsp view->lines = LINES;
694 f7d12f7e 2018-08-01 stsp view->cols = COLS;
695 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
696 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
697 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
698 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
699 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
700 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
701 96a765a8 2018-08-04 stsp view_close(view);
702 ea5e7bb5 2018-08-01 stsp return NULL;
704 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
705 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
706 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
711 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
712 ea5e7bb5 2018-08-01 stsp return view;
716 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
718 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
720 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
723 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
725 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
727 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
730 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
732 5c60c32a 2018-10-18 stsp static const struct got_error *
733 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
735 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
737 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
738 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
739 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
741 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
742 9b058f45 2022-06-30 mark view->begin_x = 0;
743 571ccd73 2022-07-19 mark } else if (!view->resized) {
744 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
745 3c1dfe12 2022-07-08 mark view->cols > 119)
746 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
748 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
749 9b058f45 2022-06-30 mark view->begin_y = 0;
751 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
752 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
753 5c60c32a 2018-10-18 stsp view->lines = LINES;
754 5c60c32a 2018-10-18 stsp view->cols = COLS;
755 5c60c32a 2018-10-18 stsp err = view_resize(view);
757 5c60c32a 2018-10-18 stsp return err;
759 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
760 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
762 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
763 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
765 5c60c32a 2018-10-18 stsp return NULL;
768 5c60c32a 2018-10-18 stsp static const struct got_error *
769 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
771 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
773 5c60c32a 2018-10-18 stsp view->begin_x = 0;
774 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
775 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
776 5c60c32a 2018-10-18 stsp view->ncols = COLS;
777 5c60c32a 2018-10-18 stsp view->lines = LINES;
778 5c60c32a 2018-10-18 stsp view->cols = COLS;
779 5c60c32a 2018-10-18 stsp err = view_resize(view);
781 5c60c32a 2018-10-18 stsp return err;
783 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
784 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
786 5c60c32a 2018-10-18 stsp return NULL;
790 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
792 5c60c32a 2018-10-18 stsp return view->parent == NULL;
796 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
798 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
802 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
804 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
808 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
810 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
811 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
814 9b058f45 2022-06-30 mark static void
815 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
817 9b058f45 2022-06-30 mark PANEL *panel;
818 9b058f45 2022-06-30 mark const struct tog_view *view_above;
820 9b058f45 2022-06-30 mark if (view->parent)
821 9b058f45 2022-06-30 mark return view_border(view->parent);
823 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
824 9b058f45 2022-06-30 mark if (panel == NULL)
827 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
828 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
829 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
830 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
831 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
833 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
834 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
837 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
838 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
839 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
840 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
841 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
843 4d8c2215 2018-08-19 stsp static const struct got_error *
844 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
846 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
847 9b058f45 2022-06-30 mark int dif, nlines, ncols;
849 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
851 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
852 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
854 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
855 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
856 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
858 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
860 4dd27a72 2022-06-29 stsp if (view->child) {
861 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
863 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
864 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
865 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
866 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
867 0dbbbe90 2022-06-17 op ncols = COLS;
869 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
870 5c60c32a 2018-10-18 stsp if (view->child->focussed)
871 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
873 5c60c32a 2018-10-18 stsp show_panel(view->panel);
875 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
877 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
878 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
881 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
882 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
883 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
886 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
888 9b058f45 2022-06-30 mark return err;
889 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
890 9b058f45 2022-06-30 mark err = offset_selection_down(view);
892 9b058f45 2022-06-30 mark return err;
894 9b058f45 2022-06-30 mark view_border(view);
895 9b058f45 2022-06-30 mark update_panels();
896 9b058f45 2022-06-30 mark doupdate();
897 9b058f45 2022-06-30 mark show_panel(view->child->panel);
898 9b058f45 2022-06-30 mark nlines = view->nlines;
900 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
901 0dbbbe90 2022-06-17 op ncols = COLS;
903 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
904 571ccd73 2022-07-19 mark err = view->resize(view, dif);
906 571ccd73 2022-07-19 mark return err;
909 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
910 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
911 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
912 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
913 0dbbbe90 2022-06-17 op wclear(view->window);
915 0dbbbe90 2022-06-17 op view->nlines = nlines;
916 0dbbbe90 2022-06-17 op view->ncols = ncols;
917 0dbbbe90 2022-06-17 op view->lines = LINES;
918 0dbbbe90 2022-06-17 op view->cols = COLS;
920 5c60c32a 2018-10-18 stsp return NULL;
923 571ccd73 2022-07-19 mark static const struct got_error *
924 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
926 571ccd73 2022-07-19 mark struct tog_log_view_state *s = &view->state.log;
927 571ccd73 2022-07-19 mark const struct got_error *err = NULL;
928 571ccd73 2022-07-19 mark int n = s->selected_entry->idx + view->lines - s->selected;
931 571ccd73 2022-07-19 mark * Request commits to account for the increased
932 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
934 571ccd73 2022-07-19 mark if (s->commits.ncommits < n) {
935 571ccd73 2022-07-19 mark view->nscrolled = n - s->commits.ncommits + increase + 1;
936 571ccd73 2022-07-19 mark err = request_log_commits(view);
939 571ccd73 2022-07-19 mark return err;
942 d9a7ab53 2022-07-11 mark static void
943 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
945 d9a7ab53 2022-07-11 mark if (n == 0)
948 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
949 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
950 d9a7ab53 2022-07-11 mark view->parent->offset += n;
952 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
953 d9a7ab53 2022-07-11 mark } else if (view->offset) {
954 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
955 d9a7ab53 2022-07-11 mark view->offset -= n;
957 d9a7ab53 2022-07-11 mark view->offset = 0;
961 3c1dfe12 2022-07-08 mark static const struct got_error *
962 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
964 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
965 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
967 3c1dfe12 2022-07-08 mark if (view->parent)
968 3c1dfe12 2022-07-08 mark v = view->parent;
972 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
973 3c1dfe12 2022-07-08 mark return NULL;
975 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
977 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
978 27187d45 2022-07-11 mark int y = v->child->begin_y;
980 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
981 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
982 3c1dfe12 2022-07-08 mark if (view->parent)
983 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
985 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
986 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
987 3c1dfe12 2022-07-08 mark view->count = 0;
988 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
989 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
990 3c1dfe12 2022-07-08 mark view->count = 0;
991 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
993 3c1dfe12 2022-07-08 mark v->ncols = COLS;
994 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
995 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
996 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
998 3c1dfe12 2022-07-08 mark return err;
999 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1000 2525dccb 2022-07-13 mark if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1001 27187d45 2022-07-11 mark v->child->nscrolled = y - v->child->begin_y;
1002 2525dccb 2022-07-13 mark else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1003 2525dccb 2022-07-13 mark v->nscrolled = v->child->begin_y - y;
1005 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1006 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1007 3c1dfe12 2022-07-08 mark if (view->parent)
1008 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1010 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1011 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1012 3c1dfe12 2022-07-08 mark view->count = 0;
1013 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1014 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1015 3c1dfe12 2022-07-08 mark view->count = 0;
1016 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1018 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1021 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1022 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1023 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1024 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1026 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1028 3c1dfe12 2022-07-08 mark return err;
1029 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1031 3c1dfe12 2022-07-08 mark return err;
1033 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1034 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1036 3c1dfe12 2022-07-08 mark return err;
1039 2525dccb 2022-07-13 mark if (v->nscrolled)
1040 3c1dfe12 2022-07-08 mark err = request_log_commits(v);
1041 2525dccb 2022-07-13 mark else if (v->child->nscrolled)
1042 3c1dfe12 2022-07-08 mark err = request_log_commits(v->child);
1044 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1046 3c1dfe12 2022-07-08 mark return err;
1049 3c1dfe12 2022-07-08 mark static void
1050 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1052 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1054 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1055 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1058 669b5ffa 2018-10-07 stsp static const struct got_error *
1059 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1061 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1063 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1064 669b5ffa 2018-10-07 stsp return NULL;
1066 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1067 669b5ffa 2018-10-07 stsp view->child = NULL;
1068 669b5ffa 2018-10-07 stsp return err;
1071 0dbbbe90 2022-06-17 op static const struct got_error *
1072 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1074 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1076 669b5ffa 2018-10-07 stsp view->child = child;
1077 669b5ffa 2018-10-07 stsp child->parent = view;
1079 3c1dfe12 2022-07-08 mark err = view_resize(view);
1081 3c1dfe12 2022-07-08 mark return err;
1083 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1084 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1086 3c1dfe12 2022-07-08 mark return err;
1089 34bc9ec9 2019-02-22 stsp static void
1090 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1092 25791caa 2018-10-24 stsp int cols, lines;
1093 25791caa 2018-10-24 stsp struct winsize size;
1095 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1096 25791caa 2018-10-24 stsp cols = 80; /* Default */
1097 25791caa 2018-10-24 stsp lines = 24;
1099 25791caa 2018-10-24 stsp cols = size.ws_col;
1100 25791caa 2018-10-24 stsp lines = size.ws_row;
1102 25791caa 2018-10-24 stsp resize_term(lines, cols);
1105 2b49a8ae 2019-06-22 stsp static const struct got_error *
1106 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1108 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1109 9b058f45 2022-06-30 mark struct tog_view *v = view;
1110 2b49a8ae 2019-06-22 stsp char pattern[1024];
1113 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1114 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1115 c0c4acc8 2021-01-24 stsp view->searching = 0;
1116 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1118 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1120 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1121 2b49a8ae 2019-06-22 stsp return NULL;
1123 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1124 9b058f45 2022-06-30 mark v = view->child;
1126 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1127 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1129 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1130 2b49a8ae 2019-06-22 stsp nocbreak();
1132 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1133 9b058f45 2022-06-30 mark wrefresh(v->window);
1136 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1137 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1138 2b49a8ae 2019-06-22 stsp return NULL;
1140 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1141 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1142 7c32bd05 2019-06-22 stsp if (err) {
1143 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1144 7c32bd05 2019-06-22 stsp return err;
1146 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1147 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1148 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1149 2b49a8ae 2019-06-22 stsp view->search_next(view);
1152 2b49a8ae 2019-06-22 stsp return NULL;
1155 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1156 d2366e29 2022-07-07 mark static const struct got_error *
1157 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1159 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1160 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1162 d2366e29 2022-07-07 mark if (view->parent)
1163 d2366e29 2022-07-07 mark v = view->parent;
1167 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1168 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1170 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1172 7532ccda 2022-07-11 mark if (!v->child)
1173 7532ccda 2022-07-11 mark return NULL;
1174 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1175 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1177 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1178 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1179 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1180 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1181 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1184 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1185 d2366e29 2022-07-07 mark v->ncols = COLS;
1186 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1187 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1189 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1191 d2366e29 2022-07-07 mark return err;
1193 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1194 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1195 d2366e29 2022-07-07 mark v->focus_child = 1;
1197 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1199 d2366e29 2022-07-07 mark return err;
1200 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1202 d2366e29 2022-07-07 mark return err;
1204 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1205 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1206 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1207 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1208 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1210 7532ccda 2022-07-11 mark offset_selection_up(v);
1211 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1213 27187d45 2022-07-11 mark if (v->type == TOG_VIEW_LOG && v->nscrolled)
1214 7532ccda 2022-07-11 mark err = request_log_commits(v);
1215 27187d45 2022-07-11 mark else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1216 7532ccda 2022-07-11 mark err = request_log_commits(v->child);
1218 d2366e29 2022-07-07 mark return err;
1222 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1223 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1225 640cd7ff 2022-06-22 mark static int
1226 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1228 9b058f45 2022-06-30 mark struct tog_view *v = view;
1229 9b058f45 2022-06-30 mark int x, n = 0;
1231 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1232 9b058f45 2022-06-30 mark v = view->child;
1233 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1234 9b058f45 2022-06-30 mark v = view->parent;
1236 640cd7ff 2022-06-22 mark view->count = 0;
1237 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1238 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1239 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1240 9b058f45 2022-06-30 mark waddch(v->window, ':');
1243 9b058f45 2022-06-30 mark x = getcurx(v->window);
1244 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1245 9b058f45 2022-06-30 mark waddch(v->window, c);
1246 9b058f45 2022-06-30 mark wrefresh(v->window);
1250 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1251 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1253 640cd7ff 2022-06-22 mark if (n >= 9999999)
1254 640cd7ff 2022-06-22 mark n = 9999999;
1256 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1257 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1259 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1260 640cd7ff 2022-06-22 mark view->count = n;
1265 0cf4efb1 2018-09-29 stsp static const struct got_error *
1266 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1267 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1269 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1270 669b5ffa 2018-10-07 stsp struct tog_view *v;
1271 1a76625f 2018-10-22 stsp int ch, errcode;
1273 e5a0f69f 2018-08-18 stsp *new = NULL;
1275 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1276 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1277 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1278 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1279 640cd7ff 2022-06-22 mark view->count = 0;
1282 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1283 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1284 82954512 2020-02-03 stsp if (errcode)
1285 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1286 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1287 3da8ef85 2021-09-21 stsp sched_yield();
1288 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1289 82954512 2020-02-03 stsp if (errcode)
1290 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1291 82954512 2020-02-03 stsp "pthread_mutex_lock");
1292 60493ae3 2019-06-20 stsp view->search_next(view);
1293 60493ae3 2019-06-20 stsp return NULL;
1296 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE);
1297 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1298 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1299 1a76625f 2018-10-22 stsp if (errcode)
1300 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1301 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1302 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1304 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1305 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1306 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1307 a6d37fac 2022-07-03 mark view->count = 0;
1309 a6d37fac 2022-07-03 mark ch = view->ch;
1311 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1312 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1313 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1315 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1316 1a76625f 2018-10-22 stsp if (errcode)
1317 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1318 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1320 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1321 25791caa 2018-10-24 stsp tog_resizeterm();
1322 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1323 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1324 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1325 25791caa 2018-10-24 stsp err = view_resize(v);
1327 25791caa 2018-10-24 stsp return err;
1328 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1330 25791caa 2018-10-24 stsp return err;
1331 cdfcfb03 2020-12-06 stsp if (v->child) {
1332 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1334 cdfcfb03 2020-12-06 stsp return err;
1335 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1336 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1338 cdfcfb03 2020-12-06 stsp return err;
1339 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1340 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1342 3c1dfe12 2022-07-08 mark return err;
1348 e5a0f69f 2018-08-18 stsp switch (ch) {
1350 640cd7ff 2022-06-22 mark view->count = 0;
1351 1e37a5c2 2019-05-12 jcs if (view->child) {
1352 e78dc838 2020-12-04 stsp view->focussed = 0;
1353 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1354 e78dc838 2020-12-04 stsp view->focus_child = 1;
1355 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1356 e78dc838 2020-12-04 stsp view->focussed = 0;
1357 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1358 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1359 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1360 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1361 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1362 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1364 9b058f45 2022-06-30 mark return err;
1366 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1367 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1369 9b058f45 2022-06-30 mark return err;
1374 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1375 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1376 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1377 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1381 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1383 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1384 9970f7fc 2020-12-03 stsp view->dying = 1;
1390 640cd7ff 2022-06-22 mark view->count = 0;
1391 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1392 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1394 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1395 e78dc838 2020-12-04 stsp view->focussed = 0;
1396 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1397 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1399 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1401 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1405 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1406 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1408 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1409 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1410 e78dc838 2020-12-04 stsp view->focussed = 1;
1411 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1413 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1414 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1415 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1417 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1421 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1425 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1426 9b058f45 2022-06-30 mark err = request_log_commits(view);
1430 9b058f45 2022-06-30 mark if (view->parent)
1431 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1433 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1436 3c1dfe12 2022-07-08 mark view->count = 0;
1437 d2366e29 2022-07-07 mark err = switch_split(view);
1440 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1443 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1445 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1448 640cd7ff 2022-06-22 mark view->count = 0;
1449 60493ae3 2019-06-20 stsp if (view->search_start)
1450 2b49a8ae 2019-06-22 stsp view_search_start(view);
1452 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1456 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1457 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1458 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1459 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1460 60493ae3 2019-06-20 stsp view->search_next(view);
1462 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1465 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1466 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1468 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1469 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1470 917d79a7 2022-07-01 stsp if (v->reset) {
1471 917d79a7 2022-07-01 stsp err = v->reset(v);
1473 917d79a7 2022-07-01 stsp return err;
1475 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1476 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1478 917d79a7 2022-07-01 stsp return err;
1483 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1487 e5a0f69f 2018-08-18 stsp return err;
1491 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1493 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1494 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1496 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1498 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1501 669b5ffa 2018-10-07 stsp return view->focussed;
1504 bcbd79e2 2018-08-19 stsp static const struct got_error *
1505 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1507 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1508 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1509 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1510 d2366e29 2022-07-07 mark char *mode;
1511 fd823528 2018-10-22 stsp int fast_refresh = 10;
1512 1a76625f 2018-10-22 stsp int done = 0, errcode;
1514 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1515 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1516 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1518 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1520 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1521 1a76625f 2018-10-22 stsp if (errcode)
1522 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1524 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1525 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1527 1004088d 2018-09-29 stsp view->focussed = 1;
1528 878940b7 2018-09-29 stsp err = view->show(view);
1530 0cf4efb1 2018-09-29 stsp return err;
1531 0cf4efb1 2018-09-29 stsp update_panels();
1532 0cf4efb1 2018-09-29 stsp doupdate();
1533 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1534 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1535 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1536 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1537 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1539 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1542 9970f7fc 2020-12-03 stsp if (view->dying) {
1543 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1545 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1546 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1548 e78dc838 2020-12-04 stsp else if (view->parent)
1549 669b5ffa 2018-10-07 stsp prev = view->parent;
1551 e78dc838 2020-12-04 stsp if (view->parent) {
1552 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1553 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1554 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1555 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1556 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1559 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1560 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1562 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1564 9970f7fc 2020-12-03 stsp err = view_close(view);
1566 e5a0f69f 2018-08-18 stsp goto done;
1568 e78dc838 2020-12-04 stsp view = NULL;
1569 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1570 e78dc838 2020-12-04 stsp if (v->focussed)
1573 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1574 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1576 e78dc838 2020-12-04 stsp view = prev;
1577 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1578 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1579 e78dc838 2020-12-04 stsp tog_view_list_head);
1581 e78dc838 2020-12-04 stsp if (view) {
1582 e78dc838 2020-12-04 stsp if (view->focus_child) {
1583 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1584 e78dc838 2020-12-04 stsp view = view->child;
1586 e78dc838 2020-12-04 stsp view->focussed = 1;
1590 bcbd79e2 2018-08-19 stsp if (new_view) {
1591 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1592 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1593 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1594 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1596 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1597 86c66b02 2018-10-18 stsp err = view_close(v);
1599 86c66b02 2018-10-18 stsp goto done;
1602 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1603 fed7eaa8 2018-10-24 stsp view = new_view;
1605 669b5ffa 2018-10-07 stsp if (view) {
1606 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1607 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1608 e78dc838 2020-12-04 stsp view = view->child;
1610 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1611 e78dc838 2020-12-04 stsp view = view->parent;
1613 e78dc838 2020-12-04 stsp show_panel(view->panel);
1614 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1615 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1616 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1617 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1619 1a76625f 2018-10-22 stsp goto done;
1621 669b5ffa 2018-10-07 stsp err = view->show(view);
1623 1a76625f 2018-10-22 stsp goto done;
1624 669b5ffa 2018-10-07 stsp if (view->child) {
1625 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1627 1a76625f 2018-10-22 stsp goto done;
1629 1a76625f 2018-10-22 stsp update_panels();
1630 1a76625f 2018-10-22 stsp doupdate();
1634 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1635 5629093a 2022-07-11 stsp const struct got_error *close_err;
1636 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1637 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1638 5629093a 2022-07-11 stsp close_err = view_close(view);
1639 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1640 5629093a 2022-07-11 stsp err = close_err;
1643 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1644 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1645 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1647 e5a0f69f 2018-08-18 stsp return err;
1650 4ed7e80c 2018-05-20 stsp __dead static void
1651 9f7d7167 2018-04-29 stsp usage_log(void)
1654 c70c5802 2018-08-01 stsp fprintf(stderr,
1655 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1656 9f7d7167 2018-04-29 stsp getprogname());
1660 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1661 80ddbec8 2018-04-29 stsp static const struct got_error *
1662 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1664 00dfcb92 2018-06-11 stsp char *vis = NULL;
1665 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1667 963b370f 2018-05-20 stsp *ws = NULL;
1668 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1669 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1670 00dfcb92 2018-06-11 stsp int vislen;
1671 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1672 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1674 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1675 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1677 00dfcb92 2018-06-11 stsp return err;
1678 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1679 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1680 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1681 a7f50699 2018-06-11 stsp goto done;
1685 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1686 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1687 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1688 a7f50699 2018-06-11 stsp goto done;
1691 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1692 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1694 00dfcb92 2018-06-11 stsp free(vis);
1695 963b370f 2018-05-20 stsp if (err) {
1696 963b370f 2018-05-20 stsp free(*ws);
1697 963b370f 2018-05-20 stsp *ws = NULL;
1698 963b370f 2018-05-20 stsp *wlen = 0;
1700 963b370f 2018-05-20 stsp return err;
1703 145b6838 2022-06-16 stsp static const struct got_error *
1704 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1706 145b6838 2022-06-16 stsp char *dst;
1707 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1709 145b6838 2022-06-16 stsp *ptr = NULL;
1710 145b6838 2022-06-16 stsp n = len = strlen(src);
1711 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1712 145b6838 2022-06-16 stsp if (dst == NULL)
1713 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1715 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1716 145b6838 2022-06-16 stsp const char c = src[idx];
1718 145b6838 2022-06-16 stsp if (c == '\t') {
1719 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1722 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1723 6e1c41ad 2022-06-16 mark if (p == NULL) {
1724 6e1c41ad 2022-06-16 mark free(dst);
1725 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1730 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1733 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1737 145b6838 2022-06-16 stsp dst[sz] = '\0';
1738 145b6838 2022-06-16 stsp *ptr = dst;
1739 145b6838 2022-06-16 stsp return NULL;
1743 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1744 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1745 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1749 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1751 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1753 4e4a9ac8 2022-06-17 op if (n == 0) {
1754 4e4a9ac8 2022-06-17 op *rcol = cols;
1758 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1759 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1760 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1762 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1764 4e4a9ac8 2022-06-17 op if (width == -1) {
1766 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1769 4e4a9ac8 2022-06-17 op if (cols + width > n)
1771 4e4a9ac8 2022-06-17 op cols += width;
1774 4e4a9ac8 2022-06-17 op *rcol = cols;
1779 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1780 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1781 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1783 ccda2f4d 2022-06-16 stsp static const struct got_error *
1784 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1785 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1787 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1789 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1790 145b6838 2022-06-16 stsp char *exstr = NULL;
1791 963b370f 2018-05-20 stsp size_t wlen;
1792 4e4a9ac8 2022-06-17 op int i, scrollx;
1794 963b370f 2018-05-20 stsp *wlinep = NULL;
1795 b700b5d6 2018-07-10 stsp *widthp = 0;
1797 145b6838 2022-06-16 stsp if (expand) {
1798 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1800 145b6838 2022-06-16 stsp return err;
1803 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1804 145b6838 2022-06-16 stsp free(exstr);
1806 963b370f 2018-05-20 stsp return err;
1808 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1810 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1811 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1814 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1815 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1819 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1820 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1822 b700b5d6 2018-07-10 stsp if (widthp)
1823 b700b5d6 2018-07-10 stsp *widthp = cols;
1824 ccda2f4d 2022-06-16 stsp if (scrollxp)
1825 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1827 963b370f 2018-05-20 stsp free(wline);
1829 963b370f 2018-05-20 stsp *wlinep = wline;
1830 963b370f 2018-05-20 stsp return err;
1833 8b473291 2019-02-21 stsp static const struct got_error*
1834 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1835 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1837 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1838 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1840 8b473291 2019-02-21 stsp const char *name;
1842 8b473291 2019-02-21 stsp *refs_str = NULL;
1844 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1845 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1846 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1849 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1850 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1852 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1853 8b473291 2019-02-21 stsp name += 5;
1854 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1855 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1857 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1858 8b473291 2019-02-21 stsp name += 6;
1859 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1860 8b473291 2019-02-21 stsp name += 8;
1861 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1862 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1865 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1868 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1869 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1870 5d844a1e 2019-08-13 stsp if (err) {
1871 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1872 48cae60d 2020-09-22 stsp free(ref_id);
1875 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1876 5d844a1e 2019-08-13 stsp err = NULL;
1877 5d844a1e 2019-08-13 stsp tag = NULL;
1880 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1881 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1882 48cae60d 2020-09-22 stsp free(ref_id);
1884 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1885 52b5abe1 2019-08-13 stsp if (cmp != 0)
1887 8b473291 2019-02-21 stsp s = *refs_str;
1888 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1889 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1890 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1892 8b473291 2019-02-21 stsp *refs_str = NULL;
1898 8b473291 2019-02-21 stsp return err;
1901 963b370f 2018-05-20 stsp static const struct got_error *
1902 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1903 27a741e5 2019-09-11 stsp int col_tab_align)
1905 e6b8b890 2020-12-29 naddy char *smallerthan;
1907 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1908 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1909 5813d178 2019-03-09 stsp author = smallerthan + 1;
1910 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1911 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1912 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1915 5813d178 2019-03-09 stsp static const struct got_error *
1916 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1917 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1918 8fdc79fe 2020-12-01 naddy int author_display_cols)
1920 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1921 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1922 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1923 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1924 5813d178 2019-03-09 stsp char *author = NULL;
1925 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1926 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1927 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1928 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1929 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1930 ccb26ccd 2018-11-05 stsp struct tm tm;
1931 45d799e2 2018-12-23 stsp time_t committer_time;
1932 11b20872 2019-11-08 stsp struct tog_color *tc;
1934 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1935 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1936 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1937 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1938 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1940 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1941 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1943 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1944 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1946 11b20872 2019-11-08 stsp wattr_on(view->window,
1947 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1948 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1950 11b20872 2019-11-08 stsp wattr_off(view->window,
1951 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1952 27a741e5 2019-09-11 stsp col = limit;
1953 b39d25c7 2018-07-10 stsp if (col > avail)
1954 b39d25c7 2018-07-10 stsp goto done;
1956 6570a66d 2019-11-08 stsp if (avail >= 120) {
1957 6570a66d 2019-11-08 stsp char *id_str;
1958 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1960 6570a66d 2019-11-08 stsp goto done;
1961 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1963 11b20872 2019-11-08 stsp wattr_on(view->window,
1964 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1965 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1967 11b20872 2019-11-08 stsp wattr_off(view->window,
1968 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1969 6570a66d 2019-11-08 stsp free(id_str);
1971 6570a66d 2019-11-08 stsp if (col > avail)
1972 6570a66d 2019-11-08 stsp goto done;
1975 10aab77f 2022-07-19 op if (s->use_committer)
1976 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
1978 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
1979 5813d178 2019-03-09 stsp if (author == NULL) {
1980 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1981 80ddbec8 2018-04-29 stsp goto done;
1983 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1985 bb737323 2018-05-20 stsp goto done;
1986 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1988 11b20872 2019-11-08 stsp wattr_on(view->window,
1989 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1990 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1992 11b20872 2019-11-08 stsp wattr_off(view->window,
1993 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1994 bb737323 2018-05-20 stsp col += author_width;
1995 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1996 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1998 bb737323 2018-05-20 stsp author_width++;
2000 9c2eaf34 2018-05-20 stsp if (col > avail)
2001 9c2eaf34 2018-05-20 stsp goto done;
2003 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2005 6d9fbc00 2018-04-29 stsp goto done;
2006 bb737323 2018-05-20 stsp logmsg = logmsg0;
2007 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2009 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2010 bb737323 2018-05-20 stsp if (newline)
2011 bb737323 2018-05-20 stsp *newline = '\0';
2012 ccda2f4d 2022-06-16 stsp limit = avail - col;
2013 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2014 4d1f6af3 2022-06-17 op limit--; /* for the border */
2015 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2016 ccda2f4d 2022-06-16 stsp limit, col, 1);
2018 29688b02 2022-06-16 stsp goto done;
2019 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2020 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2021 27a741e5 2019-09-11 stsp while (col < avail) {
2022 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2026 80ddbec8 2018-04-29 stsp free(logmsg0);
2027 bb737323 2018-05-20 stsp free(wlogmsg);
2028 5813d178 2019-03-09 stsp free(author);
2029 bb737323 2018-05-20 stsp free(wauthor);
2030 80ddbec8 2018-04-29 stsp free(line);
2031 80ddbec8 2018-04-29 stsp return err;
2034 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2035 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2036 899d86c2 2018-05-10 stsp struct got_object_id *id)
2038 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2040 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2041 80ddbec8 2018-04-29 stsp if (entry == NULL)
2042 899d86c2 2018-05-10 stsp return NULL;
2044 899d86c2 2018-05-10 stsp entry->id = id;
2045 99db9666 2018-05-07 stsp entry->commit = commit;
2046 899d86c2 2018-05-10 stsp return entry;
2049 99db9666 2018-05-07 stsp static void
2050 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2052 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2054 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2055 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2056 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2057 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2058 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2059 99db9666 2018-05-07 stsp free(entry);
2062 99db9666 2018-05-07 stsp static void
2063 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2065 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2066 99db9666 2018-05-07 stsp pop_commit(commits);
2069 c4972b91 2018-05-07 stsp static const struct got_error *
2070 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2071 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2073 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2074 13add988 2019-10-15 stsp regmatch_t regmatch;
2075 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2077 13add988 2019-10-15 stsp *have_match = 0;
2079 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2081 13add988 2019-10-15 stsp return err;
2083 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2085 13add988 2019-10-15 stsp goto done;
2087 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2088 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2089 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2090 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2091 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2092 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2093 13add988 2019-10-15 stsp *have_match = 1;
2095 13add988 2019-10-15 stsp free(id_str);
2096 13add988 2019-10-15 stsp free(logmsg);
2097 13add988 2019-10-15 stsp return err;
2100 13add988 2019-10-15 stsp static const struct got_error *
2101 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2103 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2106 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2107 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2108 1a76625f 2018-10-22 stsp * while updating the display.
2111 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2112 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2113 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2114 1a76625f 2018-10-22 stsp int errcode;
2116 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2117 4e0d2870 2020-12-07 naddy NULL, NULL);
2118 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2121 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2124 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2125 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2126 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2130 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2131 1a76625f 2018-10-22 stsp if (errcode) {
2132 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2133 13add988 2019-10-15 stsp "pthread_mutex_lock");
2137 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2138 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2139 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2141 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2142 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2143 7c1452c1 2020-03-26 stsp int have_match;
2144 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2147 7c1452c1 2020-03-26 stsp if (have_match)
2148 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2151 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2152 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2153 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2154 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2157 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2159 9ba79e04 2018-06-11 stsp return err;
2162 2b779855 2020-12-05 naddy static void
2163 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2165 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2166 2b779855 2020-12-05 naddy int ncommits = 0;
2168 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2169 2b779855 2020-12-05 naddy while (entry) {
2170 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2171 2b779855 2020-12-05 naddy s->selected_entry = entry;
2174 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2175 2b779855 2020-12-05 naddy ncommits++;
2179 0553a4e3 2018-04-30 stsp static const struct got_error *
2180 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2182 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2183 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2184 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2185 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2186 60493ae3 2019-06-20 stsp int width;
2187 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2188 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2189 8b473291 2019-02-21 stsp char *refs_str = NULL;
2190 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2191 11b20872 2019-11-08 stsp struct tog_color *tc;
2192 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2194 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2195 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2196 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2197 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2199 ecb28ae0 2018-07-16 stsp return err;
2200 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2201 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2202 d2075bf3 2020-12-25 stsp if (refs) {
2203 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2204 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2206 d2075bf3 2020-12-25 stsp goto done;
2210 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2211 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2213 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2214 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2215 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2216 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2217 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2218 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2219 8f4ed634 2020-03-26 stsp goto done;
2222 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2224 f9686aa5 2020-03-27 stsp if (view->searching) {
2225 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2226 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2227 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2228 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2229 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2230 f9686aa5 2020-03-27 stsp search_str = "searching...";
2233 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2234 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2235 f9686aa5 2020-03-27 stsp search_str ? search_str :
2236 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2237 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2238 8f4ed634 2020-03-26 stsp goto done;
2242 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2243 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2244 87411fa9 2022-06-16 stsp "........................................",
2245 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2247 1a76625f 2018-10-22 stsp header = NULL;
2248 1a76625f 2018-10-22 stsp goto done;
2250 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2251 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2252 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2253 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2254 1a76625f 2018-10-22 stsp header = NULL;
2255 1a76625f 2018-10-22 stsp goto done;
2257 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2259 1a76625f 2018-10-22 stsp goto done;
2261 2814baeb 2018-08-01 stsp werase(view->window);
2263 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2264 a3404814 2018-09-02 stsp wstandout(view->window);
2265 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2267 11b20872 2019-11-08 stsp wattr_on(view->window,
2268 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2269 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2271 11b20872 2019-11-08 stsp wattr_off(view->window,
2272 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2273 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2274 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2277 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2278 a3404814 2018-09-02 stsp wstandend(view->window);
2279 ecb28ae0 2018-07-16 stsp free(wline);
2280 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2281 1a76625f 2018-10-22 stsp goto done;
2283 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2284 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2285 5813d178 2019-03-09 stsp ncommits = 0;
2286 145b6838 2022-06-16 stsp view->maxx = 0;
2287 5813d178 2019-03-09 stsp while (entry) {
2288 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2289 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2290 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2291 5813d178 2019-03-09 stsp int width;
2292 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2294 10aab77f 2022-07-19 op if (s->use_committer)
2295 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2297 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2298 5813d178 2019-03-09 stsp if (author == NULL) {
2299 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2300 5813d178 2019-03-09 stsp goto done;
2302 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2303 27a741e5 2019-09-11 stsp date_display_cols);
2304 5813d178 2019-03-09 stsp if (author_cols < width)
2305 5813d178 2019-03-09 stsp author_cols = width;
2306 5813d178 2019-03-09 stsp free(wauthor);
2307 5813d178 2019-03-09 stsp free(author);
2308 a310d9c3 2022-07-21 florian if (err)
2309 a310d9c3 2022-07-21 florian goto done;
2310 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2312 145b6838 2022-06-16 stsp goto done;
2313 145b6838 2022-06-16 stsp msg = msg0;
2314 145b6838 2022-06-16 stsp while (*msg == '\n')
2316 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2317 29688b02 2022-06-16 stsp *eol = '\0';
2318 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2319 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2321 29688b02 2022-06-16 stsp goto done;
2322 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2323 145b6838 2022-06-16 stsp free(msg0);
2324 29688b02 2022-06-16 stsp free(wmsg);
2325 7ca04879 2019-10-19 stsp ncommits++;
2326 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2329 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2330 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2331 867c6645 2018-07-10 stsp ncommits = 0;
2332 899d86c2 2018-05-10 stsp while (entry) {
2333 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2335 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2336 2814baeb 2018-08-01 stsp wstandout(view->window);
2337 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2338 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2339 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2340 2814baeb 2018-08-01 stsp wstandend(view->window);
2342 60493ae3 2019-06-20 stsp goto done;
2343 0553a4e3 2018-04-30 stsp ncommits++;
2344 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2345 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2348 9b058f45 2022-06-30 mark view_border(view);
2350 1a76625f 2018-10-22 stsp free(id_str);
2351 8b473291 2019-02-21 stsp free(refs_str);
2352 1a76625f 2018-10-22 stsp free(ncommits_str);
2353 1a76625f 2018-10-22 stsp free(header);
2354 80ddbec8 2018-04-29 stsp return err;
2357 07b55e75 2018-05-10 stsp static void
2358 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2360 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2361 07b55e75 2018-05-10 stsp int nscrolled = 0;
2363 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2364 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2367 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2368 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2369 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2370 07b55e75 2018-05-10 stsp if (entry) {
2371 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2372 07b55e75 2018-05-10 stsp nscrolled++;
2377 aa075928 2018-05-10 stsp static const struct got_error *
2378 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2380 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2381 5e224a3e 2019-02-22 stsp int errcode;
2383 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2385 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2386 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2387 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2388 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2389 7aafa0d1 2019-02-22 stsp if (errcode)
2390 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2391 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2394 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2395 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2397 7c1452c1 2020-03-26 stsp if (!wait)
2400 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2401 ffe38506 2020-12-01 naddy show_log_view(view);
2402 7c1452c1 2020-03-26 stsp update_panels();
2403 7c1452c1 2020-03-26 stsp doupdate();
2405 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2406 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2407 82954512 2020-02-03 stsp if (errcode)
2408 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2409 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2411 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2412 ffe38506 2020-12-01 naddy show_log_view(view);
2413 7c1452c1 2020-03-26 stsp update_panels();
2414 7c1452c1 2020-03-26 stsp doupdate();
2417 5e224a3e 2019-02-22 stsp return NULL;
2420 5e224a3e 2019-02-22 stsp static const struct got_error *
2421 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2423 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2424 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2426 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2427 2525dccb 2022-07-13 mark return NULL;
2429 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2430 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2431 9b058f45 2022-06-30 mark view->nscrolled = 0;
2433 9b058f45 2022-06-30 mark return err;
2436 9b058f45 2022-06-30 mark static const struct got_error *
2437 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2439 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2440 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2441 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2442 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2444 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2445 5e224a3e 2019-02-22 stsp return NULL;
2447 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2448 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2449 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2451 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2453 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2454 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2456 5e224a3e 2019-02-22 stsp return err;
2460 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2461 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2464 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2465 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2467 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2468 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2470 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2471 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2473 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2474 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2476 9b058f45 2022-06-30 mark view->nscrolled = 0;
2478 dd0a52c1 2018-05-20 stsp return err;
2481 cd0acaa7 2018-05-20 stsp static const struct got_error *
2482 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2483 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2484 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2486 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2487 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2488 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2490 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2491 15a94983 2018-12-23 stsp if (diff_view == NULL)
2492 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2494 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2495 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2496 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2497 e5a0f69f 2018-08-18 stsp if (err == NULL)
2498 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2499 cd0acaa7 2018-05-20 stsp return err;
2502 80ddbec8 2018-04-29 stsp static const struct got_error *
2503 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2504 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2506 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2508 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2509 941e9f74 2019-05-21 stsp if (parent == NULL)
2510 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2512 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2513 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2514 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2515 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2516 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2517 941e9f74 2019-05-21 stsp s->tree = subtree;
2518 941e9f74 2019-05-21 stsp s->selected = 0;
2519 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2520 941e9f74 2019-05-21 stsp return NULL;
2523 941e9f74 2019-05-21 stsp static const struct got_error *
2524 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2525 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2527 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2528 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2529 941e9f74 2019-05-21 stsp const char *p;
2530 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2532 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2534 941e9f74 2019-05-21 stsp while (*p) {
2535 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2536 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2537 56e0773d 2019-11-28 stsp char *te_name;
2539 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2542 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2543 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2544 941e9f74 2019-05-21 stsp if (slash == NULL)
2545 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2547 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2548 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2549 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2552 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2553 56e0773d 2019-11-28 stsp if (te == NULL) {
2554 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2555 56e0773d 2019-11-28 stsp free(te_name);
2558 56e0773d 2019-11-28 stsp free(te_name);
2559 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2561 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2562 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2564 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2565 941e9f74 2019-05-21 stsp if (slash)
2566 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2568 941e9f74 2019-05-21 stsp subpath = strdup(path);
2569 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2570 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2574 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2579 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2580 941e9f74 2019-05-21 stsp free(tree_id);
2584 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2585 941e9f74 2019-05-21 stsp if (err) {
2586 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2589 941e9f74 2019-05-21 stsp if (slash == NULL)
2591 941e9f74 2019-05-21 stsp free(subpath);
2592 941e9f74 2019-05-21 stsp subpath = NULL;
2593 941e9f74 2019-05-21 stsp p = slash;
2596 941e9f74 2019-05-21 stsp free(subpath);
2597 1a76625f 2018-10-22 stsp return err;
2600 61266923 2020-01-14 stsp static const struct got_error *
2601 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2602 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2603 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2605 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2606 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2607 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2609 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2610 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2611 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2613 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2615 55cccc34 2020-02-20 stsp return err;
2616 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2618 55cccc34 2020-02-20 stsp *new_view = tree_view;
2620 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2621 55cccc34 2020-02-20 stsp return NULL;
2623 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2626 55cccc34 2020-02-20 stsp static const struct got_error *
2627 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2629 61266923 2020-01-14 stsp sigset_t sigset;
2630 61266923 2020-01-14 stsp int errcode;
2632 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2633 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2635 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2636 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2637 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2638 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2639 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2640 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2641 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2642 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2643 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2645 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2646 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2647 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2649 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2650 61266923 2020-01-14 stsp if (errcode)
2651 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2653 61266923 2020-01-14 stsp return NULL;
2656 1a76625f 2018-10-22 stsp static void *
2657 1a76625f 2018-10-22 stsp log_thread(void *arg)
2659 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2660 1a76625f 2018-10-22 stsp int errcode = 0;
2661 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2662 1a76625f 2018-10-22 stsp int done = 0;
2665 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2666 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2668 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2669 5629093a 2022-07-11 stsp if (errcode) {
2670 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2671 61266923 2020-01-14 stsp return (void *)err;
2674 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2675 5629093a 2022-07-11 stsp if (err) {
2676 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2677 5629093a 2022-07-11 stsp goto done;
2680 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2681 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2682 5629093a 2022-07-11 stsp if (errcode) {
2683 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2684 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2685 5629093a 2022-07-11 stsp goto done;
2687 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2688 1a76625f 2018-10-22 stsp if (err) {
2689 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2690 5629093a 2022-07-11 stsp goto done;
2691 1a76625f 2018-10-22 stsp err = NULL;
2693 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2694 1a76625f 2018-10-22 stsp a->commits_needed--;
2696 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2697 3abe8080 2019-04-10 stsp if (errcode) {
2698 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2699 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2700 5629093a 2022-07-11 stsp goto done;
2701 3abe8080 2019-04-10 stsp } else if (*a->quit)
2703 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2704 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2705 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2706 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2709 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2710 7c1452c1 2020-03-26 stsp if (errcode) {
2711 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2712 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2713 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2714 5629093a 2022-07-11 stsp goto done;
2718 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2720 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2721 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2722 7c1452c1 2020-03-26 stsp &tog_mutex);
2723 5629093a 2022-07-11 stsp if (errcode) {
2724 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2725 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2726 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2727 5629093a 2022-07-11 stsp goto done;
2729 21355643 2020-12-06 stsp if (*a->quit)
2734 3abe8080 2019-04-10 stsp a->log_complete = 1;
2735 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2736 5629093a 2022-07-11 stsp if (errcode)
2737 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2739 5629093a 2022-07-11 stsp if (err) {
2740 5629093a 2022-07-11 stsp tog_thread_error = 1;
2741 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2743 1a76625f 2018-10-22 stsp return (void *)err;
2746 1a76625f 2018-10-22 stsp static const struct got_error *
2747 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2749 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2750 1a76625f 2018-10-22 stsp int errcode;
2752 1a76625f 2018-10-22 stsp if (s->thread) {
2753 1a76625f 2018-10-22 stsp s->quit = 1;
2754 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2755 1a76625f 2018-10-22 stsp if (errcode)
2756 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2757 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2758 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2759 1a76625f 2018-10-22 stsp if (errcode)
2760 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2761 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2762 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2763 1a76625f 2018-10-22 stsp if (errcode)
2764 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2765 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2766 1a76625f 2018-10-22 stsp if (errcode)
2767 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2768 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2769 1a76625f 2018-10-22 stsp s->thread = NULL;
2772 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2773 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2774 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2777 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2778 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2779 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2780 74467cc8 2022-06-15 stsp if (err == NULL)
2781 74467cc8 2022-06-15 stsp err = pack_err;
2782 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2785 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2786 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2787 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2790 5629093a 2022-07-11 stsp return err ? err : thread_err;
2793 9343a5fb 2018-06-23 stsp static const struct got_error *
2794 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2796 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2797 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2798 276b94a1 2020-11-13 naddy int errcode;
2800 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2802 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2803 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2804 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2806 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2807 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2808 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2810 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2811 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2812 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2813 1a76625f 2018-10-22 stsp free(s->start_id);
2814 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2815 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2816 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2817 1a76625f 2018-10-22 stsp return err;
2820 1a76625f 2018-10-22 stsp static const struct got_error *
2821 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2823 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2825 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2826 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2827 60493ae3 2019-06-20 stsp return NULL;
2830 60493ae3 2019-06-20 stsp static const struct got_error *
2831 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2833 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2834 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2835 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2837 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2838 f9686aa5 2020-03-27 stsp show_log_view(view);
2839 f9686aa5 2020-03-27 stsp update_panels();
2840 f9686aa5 2020-03-27 stsp doupdate();
2842 96e2b566 2019-07-08 stsp if (s->search_entry) {
2843 13add988 2019-10-15 stsp int errcode, ch;
2844 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2845 13add988 2019-10-15 stsp if (errcode)
2846 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2847 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2848 13add988 2019-10-15 stsp ch = wgetch(view->window);
2849 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2850 13add988 2019-10-15 stsp if (errcode)
2851 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2852 13add988 2019-10-15 stsp "pthread_mutex_lock");
2853 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2854 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2855 678cbce5 2019-07-28 stsp return NULL;
2857 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2858 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2860 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2861 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2862 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2863 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2864 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2867 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2868 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2869 f704b35c 2022-06-18 stsp * might have changed.
2871 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2872 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2873 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2875 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2877 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2878 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2879 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2881 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2882 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2885 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2888 60493ae3 2019-06-20 stsp while (1) {
2889 13add988 2019-10-15 stsp int have_match = 0;
2891 60493ae3 2019-06-20 stsp if (entry == NULL) {
2892 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2893 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2894 f9967bca 2020-03-27 stsp view->search_next_done =
2895 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2896 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2897 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2898 f801134a 2019-06-25 stsp return NULL;
2901 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2902 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2903 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2905 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2906 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2909 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2910 13add988 2019-10-15 stsp &view->regex);
2913 13add988 2019-10-15 stsp if (have_match) {
2914 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2915 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2919 96e2b566 2019-07-08 stsp s->search_entry = entry;
2920 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2921 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2923 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2926 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2927 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2928 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2929 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2931 60493ae3 2019-06-20 stsp return err;
2934 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2935 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2937 60493ae3 2019-06-20 stsp return err;
2942 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2944 60493ae3 2019-06-20 stsp return NULL;
2947 60493ae3 2019-06-20 stsp static const struct got_error *
2948 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2949 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2950 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2952 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2953 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2954 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2955 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2956 1a76625f 2018-10-22 stsp int errcode;
2958 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2959 f135c941 2020-02-20 stsp free(s->in_repo_path);
2960 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2961 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2962 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2965 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2966 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2967 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2969 fb2756b9 2018-08-04 stsp s->repo = repo;
2970 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2971 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2972 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2973 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2974 9cd7cbd1 2020-12-07 stsp goto done;
2977 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2978 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2979 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2980 5036bf37 2018-09-24 stsp goto done;
2982 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2984 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2985 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2986 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2987 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2989 11b20872 2019-11-08 stsp goto done;
2990 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2991 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2992 11b20872 2019-11-08 stsp if (err) {
2993 11b20872 2019-11-08 stsp free_colors(&s->colors);
2994 11b20872 2019-11-08 stsp goto done;
2996 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2997 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2998 11b20872 2019-11-08 stsp if (err) {
2999 11b20872 2019-11-08 stsp free_colors(&s->colors);
3000 11b20872 2019-11-08 stsp goto done;
3004 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3005 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3006 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3007 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3008 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3009 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3011 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3012 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3014 74467cc8 2022-06-15 stsp goto done;
3016 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3017 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3018 0ae84acc 2022-06-15 tracey if (err)
3019 0ae84acc 2022-06-15 tracey goto done;
3020 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3021 b672a97a 2020-01-27 stsp !s->log_branches);
3023 1a76625f 2018-10-22 stsp goto done;
3024 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3025 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3027 c5b78334 2020-01-12 stsp goto done;
3029 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3030 1a76625f 2018-10-22 stsp if (errcode) {
3031 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3032 1a76625f 2018-10-22 stsp goto done;
3034 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3035 7c1452c1 2020-03-26 stsp if (errcode) {
3036 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3037 7c1452c1 2020-03-26 stsp goto done;
3040 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3041 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3042 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3043 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3044 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3045 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3046 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3047 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3048 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3049 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3050 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3051 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3052 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3055 1a76625f 2018-10-22 stsp close_log_view(view);
3056 ba4f502b 2018-08-04 stsp return err;
3059 e5a0f69f 2018-08-18 stsp static const struct got_error *
3060 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3062 f2f6d207 2020-11-24 stsp const struct got_error *err;
3063 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3065 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3066 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3067 2b380cc8 2018-10-24 stsp &s->thread_args);
3068 2b380cc8 2018-10-24 stsp if (errcode)
3069 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3070 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3071 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3073 f2f6d207 2020-11-24 stsp return err;
3077 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3080 b880cc75 2022-06-30 mark static void
3081 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3083 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3085 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3086 b880cc75 2022-06-30 mark view->count = 0;
3087 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3090 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3092 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3094 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3095 b880cc75 2022-06-30 mark --s->selected;
3097 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3099 b880cc75 2022-06-30 mark select_commit(s);
3103 b880cc75 2022-06-30 mark static const struct got_error *
3104 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3106 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3107 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
3108 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3110 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
3111 b880cc75 2022-06-30 mark if (first == NULL) {
3112 b880cc75 2022-06-30 mark view->count = 0;
3113 b880cc75 2022-06-30 mark return NULL;
3116 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3117 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3118 b880cc75 2022-06-30 mark return NULL;
3120 b880cc75 2022-06-30 mark if (!page) {
3121 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3123 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3124 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3125 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3126 b880cc75 2022-06-30 mark ++s->selected;
3128 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3129 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3130 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3131 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3132 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3134 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3135 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3136 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3138 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
3140 b880cc75 2022-06-30 mark return err;
3141 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
3142 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3143 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
3147 b880cc75 2022-06-30 mark return err;
3150 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3151 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3153 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3154 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3156 b880cc75 2022-06-30 mark select_commit(s);
3158 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3159 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3160 b880cc75 2022-06-30 mark view->count = 0;
3162 b880cc75 2022-06-30 mark return NULL;
3165 9b058f45 2022-06-30 mark static void
3166 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3171 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3172 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3173 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3174 7532ccda 2022-07-11 mark else if (view->resized_y)
3175 7532ccda 2022-07-11 mark *y = view->resized_y;
3177 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3178 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3179 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3180 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3181 7532ccda 2022-07-11 mark else if (view->resized_x)
3182 7532ccda 2022-07-11 mark *x = view->resized_x;
3184 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3188 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3189 e5a0f69f 2018-08-18 stsp static const struct got_error *
3190 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3192 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3194 9b058f45 2022-06-30 mark view->nlines = y;
3195 d2366e29 2022-07-07 mark view->ncols = COLS;
3196 9b058f45 2022-06-30 mark err = view_resize(view);
3198 9b058f45 2022-06-30 mark return err;
3200 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3202 9b058f45 2022-06-30 mark return err;
3205 9b058f45 2022-06-30 mark static const struct got_error *
3206 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3208 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3209 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3210 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3211 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3212 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3213 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
3215 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3216 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3217 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3218 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3219 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3220 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3222 b880cc75 2022-06-30 mark return err;
3225 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3226 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3227 0dca135e 2022-06-30 mark --eos; /* border */
3229 528dedf3 2021-08-30 stsp switch (ch) {
3231 1e37a5c2 2019-05-12 jcs s->quit = 1;
3234 145b6838 2022-06-16 stsp view->x = 0;
3237 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3238 640cd7ff 2022-06-22 mark view->count = 0;
3240 145b6838 2022-06-16 stsp case KEY_RIGHT:
3242 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3243 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3245 640cd7ff 2022-06-22 mark view->count = 0;
3247 145b6838 2022-06-16 stsp case KEY_LEFT:
3249 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3250 640cd7ff 2022-06-22 mark if (view->x <= 0)
3251 640cd7ff 2022-06-22 mark view->count = 0;
3254 1e37a5c2 2019-05-12 jcs case KEY_UP:
3257 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3258 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3261 912a3f79 2021-08-30 j case KEY_HOME:
3262 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3263 640cd7ff 2022-06-22 mark view->count = 0;
3265 83cc4199 2022-06-13 stsp case CTRL('u'):
3267 83cc4199 2022-06-13 stsp nscroll /= 2;
3268 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3269 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3270 a4292ac5 2019-05-12 jcs case CTRL('b'):
3272 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3275 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3278 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3279 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3282 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3285 912a3f79 2021-08-30 j case KEY_END: {
3286 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3287 912a3f79 2021-08-30 j * traverse them all. */
3288 640cd7ff 2022-06-22 mark view->count = 0;
3289 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3290 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3291 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3294 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3295 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3296 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3297 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3299 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3300 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3302 f3bc9f1d 2021-09-05 naddy if (n > 0)
3303 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3304 2b779855 2020-12-05 naddy select_commit(s);
3307 80b7e8da 2022-06-11 stsp case CTRL('d'):
3309 83cc4199 2022-06-13 stsp nscroll /= 2;
3310 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3311 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3312 61417565 2022-06-20 mark case CTRL('f'):
3313 48bb96f0 2022-06-20 naddy case 'f':
3315 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3317 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3318 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3319 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3320 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3321 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3322 2b779855 2020-12-05 naddy select_commit(s);
3323 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3324 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3325 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3326 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3327 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3330 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3331 49b24ee5 2022-07-03 mark case '\r':
3332 640cd7ff 2022-06-22 mark view->count = 0;
3333 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3336 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3337 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3338 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3340 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3341 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3342 78756c87 2020-11-24 stsp view, s->repo);
3346 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3347 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3348 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3353 e78dc838 2020-12-04 stsp view->focussed = 0;
3354 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3355 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3356 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3358 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3359 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
3360 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3362 1e37a5c2 2019-05-12 jcs return err;
3363 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3366 e78dc838 2020-12-04 stsp view->focus_child = 1;
3368 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3371 640cd7ff 2022-06-22 mark view->count = 0;
3372 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3374 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3375 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3376 49b24ee5 2022-07-03 mark err = browse_commit_tree(&tree_view, begin_y, begin_x,
3377 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3381 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3382 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3383 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3387 e78dc838 2020-12-04 stsp view->focussed = 0;
3388 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3389 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
3390 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
3391 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3392 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
3393 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3395 1e37a5c2 2019-05-12 jcs return err;
3396 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3399 e78dc838 2020-12-04 stsp view->focus_child = 1;
3401 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3403 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3404 21355643 2020-12-06 stsp case CTRL('l'):
3406 640cd7ff 2022-06-22 mark view->count = 0;
3407 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3408 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3410 21355643 2020-12-06 stsp err = stop_log_thread(s);
3412 74cfe85e 2020-10-20 stsp return err;
3413 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3414 21355643 2020-12-06 stsp char *parent_path;
3415 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3417 21355643 2020-12-06 stsp return err;
3418 21355643 2020-12-06 stsp free(s->in_repo_path);
3419 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3420 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3421 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3422 21355643 2020-12-06 stsp struct got_object_id *start_id;
3423 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3424 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3425 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3427 21355643 2020-12-06 stsp return err;
3428 21355643 2020-12-06 stsp free(s->start_id);
3429 21355643 2020-12-06 stsp s->start_id = start_id;
3430 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3431 21355643 2020-12-06 stsp } else /* 'B' */
3432 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3434 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3435 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3437 b0dd8d27 2022-06-16 stsp return err;
3439 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3440 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3441 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3443 21355643 2020-12-06 stsp return err;
3444 51a10b52 2020-12-26 stsp tog_free_refs();
3445 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3447 ca51c541 2020-12-07 stsp return err;
3448 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3449 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3451 d01904d4 2019-06-25 stsp return err;
3452 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3453 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3455 b672a97a 2020-01-27 stsp return err;
3456 21355643 2020-12-06 stsp free_commits(&s->commits);
3457 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3458 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3459 21355643 2020-12-06 stsp s->selected_entry = NULL;
3460 21355643 2020-12-06 stsp s->selected = 0;
3461 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3462 21355643 2020-12-06 stsp s->quit = 0;
3463 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3464 dfee752e 2022-06-18 op s->matched_entry = NULL;
3465 dfee752e 2022-06-18 op s->search_entry = NULL;
3468 640cd7ff 2022-06-22 mark view->count = 0;
3469 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3470 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3471 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3472 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3473 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3474 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3475 6458efa5 2020-11-24 stsp if (err) {
3476 6458efa5 2020-11-24 stsp view_close(ref_view);
3477 6458efa5 2020-11-24 stsp return err;
3479 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3480 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3481 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3485 e78dc838 2020-12-04 stsp view->focussed = 0;
3486 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3487 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
3488 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
3489 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3490 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
3491 6458efa5 2020-11-24 stsp err = view_close_child(view);
3493 6458efa5 2020-11-24 stsp return err;
3494 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3497 e78dc838 2020-12-04 stsp view->focus_child = 1;
3499 6458efa5 2020-11-24 stsp *new_view = ref_view;
3502 640cd7ff 2022-06-22 mark view->count = 0;
3506 80ddbec8 2018-04-29 stsp return err;
3509 4ed7e80c 2018-05-20 stsp static const struct got_error *
3510 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3512 c2db6724 2019-01-04 stsp const struct got_error *error;
3514 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3515 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3516 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3518 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3519 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3521 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3522 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3524 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3525 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3527 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3528 c2db6724 2019-01-04 stsp if (error != NULL)
3529 c2db6724 2019-01-04 stsp return error;
3531 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3532 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3534 c2db6724 2019-01-04 stsp return NULL;
3537 a915003a 2019-02-05 stsp static void
3538 a915003a 2019-02-05 stsp init_curses(void)
3541 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3542 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3543 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3545 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3546 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3547 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3548 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3549 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3551 a915003a 2019-02-05 stsp initscr();
3553 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3556 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3557 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3558 a915003a 2019-02-05 stsp curs_set(0);
3559 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3560 6d17833f 2019-11-08 stsp start_color();
3561 6d17833f 2019-11-08 stsp use_default_colors();
3565 c2db6724 2019-01-04 stsp static const struct got_error *
3566 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3567 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3569 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3571 f135c941 2020-02-20 stsp if (argc == 0) {
3572 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3573 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3574 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3575 f135c941 2020-02-20 stsp return NULL;
3578 f135c941 2020-02-20 stsp if (worktree) {
3579 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3582 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3584 f135c941 2020-02-20 stsp return err;
3585 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3586 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3587 bfd61697 2020-11-14 stsp p) == -1) {
3588 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3589 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3593 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3595 f135c941 2020-02-20 stsp return err;
3598 f135c941 2020-02-20 stsp static const struct got_error *
3599 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3601 80ddbec8 2018-04-29 stsp const struct got_error *error;
3602 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3603 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3604 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3605 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3606 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3607 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3608 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3609 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3610 04cc582a 2018-08-01 stsp struct tog_view *view;
3611 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3613 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3614 80ddbec8 2018-04-29 stsp switch (ch) {
3616 b672a97a 2020-01-27 stsp log_branches = 1;
3619 80ddbec8 2018-04-29 stsp start_commit = optarg;
3622 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3623 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3624 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3628 17020d27 2019-03-07 stsp usage_log();
3629 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3633 80ddbec8 2018-04-29 stsp argc -= optind;
3634 80ddbec8 2018-04-29 stsp argv += optind;
3636 f135c941 2020-02-20 stsp if (argc > 1)
3637 f135c941 2020-02-20 stsp usage_log();
3639 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3640 0ae84acc 2022-06-15 tracey if (error != NULL)
3641 0ae84acc 2022-06-15 tracey goto done;
3643 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3644 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3645 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3646 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3647 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3648 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3649 c156c7a4 2020-12-18 stsp goto done;
3650 a1fbf39a 2019-08-11 stsp if (worktree)
3651 6962eb72 2020-02-20 stsp repo_path =
3652 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3654 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3655 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3656 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3657 c156c7a4 2020-12-18 stsp goto done;
3661 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3662 80ddbec8 2018-04-29 stsp if (error != NULL)
3663 ecb28ae0 2018-07-16 stsp goto done;
3665 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3666 f135c941 2020-02-20 stsp repo, worktree);
3667 f135c941 2020-02-20 stsp if (error)
3668 f135c941 2020-02-20 stsp goto done;
3670 f135c941 2020-02-20 stsp init_curses();
3672 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3673 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3674 c02c541e 2019-03-29 stsp if (error)
3675 c02c541e 2019-03-29 stsp goto done;
3677 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3678 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3679 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3680 87670572 2020-12-26 naddy if (error)
3681 87670572 2020-12-26 naddy goto done;
3684 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3685 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3686 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3687 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3688 d8f38dc4 2020-12-05 stsp if (error)
3689 d8f38dc4 2020-12-05 stsp goto done;
3690 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3692 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3693 d8f38dc4 2020-12-05 stsp if (error == NULL)
3694 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3695 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3696 d8f38dc4 2020-12-05 stsp goto done;
3697 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3698 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3699 d8f38dc4 2020-12-05 stsp if (error)
3700 d8f38dc4 2020-12-05 stsp goto done;
3703 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3704 04cc582a 2018-08-01 stsp if (view == NULL) {
3705 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3706 04cc582a 2018-08-01 stsp goto done;
3708 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3709 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3710 ba4f502b 2018-08-04 stsp if (error)
3711 ba4f502b 2018-08-04 stsp goto done;
3712 2fc00ff4 2019-08-31 stsp if (worktree) {
3713 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3714 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3715 2fc00ff4 2019-08-31 stsp worktree = NULL;
3717 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3719 f135c941 2020-02-20 stsp free(in_repo_path);
3720 ecb28ae0 2018-07-16 stsp free(repo_path);
3721 ecb28ae0 2018-07-16 stsp free(cwd);
3722 899d86c2 2018-05-10 stsp free(start_id);
3723 d8f38dc4 2020-12-05 stsp free(label);
3725 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3726 1d0f4054 2021-06-17 stsp if (repo) {
3727 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3728 1d0f4054 2021-06-17 stsp if (error == NULL)
3729 1d0f4054 2021-06-17 stsp error = close_err;
3731 ec142235 2019-03-07 stsp if (worktree)
3732 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3733 0ae84acc 2022-06-15 tracey if (pack_fds) {
3734 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3735 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3736 0ae84acc 2022-06-15 tracey if (error == NULL)
3737 0ae84acc 2022-06-15 tracey error = pack_err;
3739 51a10b52 2020-12-26 stsp tog_free_refs();
3740 80ddbec8 2018-04-29 stsp return error;
3743 4ed7e80c 2018-05-20 stsp __dead static void
3744 9f7d7167 2018-04-29 stsp usage_diff(void)
3747 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3748 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3752 6d17833f 2019-11-08 stsp static int
3753 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3754 41605754 2020-11-12 stsp regmatch_t *regmatch)
3756 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3759 336075a4 2022-06-25 op static struct tog_color *
3760 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3762 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3764 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3765 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3766 6d17833f 2019-11-08 stsp return tc;
3769 6d17833f 2019-11-08 stsp return NULL;
3772 4ed7e80c 2018-05-20 stsp static const struct got_error *
3773 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3774 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3776 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3777 44a87665 2022-06-16 stsp char *exstr = NULL;
3778 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3779 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3780 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3781 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3783 41605754 2020-11-12 stsp *wtotal = 0;
3785 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3786 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3788 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3790 44a87665 2022-06-16 stsp return err;
3792 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3793 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3794 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3795 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3796 44a87665 2022-06-16 stsp goto done;
3798 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3799 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3800 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3801 1853e0f4 2022-06-16 stsp goto done;
3803 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3804 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3805 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3806 1853e0f4 2022-06-16 stsp goto done;
3809 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3810 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3811 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3813 1853e0f4 2022-06-16 stsp goto done;
3814 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3816 1853e0f4 2022-06-16 stsp free(wline);
3817 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3818 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3820 1853e0f4 2022-06-16 stsp goto done;
3821 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3822 1853e0f4 2022-06-16 stsp wlimit -= width;
3823 1853e0f4 2022-06-16 stsp *wtotal += width;
3826 41605754 2020-11-12 stsp if (wlimit > 0) {
3827 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3828 1853e0f4 2022-06-16 stsp size_t wlen;
3830 1853e0f4 2022-06-16 stsp free(wline);
3831 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3832 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3834 1853e0f4 2022-06-16 stsp goto done;
3835 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3836 1853e0f4 2022-06-16 stsp while (i < wlen) {
3837 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3838 1853e0f4 2022-06-16 stsp if (width == -1) {
3839 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3840 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3841 1853e0f4 2022-06-16 stsp goto done;
3843 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3845 61417565 2022-06-20 mark w += width;
3848 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3849 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3850 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3851 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3852 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3853 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3854 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3858 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3859 1853e0f4 2022-06-16 stsp free(wline);
3860 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3861 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3862 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3863 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3865 1853e0f4 2022-06-16 stsp goto done;
3866 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3868 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3869 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3871 1853e0f4 2022-06-16 stsp goto done;
3872 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3874 1853e0f4 2022-06-16 stsp *wtotal += width2;
3877 145b6838 2022-06-16 stsp free(wline);
3878 44a87665 2022-06-16 stsp free(exstr);
3879 1853e0f4 2022-06-16 stsp free(seg0);
3880 1853e0f4 2022-06-16 stsp free(seg1);
3881 1853e0f4 2022-06-16 stsp free(seg2);
3882 1853e0f4 2022-06-16 stsp return err;
3885 41605754 2020-11-12 stsp static const struct got_error *
3886 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3888 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3889 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3890 61e69b96 2018-05-20 stsp const struct got_error *err;
3891 fe621944 2020-11-10 stsp int nprinted = 0;
3892 b304db33 2018-05-20 stsp char *line;
3893 826082fe 2020-12-10 stsp size_t linesize = 0;
3894 826082fe 2020-12-10 stsp ssize_t linelen;
3895 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3896 61e69b96 2018-05-20 stsp wchar_t *wline;
3897 e0b650dd 2018-05-20 stsp int width;
3898 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3899 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3900 fe621944 2020-11-10 stsp off_t line_offset;
3902 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3903 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3904 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3906 f7d12f7e 2018-08-01 stsp werase(view->window);
3908 a3404814 2018-09-02 stsp if (header) {
3909 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3910 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3911 135a2da0 2020-11-11 stsp header) == -1)
3912 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3913 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3915 135a2da0 2020-11-11 stsp free(line);
3917 a3404814 2018-09-02 stsp return err;
3919 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3920 a3404814 2018-09-02 stsp wstandout(view->window);
3921 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3922 e54cc94a 2020-11-11 stsp free(wline);
3923 e54cc94a 2020-11-11 stsp wline = NULL;
3924 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3925 a3404814 2018-09-02 stsp wstandend(view->window);
3926 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3927 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3929 a3404814 2018-09-02 stsp if (max_lines <= 1)
3930 a3404814 2018-09-02 stsp return NULL;
3931 a3404814 2018-09-02 stsp max_lines--;
3934 89f1a395 2020-12-01 naddy s->eof = 0;
3935 145b6838 2022-06-16 stsp view->maxx = 0;
3936 826082fe 2020-12-10 stsp line = NULL;
3937 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3938 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3939 826082fe 2020-12-10 stsp if (linelen == -1) {
3940 826082fe 2020-12-10 stsp if (feof(s->f)) {
3941 826082fe 2020-12-10 stsp s->eof = 1;
3944 826082fe 2020-12-10 stsp free(line);
3945 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3948 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3949 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3950 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3951 1853e0f4 2022-06-16 stsp if (err) {
3952 1853e0f4 2022-06-16 stsp free(line);
3953 1853e0f4 2022-06-16 stsp return err;
3955 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3956 1853e0f4 2022-06-16 stsp free(wline);
3957 1853e0f4 2022-06-16 stsp wline = NULL;
3959 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3961 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3962 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3963 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3964 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3965 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3966 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3967 41605754 2020-11-12 stsp if (err) {
3968 41605754 2020-11-12 stsp free(line);
3969 41605754 2020-11-12 stsp return err;
3973 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3974 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3975 969c159c 2022-06-16 stsp if (err) {
3976 969c159c 2022-06-16 stsp free(line);
3977 969c159c 2022-06-16 stsp return err;
3979 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3980 969c159c 2022-06-16 stsp free(wline);
3981 41605754 2020-11-12 stsp wline = NULL;
3984 6d17833f 2019-11-08 stsp wattr_off(view->window,
3985 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3986 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3987 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3988 fe621944 2020-11-10 stsp nprinted++;
3990 826082fe 2020-12-10 stsp free(line);
3991 fe621944 2020-11-10 stsp if (nprinted >= 1)
3992 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3993 89f1a395 2020-12-01 naddy (nprinted - 1);
3995 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3997 9b058f45 2022-06-30 mark view_border(view);
3999 89f1a395 2020-12-01 naddy if (s->eof) {
4000 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4001 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4002 c3e9aa98 2019-05-13 jcs nprinted++;
4005 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4006 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4008 c3e9aa98 2019-05-13 jcs return err;
4011 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4012 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4013 e54cc94a 2020-11-11 stsp free(wline);
4014 e54cc94a 2020-11-11 stsp wline = NULL;
4015 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4018 26ed57b2 2018-05-19 stsp return NULL;
4021 abd2672a 2018-12-23 stsp static char *
4022 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4024 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4025 09867e48 2019-08-13 stsp char *p, *s;
4027 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4028 09867e48 2019-08-13 stsp if (tm == NULL)
4029 09867e48 2019-08-13 stsp return NULL;
4030 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4031 09867e48 2019-08-13 stsp if (s == NULL)
4032 09867e48 2019-08-13 stsp return NULL;
4033 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4035 abd2672a 2018-12-23 stsp *p = '\0';
4039 4ed7e80c 2018-05-20 stsp static const struct got_error *
4040 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4041 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4043 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4044 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4045 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4046 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4048 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4049 0208f208 2020-05-05 stsp if (qid != NULL) {
4050 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4051 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4052 d7b5a0e8 2022-04-20 stsp &qid->id);
4054 0208f208 2020-05-05 stsp return err;
4056 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4057 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4058 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4059 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4060 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4062 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4066 0208f208 2020-05-05 stsp if (tree_id1) {
4067 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4069 0208f208 2020-05-05 stsp goto done;
4072 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4073 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4075 0208f208 2020-05-05 stsp goto done;
4077 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4078 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4080 0208f208 2020-05-05 stsp if (tree1)
4081 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4082 0208f208 2020-05-05 stsp if (tree2)
4083 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4084 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4085 0208f208 2020-05-05 stsp return err;
4088 0208f208 2020-05-05 stsp static const struct got_error *
4089 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4093 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4094 fe621944 2020-11-10 stsp if (p == NULL)
4095 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4096 fe621944 2020-11-10 stsp *line_offsets = p;
4097 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4098 fe621944 2020-11-10 stsp (*nlines)++;
4099 fe621944 2020-11-10 stsp return NULL;
4102 fe621944 2020-11-10 stsp static const struct got_error *
4103 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4104 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4105 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4107 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4108 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4109 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4110 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4111 45d799e2 2018-12-23 stsp time_t committer_time;
4112 45d799e2 2018-12-23 stsp const char *author, *committer;
4113 8b473291 2019-02-21 stsp char *refs_str = NULL;
4114 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4115 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4116 fe621944 2020-11-10 stsp off_t outoff = 0;
4119 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4121 8b473291 2019-02-21 stsp if (refs) {
4122 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4124 8b473291 2019-02-21 stsp return err;
4127 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4129 abd2672a 2018-12-23 stsp return err;
4131 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4132 15a94983 2018-12-23 stsp if (err) {
4133 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4134 15a94983 2018-12-23 stsp goto done;
4137 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4139 fe621944 2020-11-10 stsp goto done;
4141 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4142 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4143 fe621944 2020-11-10 stsp if (n < 0) {
4144 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4145 abd2672a 2018-12-23 stsp goto done;
4147 fe621944 2020-11-10 stsp outoff += n;
4148 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4150 fe621944 2020-11-10 stsp goto done;
4152 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4153 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4154 fe621944 2020-11-10 stsp if (n < 0) {
4155 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4156 abd2672a 2018-12-23 stsp goto done;
4158 fe621944 2020-11-10 stsp outoff += n;
4159 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4161 fe621944 2020-11-10 stsp goto done;
4163 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4164 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4165 fe621944 2020-11-10 stsp if (datestr) {
4166 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4167 fe621944 2020-11-10 stsp if (n < 0) {
4168 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4169 fe621944 2020-11-10 stsp goto done;
4171 fe621944 2020-11-10 stsp outoff += n;
4172 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4174 fe621944 2020-11-10 stsp goto done;
4176 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4177 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4178 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4179 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4180 fe621944 2020-11-10 stsp if (n < 0) {
4181 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4182 fe621944 2020-11-10 stsp goto done;
4184 fe621944 2020-11-10 stsp outoff += n;
4185 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4187 fe621944 2020-11-10 stsp goto done;
4189 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4190 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4191 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4192 9f98ca05 2021-09-24 stsp int pn = 1;
4193 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4194 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4195 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4197 9f98ca05 2021-09-24 stsp goto done;
4198 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4199 9f98ca05 2021-09-24 stsp if (n < 0) {
4200 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4201 9f98ca05 2021-09-24 stsp goto done;
4203 9f98ca05 2021-09-24 stsp outoff += n;
4204 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
4206 9f98ca05 2021-09-24 stsp goto done;
4207 9f98ca05 2021-09-24 stsp free(id_str);
4208 9f98ca05 2021-09-24 stsp id_str = NULL;
4212 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4214 5943eee2 2019-08-13 stsp goto done;
4215 fe621944 2020-11-10 stsp s = logmsg;
4216 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4217 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4218 fe621944 2020-11-10 stsp if (n < 0) {
4219 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4220 fe621944 2020-11-10 stsp goto done;
4222 fe621944 2020-11-10 stsp outoff += n;
4223 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4225 fe621944 2020-11-10 stsp goto done;
4228 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4230 0208f208 2020-05-05 stsp goto done;
4231 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4232 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4233 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4234 fe621944 2020-11-10 stsp if (n < 0) {
4235 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4236 fe621944 2020-11-10 stsp goto done;
4238 fe621944 2020-11-10 stsp outoff += n;
4239 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4241 fe621944 2020-11-10 stsp goto done;
4242 0208f208 2020-05-05 stsp free((char *)pe->path);
4243 0208f208 2020-05-05 stsp free(pe->data);
4246 0208f208 2020-05-05 stsp fputc('\n', outfile);
4248 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4250 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4251 abd2672a 2018-12-23 stsp free(id_str);
4252 5943eee2 2019-08-13 stsp free(logmsg);
4253 8b473291 2019-02-21 stsp free(refs_str);
4254 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4255 7510f233 2020-08-09 stsp if (err) {
4256 ae6a6978 2020-08-09 stsp free(*line_offsets);
4257 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4258 ae6a6978 2020-08-09 stsp *nlines = 0;
4260 fe621944 2020-11-10 stsp return err;
4263 abd2672a 2018-12-23 stsp static const struct got_error *
4264 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4266 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4267 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4268 15a94983 2018-12-23 stsp int obj_type;
4270 fe621944 2020-11-10 stsp free(s->line_offsets);
4271 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4272 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4273 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4274 fe621944 2020-11-10 stsp s->nlines = 0;
4276 511a516b 2018-05-19 stsp f = got_opentemp();
4277 48ae06ee 2018-10-18 stsp if (f == NULL) {
4278 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4279 48ae06ee 2018-10-18 stsp goto done;
4281 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4282 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4283 fb43ecf1 2019-02-11 stsp goto done;
4287 15a94983 2018-12-23 stsp if (s->id1)
4288 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4290 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4292 15a94983 2018-12-23 stsp goto done;
4294 15a94983 2018-12-23 stsp switch (obj_type) {
4295 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4296 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4297 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4298 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4299 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4301 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4302 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4303 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4304 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4305 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4307 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4308 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4309 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4310 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4311 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4313 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4315 3ffacbe1 2020-02-02 tracey goto done;
4316 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4317 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4318 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4319 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4320 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4321 f44b1f58 2020-02-02 tracey if (err)
4322 f44b1f58 2020-02-02 tracey goto done;
4323 f44b1f58 2020-02-02 tracey } else {
4324 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4325 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4326 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4327 fe621944 2020-11-10 stsp err = write_commit_info(
4328 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4329 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4330 f44b1f58 2020-02-02 tracey if (err)
4331 f44b1f58 2020-02-02 tracey goto done;
4336 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4338 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4339 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4340 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4341 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4345 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4348 f44b1f58 2020-02-02 tracey if (err)
4349 f44b1f58 2020-02-02 tracey goto done;
4351 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4352 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4353 48ae06ee 2018-10-18 stsp return err;
4356 f5215bb9 2019-02-22 stsp static void
4357 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4359 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4360 f5215bb9 2019-02-22 stsp update_panels();
4361 f5215bb9 2019-02-22 stsp doupdate();
4364 f44b1f58 2020-02-02 tracey static const struct got_error *
4365 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4367 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4369 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4370 f44b1f58 2020-02-02 tracey return NULL;
4373 f44b1f58 2020-02-02 tracey static const struct got_error *
4374 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4376 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4377 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4378 f44b1f58 2020-02-02 tracey int lineno;
4379 cb713507 2022-06-16 stsp char *line = NULL;
4380 826082fe 2020-12-10 stsp size_t linesize = 0;
4381 826082fe 2020-12-10 stsp ssize_t linelen;
4383 f44b1f58 2020-02-02 tracey if (!view->searching) {
4384 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4385 f44b1f58 2020-02-02 tracey return NULL;
4388 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4389 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4390 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4392 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4394 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4396 f44b1f58 2020-02-02 tracey while (1) {
4397 f44b1f58 2020-02-02 tracey off_t offset;
4399 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4400 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4401 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4406 f44b1f58 2020-02-02 tracey lineno = 1;
4408 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4411 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4412 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4413 f44b1f58 2020-02-02 tracey free(line);
4414 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4416 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4417 cb713507 2022-06-16 stsp if (linelen != -1) {
4418 cb713507 2022-06-16 stsp char *exstr;
4419 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4422 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4423 cb713507 2022-06-16 stsp &view->regmatch)) {
4424 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4425 cb713507 2022-06-16 stsp s->matched_line = lineno;
4426 cb713507 2022-06-16 stsp free(exstr);
4429 cb713507 2022-06-16 stsp free(exstr);
4431 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4432 f44b1f58 2020-02-02 tracey lineno++;
4434 f44b1f58 2020-02-02 tracey lineno--;
4436 826082fe 2020-12-10 stsp free(line);
4438 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4439 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4440 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4443 145b6838 2022-06-16 stsp return err;
4446 48ae06ee 2018-10-18 stsp static const struct got_error *
4447 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4449 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4450 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4452 b72706c3 2022-06-01 stsp free(s->id1);
4453 b72706c3 2022-06-01 stsp s->id1 = NULL;
4454 b72706c3 2022-06-01 stsp free(s->id2);
4455 b72706c3 2022-06-01 stsp s->id2 = NULL;
4456 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4457 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4458 b72706c3 2022-06-01 stsp s->f = NULL;
4459 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4460 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4461 b72706c3 2022-06-01 stsp s->f1 = NULL;
4462 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4463 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4464 b72706c3 2022-06-01 stsp s->f2 = NULL;
4465 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4466 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4467 f9d37699 2022-06-28 stsp s->fd1 = -1;
4468 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4469 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4470 f9d37699 2022-06-28 stsp s->fd2 = -1;
4471 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4472 b72706c3 2022-06-01 stsp free(s->line_offsets);
4473 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4474 b72706c3 2022-06-01 stsp s->nlines = 0;
4475 b72706c3 2022-06-01 stsp return err;
4478 b72706c3 2022-06-01 stsp static const struct got_error *
4479 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4480 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4481 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4482 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4484 48ae06ee 2018-10-18 stsp const struct got_error *err;
4485 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4487 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4488 f9d37699 2022-06-28 stsp s->fd1 = -1;
4489 f9d37699 2022-06-28 stsp s->fd2 = -1;
4491 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4492 15a94983 2018-12-23 stsp int type1, type2;
4493 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4495 15a94983 2018-12-23 stsp return err;
4496 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4498 15a94983 2018-12-23 stsp return err;
4500 15a94983 2018-12-23 stsp if (type1 != type2)
4501 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4503 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4504 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4505 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4506 f44b1f58 2020-02-02 tracey s->repo = repo;
4507 f44b1f58 2020-02-02 tracey s->id1 = id1;
4508 f44b1f58 2020-02-02 tracey s->id2 = id2;
4509 3dbaef42 2020-11-24 stsp s->label1 = label1;
4510 3dbaef42 2020-11-24 stsp s->label2 = label2;
4512 15a94983 2018-12-23 stsp if (id1) {
4513 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4514 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4515 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4517 5465d566 2020-02-01 tracey s->id1 = NULL;
4519 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4520 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4521 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4522 b72706c3 2022-06-01 stsp goto done;
4525 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4526 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4527 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4528 a00719e9 2022-06-17 stsp goto done;
4531 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4532 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4533 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4534 b72706c3 2022-06-01 stsp goto done;
4537 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4538 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4539 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4540 f9d37699 2022-06-28 stsp goto done;
4543 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4544 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4545 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4546 f9d37699 2022-06-28 stsp goto done;
4549 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4550 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4551 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4552 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4553 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4554 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4555 5465d566 2020-02-01 tracey s->repo = repo;
4557 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4558 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4559 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4560 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4561 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4563 b72706c3 2022-06-01 stsp goto done;
4564 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4565 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4566 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4568 b72706c3 2022-06-01 stsp goto done;
4569 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4570 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4571 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4573 b72706c3 2022-06-01 stsp goto done;
4575 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4576 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4577 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4578 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4579 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4581 b72706c3 2022-06-01 stsp goto done;
4583 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4584 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4585 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4587 b72706c3 2022-06-01 stsp goto done;
4589 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4590 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4591 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4593 b72706c3 2022-06-01 stsp goto done;
4596 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4597 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4598 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4599 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4601 5465d566 2020-02-01 tracey err = create_diff(s);
4603 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4604 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4605 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4606 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4607 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4608 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4611 b72706c3 2022-06-01 stsp close_diff_view(view);
4612 e5a0f69f 2018-08-18 stsp return err;
4615 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4616 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4618 a3404814 2018-09-02 stsp const struct got_error *err;
4619 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4620 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4621 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4623 a3404814 2018-09-02 stsp if (s->id1) {
4624 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4626 a3404814 2018-09-02 stsp return err;
4627 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4629 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4631 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4633 a3404814 2018-09-02 stsp return err;
4634 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4636 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4637 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4638 a3404814 2018-09-02 stsp free(id_str1);
4639 a3404814 2018-09-02 stsp free(id_str2);
4640 a3404814 2018-09-02 stsp return err;
4642 a3404814 2018-09-02 stsp free(id_str1);
4643 a3404814 2018-09-02 stsp free(id_str2);
4645 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4646 267bb3b8 2021-08-01 stsp free(header);
4647 267bb3b8 2021-08-01 stsp return err;
4650 15a087fe 2019-02-21 stsp static const struct got_error *
4651 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4652 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4654 d7a04538 2019-02-21 stsp const struct got_error *err;
4655 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4656 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4657 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4659 15a087fe 2019-02-21 stsp free(s->id2);
4660 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4661 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4662 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4664 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4666 d7a04538 2019-02-21 stsp return err;
4667 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4668 15a087fe 2019-02-21 stsp free(s->id1);
4669 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4670 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4671 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4672 15a087fe 2019-02-21 stsp return NULL;
4675 0cf4efb1 2018-09-29 stsp static const struct got_error *
4676 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4678 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4680 917d79a7 2022-07-01 stsp view->count = 0;
4681 917d79a7 2022-07-01 stsp wclear(view->window);
4682 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4683 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4684 917d79a7 2022-07-01 stsp s->matched_line = 0;
4685 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4686 917d79a7 2022-07-01 stsp return create_diff(s);
4689 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4690 c0f61fa4 2022-07-11 mark int, int, int);
4691 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4692 c0f61fa4 2022-07-11 mark int, int);
4694 917d79a7 2022-07-01 stsp static const struct got_error *
4695 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4697 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4698 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4699 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4700 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4701 826082fe 2020-12-10 stsp char *line = NULL;
4702 826082fe 2020-12-10 stsp size_t linesize = 0;
4703 826082fe 2020-12-10 stsp ssize_t linelen;
4704 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4706 e5a0f69f 2018-08-18 stsp switch (ch) {
4708 145b6838 2022-06-16 stsp view->x = 0;
4711 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4712 640cd7ff 2022-06-22 mark view->count = 0;
4714 145b6838 2022-06-16 stsp case KEY_RIGHT:
4716 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4717 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4719 640cd7ff 2022-06-22 mark view->count = 0;
4721 145b6838 2022-06-16 stsp case KEY_LEFT:
4723 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4724 640cd7ff 2022-06-22 mark if (view->x <= 0)
4725 640cd7ff 2022-06-22 mark view->count = 0;
4729 3dbaef42 2020-11-24 stsp if (ch == 'a')
4730 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4731 3dbaef42 2020-11-24 stsp if (ch == 'w')
4732 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4733 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4736 912a3f79 2021-08-30 j case KEY_HOME:
4737 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4738 640cd7ff 2022-06-22 mark view->count = 0;
4741 912a3f79 2021-08-30 j case KEY_END:
4742 640cd7ff 2022-06-22 mark view->count = 0;
4746 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4750 1e37a5c2 2019-05-12 jcs case KEY_UP:
4751 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4752 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4753 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4755 640cd7ff 2022-06-22 mark view->count = 0;
4757 83cc4199 2022-06-13 stsp case CTRL('u'):
4759 83cc4199 2022-06-13 stsp nscroll /= 2;
4760 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4761 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4762 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4764 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4765 640cd7ff 2022-06-22 mark view->count = 0;
4769 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4770 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4773 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4774 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4775 1e37a5c2 2019-05-12 jcs if (!s->eof)
4776 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4778 640cd7ff 2022-06-22 mark view->count = 0;
4780 83cc4199 2022-06-13 stsp case CTRL('d'):
4782 83cc4199 2022-06-13 stsp nscroll /= 2;
4783 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4784 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4785 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4788 640cd7ff 2022-06-22 mark if (s->eof) {
4789 640cd7ff 2022-06-22 mark view->count = 0;
4793 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4794 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4795 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4796 826082fe 2020-12-10 stsp if (linelen == -1) {
4797 826082fe 2020-12-10 stsp if (feof(s->f)) {
4798 826082fe 2020-12-10 stsp s->eof = 1;
4800 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4804 826082fe 2020-12-10 stsp free(line);
4807 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4808 1e37a5c2 2019-05-12 jcs s->diff_context--;
4809 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4810 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4811 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4812 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4813 27829c9e 2020-11-21 stsp s->nlines) {
4814 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4815 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4818 640cd7ff 2022-06-22 mark view->count = 0;
4821 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4822 1e37a5c2 2019-05-12 jcs s->diff_context++;
4823 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4824 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4825 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4827 640cd7ff 2022-06-22 mark view->count = 0;
4833 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4837 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4838 640cd7ff 2022-06-22 mark view->count = 0;
4841 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4843 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4844 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4845 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4847 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4848 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4851 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4853 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4856 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4859 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4860 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4861 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4863 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4864 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4865 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4867 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4868 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4871 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4873 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4875 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4876 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4877 c0f61fa4 2022-07-11 mark bs->selected_line);
4878 c0f61fa4 2022-07-11 mark if (id == NULL)
4881 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
4884 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4888 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4889 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4890 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4891 145b6838 2022-06-16 stsp view->x = 0;
4893 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4894 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4897 640cd7ff 2022-06-22 mark view->count = 0;
4901 bcbd79e2 2018-08-19 stsp return err;
4904 4ed7e80c 2018-05-20 stsp static const struct got_error *
4905 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4907 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4908 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4909 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4910 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4911 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4912 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4913 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4914 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4915 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4916 3dbaef42 2020-11-24 stsp const char *errstr;
4917 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4918 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4920 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4921 26ed57b2 2018-05-19 stsp switch (ch) {
4923 64453f7e 2020-11-21 stsp force_text_diff = 1;
4926 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4928 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4929 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4930 5a20d08d 2022-02-09 op errstr, errstr);
4932 09b5bff8 2020-02-23 naddy case 'r':
4933 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4934 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4935 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4937 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4940 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4943 17020d27 2019-03-07 stsp usage_diff();
4944 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4948 26ed57b2 2018-05-19 stsp argc -= optind;
4949 26ed57b2 2018-05-19 stsp argv += optind;
4951 26ed57b2 2018-05-19 stsp if (argc == 0) {
4952 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4953 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4954 15a94983 2018-12-23 stsp id_str1 = argv[0];
4955 15a94983 2018-12-23 stsp id_str2 = argv[1];
4957 26ed57b2 2018-05-19 stsp usage_diff();
4959 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4960 0ae84acc 2022-06-15 tracey if (error)
4961 0ae84acc 2022-06-15 tracey goto done;
4963 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4964 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4965 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4966 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4967 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4968 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4969 c156c7a4 2020-12-18 stsp goto done;
4970 a273ac94 2020-02-23 naddy if (worktree)
4971 a273ac94 2020-02-23 naddy repo_path =
4972 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4974 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4975 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4976 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4977 c156c7a4 2020-12-18 stsp goto done;
4981 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4982 eb6600df 2019-01-04 stsp if (error)
4983 eb6600df 2019-01-04 stsp goto done;
4985 a273ac94 2020-02-23 naddy init_curses();
4987 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4988 51a10b52 2020-12-26 stsp if (error)
4989 51a10b52 2020-12-26 stsp goto done;
4991 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4992 26ed57b2 2018-05-19 stsp if (error)
4993 26ed57b2 2018-05-19 stsp goto done;
4995 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4996 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4997 26ed57b2 2018-05-19 stsp if (error)
4998 26ed57b2 2018-05-19 stsp goto done;
5000 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5001 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5002 26ed57b2 2018-05-19 stsp if (error)
5003 26ed57b2 2018-05-19 stsp goto done;
5005 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5006 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5007 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5008 ea5e7bb5 2018-08-01 stsp goto done;
5010 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5011 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5012 5dc9f4bc 2018-08-04 stsp if (error)
5013 5dc9f4bc 2018-08-04 stsp goto done;
5014 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5016 3dbaef42 2020-11-24 stsp free(label1);
5017 3dbaef42 2020-11-24 stsp free(label2);
5018 c02c541e 2019-03-29 stsp free(repo_path);
5019 a273ac94 2020-02-23 naddy free(cwd);
5020 1d0f4054 2021-06-17 stsp if (repo) {
5021 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5022 1d0f4054 2021-06-17 stsp if (error == NULL)
5023 1d0f4054 2021-06-17 stsp error = close_err;
5025 a273ac94 2020-02-23 naddy if (worktree)
5026 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5027 0ae84acc 2022-06-15 tracey if (pack_fds) {
5028 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5029 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5030 0ae84acc 2022-06-15 tracey if (error == NULL)
5031 0ae84acc 2022-06-15 tracey error = pack_err;
5033 51a10b52 2020-12-26 stsp tog_free_refs();
5034 26ed57b2 2018-05-19 stsp return error;
5037 4ed7e80c 2018-05-20 stsp __dead static void
5038 9f7d7167 2018-04-29 stsp usage_blame(void)
5041 87411fa9 2022-06-16 stsp fprintf(stderr,
5042 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5043 9f7d7167 2018-04-29 stsp getprogname());
5047 84451b3e 2018-07-10 stsp struct tog_blame_line {
5048 84451b3e 2018-07-10 stsp int annotated;
5049 84451b3e 2018-07-10 stsp struct got_object_id *id;
5052 4ed7e80c 2018-05-20 stsp static const struct got_error *
5053 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5055 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5056 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5057 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5058 84451b3e 2018-07-10 stsp const struct got_error *err;
5059 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5060 826082fe 2020-12-10 stsp char *line = NULL;
5061 826082fe 2020-12-10 stsp size_t linesize = 0;
5062 826082fe 2020-12-10 stsp ssize_t linelen;
5063 84451b3e 2018-07-10 stsp wchar_t *wline;
5064 27a741e5 2019-09-11 stsp int width;
5065 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5066 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5067 ab089a2a 2018-07-12 stsp char *id_str;
5068 11b20872 2019-11-08 stsp struct tog_color *tc;
5070 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5072 ab089a2a 2018-07-12 stsp return err;
5074 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5075 f7d12f7e 2018-08-01 stsp werase(view->window);
5077 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5078 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5079 ab089a2a 2018-07-12 stsp free(id_str);
5080 ab089a2a 2018-07-12 stsp return err;
5083 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5084 ab089a2a 2018-07-12 stsp free(line);
5085 2550e4c3 2018-07-13 stsp line = NULL;
5087 1cae65b4 2019-09-22 stsp return err;
5088 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5089 a3404814 2018-09-02 stsp wstandout(view->window);
5090 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5092 11b20872 2019-11-08 stsp wattr_on(view->window,
5093 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5094 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5096 11b20872 2019-11-08 stsp wattr_off(view->window,
5097 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5098 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5099 a3404814 2018-09-02 stsp wstandend(view->window);
5100 2550e4c3 2018-07-13 stsp free(wline);
5101 2550e4c3 2018-07-13 stsp wline = NULL;
5102 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5103 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5105 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5106 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5107 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5108 ab089a2a 2018-07-12 stsp free(id_str);
5109 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5111 ab089a2a 2018-07-12 stsp free(id_str);
5112 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5113 3f60a8ef 2018-07-10 stsp free(line);
5114 2550e4c3 2018-07-13 stsp line = NULL;
5116 3f60a8ef 2018-07-10 stsp return err;
5117 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5118 2550e4c3 2018-07-13 stsp free(wline);
5119 2550e4c3 2018-07-13 stsp wline = NULL;
5120 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5121 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5123 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5124 145b6838 2022-06-16 stsp view->maxx = 0;
5125 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5126 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5127 826082fe 2020-12-10 stsp if (linelen == -1) {
5128 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5129 826082fe 2020-12-10 stsp s->eof = 1;
5132 84451b3e 2018-07-10 stsp free(line);
5133 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5135 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5138 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5139 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5140 1853e0f4 2022-06-16 stsp if (err) {
5141 1853e0f4 2022-06-16 stsp free(line);
5142 1853e0f4 2022-06-16 stsp return err;
5144 1853e0f4 2022-06-16 stsp free(wline);
5145 1853e0f4 2022-06-16 stsp wline = NULL;
5146 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5148 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5149 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5151 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5152 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5153 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5154 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5155 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5156 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5157 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5158 8d0fe45a 2019-08-12 stsp char *id_str;
5159 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5160 87411fa9 2022-06-16 stsp blame_line->id);
5161 8d0fe45a 2019-08-12 stsp if (err) {
5162 8d0fe45a 2019-08-12 stsp free(line);
5163 8d0fe45a 2019-08-12 stsp return err;
5165 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5167 11b20872 2019-11-08 stsp wattr_on(view->window,
5168 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5169 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5171 11b20872 2019-11-08 stsp wattr_off(view->window,
5172 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5173 8d0fe45a 2019-08-12 stsp free(id_str);
5174 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5176 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5177 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5180 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5181 ee41ec32 2018-07-10 stsp prev_id = NULL;
5184 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5185 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5186 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5188 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5189 41605754 2020-11-12 stsp width = 9;
5190 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5191 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5192 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5193 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5194 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5195 41605754 2020-11-12 stsp if (err) {
5196 41605754 2020-11-12 stsp free(line);
5197 41605754 2020-11-12 stsp return err;
5199 41605754 2020-11-12 stsp width += 9;
5202 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5203 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5204 4cb9d869 2022-06-16 stsp if (err) {
5205 4cb9d869 2022-06-16 stsp free(line);
5206 4cb9d869 2022-06-16 stsp return err;
5208 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5209 a75b3e2d 2022-06-16 stsp width += 9;
5210 41605754 2020-11-12 stsp free(wline);
5211 41605754 2020-11-12 stsp wline = NULL;
5214 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5215 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5216 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5217 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5219 826082fe 2020-12-10 stsp free(line);
5220 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5222 9b058f45 2022-06-30 mark view_border(view);
5224 84451b3e 2018-07-10 stsp return NULL;
5227 84451b3e 2018-07-10 stsp static const struct got_error *
5228 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5229 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5231 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5232 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5233 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5234 1a76625f 2018-10-22 stsp int errcode;
5236 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5237 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5238 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5240 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5241 1a76625f 2018-10-22 stsp if (errcode)
5242 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5244 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5245 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5246 d68a0a7d 2018-07-10 stsp goto done;
5249 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5250 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5252 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5253 d68a0a7d 2018-07-10 stsp if (line->annotated)
5254 d68a0a7d 2018-07-10 stsp goto done;
5256 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5257 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5258 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5259 84451b3e 2018-07-10 stsp goto done;
5261 84451b3e 2018-07-10 stsp line->annotated = 1;
5263 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5264 1a76625f 2018-10-22 stsp if (errcode)
5265 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5266 84451b3e 2018-07-10 stsp return err;
5269 84451b3e 2018-07-10 stsp static void *
5270 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5272 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5273 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5274 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5275 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5276 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5278 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5279 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5280 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5282 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5283 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5284 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5285 e6e73e55 2022-06-30 tracey goto done;
5288 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5289 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5290 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5291 e6e73e55 2022-06-30 tracey goto done;
5293 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5294 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5295 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5296 e6e73e55 2022-06-30 tracey goto done;
5299 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5301 e6e73e55 2022-06-30 tracey goto done;
5303 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5304 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5305 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5306 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5307 fc06ba56 2019-08-22 stsp err = NULL;
5309 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5310 e6e73e55 2022-06-30 tracey if (errcode) {
5311 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5312 e6e73e55 2022-06-30 tracey goto done;
5315 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5316 1d0f4054 2021-06-17 stsp if (err == NULL)
5317 1d0f4054 2021-06-17 stsp err = close_err;
5318 c9beca56 2018-07-22 stsp ta->repo = NULL;
5319 c9beca56 2018-07-22 stsp *ta->complete = 1;
5321 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5322 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5323 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5326 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5327 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5328 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5329 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5330 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5331 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5332 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5333 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5335 18430de3 2018-07-10 stsp return (void *)err;
5338 245d91c1 2018-07-12 stsp static struct got_object_id *
5339 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5340 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5342 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5344 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5345 8d0fe45a 2019-08-12 stsp return NULL;
5347 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5348 c0f61fa4 2022-07-11 mark if (!line->annotated)
5349 c0f61fa4 2022-07-11 mark return NULL;
5351 c0f61fa4 2022-07-11 mark return line->id;
5354 c0f61fa4 2022-07-11 mark static struct got_object_id *
5355 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5356 c0f61fa4 2022-07-11 mark int lineno)
5358 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5360 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5361 c0f61fa4 2022-07-11 mark return NULL;
5363 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5364 245d91c1 2018-07-12 stsp if (!line->annotated)
5365 245d91c1 2018-07-12 stsp return NULL;
5367 245d91c1 2018-07-12 stsp return line->id;
5370 b880a918 2018-07-10 stsp static const struct got_error *
5371 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5373 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5376 245d91c1 2018-07-12 stsp if (blame->thread) {
5377 1a76625f 2018-10-22 stsp int errcode;
5378 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5379 1a76625f 2018-10-22 stsp if (errcode)
5380 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5381 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5382 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5383 1a76625f 2018-10-22 stsp if (errcode)
5384 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5385 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5386 1a76625f 2018-10-22 stsp if (errcode)
5387 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5388 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5389 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5390 245d91c1 2018-07-12 stsp err = NULL;
5391 245d91c1 2018-07-12 stsp blame->thread = NULL;
5393 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5394 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5395 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5396 1d0f4054 2021-06-17 stsp if (err == NULL)
5397 1d0f4054 2021-06-17 stsp err = close_err;
5398 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5400 245d91c1 2018-07-12 stsp if (blame->f) {
5401 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5402 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5403 245d91c1 2018-07-12 stsp blame->f = NULL;
5405 57670559 2018-12-23 stsp if (blame->lines) {
5406 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5407 57670559 2018-12-23 stsp free(blame->lines[i].id);
5408 57670559 2018-12-23 stsp free(blame->lines);
5409 57670559 2018-12-23 stsp blame->lines = NULL;
5411 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5412 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5413 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5414 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5415 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5416 0ae84acc 2022-06-15 tracey if (err == NULL)
5417 0ae84acc 2022-06-15 tracey err = pack_err;
5418 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5420 245d91c1 2018-07-12 stsp return err;
5423 245d91c1 2018-07-12 stsp static const struct got_error *
5424 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5426 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5427 fc06ba56 2019-08-22 stsp int *done = arg;
5428 fc06ba56 2019-08-22 stsp int errcode;
5430 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5431 fc06ba56 2019-08-22 stsp if (errcode)
5432 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5433 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5435 fc06ba56 2019-08-22 stsp if (*done)
5436 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5438 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5439 fc06ba56 2019-08-22 stsp if (errcode)
5440 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5441 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5443 fc06ba56 2019-08-22 stsp return err;
5446 fc06ba56 2019-08-22 stsp static const struct got_error *
5447 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5449 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5450 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5451 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5452 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5453 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5454 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5455 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5456 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5457 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5459 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5460 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5462 15a94983 2018-12-23 stsp return err;
5464 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5465 eb81bc23 2022-06-28 tracey if (fd == -1) {
5466 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5467 eb81bc23 2022-06-28 tracey goto done;
5470 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5472 a44927cc 2022-04-07 stsp goto done;
5474 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5476 84451b3e 2018-07-10 stsp goto done;
5478 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5479 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5480 84451b3e 2018-07-10 stsp goto done;
5483 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5485 a70480e0 2018-06-23 stsp goto done;
5486 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5487 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5488 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5489 84451b3e 2018-07-10 stsp goto done;
5491 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5492 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5494 1fddf795 2021-01-20 stsp goto done;
5495 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5496 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5497 84451b3e 2018-07-10 stsp goto done;
5500 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5501 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5502 b02560ec 2019-08-19 stsp blame->nlines--;
5504 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5505 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5506 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5507 84451b3e 2018-07-10 stsp goto done;
5510 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5512 bd24772e 2018-07-11 stsp goto done;
5513 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5514 0ae84acc 2022-06-15 tracey pack_fds);
5515 0ae84acc 2022-06-15 tracey if (err)
5516 0ae84acc 2022-06-15 tracey goto done;
5518 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5519 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5520 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5521 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5522 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5523 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5524 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5525 245d91c1 2018-07-12 stsp goto done;
5527 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5529 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5530 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5531 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5532 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5533 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5534 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5535 a5388363 2020-12-01 naddy s->blame_complete = 0;
5537 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5538 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5539 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5540 f5a09613 2020-12-13 naddy s->selected_line = 1;
5542 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5545 a44927cc 2022-04-07 stsp if (commit)
5546 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5547 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5548 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5550 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5551 27d434c2 2018-09-15 stsp free(obj_id);
5553 245d91c1 2018-07-12 stsp stop_blame(blame);
5554 245d91c1 2018-07-12 stsp return err;
5557 245d91c1 2018-07-12 stsp static const struct got_error *
5558 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5559 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5561 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5562 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5564 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5566 c4843652 2019-08-12 stsp s->path = strdup(path);
5567 c4843652 2019-08-12 stsp if (s->path == NULL)
5568 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5570 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5571 c4843652 2019-08-12 stsp if (err) {
5572 c4843652 2019-08-12 stsp free(s->path);
5573 7cbe629d 2018-08-04 stsp return err;
5576 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5577 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5578 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5579 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5580 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5581 fb2756b9 2018-08-04 stsp s->repo = repo;
5582 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5583 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5585 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5586 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5587 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5588 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5590 11b20872 2019-11-08 stsp return err;
5593 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5594 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5595 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5596 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5597 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5598 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5600 a5388363 2020-12-01 naddy return run_blame(view);
5603 e5a0f69f 2018-08-18 stsp static const struct got_error *
5604 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5606 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5607 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5609 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5610 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5612 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5613 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5614 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5615 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5616 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5619 e5a0f69f 2018-08-18 stsp free(s->path);
5620 11b20872 2019-11-08 stsp free_colors(&s->colors);
5621 e5a0f69f 2018-08-18 stsp return err;
5624 7cbe629d 2018-08-04 stsp static const struct got_error *
5625 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5627 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5629 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5630 6c4c42e0 2019-06-24 stsp return NULL;
5633 6c4c42e0 2019-06-24 stsp static const struct got_error *
5634 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5636 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5637 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5638 6c4c42e0 2019-06-24 stsp int lineno;
5639 cb713507 2022-06-16 stsp char *line = NULL;
5640 826082fe 2020-12-10 stsp size_t linesize = 0;
5641 826082fe 2020-12-10 stsp ssize_t linelen;
5643 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5644 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5645 6c4c42e0 2019-06-24 stsp return NULL;
5648 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5649 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5650 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5652 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5654 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5656 6c4c42e0 2019-06-24 stsp while (1) {
5657 6c4c42e0 2019-06-24 stsp off_t offset;
5659 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5660 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5661 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5665 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5666 6c4c42e0 2019-06-24 stsp lineno = 1;
5668 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5671 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5672 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5673 6c4c42e0 2019-06-24 stsp free(line);
5674 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5676 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5677 cb713507 2022-06-16 stsp if (linelen != -1) {
5678 cb713507 2022-06-16 stsp char *exstr;
5679 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5682 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5683 cb713507 2022-06-16 stsp &view->regmatch)) {
5684 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5685 cb713507 2022-06-16 stsp s->matched_line = lineno;
5686 cb713507 2022-06-16 stsp free(exstr);
5689 cb713507 2022-06-16 stsp free(exstr);
5691 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5696 826082fe 2020-12-10 stsp free(line);
5698 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5699 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5700 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5703 145b6838 2022-06-16 stsp return err;
5706 6c4c42e0 2019-06-24 stsp static const struct got_error *
5707 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5709 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5710 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5711 2b380cc8 2018-10-24 stsp int errcode;
5713 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5714 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5715 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5716 2b380cc8 2018-10-24 stsp if (errcode)
5717 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5719 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5722 51fe7530 2019-08-19 stsp if (s->blame_complete)
5723 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5725 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5727 9b058f45 2022-06-30 mark view_border(view);
5728 e5a0f69f 2018-08-18 stsp return err;
5731 e5a0f69f 2018-08-18 stsp static const struct got_error *
5732 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5733 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
5735 05f04cdf 2022-07-20 mark struct tog_view *log_view;
5736 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
5738 05f04cdf 2022-07-20 mark *new_view = NULL;
5740 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5741 05f04cdf 2022-07-20 mark if (log_view == NULL)
5742 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
5744 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5746 05f04cdf 2022-07-20 mark view_close(log_view);
5748 05f04cdf 2022-07-20 mark *new_view = log_view;
5750 05f04cdf 2022-07-20 mark return err;
5753 05f04cdf 2022-07-20 mark static const struct got_error *
5754 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5756 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5757 05f04cdf 2022-07-20 mark struct tog_view *diff_view, *log_view;
5758 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5759 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5761 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5762 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5763 9b058f45 2022-06-30 mark --eos; /* border */
5765 e5a0f69f 2018-08-18 stsp switch (ch) {
5767 145b6838 2022-06-16 stsp view->x = 0;
5770 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5771 640cd7ff 2022-06-22 mark view->count = 0;
5773 145b6838 2022-06-16 stsp case KEY_RIGHT:
5775 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5776 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5778 640cd7ff 2022-06-22 mark view->count = 0;
5780 145b6838 2022-06-16 stsp case KEY_LEFT:
5782 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5783 640cd7ff 2022-06-22 mark if (view->x <= 0)
5784 640cd7ff 2022-06-22 mark view->count = 0;
5787 1e37a5c2 2019-05-12 jcs s->done = 1;
5789 4deef56f 2021-09-02 naddy case 'g':
5790 4deef56f 2021-09-02 naddy case KEY_HOME:
5791 4deef56f 2021-09-02 naddy s->selected_line = 1;
5792 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5793 640cd7ff 2022-06-22 mark view->count = 0;
5795 4deef56f 2021-09-02 naddy case 'G':
5796 4deef56f 2021-09-02 naddy case KEY_END:
5797 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5798 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5799 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5801 9b058f45 2022-06-30 mark s->selected_line = eos;
5802 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5804 640cd7ff 2022-06-22 mark view->count = 0;
5807 1e37a5c2 2019-05-12 jcs case KEY_UP:
5808 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5809 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5810 1e37a5c2 2019-05-12 jcs s->selected_line--;
5811 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5812 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5813 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5815 640cd7ff 2022-06-22 mark view->count = 0;
5817 83cc4199 2022-06-13 stsp case CTRL('u'):
5819 83cc4199 2022-06-13 stsp nscroll /= 2;
5820 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5821 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5822 ea025d1d 2020-02-22 naddy case CTRL('b'):
5824 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5825 640cd7ff 2022-06-22 mark if (view->count > 1)
5826 640cd7ff 2022-06-22 mark nscroll += nscroll;
5827 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5828 640cd7ff 2022-06-22 mark view->count = 0;
5831 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5832 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5834 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5837 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5838 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5839 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5840 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5841 1e37a5c2 2019-05-12 jcs s->selected_line++;
5842 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5843 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5845 640cd7ff 2022-06-22 mark view->count = 0;
5848 1e37a5c2 2019-05-12 jcs case 'p': {
5849 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5851 640cd7ff 2022-06-22 mark view->count = 0;
5852 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5853 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5854 1e37a5c2 2019-05-12 jcs if (id == NULL)
5856 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5857 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5858 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5859 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5860 1e37a5c2 2019-05-12 jcs int obj_type;
5861 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5862 1e37a5c2 2019-05-12 jcs s->repo, id);
5865 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5866 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5867 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5868 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5871 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5872 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5873 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5876 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5877 a44927cc 2022-04-07 stsp pcommit, s->path);
5878 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5879 e5a0f69f 2018-08-18 stsp if (err) {
5880 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5881 1e37a5c2 2019-05-12 jcs err = NULL;
5882 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5885 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5887 1e37a5c2 2019-05-12 jcs free(blob_id);
5888 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5889 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5890 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5893 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5894 d7b5a0e8 2022-04-20 stsp &pid->id);
5895 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5897 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5898 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5900 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5905 1e37a5c2 2019-05-12 jcs s->done = 1;
5906 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5907 1e37a5c2 2019-05-12 jcs s->done = 0;
5908 1e37a5c2 2019-05-12 jcs if (thread_err)
5910 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5911 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5912 a5388363 2020-12-01 naddy err = run_blame(view);
5917 61417565 2022-06-20 mark case 'C': {
5918 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5920 640cd7ff 2022-06-22 mark view->count = 0;
5921 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5922 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5924 1e37a5c2 2019-05-12 jcs s->done = 1;
5925 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5926 1e37a5c2 2019-05-12 jcs s->done = 0;
5927 1e37a5c2 2019-05-12 jcs if (thread_err)
5929 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5930 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5931 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5932 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5933 a5388363 2020-12-01 naddy err = run_blame(view);
5938 05f04cdf 2022-07-20 mark case 'L': {
5939 05f04cdf 2022-07-20 mark struct got_object_id *id = NULL;
5941 05f04cdf 2022-07-20 mark view->count = 0;
5942 05f04cdf 2022-07-20 mark id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5943 05f04cdf 2022-07-20 mark s->first_displayed_line, s->selected_line);
5944 05f04cdf 2022-07-20 mark if (id == NULL)
5947 05f04cdf 2022-07-20 mark if (view_is_parent_view(view))
5948 05f04cdf 2022-07-20 mark view_get_split(view, &begin_y, &begin_x);
5949 05f04cdf 2022-07-20 mark err = log_annotated_line(&log_view, begin_y, begin_x,
5950 05f04cdf 2022-07-20 mark s->repo, id);
5953 05f04cdf 2022-07-20 mark if (view_is_parent_view(view) &&
5954 05f04cdf 2022-07-20 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5955 05f04cdf 2022-07-20 mark err = view_init_hsplit(view, begin_y);
5960 05f04cdf 2022-07-20 mark view->focussed = 0;
5961 05f04cdf 2022-07-20 mark log_view->focussed = 1;
5962 05f04cdf 2022-07-20 mark log_view->mode = view->mode;
5963 05f04cdf 2022-07-20 mark log_view->nlines = view->lines - begin_y;
5964 05f04cdf 2022-07-20 mark if (view_is_parent_view(view)) {
5965 05f04cdf 2022-07-20 mark view_transfer_size(log_view, view);
5966 05f04cdf 2022-07-20 mark err = view_close_child(view);
5968 05f04cdf 2022-07-20 mark return err;
5969 05f04cdf 2022-07-20 mark err = view_set_child(view, log_view);
5971 05f04cdf 2022-07-20 mark return err;
5972 05f04cdf 2022-07-20 mark view->focus_child = 1;
5974 05f04cdf 2022-07-20 mark *new_view = log_view;
5977 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5978 1e37a5c2 2019-05-12 jcs case '\r': {
5979 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5980 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5981 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5983 640cd7ff 2022-06-22 mark view->count = 0;
5984 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5985 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5986 1e37a5c2 2019-05-12 jcs if (id == NULL)
5988 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5991 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5992 c0f61fa4 2022-07-11 mark if (*new_view) {
5993 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
5994 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
5997 c0f61fa4 2022-07-11 mark diff_view = *new_view;
5999 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6000 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6002 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6003 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6004 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6005 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6006 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6010 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6011 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6012 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6014 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6017 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6018 c0f61fa4 2022-07-11 mark s->selected_line;
6019 c0f61fa4 2022-07-11 mark if (*new_view)
6020 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6021 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6022 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6023 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6028 e78dc838 2020-12-04 stsp view->focussed = 0;
6029 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6030 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6031 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6032 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6033 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6034 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6037 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6040 e78dc838 2020-12-04 stsp view->focus_child = 1;
6042 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6047 83cc4199 2022-06-13 stsp case CTRL('d'):
6049 83cc4199 2022-06-13 stsp nscroll /= 2;
6050 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6051 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6052 ea025d1d 2020-02-22 naddy case CTRL('f'):
6055 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6056 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6057 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6058 640cd7ff 2022-06-22 mark view->count = 0;
6061 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6062 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6063 83cc4199 2022-06-13 stsp s->selected_line +=
6064 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6065 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6067 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6068 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6070 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6071 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6073 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6074 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6075 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6076 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6080 640cd7ff 2022-06-22 mark view->count = 0;
6083 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6086 917d79a7 2022-07-01 stsp static const struct got_error *
6087 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6089 917d79a7 2022-07-01 stsp const struct got_error *err;
6090 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6092 917d79a7 2022-07-01 stsp view->count = 0;
6093 917d79a7 2022-07-01 stsp s->done = 1;
6094 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6095 917d79a7 2022-07-01 stsp s->done = 0;
6097 917d79a7 2022-07-01 stsp return err;
6098 917d79a7 2022-07-01 stsp return run_blame(view);
6101 a70480e0 2018-06-23 stsp static const struct got_error *
6102 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6104 a70480e0 2018-06-23 stsp const struct got_error *error;
6105 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6106 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6107 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6108 0587e10c 2020-07-23 stsp char *link_target = NULL;
6109 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6110 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6111 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6113 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6114 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6116 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6117 a70480e0 2018-06-23 stsp switch (ch) {
6119 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6122 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6123 69069811 2018-08-02 stsp if (repo_path == NULL)
6124 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6128 17020d27 2019-03-07 stsp usage_blame();
6129 a70480e0 2018-06-23 stsp /* NOTREACHED */
6133 a70480e0 2018-06-23 stsp argc -= optind;
6134 a70480e0 2018-06-23 stsp argv += optind;
6136 f135c941 2020-02-20 stsp if (argc != 1)
6137 a70480e0 2018-06-23 stsp usage_blame();
6139 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6140 0ae84acc 2022-06-15 tracey if (error != NULL)
6141 0ae84acc 2022-06-15 tracey goto done;
6143 69069811 2018-08-02 stsp if (repo_path == NULL) {
6144 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6145 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6146 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6147 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6148 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6149 c156c7a4 2020-12-18 stsp goto done;
6150 f135c941 2020-02-20 stsp if (worktree)
6151 eb41ed75 2019-02-05 stsp repo_path =
6152 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6154 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6155 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6156 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6157 c156c7a4 2020-12-18 stsp goto done;
6161 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6162 c02c541e 2019-03-29 stsp if (error != NULL)
6163 8e94dd5b 2019-01-04 stsp goto done;
6165 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6166 f135c941 2020-02-20 stsp worktree);
6167 c02c541e 2019-03-29 stsp if (error)
6168 92205607 2019-01-04 stsp goto done;
6170 f135c941 2020-02-20 stsp init_curses();
6172 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6173 51a10b52 2020-12-26 stsp if (error)
6174 51a10b52 2020-12-26 stsp goto done;
6176 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6177 eb41ed75 2019-02-05 stsp if (error)
6178 69069811 2018-08-02 stsp goto done;
6180 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6181 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6182 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6183 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6184 a70480e0 2018-06-23 stsp if (error != NULL)
6185 66b4983c 2018-06-23 stsp goto done;
6186 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6187 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6189 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6190 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6192 a19e88aa 2018-06-23 stsp if (error != NULL)
6193 8b473291 2019-02-21 stsp goto done;
6195 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6196 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6197 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6198 e1cd8fed 2018-08-01 stsp goto done;
6201 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6202 a44927cc 2022-04-07 stsp if (error)
6203 a44927cc 2022-04-07 stsp goto done;
6205 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6206 a44927cc 2022-04-07 stsp commit, repo);
6207 7cbe629d 2018-08-04 stsp if (error)
6208 7cbe629d 2018-08-04 stsp goto done;
6210 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6211 78756c87 2020-11-24 stsp commit_id, repo);
6212 0587e10c 2020-07-23 stsp if (error)
6213 0587e10c 2020-07-23 stsp goto done;
6214 12314ad4 2019-08-31 stsp if (worktree) {
6215 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6216 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6217 12314ad4 2019-08-31 stsp worktree = NULL;
6219 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6221 69069811 2018-08-02 stsp free(repo_path);
6222 f135c941 2020-02-20 stsp free(in_repo_path);
6223 0587e10c 2020-07-23 stsp free(link_target);
6224 69069811 2018-08-02 stsp free(cwd);
6225 a70480e0 2018-06-23 stsp free(commit_id);
6226 a44927cc 2022-04-07 stsp if (commit)
6227 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6228 eb41ed75 2019-02-05 stsp if (worktree)
6229 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6230 1d0f4054 2021-06-17 stsp if (repo) {
6231 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6232 1d0f4054 2021-06-17 stsp if (error == NULL)
6233 1d0f4054 2021-06-17 stsp error = close_err;
6235 0ae84acc 2022-06-15 tracey if (pack_fds) {
6236 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6237 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6238 0ae84acc 2022-06-15 tracey if (error == NULL)
6239 0ae84acc 2022-06-15 tracey error = pack_err;
6241 51a10b52 2020-12-26 stsp tog_free_refs();
6242 a70480e0 2018-06-23 stsp return error;
6245 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6246 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6248 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6249 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6250 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6251 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6252 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6253 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6254 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6256 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6257 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6258 9b058f45 2022-06-30 mark --limit; /* border */
6260 f7d12f7e 2018-08-01 stsp werase(view->window);
6262 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6263 ffd1d5e5 2018-06-23 stsp return NULL;
6265 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6268 ffd1d5e5 2018-06-23 stsp return err;
6269 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6270 a3404814 2018-09-02 stsp wstandout(view->window);
6271 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6273 11b20872 2019-11-08 stsp wattr_on(view->window,
6274 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6275 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6277 11b20872 2019-11-08 stsp wattr_off(view->window,
6278 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6279 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6280 a3404814 2018-09-02 stsp wstandend(view->window);
6281 2550e4c3 2018-07-13 stsp free(wline);
6282 2550e4c3 2018-07-13 stsp wline = NULL;
6283 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6284 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6285 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6286 ffd1d5e5 2018-06-23 stsp return NULL;
6287 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6290 ce52c690 2018-06-23 stsp return err;
6291 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6292 2550e4c3 2018-07-13 stsp free(wline);
6293 2550e4c3 2018-07-13 stsp wline = NULL;
6294 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6295 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6296 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6297 ffd1d5e5 2018-06-23 stsp return NULL;
6298 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6299 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6300 a1eca9bb 2018-06-23 stsp return NULL;
6302 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6303 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6304 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6305 0cf4efb1 2018-09-29 stsp if (view->focussed)
6306 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6307 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6309 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6310 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6311 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6312 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6313 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6314 ffd1d5e5 2018-06-23 stsp return NULL;
6318 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6321 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6322 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6323 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6324 848d6979 2019-08-12 stsp const char *modestr = "";
6325 56e0773d 2019-11-28 stsp mode_t mode;
6327 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6328 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6330 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6331 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6332 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6334 638f9024 2019-05-13 stsp return got_error_from_errno(
6335 230a42bd 2019-05-11 jcs "got_object_id_str");
6337 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6338 63c5ca5d 2019-08-24 stsp modestr = "$";
6339 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6342 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6343 d86d3b18 2020-12-01 naddy te, s->repo);
6344 0d6c6ee3 2020-05-20 stsp if (err) {
6345 0d6c6ee3 2020-05-20 stsp free(id_str);
6346 0d6c6ee3 2020-05-20 stsp return err;
6348 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6349 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6350 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6352 848d6979 2019-08-12 stsp modestr = "@";
6354 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6355 848d6979 2019-08-12 stsp modestr = "/";
6356 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6357 848d6979 2019-08-12 stsp modestr = "*";
6358 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6359 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6360 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6361 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6362 1d13200f 2018-07-12 stsp free(id_str);
6363 0d6c6ee3 2020-05-20 stsp free(link_target);
6364 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6366 1d13200f 2018-07-12 stsp free(id_str);
6367 0d6c6ee3 2020-05-20 stsp free(link_target);
6368 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6370 ffd1d5e5 2018-06-23 stsp if (err) {
6371 ffd1d5e5 2018-06-23 stsp free(line);
6374 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6375 0cf4efb1 2018-09-29 stsp if (view->focussed)
6376 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6377 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6379 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6381 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6382 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6383 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6385 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6386 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6387 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6388 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6389 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6390 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6391 ffd1d5e5 2018-06-23 stsp free(line);
6392 2550e4c3 2018-07-13 stsp free(wline);
6393 2550e4c3 2018-07-13 stsp wline = NULL;
6395 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6396 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6397 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6401 ffd1d5e5 2018-06-23 stsp return err;
6404 ffd1d5e5 2018-06-23 stsp static void
6405 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6407 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6408 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6409 fa86c4bf 2020-11-29 stsp int i = 0;
6411 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6414 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6415 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6416 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6417 fa86c4bf 2020-11-29 stsp if (!isroot)
6418 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6421 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6422 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6426 9b058f45 2022-06-30 mark static const struct got_error *
6427 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6429 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6430 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6431 ffd1d5e5 2018-06-23 stsp int n = 0;
6433 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6434 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6435 694d3271 2020-12-01 naddy s->first_displayed_entry);
6437 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6439 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6440 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6442 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6443 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6444 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6445 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6449 9b058f45 2022-06-30 mark return NULL;
6452 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6453 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6454 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6456 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6457 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6458 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6460 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6461 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6462 56e0773d 2019-11-28 stsp + 1 /* slash */;
6464 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6466 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6467 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6468 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6470 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6471 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6472 d9765a41 2018-06-23 stsp while (pt) {
6473 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6474 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6475 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6476 cb2ebc8a 2018-06-23 stsp goto done;
6478 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6479 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6480 cb2ebc8a 2018-06-23 stsp goto done;
6482 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6485 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6486 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6487 ce52c690 2018-06-23 stsp goto done;
6491 ce52c690 2018-06-23 stsp if (err) {
6492 ce52c690 2018-06-23 stsp free(*path);
6493 ce52c690 2018-06-23 stsp *path = NULL;
6495 ce52c690 2018-06-23 stsp return err;
6498 ce52c690 2018-06-23 stsp static const struct got_error *
6499 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6500 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6501 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6503 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6504 ce52c690 2018-06-23 stsp char *path;
6505 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6507 a0de39f3 2019-08-09 stsp *new_view = NULL;
6509 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6511 ce52c690 2018-06-23 stsp return err;
6513 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6514 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6515 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6516 83ce39e3 2019-08-12 stsp goto done;
6519 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6520 e5a0f69f 2018-08-18 stsp if (err) {
6521 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6522 fc06ba56 2019-08-22 stsp err = NULL;
6523 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6525 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6527 83ce39e3 2019-08-12 stsp free(path);
6528 69efd4c4 2018-07-18 stsp return err;
6531 69efd4c4 2018-07-18 stsp static const struct got_error *
6532 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6533 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6535 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6536 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6537 69efd4c4 2018-07-18 stsp char *path;
6539 a0de39f3 2019-08-09 stsp *new_view = NULL;
6541 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6542 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6543 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6545 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6547 69efd4c4 2018-07-18 stsp return err;
6549 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6552 e5a0f69f 2018-08-18 stsp view_close(log_view);
6554 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6555 cb2ebc8a 2018-06-23 stsp free(path);
6556 cb2ebc8a 2018-06-23 stsp return err;
6559 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6560 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6561 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6563 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6564 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6565 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6566 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6568 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6569 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6571 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6572 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6573 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6575 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6577 bc573f3b 2021-07-10 stsp goto done;
6580 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6581 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6582 bc573f3b 2021-07-10 stsp * closed on demand.
6584 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6585 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6587 bc573f3b 2021-07-10 stsp goto done;
6588 bc573f3b 2021-07-10 stsp s->tree = s->root;
6590 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6591 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6592 ffd1d5e5 2018-06-23 stsp goto done;
6594 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6595 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6596 ffd1d5e5 2018-06-23 stsp goto done;
6599 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6600 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6601 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6602 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6603 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6604 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6605 9cd7cbd1 2020-12-07 stsp goto done;
6608 fb2756b9 2018-08-04 stsp s->repo = repo;
6610 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6611 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6612 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6613 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6615 c0b01bdb 2019-11-08 stsp goto done;
6616 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6617 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6619 c0b01bdb 2019-11-08 stsp goto done;
6620 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6621 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6622 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6624 c0b01bdb 2019-11-08 stsp goto done;
6626 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6627 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6628 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6630 11b20872 2019-11-08 stsp goto done;
6632 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6633 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6635 c0b01bdb 2019-11-08 stsp goto done;
6638 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6639 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6640 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6641 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6642 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6644 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6645 bc573f3b 2021-07-10 stsp if (commit)
6646 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6648 bc573f3b 2021-07-10 stsp close_tree_view(view);
6649 ad80ab7b 2018-08-04 stsp return err;
6652 e5a0f69f 2018-08-18 stsp static const struct got_error *
6653 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6655 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6657 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6658 fb2756b9 2018-08-04 stsp free(s->tree_label);
6659 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6660 6484ec90 2018-09-29 stsp free(s->commit_id);
6661 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6662 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6663 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6664 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6665 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6666 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6667 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6668 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6669 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6670 ad80ab7b 2018-08-04 stsp free(parent);
6673 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6674 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6675 bc573f3b 2021-07-10 stsp if (s->root)
6676 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6677 7c32bd05 2019-06-22 stsp return NULL;
6680 7c32bd05 2019-06-22 stsp static const struct got_error *
6681 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6683 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6685 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6686 7c32bd05 2019-06-22 stsp return NULL;
6689 7c32bd05 2019-06-22 stsp static int
6690 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6692 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6694 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6698 7c32bd05 2019-06-22 stsp static const struct got_error *
6699 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6701 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6702 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6704 7c32bd05 2019-06-22 stsp if (!view->searching) {
6705 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6706 7c32bd05 2019-06-22 stsp return NULL;
6709 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6710 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6711 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6712 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6713 56e0773d 2019-11-28 stsp s->selected_entry);
6715 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6717 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6718 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6720 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6721 56e0773d 2019-11-28 stsp s->selected_entry);
6724 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6725 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6726 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6727 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6729 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6732 7c32bd05 2019-06-22 stsp while (1) {
6733 56e0773d 2019-11-28 stsp if (te == NULL) {
6734 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6735 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6736 ac66afb8 2019-06-24 stsp return NULL;
6738 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6739 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6741 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6744 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6745 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6746 56e0773d 2019-11-28 stsp s->matched_entry = te;
6750 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6751 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6753 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6756 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6757 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6758 7c32bd05 2019-06-22 stsp s->selected = 0;
6761 e5a0f69f 2018-08-18 stsp return NULL;
6764 ad80ab7b 2018-08-04 stsp static const struct got_error *
6765 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6767 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6768 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6769 e5a0f69f 2018-08-18 stsp char *parent_path;
6771 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6773 e5a0f69f 2018-08-18 stsp return err;
6775 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6776 e5a0f69f 2018-08-18 stsp free(parent_path);
6778 9b058f45 2022-06-30 mark view_border(view);
6779 e5a0f69f 2018-08-18 stsp return err;
6782 e5a0f69f 2018-08-18 stsp static const struct got_error *
6783 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6785 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6786 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6787 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6788 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6789 49b24ee5 2022-07-03 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6791 e5a0f69f 2018-08-18 stsp switch (ch) {
6793 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6794 640cd7ff 2022-06-22 mark view->count = 0;
6797 640cd7ff 2022-06-22 mark view->count = 0;
6798 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6800 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6801 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6802 49b24ee5 2022-07-03 mark err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6803 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6804 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6805 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6809 e78dc838 2020-12-04 stsp view->focussed = 0;
6810 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6811 49b24ee5 2022-07-03 mark log_view->mode = view->mode;
6812 49b24ee5 2022-07-03 mark log_view->nlines = view->lines - begin_y;
6813 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6814 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
6815 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6817 1e37a5c2 2019-05-12 jcs return err;
6818 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6821 e78dc838 2020-12-04 stsp view->focus_child = 1;
6823 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6826 640cd7ff 2022-06-22 mark view->count = 0;
6827 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6828 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6829 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6830 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6831 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6832 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6833 152c1c93 2020-11-29 stsp if (err) {
6834 152c1c93 2020-11-29 stsp view_close(ref_view);
6835 152c1c93 2020-11-29 stsp return err;
6837 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6838 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6839 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6843 e78dc838 2020-12-04 stsp view->focussed = 0;
6844 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6845 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
6846 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
6847 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6848 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
6849 152c1c93 2020-11-29 stsp err = view_close_child(view);
6851 152c1c93 2020-11-29 stsp return err;
6852 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6855 e78dc838 2020-12-04 stsp view->focus_child = 1;
6857 152c1c93 2020-11-29 stsp *new_view = ref_view;
6859 e4526bf5 2021-09-03 naddy case 'g':
6860 e4526bf5 2021-09-03 naddy case KEY_HOME:
6861 e4526bf5 2021-09-03 naddy s->selected = 0;
6862 640cd7ff 2022-06-22 mark view->count = 0;
6863 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6864 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6865 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6867 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6869 e4526bf5 2021-09-03 naddy case 'G':
6870 9b058f45 2022-06-30 mark case KEY_END: {
6871 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6873 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6874 9b058f45 2022-06-30 mark --eos; /* border */
6875 e4526bf5 2021-09-03 naddy s->selected = 0;
6876 640cd7ff 2022-06-22 mark view->count = 0;
6877 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6878 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6879 e4526bf5 2021-09-03 naddy if (te == NULL) {
6880 ad055527 2022-07-16 mark if (s->tree != s->root) {
6881 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6886 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6887 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6889 e4526bf5 2021-09-03 naddy if (n > 0)
6890 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6894 1e37a5c2 2019-05-12 jcs case KEY_UP:
6895 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6896 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6897 1e37a5c2 2019-05-12 jcs s->selected--;
6900 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6901 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6902 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6903 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6904 640cd7ff 2022-06-22 mark view->count = 0;
6906 83cc4199 2022-06-13 stsp case CTRL('u'):
6908 83cc4199 2022-06-13 stsp nscroll /= 2;
6909 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6910 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6911 ea025d1d 2020-02-22 naddy case CTRL('b'):
6913 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6914 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6915 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6916 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6918 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6919 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6921 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6922 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6923 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6924 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6925 640cd7ff 2022-06-22 mark view->count = 0;
6928 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6929 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6930 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6931 1e37a5c2 2019-05-12 jcs s->selected++;
6934 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6935 640cd7ff 2022-06-22 mark == NULL) {
6936 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6937 640cd7ff 2022-06-22 mark view->count = 0;
6940 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6942 83cc4199 2022-06-13 stsp case CTRL('d'):
6944 83cc4199 2022-06-13 stsp nscroll /= 2;
6945 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6946 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6947 ea025d1d 2020-02-22 naddy case CTRL('f'):
6949 48bb96f0 2022-06-20 naddy case ' ':
6950 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6952 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6953 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6954 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6955 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6957 640cd7ff 2022-06-22 mark view->count = 0;
6960 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6962 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6964 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6965 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6966 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6967 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6968 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6969 640cd7ff 2022-06-22 mark view->count = 0;
6972 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6973 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6975 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6976 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6977 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6978 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6979 1e37a5c2 2019-05-12 jcs s->selected_entry =
6980 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6981 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6982 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6983 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6987 1e37a5c2 2019-05-12 jcs free(parent);
6988 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6989 56e0773d 2019-11-28 stsp s->selected_entry))) {
6990 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6991 640cd7ff 2022-06-22 mark view->count = 0;
6992 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6993 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6996 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6997 941e9f74 2019-05-21 stsp if (err) {
6998 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7001 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7002 56e0773d 2019-11-28 stsp s->selected_entry))) {
7003 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7004 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
7006 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
7007 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7009 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
7010 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7011 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7015 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7016 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7017 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7022 640cd7ff 2022-06-22 mark view->count = 0;
7023 e78dc838 2020-12-04 stsp view->focussed = 0;
7024 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7025 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
7026 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
7027 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7028 3c1dfe12 2022-07-08 mark view_transfer_size(blame_view, view);
7029 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7031 669b5ffa 2018-10-07 stsp return err;
7032 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
7035 e78dc838 2020-12-04 stsp view->focus_child = 1;
7037 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7040 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7041 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7042 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7043 640cd7ff 2022-06-22 mark view->count = 0;
7046 640cd7ff 2022-06-22 mark view->count = 0;
7050 ffd1d5e5 2018-06-23 stsp return err;
7053 ffd1d5e5 2018-06-23 stsp __dead static void
7054 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7057 87411fa9 2022-06-16 stsp fprintf(stderr,
7058 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7059 ffd1d5e5 2018-06-23 stsp getprogname());
7063 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7064 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7066 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7067 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7068 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7069 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7070 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7071 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7072 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7073 4e97c21c 2020-12-06 stsp char *label = NULL;
7074 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7075 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7077 5221c383 2018-08-01 stsp struct tog_view *view;
7078 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7080 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7081 ffd1d5e5 2018-06-23 stsp switch (ch) {
7083 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7086 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7087 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7088 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7092 e99e2d15 2020-11-24 naddy usage_tree();
7093 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7097 ffd1d5e5 2018-06-23 stsp argc -= optind;
7098 ffd1d5e5 2018-06-23 stsp argv += optind;
7100 55cccc34 2020-02-20 stsp if (argc > 1)
7101 e99e2d15 2020-11-24 naddy usage_tree();
7103 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7104 0ae84acc 2022-06-15 tracey if (error != NULL)
7105 0ae84acc 2022-06-15 tracey goto done;
7107 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7108 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7109 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7110 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7111 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7112 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7113 c156c7a4 2020-12-18 stsp goto done;
7114 55cccc34 2020-02-20 stsp if (worktree)
7115 52185f70 2019-02-05 stsp repo_path =
7116 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7118 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7119 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7120 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7121 c156c7a4 2020-12-18 stsp goto done;
7125 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7126 c02c541e 2019-03-29 stsp if (error != NULL)
7127 52185f70 2019-02-05 stsp goto done;
7129 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7130 55cccc34 2020-02-20 stsp repo, worktree);
7131 55cccc34 2020-02-20 stsp if (error)
7132 55cccc34 2020-02-20 stsp goto done;
7134 55cccc34 2020-02-20 stsp init_curses();
7136 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7137 c02c541e 2019-03-29 stsp if (error)
7138 52185f70 2019-02-05 stsp goto done;
7140 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7141 51a10b52 2020-12-26 stsp if (error)
7142 51a10b52 2020-12-26 stsp goto done;
7144 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7145 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7146 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7147 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7148 4e97c21c 2020-12-06 stsp if (error)
7149 4e97c21c 2020-12-06 stsp goto done;
7150 4e97c21c 2020-12-06 stsp head_ref_name = label;
7152 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7153 4e97c21c 2020-12-06 stsp if (error == NULL)
7154 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7155 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7156 4e97c21c 2020-12-06 stsp goto done;
7157 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7158 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7159 4e97c21c 2020-12-06 stsp if (error)
7160 4e97c21c 2020-12-06 stsp goto done;
7163 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7164 a44927cc 2022-04-07 stsp if (error)
7165 a44927cc 2022-04-07 stsp goto done;
7167 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7168 5221c383 2018-08-01 stsp if (view == NULL) {
7169 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7170 5221c383 2018-08-01 stsp goto done;
7172 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7173 ad80ab7b 2018-08-04 stsp if (error)
7174 ad80ab7b 2018-08-04 stsp goto done;
7175 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7176 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7177 d91faf3b 2020-12-01 naddy in_repo_path);
7178 55cccc34 2020-02-20 stsp if (error)
7179 55cccc34 2020-02-20 stsp goto done;
7182 55cccc34 2020-02-20 stsp if (worktree) {
7183 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7184 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7185 55cccc34 2020-02-20 stsp worktree = NULL;
7187 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7189 52185f70 2019-02-05 stsp free(repo_path);
7190 e4a0e26d 2020-02-20 stsp free(cwd);
7191 ffd1d5e5 2018-06-23 stsp free(commit_id);
7192 4e97c21c 2020-12-06 stsp free(label);
7194 486cd271 2020-12-06 stsp got_ref_close(ref);
7195 1d0f4054 2021-06-17 stsp if (repo) {
7196 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7197 1d0f4054 2021-06-17 stsp if (error == NULL)
7198 1d0f4054 2021-06-17 stsp error = close_err;
7200 0ae84acc 2022-06-15 tracey if (pack_fds) {
7201 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7202 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7203 0ae84acc 2022-06-15 tracey if (error == NULL)
7204 0ae84acc 2022-06-15 tracey error = pack_err;
7206 51a10b52 2020-12-26 stsp tog_free_refs();
7207 ffd1d5e5 2018-06-23 stsp return error;
7210 6458efa5 2020-11-24 stsp static const struct got_error *
7211 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7213 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7214 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7216 6458efa5 2020-11-24 stsp s->nrefs = 0;
7217 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7218 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7219 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7220 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7221 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7224 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7225 6458efa5 2020-11-24 stsp if (re == NULL)
7226 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7228 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7229 8924d611 2020-12-26 stsp if (re->ref == NULL)
7230 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7231 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7232 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7235 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7236 6458efa5 2020-11-24 stsp return NULL;
7240 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7242 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7244 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7245 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7246 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7247 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7252 6458efa5 2020-11-24 stsp static const struct got_error *
7253 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7255 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7256 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7258 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7259 6458efa5 2020-11-24 stsp s->repo = repo;
7261 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7262 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7264 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7266 6458efa5 2020-11-24 stsp return err;
7268 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7269 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7270 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7271 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7273 6458efa5 2020-11-24 stsp goto done;
7275 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7276 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7277 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7279 6458efa5 2020-11-24 stsp goto done;
7281 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7282 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7283 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7285 cc488aa7 2022-01-23 stsp goto done;
7287 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7288 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7289 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7291 6458efa5 2020-11-24 stsp goto done;
7294 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7295 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7296 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7297 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7298 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7301 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7302 6458efa5 2020-11-24 stsp return err;
7305 6458efa5 2020-11-24 stsp static const struct got_error *
7306 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7308 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7310 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7311 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7313 6458efa5 2020-11-24 stsp return NULL;
7316 6458efa5 2020-11-24 stsp static const struct got_error *
7317 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7318 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7320 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7321 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7322 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7323 6458efa5 2020-11-24 stsp int obj_type;
7325 c42c9805 2020-11-24 stsp *commit_id = NULL;
7327 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7329 6458efa5 2020-11-24 stsp return err;
7331 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7333 6458efa5 2020-11-24 stsp goto done;
7335 6458efa5 2020-11-24 stsp switch (obj_type) {
7336 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7337 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7339 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7340 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7342 6458efa5 2020-11-24 stsp goto done;
7343 c42c9805 2020-11-24 stsp free(obj_id);
7344 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7345 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7347 6458efa5 2020-11-24 stsp goto done;
7348 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7349 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7350 c42c9805 2020-11-24 stsp goto done;
7352 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7353 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7354 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7355 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7356 c42c9805 2020-11-24 stsp goto done;
7360 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7366 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7367 c42c9805 2020-11-24 stsp if (err) {
7368 c42c9805 2020-11-24 stsp free(*commit_id);
7369 c42c9805 2020-11-24 stsp *commit_id = NULL;
7371 c42c9805 2020-11-24 stsp return err;
7374 c42c9805 2020-11-24 stsp static const struct got_error *
7375 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7376 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7378 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7379 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7380 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7382 c42c9805 2020-11-24 stsp *new_view = NULL;
7384 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7385 c42c9805 2020-11-24 stsp if (err) {
7386 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7387 c42c9805 2020-11-24 stsp return err;
7389 c42c9805 2020-11-24 stsp return NULL;
7392 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7393 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7394 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7395 6458efa5 2020-11-24 stsp goto done;
7398 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7399 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7402 6458efa5 2020-11-24 stsp view_close(log_view);
7404 6458efa5 2020-11-24 stsp *new_view = log_view;
7405 c42c9805 2020-11-24 stsp free(commit_id);
7406 6458efa5 2020-11-24 stsp return err;
7409 ce5b7c56 2019-07-09 stsp static void
7410 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7412 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7413 34ba6917 2020-11-30 stsp int i = 0;
7415 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7418 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7419 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7420 34ba6917 2020-11-30 stsp if (re == NULL)
7422 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7423 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7427 9b058f45 2022-06-30 mark static const struct got_error *
7428 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7430 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7431 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7432 6458efa5 2020-11-24 stsp int n = 0;
7434 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7435 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7437 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7439 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7440 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7442 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7443 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7444 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7445 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7449 9b058f45 2022-06-30 mark return NULL;
7452 6458efa5 2020-11-24 stsp static const struct got_error *
7453 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7455 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7457 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7458 6458efa5 2020-11-24 stsp return NULL;
7461 6458efa5 2020-11-24 stsp static int
7462 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7464 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7466 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7470 6458efa5 2020-11-24 stsp static const struct got_error *
7471 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7473 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7474 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7476 6458efa5 2020-11-24 stsp if (!view->searching) {
7477 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7478 6458efa5 2020-11-24 stsp return NULL;
7481 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7482 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7483 6458efa5 2020-11-24 stsp if (s->selected_entry)
7484 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7486 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7487 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7489 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7490 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7492 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7493 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7496 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7497 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7498 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7499 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7501 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7504 6458efa5 2020-11-24 stsp while (1) {
7505 6458efa5 2020-11-24 stsp if (re == NULL) {
7506 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7507 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7508 6458efa5 2020-11-24 stsp return NULL;
7510 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7511 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7513 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7516 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7517 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7518 6458efa5 2020-11-24 stsp s->matched_entry = re;
7522 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7523 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7525 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7528 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7529 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7530 6458efa5 2020-11-24 stsp s->selected = 0;
7533 6458efa5 2020-11-24 stsp return NULL;
7536 6458efa5 2020-11-24 stsp static const struct got_error *
7537 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7539 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7540 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7541 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7542 6458efa5 2020-11-24 stsp char *line = NULL;
7543 6458efa5 2020-11-24 stsp wchar_t *wline;
7544 6458efa5 2020-11-24 stsp struct tog_color *tc;
7545 6458efa5 2020-11-24 stsp int width, n;
7546 6458efa5 2020-11-24 stsp int limit = view->nlines;
7548 6458efa5 2020-11-24 stsp werase(view->window);
7550 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7551 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7552 9b058f45 2022-06-30 mark --limit; /* border */
7554 6458efa5 2020-11-24 stsp if (limit == 0)
7555 6458efa5 2020-11-24 stsp return NULL;
7557 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7559 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7560 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7561 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7563 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7564 6458efa5 2020-11-24 stsp if (err) {
7565 6458efa5 2020-11-24 stsp free(line);
7566 6458efa5 2020-11-24 stsp return err;
7568 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7569 6458efa5 2020-11-24 stsp wstandout(view->window);
7570 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7571 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7572 6458efa5 2020-11-24 stsp wstandend(view->window);
7573 6458efa5 2020-11-24 stsp free(wline);
7574 6458efa5 2020-11-24 stsp wline = NULL;
7575 6458efa5 2020-11-24 stsp free(line);
7576 6458efa5 2020-11-24 stsp line = NULL;
7577 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7578 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7579 6458efa5 2020-11-24 stsp if (--limit <= 0)
7580 6458efa5 2020-11-24 stsp return NULL;
7583 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7584 6458efa5 2020-11-24 stsp char *line = NULL;
7585 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7587 b4996bee 2022-06-16 stsp if (s->show_date) {
7588 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7589 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7590 b4996bee 2022-06-16 stsp struct got_object_id *id;
7591 b4996bee 2022-06-16 stsp struct tm tm;
7594 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7596 b4996bee 2022-06-16 stsp return err;
7597 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7598 b4996bee 2022-06-16 stsp if (err) {
7599 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7601 b4996bee 2022-06-16 stsp return err;
7603 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7605 b4996bee 2022-06-16 stsp if (err) {
7607 b4996bee 2022-06-16 stsp return err;
7609 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7610 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7612 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7613 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7616 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7617 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7618 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7619 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7621 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7622 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7623 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7624 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7625 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7626 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7627 6458efa5 2020-11-24 stsp struct got_object_id *id;
7628 6458efa5 2020-11-24 stsp char *id_str;
7629 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7631 6458efa5 2020-11-24 stsp return err;
7632 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7633 6458efa5 2020-11-24 stsp if (err) {
7635 6458efa5 2020-11-24 stsp return err;
7637 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7638 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7639 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7641 6458efa5 2020-11-24 stsp free(id_str);
7642 6458efa5 2020-11-24 stsp return err;
7645 6458efa5 2020-11-24 stsp free(id_str);
7646 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7647 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7648 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7650 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7652 6458efa5 2020-11-24 stsp if (err) {
7653 6458efa5 2020-11-24 stsp free(line);
7654 6458efa5 2020-11-24 stsp return err;
7656 6458efa5 2020-11-24 stsp if (n == s->selected) {
7657 6458efa5 2020-11-24 stsp if (view->focussed)
7658 6458efa5 2020-11-24 stsp wstandout(view->window);
7659 6458efa5 2020-11-24 stsp s->selected_entry = re;
7661 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7663 6458efa5 2020-11-24 stsp wattr_on(view->window,
7664 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7665 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7667 6458efa5 2020-11-24 stsp wattr_off(view->window,
7668 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7669 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7670 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7671 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7672 6458efa5 2020-11-24 stsp wstandend(view->window);
7673 6458efa5 2020-11-24 stsp free(line);
7674 6458efa5 2020-11-24 stsp free(wline);
7675 6458efa5 2020-11-24 stsp wline = NULL;
7677 6458efa5 2020-11-24 stsp s->ndisplayed++;
7678 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7681 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7684 9b058f45 2022-06-30 mark view_border(view);
7685 6458efa5 2020-11-24 stsp return err;
7688 6458efa5 2020-11-24 stsp static const struct got_error *
7689 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7690 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7692 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7693 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7694 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7696 c42c9805 2020-11-24 stsp *new_view = NULL;
7698 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7699 c42c9805 2020-11-24 stsp if (err) {
7700 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7701 c42c9805 2020-11-24 stsp return err;
7703 c42c9805 2020-11-24 stsp return NULL;
7707 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7708 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7709 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7710 c42c9805 2020-11-24 stsp goto done;
7713 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7714 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7716 c42c9805 2020-11-24 stsp goto done;
7718 c42c9805 2020-11-24 stsp *new_view = tree_view;
7720 c42c9805 2020-11-24 stsp free(commit_id);
7721 c42c9805 2020-11-24 stsp return err;
7723 c42c9805 2020-11-24 stsp static const struct got_error *
7724 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7726 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7727 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7728 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7729 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7730 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7732 6458efa5 2020-11-24 stsp switch (ch) {
7734 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7735 640cd7ff 2022-06-22 mark view->count = 0;
7738 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7739 640cd7ff 2022-06-22 mark view->count = 0;
7742 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7743 640cd7ff 2022-06-22 mark view->count = 0;
7744 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7745 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7746 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7749 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7750 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7751 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7754 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7755 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7757 6458efa5 2020-11-24 stsp case KEY_ENTER:
7758 6458efa5 2020-11-24 stsp case '\r':
7759 640cd7ff 2022-06-22 mark view->count = 0;
7760 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7762 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7763 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7765 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7766 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7770 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7771 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7772 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7777 e78dc838 2020-12-04 stsp view->focussed = 0;
7778 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7779 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7780 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7781 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7782 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
7783 6458efa5 2020-11-24 stsp err = view_close_child(view);
7785 6458efa5 2020-11-24 stsp return err;
7786 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7789 e78dc838 2020-12-04 stsp view->focus_child = 1;
7791 6458efa5 2020-11-24 stsp *new_view = log_view;
7794 640cd7ff 2022-06-22 mark view->count = 0;
7795 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7797 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7798 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
7799 49b24ee5 2022-07-03 mark err = browse_ref_tree(&tree_view, begin_y, begin_x,
7800 49b24ee5 2022-07-03 mark s->selected_entry, s->repo);
7801 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7803 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7804 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7805 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
7809 e78dc838 2020-12-04 stsp view->focussed = 0;
7810 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7811 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
7812 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
7813 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7814 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
7815 c42c9805 2020-11-24 stsp err = view_close_child(view);
7817 c42c9805 2020-11-24 stsp return err;
7818 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7821 e78dc838 2020-12-04 stsp view->focus_child = 1;
7823 c42c9805 2020-11-24 stsp *new_view = tree_view;
7825 e4526bf5 2021-09-03 naddy case 'g':
7826 e4526bf5 2021-09-03 naddy case KEY_HOME:
7827 e4526bf5 2021-09-03 naddy s->selected = 0;
7828 640cd7ff 2022-06-22 mark view->count = 0;
7829 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7831 e4526bf5 2021-09-03 naddy case 'G':
7832 9b058f45 2022-06-30 mark case KEY_END: {
7833 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7835 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7836 9b058f45 2022-06-30 mark --eos; /* border */
7837 e4526bf5 2021-09-03 naddy s->selected = 0;
7838 640cd7ff 2022-06-22 mark view->count = 0;
7839 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7840 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7841 e4526bf5 2021-09-03 naddy if (re == NULL)
7843 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7844 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7846 e4526bf5 2021-09-03 naddy if (n > 0)
7847 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7851 6458efa5 2020-11-24 stsp case KEY_UP:
7852 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7853 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7854 6458efa5 2020-11-24 stsp s->selected--;
7857 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7858 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7859 640cd7ff 2022-06-22 mark view->count = 0;
7861 83cc4199 2022-06-13 stsp case CTRL('u'):
7863 83cc4199 2022-06-13 stsp nscroll /= 2;
7864 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7865 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7866 6458efa5 2020-11-24 stsp case CTRL('b'):
7868 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7869 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7870 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7871 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7872 640cd7ff 2022-06-22 mark view->count = 0;
7875 6458efa5 2020-11-24 stsp case KEY_DOWN:
7876 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7877 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7878 6458efa5 2020-11-24 stsp s->selected++;
7881 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7882 6458efa5 2020-11-24 stsp /* can't scroll any further */
7883 640cd7ff 2022-06-22 mark view->count = 0;
7886 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7888 83cc4199 2022-06-13 stsp case CTRL('d'):
7890 83cc4199 2022-06-13 stsp nscroll /= 2;
7891 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7892 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7893 6458efa5 2020-11-24 stsp case CTRL('f'):
7895 48bb96f0 2022-06-20 naddy case ' ':
7896 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7897 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7898 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7899 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7900 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7901 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7902 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7903 640cd7ff 2022-06-22 mark view->count = 0;
7906 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7908 6458efa5 2020-11-24 stsp case CTRL('l'):
7909 640cd7ff 2022-06-22 mark view->count = 0;
7910 8924d611 2020-12-26 stsp tog_free_refs();
7911 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7914 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7915 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7917 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7918 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7919 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7922 640cd7ff 2022-06-22 mark view->count = 0;
7926 6458efa5 2020-11-24 stsp return err;
7929 6458efa5 2020-11-24 stsp __dead static void
7930 6458efa5 2020-11-24 stsp usage_ref(void)
7933 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7934 6458efa5 2020-11-24 stsp getprogname());
7938 6458efa5 2020-11-24 stsp static const struct got_error *
7939 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7941 6458efa5 2020-11-24 stsp const struct got_error *error;
7942 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7943 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7944 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7946 6458efa5 2020-11-24 stsp struct tog_view *view;
7947 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7949 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7950 6458efa5 2020-11-24 stsp switch (ch) {
7952 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7953 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7954 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7958 e99e2d15 2020-11-24 naddy usage_ref();
7959 6458efa5 2020-11-24 stsp /* NOTREACHED */
7963 6458efa5 2020-11-24 stsp argc -= optind;
7964 6458efa5 2020-11-24 stsp argv += optind;
7966 6458efa5 2020-11-24 stsp if (argc > 1)
7967 e99e2d15 2020-11-24 naddy usage_ref();
7969 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7970 0ae84acc 2022-06-15 tracey if (error != NULL)
7971 0ae84acc 2022-06-15 tracey goto done;
7973 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7974 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7975 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7976 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7977 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7978 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7979 c156c7a4 2020-12-18 stsp goto done;
7980 6458efa5 2020-11-24 stsp if (worktree)
7981 6458efa5 2020-11-24 stsp repo_path =
7982 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7984 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7985 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7986 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7987 c156c7a4 2020-12-18 stsp goto done;
7991 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7992 6458efa5 2020-11-24 stsp if (error != NULL)
7993 6458efa5 2020-11-24 stsp goto done;
7995 6458efa5 2020-11-24 stsp init_curses();
7997 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7998 51a10b52 2020-12-26 stsp if (error)
7999 51a10b52 2020-12-26 stsp goto done;
8001 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8002 6458efa5 2020-11-24 stsp if (error)
8003 6458efa5 2020-11-24 stsp goto done;
8005 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8006 6458efa5 2020-11-24 stsp if (view == NULL) {
8007 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8008 6458efa5 2020-11-24 stsp goto done;
8011 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8012 6458efa5 2020-11-24 stsp if (error)
8013 6458efa5 2020-11-24 stsp goto done;
8015 6458efa5 2020-11-24 stsp if (worktree) {
8016 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8017 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8018 6458efa5 2020-11-24 stsp worktree = NULL;
8020 6458efa5 2020-11-24 stsp error = view_loop(view);
8022 6458efa5 2020-11-24 stsp free(repo_path);
8023 6458efa5 2020-11-24 stsp free(cwd);
8024 1d0f4054 2021-06-17 stsp if (repo) {
8025 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8026 1d0f4054 2021-06-17 stsp if (close_err)
8027 1d0f4054 2021-06-17 stsp error = close_err;
8029 0ae84acc 2022-06-15 tracey if (pack_fds) {
8030 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8031 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8032 0ae84acc 2022-06-15 tracey if (error == NULL)
8033 0ae84acc 2022-06-15 tracey error = pack_err;
8035 51a10b52 2020-12-26 stsp tog_free_refs();
8036 6458efa5 2020-11-24 stsp return error;
8040 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
8041 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
8043 6458efa5 2020-11-24 stsp static void
8044 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
8046 9b058f45 2022-06-30 mark switch (view->type) {
8047 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8048 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8049 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
8050 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
8054 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
8055 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
8057 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
8058 9b058f45 2022-06-30 mark s->selected_line += view->offset;
8061 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
8062 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
8063 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
8065 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
8066 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
8067 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
8069 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
8070 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
8071 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
8077 9b058f45 2022-06-30 mark view->offset = 0;
8081 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
8082 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
8084 9b058f45 2022-06-30 mark static const struct got_error *
8085 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
8087 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
8088 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
8089 9b058f45 2022-06-30 mark int *selected = NULL;
8090 9b058f45 2022-06-30 mark int header, offset;
8092 9b058f45 2022-06-30 mark switch (view->type) {
8093 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8094 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8095 9b058f45 2022-06-30 mark header = 3;
8096 9b058f45 2022-06-30 mark scrolld = NULL;
8097 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8098 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8099 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8100 9b058f45 2022-06-30 mark s->selected_line -= offset;
8101 9b058f45 2022-06-30 mark view->offset = offset;
8105 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8106 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8107 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8108 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8109 9b058f45 2022-06-30 mark selected = &s->selected;
8112 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8113 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8114 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8115 9b058f45 2022-06-30 mark header = 3;
8116 9b058f45 2022-06-30 mark selected = &s->selected;
8119 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8120 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8121 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8122 9b058f45 2022-06-30 mark header = 5;
8123 9b058f45 2022-06-30 mark selected = &s->selected;
8127 9b058f45 2022-06-30 mark selected = NULL;
8128 9b058f45 2022-06-30 mark scrolld = NULL;
8129 9b058f45 2022-06-30 mark header = 0;
8133 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8134 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8135 9b058f45 2022-06-30 mark view->offset = offset;
8136 9b058f45 2022-06-30 mark if (scrolld && offset) {
8137 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8138 9b058f45 2022-06-30 mark *selected -= offset;
8142 9b058f45 2022-06-30 mark return err;
8145 9b058f45 2022-06-30 mark static void
8146 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8150 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8151 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8152 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8153 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8155 6879ba42 2020-10-01 naddy fputc('\n', fp);
8158 4ed7e80c 2018-05-20 stsp __dead static void
8159 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8161 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8163 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8164 6879ba42 2020-10-01 naddy getprogname());
8165 6879ba42 2020-10-01 naddy if (hflag) {
8166 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8167 6879ba42 2020-10-01 naddy list_commands(fp);
8169 6879ba42 2020-10-01 naddy exit(status);
8172 c2301be8 2018-04-30 stsp static char **
8173 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8175 ee85c5e8 2020-02-29 stsp va_list ap;
8176 c2301be8 2018-04-30 stsp char **argv;
8179 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8181 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8182 c2301be8 2018-04-30 stsp if (argv == NULL)
8183 c2301be8 2018-04-30 stsp err(1, "calloc");
8184 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8185 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8186 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8187 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8190 ee85c5e8 2020-02-29 stsp va_end(ap);
8191 c2301be8 2018-04-30 stsp return argv;
8195 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8196 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8197 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8198 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8199 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8201 ee85c5e8 2020-02-29 stsp static const struct got_error *
8202 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8204 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8205 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8206 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8207 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8208 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8209 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8210 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8211 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8212 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8214 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8215 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8216 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8218 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8219 0ae84acc 2022-06-15 tracey if (error != NULL)
8220 0ae84acc 2022-06-15 tracey goto done;
8222 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8223 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8224 ee85c5e8 2020-02-29 stsp goto done;
8226 ee85c5e8 2020-02-29 stsp if (worktree)
8227 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8229 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8230 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8231 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8232 ee85c5e8 2020-02-29 stsp goto done;
8235 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8236 ee85c5e8 2020-02-29 stsp if (error != NULL)
8237 ee85c5e8 2020-02-29 stsp goto done;
8239 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8240 ee85c5e8 2020-02-29 stsp repo, worktree);
8241 ee85c5e8 2020-02-29 stsp if (error)
8242 ee85c5e8 2020-02-29 stsp goto done;
8244 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8245 84de9106 2020-12-26 stsp if (error)
8246 84de9106 2020-12-26 stsp goto done;
8247 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8248 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8249 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8250 ee85c5e8 2020-02-29 stsp if (error)
8251 ee85c5e8 2020-02-29 stsp goto done;
8253 ee85c5e8 2020-02-29 stsp if (worktree) {
8254 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8255 ee85c5e8 2020-02-29 stsp worktree = NULL;
8258 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8259 a44927cc 2022-04-07 stsp if (error)
8260 a44927cc 2022-04-07 stsp goto done;
8262 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8263 ee85c5e8 2020-02-29 stsp if (error) {
8264 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8265 ee85c5e8 2020-02-29 stsp goto done;
8266 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8267 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8268 6879ba42 2020-10-01 naddy usage(1, 1);
8269 ee85c5e8 2020-02-29 stsp /* not reached */
8272 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8273 1d0f4054 2021-06-17 stsp if (error == NULL)
8274 1d0f4054 2021-06-17 stsp error = close_err;
8275 ee85c5e8 2020-02-29 stsp repo = NULL;
8277 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8278 ee85c5e8 2020-02-29 stsp if (error)
8279 ee85c5e8 2020-02-29 stsp goto done;
8281 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8283 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8284 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8286 1d0f4054 2021-06-17 stsp if (repo) {
8287 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8288 1d0f4054 2021-06-17 stsp if (error == NULL)
8289 1d0f4054 2021-06-17 stsp error = close_err;
8291 a44927cc 2022-04-07 stsp if (commit)
8292 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8293 ee85c5e8 2020-02-29 stsp if (worktree)
8294 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8295 0ae84acc 2022-06-15 tracey if (pack_fds) {
8296 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8297 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8298 0ae84acc 2022-06-15 tracey if (error == NULL)
8299 0ae84acc 2022-06-15 tracey error = pack_err;
8302 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8303 ee85c5e8 2020-02-29 stsp free(commit_id);
8304 ee85c5e8 2020-02-29 stsp free(cwd);
8305 ee85c5e8 2020-02-29 stsp free(repo_path);
8306 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8307 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8309 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8310 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8311 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8313 87670572 2020-12-26 naddy tog_free_refs();
8314 ee85c5e8 2020-02-29 stsp return error;
8318 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8320 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8321 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8322 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8323 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8324 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8325 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8326 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8328 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8330 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8332 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8333 9f7d7167 2018-04-29 stsp switch (ch) {
8335 9f7d7167 2018-04-29 stsp hflag = 1;
8338 53ccebc2 2019-07-30 stsp Vflag = 1;
8341 6879ba42 2020-10-01 naddy usage(hflag, 1);
8342 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8346 9f7d7167 2018-04-29 stsp argc -= optind;
8347 9f7d7167 2018-04-29 stsp argv += optind;
8348 9814e6a3 2020-09-27 naddy optind = 1;
8349 c2301be8 2018-04-30 stsp optreset = 1;
8351 53ccebc2 2019-07-30 stsp if (Vflag) {
8352 53ccebc2 2019-07-30 stsp got_version_print_str();
8353 6879ba42 2020-10-01 naddy return 0;
8356 4010e238 2020-12-04 stsp #ifndef PROFILE
8357 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8358 4010e238 2020-12-04 stsp NULL) == -1)
8359 4010e238 2020-12-04 stsp err(1, "pledge");
8362 c2301be8 2018-04-30 stsp if (argc == 0) {
8363 f29d3e89 2018-06-23 stsp if (hflag)
8364 6879ba42 2020-10-01 naddy usage(hflag, 0);
8365 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8366 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8368 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8372 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8373 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8374 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8375 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8376 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8382 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8383 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8384 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8385 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8386 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8387 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8390 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8391 ee85c5e8 2020-02-29 stsp if (argc != 1)
8392 6879ba42 2020-10-01 naddy usage(0, 1);
8393 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8394 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8396 ee85c5e8 2020-02-29 stsp if (hflag)
8397 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8399 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8403 b46c1e04 2020-09-20 naddy putchar('\n');
8404 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8406 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8407 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8408 a2f4a359 2020-02-28 stsp free(cmd_argv);
8411 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8412 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);