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 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
336 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
339 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
341 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
342 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
343 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
344 1a76625f 2018-10-22 stsp int commits_needed;
345 fb280deb 2021-08-30 stsp int load_all;
346 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
347 1a76625f 2018-10-22 stsp struct commit_queue *commits;
348 1a76625f 2018-10-22 stsp const char *in_repo_path;
349 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
350 1a76625f 2018-10-22 stsp struct got_repository *repo;
351 74467cc8 2022-06-15 stsp int *pack_fds;
352 1a76625f 2018-10-22 stsp int log_complete;
353 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
354 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
355 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
356 13add988 2019-10-15 stsp int *searching;
357 13add988 2019-10-15 stsp int *search_next_done;
358 13add988 2019-10-15 stsp regex_t *regex;
361 1a76625f 2018-10-22 stsp struct tog_log_view_state {
362 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
363 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
365 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
366 b01e7d3b 2018-08-04 stsp int selected;
367 b01e7d3b 2018-08-04 stsp char *in_repo_path;
368 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
369 b672a97a 2020-01-27 stsp int log_branches;
370 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
371 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
372 1a76625f 2018-10-22 stsp sig_atomic_t quit;
373 1a76625f 2018-10-22 stsp pthread_t thread;
374 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
375 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
376 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
377 11b20872 2019-11-08 stsp struct tog_colors colors;
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
381 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
385 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
388 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
389 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
390 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
391 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
392 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
394 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
396 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
397 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
398 e9424729 2018-08-04 stsp int nlines;
400 e9424729 2018-08-04 stsp struct tog_view *view;
401 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
405 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
406 e9424729 2018-08-04 stsp const char *path;
407 e9424729 2018-08-04 stsp struct got_repository *repo;
408 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
409 e9424729 2018-08-04 stsp int *complete;
410 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
411 fc06ba56 2019-08-22 stsp void *cancel_arg;
414 e9424729 2018-08-04 stsp struct tog_blame {
416 be659d10 2020-11-18 stsp off_t filesize;
417 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
418 6fcac457 2018-11-19 stsp int nlines;
419 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
420 e9424729 2018-08-04 stsp pthread_t thread;
421 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
422 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
423 e9424729 2018-08-04 stsp const char *path;
424 0ae84acc 2022-06-15 tracey int *pack_fds;
427 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
428 7cbe629d 2018-08-04 stsp int first_displayed_line;
429 7cbe629d 2018-08-04 stsp int last_displayed_line;
430 7cbe629d 2018-08-04 stsp int selected_line;
431 7cbe629d 2018-08-04 stsp int blame_complete;
434 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
435 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
436 e5a0f69f 2018-08-18 stsp char *path;
437 7cbe629d 2018-08-04 stsp struct got_repository *repo;
438 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
439 e9424729 2018-08-04 stsp struct tog_blame blame;
440 6c4c42e0 2019-06-24 stsp int matched_line;
441 11b20872 2019-11-08 stsp struct tog_colors colors;
444 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
445 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
446 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
447 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
448 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
449 ad80ab7b 2018-08-04 stsp int selected;
452 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
454 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
455 ad80ab7b 2018-08-04 stsp char *tree_label;
456 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
457 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
458 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
459 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
460 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
461 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
462 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
463 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
464 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
465 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
466 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
467 6458efa5 2020-11-24 stsp struct tog_colors colors;
470 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
471 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
472 6458efa5 2020-11-24 stsp struct got_reference *ref;
476 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
478 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
479 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
481 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
482 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
483 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
484 6458efa5 2020-11-24 stsp struct got_repository *repo;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
486 bddb1296 2019-11-08 stsp struct tog_colors colors;
490 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
492 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
493 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
494 669b5ffa 2018-10-07 stsp * there is enough screen estate.
496 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
497 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
499 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
500 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
501 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
503 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
504 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
506 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
508 cc3c9aac 2018-08-01 stsp struct tog_view {
509 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
510 26ed57b2 2018-05-19 stsp WINDOW *window;
511 26ed57b2 2018-05-19 stsp PANEL *panel;
512 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
513 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
514 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
515 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
516 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
517 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
519 669b5ffa 2018-10-07 stsp struct tog_view *parent;
520 669b5ffa 2018-10-07 stsp struct tog_view *child;
523 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
524 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
525 e78dc838 2020-12-04 stsp * between parent and child.
526 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
527 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
528 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
529 e78dc838 2020-12-04 stsp * situations.
531 e78dc838 2020-12-04 stsp int focus_child;
533 9b058f45 2022-06-30 mark enum tog_view_mode mode;
534 5dc9f4bc 2018-08-04 stsp /* type-specific state */
535 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
537 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
538 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
539 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
540 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
541 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
544 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
545 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
546 e78dc838 2020-12-04 stsp struct tog_view *, int);
547 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
548 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
550 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
551 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
552 c0c4acc8 2021-01-24 stsp int search_started;
553 60493ae3 2019-06-20 stsp int searching;
554 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
555 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
556 60493ae3 2019-06-20 stsp int search_next_done;
557 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
558 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
559 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
560 1803e47f 2019-06-22 stsp regex_t regex;
561 41605754 2020-11-12 stsp regmatch_t regmatch;
564 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
565 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
566 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
567 78756c87 2020-11-24 stsp struct got_repository *);
568 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
569 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
570 e78dc838 2020-12-04 stsp struct tog_view *, int);
571 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
572 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
573 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
574 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
576 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
577 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
578 78756c87 2020-11-24 stsp const char *, const char *, int);
579 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
583 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
584 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
587 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
588 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
593 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
594 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
596 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
597 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
598 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
600 e78dc838 2020-12-04 stsp struct tog_view *, int);
601 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
602 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
603 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
605 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
606 6458efa5 2020-11-24 stsp struct got_repository *);
607 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
608 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
609 e78dc838 2020-12-04 stsp struct tog_view *, int);
610 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
611 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
614 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
615 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
616 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
617 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
618 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
620 25791caa 2018-10-24 stsp static void
621 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
623 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
626 83baff54 2019-08-12 stsp static void
627 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
629 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
632 61266923 2020-01-14 stsp static void
633 61266923 2020-01-14 stsp tog_sigcont(int signo)
635 61266923 2020-01-14 stsp tog_sigcont_received = 1;
638 2497f032 2022-05-31 stsp static void
639 2497f032 2022-05-31 stsp tog_sigint(int signo)
641 2497f032 2022-05-31 stsp tog_sigint_received = 1;
644 2497f032 2022-05-31 stsp static void
645 2497f032 2022-05-31 stsp tog_sigterm(int signo)
647 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
651 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
653 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
654 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
657 e5a0f69f 2018-08-18 stsp static const struct got_error *
658 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
660 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
662 669b5ffa 2018-10-07 stsp if (view->child) {
663 669b5ffa 2018-10-07 stsp view_close(view->child);
664 669b5ffa 2018-10-07 stsp view->child = NULL;
666 e5a0f69f 2018-08-18 stsp if (view->close)
667 e5a0f69f 2018-08-18 stsp err = view->close(view);
668 ea5e7bb5 2018-08-01 stsp if (view->panel)
669 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
670 ea5e7bb5 2018-08-01 stsp if (view->window)
671 ea5e7bb5 2018-08-01 stsp delwin(view->window);
672 ea5e7bb5 2018-08-01 stsp free(view);
673 e5a0f69f 2018-08-18 stsp return err;
676 ea5e7bb5 2018-08-01 stsp static struct tog_view *
677 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
678 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
680 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
682 ea5e7bb5 2018-08-01 stsp if (view == NULL)
683 ea5e7bb5 2018-08-01 stsp return NULL;
685 d6b05b5b 2018-08-04 stsp view->type = type;
686 f7d12f7e 2018-08-01 stsp view->lines = LINES;
687 f7d12f7e 2018-08-01 stsp view->cols = COLS;
688 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
689 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
690 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
691 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
692 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
693 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
694 96a765a8 2018-08-04 stsp view_close(view);
695 ea5e7bb5 2018-08-01 stsp return NULL;
697 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
698 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
699 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
700 96a765a8 2018-08-04 stsp view_close(view);
701 ea5e7bb5 2018-08-01 stsp return NULL;
704 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
705 ea5e7bb5 2018-08-01 stsp return view;
709 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
711 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
713 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
716 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
718 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
720 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
723 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
725 5c60c32a 2018-10-18 stsp static const struct got_error *
726 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
728 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
730 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
731 9b058f45 2022-06-30 mark view->begin_y = view_split_begin_y(view->nlines);
732 9b058f45 2022-06-30 mark view->begin_x = 0;
734 9b058f45 2022-06-30 mark view->begin_x = view_split_begin_x(0);
735 9b058f45 2022-06-30 mark view->begin_y = 0;
737 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
738 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
739 5c60c32a 2018-10-18 stsp view->lines = LINES;
740 5c60c32a 2018-10-18 stsp view->cols = COLS;
741 5c60c32a 2018-10-18 stsp err = view_resize(view);
743 5c60c32a 2018-10-18 stsp return err;
745 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
746 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
748 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
749 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
751 5c60c32a 2018-10-18 stsp return NULL;
754 5c60c32a 2018-10-18 stsp static const struct got_error *
755 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
757 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
759 5c60c32a 2018-10-18 stsp view->begin_x = 0;
760 5c60c32a 2018-10-18 stsp view->begin_y = 0;
761 5c60c32a 2018-10-18 stsp view->nlines = LINES;
762 5c60c32a 2018-10-18 stsp view->ncols = COLS;
763 5c60c32a 2018-10-18 stsp view->lines = LINES;
764 5c60c32a 2018-10-18 stsp view->cols = COLS;
765 5c60c32a 2018-10-18 stsp err = view_resize(view);
767 5c60c32a 2018-10-18 stsp return err;
769 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
770 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
772 5c60c32a 2018-10-18 stsp return NULL;
776 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
778 5c60c32a 2018-10-18 stsp return view->parent == NULL;
782 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
784 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
788 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
790 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
793 9b058f45 2022-06-30 mark static void
794 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
796 9b058f45 2022-06-30 mark PANEL *panel;
797 9b058f45 2022-06-30 mark const struct tog_view *view_above;
799 9b058f45 2022-06-30 mark if (view->parent)
800 9b058f45 2022-06-30 mark return view_border(view->parent);
802 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
803 9b058f45 2022-06-30 mark if (panel == NULL)
806 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
807 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
808 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
809 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
810 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
812 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
813 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
816 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
817 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
818 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
820 4d8c2215 2018-08-19 stsp static const struct got_error *
821 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
823 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
824 9b058f45 2022-06-30 mark int dif, nlines, ncols;
826 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
828 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
829 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
831 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
832 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
833 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
835 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
837 4dd27a72 2022-06-29 stsp if (view->child) {
838 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
840 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
841 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
842 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
843 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
844 0dbbbe90 2022-06-17 op ncols = COLS;
846 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
847 5c60c32a 2018-10-18 stsp if (view->child->focussed)
848 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
850 5c60c32a 2018-10-18 stsp show_panel(view->panel);
852 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
854 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
855 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
858 9b058f45 2022-06-30 mark * Request commits if terminal height was increased in a log
859 9b058f45 2022-06-30 mark * view so we have enough commits loaded to populate the view.
861 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG && dif > 0) {
862 9b058f45 2022-06-30 mark struct tog_log_view_state *ts = &view->state.log;
864 9b058f45 2022-06-30 mark if (ts->commits.ncommits < ts->selected_entry->idx +
865 9b058f45 2022-06-30 mark view->lines - ts->selected) {
866 9b058f45 2022-06-30 mark view->nscrolled = ts->selected_entry->idx +
867 9b058f45 2022-06-30 mark view->lines - ts->selected -
868 9b058f45 2022-06-30 mark ts->commits.ncommits + dif;
869 9b058f45 2022-06-30 mark err = request_log_commits(view);
871 9b058f45 2022-06-30 mark return err;
876 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
877 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
878 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
881 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
883 9b058f45 2022-06-30 mark return err;
884 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
885 9b058f45 2022-06-30 mark err = offset_selection_down(view);
887 9b058f45 2022-06-30 mark return err;
889 9b058f45 2022-06-30 mark view_border(view);
890 9b058f45 2022-06-30 mark update_panels();
891 9b058f45 2022-06-30 mark doupdate();
892 9b058f45 2022-06-30 mark show_panel(view->child->panel);
893 9b058f45 2022-06-30 mark nlines = view->nlines;
895 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
896 0dbbbe90 2022-06-17 op ncols = COLS;
898 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
899 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
900 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
901 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
902 0dbbbe90 2022-06-17 op wclear(view->window);
904 0dbbbe90 2022-06-17 op view->nlines = nlines;
905 0dbbbe90 2022-06-17 op view->ncols = ncols;
906 0dbbbe90 2022-06-17 op view->lines = LINES;
907 0dbbbe90 2022-06-17 op view->cols = COLS;
909 5c60c32a 2018-10-18 stsp return NULL;
912 669b5ffa 2018-10-07 stsp static const struct got_error *
913 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
915 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
917 669b5ffa 2018-10-07 stsp if (view->child == NULL)
918 669b5ffa 2018-10-07 stsp return NULL;
920 669b5ffa 2018-10-07 stsp err = view_close(view->child);
921 669b5ffa 2018-10-07 stsp view->child = NULL;
922 669b5ffa 2018-10-07 stsp return err;
925 0dbbbe90 2022-06-17 op static const struct got_error *
926 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
928 669b5ffa 2018-10-07 stsp view->child = child;
929 669b5ffa 2018-10-07 stsp child->parent = view;
931 0dbbbe90 2022-06-17 op return view_resize(view);
934 34bc9ec9 2019-02-22 stsp static void
935 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
937 25791caa 2018-10-24 stsp int cols, lines;
938 25791caa 2018-10-24 stsp struct winsize size;
940 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
941 25791caa 2018-10-24 stsp cols = 80; /* Default */
942 25791caa 2018-10-24 stsp lines = 24;
944 25791caa 2018-10-24 stsp cols = size.ws_col;
945 25791caa 2018-10-24 stsp lines = size.ws_row;
947 25791caa 2018-10-24 stsp resize_term(lines, cols);
950 2b49a8ae 2019-06-22 stsp static const struct got_error *
951 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
953 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
954 9b058f45 2022-06-30 mark struct tog_view *v = view;
955 2b49a8ae 2019-06-22 stsp char pattern[1024];
958 c0c4acc8 2021-01-24 stsp if (view->search_started) {
959 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
960 c0c4acc8 2021-01-24 stsp view->searching = 0;
961 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
963 c0c4acc8 2021-01-24 stsp view->search_started = 0;
965 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
966 2b49a8ae 2019-06-22 stsp return NULL;
968 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
969 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
970 9b058f45 2022-06-30 mark v = view->child;
972 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
973 9b058f45 2022-06-30 mark wclrtoeol(v->window);
975 2b49a8ae 2019-06-22 stsp nocbreak();
977 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
978 9b058f45 2022-06-30 mark wrefresh(v->window);
981 2b49a8ae 2019-06-22 stsp if (ret == ERR)
982 2b49a8ae 2019-06-22 stsp return NULL;
984 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
985 7c32bd05 2019-06-22 stsp err = view->search_start(view);
987 7c32bd05 2019-06-22 stsp regfree(&view->regex);
988 7c32bd05 2019-06-22 stsp return err;
990 c0c4acc8 2021-01-24 stsp view->search_started = 1;
991 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
992 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
993 2b49a8ae 2019-06-22 stsp view->search_next(view);
996 2b49a8ae 2019-06-22 stsp return NULL;
1000 640cd7ff 2022-06-22 mark * Compute view->count from numeric user input. User has five-tenths of a
1001 640cd7ff 2022-06-22 mark * second to follow each numeric keypress with another number to form count.
1002 640cd7ff 2022-06-22 mark * Return first non-numeric input or ERR and assign total to view->count.
1003 640cd7ff 2022-06-22 mark * XXX Should we add support for user-defined timeout?
1005 640cd7ff 2022-06-22 mark static int
1006 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1008 9b058f45 2022-06-30 mark struct tog_view *v = view;
1009 9b058f45 2022-06-30 mark int x, n = 0;
1011 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1012 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
1013 9b058f45 2022-06-30 mark v = view->child;
1014 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1015 9b058f45 2022-06-30 mark v = view->parent;
1017 640cd7ff 2022-06-22 mark view->count = 0;
1018 640cd7ff 2022-06-22 mark halfdelay(5); /* block for half a second */
1019 9b058f45 2022-06-30 mark wattron(v->window, A_BOLD);
1020 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1021 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1022 9b058f45 2022-06-30 mark waddch(v->window, ':');
1025 9b058f45 2022-06-30 mark x = getcurx(v->window);
1026 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1027 9b058f45 2022-06-30 mark waddch(v->window, c);
1028 9b058f45 2022-06-30 mark wrefresh(v->window);
1032 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1033 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1035 640cd7ff 2022-06-22 mark if (n >= 9999999)
1036 640cd7ff 2022-06-22 mark n = 9999999;
1038 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1039 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1041 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1042 640cd7ff 2022-06-22 mark view->count = n;
1044 9b058f45 2022-06-30 mark wattroff(v->window, A_BOLD);
1045 640cd7ff 2022-06-22 mark cbreak(); /* return to blocking */
1049 0cf4efb1 2018-09-29 stsp static const struct got_error *
1050 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1051 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1053 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1054 669b5ffa 2018-10-07 stsp struct tog_view *v;
1055 1a76625f 2018-10-22 stsp int ch, errcode;
1057 e5a0f69f 2018-08-18 stsp *new = NULL;
1059 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1060 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1061 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1062 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1063 640cd7ff 2022-06-22 mark view->count = 0;
1066 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1067 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1068 82954512 2020-02-03 stsp if (errcode)
1069 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1070 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1071 3da8ef85 2021-09-21 stsp sched_yield();
1072 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1073 82954512 2020-02-03 stsp if (errcode)
1074 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1075 82954512 2020-02-03 stsp "pthread_mutex_lock");
1076 60493ae3 2019-06-20 stsp view->search_next(view);
1077 60493ae3 2019-06-20 stsp return NULL;
1080 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
1081 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1082 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1083 1a76625f 2018-10-22 stsp if (errcode)
1084 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1085 640cd7ff 2022-06-22 mark /* If we have an unfinished count, don't get a new key map. */
1086 640cd7ff 2022-06-22 mark ch = view->ch;
1087 640cd7ff 2022-06-22 mark if ((view->count && --view->count == 0) || !view->count) {
1088 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1089 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1090 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1092 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1093 1a76625f 2018-10-22 stsp if (errcode)
1094 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1095 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
1097 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1098 25791caa 2018-10-24 stsp tog_resizeterm();
1099 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1100 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1101 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1102 25791caa 2018-10-24 stsp err = view_resize(v);
1104 25791caa 2018-10-24 stsp return err;
1105 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1107 25791caa 2018-10-24 stsp return err;
1108 cdfcfb03 2020-12-06 stsp if (v->child) {
1109 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1111 cdfcfb03 2020-12-06 stsp return err;
1112 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1113 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1115 cdfcfb03 2020-12-06 stsp return err;
1120 e5a0f69f 2018-08-18 stsp switch (ch) {
1122 640cd7ff 2022-06-22 mark view->count = 0;
1123 1e37a5c2 2019-05-12 jcs if (view->child) {
1124 e78dc838 2020-12-04 stsp view->focussed = 0;
1125 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1126 e78dc838 2020-12-04 stsp view->focus_child = 1;
1127 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1128 e78dc838 2020-12-04 stsp view->focussed = 0;
1129 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1130 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1131 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1132 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1133 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1134 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1136 9b058f45 2022-06-30 mark return err;
1138 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1139 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1141 9b058f45 2022-06-30 mark return err;
1146 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1147 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1148 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1149 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1153 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1154 9b058f45 2022-06-30 mark view->parent->mode = TOG_VIEW_SPLIT_NONE;
1156 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1157 9970f7fc 2020-12-03 stsp view->dying = 1;
1163 640cd7ff 2022-06-22 mark view->count = 0;
1164 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1165 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1167 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1168 e78dc838 2020-12-04 stsp view->focussed = 0;
1169 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1170 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1172 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1175 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1176 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1178 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1179 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1180 e78dc838 2020-12-04 stsp view->focussed = 1;
1181 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1183 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1184 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1185 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1189 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1193 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1194 9b058f45 2022-06-30 mark err = request_log_commits(view);
1198 9b058f45 2022-06-30 mark if (view->parent)
1199 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1201 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1203 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1206 640cd7ff 2022-06-22 mark view->count = 0;
1207 60493ae3 2019-06-20 stsp if (view->search_start)
1208 2b49a8ae 2019-06-22 stsp view_search_start(view);
1210 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1214 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1215 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1216 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1217 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1218 60493ae3 2019-06-20 stsp view->search_next(view);
1220 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1223 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1224 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1226 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1227 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1228 917d79a7 2022-07-01 stsp if (v->reset) {
1229 917d79a7 2022-07-01 stsp err = v->reset(v);
1231 917d79a7 2022-07-01 stsp return err;
1233 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1234 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1236 917d79a7 2022-07-01 stsp return err;
1241 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1245 e5a0f69f 2018-08-18 stsp return err;
1249 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1251 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1252 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1254 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1256 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1259 669b5ffa 2018-10-07 stsp return view->focussed;
1262 bcbd79e2 2018-08-19 stsp static const struct got_error *
1263 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1265 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1266 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1267 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1268 fd823528 2018-10-22 stsp int fast_refresh = 10;
1269 1a76625f 2018-10-22 stsp int done = 0, errcode;
1271 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1272 1a76625f 2018-10-22 stsp if (errcode)
1273 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1275 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1276 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1278 1004088d 2018-09-29 stsp view->focussed = 1;
1279 878940b7 2018-09-29 stsp err = view->show(view);
1281 0cf4efb1 2018-09-29 stsp return err;
1282 0cf4efb1 2018-09-29 stsp update_panels();
1283 0cf4efb1 2018-09-29 stsp doupdate();
1284 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1285 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1286 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1287 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1289 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1292 9970f7fc 2020-12-03 stsp if (view->dying) {
1293 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1295 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1296 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1298 e78dc838 2020-12-04 stsp else if (view->parent)
1299 669b5ffa 2018-10-07 stsp prev = view->parent;
1301 e78dc838 2020-12-04 stsp if (view->parent) {
1302 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1303 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1304 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1305 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1306 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1310 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1312 9970f7fc 2020-12-03 stsp err = view_close(view);
1314 e5a0f69f 2018-08-18 stsp goto done;
1316 e78dc838 2020-12-04 stsp view = NULL;
1317 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1318 e78dc838 2020-12-04 stsp if (v->focussed)
1321 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1322 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1324 e78dc838 2020-12-04 stsp view = prev;
1325 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1326 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1327 e78dc838 2020-12-04 stsp tog_view_list_head);
1329 e78dc838 2020-12-04 stsp if (view) {
1330 e78dc838 2020-12-04 stsp if (view->focus_child) {
1331 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1332 e78dc838 2020-12-04 stsp view = view->child;
1334 e78dc838 2020-12-04 stsp view->focussed = 1;
1338 bcbd79e2 2018-08-19 stsp if (new_view) {
1339 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1340 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1341 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1342 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1344 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1345 86c66b02 2018-10-18 stsp err = view_close(v);
1347 86c66b02 2018-10-18 stsp goto done;
1350 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1351 fed7eaa8 2018-10-24 stsp view = new_view;
1353 669b5ffa 2018-10-07 stsp if (view) {
1354 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1355 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1356 e78dc838 2020-12-04 stsp view = view->child;
1358 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1359 e78dc838 2020-12-04 stsp view = view->parent;
1361 e78dc838 2020-12-04 stsp show_panel(view->panel);
1362 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1363 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1364 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1365 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1367 1a76625f 2018-10-22 stsp goto done;
1369 669b5ffa 2018-10-07 stsp err = view->show(view);
1371 1a76625f 2018-10-22 stsp goto done;
1372 669b5ffa 2018-10-07 stsp if (view->child) {
1373 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1375 1a76625f 2018-10-22 stsp goto done;
1377 1a76625f 2018-10-22 stsp update_panels();
1378 1a76625f 2018-10-22 stsp doupdate();
1382 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1383 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1384 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1385 e5a0f69f 2018-08-18 stsp view_close(view);
1388 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1389 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1390 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1392 e5a0f69f 2018-08-18 stsp return err;
1395 4ed7e80c 2018-05-20 stsp __dead static void
1396 9f7d7167 2018-04-29 stsp usage_log(void)
1399 c70c5802 2018-08-01 stsp fprintf(stderr,
1400 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1401 9f7d7167 2018-04-29 stsp getprogname());
1405 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1406 80ddbec8 2018-04-29 stsp static const struct got_error *
1407 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1409 00dfcb92 2018-06-11 stsp char *vis = NULL;
1410 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1412 963b370f 2018-05-20 stsp *ws = NULL;
1413 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1414 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1415 00dfcb92 2018-06-11 stsp int vislen;
1416 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1417 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1419 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1420 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1422 00dfcb92 2018-06-11 stsp return err;
1423 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1424 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1425 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1426 a7f50699 2018-06-11 stsp goto done;
1430 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1431 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1433 a7f50699 2018-06-11 stsp goto done;
1436 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1437 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1439 00dfcb92 2018-06-11 stsp free(vis);
1440 963b370f 2018-05-20 stsp if (err) {
1441 963b370f 2018-05-20 stsp free(*ws);
1442 963b370f 2018-05-20 stsp *ws = NULL;
1443 963b370f 2018-05-20 stsp *wlen = 0;
1445 963b370f 2018-05-20 stsp return err;
1448 145b6838 2022-06-16 stsp static const struct got_error *
1449 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1451 145b6838 2022-06-16 stsp char *dst;
1452 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1454 145b6838 2022-06-16 stsp *ptr = NULL;
1455 145b6838 2022-06-16 stsp n = len = strlen(src);
1456 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1457 145b6838 2022-06-16 stsp if (dst == NULL)
1458 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1460 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1461 145b6838 2022-06-16 stsp const char c = src[idx];
1463 145b6838 2022-06-16 stsp if (c == '\t') {
1464 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1467 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1468 6e1c41ad 2022-06-16 mark if (p == NULL) {
1469 6e1c41ad 2022-06-16 mark free(dst);
1470 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1475 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1478 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1482 145b6838 2022-06-16 stsp dst[sz] = '\0';
1483 145b6838 2022-06-16 stsp *ptr = dst;
1484 145b6838 2022-06-16 stsp return NULL;
1488 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1489 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1490 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1494 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1496 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1498 4e4a9ac8 2022-06-17 op if (n == 0) {
1499 4e4a9ac8 2022-06-17 op *rcol = cols;
1503 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1504 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1505 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1507 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1509 4e4a9ac8 2022-06-17 op if (width == -1) {
1511 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1514 4e4a9ac8 2022-06-17 op if (cols + width > n)
1516 4e4a9ac8 2022-06-17 op cols += width;
1519 4e4a9ac8 2022-06-17 op *rcol = cols;
1524 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1525 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1526 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1528 ccda2f4d 2022-06-16 stsp static const struct got_error *
1529 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1530 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1532 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1534 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1535 145b6838 2022-06-16 stsp char *exstr = NULL;
1536 963b370f 2018-05-20 stsp size_t wlen;
1537 4e4a9ac8 2022-06-17 op int i, scrollx;
1539 963b370f 2018-05-20 stsp *wlinep = NULL;
1540 b700b5d6 2018-07-10 stsp *widthp = 0;
1542 145b6838 2022-06-16 stsp if (expand) {
1543 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1545 145b6838 2022-06-16 stsp return err;
1548 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1549 145b6838 2022-06-16 stsp free(exstr);
1551 963b370f 2018-05-20 stsp return err;
1553 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1555 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1556 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1559 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1560 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1564 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1565 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1567 b700b5d6 2018-07-10 stsp if (widthp)
1568 b700b5d6 2018-07-10 stsp *widthp = cols;
1569 ccda2f4d 2022-06-16 stsp if (scrollxp)
1570 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1572 963b370f 2018-05-20 stsp free(wline);
1574 963b370f 2018-05-20 stsp *wlinep = wline;
1575 963b370f 2018-05-20 stsp return err;
1578 8b473291 2019-02-21 stsp static const struct got_error*
1579 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1580 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1582 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1583 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1585 8b473291 2019-02-21 stsp const char *name;
1587 8b473291 2019-02-21 stsp *refs_str = NULL;
1589 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1590 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1591 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1594 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1595 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1597 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1598 8b473291 2019-02-21 stsp name += 5;
1599 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1600 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1602 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1603 8b473291 2019-02-21 stsp name += 6;
1604 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1605 8b473291 2019-02-21 stsp name += 8;
1606 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1607 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1610 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1613 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1614 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1615 5d844a1e 2019-08-13 stsp if (err) {
1616 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1617 48cae60d 2020-09-22 stsp free(ref_id);
1620 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1621 5d844a1e 2019-08-13 stsp err = NULL;
1622 5d844a1e 2019-08-13 stsp tag = NULL;
1625 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1626 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1627 48cae60d 2020-09-22 stsp free(ref_id);
1629 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1630 52b5abe1 2019-08-13 stsp if (cmp != 0)
1632 8b473291 2019-02-21 stsp s = *refs_str;
1633 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1634 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1635 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1637 8b473291 2019-02-21 stsp *refs_str = NULL;
1643 8b473291 2019-02-21 stsp return err;
1646 963b370f 2018-05-20 stsp static const struct got_error *
1647 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1648 27a741e5 2019-09-11 stsp int col_tab_align)
1650 e6b8b890 2020-12-29 naddy char *smallerthan;
1652 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1653 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1654 5813d178 2019-03-09 stsp author = smallerthan + 1;
1655 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1656 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1657 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1660 5813d178 2019-03-09 stsp static const struct got_error *
1661 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1662 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1663 8fdc79fe 2020-12-01 naddy int author_display_cols)
1665 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1666 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1667 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1668 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1669 5813d178 2019-03-09 stsp char *author = NULL;
1670 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1671 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1672 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1673 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1674 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1675 ccb26ccd 2018-11-05 stsp struct tm tm;
1676 45d799e2 2018-12-23 stsp time_t committer_time;
1677 11b20872 2019-11-08 stsp struct tog_color *tc;
1679 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1680 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1681 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1682 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1683 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1685 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1686 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1688 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1689 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1691 11b20872 2019-11-08 stsp wattr_on(view->window,
1692 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1693 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1695 11b20872 2019-11-08 stsp wattr_off(view->window,
1696 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1697 27a741e5 2019-09-11 stsp col = limit;
1698 b39d25c7 2018-07-10 stsp if (col > avail)
1699 b39d25c7 2018-07-10 stsp goto done;
1701 6570a66d 2019-11-08 stsp if (avail >= 120) {
1702 6570a66d 2019-11-08 stsp char *id_str;
1703 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1705 6570a66d 2019-11-08 stsp goto done;
1706 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1708 11b20872 2019-11-08 stsp wattr_on(view->window,
1709 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1710 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1712 11b20872 2019-11-08 stsp wattr_off(view->window,
1713 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1714 6570a66d 2019-11-08 stsp free(id_str);
1716 6570a66d 2019-11-08 stsp if (col > avail)
1717 6570a66d 2019-11-08 stsp goto done;
1720 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1721 5813d178 2019-03-09 stsp if (author == NULL) {
1722 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1723 80ddbec8 2018-04-29 stsp goto done;
1725 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1727 bb737323 2018-05-20 stsp goto done;
1728 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1730 11b20872 2019-11-08 stsp wattr_on(view->window,
1731 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1732 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1734 11b20872 2019-11-08 stsp wattr_off(view->window,
1735 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1736 bb737323 2018-05-20 stsp col += author_width;
1737 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1738 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1740 bb737323 2018-05-20 stsp author_width++;
1742 9c2eaf34 2018-05-20 stsp if (col > avail)
1743 9c2eaf34 2018-05-20 stsp goto done;
1745 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1747 6d9fbc00 2018-04-29 stsp goto done;
1748 bb737323 2018-05-20 stsp logmsg = logmsg0;
1749 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1751 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1752 bb737323 2018-05-20 stsp if (newline)
1753 bb737323 2018-05-20 stsp *newline = '\0';
1754 ccda2f4d 2022-06-16 stsp limit = avail - col;
1755 63ba1a3a 2022-06-21 op if (view->child && view_is_splitscreen(view->child) && limit > 0)
1756 4d1f6af3 2022-06-17 op limit--; /* for the border */
1757 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1758 ccda2f4d 2022-06-16 stsp limit, col, 1);
1760 29688b02 2022-06-16 stsp goto done;
1761 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1762 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1763 27a741e5 2019-09-11 stsp while (col < avail) {
1764 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1768 80ddbec8 2018-04-29 stsp free(logmsg0);
1769 bb737323 2018-05-20 stsp free(wlogmsg);
1770 5813d178 2019-03-09 stsp free(author);
1771 bb737323 2018-05-20 stsp free(wauthor);
1772 80ddbec8 2018-04-29 stsp free(line);
1773 80ddbec8 2018-04-29 stsp return err;
1776 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1777 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1778 899d86c2 2018-05-10 stsp struct got_object_id *id)
1780 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1782 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1783 80ddbec8 2018-04-29 stsp if (entry == NULL)
1784 899d86c2 2018-05-10 stsp return NULL;
1786 899d86c2 2018-05-10 stsp entry->id = id;
1787 99db9666 2018-05-07 stsp entry->commit = commit;
1788 899d86c2 2018-05-10 stsp return entry;
1791 99db9666 2018-05-07 stsp static void
1792 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1794 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1796 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1797 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1798 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1799 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1800 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1801 99db9666 2018-05-07 stsp free(entry);
1804 99db9666 2018-05-07 stsp static void
1805 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1807 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1808 99db9666 2018-05-07 stsp pop_commit(commits);
1811 c4972b91 2018-05-07 stsp static const struct got_error *
1812 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1813 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1815 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1816 13add988 2019-10-15 stsp regmatch_t regmatch;
1817 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1819 13add988 2019-10-15 stsp *have_match = 0;
1821 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1823 13add988 2019-10-15 stsp return err;
1825 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1827 13add988 2019-10-15 stsp goto done;
1829 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1830 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1831 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1832 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1833 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1834 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1835 13add988 2019-10-15 stsp *have_match = 1;
1837 13add988 2019-10-15 stsp free(id_str);
1838 13add988 2019-10-15 stsp free(logmsg);
1839 13add988 2019-10-15 stsp return err;
1842 13add988 2019-10-15 stsp static const struct got_error *
1843 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1845 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1848 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1849 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1850 1a76625f 2018-10-22 stsp * while updating the display.
1853 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1854 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1855 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1856 1a76625f 2018-10-22 stsp int errcode;
1858 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1859 4e0d2870 2020-12-07 naddy NULL, NULL);
1860 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1863 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1866 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1867 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1868 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1872 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1873 1a76625f 2018-10-22 stsp if (errcode) {
1874 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1875 13add988 2019-10-15 stsp "pthread_mutex_lock");
1879 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1880 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1881 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1883 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1884 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1885 7c1452c1 2020-03-26 stsp int have_match;
1886 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1889 7c1452c1 2020-03-26 stsp if (have_match)
1890 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1893 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1894 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1895 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1896 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1899 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1901 9ba79e04 2018-06-11 stsp return err;
1904 2b779855 2020-12-05 naddy static void
1905 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1907 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1908 2b779855 2020-12-05 naddy int ncommits = 0;
1910 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1911 2b779855 2020-12-05 naddy while (entry) {
1912 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1913 2b779855 2020-12-05 naddy s->selected_entry = entry;
1916 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1917 2b779855 2020-12-05 naddy ncommits++;
1921 0553a4e3 2018-04-30 stsp static const struct got_error *
1922 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1924 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1925 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1926 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1927 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1928 60493ae3 2019-06-20 stsp int width;
1929 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1930 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1931 8b473291 2019-02-21 stsp char *refs_str = NULL;
1932 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1933 11b20872 2019-11-08 stsp struct tog_color *tc;
1934 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1936 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1937 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1938 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1939 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1941 ecb28ae0 2018-07-16 stsp return err;
1942 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1943 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1944 d2075bf3 2020-12-25 stsp if (refs) {
1945 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1946 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1948 d2075bf3 2020-12-25 stsp goto done;
1952 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1953 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1955 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1956 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1957 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1958 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1959 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1960 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1961 8f4ed634 2020-03-26 stsp goto done;
1964 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1966 f9686aa5 2020-03-27 stsp if (view->searching) {
1967 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1968 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1969 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1970 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1971 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1972 f9686aa5 2020-03-27 stsp search_str = "searching...";
1975 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1976 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1977 f9686aa5 2020-03-27 stsp search_str ? search_str :
1978 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1979 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1980 8f4ed634 2020-03-26 stsp goto done;
1984 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1985 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1986 87411fa9 2022-06-16 stsp "........................................",
1987 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1988 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1989 1a76625f 2018-10-22 stsp header = NULL;
1990 1a76625f 2018-10-22 stsp goto done;
1992 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1993 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1994 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1995 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1996 1a76625f 2018-10-22 stsp header = NULL;
1997 1a76625f 2018-10-22 stsp goto done;
1999 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2001 1a76625f 2018-10-22 stsp goto done;
2003 2814baeb 2018-08-01 stsp werase(view->window);
2005 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2006 a3404814 2018-09-02 stsp wstandout(view->window);
2007 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2009 11b20872 2019-11-08 stsp wattr_on(view->window,
2010 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2011 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2013 11b20872 2019-11-08 stsp wattr_off(view->window,
2014 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2015 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2016 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2019 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2020 a3404814 2018-09-02 stsp wstandend(view->window);
2021 ecb28ae0 2018-07-16 stsp free(wline);
2022 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2023 1a76625f 2018-10-22 stsp goto done;
2025 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2026 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2027 5813d178 2019-03-09 stsp ncommits = 0;
2028 145b6838 2022-06-16 stsp view->maxx = 0;
2029 5813d178 2019-03-09 stsp while (entry) {
2030 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2031 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2032 5813d178 2019-03-09 stsp int width;
2033 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2035 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2036 5813d178 2019-03-09 stsp if (author == NULL) {
2037 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2038 5813d178 2019-03-09 stsp goto done;
2040 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2041 27a741e5 2019-09-11 stsp date_display_cols);
2042 5813d178 2019-03-09 stsp if (author_cols < width)
2043 5813d178 2019-03-09 stsp author_cols = width;
2044 5813d178 2019-03-09 stsp free(wauthor);
2045 5813d178 2019-03-09 stsp free(author);
2046 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
2048 145b6838 2022-06-16 stsp goto done;
2049 145b6838 2022-06-16 stsp msg = msg0;
2050 145b6838 2022-06-16 stsp while (*msg == '\n')
2052 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2053 29688b02 2022-06-16 stsp *eol = '\0';
2054 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2055 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2057 29688b02 2022-06-16 stsp goto done;
2058 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2059 145b6838 2022-06-16 stsp free(msg0);
2060 29688b02 2022-06-16 stsp free(wmsg);
2061 7ca04879 2019-10-19 stsp ncommits++;
2062 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2065 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2066 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2067 867c6645 2018-07-10 stsp ncommits = 0;
2068 899d86c2 2018-05-10 stsp while (entry) {
2069 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2071 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2072 2814baeb 2018-08-01 stsp wstandout(view->window);
2073 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2074 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2075 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2076 2814baeb 2018-08-01 stsp wstandend(view->window);
2078 60493ae3 2019-06-20 stsp goto done;
2079 0553a4e3 2018-04-30 stsp ncommits++;
2080 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2081 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2084 9b058f45 2022-06-30 mark view_border(view);
2086 1a76625f 2018-10-22 stsp free(id_str);
2087 8b473291 2019-02-21 stsp free(refs_str);
2088 1a76625f 2018-10-22 stsp free(ncommits_str);
2089 1a76625f 2018-10-22 stsp free(header);
2090 80ddbec8 2018-04-29 stsp return err;
2093 07b55e75 2018-05-10 stsp static void
2094 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2096 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2097 07b55e75 2018-05-10 stsp int nscrolled = 0;
2099 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2100 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2103 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2104 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2105 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2106 07b55e75 2018-05-10 stsp if (entry) {
2107 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2108 07b55e75 2018-05-10 stsp nscrolled++;
2113 aa075928 2018-05-10 stsp static const struct got_error *
2114 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2116 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2117 5e224a3e 2019-02-22 stsp int errcode;
2119 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2121 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2122 ffe38506 2020-12-01 naddy if (ta->log_complete)
2125 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2126 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2127 7aafa0d1 2019-02-22 stsp if (errcode)
2128 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2129 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2132 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2133 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2135 7c1452c1 2020-03-26 stsp if (!wait)
2138 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2139 ffe38506 2020-12-01 naddy show_log_view(view);
2140 7c1452c1 2020-03-26 stsp update_panels();
2141 7c1452c1 2020-03-26 stsp doupdate();
2143 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2144 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2145 82954512 2020-02-03 stsp if (errcode)
2146 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2147 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2149 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2150 ffe38506 2020-12-01 naddy show_log_view(view);
2151 7c1452c1 2020-03-26 stsp update_panels();
2152 7c1452c1 2020-03-26 stsp doupdate();
2155 5e224a3e 2019-02-22 stsp return NULL;
2158 5e224a3e 2019-02-22 stsp static const struct got_error *
2159 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2161 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2162 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2164 9b058f45 2022-06-30 mark state->thread_args.commits_needed = view->nscrolled;
2165 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2166 9b058f45 2022-06-30 mark view->nscrolled = 0;
2168 9b058f45 2022-06-30 mark return err;
2171 9b058f45 2022-06-30 mark static const struct got_error *
2172 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2174 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2175 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2176 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2177 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2179 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2180 5e224a3e 2019-02-22 stsp return NULL;
2182 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2183 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2184 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2186 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2188 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2189 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2191 5e224a3e 2019-02-22 stsp return err;
2195 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2196 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2199 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2200 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2202 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2203 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2205 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2206 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2208 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
2209 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2211 9b058f45 2022-06-30 mark view->nscrolled = 0;
2213 dd0a52c1 2018-05-20 stsp return err;
2216 cd0acaa7 2018-05-20 stsp static const struct got_error *
2217 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2218 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2219 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2221 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2222 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2223 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2225 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2226 15a94983 2018-12-23 stsp if (diff_view == NULL)
2227 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2229 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2230 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2231 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2232 e5a0f69f 2018-08-18 stsp if (err == NULL)
2233 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2234 cd0acaa7 2018-05-20 stsp return err;
2237 80ddbec8 2018-04-29 stsp static const struct got_error *
2238 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2239 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2241 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2243 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2244 941e9f74 2019-05-21 stsp if (parent == NULL)
2245 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2247 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2248 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2249 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2250 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2251 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2252 941e9f74 2019-05-21 stsp s->tree = subtree;
2253 941e9f74 2019-05-21 stsp s->selected = 0;
2254 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2255 941e9f74 2019-05-21 stsp return NULL;
2258 941e9f74 2019-05-21 stsp static const struct got_error *
2259 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2260 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2262 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2263 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2264 941e9f74 2019-05-21 stsp const char *p;
2265 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2267 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2269 941e9f74 2019-05-21 stsp while (*p) {
2270 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2271 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2272 56e0773d 2019-11-28 stsp char *te_name;
2274 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2277 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2278 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2279 941e9f74 2019-05-21 stsp if (slash == NULL)
2280 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2282 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2283 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2284 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2287 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2288 56e0773d 2019-11-28 stsp if (te == NULL) {
2289 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2290 56e0773d 2019-11-28 stsp free(te_name);
2293 56e0773d 2019-11-28 stsp free(te_name);
2294 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2296 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2297 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2299 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2300 941e9f74 2019-05-21 stsp if (slash)
2301 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2303 941e9f74 2019-05-21 stsp subpath = strdup(path);
2304 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2305 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2309 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2314 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2315 941e9f74 2019-05-21 stsp free(tree_id);
2319 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2320 941e9f74 2019-05-21 stsp if (err) {
2321 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2324 941e9f74 2019-05-21 stsp if (slash == NULL)
2326 941e9f74 2019-05-21 stsp free(subpath);
2327 941e9f74 2019-05-21 stsp subpath = NULL;
2328 941e9f74 2019-05-21 stsp p = slash;
2331 941e9f74 2019-05-21 stsp free(subpath);
2332 1a76625f 2018-10-22 stsp return err;
2335 61266923 2020-01-14 stsp static const struct got_error *
2336 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2337 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2338 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2340 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2341 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2342 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2344 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2345 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2346 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2348 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2350 55cccc34 2020-02-20 stsp return err;
2351 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2353 55cccc34 2020-02-20 stsp *new_view = tree_view;
2355 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2356 55cccc34 2020-02-20 stsp return NULL;
2358 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2361 55cccc34 2020-02-20 stsp static const struct got_error *
2362 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2364 61266923 2020-01-14 stsp sigset_t sigset;
2365 61266923 2020-01-14 stsp int errcode;
2367 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2368 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2370 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2371 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2372 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2373 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2374 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2375 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2376 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2377 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2378 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2380 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2381 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2382 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2384 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2385 61266923 2020-01-14 stsp if (errcode)
2386 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2388 61266923 2020-01-14 stsp return NULL;
2391 1a76625f 2018-10-22 stsp static void *
2392 1a76625f 2018-10-22 stsp log_thread(void *arg)
2394 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2395 1a76625f 2018-10-22 stsp int errcode = 0;
2396 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2397 1a76625f 2018-10-22 stsp int done = 0;
2399 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2401 61266923 2020-01-14 stsp return (void *)err;
2403 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2404 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2405 1a76625f 2018-10-22 stsp if (err) {
2406 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2407 1a76625f 2018-10-22 stsp return (void *)err;
2408 1a76625f 2018-10-22 stsp err = NULL;
2410 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2411 1a76625f 2018-10-22 stsp a->commits_needed--;
2413 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2414 3abe8080 2019-04-10 stsp if (errcode) {
2415 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2416 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2418 3abe8080 2019-04-10 stsp } else if (*a->quit)
2420 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2421 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2422 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2423 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2426 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2427 7c1452c1 2020-03-26 stsp if (errcode) {
2428 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2429 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2430 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2435 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2437 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2438 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2439 7c1452c1 2020-03-26 stsp &tog_mutex);
2440 7c1452c1 2020-03-26 stsp if (errcode)
2441 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2442 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2443 21355643 2020-12-06 stsp if (*a->quit)
2448 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2449 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2450 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2451 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2453 3abe8080 2019-04-10 stsp a->log_complete = 1;
2454 1a76625f 2018-10-22 stsp return (void *)err;
2457 1a76625f 2018-10-22 stsp static const struct got_error *
2458 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2460 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2461 1a76625f 2018-10-22 stsp int errcode;
2463 1a76625f 2018-10-22 stsp if (s->thread) {
2464 1a76625f 2018-10-22 stsp s->quit = 1;
2465 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2466 1a76625f 2018-10-22 stsp if (errcode)
2467 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2468 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2469 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2470 1a76625f 2018-10-22 stsp if (errcode)
2471 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2472 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2473 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2474 1a76625f 2018-10-22 stsp if (errcode)
2475 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2476 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2477 1a76625f 2018-10-22 stsp if (errcode)
2478 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2479 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2480 1a76625f 2018-10-22 stsp s->thread = NULL;
2483 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2484 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2485 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2488 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2489 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2490 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2491 74467cc8 2022-06-15 stsp if (err == NULL)
2492 74467cc8 2022-06-15 stsp err = pack_err;
2493 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2496 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2497 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2498 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2501 9343a5fb 2018-06-23 stsp return err;
2504 9343a5fb 2018-06-23 stsp static const struct got_error *
2505 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2507 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2508 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2509 276b94a1 2020-11-13 naddy int errcode;
2511 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2513 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2514 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2515 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2517 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2518 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2519 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2521 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2522 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2523 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2524 1a76625f 2018-10-22 stsp free(s->start_id);
2525 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2526 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2527 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2528 1a76625f 2018-10-22 stsp return err;
2531 1a76625f 2018-10-22 stsp static const struct got_error *
2532 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2534 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2536 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2537 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2538 60493ae3 2019-06-20 stsp return NULL;
2541 60493ae3 2019-06-20 stsp static const struct got_error *
2542 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2544 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2545 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2546 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2548 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2549 f9686aa5 2020-03-27 stsp show_log_view(view);
2550 f9686aa5 2020-03-27 stsp update_panels();
2551 f9686aa5 2020-03-27 stsp doupdate();
2553 96e2b566 2019-07-08 stsp if (s->search_entry) {
2554 13add988 2019-10-15 stsp int errcode, ch;
2555 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2556 13add988 2019-10-15 stsp if (errcode)
2557 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2558 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2559 13add988 2019-10-15 stsp ch = wgetch(view->window);
2560 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2561 13add988 2019-10-15 stsp if (errcode)
2562 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2563 13add988 2019-10-15 stsp "pthread_mutex_lock");
2564 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2565 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2566 678cbce5 2019-07-28 stsp return NULL;
2568 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2569 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2571 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2572 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2573 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2574 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2575 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2578 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2579 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2580 f704b35c 2022-06-18 stsp * might have changed.
2582 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2583 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2584 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2586 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2588 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2589 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2590 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2592 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2593 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2596 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2599 60493ae3 2019-06-20 stsp while (1) {
2600 13add988 2019-10-15 stsp int have_match = 0;
2602 60493ae3 2019-06-20 stsp if (entry == NULL) {
2603 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2604 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2605 f9967bca 2020-03-27 stsp view->search_next_done =
2606 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2607 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2608 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2609 f801134a 2019-06-25 stsp return NULL;
2612 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2613 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2614 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2616 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2617 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2620 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2621 13add988 2019-10-15 stsp &view->regex);
2624 13add988 2019-10-15 stsp if (have_match) {
2625 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2626 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2630 96e2b566 2019-07-08 stsp s->search_entry = entry;
2631 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2632 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2634 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2637 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2638 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2639 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2640 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2642 60493ae3 2019-06-20 stsp return err;
2645 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2646 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2648 60493ae3 2019-06-20 stsp return err;
2653 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2655 60493ae3 2019-06-20 stsp return NULL;
2658 60493ae3 2019-06-20 stsp static const struct got_error *
2659 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2660 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2661 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2663 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2664 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2665 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2666 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2667 1a76625f 2018-10-22 stsp int errcode;
2669 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2670 f135c941 2020-02-20 stsp free(s->in_repo_path);
2671 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2672 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2673 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2676 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2677 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2678 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2680 fb2756b9 2018-08-04 stsp s->repo = repo;
2681 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2682 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2683 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2684 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2685 9cd7cbd1 2020-12-07 stsp goto done;
2688 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2689 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2690 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2691 5036bf37 2018-09-24 stsp goto done;
2693 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2695 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2696 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2697 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2698 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2700 11b20872 2019-11-08 stsp goto done;
2701 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2702 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2703 11b20872 2019-11-08 stsp if (err) {
2704 11b20872 2019-11-08 stsp free_colors(&s->colors);
2705 11b20872 2019-11-08 stsp goto done;
2707 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2708 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2709 11b20872 2019-11-08 stsp if (err) {
2710 11b20872 2019-11-08 stsp free_colors(&s->colors);
2711 11b20872 2019-11-08 stsp goto done;
2715 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2716 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2717 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2718 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2719 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2721 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2722 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2724 74467cc8 2022-06-15 stsp goto done;
2726 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2727 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2728 0ae84acc 2022-06-15 tracey if (err)
2729 0ae84acc 2022-06-15 tracey goto done;
2730 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2731 b672a97a 2020-01-27 stsp !s->log_branches);
2733 1a76625f 2018-10-22 stsp goto done;
2734 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2735 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2737 c5b78334 2020-01-12 stsp goto done;
2739 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2740 1a76625f 2018-10-22 stsp if (errcode) {
2741 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2742 1a76625f 2018-10-22 stsp goto done;
2744 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2745 7c1452c1 2020-03-26 stsp if (errcode) {
2746 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2747 7c1452c1 2020-03-26 stsp goto done;
2750 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2751 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2752 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2753 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2754 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2755 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2756 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2757 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2758 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2759 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2760 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2761 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2762 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2765 1a76625f 2018-10-22 stsp close_log_view(view);
2766 ba4f502b 2018-08-04 stsp return err;
2769 e5a0f69f 2018-08-18 stsp static const struct got_error *
2770 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2772 f2f6d207 2020-11-24 stsp const struct got_error *err;
2773 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2775 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2776 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2777 2b380cc8 2018-10-24 stsp &s->thread_args);
2778 2b380cc8 2018-10-24 stsp if (errcode)
2779 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2780 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2781 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2783 f2f6d207 2020-11-24 stsp return err;
2787 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2790 b880cc75 2022-06-30 mark static void
2791 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
2793 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2795 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
2796 b880cc75 2022-06-30 mark view->count = 0;
2797 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
2800 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2802 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2804 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
2805 b880cc75 2022-06-30 mark --s->selected;
2807 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2809 b880cc75 2022-06-30 mark select_commit(s);
2813 b880cc75 2022-06-30 mark static const struct got_error *
2814 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
2816 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2817 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
2818 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
2820 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
2821 b880cc75 2022-06-30 mark if (first == NULL) {
2822 b880cc75 2022-06-30 mark view->count = 0;
2823 b880cc75 2022-06-30 mark return NULL;
2826 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2827 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
2828 b880cc75 2022-06-30 mark return NULL;
2830 b880cc75 2022-06-30 mark if (!page) {
2831 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
2833 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2834 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
2835 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
2836 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
2837 b880cc75 2022-06-30 mark ++s->selected;
2839 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
2840 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
2841 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2842 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
2843 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
2845 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
2846 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
2847 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2849 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
2851 b880cc75 2022-06-30 mark return err;
2852 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
2853 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2854 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
2858 b880cc75 2022-06-30 mark return err;
2861 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
2862 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
2864 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
2865 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2867 b880cc75 2022-06-30 mark select_commit(s);
2869 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2870 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
2871 b880cc75 2022-06-30 mark view->count = 0;
2873 b880cc75 2022-06-30 mark return NULL;
2877 9b058f45 2022-06-30 mark * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2878 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2879 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_HRZN horizontal split
2880 9b058f45 2022-06-30 mark * Assign start column and line of the new split to *x and *y, respectively,
2881 9b058f45 2022-06-30 mark * and assign view mode to view->mode.
2883 9b058f45 2022-06-30 mark static void
2884 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
2886 9b058f45 2022-06-30 mark char *mode;
2891 9b058f45 2022-06-30 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
2893 9b058f45 2022-06-30 mark if (!mode || mode[0] != 'h') {
2894 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_VERT;
2895 9b058f45 2022-06-30 mark *x = view_split_begin_x(view->begin_x);
2896 9b058f45 2022-06-30 mark } else if (mode && mode[0] == 'h') {
2897 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_HRZN;
2898 9b058f45 2022-06-30 mark *y = view_split_begin_y(view->lines);
2902 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
2903 e5a0f69f 2018-08-18 stsp static const struct got_error *
2904 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
2906 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2908 9b058f45 2022-06-30 mark view->nlines = y;
2909 9b058f45 2022-06-30 mark err = view_resize(view);
2911 9b058f45 2022-06-30 mark return err;
2913 9b058f45 2022-06-30 mark err = offset_selection_down(view);
2915 9b058f45 2022-06-30 mark return err;
2918 9b058f45 2022-06-30 mark static const struct got_error *
2919 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2921 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2922 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2923 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2924 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2925 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2926 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
2928 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2929 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2930 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2931 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2932 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
2933 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
2935 b880cc75 2022-06-30 mark return err;
2938 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
2939 0dca135e 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2940 0dca135e 2022-06-30 mark view_is_splitscreen(view->child))
2941 0dca135e 2022-06-30 mark --eos; /* border */
2944 528dedf3 2021-08-30 stsp switch (ch) {
2946 1e37a5c2 2019-05-12 jcs s->quit = 1;
2949 145b6838 2022-06-16 stsp view->x = 0;
2952 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2953 640cd7ff 2022-06-22 mark view->count = 0;
2955 145b6838 2022-06-16 stsp case KEY_RIGHT:
2957 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2958 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2960 640cd7ff 2022-06-22 mark view->count = 0;
2962 145b6838 2022-06-16 stsp case KEY_LEFT:
2964 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2965 640cd7ff 2022-06-22 mark if (view->x <= 0)
2966 640cd7ff 2022-06-22 mark view->count = 0;
2969 1e37a5c2 2019-05-12 jcs case KEY_UP:
2972 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2973 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
2976 912a3f79 2021-08-30 j case KEY_HOME:
2977 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
2978 640cd7ff 2022-06-22 mark view->count = 0;
2980 83cc4199 2022-06-13 stsp case CTRL('u'):
2982 83cc4199 2022-06-13 stsp nscroll /= 2;
2983 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2984 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2985 a4292ac5 2019-05-12 jcs case CTRL('b'):
2987 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
2990 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2993 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2994 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
2997 912a3f79 2021-08-30 j case KEY_END: {
2998 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2999 912a3f79 2021-08-30 j * traverse them all. */
3000 640cd7ff 2022-06-22 mark view->count = 0;
3001 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3002 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3003 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3006 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3007 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3008 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3009 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3011 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3012 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3014 f3bc9f1d 2021-09-05 naddy if (n > 0)
3015 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3016 2b779855 2020-12-05 naddy select_commit(s);
3019 80b7e8da 2022-06-11 stsp case CTRL('d'):
3021 83cc4199 2022-06-13 stsp nscroll /= 2;
3022 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3023 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3024 61417565 2022-06-20 mark case CTRL('f'):
3025 48bb96f0 2022-06-20 naddy case 'f':
3027 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3029 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3030 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3031 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3032 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3033 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3034 2b779855 2020-12-05 naddy select_commit(s);
3035 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3036 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3037 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3038 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3039 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3042 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3043 9b058f45 2022-06-30 mark case '\r': {
3044 640cd7ff 2022-06-22 mark view->count = 0;
3045 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3048 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3049 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3050 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3052 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3053 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3054 78756c87 2020-11-24 stsp view, s->repo);
3058 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3059 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3064 e78dc838 2020-12-04 stsp view->focussed = 0;
3065 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3066 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3067 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3069 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3070 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3072 1e37a5c2 2019-05-12 jcs return err;
3073 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3076 e78dc838 2020-12-04 stsp view->focus_child = 1;
3078 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3082 640cd7ff 2022-06-22 mark view->count = 0;
3083 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3085 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3086 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3087 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
3088 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3092 e78dc838 2020-12-04 stsp view->focussed = 0;
3093 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3094 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3095 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3097 1e37a5c2 2019-05-12 jcs return err;
3098 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3101 e78dc838 2020-12-04 stsp view->focus_child = 1;
3103 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3105 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3106 21355643 2020-12-06 stsp case CTRL('l'):
3108 640cd7ff 2022-06-22 mark view->count = 0;
3109 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3110 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3112 21355643 2020-12-06 stsp err = stop_log_thread(s);
3114 74cfe85e 2020-10-20 stsp return err;
3115 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3116 21355643 2020-12-06 stsp char *parent_path;
3117 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3119 21355643 2020-12-06 stsp return err;
3120 21355643 2020-12-06 stsp free(s->in_repo_path);
3121 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3122 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3123 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3124 21355643 2020-12-06 stsp struct got_object_id *start_id;
3125 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3126 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3127 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3129 21355643 2020-12-06 stsp return err;
3130 21355643 2020-12-06 stsp free(s->start_id);
3131 21355643 2020-12-06 stsp s->start_id = start_id;
3132 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3133 21355643 2020-12-06 stsp } else /* 'B' */
3134 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3136 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3137 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3139 b0dd8d27 2022-06-16 stsp return err;
3141 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3142 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3143 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3145 21355643 2020-12-06 stsp return err;
3146 51a10b52 2020-12-26 stsp tog_free_refs();
3147 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3149 ca51c541 2020-12-07 stsp return err;
3150 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3151 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3153 d01904d4 2019-06-25 stsp return err;
3154 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3155 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3157 b672a97a 2020-01-27 stsp return err;
3158 21355643 2020-12-06 stsp free_commits(&s->commits);
3159 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3160 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3161 21355643 2020-12-06 stsp s->selected_entry = NULL;
3162 21355643 2020-12-06 stsp s->selected = 0;
3163 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3164 21355643 2020-12-06 stsp s->quit = 0;
3165 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3166 dfee752e 2022-06-18 op s->matched_entry = NULL;
3167 dfee752e 2022-06-18 op s->search_entry = NULL;
3170 640cd7ff 2022-06-22 mark view->count = 0;
3171 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3172 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
3173 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
3174 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
3175 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3176 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3177 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3178 6458efa5 2020-11-24 stsp if (err) {
3179 6458efa5 2020-11-24 stsp view_close(ref_view);
3180 6458efa5 2020-11-24 stsp return err;
3182 e78dc838 2020-12-04 stsp view->focussed = 0;
3183 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3184 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3185 6458efa5 2020-11-24 stsp err = view_close_child(view);
3187 6458efa5 2020-11-24 stsp return err;
3188 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3191 e78dc838 2020-12-04 stsp view->focus_child = 1;
3193 6458efa5 2020-11-24 stsp *new_view = ref_view;
3196 640cd7ff 2022-06-22 mark view->count = 0;
3200 80ddbec8 2018-04-29 stsp return err;
3203 4ed7e80c 2018-05-20 stsp static const struct got_error *
3204 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3206 c2db6724 2019-01-04 stsp const struct got_error *error;
3208 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3209 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3210 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3212 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3213 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3215 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3216 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3218 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3219 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3221 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3222 c2db6724 2019-01-04 stsp if (error != NULL)
3223 c2db6724 2019-01-04 stsp return error;
3225 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3226 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3228 c2db6724 2019-01-04 stsp return NULL;
3231 a915003a 2019-02-05 stsp static void
3232 a915003a 2019-02-05 stsp init_curses(void)
3235 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3236 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3237 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3239 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3240 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3241 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3242 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3243 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3245 a915003a 2019-02-05 stsp initscr();
3247 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3250 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3251 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3252 a915003a 2019-02-05 stsp curs_set(0);
3253 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3254 6d17833f 2019-11-08 stsp start_color();
3255 6d17833f 2019-11-08 stsp use_default_colors();
3259 c2db6724 2019-01-04 stsp static const struct got_error *
3260 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3261 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3263 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3265 f135c941 2020-02-20 stsp if (argc == 0) {
3266 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3267 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3268 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3269 f135c941 2020-02-20 stsp return NULL;
3272 f135c941 2020-02-20 stsp if (worktree) {
3273 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3276 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3278 f135c941 2020-02-20 stsp return err;
3279 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3280 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3281 bfd61697 2020-11-14 stsp p) == -1) {
3282 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3283 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3287 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3289 f135c941 2020-02-20 stsp return err;
3292 f135c941 2020-02-20 stsp static const struct got_error *
3293 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3295 80ddbec8 2018-04-29 stsp const struct got_error *error;
3296 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3297 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3298 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3299 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3300 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3301 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3302 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3303 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3304 04cc582a 2018-08-01 stsp struct tog_view *view;
3305 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3307 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3308 80ddbec8 2018-04-29 stsp switch (ch) {
3310 b672a97a 2020-01-27 stsp log_branches = 1;
3313 80ddbec8 2018-04-29 stsp start_commit = optarg;
3316 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3317 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3318 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3322 17020d27 2019-03-07 stsp usage_log();
3323 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3327 80ddbec8 2018-04-29 stsp argc -= optind;
3328 80ddbec8 2018-04-29 stsp argv += optind;
3330 f135c941 2020-02-20 stsp if (argc > 1)
3331 f135c941 2020-02-20 stsp usage_log();
3333 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3334 0ae84acc 2022-06-15 tracey if (error != NULL)
3335 0ae84acc 2022-06-15 tracey goto done;
3337 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3338 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3339 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3340 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3341 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3342 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3343 c156c7a4 2020-12-18 stsp goto done;
3344 a1fbf39a 2019-08-11 stsp if (worktree)
3345 6962eb72 2020-02-20 stsp repo_path =
3346 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3348 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3349 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3350 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3351 c156c7a4 2020-12-18 stsp goto done;
3355 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3356 80ddbec8 2018-04-29 stsp if (error != NULL)
3357 ecb28ae0 2018-07-16 stsp goto done;
3359 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3360 f135c941 2020-02-20 stsp repo, worktree);
3361 f135c941 2020-02-20 stsp if (error)
3362 f135c941 2020-02-20 stsp goto done;
3364 f135c941 2020-02-20 stsp init_curses();
3366 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3367 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3368 c02c541e 2019-03-29 stsp if (error)
3369 c02c541e 2019-03-29 stsp goto done;
3371 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3372 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3373 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3374 87670572 2020-12-26 naddy if (error)
3375 87670572 2020-12-26 naddy goto done;
3378 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3379 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3380 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3381 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3382 d8f38dc4 2020-12-05 stsp if (error)
3383 d8f38dc4 2020-12-05 stsp goto done;
3384 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3386 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3387 d8f38dc4 2020-12-05 stsp if (error == NULL)
3388 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3389 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3390 d8f38dc4 2020-12-05 stsp goto done;
3391 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3392 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3393 d8f38dc4 2020-12-05 stsp if (error)
3394 d8f38dc4 2020-12-05 stsp goto done;
3397 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3398 04cc582a 2018-08-01 stsp if (view == NULL) {
3399 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3400 04cc582a 2018-08-01 stsp goto done;
3402 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3403 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3404 ba4f502b 2018-08-04 stsp if (error)
3405 ba4f502b 2018-08-04 stsp goto done;
3406 2fc00ff4 2019-08-31 stsp if (worktree) {
3407 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3408 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3409 2fc00ff4 2019-08-31 stsp worktree = NULL;
3411 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3413 f135c941 2020-02-20 stsp free(in_repo_path);
3414 ecb28ae0 2018-07-16 stsp free(repo_path);
3415 ecb28ae0 2018-07-16 stsp free(cwd);
3416 899d86c2 2018-05-10 stsp free(start_id);
3417 d8f38dc4 2020-12-05 stsp free(label);
3419 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3420 1d0f4054 2021-06-17 stsp if (repo) {
3421 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3422 1d0f4054 2021-06-17 stsp if (error == NULL)
3423 1d0f4054 2021-06-17 stsp error = close_err;
3425 ec142235 2019-03-07 stsp if (worktree)
3426 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3427 0ae84acc 2022-06-15 tracey if (pack_fds) {
3428 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3429 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3430 0ae84acc 2022-06-15 tracey if (error == NULL)
3431 0ae84acc 2022-06-15 tracey error = pack_err;
3433 51a10b52 2020-12-26 stsp tog_free_refs();
3434 80ddbec8 2018-04-29 stsp return error;
3437 4ed7e80c 2018-05-20 stsp __dead static void
3438 9f7d7167 2018-04-29 stsp usage_diff(void)
3441 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3442 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3446 6d17833f 2019-11-08 stsp static int
3447 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3448 41605754 2020-11-12 stsp regmatch_t *regmatch)
3450 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3453 336075a4 2022-06-25 op static struct tog_color *
3454 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3456 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3458 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3459 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3460 6d17833f 2019-11-08 stsp return tc;
3463 6d17833f 2019-11-08 stsp return NULL;
3466 4ed7e80c 2018-05-20 stsp static const struct got_error *
3467 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3468 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3470 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3471 44a87665 2022-06-16 stsp char *exstr = NULL;
3472 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3473 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3474 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3475 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3477 41605754 2020-11-12 stsp *wtotal = 0;
3479 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3480 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3482 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3484 44a87665 2022-06-16 stsp return err;
3486 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3487 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3488 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3489 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3490 44a87665 2022-06-16 stsp goto done;
3492 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3493 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3494 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3495 1853e0f4 2022-06-16 stsp goto done;
3497 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3498 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3499 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3500 1853e0f4 2022-06-16 stsp goto done;
3503 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3504 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3505 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3507 1853e0f4 2022-06-16 stsp goto done;
3508 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3510 1853e0f4 2022-06-16 stsp free(wline);
3511 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3512 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3514 1853e0f4 2022-06-16 stsp goto done;
3515 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3516 1853e0f4 2022-06-16 stsp wlimit -= width;
3517 1853e0f4 2022-06-16 stsp *wtotal += width;
3520 41605754 2020-11-12 stsp if (wlimit > 0) {
3521 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3522 1853e0f4 2022-06-16 stsp size_t wlen;
3524 1853e0f4 2022-06-16 stsp free(wline);
3525 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3526 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3528 1853e0f4 2022-06-16 stsp goto done;
3529 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3530 1853e0f4 2022-06-16 stsp while (i < wlen) {
3531 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3532 1853e0f4 2022-06-16 stsp if (width == -1) {
3533 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3534 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3535 1853e0f4 2022-06-16 stsp goto done;
3537 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3539 61417565 2022-06-20 mark w += width;
3542 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3543 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3544 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3545 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3546 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3547 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3548 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3552 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3553 1853e0f4 2022-06-16 stsp free(wline);
3554 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3555 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3556 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3557 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3559 1853e0f4 2022-06-16 stsp goto done;
3560 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3562 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3563 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3565 1853e0f4 2022-06-16 stsp goto done;
3566 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3568 1853e0f4 2022-06-16 stsp *wtotal += width2;
3571 145b6838 2022-06-16 stsp free(wline);
3572 44a87665 2022-06-16 stsp free(exstr);
3573 1853e0f4 2022-06-16 stsp free(seg0);
3574 1853e0f4 2022-06-16 stsp free(seg1);
3575 1853e0f4 2022-06-16 stsp free(seg2);
3576 1853e0f4 2022-06-16 stsp return err;
3579 41605754 2020-11-12 stsp static const struct got_error *
3580 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3582 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3583 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3584 61e69b96 2018-05-20 stsp const struct got_error *err;
3585 fe621944 2020-11-10 stsp int nprinted = 0;
3586 b304db33 2018-05-20 stsp char *line;
3587 826082fe 2020-12-10 stsp size_t linesize = 0;
3588 826082fe 2020-12-10 stsp ssize_t linelen;
3589 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3590 61e69b96 2018-05-20 stsp wchar_t *wline;
3591 e0b650dd 2018-05-20 stsp int width;
3592 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3593 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3594 fe621944 2020-11-10 stsp off_t line_offset;
3596 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3597 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3598 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3600 f7d12f7e 2018-08-01 stsp werase(view->window);
3602 a3404814 2018-09-02 stsp if (header) {
3603 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3604 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3605 135a2da0 2020-11-11 stsp header) == -1)
3606 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3607 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3609 135a2da0 2020-11-11 stsp free(line);
3611 a3404814 2018-09-02 stsp return err;
3613 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3614 a3404814 2018-09-02 stsp wstandout(view->window);
3615 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3616 e54cc94a 2020-11-11 stsp free(wline);
3617 e54cc94a 2020-11-11 stsp wline = NULL;
3618 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3619 a3404814 2018-09-02 stsp wstandend(view->window);
3620 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3621 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3623 a3404814 2018-09-02 stsp if (max_lines <= 1)
3624 a3404814 2018-09-02 stsp return NULL;
3625 a3404814 2018-09-02 stsp max_lines--;
3628 89f1a395 2020-12-01 naddy s->eof = 0;
3629 145b6838 2022-06-16 stsp view->maxx = 0;
3630 826082fe 2020-12-10 stsp line = NULL;
3631 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3632 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3633 826082fe 2020-12-10 stsp if (linelen == -1) {
3634 826082fe 2020-12-10 stsp if (feof(s->f)) {
3635 826082fe 2020-12-10 stsp s->eof = 1;
3638 826082fe 2020-12-10 stsp free(line);
3639 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3642 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3643 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3644 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3645 1853e0f4 2022-06-16 stsp if (err) {
3646 1853e0f4 2022-06-16 stsp free(line);
3647 1853e0f4 2022-06-16 stsp return err;
3649 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3650 1853e0f4 2022-06-16 stsp free(wline);
3651 1853e0f4 2022-06-16 stsp wline = NULL;
3653 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3655 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3656 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3657 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3658 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3659 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3660 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3661 41605754 2020-11-12 stsp if (err) {
3662 41605754 2020-11-12 stsp free(line);
3663 41605754 2020-11-12 stsp return err;
3667 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3668 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3669 969c159c 2022-06-16 stsp if (err) {
3670 969c159c 2022-06-16 stsp free(line);
3671 969c159c 2022-06-16 stsp return err;
3673 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3674 969c159c 2022-06-16 stsp free(wline);
3675 41605754 2020-11-12 stsp wline = NULL;
3678 6d17833f 2019-11-08 stsp wattr_off(view->window,
3679 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3680 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3681 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3682 fe621944 2020-11-10 stsp nprinted++;
3684 826082fe 2020-12-10 stsp free(line);
3685 fe621944 2020-11-10 stsp if (nprinted >= 1)
3686 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3687 89f1a395 2020-12-01 naddy (nprinted - 1);
3689 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3691 9b058f45 2022-06-30 mark view_border(view);
3693 89f1a395 2020-12-01 naddy if (s->eof) {
3694 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3695 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3696 c3e9aa98 2019-05-13 jcs nprinted++;
3699 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3700 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3702 c3e9aa98 2019-05-13 jcs return err;
3705 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3706 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3707 e54cc94a 2020-11-11 stsp free(wline);
3708 e54cc94a 2020-11-11 stsp wline = NULL;
3709 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3712 26ed57b2 2018-05-19 stsp return NULL;
3715 abd2672a 2018-12-23 stsp static char *
3716 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3718 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3719 09867e48 2019-08-13 stsp char *p, *s;
3721 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3722 09867e48 2019-08-13 stsp if (tm == NULL)
3723 09867e48 2019-08-13 stsp return NULL;
3724 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3725 09867e48 2019-08-13 stsp if (s == NULL)
3726 09867e48 2019-08-13 stsp return NULL;
3727 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3729 abd2672a 2018-12-23 stsp *p = '\0';
3733 4ed7e80c 2018-05-20 stsp static const struct got_error *
3734 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3735 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3737 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3738 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3739 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3740 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3742 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3743 0208f208 2020-05-05 stsp if (qid != NULL) {
3744 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3745 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3746 d7b5a0e8 2022-04-20 stsp &qid->id);
3748 0208f208 2020-05-05 stsp return err;
3750 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3751 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3752 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3753 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3754 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3756 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3760 0208f208 2020-05-05 stsp if (tree_id1) {
3761 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3763 0208f208 2020-05-05 stsp goto done;
3766 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3767 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3769 0208f208 2020-05-05 stsp goto done;
3771 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3772 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3774 0208f208 2020-05-05 stsp if (tree1)
3775 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3776 0208f208 2020-05-05 stsp if (tree2)
3777 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3778 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3779 0208f208 2020-05-05 stsp return err;
3782 0208f208 2020-05-05 stsp static const struct got_error *
3783 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3787 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3788 fe621944 2020-11-10 stsp if (p == NULL)
3789 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3790 fe621944 2020-11-10 stsp *line_offsets = p;
3791 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3792 fe621944 2020-11-10 stsp (*nlines)++;
3793 fe621944 2020-11-10 stsp return NULL;
3796 fe621944 2020-11-10 stsp static const struct got_error *
3797 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3798 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3799 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3801 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3802 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3803 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3804 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3805 45d799e2 2018-12-23 stsp time_t committer_time;
3806 45d799e2 2018-12-23 stsp const char *author, *committer;
3807 8b473291 2019-02-21 stsp char *refs_str = NULL;
3808 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3809 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3810 fe621944 2020-11-10 stsp off_t outoff = 0;
3813 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3815 8b473291 2019-02-21 stsp if (refs) {
3816 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3818 8b473291 2019-02-21 stsp return err;
3821 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3823 abd2672a 2018-12-23 stsp return err;
3825 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3826 15a94983 2018-12-23 stsp if (err) {
3827 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3828 15a94983 2018-12-23 stsp goto done;
3831 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3833 fe621944 2020-11-10 stsp goto done;
3835 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3836 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3837 fe621944 2020-11-10 stsp if (n < 0) {
3838 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3839 abd2672a 2018-12-23 stsp goto done;
3841 fe621944 2020-11-10 stsp outoff += n;
3842 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3844 fe621944 2020-11-10 stsp goto done;
3846 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3847 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3848 fe621944 2020-11-10 stsp if (n < 0) {
3849 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3850 abd2672a 2018-12-23 stsp goto done;
3852 fe621944 2020-11-10 stsp outoff += n;
3853 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3855 fe621944 2020-11-10 stsp goto done;
3857 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3858 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3859 fe621944 2020-11-10 stsp if (datestr) {
3860 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3861 fe621944 2020-11-10 stsp if (n < 0) {
3862 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3863 fe621944 2020-11-10 stsp goto done;
3865 fe621944 2020-11-10 stsp outoff += n;
3866 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3868 fe621944 2020-11-10 stsp goto done;
3870 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3871 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3872 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3873 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3874 fe621944 2020-11-10 stsp if (n < 0) {
3875 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3876 fe621944 2020-11-10 stsp goto done;
3878 fe621944 2020-11-10 stsp outoff += n;
3879 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3881 fe621944 2020-11-10 stsp goto done;
3883 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3884 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3885 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3886 9f98ca05 2021-09-24 stsp int pn = 1;
3887 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3888 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3889 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3891 9f98ca05 2021-09-24 stsp goto done;
3892 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3893 9f98ca05 2021-09-24 stsp if (n < 0) {
3894 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3895 9f98ca05 2021-09-24 stsp goto done;
3897 9f98ca05 2021-09-24 stsp outoff += n;
3898 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3900 9f98ca05 2021-09-24 stsp goto done;
3901 9f98ca05 2021-09-24 stsp free(id_str);
3902 9f98ca05 2021-09-24 stsp id_str = NULL;
3906 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3908 5943eee2 2019-08-13 stsp goto done;
3909 fe621944 2020-11-10 stsp s = logmsg;
3910 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3911 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3912 fe621944 2020-11-10 stsp if (n < 0) {
3913 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3914 fe621944 2020-11-10 stsp goto done;
3916 fe621944 2020-11-10 stsp outoff += n;
3917 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3919 fe621944 2020-11-10 stsp goto done;
3922 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3924 0208f208 2020-05-05 stsp goto done;
3925 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3926 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3927 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3928 fe621944 2020-11-10 stsp if (n < 0) {
3929 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3930 fe621944 2020-11-10 stsp goto done;
3932 fe621944 2020-11-10 stsp outoff += n;
3933 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3935 fe621944 2020-11-10 stsp goto done;
3936 0208f208 2020-05-05 stsp free((char *)pe->path);
3937 0208f208 2020-05-05 stsp free(pe->data);
3940 0208f208 2020-05-05 stsp fputc('\n', outfile);
3942 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3944 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3945 abd2672a 2018-12-23 stsp free(id_str);
3946 5943eee2 2019-08-13 stsp free(logmsg);
3947 8b473291 2019-02-21 stsp free(refs_str);
3948 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3949 7510f233 2020-08-09 stsp if (err) {
3950 ae6a6978 2020-08-09 stsp free(*line_offsets);
3951 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3952 ae6a6978 2020-08-09 stsp *nlines = 0;
3954 fe621944 2020-11-10 stsp return err;
3957 abd2672a 2018-12-23 stsp static const struct got_error *
3958 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3960 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3961 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3962 15a94983 2018-12-23 stsp int obj_type;
3964 fe621944 2020-11-10 stsp free(s->line_offsets);
3965 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3966 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3967 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3968 fe621944 2020-11-10 stsp s->nlines = 0;
3970 511a516b 2018-05-19 stsp f = got_opentemp();
3971 48ae06ee 2018-10-18 stsp if (f == NULL) {
3972 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3973 48ae06ee 2018-10-18 stsp goto done;
3975 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3976 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3977 fb43ecf1 2019-02-11 stsp goto done;
3981 15a94983 2018-12-23 stsp if (s->id1)
3982 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3984 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3986 15a94983 2018-12-23 stsp goto done;
3988 15a94983 2018-12-23 stsp switch (obj_type) {
3989 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3990 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3991 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3992 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
3993 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3995 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3996 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3997 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3998 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
3999 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4001 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4002 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4003 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4004 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4005 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4007 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4009 3ffacbe1 2020-02-02 tracey goto done;
4010 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4011 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4012 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4013 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4014 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4015 f44b1f58 2020-02-02 tracey if (err)
4016 f44b1f58 2020-02-02 tracey goto done;
4017 f44b1f58 2020-02-02 tracey } else {
4018 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4019 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4020 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4021 fe621944 2020-11-10 stsp err = write_commit_info(
4022 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4023 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4024 f44b1f58 2020-02-02 tracey if (err)
4025 f44b1f58 2020-02-02 tracey goto done;
4030 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4032 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4033 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4034 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4035 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4039 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4042 f44b1f58 2020-02-02 tracey if (err)
4043 f44b1f58 2020-02-02 tracey goto done;
4045 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4046 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4047 48ae06ee 2018-10-18 stsp return err;
4050 f5215bb9 2019-02-22 stsp static void
4051 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4053 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4054 f5215bb9 2019-02-22 stsp update_panels();
4055 f5215bb9 2019-02-22 stsp doupdate();
4058 f44b1f58 2020-02-02 tracey static const struct got_error *
4059 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4061 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4063 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4064 f44b1f58 2020-02-02 tracey return NULL;
4067 f44b1f58 2020-02-02 tracey static const struct got_error *
4068 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4070 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4071 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4072 f44b1f58 2020-02-02 tracey int lineno;
4073 cb713507 2022-06-16 stsp char *line = NULL;
4074 826082fe 2020-12-10 stsp size_t linesize = 0;
4075 826082fe 2020-12-10 stsp ssize_t linelen;
4077 f44b1f58 2020-02-02 tracey if (!view->searching) {
4078 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4079 f44b1f58 2020-02-02 tracey return NULL;
4082 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4083 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4084 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4086 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4088 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4090 f44b1f58 2020-02-02 tracey while (1) {
4091 f44b1f58 2020-02-02 tracey off_t offset;
4093 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4094 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4095 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4099 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4100 f44b1f58 2020-02-02 tracey lineno = 1;
4102 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4105 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4106 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4107 f44b1f58 2020-02-02 tracey free(line);
4108 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4110 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4111 cb713507 2022-06-16 stsp if (linelen != -1) {
4112 cb713507 2022-06-16 stsp char *exstr;
4113 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4116 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4117 cb713507 2022-06-16 stsp &view->regmatch)) {
4118 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4119 cb713507 2022-06-16 stsp s->matched_line = lineno;
4120 cb713507 2022-06-16 stsp free(exstr);
4123 cb713507 2022-06-16 stsp free(exstr);
4125 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4126 f44b1f58 2020-02-02 tracey lineno++;
4128 f44b1f58 2020-02-02 tracey lineno--;
4130 826082fe 2020-12-10 stsp free(line);
4132 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4133 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4134 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4137 145b6838 2022-06-16 stsp return err;
4140 48ae06ee 2018-10-18 stsp static const struct got_error *
4141 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4143 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4144 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4146 b72706c3 2022-06-01 stsp free(s->id1);
4147 b72706c3 2022-06-01 stsp s->id1 = NULL;
4148 b72706c3 2022-06-01 stsp free(s->id2);
4149 b72706c3 2022-06-01 stsp s->id2 = NULL;
4150 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4151 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4152 b72706c3 2022-06-01 stsp s->f = NULL;
4153 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4154 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4155 b72706c3 2022-06-01 stsp s->f1 = NULL;
4156 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4157 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4158 b72706c3 2022-06-01 stsp s->f2 = NULL;
4159 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4160 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4161 f9d37699 2022-06-28 stsp s->fd1 = -1;
4162 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4163 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4164 f9d37699 2022-06-28 stsp s->fd2 = -1;
4165 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4166 b72706c3 2022-06-01 stsp free(s->line_offsets);
4167 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4168 b72706c3 2022-06-01 stsp s->nlines = 0;
4169 b72706c3 2022-06-01 stsp return err;
4172 b72706c3 2022-06-01 stsp static const struct got_error *
4173 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4174 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4175 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4176 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4178 48ae06ee 2018-10-18 stsp const struct got_error *err;
4179 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4181 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4182 f9d37699 2022-06-28 stsp s->fd1 = -1;
4183 f9d37699 2022-06-28 stsp s->fd2 = -1;
4185 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4186 15a94983 2018-12-23 stsp int type1, type2;
4187 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4189 15a94983 2018-12-23 stsp return err;
4190 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4192 15a94983 2018-12-23 stsp return err;
4194 15a94983 2018-12-23 stsp if (type1 != type2)
4195 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4197 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4198 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4199 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4200 f44b1f58 2020-02-02 tracey s->repo = repo;
4201 f44b1f58 2020-02-02 tracey s->id1 = id1;
4202 f44b1f58 2020-02-02 tracey s->id2 = id2;
4203 3dbaef42 2020-11-24 stsp s->label1 = label1;
4204 3dbaef42 2020-11-24 stsp s->label2 = label2;
4206 15a94983 2018-12-23 stsp if (id1) {
4207 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4208 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4209 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4211 5465d566 2020-02-01 tracey s->id1 = NULL;
4213 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4214 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4215 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4216 b72706c3 2022-06-01 stsp goto done;
4219 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4220 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4221 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4222 a00719e9 2022-06-17 stsp goto done;
4225 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4226 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4227 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4228 b72706c3 2022-06-01 stsp goto done;
4231 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4232 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4233 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4234 f9d37699 2022-06-28 stsp goto done;
4237 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4238 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4239 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4240 f9d37699 2022-06-28 stsp goto done;
4243 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4244 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4245 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4246 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4247 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4248 5465d566 2020-02-01 tracey s->log_view = log_view;
4249 5465d566 2020-02-01 tracey s->repo = repo;
4251 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4252 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4253 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4254 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4255 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4257 b72706c3 2022-06-01 stsp goto done;
4258 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4259 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4260 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4262 b72706c3 2022-06-01 stsp goto done;
4263 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4264 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4265 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4267 b72706c3 2022-06-01 stsp goto done;
4269 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4270 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4271 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4272 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4273 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4275 b72706c3 2022-06-01 stsp goto done;
4277 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4278 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4279 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4281 b72706c3 2022-06-01 stsp goto done;
4283 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4284 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4285 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4287 b72706c3 2022-06-01 stsp goto done;
4290 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4291 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4292 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4294 5465d566 2020-02-01 tracey err = create_diff(s);
4296 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4297 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4298 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4299 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4300 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4301 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4304 b72706c3 2022-06-01 stsp close_diff_view(view);
4305 e5a0f69f 2018-08-18 stsp return err;
4308 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4309 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4311 a3404814 2018-09-02 stsp const struct got_error *err;
4312 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4313 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4314 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4316 a3404814 2018-09-02 stsp if (s->id1) {
4317 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4319 a3404814 2018-09-02 stsp return err;
4320 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4322 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4324 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4326 a3404814 2018-09-02 stsp return err;
4327 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4329 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4330 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4331 a3404814 2018-09-02 stsp free(id_str1);
4332 a3404814 2018-09-02 stsp free(id_str2);
4333 a3404814 2018-09-02 stsp return err;
4335 a3404814 2018-09-02 stsp free(id_str1);
4336 a3404814 2018-09-02 stsp free(id_str2);
4338 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4339 267bb3b8 2021-08-01 stsp free(header);
4340 267bb3b8 2021-08-01 stsp return err;
4343 15a087fe 2019-02-21 stsp static const struct got_error *
4344 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4345 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4347 d7a04538 2019-02-21 stsp const struct got_error *err;
4348 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4349 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4350 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4352 15a087fe 2019-02-21 stsp free(s->id2);
4353 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4354 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4355 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4357 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4359 d7a04538 2019-02-21 stsp return err;
4360 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4361 15a087fe 2019-02-21 stsp free(s->id1);
4362 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4363 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4364 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4365 15a087fe 2019-02-21 stsp return NULL;
4368 0cf4efb1 2018-09-29 stsp static const struct got_error *
4369 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4371 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4373 917d79a7 2022-07-01 stsp view->count = 0;
4374 917d79a7 2022-07-01 stsp wclear(view->window);
4375 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4376 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4377 917d79a7 2022-07-01 stsp s->matched_line = 0;
4378 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4379 917d79a7 2022-07-01 stsp return create_diff(s);
4382 917d79a7 2022-07-01 stsp static const struct got_error *
4383 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4385 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4386 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4387 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4388 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4389 826082fe 2020-12-10 stsp char *line = NULL;
4390 826082fe 2020-12-10 stsp size_t linesize = 0;
4391 826082fe 2020-12-10 stsp ssize_t linelen;
4392 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4394 e5a0f69f 2018-08-18 stsp switch (ch) {
4396 145b6838 2022-06-16 stsp view->x = 0;
4399 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4400 640cd7ff 2022-06-22 mark view->count = 0;
4402 145b6838 2022-06-16 stsp case KEY_RIGHT:
4404 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4405 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4407 640cd7ff 2022-06-22 mark view->count = 0;
4409 145b6838 2022-06-16 stsp case KEY_LEFT:
4411 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4412 640cd7ff 2022-06-22 mark if (view->x <= 0)
4413 640cd7ff 2022-06-22 mark view->count = 0;
4417 3dbaef42 2020-11-24 stsp if (ch == 'a')
4418 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4419 3dbaef42 2020-11-24 stsp if (ch == 'w')
4420 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4421 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4424 912a3f79 2021-08-30 j case KEY_HOME:
4425 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4426 640cd7ff 2022-06-22 mark view->count = 0;
4429 912a3f79 2021-08-30 j case KEY_END:
4430 640cd7ff 2022-06-22 mark view->count = 0;
4434 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4438 1e37a5c2 2019-05-12 jcs case KEY_UP:
4439 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4440 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4441 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4443 640cd7ff 2022-06-22 mark view->count = 0;
4445 83cc4199 2022-06-13 stsp case CTRL('u'):
4447 83cc4199 2022-06-13 stsp nscroll /= 2;
4448 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4449 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4450 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4452 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4453 640cd7ff 2022-06-22 mark view->count = 0;
4457 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4458 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4461 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4462 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4463 1e37a5c2 2019-05-12 jcs if (!s->eof)
4464 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4466 640cd7ff 2022-06-22 mark view->count = 0;
4468 83cc4199 2022-06-13 stsp case CTRL('d'):
4470 83cc4199 2022-06-13 stsp nscroll /= 2;
4471 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4472 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4473 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4476 640cd7ff 2022-06-22 mark if (s->eof) {
4477 640cd7ff 2022-06-22 mark view->count = 0;
4481 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4482 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4483 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4484 826082fe 2020-12-10 stsp if (linelen == -1) {
4485 826082fe 2020-12-10 stsp if (feof(s->f)) {
4486 826082fe 2020-12-10 stsp s->eof = 1;
4488 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4492 826082fe 2020-12-10 stsp free(line);
4495 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4496 1e37a5c2 2019-05-12 jcs s->diff_context--;
4497 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4498 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4499 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4500 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4501 27829c9e 2020-11-21 stsp s->nlines) {
4502 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4503 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4506 640cd7ff 2022-06-22 mark view->count = 0;
4509 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4510 1e37a5c2 2019-05-12 jcs s->diff_context++;
4511 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4512 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4513 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4515 640cd7ff 2022-06-22 mark view->count = 0;
4519 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4520 640cd7ff 2022-06-22 mark view->count = 0;
4523 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4524 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4526 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4527 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4531 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4534 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4538 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4539 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4540 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4541 145b6838 2022-06-16 stsp view->x = 0;
4543 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4544 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4548 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4549 640cd7ff 2022-06-22 mark view->count = 0;
4552 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4553 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4555 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4556 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4560 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4563 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4567 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4568 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4569 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4570 145b6838 2022-06-16 stsp view->x = 0;
4572 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4573 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4576 640cd7ff 2022-06-22 mark view->count = 0;
4580 bcbd79e2 2018-08-19 stsp return err;
4583 4ed7e80c 2018-05-20 stsp static const struct got_error *
4584 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4586 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4587 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4588 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4589 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4590 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4591 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4592 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4593 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4594 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4595 3dbaef42 2020-11-24 stsp const char *errstr;
4596 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4597 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4599 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4600 26ed57b2 2018-05-19 stsp switch (ch) {
4602 64453f7e 2020-11-21 stsp force_text_diff = 1;
4605 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4607 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4608 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4609 5a20d08d 2022-02-09 op errstr, errstr);
4611 09b5bff8 2020-02-23 naddy case 'r':
4612 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4613 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4614 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4616 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4619 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4622 17020d27 2019-03-07 stsp usage_diff();
4623 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4627 26ed57b2 2018-05-19 stsp argc -= optind;
4628 26ed57b2 2018-05-19 stsp argv += optind;
4630 26ed57b2 2018-05-19 stsp if (argc == 0) {
4631 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4632 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4633 15a94983 2018-12-23 stsp id_str1 = argv[0];
4634 15a94983 2018-12-23 stsp id_str2 = argv[1];
4636 26ed57b2 2018-05-19 stsp usage_diff();
4638 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4639 0ae84acc 2022-06-15 tracey if (error)
4640 0ae84acc 2022-06-15 tracey goto done;
4642 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4643 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4644 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4645 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4646 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4647 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4648 c156c7a4 2020-12-18 stsp goto done;
4649 a273ac94 2020-02-23 naddy if (worktree)
4650 a273ac94 2020-02-23 naddy repo_path =
4651 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4653 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4654 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4655 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4656 c156c7a4 2020-12-18 stsp goto done;
4660 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4661 eb6600df 2019-01-04 stsp if (error)
4662 eb6600df 2019-01-04 stsp goto done;
4664 a273ac94 2020-02-23 naddy init_curses();
4666 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4667 51a10b52 2020-12-26 stsp if (error)
4668 51a10b52 2020-12-26 stsp goto done;
4670 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4671 26ed57b2 2018-05-19 stsp if (error)
4672 26ed57b2 2018-05-19 stsp goto done;
4674 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4675 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4676 26ed57b2 2018-05-19 stsp if (error)
4677 26ed57b2 2018-05-19 stsp goto done;
4679 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4680 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4681 26ed57b2 2018-05-19 stsp if (error)
4682 26ed57b2 2018-05-19 stsp goto done;
4684 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4685 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4686 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4687 ea5e7bb5 2018-08-01 stsp goto done;
4689 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4690 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4691 5dc9f4bc 2018-08-04 stsp if (error)
4692 5dc9f4bc 2018-08-04 stsp goto done;
4693 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4695 3dbaef42 2020-11-24 stsp free(label1);
4696 3dbaef42 2020-11-24 stsp free(label2);
4697 c02c541e 2019-03-29 stsp free(repo_path);
4698 a273ac94 2020-02-23 naddy free(cwd);
4699 1d0f4054 2021-06-17 stsp if (repo) {
4700 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4701 1d0f4054 2021-06-17 stsp if (error == NULL)
4702 1d0f4054 2021-06-17 stsp error = close_err;
4704 a273ac94 2020-02-23 naddy if (worktree)
4705 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4706 0ae84acc 2022-06-15 tracey if (pack_fds) {
4707 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4708 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4709 0ae84acc 2022-06-15 tracey if (error == NULL)
4710 0ae84acc 2022-06-15 tracey error = pack_err;
4712 51a10b52 2020-12-26 stsp tog_free_refs();
4713 26ed57b2 2018-05-19 stsp return error;
4716 4ed7e80c 2018-05-20 stsp __dead static void
4717 9f7d7167 2018-04-29 stsp usage_blame(void)
4720 87411fa9 2022-06-16 stsp fprintf(stderr,
4721 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4722 9f7d7167 2018-04-29 stsp getprogname());
4726 84451b3e 2018-07-10 stsp struct tog_blame_line {
4727 84451b3e 2018-07-10 stsp int annotated;
4728 84451b3e 2018-07-10 stsp struct got_object_id *id;
4731 4ed7e80c 2018-05-20 stsp static const struct got_error *
4732 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4734 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4735 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4736 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4737 84451b3e 2018-07-10 stsp const struct got_error *err;
4738 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4739 826082fe 2020-12-10 stsp char *line = NULL;
4740 826082fe 2020-12-10 stsp size_t linesize = 0;
4741 826082fe 2020-12-10 stsp ssize_t linelen;
4742 84451b3e 2018-07-10 stsp wchar_t *wline;
4743 27a741e5 2019-09-11 stsp int width;
4744 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4745 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4746 ab089a2a 2018-07-12 stsp char *id_str;
4747 11b20872 2019-11-08 stsp struct tog_color *tc;
4749 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4751 ab089a2a 2018-07-12 stsp return err;
4753 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4754 f7d12f7e 2018-08-01 stsp werase(view->window);
4756 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4757 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4758 ab089a2a 2018-07-12 stsp free(id_str);
4759 ab089a2a 2018-07-12 stsp return err;
4762 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4763 ab089a2a 2018-07-12 stsp free(line);
4764 2550e4c3 2018-07-13 stsp line = NULL;
4766 1cae65b4 2019-09-22 stsp return err;
4767 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4768 a3404814 2018-09-02 stsp wstandout(view->window);
4769 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4771 11b20872 2019-11-08 stsp wattr_on(view->window,
4772 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4773 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4775 11b20872 2019-11-08 stsp wattr_off(view->window,
4776 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4777 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4778 a3404814 2018-09-02 stsp wstandend(view->window);
4779 2550e4c3 2018-07-13 stsp free(wline);
4780 2550e4c3 2018-07-13 stsp wline = NULL;
4781 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4782 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4784 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4785 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4786 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4787 ab089a2a 2018-07-12 stsp free(id_str);
4788 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4790 ab089a2a 2018-07-12 stsp free(id_str);
4791 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4792 3f60a8ef 2018-07-10 stsp free(line);
4793 2550e4c3 2018-07-13 stsp line = NULL;
4795 3f60a8ef 2018-07-10 stsp return err;
4796 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4797 2550e4c3 2018-07-13 stsp free(wline);
4798 2550e4c3 2018-07-13 stsp wline = NULL;
4799 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4800 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4802 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4803 145b6838 2022-06-16 stsp view->maxx = 0;
4804 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4805 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4806 826082fe 2020-12-10 stsp if (linelen == -1) {
4807 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4808 826082fe 2020-12-10 stsp s->eof = 1;
4811 84451b3e 2018-07-10 stsp free(line);
4812 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4814 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4817 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4818 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4819 1853e0f4 2022-06-16 stsp if (err) {
4820 1853e0f4 2022-06-16 stsp free(line);
4821 1853e0f4 2022-06-16 stsp return err;
4823 1853e0f4 2022-06-16 stsp free(wline);
4824 1853e0f4 2022-06-16 stsp wline = NULL;
4825 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4827 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4828 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4830 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4831 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4832 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4833 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4834 27a741e5 2019-09-11 stsp !(view->focussed &&
4835 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4836 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4837 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4838 8d0fe45a 2019-08-12 stsp char *id_str;
4839 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4840 87411fa9 2022-06-16 stsp blame_line->id);
4841 8d0fe45a 2019-08-12 stsp if (err) {
4842 8d0fe45a 2019-08-12 stsp free(line);
4843 8d0fe45a 2019-08-12 stsp return err;
4845 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4847 11b20872 2019-11-08 stsp wattr_on(view->window,
4848 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4849 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4851 11b20872 2019-11-08 stsp wattr_off(view->window,
4852 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4853 8d0fe45a 2019-08-12 stsp free(id_str);
4854 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4856 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4857 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4860 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4861 ee41ec32 2018-07-10 stsp prev_id = NULL;
4864 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4865 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4866 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4868 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4869 41605754 2020-11-12 stsp width = 9;
4870 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4871 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4872 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4873 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4874 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4875 41605754 2020-11-12 stsp if (err) {
4876 41605754 2020-11-12 stsp free(line);
4877 41605754 2020-11-12 stsp return err;
4879 41605754 2020-11-12 stsp width += 9;
4882 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4883 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4884 4cb9d869 2022-06-16 stsp if (err) {
4885 4cb9d869 2022-06-16 stsp free(line);
4886 4cb9d869 2022-06-16 stsp return err;
4888 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4889 a75b3e2d 2022-06-16 stsp width += 9;
4890 41605754 2020-11-12 stsp free(wline);
4891 41605754 2020-11-12 stsp wline = NULL;
4894 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4895 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4896 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4897 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4899 826082fe 2020-12-10 stsp free(line);
4900 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4902 9b058f45 2022-06-30 mark view_border(view);
4904 84451b3e 2018-07-10 stsp return NULL;
4907 84451b3e 2018-07-10 stsp static const struct got_error *
4908 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4909 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4911 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4912 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4913 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4914 1a76625f 2018-10-22 stsp int errcode;
4916 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4917 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4918 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4920 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4921 1a76625f 2018-10-22 stsp if (errcode)
4922 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4924 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4925 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4926 d68a0a7d 2018-07-10 stsp goto done;
4929 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4930 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4932 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4933 d68a0a7d 2018-07-10 stsp if (line->annotated)
4934 d68a0a7d 2018-07-10 stsp goto done;
4936 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4937 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4938 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4939 84451b3e 2018-07-10 stsp goto done;
4941 84451b3e 2018-07-10 stsp line->annotated = 1;
4943 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4944 1a76625f 2018-10-22 stsp if (errcode)
4945 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4946 84451b3e 2018-07-10 stsp return err;
4949 84451b3e 2018-07-10 stsp static void *
4950 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4952 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4953 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4954 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4955 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
4956 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
4958 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
4959 e6e73e55 2022-06-30 tracey if (fd1 == -1)
4960 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
4962 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
4963 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
4964 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
4965 e6e73e55 2022-06-30 tracey goto done;
4968 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
4969 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
4970 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4971 e6e73e55 2022-06-30 tracey goto done;
4973 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
4974 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
4975 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4976 e6e73e55 2022-06-30 tracey goto done;
4979 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4981 e6e73e55 2022-06-30 tracey goto done;
4983 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4984 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
4985 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
4986 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4987 fc06ba56 2019-08-22 stsp err = NULL;
4989 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4990 e6e73e55 2022-06-30 tracey if (errcode) {
4991 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
4992 e6e73e55 2022-06-30 tracey goto done;
4995 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4996 1d0f4054 2021-06-17 stsp if (err == NULL)
4997 1d0f4054 2021-06-17 stsp err = close_err;
4998 c9beca56 2018-07-22 stsp ta->repo = NULL;
4999 c9beca56 2018-07-22 stsp *ta->complete = 1;
5001 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5002 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5003 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5006 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5007 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5008 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5009 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5010 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5011 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5012 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5013 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5015 18430de3 2018-07-10 stsp return (void *)err;
5018 245d91c1 2018-07-12 stsp static struct got_object_id *
5019 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5020 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5022 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5024 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5025 8d0fe45a 2019-08-12 stsp return NULL;
5027 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5028 245d91c1 2018-07-12 stsp if (!line->annotated)
5029 245d91c1 2018-07-12 stsp return NULL;
5031 245d91c1 2018-07-12 stsp return line->id;
5034 b880a918 2018-07-10 stsp static const struct got_error *
5035 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5037 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5040 245d91c1 2018-07-12 stsp if (blame->thread) {
5041 1a76625f 2018-10-22 stsp int errcode;
5042 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5043 1a76625f 2018-10-22 stsp if (errcode)
5044 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5045 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5046 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5047 1a76625f 2018-10-22 stsp if (errcode)
5048 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5049 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5050 1a76625f 2018-10-22 stsp if (errcode)
5051 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5052 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5053 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5054 245d91c1 2018-07-12 stsp err = NULL;
5055 245d91c1 2018-07-12 stsp blame->thread = NULL;
5057 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5058 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5059 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5060 1d0f4054 2021-06-17 stsp if (err == NULL)
5061 1d0f4054 2021-06-17 stsp err = close_err;
5062 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5064 245d91c1 2018-07-12 stsp if (blame->f) {
5065 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5066 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5067 245d91c1 2018-07-12 stsp blame->f = NULL;
5069 57670559 2018-12-23 stsp if (blame->lines) {
5070 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5071 57670559 2018-12-23 stsp free(blame->lines[i].id);
5072 57670559 2018-12-23 stsp free(blame->lines);
5073 57670559 2018-12-23 stsp blame->lines = NULL;
5075 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5076 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5077 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5078 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5079 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5080 0ae84acc 2022-06-15 tracey if (err == NULL)
5081 0ae84acc 2022-06-15 tracey err = pack_err;
5082 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5084 245d91c1 2018-07-12 stsp return err;
5087 245d91c1 2018-07-12 stsp static const struct got_error *
5088 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5090 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5091 fc06ba56 2019-08-22 stsp int *done = arg;
5092 fc06ba56 2019-08-22 stsp int errcode;
5094 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5095 fc06ba56 2019-08-22 stsp if (errcode)
5096 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5097 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5099 fc06ba56 2019-08-22 stsp if (*done)
5100 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5102 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5103 fc06ba56 2019-08-22 stsp if (errcode)
5104 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5105 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5107 fc06ba56 2019-08-22 stsp return err;
5110 fc06ba56 2019-08-22 stsp static const struct got_error *
5111 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5113 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5114 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5115 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5116 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5117 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5118 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5119 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5120 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5121 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5123 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5124 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5126 15a94983 2018-12-23 stsp return err;
5128 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5129 eb81bc23 2022-06-28 tracey if (fd == -1) {
5130 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5131 eb81bc23 2022-06-28 tracey goto done;
5134 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5136 a44927cc 2022-04-07 stsp goto done;
5138 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5140 84451b3e 2018-07-10 stsp goto done;
5142 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5143 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5144 84451b3e 2018-07-10 stsp goto done;
5147 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5149 a70480e0 2018-06-23 stsp goto done;
5150 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5151 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5152 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5153 84451b3e 2018-07-10 stsp goto done;
5155 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5156 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5158 1fddf795 2021-01-20 stsp goto done;
5159 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5160 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5161 84451b3e 2018-07-10 stsp goto done;
5164 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5165 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5166 b02560ec 2019-08-19 stsp blame->nlines--;
5168 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5169 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5170 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5171 84451b3e 2018-07-10 stsp goto done;
5174 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5176 bd24772e 2018-07-11 stsp goto done;
5177 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5178 0ae84acc 2022-06-15 tracey pack_fds);
5179 0ae84acc 2022-06-15 tracey if (err)
5180 0ae84acc 2022-06-15 tracey goto done;
5182 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5183 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5184 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5185 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5186 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5187 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5188 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5189 245d91c1 2018-07-12 stsp goto done;
5191 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5193 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5194 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5195 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5196 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5197 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5198 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5199 a5388363 2020-12-01 naddy s->blame_complete = 0;
5201 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5202 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5203 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5204 f5a09613 2020-12-13 naddy s->selected_line = 1;
5206 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5209 a44927cc 2022-04-07 stsp if (commit)
5210 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5211 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5212 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5214 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5215 27d434c2 2018-09-15 stsp free(obj_id);
5217 245d91c1 2018-07-12 stsp stop_blame(blame);
5218 245d91c1 2018-07-12 stsp return err;
5221 245d91c1 2018-07-12 stsp static const struct got_error *
5222 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5223 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5225 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5226 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5228 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5230 c4843652 2019-08-12 stsp s->path = strdup(path);
5231 c4843652 2019-08-12 stsp if (s->path == NULL)
5232 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5234 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5235 c4843652 2019-08-12 stsp if (err) {
5236 c4843652 2019-08-12 stsp free(s->path);
5237 7cbe629d 2018-08-04 stsp return err;
5240 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5241 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5242 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5243 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5244 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5245 fb2756b9 2018-08-04 stsp s->repo = repo;
5246 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5247 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5249 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5250 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5251 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5252 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5254 11b20872 2019-11-08 stsp return err;
5257 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5258 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5259 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5260 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5261 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5262 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5264 a5388363 2020-12-01 naddy return run_blame(view);
5267 e5a0f69f 2018-08-18 stsp static const struct got_error *
5268 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5270 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5271 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5273 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5274 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5276 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5277 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5278 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5279 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5280 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5283 e5a0f69f 2018-08-18 stsp free(s->path);
5284 11b20872 2019-11-08 stsp free_colors(&s->colors);
5285 e5a0f69f 2018-08-18 stsp return err;
5288 7cbe629d 2018-08-04 stsp static const struct got_error *
5289 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5291 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5293 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5294 6c4c42e0 2019-06-24 stsp return NULL;
5297 6c4c42e0 2019-06-24 stsp static const struct got_error *
5298 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5300 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5301 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5302 6c4c42e0 2019-06-24 stsp int lineno;
5303 cb713507 2022-06-16 stsp char *line = NULL;
5304 826082fe 2020-12-10 stsp size_t linesize = 0;
5305 826082fe 2020-12-10 stsp ssize_t linelen;
5307 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5308 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5309 6c4c42e0 2019-06-24 stsp return NULL;
5312 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5313 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5314 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5316 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5318 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5320 6c4c42e0 2019-06-24 stsp while (1) {
5321 6c4c42e0 2019-06-24 stsp off_t offset;
5323 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5324 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5325 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5329 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5330 6c4c42e0 2019-06-24 stsp lineno = 1;
5332 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5335 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5336 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5337 6c4c42e0 2019-06-24 stsp free(line);
5338 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5340 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5341 cb713507 2022-06-16 stsp if (linelen != -1) {
5342 cb713507 2022-06-16 stsp char *exstr;
5343 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5346 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5347 cb713507 2022-06-16 stsp &view->regmatch)) {
5348 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5349 cb713507 2022-06-16 stsp s->matched_line = lineno;
5350 cb713507 2022-06-16 stsp free(exstr);
5353 cb713507 2022-06-16 stsp free(exstr);
5355 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5360 826082fe 2020-12-10 stsp free(line);
5362 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5363 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5364 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5367 145b6838 2022-06-16 stsp return err;
5370 6c4c42e0 2019-06-24 stsp static const struct got_error *
5371 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5373 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5374 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5375 2b380cc8 2018-10-24 stsp int errcode;
5377 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5378 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5379 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5380 2b380cc8 2018-10-24 stsp if (errcode)
5381 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5383 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5386 51fe7530 2019-08-19 stsp if (s->blame_complete)
5387 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5389 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5391 9b058f45 2022-06-30 mark view_border(view);
5392 e5a0f69f 2018-08-18 stsp return err;
5395 e5a0f69f 2018-08-18 stsp static const struct got_error *
5396 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5398 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5399 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5400 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5401 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5403 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5404 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5405 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5406 9b058f45 2022-06-30 mark --eos; /* border */
5408 e5a0f69f 2018-08-18 stsp switch (ch) {
5410 145b6838 2022-06-16 stsp view->x = 0;
5413 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5414 640cd7ff 2022-06-22 mark view->count = 0;
5416 145b6838 2022-06-16 stsp case KEY_RIGHT:
5418 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5419 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5421 640cd7ff 2022-06-22 mark view->count = 0;
5423 145b6838 2022-06-16 stsp case KEY_LEFT:
5425 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5426 640cd7ff 2022-06-22 mark if (view->x <= 0)
5427 640cd7ff 2022-06-22 mark view->count = 0;
5430 1e37a5c2 2019-05-12 jcs s->done = 1;
5432 4deef56f 2021-09-02 naddy case 'g':
5433 4deef56f 2021-09-02 naddy case KEY_HOME:
5434 4deef56f 2021-09-02 naddy s->selected_line = 1;
5435 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5436 640cd7ff 2022-06-22 mark view->count = 0;
5438 4deef56f 2021-09-02 naddy case 'G':
5439 4deef56f 2021-09-02 naddy case KEY_END:
5440 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5441 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5442 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5444 9b058f45 2022-06-30 mark s->selected_line = eos;
5445 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5447 640cd7ff 2022-06-22 mark view->count = 0;
5450 1e37a5c2 2019-05-12 jcs case KEY_UP:
5451 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5452 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5453 1e37a5c2 2019-05-12 jcs s->selected_line--;
5454 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5455 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5456 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5458 640cd7ff 2022-06-22 mark view->count = 0;
5460 83cc4199 2022-06-13 stsp case CTRL('u'):
5462 83cc4199 2022-06-13 stsp nscroll /= 2;
5463 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5464 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5465 ea025d1d 2020-02-22 naddy case CTRL('b'):
5467 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5468 640cd7ff 2022-06-22 mark if (view->count > 1)
5469 640cd7ff 2022-06-22 mark nscroll += nscroll;
5470 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5471 640cd7ff 2022-06-22 mark view->count = 0;
5474 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5475 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5477 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5480 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5481 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5482 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5483 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5484 1e37a5c2 2019-05-12 jcs s->selected_line++;
5485 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5486 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5488 640cd7ff 2022-06-22 mark view->count = 0;
5491 1e37a5c2 2019-05-12 jcs case 'p': {
5492 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5494 640cd7ff 2022-06-22 mark view->count = 0;
5495 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5496 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5497 1e37a5c2 2019-05-12 jcs if (id == NULL)
5499 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5500 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5501 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5502 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5503 1e37a5c2 2019-05-12 jcs int obj_type;
5504 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5505 1e37a5c2 2019-05-12 jcs s->repo, id);
5508 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5509 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5510 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5511 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5514 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5515 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5516 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5519 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5520 a44927cc 2022-04-07 stsp pcommit, s->path);
5521 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5522 e5a0f69f 2018-08-18 stsp if (err) {
5523 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5524 1e37a5c2 2019-05-12 jcs err = NULL;
5525 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5528 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5530 1e37a5c2 2019-05-12 jcs free(blob_id);
5531 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5532 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5533 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5536 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5537 d7b5a0e8 2022-04-20 stsp &pid->id);
5538 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5540 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5541 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5543 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5548 1e37a5c2 2019-05-12 jcs s->done = 1;
5549 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5550 1e37a5c2 2019-05-12 jcs s->done = 0;
5551 1e37a5c2 2019-05-12 jcs if (thread_err)
5553 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5554 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5555 a5388363 2020-12-01 naddy err = run_blame(view);
5560 61417565 2022-06-20 mark case 'C': {
5561 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5563 640cd7ff 2022-06-22 mark view->count = 0;
5564 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5565 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5567 1e37a5c2 2019-05-12 jcs s->done = 1;
5568 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5569 1e37a5c2 2019-05-12 jcs s->done = 0;
5570 1e37a5c2 2019-05-12 jcs if (thread_err)
5572 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5573 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5574 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5575 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5576 a5388363 2020-12-01 naddy err = run_blame(view);
5581 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5582 1e37a5c2 2019-05-12 jcs case '\r': {
5583 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5584 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5585 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5587 640cd7ff 2022-06-22 mark view->count = 0;
5588 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5589 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5590 1e37a5c2 2019-05-12 jcs if (id == NULL)
5592 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5595 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5596 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5597 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
5599 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5600 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5601 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5602 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5605 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5606 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5607 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5609 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5612 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5613 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5618 e78dc838 2020-12-04 stsp view->focussed = 0;
5619 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5620 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5621 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5622 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5623 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5626 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5629 e78dc838 2020-12-04 stsp view->focus_child = 1;
5631 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5636 83cc4199 2022-06-13 stsp case CTRL('d'):
5638 83cc4199 2022-06-13 stsp nscroll /= 2;
5639 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5640 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5641 ea025d1d 2020-02-22 naddy case CTRL('f'):
5644 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5645 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5646 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5647 640cd7ff 2022-06-22 mark view->count = 0;
5650 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5651 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5652 83cc4199 2022-06-13 stsp s->selected_line +=
5653 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5654 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5656 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5657 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5659 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5660 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5662 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5663 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5664 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5665 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5669 640cd7ff 2022-06-22 mark view->count = 0;
5672 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5675 917d79a7 2022-07-01 stsp static const struct got_error *
5676 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
5678 917d79a7 2022-07-01 stsp const struct got_error *err;
5679 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
5681 917d79a7 2022-07-01 stsp view->count = 0;
5682 917d79a7 2022-07-01 stsp s->done = 1;
5683 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
5684 917d79a7 2022-07-01 stsp s->done = 0;
5686 917d79a7 2022-07-01 stsp return err;
5687 917d79a7 2022-07-01 stsp return run_blame(view);
5690 a70480e0 2018-06-23 stsp static const struct got_error *
5691 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5693 a70480e0 2018-06-23 stsp const struct got_error *error;
5694 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5695 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5696 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5697 0587e10c 2020-07-23 stsp char *link_target = NULL;
5698 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5699 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5700 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5702 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5703 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5705 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5706 a70480e0 2018-06-23 stsp switch (ch) {
5708 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5711 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5712 69069811 2018-08-02 stsp if (repo_path == NULL)
5713 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5717 17020d27 2019-03-07 stsp usage_blame();
5718 a70480e0 2018-06-23 stsp /* NOTREACHED */
5722 a70480e0 2018-06-23 stsp argc -= optind;
5723 a70480e0 2018-06-23 stsp argv += optind;
5725 f135c941 2020-02-20 stsp if (argc != 1)
5726 a70480e0 2018-06-23 stsp usage_blame();
5728 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5729 0ae84acc 2022-06-15 tracey if (error != NULL)
5730 0ae84acc 2022-06-15 tracey goto done;
5732 69069811 2018-08-02 stsp if (repo_path == NULL) {
5733 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5734 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5735 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5736 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5737 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5738 c156c7a4 2020-12-18 stsp goto done;
5739 f135c941 2020-02-20 stsp if (worktree)
5740 eb41ed75 2019-02-05 stsp repo_path =
5741 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5743 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5744 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5745 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5746 c156c7a4 2020-12-18 stsp goto done;
5750 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5751 c02c541e 2019-03-29 stsp if (error != NULL)
5752 8e94dd5b 2019-01-04 stsp goto done;
5754 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5755 f135c941 2020-02-20 stsp worktree);
5756 c02c541e 2019-03-29 stsp if (error)
5757 92205607 2019-01-04 stsp goto done;
5759 f135c941 2020-02-20 stsp init_curses();
5761 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5762 51a10b52 2020-12-26 stsp if (error)
5763 51a10b52 2020-12-26 stsp goto done;
5765 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5766 eb41ed75 2019-02-05 stsp if (error)
5767 69069811 2018-08-02 stsp goto done;
5769 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5770 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5771 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5772 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5773 a70480e0 2018-06-23 stsp if (error != NULL)
5774 66b4983c 2018-06-23 stsp goto done;
5775 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5776 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5778 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5779 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5781 a19e88aa 2018-06-23 stsp if (error != NULL)
5782 8b473291 2019-02-21 stsp goto done;
5784 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5785 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5786 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5787 e1cd8fed 2018-08-01 stsp goto done;
5790 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5791 a44927cc 2022-04-07 stsp if (error)
5792 a44927cc 2022-04-07 stsp goto done;
5794 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5795 a44927cc 2022-04-07 stsp commit, repo);
5796 7cbe629d 2018-08-04 stsp if (error)
5797 7cbe629d 2018-08-04 stsp goto done;
5799 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5800 78756c87 2020-11-24 stsp commit_id, repo);
5801 0587e10c 2020-07-23 stsp if (error)
5802 0587e10c 2020-07-23 stsp goto done;
5803 12314ad4 2019-08-31 stsp if (worktree) {
5804 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5805 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5806 12314ad4 2019-08-31 stsp worktree = NULL;
5808 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5810 69069811 2018-08-02 stsp free(repo_path);
5811 f135c941 2020-02-20 stsp free(in_repo_path);
5812 0587e10c 2020-07-23 stsp free(link_target);
5813 69069811 2018-08-02 stsp free(cwd);
5814 a70480e0 2018-06-23 stsp free(commit_id);
5815 a44927cc 2022-04-07 stsp if (commit)
5816 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5817 eb41ed75 2019-02-05 stsp if (worktree)
5818 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5819 1d0f4054 2021-06-17 stsp if (repo) {
5820 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5821 1d0f4054 2021-06-17 stsp if (error == NULL)
5822 1d0f4054 2021-06-17 stsp error = close_err;
5824 0ae84acc 2022-06-15 tracey if (pack_fds) {
5825 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5826 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5827 0ae84acc 2022-06-15 tracey if (error == NULL)
5828 0ae84acc 2022-06-15 tracey error = pack_err;
5830 51a10b52 2020-12-26 stsp tog_free_refs();
5831 a70480e0 2018-06-23 stsp return error;
5834 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5835 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5837 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5838 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5839 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5840 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5841 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5842 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5843 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5845 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5846 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5847 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5848 9b058f45 2022-06-30 mark --limit; /* border */
5850 f7d12f7e 2018-08-01 stsp werase(view->window);
5852 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5853 ffd1d5e5 2018-06-23 stsp return NULL;
5855 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5858 ffd1d5e5 2018-06-23 stsp return err;
5859 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5860 a3404814 2018-09-02 stsp wstandout(view->window);
5861 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5863 11b20872 2019-11-08 stsp wattr_on(view->window,
5864 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5865 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5867 11b20872 2019-11-08 stsp wattr_off(view->window,
5868 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5869 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5870 a3404814 2018-09-02 stsp wstandend(view->window);
5871 2550e4c3 2018-07-13 stsp free(wline);
5872 2550e4c3 2018-07-13 stsp wline = NULL;
5873 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5874 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5875 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5876 ffd1d5e5 2018-06-23 stsp return NULL;
5877 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5880 ce52c690 2018-06-23 stsp return err;
5881 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5882 2550e4c3 2018-07-13 stsp free(wline);
5883 2550e4c3 2018-07-13 stsp wline = NULL;
5884 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5885 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5886 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5887 ffd1d5e5 2018-06-23 stsp return NULL;
5888 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5889 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5890 a1eca9bb 2018-06-23 stsp return NULL;
5892 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5893 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5894 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5895 0cf4efb1 2018-09-29 stsp if (view->focussed)
5896 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5897 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5899 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5900 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5901 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5902 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5903 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5904 ffd1d5e5 2018-06-23 stsp return NULL;
5908 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5911 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5912 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5913 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5914 848d6979 2019-08-12 stsp const char *modestr = "";
5915 56e0773d 2019-11-28 stsp mode_t mode;
5917 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5918 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5920 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5921 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5922 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5924 638f9024 2019-05-13 stsp return got_error_from_errno(
5925 230a42bd 2019-05-11 jcs "got_object_id_str");
5927 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5928 63c5ca5d 2019-08-24 stsp modestr = "$";
5929 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5932 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5933 d86d3b18 2020-12-01 naddy te, s->repo);
5934 0d6c6ee3 2020-05-20 stsp if (err) {
5935 0d6c6ee3 2020-05-20 stsp free(id_str);
5936 0d6c6ee3 2020-05-20 stsp return err;
5938 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5939 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5940 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5942 848d6979 2019-08-12 stsp modestr = "@";
5944 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5945 848d6979 2019-08-12 stsp modestr = "/";
5946 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5947 848d6979 2019-08-12 stsp modestr = "*";
5948 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5949 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5950 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5951 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5952 1d13200f 2018-07-12 stsp free(id_str);
5953 0d6c6ee3 2020-05-20 stsp free(link_target);
5954 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5956 1d13200f 2018-07-12 stsp free(id_str);
5957 0d6c6ee3 2020-05-20 stsp free(link_target);
5958 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5960 ffd1d5e5 2018-06-23 stsp if (err) {
5961 ffd1d5e5 2018-06-23 stsp free(line);
5964 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5965 0cf4efb1 2018-09-29 stsp if (view->focussed)
5966 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5967 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5969 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5971 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5972 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5973 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5975 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5976 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5977 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5978 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5979 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5980 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5981 ffd1d5e5 2018-06-23 stsp free(line);
5982 2550e4c3 2018-07-13 stsp free(wline);
5983 2550e4c3 2018-07-13 stsp wline = NULL;
5985 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5986 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5987 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5991 ffd1d5e5 2018-06-23 stsp return err;
5994 ffd1d5e5 2018-06-23 stsp static void
5995 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5997 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5998 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5999 fa86c4bf 2020-11-29 stsp int i = 0;
6001 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6004 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6005 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6006 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6007 fa86c4bf 2020-11-29 stsp if (!isroot)
6008 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6011 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6012 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6016 9b058f45 2022-06-30 mark static const struct got_error *
6017 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6019 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6020 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6021 ffd1d5e5 2018-06-23 stsp int n = 0;
6023 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6024 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6025 694d3271 2020-12-01 naddy s->first_displayed_entry);
6027 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6029 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6030 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6032 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6033 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6034 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6035 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6039 9b058f45 2022-06-30 mark return NULL;
6042 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6043 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6044 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6046 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6047 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6048 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6050 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6051 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6052 56e0773d 2019-11-28 stsp + 1 /* slash */;
6054 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6056 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6057 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6058 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6060 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6061 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6062 d9765a41 2018-06-23 stsp while (pt) {
6063 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6064 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6065 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6066 cb2ebc8a 2018-06-23 stsp goto done;
6068 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6069 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6070 cb2ebc8a 2018-06-23 stsp goto done;
6072 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6075 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6076 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6077 ce52c690 2018-06-23 stsp goto done;
6081 ce52c690 2018-06-23 stsp if (err) {
6082 ce52c690 2018-06-23 stsp free(*path);
6083 ce52c690 2018-06-23 stsp *path = NULL;
6085 ce52c690 2018-06-23 stsp return err;
6088 ce52c690 2018-06-23 stsp static const struct got_error *
6089 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6090 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6091 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6093 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6094 ce52c690 2018-06-23 stsp char *path;
6095 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6097 a0de39f3 2019-08-09 stsp *new_view = NULL;
6099 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6101 ce52c690 2018-06-23 stsp return err;
6103 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6104 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6105 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6106 83ce39e3 2019-08-12 stsp goto done;
6109 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6110 e5a0f69f 2018-08-18 stsp if (err) {
6111 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6112 fc06ba56 2019-08-22 stsp err = NULL;
6113 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6115 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6117 83ce39e3 2019-08-12 stsp free(path);
6118 69efd4c4 2018-07-18 stsp return err;
6121 69efd4c4 2018-07-18 stsp static const struct got_error *
6122 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6123 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6125 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6126 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6127 69efd4c4 2018-07-18 stsp char *path;
6129 a0de39f3 2019-08-09 stsp *new_view = NULL;
6131 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6132 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6133 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6135 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6137 69efd4c4 2018-07-18 stsp return err;
6139 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6142 e5a0f69f 2018-08-18 stsp view_close(log_view);
6144 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6145 cb2ebc8a 2018-06-23 stsp free(path);
6146 cb2ebc8a 2018-06-23 stsp return err;
6149 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6150 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6151 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6153 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6154 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6155 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6156 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6158 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6159 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6161 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6162 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6163 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6165 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6167 bc573f3b 2021-07-10 stsp goto done;
6170 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6171 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6172 bc573f3b 2021-07-10 stsp * closed on demand.
6174 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6175 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6177 bc573f3b 2021-07-10 stsp goto done;
6178 bc573f3b 2021-07-10 stsp s->tree = s->root;
6180 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6181 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6182 ffd1d5e5 2018-06-23 stsp goto done;
6184 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6185 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6186 ffd1d5e5 2018-06-23 stsp goto done;
6189 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6190 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6191 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6192 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6193 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6194 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6195 9cd7cbd1 2020-12-07 stsp goto done;
6198 fb2756b9 2018-08-04 stsp s->repo = repo;
6200 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6201 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6202 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6203 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6205 c0b01bdb 2019-11-08 stsp goto done;
6206 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6207 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6209 c0b01bdb 2019-11-08 stsp goto done;
6210 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6211 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6212 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6214 c0b01bdb 2019-11-08 stsp goto done;
6216 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6217 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6218 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6220 11b20872 2019-11-08 stsp goto done;
6222 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6223 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6225 c0b01bdb 2019-11-08 stsp goto done;
6228 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6229 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6230 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6231 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6232 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6234 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6235 bc573f3b 2021-07-10 stsp if (commit)
6236 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6238 bc573f3b 2021-07-10 stsp close_tree_view(view);
6239 ad80ab7b 2018-08-04 stsp return err;
6242 e5a0f69f 2018-08-18 stsp static const struct got_error *
6243 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6245 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6247 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6248 fb2756b9 2018-08-04 stsp free(s->tree_label);
6249 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6250 6484ec90 2018-09-29 stsp free(s->commit_id);
6251 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6252 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6253 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6254 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6255 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6256 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6257 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6258 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6259 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6260 ad80ab7b 2018-08-04 stsp free(parent);
6263 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6264 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6265 bc573f3b 2021-07-10 stsp if (s->root)
6266 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6267 7c32bd05 2019-06-22 stsp return NULL;
6270 7c32bd05 2019-06-22 stsp static const struct got_error *
6271 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6273 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6275 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6276 7c32bd05 2019-06-22 stsp return NULL;
6279 7c32bd05 2019-06-22 stsp static int
6280 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6282 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6284 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6288 7c32bd05 2019-06-22 stsp static const struct got_error *
6289 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6291 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6292 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6294 7c32bd05 2019-06-22 stsp if (!view->searching) {
6295 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6296 7c32bd05 2019-06-22 stsp return NULL;
6299 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6300 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6301 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6302 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6303 56e0773d 2019-11-28 stsp s->selected_entry);
6305 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6307 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6308 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6310 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6311 56e0773d 2019-11-28 stsp s->selected_entry);
6314 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6315 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6316 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6317 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6319 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6322 7c32bd05 2019-06-22 stsp while (1) {
6323 56e0773d 2019-11-28 stsp if (te == NULL) {
6324 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6325 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6326 ac66afb8 2019-06-24 stsp return NULL;
6328 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6329 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6331 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6334 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6335 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6336 56e0773d 2019-11-28 stsp s->matched_entry = te;
6340 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6341 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6343 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6346 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6347 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6348 7c32bd05 2019-06-22 stsp s->selected = 0;
6351 e5a0f69f 2018-08-18 stsp return NULL;
6354 ad80ab7b 2018-08-04 stsp static const struct got_error *
6355 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6357 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6358 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6359 e5a0f69f 2018-08-18 stsp char *parent_path;
6361 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6363 e5a0f69f 2018-08-18 stsp return err;
6365 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6366 e5a0f69f 2018-08-18 stsp free(parent_path);
6368 9b058f45 2022-06-30 mark view_border(view);
6369 e5a0f69f 2018-08-18 stsp return err;
6372 e5a0f69f 2018-08-18 stsp static const struct got_error *
6373 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6375 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6376 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6377 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6378 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6379 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
6381 e5a0f69f 2018-08-18 stsp switch (ch) {
6383 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6384 640cd7ff 2022-06-22 mark view->count = 0;
6387 640cd7ff 2022-06-22 mark view->count = 0;
6388 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6390 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6391 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
6392 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
6393 e78dc838 2020-12-04 stsp view->focussed = 0;
6394 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6395 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6396 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6398 1e37a5c2 2019-05-12 jcs return err;
6399 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6402 e78dc838 2020-12-04 stsp view->focus_child = 1;
6404 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6407 640cd7ff 2022-06-22 mark view->count = 0;
6408 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6409 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
6410 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
6411 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
6412 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6413 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6414 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6415 152c1c93 2020-11-29 stsp if (err) {
6416 152c1c93 2020-11-29 stsp view_close(ref_view);
6417 152c1c93 2020-11-29 stsp return err;
6419 e78dc838 2020-12-04 stsp view->focussed = 0;
6420 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6421 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6422 152c1c93 2020-11-29 stsp err = view_close_child(view);
6424 152c1c93 2020-11-29 stsp return err;
6425 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6428 e78dc838 2020-12-04 stsp view->focus_child = 1;
6430 152c1c93 2020-11-29 stsp *new_view = ref_view;
6432 e4526bf5 2021-09-03 naddy case 'g':
6433 e4526bf5 2021-09-03 naddy case KEY_HOME:
6434 e4526bf5 2021-09-03 naddy s->selected = 0;
6435 640cd7ff 2022-06-22 mark view->count = 0;
6436 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6437 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6438 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6440 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6442 e4526bf5 2021-09-03 naddy case 'G':
6443 9b058f45 2022-06-30 mark case KEY_END: {
6444 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6446 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6447 9b058f45 2022-06-30 mark --eos; /* border */
6448 e4526bf5 2021-09-03 naddy s->selected = 0;
6449 640cd7ff 2022-06-22 mark view->count = 0;
6450 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6451 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6452 e4526bf5 2021-09-03 naddy if (te == NULL) {
6453 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6454 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6459 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6460 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6462 e4526bf5 2021-09-03 naddy if (n > 0)
6463 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6467 1e37a5c2 2019-05-12 jcs case KEY_UP:
6468 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6469 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6470 1e37a5c2 2019-05-12 jcs s->selected--;
6473 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6474 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6475 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6476 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6477 640cd7ff 2022-06-22 mark view->count = 0;
6479 83cc4199 2022-06-13 stsp case CTRL('u'):
6481 83cc4199 2022-06-13 stsp nscroll /= 2;
6482 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6483 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6484 ea025d1d 2020-02-22 naddy case CTRL('b'):
6486 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6487 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6488 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6489 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6491 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6492 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6494 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6495 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6496 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6497 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6498 640cd7ff 2022-06-22 mark view->count = 0;
6501 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6502 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6503 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6504 1e37a5c2 2019-05-12 jcs s->selected++;
6507 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6508 640cd7ff 2022-06-22 mark == NULL) {
6509 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6510 640cd7ff 2022-06-22 mark view->count = 0;
6513 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6515 83cc4199 2022-06-13 stsp case CTRL('d'):
6517 83cc4199 2022-06-13 stsp nscroll /= 2;
6518 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6519 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6520 ea025d1d 2020-02-22 naddy case CTRL('f'):
6522 48bb96f0 2022-06-20 naddy case ' ':
6523 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6525 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6526 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6527 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6528 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6530 640cd7ff 2022-06-22 mark view->count = 0;
6533 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6535 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6537 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6538 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6539 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6540 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6541 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6542 640cd7ff 2022-06-22 mark view->count = 0;
6545 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6546 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6548 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6549 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6550 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6551 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6552 1e37a5c2 2019-05-12 jcs s->selected_entry =
6553 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6554 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6555 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6556 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6560 1e37a5c2 2019-05-12 jcs free(parent);
6561 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6562 56e0773d 2019-11-28 stsp s->selected_entry))) {
6563 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6564 640cd7ff 2022-06-22 mark view->count = 0;
6565 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6566 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6569 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6570 941e9f74 2019-05-21 stsp if (err) {
6571 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6574 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6575 56e0773d 2019-11-28 stsp s->selected_entry))) {
6576 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6577 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6579 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6580 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6582 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6583 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6584 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6588 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6589 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6594 640cd7ff 2022-06-22 mark view->count = 0;
6595 e78dc838 2020-12-04 stsp view->focussed = 0;
6596 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6597 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6598 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6599 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6600 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6602 669b5ffa 2018-10-07 stsp return err;
6603 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6606 e78dc838 2020-12-04 stsp view->focus_child = 1;
6608 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6611 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6612 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6613 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6614 640cd7ff 2022-06-22 mark view->count = 0;
6617 640cd7ff 2022-06-22 mark view->count = 0;
6621 ffd1d5e5 2018-06-23 stsp return err;
6624 ffd1d5e5 2018-06-23 stsp __dead static void
6625 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6628 87411fa9 2022-06-16 stsp fprintf(stderr,
6629 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6630 ffd1d5e5 2018-06-23 stsp getprogname());
6634 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6635 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6637 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6638 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6639 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6640 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6641 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6642 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6643 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6644 4e97c21c 2020-12-06 stsp char *label = NULL;
6645 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6646 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6648 5221c383 2018-08-01 stsp struct tog_view *view;
6649 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6651 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6652 ffd1d5e5 2018-06-23 stsp switch (ch) {
6654 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6657 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6658 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6659 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6663 e99e2d15 2020-11-24 naddy usage_tree();
6664 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6668 ffd1d5e5 2018-06-23 stsp argc -= optind;
6669 ffd1d5e5 2018-06-23 stsp argv += optind;
6671 55cccc34 2020-02-20 stsp if (argc > 1)
6672 e99e2d15 2020-11-24 naddy usage_tree();
6674 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6675 0ae84acc 2022-06-15 tracey if (error != NULL)
6676 0ae84acc 2022-06-15 tracey goto done;
6678 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6679 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6680 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6681 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6682 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6683 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6684 c156c7a4 2020-12-18 stsp goto done;
6685 55cccc34 2020-02-20 stsp if (worktree)
6686 52185f70 2019-02-05 stsp repo_path =
6687 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6689 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6690 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6691 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6692 c156c7a4 2020-12-18 stsp goto done;
6696 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6697 c02c541e 2019-03-29 stsp if (error != NULL)
6698 52185f70 2019-02-05 stsp goto done;
6700 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6701 55cccc34 2020-02-20 stsp repo, worktree);
6702 55cccc34 2020-02-20 stsp if (error)
6703 55cccc34 2020-02-20 stsp goto done;
6705 55cccc34 2020-02-20 stsp init_curses();
6707 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6708 c02c541e 2019-03-29 stsp if (error)
6709 52185f70 2019-02-05 stsp goto done;
6711 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6712 51a10b52 2020-12-26 stsp if (error)
6713 51a10b52 2020-12-26 stsp goto done;
6715 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6716 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6717 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6718 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6719 4e97c21c 2020-12-06 stsp if (error)
6720 4e97c21c 2020-12-06 stsp goto done;
6721 4e97c21c 2020-12-06 stsp head_ref_name = label;
6723 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6724 4e97c21c 2020-12-06 stsp if (error == NULL)
6725 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6726 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6727 4e97c21c 2020-12-06 stsp goto done;
6728 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6729 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6730 4e97c21c 2020-12-06 stsp if (error)
6731 4e97c21c 2020-12-06 stsp goto done;
6734 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6735 a44927cc 2022-04-07 stsp if (error)
6736 a44927cc 2022-04-07 stsp goto done;
6738 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6739 5221c383 2018-08-01 stsp if (view == NULL) {
6740 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6741 5221c383 2018-08-01 stsp goto done;
6743 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6744 ad80ab7b 2018-08-04 stsp if (error)
6745 ad80ab7b 2018-08-04 stsp goto done;
6746 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6747 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6748 d91faf3b 2020-12-01 naddy in_repo_path);
6749 55cccc34 2020-02-20 stsp if (error)
6750 55cccc34 2020-02-20 stsp goto done;
6753 55cccc34 2020-02-20 stsp if (worktree) {
6754 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6755 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6756 55cccc34 2020-02-20 stsp worktree = NULL;
6758 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6760 52185f70 2019-02-05 stsp free(repo_path);
6761 e4a0e26d 2020-02-20 stsp free(cwd);
6762 ffd1d5e5 2018-06-23 stsp free(commit_id);
6763 4e97c21c 2020-12-06 stsp free(label);
6765 486cd271 2020-12-06 stsp got_ref_close(ref);
6766 1d0f4054 2021-06-17 stsp if (repo) {
6767 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6768 1d0f4054 2021-06-17 stsp if (error == NULL)
6769 1d0f4054 2021-06-17 stsp error = close_err;
6771 0ae84acc 2022-06-15 tracey if (pack_fds) {
6772 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6773 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6774 0ae84acc 2022-06-15 tracey if (error == NULL)
6775 0ae84acc 2022-06-15 tracey error = pack_err;
6777 51a10b52 2020-12-26 stsp tog_free_refs();
6778 ffd1d5e5 2018-06-23 stsp return error;
6781 6458efa5 2020-11-24 stsp static const struct got_error *
6782 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6784 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6785 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6787 6458efa5 2020-11-24 stsp s->nrefs = 0;
6788 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6789 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6790 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6791 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6792 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6795 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6796 6458efa5 2020-11-24 stsp if (re == NULL)
6797 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6799 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6800 8924d611 2020-12-26 stsp if (re->ref == NULL)
6801 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6802 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6803 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6806 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6807 6458efa5 2020-11-24 stsp return NULL;
6811 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6813 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6815 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6816 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6817 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6818 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6823 6458efa5 2020-11-24 stsp static const struct got_error *
6824 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6826 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6827 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6829 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6830 6458efa5 2020-11-24 stsp s->repo = repo;
6832 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6833 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6835 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6837 6458efa5 2020-11-24 stsp return err;
6839 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6840 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6841 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6842 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6844 6458efa5 2020-11-24 stsp goto done;
6846 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6847 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6848 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6850 6458efa5 2020-11-24 stsp goto done;
6852 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6853 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6854 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6856 cc488aa7 2022-01-23 stsp goto done;
6858 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6859 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6860 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6862 6458efa5 2020-11-24 stsp goto done;
6865 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6866 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6867 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6868 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6869 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6872 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6873 6458efa5 2020-11-24 stsp return err;
6876 6458efa5 2020-11-24 stsp static const struct got_error *
6877 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6879 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6881 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6882 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6884 6458efa5 2020-11-24 stsp return NULL;
6887 6458efa5 2020-11-24 stsp static const struct got_error *
6888 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6889 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6891 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6892 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6893 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6894 6458efa5 2020-11-24 stsp int obj_type;
6896 c42c9805 2020-11-24 stsp *commit_id = NULL;
6898 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6900 6458efa5 2020-11-24 stsp return err;
6902 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6904 6458efa5 2020-11-24 stsp goto done;
6906 6458efa5 2020-11-24 stsp switch (obj_type) {
6907 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6908 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6910 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6911 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6913 6458efa5 2020-11-24 stsp goto done;
6914 c42c9805 2020-11-24 stsp free(obj_id);
6915 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6916 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6918 6458efa5 2020-11-24 stsp goto done;
6919 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6920 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6921 c42c9805 2020-11-24 stsp goto done;
6923 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6924 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6925 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6926 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6927 c42c9805 2020-11-24 stsp goto done;
6931 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6937 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6938 c42c9805 2020-11-24 stsp if (err) {
6939 c42c9805 2020-11-24 stsp free(*commit_id);
6940 c42c9805 2020-11-24 stsp *commit_id = NULL;
6942 c42c9805 2020-11-24 stsp return err;
6945 c42c9805 2020-11-24 stsp static const struct got_error *
6946 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6947 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6949 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6950 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6951 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6953 c42c9805 2020-11-24 stsp *new_view = NULL;
6955 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6956 c42c9805 2020-11-24 stsp if (err) {
6957 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6958 c42c9805 2020-11-24 stsp return err;
6960 c42c9805 2020-11-24 stsp return NULL;
6963 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6964 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6965 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6966 6458efa5 2020-11-24 stsp goto done;
6969 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6970 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6973 6458efa5 2020-11-24 stsp view_close(log_view);
6975 6458efa5 2020-11-24 stsp *new_view = log_view;
6976 c42c9805 2020-11-24 stsp free(commit_id);
6977 6458efa5 2020-11-24 stsp return err;
6980 ce5b7c56 2019-07-09 stsp static void
6981 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6983 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6984 34ba6917 2020-11-30 stsp int i = 0;
6986 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6989 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6990 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6991 34ba6917 2020-11-30 stsp if (re == NULL)
6993 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6994 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6998 9b058f45 2022-06-30 mark static const struct got_error *
6999 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7001 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7002 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7003 6458efa5 2020-11-24 stsp int n = 0;
7005 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7006 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7008 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7010 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7011 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7013 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7014 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7015 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7016 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7020 9b058f45 2022-06-30 mark return NULL;
7023 6458efa5 2020-11-24 stsp static const struct got_error *
7024 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7026 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7028 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7029 6458efa5 2020-11-24 stsp return NULL;
7032 6458efa5 2020-11-24 stsp static int
7033 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7035 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7037 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7041 6458efa5 2020-11-24 stsp static const struct got_error *
7042 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7044 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7045 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7047 6458efa5 2020-11-24 stsp if (!view->searching) {
7048 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7049 6458efa5 2020-11-24 stsp return NULL;
7052 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7053 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7054 6458efa5 2020-11-24 stsp if (s->selected_entry)
7055 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7057 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7058 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7060 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7061 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7063 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7064 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7067 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7068 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7069 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7070 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7072 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7075 6458efa5 2020-11-24 stsp while (1) {
7076 6458efa5 2020-11-24 stsp if (re == NULL) {
7077 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7078 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7079 6458efa5 2020-11-24 stsp return NULL;
7081 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7082 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7084 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7087 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7088 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7089 6458efa5 2020-11-24 stsp s->matched_entry = re;
7093 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7094 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7096 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7099 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7100 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7101 6458efa5 2020-11-24 stsp s->selected = 0;
7104 6458efa5 2020-11-24 stsp return NULL;
7107 6458efa5 2020-11-24 stsp static const struct got_error *
7108 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7110 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7111 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7112 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7113 6458efa5 2020-11-24 stsp char *line = NULL;
7114 6458efa5 2020-11-24 stsp wchar_t *wline;
7115 6458efa5 2020-11-24 stsp struct tog_color *tc;
7116 6458efa5 2020-11-24 stsp int width, n;
7117 6458efa5 2020-11-24 stsp int limit = view->nlines;
7119 6458efa5 2020-11-24 stsp werase(view->window);
7121 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7122 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7123 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
7124 9b058f45 2022-06-30 mark --limit; /* border */
7126 6458efa5 2020-11-24 stsp if (limit == 0)
7127 6458efa5 2020-11-24 stsp return NULL;
7129 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7131 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7132 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7133 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7135 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7136 6458efa5 2020-11-24 stsp if (err) {
7137 6458efa5 2020-11-24 stsp free(line);
7138 6458efa5 2020-11-24 stsp return err;
7140 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7141 6458efa5 2020-11-24 stsp wstandout(view->window);
7142 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7143 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7144 6458efa5 2020-11-24 stsp wstandend(view->window);
7145 6458efa5 2020-11-24 stsp free(wline);
7146 6458efa5 2020-11-24 stsp wline = NULL;
7147 6458efa5 2020-11-24 stsp free(line);
7148 6458efa5 2020-11-24 stsp line = NULL;
7149 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7150 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7151 6458efa5 2020-11-24 stsp if (--limit <= 0)
7152 6458efa5 2020-11-24 stsp return NULL;
7155 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7156 6458efa5 2020-11-24 stsp char *line = NULL;
7157 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7159 b4996bee 2022-06-16 stsp if (s->show_date) {
7160 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7161 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7162 b4996bee 2022-06-16 stsp struct got_object_id *id;
7163 b4996bee 2022-06-16 stsp struct tm tm;
7166 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7168 b4996bee 2022-06-16 stsp return err;
7169 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7170 b4996bee 2022-06-16 stsp if (err) {
7171 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7173 b4996bee 2022-06-16 stsp return err;
7175 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7177 b4996bee 2022-06-16 stsp if (err) {
7179 b4996bee 2022-06-16 stsp return err;
7181 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7182 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7184 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7185 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7188 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7189 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7190 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7191 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7193 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7194 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7195 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7196 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7197 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7198 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7199 6458efa5 2020-11-24 stsp struct got_object_id *id;
7200 6458efa5 2020-11-24 stsp char *id_str;
7201 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7203 6458efa5 2020-11-24 stsp return err;
7204 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7205 6458efa5 2020-11-24 stsp if (err) {
7207 6458efa5 2020-11-24 stsp return err;
7209 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7210 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7211 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7213 6458efa5 2020-11-24 stsp free(id_str);
7214 6458efa5 2020-11-24 stsp return err;
7217 6458efa5 2020-11-24 stsp free(id_str);
7218 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7219 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7220 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7222 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7224 6458efa5 2020-11-24 stsp if (err) {
7225 6458efa5 2020-11-24 stsp free(line);
7226 6458efa5 2020-11-24 stsp return err;
7228 6458efa5 2020-11-24 stsp if (n == s->selected) {
7229 6458efa5 2020-11-24 stsp if (view->focussed)
7230 6458efa5 2020-11-24 stsp wstandout(view->window);
7231 6458efa5 2020-11-24 stsp s->selected_entry = re;
7233 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7235 6458efa5 2020-11-24 stsp wattr_on(view->window,
7236 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7237 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7239 6458efa5 2020-11-24 stsp wattr_off(view->window,
7240 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7241 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7242 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7243 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7244 6458efa5 2020-11-24 stsp wstandend(view->window);
7245 6458efa5 2020-11-24 stsp free(line);
7246 6458efa5 2020-11-24 stsp free(wline);
7247 6458efa5 2020-11-24 stsp wline = NULL;
7249 6458efa5 2020-11-24 stsp s->ndisplayed++;
7250 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7253 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7256 9b058f45 2022-06-30 mark view_border(view);
7257 6458efa5 2020-11-24 stsp return err;
7260 6458efa5 2020-11-24 stsp static const struct got_error *
7261 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
7262 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7264 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7265 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7266 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7268 c42c9805 2020-11-24 stsp *new_view = NULL;
7270 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7271 c42c9805 2020-11-24 stsp if (err) {
7272 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7273 c42c9805 2020-11-24 stsp return err;
7275 c42c9805 2020-11-24 stsp return NULL;
7279 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7280 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7281 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7282 c42c9805 2020-11-24 stsp goto done;
7285 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7286 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7288 c42c9805 2020-11-24 stsp goto done;
7290 c42c9805 2020-11-24 stsp *new_view = tree_view;
7292 c42c9805 2020-11-24 stsp free(commit_id);
7293 c42c9805 2020-11-24 stsp return err;
7295 c42c9805 2020-11-24 stsp static const struct got_error *
7296 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7298 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7299 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7300 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7301 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7302 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7304 6458efa5 2020-11-24 stsp switch (ch) {
7306 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7307 640cd7ff 2022-06-22 mark view->count = 0;
7310 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7311 640cd7ff 2022-06-22 mark view->count = 0;
7314 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7315 640cd7ff 2022-06-22 mark view->count = 0;
7316 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7317 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7318 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7321 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7322 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7323 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7326 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7327 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7329 6458efa5 2020-11-24 stsp case KEY_ENTER:
7330 6458efa5 2020-11-24 stsp case '\r':
7331 640cd7ff 2022-06-22 mark view->count = 0;
7332 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7334 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7335 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7337 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7338 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7342 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7343 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7348 e78dc838 2020-12-04 stsp view->focussed = 0;
7349 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7350 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7351 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7352 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7353 6458efa5 2020-11-24 stsp err = view_close_child(view);
7355 6458efa5 2020-11-24 stsp return err;
7356 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7359 e78dc838 2020-12-04 stsp view->focus_child = 1;
7361 6458efa5 2020-11-24 stsp *new_view = log_view;
7364 640cd7ff 2022-06-22 mark view->count = 0;
7365 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7367 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7368 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
7369 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7371 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7373 e78dc838 2020-12-04 stsp view->focussed = 0;
7374 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7375 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7376 c42c9805 2020-11-24 stsp err = view_close_child(view);
7378 c42c9805 2020-11-24 stsp return err;
7379 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7382 e78dc838 2020-12-04 stsp view->focus_child = 1;
7384 c42c9805 2020-11-24 stsp *new_view = tree_view;
7386 e4526bf5 2021-09-03 naddy case 'g':
7387 e4526bf5 2021-09-03 naddy case KEY_HOME:
7388 e4526bf5 2021-09-03 naddy s->selected = 0;
7389 640cd7ff 2022-06-22 mark view->count = 0;
7390 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7392 e4526bf5 2021-09-03 naddy case 'G':
7393 9b058f45 2022-06-30 mark case KEY_END: {
7394 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7396 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7397 9b058f45 2022-06-30 mark --eos; /* border */
7398 e4526bf5 2021-09-03 naddy s->selected = 0;
7399 640cd7ff 2022-06-22 mark view->count = 0;
7400 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7401 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7402 e4526bf5 2021-09-03 naddy if (re == NULL)
7404 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7405 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7407 e4526bf5 2021-09-03 naddy if (n > 0)
7408 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7412 6458efa5 2020-11-24 stsp case KEY_UP:
7413 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7414 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7415 6458efa5 2020-11-24 stsp s->selected--;
7418 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7419 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7420 640cd7ff 2022-06-22 mark view->count = 0;
7422 83cc4199 2022-06-13 stsp case CTRL('u'):
7424 83cc4199 2022-06-13 stsp nscroll /= 2;
7425 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7426 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7427 6458efa5 2020-11-24 stsp case CTRL('b'):
7429 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7430 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7431 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7432 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7433 640cd7ff 2022-06-22 mark view->count = 0;
7436 6458efa5 2020-11-24 stsp case KEY_DOWN:
7437 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7438 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7439 6458efa5 2020-11-24 stsp s->selected++;
7442 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7443 6458efa5 2020-11-24 stsp /* can't scroll any further */
7444 640cd7ff 2022-06-22 mark view->count = 0;
7447 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7449 83cc4199 2022-06-13 stsp case CTRL('d'):
7451 83cc4199 2022-06-13 stsp nscroll /= 2;
7452 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7453 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7454 6458efa5 2020-11-24 stsp case CTRL('f'):
7456 48bb96f0 2022-06-20 naddy case ' ':
7457 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7458 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7459 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7460 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7461 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7462 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7463 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7464 640cd7ff 2022-06-22 mark view->count = 0;
7467 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7469 6458efa5 2020-11-24 stsp case CTRL('l'):
7470 640cd7ff 2022-06-22 mark view->count = 0;
7471 8924d611 2020-12-26 stsp tog_free_refs();
7472 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7475 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7476 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7478 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7479 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7480 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7483 640cd7ff 2022-06-22 mark view->count = 0;
7487 6458efa5 2020-11-24 stsp return err;
7490 6458efa5 2020-11-24 stsp __dead static void
7491 6458efa5 2020-11-24 stsp usage_ref(void)
7494 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7495 6458efa5 2020-11-24 stsp getprogname());
7499 6458efa5 2020-11-24 stsp static const struct got_error *
7500 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7502 6458efa5 2020-11-24 stsp const struct got_error *error;
7503 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7504 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7505 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7507 6458efa5 2020-11-24 stsp struct tog_view *view;
7508 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7510 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7511 6458efa5 2020-11-24 stsp switch (ch) {
7513 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7514 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7515 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7519 e99e2d15 2020-11-24 naddy usage_ref();
7520 6458efa5 2020-11-24 stsp /* NOTREACHED */
7524 6458efa5 2020-11-24 stsp argc -= optind;
7525 6458efa5 2020-11-24 stsp argv += optind;
7527 6458efa5 2020-11-24 stsp if (argc > 1)
7528 e99e2d15 2020-11-24 naddy usage_ref();
7530 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7531 0ae84acc 2022-06-15 tracey if (error != NULL)
7532 0ae84acc 2022-06-15 tracey goto done;
7534 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7535 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7536 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7537 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7538 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7539 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7540 c156c7a4 2020-12-18 stsp goto done;
7541 6458efa5 2020-11-24 stsp if (worktree)
7542 6458efa5 2020-11-24 stsp repo_path =
7543 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7545 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7546 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7547 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7548 c156c7a4 2020-12-18 stsp goto done;
7552 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7553 6458efa5 2020-11-24 stsp if (error != NULL)
7554 6458efa5 2020-11-24 stsp goto done;
7556 6458efa5 2020-11-24 stsp init_curses();
7558 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7559 51a10b52 2020-12-26 stsp if (error)
7560 51a10b52 2020-12-26 stsp goto done;
7562 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7563 6458efa5 2020-11-24 stsp if (error)
7564 6458efa5 2020-11-24 stsp goto done;
7566 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7567 6458efa5 2020-11-24 stsp if (view == NULL) {
7568 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7569 6458efa5 2020-11-24 stsp goto done;
7572 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7573 6458efa5 2020-11-24 stsp if (error)
7574 6458efa5 2020-11-24 stsp goto done;
7576 6458efa5 2020-11-24 stsp if (worktree) {
7577 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7578 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7579 6458efa5 2020-11-24 stsp worktree = NULL;
7581 6458efa5 2020-11-24 stsp error = view_loop(view);
7583 6458efa5 2020-11-24 stsp free(repo_path);
7584 6458efa5 2020-11-24 stsp free(cwd);
7585 1d0f4054 2021-06-17 stsp if (repo) {
7586 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7587 1d0f4054 2021-06-17 stsp if (close_err)
7588 1d0f4054 2021-06-17 stsp error = close_err;
7590 0ae84acc 2022-06-15 tracey if (pack_fds) {
7591 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7592 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7593 0ae84acc 2022-06-15 tracey if (error == NULL)
7594 0ae84acc 2022-06-15 tracey error = pack_err;
7596 51a10b52 2020-12-26 stsp tog_free_refs();
7597 6458efa5 2020-11-24 stsp return error;
7601 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7602 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7604 6458efa5 2020-11-24 stsp static void
7605 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7607 9b058f45 2022-06-30 mark switch (view->type) {
7608 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7609 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7610 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7611 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7615 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7616 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7618 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7619 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7622 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7623 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7624 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7626 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7627 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7628 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7630 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7631 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7632 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7638 9b058f45 2022-06-30 mark view->offset = 0;
7642 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7643 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7645 9b058f45 2022-06-30 mark static const struct got_error *
7646 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7648 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7649 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7650 9b058f45 2022-06-30 mark int *selected = NULL;
7651 9b058f45 2022-06-30 mark int header, offset;
7653 9b058f45 2022-06-30 mark switch (view->type) {
7654 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7655 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7656 9b058f45 2022-06-30 mark header = 3;
7657 9b058f45 2022-06-30 mark scrolld = NULL;
7658 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7659 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7660 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7661 9b058f45 2022-06-30 mark s->selected_line -= offset;
7662 9b058f45 2022-06-30 mark view->offset = offset;
7666 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7667 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7668 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7669 9b058f45 2022-06-30 mark header = 3;
7670 9b058f45 2022-06-30 mark selected = &s->selected;
7673 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7674 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7675 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7676 9b058f45 2022-06-30 mark header = 3;
7677 9b058f45 2022-06-30 mark selected = &s->selected;
7680 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7681 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7682 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7683 9b058f45 2022-06-30 mark header = 5;
7684 9b058f45 2022-06-30 mark selected = &s->selected;
7688 9b058f45 2022-06-30 mark selected = NULL;
7689 9b058f45 2022-06-30 mark scrolld = NULL;
7690 9b058f45 2022-06-30 mark header = 0;
7694 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7695 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7696 9b058f45 2022-06-30 mark view->offset = offset;
7697 9b058f45 2022-06-30 mark if (scrolld && offset) {
7698 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7699 9b058f45 2022-06-30 mark *selected -= offset;
7703 9b058f45 2022-06-30 mark return err;
7706 9b058f45 2022-06-30 mark static void
7707 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7711 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7712 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7713 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7714 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7716 6879ba42 2020-10-01 naddy fputc('\n', fp);
7719 4ed7e80c 2018-05-20 stsp __dead static void
7720 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7722 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7724 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7725 6879ba42 2020-10-01 naddy getprogname());
7726 6879ba42 2020-10-01 naddy if (hflag) {
7727 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7728 6879ba42 2020-10-01 naddy list_commands(fp);
7730 6879ba42 2020-10-01 naddy exit(status);
7733 c2301be8 2018-04-30 stsp static char **
7734 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7736 ee85c5e8 2020-02-29 stsp va_list ap;
7737 c2301be8 2018-04-30 stsp char **argv;
7740 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7742 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7743 c2301be8 2018-04-30 stsp if (argv == NULL)
7744 c2301be8 2018-04-30 stsp err(1, "calloc");
7745 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7746 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7747 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7748 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7751 ee85c5e8 2020-02-29 stsp va_end(ap);
7752 c2301be8 2018-04-30 stsp return argv;
7756 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7757 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7758 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7759 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7760 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7762 ee85c5e8 2020-02-29 stsp static const struct got_error *
7763 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7765 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7766 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7767 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7768 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7769 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7770 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7771 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7772 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7773 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7775 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7776 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7777 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7779 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7780 0ae84acc 2022-06-15 tracey if (error != NULL)
7781 0ae84acc 2022-06-15 tracey goto done;
7783 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7784 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7785 ee85c5e8 2020-02-29 stsp goto done;
7787 ee85c5e8 2020-02-29 stsp if (worktree)
7788 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7790 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7791 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7792 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7793 ee85c5e8 2020-02-29 stsp goto done;
7796 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7797 ee85c5e8 2020-02-29 stsp if (error != NULL)
7798 ee85c5e8 2020-02-29 stsp goto done;
7800 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7801 ee85c5e8 2020-02-29 stsp repo, worktree);
7802 ee85c5e8 2020-02-29 stsp if (error)
7803 ee85c5e8 2020-02-29 stsp goto done;
7805 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7806 84de9106 2020-12-26 stsp if (error)
7807 84de9106 2020-12-26 stsp goto done;
7808 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7809 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7810 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7811 ee85c5e8 2020-02-29 stsp if (error)
7812 ee85c5e8 2020-02-29 stsp goto done;
7814 ee85c5e8 2020-02-29 stsp if (worktree) {
7815 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7816 ee85c5e8 2020-02-29 stsp worktree = NULL;
7819 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7820 a44927cc 2022-04-07 stsp if (error)
7821 a44927cc 2022-04-07 stsp goto done;
7823 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7824 ee85c5e8 2020-02-29 stsp if (error) {
7825 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7826 ee85c5e8 2020-02-29 stsp goto done;
7827 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7828 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7829 6879ba42 2020-10-01 naddy usage(1, 1);
7830 ee85c5e8 2020-02-29 stsp /* not reached */
7833 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7834 1d0f4054 2021-06-17 stsp if (error == NULL)
7835 1d0f4054 2021-06-17 stsp error = close_err;
7836 ee85c5e8 2020-02-29 stsp repo = NULL;
7838 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7839 ee85c5e8 2020-02-29 stsp if (error)
7840 ee85c5e8 2020-02-29 stsp goto done;
7842 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7844 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7845 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7847 1d0f4054 2021-06-17 stsp if (repo) {
7848 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7849 1d0f4054 2021-06-17 stsp if (error == NULL)
7850 1d0f4054 2021-06-17 stsp error = close_err;
7852 a44927cc 2022-04-07 stsp if (commit)
7853 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7854 ee85c5e8 2020-02-29 stsp if (worktree)
7855 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7856 0ae84acc 2022-06-15 tracey if (pack_fds) {
7857 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7858 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7859 0ae84acc 2022-06-15 tracey if (error == NULL)
7860 0ae84acc 2022-06-15 tracey error = pack_err;
7863 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7864 ee85c5e8 2020-02-29 stsp free(commit_id);
7865 ee85c5e8 2020-02-29 stsp free(cwd);
7866 ee85c5e8 2020-02-29 stsp free(repo_path);
7867 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7868 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7870 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7871 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7872 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7874 87670572 2020-12-26 naddy tog_free_refs();
7875 ee85c5e8 2020-02-29 stsp return error;
7879 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7881 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7882 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7883 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7884 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7885 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7886 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7887 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7889 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
7891 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7893 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7894 9f7d7167 2018-04-29 stsp switch (ch) {
7896 9f7d7167 2018-04-29 stsp hflag = 1;
7899 53ccebc2 2019-07-30 stsp Vflag = 1;
7902 6879ba42 2020-10-01 naddy usage(hflag, 1);
7903 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7907 9f7d7167 2018-04-29 stsp argc -= optind;
7908 9f7d7167 2018-04-29 stsp argv += optind;
7909 9814e6a3 2020-09-27 naddy optind = 1;
7910 c2301be8 2018-04-30 stsp optreset = 1;
7912 53ccebc2 2019-07-30 stsp if (Vflag) {
7913 53ccebc2 2019-07-30 stsp got_version_print_str();
7914 6879ba42 2020-10-01 naddy return 0;
7917 4010e238 2020-12-04 stsp #ifndef PROFILE
7918 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7919 4010e238 2020-12-04 stsp NULL) == -1)
7920 4010e238 2020-12-04 stsp err(1, "pledge");
7923 c2301be8 2018-04-30 stsp if (argc == 0) {
7924 f29d3e89 2018-06-23 stsp if (hflag)
7925 6879ba42 2020-10-01 naddy usage(hflag, 0);
7926 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7927 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7929 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7933 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7934 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7935 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7936 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7937 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7943 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7944 917d79a7 2022-07-01 stsp if (diff_algo_str) {
7945 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
7946 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7947 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
7948 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7951 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7952 ee85c5e8 2020-02-29 stsp if (argc != 1)
7953 6879ba42 2020-10-01 naddy usage(0, 1);
7954 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7955 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7957 ee85c5e8 2020-02-29 stsp if (hflag)
7958 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7960 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7964 b46c1e04 2020-09-20 naddy putchar('\n');
7965 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7967 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7968 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7969 a2f4a359 2020-02-28 stsp free(cmd_argv);
7972 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7973 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);