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;
794 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
796 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
797 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
800 9b058f45 2022-06-30 mark static void
801 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
803 9b058f45 2022-06-30 mark PANEL *panel;
804 9b058f45 2022-06-30 mark const struct tog_view *view_above;
806 9b058f45 2022-06-30 mark if (view->parent)
807 9b058f45 2022-06-30 mark return view_border(view->parent);
809 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
810 9b058f45 2022-06-30 mark if (panel == NULL)
813 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
814 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
815 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
816 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
817 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
819 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
820 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
823 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
824 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
825 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
827 4d8c2215 2018-08-19 stsp static const struct got_error *
828 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
830 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
831 9b058f45 2022-06-30 mark int dif, nlines, ncols;
833 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
835 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
836 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
838 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
839 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
840 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
842 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
844 4dd27a72 2022-06-29 stsp if (view->child) {
845 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
847 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
848 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
849 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
850 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
851 0dbbbe90 2022-06-17 op ncols = COLS;
853 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
854 5c60c32a 2018-10-18 stsp if (view->child->focussed)
855 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
857 5c60c32a 2018-10-18 stsp show_panel(view->panel);
859 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
861 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
862 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
865 9b058f45 2022-06-30 mark * Request commits if terminal height was increased in a log
866 9b058f45 2022-06-30 mark * view so we have enough commits loaded to populate the view.
868 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG && dif > 0) {
869 9b058f45 2022-06-30 mark struct tog_log_view_state *ts = &view->state.log;
871 9b058f45 2022-06-30 mark if (ts->commits.ncommits < ts->selected_entry->idx +
872 9b058f45 2022-06-30 mark view->lines - ts->selected) {
873 9b058f45 2022-06-30 mark view->nscrolled = ts->selected_entry->idx +
874 9b058f45 2022-06-30 mark view->lines - ts->selected -
875 9b058f45 2022-06-30 mark ts->commits.ncommits + dif;
876 9b058f45 2022-06-30 mark err = request_log_commits(view);
878 9b058f45 2022-06-30 mark return err;
883 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
884 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
885 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
888 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
890 9b058f45 2022-06-30 mark return err;
891 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
892 9b058f45 2022-06-30 mark err = offset_selection_down(view);
894 9b058f45 2022-06-30 mark return err;
896 9b058f45 2022-06-30 mark view_border(view);
897 9b058f45 2022-06-30 mark update_panels();
898 9b058f45 2022-06-30 mark doupdate();
899 9b058f45 2022-06-30 mark show_panel(view->child->panel);
900 9b058f45 2022-06-30 mark nlines = view->nlines;
902 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
903 0dbbbe90 2022-06-17 op ncols = COLS;
905 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
906 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
907 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
908 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
909 0dbbbe90 2022-06-17 op wclear(view->window);
911 0dbbbe90 2022-06-17 op view->nlines = nlines;
912 0dbbbe90 2022-06-17 op view->ncols = ncols;
913 0dbbbe90 2022-06-17 op view->lines = LINES;
914 0dbbbe90 2022-06-17 op view->cols = COLS;
916 5c60c32a 2018-10-18 stsp return NULL;
919 669b5ffa 2018-10-07 stsp static const struct got_error *
920 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
922 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
924 669b5ffa 2018-10-07 stsp if (view->child == NULL)
925 669b5ffa 2018-10-07 stsp return NULL;
927 669b5ffa 2018-10-07 stsp err = view_close(view->child);
928 669b5ffa 2018-10-07 stsp view->child = NULL;
929 669b5ffa 2018-10-07 stsp return err;
932 0dbbbe90 2022-06-17 op static const struct got_error *
933 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
935 669b5ffa 2018-10-07 stsp view->child = child;
936 669b5ffa 2018-10-07 stsp child->parent = view;
938 0dbbbe90 2022-06-17 op return view_resize(view);
941 34bc9ec9 2019-02-22 stsp static void
942 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
944 25791caa 2018-10-24 stsp int cols, lines;
945 25791caa 2018-10-24 stsp struct winsize size;
947 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
948 25791caa 2018-10-24 stsp cols = 80; /* Default */
949 25791caa 2018-10-24 stsp lines = 24;
951 25791caa 2018-10-24 stsp cols = size.ws_col;
952 25791caa 2018-10-24 stsp lines = size.ws_row;
954 25791caa 2018-10-24 stsp resize_term(lines, cols);
957 2b49a8ae 2019-06-22 stsp static const struct got_error *
958 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
960 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
961 9b058f45 2022-06-30 mark struct tog_view *v = view;
962 2b49a8ae 2019-06-22 stsp char pattern[1024];
965 c0c4acc8 2021-01-24 stsp if (view->search_started) {
966 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
967 c0c4acc8 2021-01-24 stsp view->searching = 0;
968 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
970 c0c4acc8 2021-01-24 stsp view->search_started = 0;
972 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
973 2b49a8ae 2019-06-22 stsp return NULL;
975 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
976 9b058f45 2022-06-30 mark v = view->child;
978 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
979 9b058f45 2022-06-30 mark wclrtoeol(v->window);
981 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
982 2b49a8ae 2019-06-22 stsp nocbreak();
984 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
985 9b058f45 2022-06-30 mark wrefresh(v->window);
988 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
989 2b49a8ae 2019-06-22 stsp if (ret == ERR)
990 2b49a8ae 2019-06-22 stsp return NULL;
992 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
993 7c32bd05 2019-06-22 stsp err = view->search_start(view);
995 7c32bd05 2019-06-22 stsp regfree(&view->regex);
996 7c32bd05 2019-06-22 stsp return err;
998 c0c4acc8 2021-01-24 stsp view->search_started = 1;
999 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1000 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1001 2b49a8ae 2019-06-22 stsp view->search_next(view);
1004 2b49a8ae 2019-06-22 stsp return NULL;
1008 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1009 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1011 640cd7ff 2022-06-22 mark static int
1012 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1014 9b058f45 2022-06-30 mark struct tog_view *v = view;
1015 9b058f45 2022-06-30 mark int x, n = 0;
1017 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1018 9b058f45 2022-06-30 mark v = view->child;
1019 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1020 9b058f45 2022-06-30 mark v = view->parent;
1022 640cd7ff 2022-06-22 mark view->count = 0;
1023 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1024 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1025 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1026 9b058f45 2022-06-30 mark waddch(v->window, ':');
1029 9b058f45 2022-06-30 mark x = getcurx(v->window);
1030 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1031 9b058f45 2022-06-30 mark waddch(v->window, c);
1032 9b058f45 2022-06-30 mark wrefresh(v->window);
1036 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1037 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1039 640cd7ff 2022-06-22 mark if (n >= 9999999)
1040 640cd7ff 2022-06-22 mark n = 9999999;
1042 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1043 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1045 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1046 640cd7ff 2022-06-22 mark view->count = n;
1051 0cf4efb1 2018-09-29 stsp static const struct got_error *
1052 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1053 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1055 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1056 669b5ffa 2018-10-07 stsp struct tog_view *v;
1057 1a76625f 2018-10-22 stsp int ch, errcode;
1059 e5a0f69f 2018-08-18 stsp *new = NULL;
1061 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1062 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1063 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1064 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1065 640cd7ff 2022-06-22 mark view->count = 0;
1068 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1069 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1070 82954512 2020-02-03 stsp if (errcode)
1071 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1072 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1073 3da8ef85 2021-09-21 stsp sched_yield();
1074 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1075 82954512 2020-02-03 stsp if (errcode)
1076 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1077 82954512 2020-02-03 stsp "pthread_mutex_lock");
1078 60493ae3 2019-06-20 stsp view->search_next(view);
1079 60493ae3 2019-06-20 stsp return NULL;
1082 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE);
1083 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1084 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1085 1a76625f 2018-10-22 stsp if (errcode)
1086 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1087 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1088 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1090 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1091 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1092 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1093 a6d37fac 2022-07-03 mark view->count = 0;
1095 a6d37fac 2022-07-03 mark ch = view->ch;
1097 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1098 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1099 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1101 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1102 1a76625f 2018-10-22 stsp if (errcode)
1103 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1104 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1106 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1107 25791caa 2018-10-24 stsp tog_resizeterm();
1108 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1109 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1110 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1111 25791caa 2018-10-24 stsp err = view_resize(v);
1113 25791caa 2018-10-24 stsp return err;
1114 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1116 25791caa 2018-10-24 stsp return err;
1117 cdfcfb03 2020-12-06 stsp if (v->child) {
1118 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1120 cdfcfb03 2020-12-06 stsp return err;
1121 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1122 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1124 cdfcfb03 2020-12-06 stsp return err;
1129 e5a0f69f 2018-08-18 stsp switch (ch) {
1131 640cd7ff 2022-06-22 mark view->count = 0;
1132 1e37a5c2 2019-05-12 jcs if (view->child) {
1133 e78dc838 2020-12-04 stsp view->focussed = 0;
1134 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1135 e78dc838 2020-12-04 stsp view->focus_child = 1;
1136 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1137 e78dc838 2020-12-04 stsp view->focussed = 0;
1138 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1139 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1140 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1141 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1142 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1143 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1145 9b058f45 2022-06-30 mark return err;
1147 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1148 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1150 9b058f45 2022-06-30 mark return err;
1155 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1156 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1157 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1158 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1162 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1163 9b058f45 2022-06-30 mark view->parent->mode = TOG_VIEW_SPLIT_NONE;
1165 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1166 9970f7fc 2020-12-03 stsp view->dying = 1;
1172 640cd7ff 2022-06-22 mark view->count = 0;
1173 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1174 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1176 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1177 e78dc838 2020-12-04 stsp view->focussed = 0;
1178 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1179 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1181 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1184 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1185 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1187 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1188 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1189 e78dc838 2020-12-04 stsp view->focussed = 1;
1190 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1192 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1193 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1194 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1198 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1202 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1203 9b058f45 2022-06-30 mark err = request_log_commits(view);
1207 9b058f45 2022-06-30 mark if (view->parent)
1208 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1210 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1212 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1215 640cd7ff 2022-06-22 mark view->count = 0;
1216 60493ae3 2019-06-20 stsp if (view->search_start)
1217 2b49a8ae 2019-06-22 stsp view_search_start(view);
1219 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1223 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1224 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1225 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1226 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1227 60493ae3 2019-06-20 stsp view->search_next(view);
1229 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1232 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1233 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1235 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1236 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1237 917d79a7 2022-07-01 stsp if (v->reset) {
1238 917d79a7 2022-07-01 stsp err = v->reset(v);
1240 917d79a7 2022-07-01 stsp return err;
1242 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1243 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1245 917d79a7 2022-07-01 stsp return err;
1250 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1254 e5a0f69f 2018-08-18 stsp return err;
1258 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1260 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1261 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1263 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1265 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1268 669b5ffa 2018-10-07 stsp return view->focussed;
1271 bcbd79e2 2018-08-19 stsp static const struct got_error *
1272 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1274 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1275 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1276 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1277 fd823528 2018-10-22 stsp int fast_refresh = 10;
1278 1a76625f 2018-10-22 stsp int done = 0, errcode;
1280 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1281 1a76625f 2018-10-22 stsp if (errcode)
1282 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1284 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1285 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1287 1004088d 2018-09-29 stsp view->focussed = 1;
1288 878940b7 2018-09-29 stsp err = view->show(view);
1290 0cf4efb1 2018-09-29 stsp return err;
1291 0cf4efb1 2018-09-29 stsp update_panels();
1292 0cf4efb1 2018-09-29 stsp doupdate();
1293 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1294 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1295 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1296 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1298 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1301 9970f7fc 2020-12-03 stsp if (view->dying) {
1302 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1304 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1305 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1307 e78dc838 2020-12-04 stsp else if (view->parent)
1308 669b5ffa 2018-10-07 stsp prev = view->parent;
1310 e78dc838 2020-12-04 stsp if (view->parent) {
1311 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1312 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1313 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1314 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1315 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1319 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1321 9970f7fc 2020-12-03 stsp err = view_close(view);
1323 e5a0f69f 2018-08-18 stsp goto done;
1325 e78dc838 2020-12-04 stsp view = NULL;
1326 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1327 e78dc838 2020-12-04 stsp if (v->focussed)
1330 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1331 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1333 e78dc838 2020-12-04 stsp view = prev;
1334 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1335 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1336 e78dc838 2020-12-04 stsp tog_view_list_head);
1338 e78dc838 2020-12-04 stsp if (view) {
1339 e78dc838 2020-12-04 stsp if (view->focus_child) {
1340 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1341 e78dc838 2020-12-04 stsp view = view->child;
1343 e78dc838 2020-12-04 stsp view->focussed = 1;
1347 bcbd79e2 2018-08-19 stsp if (new_view) {
1348 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1349 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1350 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1351 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1353 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1354 86c66b02 2018-10-18 stsp err = view_close(v);
1356 86c66b02 2018-10-18 stsp goto done;
1359 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1360 fed7eaa8 2018-10-24 stsp view = new_view;
1362 669b5ffa 2018-10-07 stsp if (view) {
1363 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1364 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1365 e78dc838 2020-12-04 stsp view = view->child;
1367 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1368 e78dc838 2020-12-04 stsp view = view->parent;
1370 e78dc838 2020-12-04 stsp show_panel(view->panel);
1371 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1372 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1373 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1374 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1376 1a76625f 2018-10-22 stsp goto done;
1378 669b5ffa 2018-10-07 stsp err = view->show(view);
1380 1a76625f 2018-10-22 stsp goto done;
1381 669b5ffa 2018-10-07 stsp if (view->child) {
1382 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1384 1a76625f 2018-10-22 stsp goto done;
1386 1a76625f 2018-10-22 stsp update_panels();
1387 1a76625f 2018-10-22 stsp doupdate();
1391 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1392 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1393 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1394 e5a0f69f 2018-08-18 stsp view_close(view);
1397 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1398 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1399 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1401 e5a0f69f 2018-08-18 stsp return err;
1404 4ed7e80c 2018-05-20 stsp __dead static void
1405 9f7d7167 2018-04-29 stsp usage_log(void)
1408 c70c5802 2018-08-01 stsp fprintf(stderr,
1409 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1410 9f7d7167 2018-04-29 stsp getprogname());
1414 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1415 80ddbec8 2018-04-29 stsp static const struct got_error *
1416 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1418 00dfcb92 2018-06-11 stsp char *vis = NULL;
1419 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1421 963b370f 2018-05-20 stsp *ws = NULL;
1422 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1423 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1424 00dfcb92 2018-06-11 stsp int vislen;
1425 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1426 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1428 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1429 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1431 00dfcb92 2018-06-11 stsp return err;
1432 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1433 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1434 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1435 a7f50699 2018-06-11 stsp goto done;
1439 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1440 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1441 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1442 a7f50699 2018-06-11 stsp goto done;
1445 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1446 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1448 00dfcb92 2018-06-11 stsp free(vis);
1449 963b370f 2018-05-20 stsp if (err) {
1450 963b370f 2018-05-20 stsp free(*ws);
1451 963b370f 2018-05-20 stsp *ws = NULL;
1452 963b370f 2018-05-20 stsp *wlen = 0;
1454 963b370f 2018-05-20 stsp return err;
1457 145b6838 2022-06-16 stsp static const struct got_error *
1458 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1460 145b6838 2022-06-16 stsp char *dst;
1461 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1463 145b6838 2022-06-16 stsp *ptr = NULL;
1464 145b6838 2022-06-16 stsp n = len = strlen(src);
1465 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1466 145b6838 2022-06-16 stsp if (dst == NULL)
1467 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1469 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1470 145b6838 2022-06-16 stsp const char c = src[idx];
1472 145b6838 2022-06-16 stsp if (c == '\t') {
1473 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1476 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1477 6e1c41ad 2022-06-16 mark if (p == NULL) {
1478 6e1c41ad 2022-06-16 mark free(dst);
1479 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1484 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1487 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1491 145b6838 2022-06-16 stsp dst[sz] = '\0';
1492 145b6838 2022-06-16 stsp *ptr = dst;
1493 145b6838 2022-06-16 stsp return NULL;
1497 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1498 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1499 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1503 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1505 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1507 4e4a9ac8 2022-06-17 op if (n == 0) {
1508 4e4a9ac8 2022-06-17 op *rcol = cols;
1512 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1513 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1514 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1516 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1518 4e4a9ac8 2022-06-17 op if (width == -1) {
1520 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1523 4e4a9ac8 2022-06-17 op if (cols + width > n)
1525 4e4a9ac8 2022-06-17 op cols += width;
1528 4e4a9ac8 2022-06-17 op *rcol = cols;
1533 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1534 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1535 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1537 ccda2f4d 2022-06-16 stsp static const struct got_error *
1538 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1539 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1541 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1543 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1544 145b6838 2022-06-16 stsp char *exstr = NULL;
1545 963b370f 2018-05-20 stsp size_t wlen;
1546 4e4a9ac8 2022-06-17 op int i, scrollx;
1548 963b370f 2018-05-20 stsp *wlinep = NULL;
1549 b700b5d6 2018-07-10 stsp *widthp = 0;
1551 145b6838 2022-06-16 stsp if (expand) {
1552 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1554 145b6838 2022-06-16 stsp return err;
1557 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1558 145b6838 2022-06-16 stsp free(exstr);
1560 963b370f 2018-05-20 stsp return err;
1562 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1564 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1565 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1568 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1569 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1573 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1574 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1576 b700b5d6 2018-07-10 stsp if (widthp)
1577 b700b5d6 2018-07-10 stsp *widthp = cols;
1578 ccda2f4d 2022-06-16 stsp if (scrollxp)
1579 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1581 963b370f 2018-05-20 stsp free(wline);
1583 963b370f 2018-05-20 stsp *wlinep = wline;
1584 963b370f 2018-05-20 stsp return err;
1587 8b473291 2019-02-21 stsp static const struct got_error*
1588 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1589 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1591 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1592 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1594 8b473291 2019-02-21 stsp const char *name;
1596 8b473291 2019-02-21 stsp *refs_str = NULL;
1598 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1599 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1600 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1603 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1604 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1606 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1607 8b473291 2019-02-21 stsp name += 5;
1608 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1609 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1611 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1612 8b473291 2019-02-21 stsp name += 6;
1613 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1614 8b473291 2019-02-21 stsp name += 8;
1615 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1616 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1619 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1622 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1623 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1624 5d844a1e 2019-08-13 stsp if (err) {
1625 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1626 48cae60d 2020-09-22 stsp free(ref_id);
1629 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1630 5d844a1e 2019-08-13 stsp err = NULL;
1631 5d844a1e 2019-08-13 stsp tag = NULL;
1634 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1635 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1636 48cae60d 2020-09-22 stsp free(ref_id);
1638 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1639 52b5abe1 2019-08-13 stsp if (cmp != 0)
1641 8b473291 2019-02-21 stsp s = *refs_str;
1642 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1643 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1644 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1646 8b473291 2019-02-21 stsp *refs_str = NULL;
1652 8b473291 2019-02-21 stsp return err;
1655 963b370f 2018-05-20 stsp static const struct got_error *
1656 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1657 27a741e5 2019-09-11 stsp int col_tab_align)
1659 e6b8b890 2020-12-29 naddy char *smallerthan;
1661 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1662 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1663 5813d178 2019-03-09 stsp author = smallerthan + 1;
1664 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1665 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1666 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1669 5813d178 2019-03-09 stsp static const struct got_error *
1670 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1671 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1672 8fdc79fe 2020-12-01 naddy int author_display_cols)
1674 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1675 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1676 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1677 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1678 5813d178 2019-03-09 stsp char *author = NULL;
1679 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1680 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1681 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1682 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1683 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1684 ccb26ccd 2018-11-05 stsp struct tm tm;
1685 45d799e2 2018-12-23 stsp time_t committer_time;
1686 11b20872 2019-11-08 stsp struct tog_color *tc;
1688 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1689 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1690 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1691 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1692 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1694 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1695 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1697 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1698 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1700 11b20872 2019-11-08 stsp wattr_on(view->window,
1701 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1702 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1704 11b20872 2019-11-08 stsp wattr_off(view->window,
1705 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1706 27a741e5 2019-09-11 stsp col = limit;
1707 b39d25c7 2018-07-10 stsp if (col > avail)
1708 b39d25c7 2018-07-10 stsp goto done;
1710 6570a66d 2019-11-08 stsp if (avail >= 120) {
1711 6570a66d 2019-11-08 stsp char *id_str;
1712 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1714 6570a66d 2019-11-08 stsp goto done;
1715 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1717 11b20872 2019-11-08 stsp wattr_on(view->window,
1718 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1719 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1721 11b20872 2019-11-08 stsp wattr_off(view->window,
1722 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1723 6570a66d 2019-11-08 stsp free(id_str);
1725 6570a66d 2019-11-08 stsp if (col > avail)
1726 6570a66d 2019-11-08 stsp goto done;
1729 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1730 5813d178 2019-03-09 stsp if (author == NULL) {
1731 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1732 80ddbec8 2018-04-29 stsp goto done;
1734 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1736 bb737323 2018-05-20 stsp goto done;
1737 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1739 11b20872 2019-11-08 stsp wattr_on(view->window,
1740 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1741 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1743 11b20872 2019-11-08 stsp wattr_off(view->window,
1744 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1745 bb737323 2018-05-20 stsp col += author_width;
1746 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1747 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1749 bb737323 2018-05-20 stsp author_width++;
1751 9c2eaf34 2018-05-20 stsp if (col > avail)
1752 9c2eaf34 2018-05-20 stsp goto done;
1754 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1756 6d9fbc00 2018-04-29 stsp goto done;
1757 bb737323 2018-05-20 stsp logmsg = logmsg0;
1758 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1760 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1761 bb737323 2018-05-20 stsp if (newline)
1762 bb737323 2018-05-20 stsp *newline = '\0';
1763 ccda2f4d 2022-06-16 stsp limit = avail - col;
1764 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
1765 4d1f6af3 2022-06-17 op limit--; /* for the border */
1766 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1767 ccda2f4d 2022-06-16 stsp limit, col, 1);
1769 29688b02 2022-06-16 stsp goto done;
1770 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1771 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1772 27a741e5 2019-09-11 stsp while (col < avail) {
1773 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1777 80ddbec8 2018-04-29 stsp free(logmsg0);
1778 bb737323 2018-05-20 stsp free(wlogmsg);
1779 5813d178 2019-03-09 stsp free(author);
1780 bb737323 2018-05-20 stsp free(wauthor);
1781 80ddbec8 2018-04-29 stsp free(line);
1782 80ddbec8 2018-04-29 stsp return err;
1785 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1786 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1787 899d86c2 2018-05-10 stsp struct got_object_id *id)
1789 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1791 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1792 80ddbec8 2018-04-29 stsp if (entry == NULL)
1793 899d86c2 2018-05-10 stsp return NULL;
1795 899d86c2 2018-05-10 stsp entry->id = id;
1796 99db9666 2018-05-07 stsp entry->commit = commit;
1797 899d86c2 2018-05-10 stsp return entry;
1800 99db9666 2018-05-07 stsp static void
1801 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1803 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1805 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1806 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1807 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1808 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1809 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1810 99db9666 2018-05-07 stsp free(entry);
1813 99db9666 2018-05-07 stsp static void
1814 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1816 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1817 99db9666 2018-05-07 stsp pop_commit(commits);
1820 c4972b91 2018-05-07 stsp static const struct got_error *
1821 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1822 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1824 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1825 13add988 2019-10-15 stsp regmatch_t regmatch;
1826 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1828 13add988 2019-10-15 stsp *have_match = 0;
1830 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1832 13add988 2019-10-15 stsp return err;
1834 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1836 13add988 2019-10-15 stsp goto done;
1838 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1839 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1840 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1841 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1842 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1843 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1844 13add988 2019-10-15 stsp *have_match = 1;
1846 13add988 2019-10-15 stsp free(id_str);
1847 13add988 2019-10-15 stsp free(logmsg);
1848 13add988 2019-10-15 stsp return err;
1851 13add988 2019-10-15 stsp static const struct got_error *
1852 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1854 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1857 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1858 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1859 1a76625f 2018-10-22 stsp * while updating the display.
1862 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1863 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1864 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1865 1a76625f 2018-10-22 stsp int errcode;
1867 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1868 4e0d2870 2020-12-07 naddy NULL, NULL);
1869 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1872 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1875 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1876 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1877 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1881 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1882 1a76625f 2018-10-22 stsp if (errcode) {
1883 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1884 13add988 2019-10-15 stsp "pthread_mutex_lock");
1888 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1889 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1890 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1892 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1893 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1894 7c1452c1 2020-03-26 stsp int have_match;
1895 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1898 7c1452c1 2020-03-26 stsp if (have_match)
1899 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1902 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1903 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1904 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1905 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1908 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1910 9ba79e04 2018-06-11 stsp return err;
1913 2b779855 2020-12-05 naddy static void
1914 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1916 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1917 2b779855 2020-12-05 naddy int ncommits = 0;
1919 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1920 2b779855 2020-12-05 naddy while (entry) {
1921 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1922 2b779855 2020-12-05 naddy s->selected_entry = entry;
1925 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1926 2b779855 2020-12-05 naddy ncommits++;
1930 0553a4e3 2018-04-30 stsp static const struct got_error *
1931 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1933 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1934 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1935 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1936 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1937 60493ae3 2019-06-20 stsp int width;
1938 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1939 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1940 8b473291 2019-02-21 stsp char *refs_str = NULL;
1941 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1942 11b20872 2019-11-08 stsp struct tog_color *tc;
1943 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1945 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1946 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1947 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1948 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1950 ecb28ae0 2018-07-16 stsp return err;
1951 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1952 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1953 d2075bf3 2020-12-25 stsp if (refs) {
1954 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1955 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1957 d2075bf3 2020-12-25 stsp goto done;
1961 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1962 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1964 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1965 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1966 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1967 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1968 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1969 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1970 8f4ed634 2020-03-26 stsp goto done;
1973 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1975 f9686aa5 2020-03-27 stsp if (view->searching) {
1976 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1977 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1978 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1979 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1980 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1981 f9686aa5 2020-03-27 stsp search_str = "searching...";
1984 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1985 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1986 f9686aa5 2020-03-27 stsp search_str ? search_str :
1987 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1988 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1989 8f4ed634 2020-03-26 stsp goto done;
1993 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1994 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1995 87411fa9 2022-06-16 stsp "........................................",
1996 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1997 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1998 1a76625f 2018-10-22 stsp header = NULL;
1999 1a76625f 2018-10-22 stsp goto done;
2001 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2002 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2003 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2004 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2005 1a76625f 2018-10-22 stsp header = NULL;
2006 1a76625f 2018-10-22 stsp goto done;
2008 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2010 1a76625f 2018-10-22 stsp goto done;
2012 2814baeb 2018-08-01 stsp werase(view->window);
2014 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2015 a3404814 2018-09-02 stsp wstandout(view->window);
2016 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2018 11b20872 2019-11-08 stsp wattr_on(view->window,
2019 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2020 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2022 11b20872 2019-11-08 stsp wattr_off(view->window,
2023 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2024 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2025 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2028 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2029 a3404814 2018-09-02 stsp wstandend(view->window);
2030 ecb28ae0 2018-07-16 stsp free(wline);
2031 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2032 1a76625f 2018-10-22 stsp goto done;
2034 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2035 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2036 5813d178 2019-03-09 stsp ncommits = 0;
2037 145b6838 2022-06-16 stsp view->maxx = 0;
2038 5813d178 2019-03-09 stsp while (entry) {
2039 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2040 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2041 5813d178 2019-03-09 stsp int width;
2042 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2044 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2045 5813d178 2019-03-09 stsp if (author == NULL) {
2046 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2047 5813d178 2019-03-09 stsp goto done;
2049 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2050 27a741e5 2019-09-11 stsp date_display_cols);
2051 5813d178 2019-03-09 stsp if (author_cols < width)
2052 5813d178 2019-03-09 stsp author_cols = width;
2053 5813d178 2019-03-09 stsp free(wauthor);
2054 5813d178 2019-03-09 stsp free(author);
2055 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
2057 145b6838 2022-06-16 stsp goto done;
2058 145b6838 2022-06-16 stsp msg = msg0;
2059 145b6838 2022-06-16 stsp while (*msg == '\n')
2061 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2062 29688b02 2022-06-16 stsp *eol = '\0';
2063 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2064 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2066 29688b02 2022-06-16 stsp goto done;
2067 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2068 145b6838 2022-06-16 stsp free(msg0);
2069 29688b02 2022-06-16 stsp free(wmsg);
2070 7ca04879 2019-10-19 stsp ncommits++;
2071 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2074 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2075 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2076 867c6645 2018-07-10 stsp ncommits = 0;
2077 899d86c2 2018-05-10 stsp while (entry) {
2078 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2080 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2081 2814baeb 2018-08-01 stsp wstandout(view->window);
2082 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2083 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2084 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2085 2814baeb 2018-08-01 stsp wstandend(view->window);
2087 60493ae3 2019-06-20 stsp goto done;
2088 0553a4e3 2018-04-30 stsp ncommits++;
2089 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2090 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2093 9b058f45 2022-06-30 mark view_border(view);
2095 1a76625f 2018-10-22 stsp free(id_str);
2096 8b473291 2019-02-21 stsp free(refs_str);
2097 1a76625f 2018-10-22 stsp free(ncommits_str);
2098 1a76625f 2018-10-22 stsp free(header);
2099 80ddbec8 2018-04-29 stsp return err;
2102 07b55e75 2018-05-10 stsp static void
2103 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2105 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2106 07b55e75 2018-05-10 stsp int nscrolled = 0;
2108 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2109 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2112 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2113 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2114 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2115 07b55e75 2018-05-10 stsp if (entry) {
2116 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2117 07b55e75 2018-05-10 stsp nscrolled++;
2122 aa075928 2018-05-10 stsp static const struct got_error *
2123 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2125 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2126 5e224a3e 2019-02-22 stsp int errcode;
2128 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2130 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2131 ffe38506 2020-12-01 naddy if (ta->log_complete)
2134 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2135 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2136 7aafa0d1 2019-02-22 stsp if (errcode)
2137 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2138 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2141 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2142 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2144 7c1452c1 2020-03-26 stsp if (!wait)
2147 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2148 ffe38506 2020-12-01 naddy show_log_view(view);
2149 7c1452c1 2020-03-26 stsp update_panels();
2150 7c1452c1 2020-03-26 stsp doupdate();
2152 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2153 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2154 82954512 2020-02-03 stsp if (errcode)
2155 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2156 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2158 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2159 ffe38506 2020-12-01 naddy show_log_view(view);
2160 7c1452c1 2020-03-26 stsp update_panels();
2161 7c1452c1 2020-03-26 stsp doupdate();
2164 5e224a3e 2019-02-22 stsp return NULL;
2167 5e224a3e 2019-02-22 stsp static const struct got_error *
2168 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2170 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2171 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2173 9b058f45 2022-06-30 mark state->thread_args.commits_needed = view->nscrolled;
2174 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2175 9b058f45 2022-06-30 mark view->nscrolled = 0;
2177 9b058f45 2022-06-30 mark return err;
2180 9b058f45 2022-06-30 mark static const struct got_error *
2181 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2183 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2184 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2185 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2186 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2188 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2189 5e224a3e 2019-02-22 stsp return NULL;
2191 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2192 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2193 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2195 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2197 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2198 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2200 5e224a3e 2019-02-22 stsp return err;
2204 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2205 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2208 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2209 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2211 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2212 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2214 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2215 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2217 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
2218 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2220 9b058f45 2022-06-30 mark view->nscrolled = 0;
2222 dd0a52c1 2018-05-20 stsp return err;
2225 cd0acaa7 2018-05-20 stsp static const struct got_error *
2226 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2227 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2228 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2230 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2231 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2232 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2234 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2235 15a94983 2018-12-23 stsp if (diff_view == NULL)
2236 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2238 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2239 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2240 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2241 e5a0f69f 2018-08-18 stsp if (err == NULL)
2242 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2243 cd0acaa7 2018-05-20 stsp return err;
2246 80ddbec8 2018-04-29 stsp static const struct got_error *
2247 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2248 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2250 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2252 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2253 941e9f74 2019-05-21 stsp if (parent == NULL)
2254 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2256 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2257 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2258 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2259 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2260 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2261 941e9f74 2019-05-21 stsp s->tree = subtree;
2262 941e9f74 2019-05-21 stsp s->selected = 0;
2263 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2264 941e9f74 2019-05-21 stsp return NULL;
2267 941e9f74 2019-05-21 stsp static const struct got_error *
2268 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2269 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2271 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2272 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2273 941e9f74 2019-05-21 stsp const char *p;
2274 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2276 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2278 941e9f74 2019-05-21 stsp while (*p) {
2279 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2280 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2281 56e0773d 2019-11-28 stsp char *te_name;
2283 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2286 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2287 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2288 941e9f74 2019-05-21 stsp if (slash == NULL)
2289 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2291 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2292 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2293 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2296 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2297 56e0773d 2019-11-28 stsp if (te == NULL) {
2298 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2299 56e0773d 2019-11-28 stsp free(te_name);
2302 56e0773d 2019-11-28 stsp free(te_name);
2303 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2305 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2306 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2308 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2309 941e9f74 2019-05-21 stsp if (slash)
2310 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2312 941e9f74 2019-05-21 stsp subpath = strdup(path);
2313 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2314 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2318 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2323 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2324 941e9f74 2019-05-21 stsp free(tree_id);
2328 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2329 941e9f74 2019-05-21 stsp if (err) {
2330 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2333 941e9f74 2019-05-21 stsp if (slash == NULL)
2335 941e9f74 2019-05-21 stsp free(subpath);
2336 941e9f74 2019-05-21 stsp subpath = NULL;
2337 941e9f74 2019-05-21 stsp p = slash;
2340 941e9f74 2019-05-21 stsp free(subpath);
2341 1a76625f 2018-10-22 stsp return err;
2344 61266923 2020-01-14 stsp static const struct got_error *
2345 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2346 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2347 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2349 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2350 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2351 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2353 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2354 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2355 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2357 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2359 55cccc34 2020-02-20 stsp return err;
2360 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2362 55cccc34 2020-02-20 stsp *new_view = tree_view;
2364 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2365 55cccc34 2020-02-20 stsp return NULL;
2367 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2370 55cccc34 2020-02-20 stsp static const struct got_error *
2371 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2373 61266923 2020-01-14 stsp sigset_t sigset;
2374 61266923 2020-01-14 stsp int errcode;
2376 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2377 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2379 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2380 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2381 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2382 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2383 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2384 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2385 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2386 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2387 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2389 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2390 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2391 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2393 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2394 61266923 2020-01-14 stsp if (errcode)
2395 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2397 61266923 2020-01-14 stsp return NULL;
2400 1a76625f 2018-10-22 stsp static void *
2401 1a76625f 2018-10-22 stsp log_thread(void *arg)
2403 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2404 1a76625f 2018-10-22 stsp int errcode = 0;
2405 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2406 1a76625f 2018-10-22 stsp int done = 0;
2408 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2410 61266923 2020-01-14 stsp return (void *)err;
2412 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2413 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2414 1a76625f 2018-10-22 stsp if (err) {
2415 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2416 1a76625f 2018-10-22 stsp return (void *)err;
2417 1a76625f 2018-10-22 stsp err = NULL;
2419 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2420 1a76625f 2018-10-22 stsp a->commits_needed--;
2422 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2423 3abe8080 2019-04-10 stsp if (errcode) {
2424 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2425 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2427 3abe8080 2019-04-10 stsp } else if (*a->quit)
2429 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2430 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2431 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2432 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2435 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2436 7c1452c1 2020-03-26 stsp if (errcode) {
2437 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2438 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2439 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2444 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2446 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2447 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2448 7c1452c1 2020-03-26 stsp &tog_mutex);
2449 7c1452c1 2020-03-26 stsp if (errcode)
2450 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2451 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2452 21355643 2020-12-06 stsp if (*a->quit)
2457 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2458 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2459 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2460 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2462 3abe8080 2019-04-10 stsp a->log_complete = 1;
2463 1a76625f 2018-10-22 stsp return (void *)err;
2466 1a76625f 2018-10-22 stsp static const struct got_error *
2467 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2469 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2470 1a76625f 2018-10-22 stsp int errcode;
2472 1a76625f 2018-10-22 stsp if (s->thread) {
2473 1a76625f 2018-10-22 stsp s->quit = 1;
2474 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2475 1a76625f 2018-10-22 stsp if (errcode)
2476 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2477 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2478 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2479 1a76625f 2018-10-22 stsp if (errcode)
2480 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2481 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2482 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2483 1a76625f 2018-10-22 stsp if (errcode)
2484 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2485 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2486 1a76625f 2018-10-22 stsp if (errcode)
2487 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2488 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2489 1a76625f 2018-10-22 stsp s->thread = NULL;
2492 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2493 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2494 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2497 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2498 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2499 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2500 74467cc8 2022-06-15 stsp if (err == NULL)
2501 74467cc8 2022-06-15 stsp err = pack_err;
2502 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2505 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2506 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2507 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2510 9343a5fb 2018-06-23 stsp return err;
2513 9343a5fb 2018-06-23 stsp static const struct got_error *
2514 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2516 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2517 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2518 276b94a1 2020-11-13 naddy int errcode;
2520 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2522 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2523 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2524 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2526 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2527 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2528 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2530 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2531 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2532 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2533 1a76625f 2018-10-22 stsp free(s->start_id);
2534 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2535 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2536 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2537 1a76625f 2018-10-22 stsp return err;
2540 1a76625f 2018-10-22 stsp static const struct got_error *
2541 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2543 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2545 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2546 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2547 60493ae3 2019-06-20 stsp return NULL;
2550 60493ae3 2019-06-20 stsp static const struct got_error *
2551 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2553 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2554 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2555 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2557 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2558 f9686aa5 2020-03-27 stsp show_log_view(view);
2559 f9686aa5 2020-03-27 stsp update_panels();
2560 f9686aa5 2020-03-27 stsp doupdate();
2562 96e2b566 2019-07-08 stsp if (s->search_entry) {
2563 13add988 2019-10-15 stsp int errcode, ch;
2564 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2565 13add988 2019-10-15 stsp if (errcode)
2566 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2567 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2568 13add988 2019-10-15 stsp ch = wgetch(view->window);
2569 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2570 13add988 2019-10-15 stsp if (errcode)
2571 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2572 13add988 2019-10-15 stsp "pthread_mutex_lock");
2573 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2574 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2575 678cbce5 2019-07-28 stsp return NULL;
2577 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2578 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2580 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2581 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2582 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2583 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2584 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2587 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2588 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2589 f704b35c 2022-06-18 stsp * might have changed.
2591 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2592 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2593 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2595 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2597 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2598 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2599 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2601 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2602 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2605 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2608 60493ae3 2019-06-20 stsp while (1) {
2609 13add988 2019-10-15 stsp int have_match = 0;
2611 60493ae3 2019-06-20 stsp if (entry == NULL) {
2612 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2613 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2614 f9967bca 2020-03-27 stsp view->search_next_done =
2615 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2616 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2617 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2618 f801134a 2019-06-25 stsp return NULL;
2621 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2622 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2623 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2625 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2626 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2629 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2630 13add988 2019-10-15 stsp &view->regex);
2633 13add988 2019-10-15 stsp if (have_match) {
2634 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2635 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2639 96e2b566 2019-07-08 stsp s->search_entry = entry;
2640 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2641 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2643 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2646 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2647 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2648 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2649 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2651 60493ae3 2019-06-20 stsp return err;
2654 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2655 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2657 60493ae3 2019-06-20 stsp return err;
2662 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2664 60493ae3 2019-06-20 stsp return NULL;
2667 60493ae3 2019-06-20 stsp static const struct got_error *
2668 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2669 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2670 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2672 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2673 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2674 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2675 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2676 1a76625f 2018-10-22 stsp int errcode;
2678 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2679 f135c941 2020-02-20 stsp free(s->in_repo_path);
2680 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2681 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2682 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2685 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2686 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2687 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2689 fb2756b9 2018-08-04 stsp s->repo = repo;
2690 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2691 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2692 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2693 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2694 9cd7cbd1 2020-12-07 stsp goto done;
2697 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2698 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2699 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2700 5036bf37 2018-09-24 stsp goto done;
2702 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2704 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2705 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2706 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2707 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2709 11b20872 2019-11-08 stsp goto done;
2710 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2711 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2712 11b20872 2019-11-08 stsp if (err) {
2713 11b20872 2019-11-08 stsp free_colors(&s->colors);
2714 11b20872 2019-11-08 stsp goto done;
2716 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2717 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2718 11b20872 2019-11-08 stsp if (err) {
2719 11b20872 2019-11-08 stsp free_colors(&s->colors);
2720 11b20872 2019-11-08 stsp goto done;
2724 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2725 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2726 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2727 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2728 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2730 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2731 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2733 74467cc8 2022-06-15 stsp goto done;
2735 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2736 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2737 0ae84acc 2022-06-15 tracey if (err)
2738 0ae84acc 2022-06-15 tracey goto done;
2739 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2740 b672a97a 2020-01-27 stsp !s->log_branches);
2742 1a76625f 2018-10-22 stsp goto done;
2743 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2744 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2746 c5b78334 2020-01-12 stsp goto done;
2748 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2749 1a76625f 2018-10-22 stsp if (errcode) {
2750 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2751 1a76625f 2018-10-22 stsp goto done;
2753 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2754 7c1452c1 2020-03-26 stsp if (errcode) {
2755 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2756 7c1452c1 2020-03-26 stsp goto done;
2759 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2760 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2761 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2762 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2763 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2764 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2765 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2766 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2767 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2768 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2769 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2770 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2771 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2774 1a76625f 2018-10-22 stsp close_log_view(view);
2775 ba4f502b 2018-08-04 stsp return err;
2778 e5a0f69f 2018-08-18 stsp static const struct got_error *
2779 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2781 f2f6d207 2020-11-24 stsp const struct got_error *err;
2782 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2784 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2785 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2786 2b380cc8 2018-10-24 stsp &s->thread_args);
2787 2b380cc8 2018-10-24 stsp if (errcode)
2788 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2789 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2790 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2792 f2f6d207 2020-11-24 stsp return err;
2796 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2799 b880cc75 2022-06-30 mark static void
2800 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
2802 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2804 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
2805 b880cc75 2022-06-30 mark view->count = 0;
2806 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
2809 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2811 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2813 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
2814 b880cc75 2022-06-30 mark --s->selected;
2816 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2818 b880cc75 2022-06-30 mark select_commit(s);
2822 b880cc75 2022-06-30 mark static const struct got_error *
2823 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
2825 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2826 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
2827 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
2829 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
2830 b880cc75 2022-06-30 mark if (first == NULL) {
2831 b880cc75 2022-06-30 mark view->count = 0;
2832 b880cc75 2022-06-30 mark return NULL;
2835 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2836 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
2837 b880cc75 2022-06-30 mark return NULL;
2839 b880cc75 2022-06-30 mark if (!page) {
2840 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
2842 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
2843 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
2844 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
2845 b880cc75 2022-06-30 mark ++s->selected;
2847 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
2848 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
2849 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2850 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
2851 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
2853 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
2854 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
2855 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2857 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
2859 b880cc75 2022-06-30 mark return err;
2860 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
2861 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2862 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
2866 b880cc75 2022-06-30 mark return err;
2869 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
2870 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
2872 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
2873 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2875 b880cc75 2022-06-30 mark select_commit(s);
2877 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2878 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
2879 b880cc75 2022-06-30 mark view->count = 0;
2881 b880cc75 2022-06-30 mark return NULL;
2885 9b058f45 2022-06-30 mark * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2886 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2887 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_HRZN horizontal split
2888 9b058f45 2022-06-30 mark * Assign start column and line of the new split to *x and *y, respectively,
2889 9b058f45 2022-06-30 mark * and assign view mode to view->mode.
2891 9b058f45 2022-06-30 mark static void
2892 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
2894 9b058f45 2022-06-30 mark char *mode;
2899 9b058f45 2022-06-30 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
2901 9b058f45 2022-06-30 mark if (!mode || mode[0] != 'h') {
2902 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_VERT;
2903 9b058f45 2022-06-30 mark *x = view_split_begin_x(view->begin_x);
2904 9b058f45 2022-06-30 mark } else if (mode && mode[0] == 'h') {
2905 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_HRZN;
2906 9b058f45 2022-06-30 mark *y = view_split_begin_y(view->lines);
2910 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
2911 e5a0f69f 2018-08-18 stsp static const struct got_error *
2912 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
2914 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2916 9b058f45 2022-06-30 mark view->nlines = y;
2917 9b058f45 2022-06-30 mark err = view_resize(view);
2919 9b058f45 2022-06-30 mark return err;
2921 9b058f45 2022-06-30 mark err = offset_selection_down(view);
2923 9b058f45 2022-06-30 mark return err;
2926 9b058f45 2022-06-30 mark static const struct got_error *
2927 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2929 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2930 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2931 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2932 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2933 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2934 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
2936 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2937 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
2938 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2939 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2940 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
2941 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
2943 b880cc75 2022-06-30 mark return err;
2946 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
2947 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
2948 0dca135e 2022-06-30 mark --eos; /* border */
2950 528dedf3 2021-08-30 stsp switch (ch) {
2952 1e37a5c2 2019-05-12 jcs s->quit = 1;
2955 145b6838 2022-06-16 stsp view->x = 0;
2958 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2959 640cd7ff 2022-06-22 mark view->count = 0;
2961 145b6838 2022-06-16 stsp case KEY_RIGHT:
2963 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2964 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2966 640cd7ff 2022-06-22 mark view->count = 0;
2968 145b6838 2022-06-16 stsp case KEY_LEFT:
2970 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2971 640cd7ff 2022-06-22 mark if (view->x <= 0)
2972 640cd7ff 2022-06-22 mark view->count = 0;
2975 1e37a5c2 2019-05-12 jcs case KEY_UP:
2978 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2979 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
2982 912a3f79 2021-08-30 j case KEY_HOME:
2983 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
2984 640cd7ff 2022-06-22 mark view->count = 0;
2986 83cc4199 2022-06-13 stsp case CTRL('u'):
2988 83cc4199 2022-06-13 stsp nscroll /= 2;
2989 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2990 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2991 a4292ac5 2019-05-12 jcs case CTRL('b'):
2993 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
2996 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2999 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3000 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3003 912a3f79 2021-08-30 j case KEY_END: {
3004 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3005 912a3f79 2021-08-30 j * traverse them all. */
3006 640cd7ff 2022-06-22 mark view->count = 0;
3007 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3008 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3009 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3012 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3013 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3014 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3015 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3017 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3018 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3020 f3bc9f1d 2021-09-05 naddy if (n > 0)
3021 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3022 2b779855 2020-12-05 naddy select_commit(s);
3025 80b7e8da 2022-06-11 stsp case CTRL('d'):
3027 83cc4199 2022-06-13 stsp nscroll /= 2;
3028 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3029 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3030 61417565 2022-06-20 mark case CTRL('f'):
3031 48bb96f0 2022-06-20 naddy case 'f':
3033 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3035 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3036 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3037 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3038 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3039 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3040 2b779855 2020-12-05 naddy select_commit(s);
3041 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3042 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3043 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3044 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3045 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3048 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3049 49b24ee5 2022-07-03 mark case '\r':
3050 640cd7ff 2022-06-22 mark view->count = 0;
3051 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3054 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3055 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3056 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3058 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3059 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3060 78756c87 2020-11-24 stsp view, s->repo);
3064 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3065 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3066 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3071 e78dc838 2020-12-04 stsp view->focussed = 0;
3072 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3073 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3074 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3076 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3077 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3079 1e37a5c2 2019-05-12 jcs return err;
3080 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3083 e78dc838 2020-12-04 stsp view->focus_child = 1;
3085 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3088 640cd7ff 2022-06-22 mark view->count = 0;
3089 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3091 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3092 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3093 49b24ee5 2022-07-03 mark err = browse_commit_tree(&tree_view, begin_y, begin_x,
3094 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3098 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3099 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3100 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3104 e78dc838 2020-12-04 stsp view->focussed = 0;
3105 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3106 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
3107 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
3108 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3109 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3111 1e37a5c2 2019-05-12 jcs return err;
3112 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3115 e78dc838 2020-12-04 stsp view->focus_child = 1;
3117 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3119 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3120 21355643 2020-12-06 stsp case CTRL('l'):
3122 640cd7ff 2022-06-22 mark view->count = 0;
3123 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3124 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3126 21355643 2020-12-06 stsp err = stop_log_thread(s);
3128 74cfe85e 2020-10-20 stsp return err;
3129 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3130 21355643 2020-12-06 stsp char *parent_path;
3131 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3133 21355643 2020-12-06 stsp return err;
3134 21355643 2020-12-06 stsp free(s->in_repo_path);
3135 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3136 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3137 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3138 21355643 2020-12-06 stsp struct got_object_id *start_id;
3139 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3140 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3141 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3143 21355643 2020-12-06 stsp return err;
3144 21355643 2020-12-06 stsp free(s->start_id);
3145 21355643 2020-12-06 stsp s->start_id = start_id;
3146 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3147 21355643 2020-12-06 stsp } else /* 'B' */
3148 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3150 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3151 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3153 b0dd8d27 2022-06-16 stsp return err;
3155 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3156 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3157 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3159 21355643 2020-12-06 stsp return err;
3160 51a10b52 2020-12-26 stsp tog_free_refs();
3161 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3163 ca51c541 2020-12-07 stsp return err;
3164 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3165 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3167 d01904d4 2019-06-25 stsp return err;
3168 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3169 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3171 b672a97a 2020-01-27 stsp return err;
3172 21355643 2020-12-06 stsp free_commits(&s->commits);
3173 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3174 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3175 21355643 2020-12-06 stsp s->selected_entry = NULL;
3176 21355643 2020-12-06 stsp s->selected = 0;
3177 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3178 21355643 2020-12-06 stsp s->quit = 0;
3179 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3180 dfee752e 2022-06-18 op s->matched_entry = NULL;
3181 dfee752e 2022-06-18 op s->search_entry = NULL;
3184 640cd7ff 2022-06-22 mark view->count = 0;
3185 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3186 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3187 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3188 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3189 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3190 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3191 6458efa5 2020-11-24 stsp if (err) {
3192 6458efa5 2020-11-24 stsp view_close(ref_view);
3193 6458efa5 2020-11-24 stsp return err;
3195 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3196 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3197 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3201 e78dc838 2020-12-04 stsp view->focussed = 0;
3202 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3203 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
3204 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
3205 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3206 6458efa5 2020-11-24 stsp err = view_close_child(view);
3208 6458efa5 2020-11-24 stsp return err;
3209 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3212 e78dc838 2020-12-04 stsp view->focus_child = 1;
3214 6458efa5 2020-11-24 stsp *new_view = ref_view;
3217 640cd7ff 2022-06-22 mark view->count = 0;
3221 80ddbec8 2018-04-29 stsp return err;
3224 4ed7e80c 2018-05-20 stsp static const struct got_error *
3225 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3227 c2db6724 2019-01-04 stsp const struct got_error *error;
3229 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3230 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3231 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3233 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3234 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3236 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3237 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3239 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3240 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3242 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3243 c2db6724 2019-01-04 stsp if (error != NULL)
3244 c2db6724 2019-01-04 stsp return error;
3246 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3247 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3249 c2db6724 2019-01-04 stsp return NULL;
3252 a915003a 2019-02-05 stsp static void
3253 a915003a 2019-02-05 stsp init_curses(void)
3256 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3257 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3258 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3260 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3261 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3262 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3263 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3264 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3266 a915003a 2019-02-05 stsp initscr();
3268 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3271 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3272 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3273 a915003a 2019-02-05 stsp curs_set(0);
3274 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3275 6d17833f 2019-11-08 stsp start_color();
3276 6d17833f 2019-11-08 stsp use_default_colors();
3280 c2db6724 2019-01-04 stsp static const struct got_error *
3281 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3282 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3284 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3286 f135c941 2020-02-20 stsp if (argc == 0) {
3287 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3288 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3289 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3290 f135c941 2020-02-20 stsp return NULL;
3293 f135c941 2020-02-20 stsp if (worktree) {
3294 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3297 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3299 f135c941 2020-02-20 stsp return err;
3300 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3301 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3302 bfd61697 2020-11-14 stsp p) == -1) {
3303 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3304 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3308 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3310 f135c941 2020-02-20 stsp return err;
3313 f135c941 2020-02-20 stsp static const struct got_error *
3314 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3316 80ddbec8 2018-04-29 stsp const struct got_error *error;
3317 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3318 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3319 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3320 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3321 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3322 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3323 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3324 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3325 04cc582a 2018-08-01 stsp struct tog_view *view;
3326 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3328 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3329 80ddbec8 2018-04-29 stsp switch (ch) {
3331 b672a97a 2020-01-27 stsp log_branches = 1;
3334 80ddbec8 2018-04-29 stsp start_commit = optarg;
3337 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3338 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3339 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3343 17020d27 2019-03-07 stsp usage_log();
3344 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3348 80ddbec8 2018-04-29 stsp argc -= optind;
3349 80ddbec8 2018-04-29 stsp argv += optind;
3351 f135c941 2020-02-20 stsp if (argc > 1)
3352 f135c941 2020-02-20 stsp usage_log();
3354 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3355 0ae84acc 2022-06-15 tracey if (error != NULL)
3356 0ae84acc 2022-06-15 tracey goto done;
3358 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3359 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3360 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3361 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3362 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3363 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3364 c156c7a4 2020-12-18 stsp goto done;
3365 a1fbf39a 2019-08-11 stsp if (worktree)
3366 6962eb72 2020-02-20 stsp repo_path =
3367 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3369 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3370 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3371 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3372 c156c7a4 2020-12-18 stsp goto done;
3376 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3377 80ddbec8 2018-04-29 stsp if (error != NULL)
3378 ecb28ae0 2018-07-16 stsp goto done;
3380 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3381 f135c941 2020-02-20 stsp repo, worktree);
3382 f135c941 2020-02-20 stsp if (error)
3383 f135c941 2020-02-20 stsp goto done;
3385 f135c941 2020-02-20 stsp init_curses();
3387 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3388 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3389 c02c541e 2019-03-29 stsp if (error)
3390 c02c541e 2019-03-29 stsp goto done;
3392 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3393 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3394 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3395 87670572 2020-12-26 naddy if (error)
3396 87670572 2020-12-26 naddy goto done;
3399 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3400 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3401 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3402 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3403 d8f38dc4 2020-12-05 stsp if (error)
3404 d8f38dc4 2020-12-05 stsp goto done;
3405 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3407 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3408 d8f38dc4 2020-12-05 stsp if (error == NULL)
3409 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3410 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3411 d8f38dc4 2020-12-05 stsp goto done;
3412 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3413 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3414 d8f38dc4 2020-12-05 stsp if (error)
3415 d8f38dc4 2020-12-05 stsp goto done;
3418 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3419 04cc582a 2018-08-01 stsp if (view == NULL) {
3420 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3421 04cc582a 2018-08-01 stsp goto done;
3423 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3424 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3425 ba4f502b 2018-08-04 stsp if (error)
3426 ba4f502b 2018-08-04 stsp goto done;
3427 2fc00ff4 2019-08-31 stsp if (worktree) {
3428 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3429 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3430 2fc00ff4 2019-08-31 stsp worktree = NULL;
3432 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3434 f135c941 2020-02-20 stsp free(in_repo_path);
3435 ecb28ae0 2018-07-16 stsp free(repo_path);
3436 ecb28ae0 2018-07-16 stsp free(cwd);
3437 899d86c2 2018-05-10 stsp free(start_id);
3438 d8f38dc4 2020-12-05 stsp free(label);
3440 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3441 1d0f4054 2021-06-17 stsp if (repo) {
3442 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3443 1d0f4054 2021-06-17 stsp if (error == NULL)
3444 1d0f4054 2021-06-17 stsp error = close_err;
3446 ec142235 2019-03-07 stsp if (worktree)
3447 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3448 0ae84acc 2022-06-15 tracey if (pack_fds) {
3449 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3450 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3451 0ae84acc 2022-06-15 tracey if (error == NULL)
3452 0ae84acc 2022-06-15 tracey error = pack_err;
3454 51a10b52 2020-12-26 stsp tog_free_refs();
3455 80ddbec8 2018-04-29 stsp return error;
3458 4ed7e80c 2018-05-20 stsp __dead static void
3459 9f7d7167 2018-04-29 stsp usage_diff(void)
3462 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3463 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3467 6d17833f 2019-11-08 stsp static int
3468 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3469 41605754 2020-11-12 stsp regmatch_t *regmatch)
3471 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3474 336075a4 2022-06-25 op static struct tog_color *
3475 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3477 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3479 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3480 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3481 6d17833f 2019-11-08 stsp return tc;
3484 6d17833f 2019-11-08 stsp return NULL;
3487 4ed7e80c 2018-05-20 stsp static const struct got_error *
3488 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3489 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3491 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3492 44a87665 2022-06-16 stsp char *exstr = NULL;
3493 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3494 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3495 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3496 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3498 41605754 2020-11-12 stsp *wtotal = 0;
3500 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3501 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3503 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3505 44a87665 2022-06-16 stsp return err;
3507 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3508 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3509 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3510 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3511 44a87665 2022-06-16 stsp goto done;
3513 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3514 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3515 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3516 1853e0f4 2022-06-16 stsp goto done;
3518 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3519 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3520 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3521 1853e0f4 2022-06-16 stsp goto done;
3524 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3525 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 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 n = MAX(width0 - skipcol, 0);
3531 1853e0f4 2022-06-16 stsp free(wline);
3532 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3533 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3535 1853e0f4 2022-06-16 stsp goto done;
3536 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3537 1853e0f4 2022-06-16 stsp wlimit -= width;
3538 1853e0f4 2022-06-16 stsp *wtotal += width;
3541 41605754 2020-11-12 stsp if (wlimit > 0) {
3542 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3543 1853e0f4 2022-06-16 stsp size_t wlen;
3545 1853e0f4 2022-06-16 stsp free(wline);
3546 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3547 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3549 1853e0f4 2022-06-16 stsp goto done;
3550 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3551 1853e0f4 2022-06-16 stsp while (i < wlen) {
3552 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3553 1853e0f4 2022-06-16 stsp if (width == -1) {
3554 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3555 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3556 1853e0f4 2022-06-16 stsp goto done;
3558 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3560 61417565 2022-06-20 mark w += width;
3563 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3564 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3565 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3566 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3567 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3568 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3569 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3573 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3574 1853e0f4 2022-06-16 stsp free(wline);
3575 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3576 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3577 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3578 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3580 1853e0f4 2022-06-16 stsp goto done;
3581 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3583 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3584 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3586 1853e0f4 2022-06-16 stsp goto done;
3587 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3589 1853e0f4 2022-06-16 stsp *wtotal += width2;
3592 145b6838 2022-06-16 stsp free(wline);
3593 44a87665 2022-06-16 stsp free(exstr);
3594 1853e0f4 2022-06-16 stsp free(seg0);
3595 1853e0f4 2022-06-16 stsp free(seg1);
3596 1853e0f4 2022-06-16 stsp free(seg2);
3597 1853e0f4 2022-06-16 stsp return err;
3600 41605754 2020-11-12 stsp static const struct got_error *
3601 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3603 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3604 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3605 61e69b96 2018-05-20 stsp const struct got_error *err;
3606 fe621944 2020-11-10 stsp int nprinted = 0;
3607 b304db33 2018-05-20 stsp char *line;
3608 826082fe 2020-12-10 stsp size_t linesize = 0;
3609 826082fe 2020-12-10 stsp ssize_t linelen;
3610 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3611 61e69b96 2018-05-20 stsp wchar_t *wline;
3612 e0b650dd 2018-05-20 stsp int width;
3613 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3614 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3615 fe621944 2020-11-10 stsp off_t line_offset;
3617 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3618 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3619 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3621 f7d12f7e 2018-08-01 stsp werase(view->window);
3623 a3404814 2018-09-02 stsp if (header) {
3624 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3625 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3626 135a2da0 2020-11-11 stsp header) == -1)
3627 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3628 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3630 135a2da0 2020-11-11 stsp free(line);
3632 a3404814 2018-09-02 stsp return err;
3634 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3635 a3404814 2018-09-02 stsp wstandout(view->window);
3636 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3637 e54cc94a 2020-11-11 stsp free(wline);
3638 e54cc94a 2020-11-11 stsp wline = NULL;
3639 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3640 a3404814 2018-09-02 stsp wstandend(view->window);
3641 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3642 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3644 a3404814 2018-09-02 stsp if (max_lines <= 1)
3645 a3404814 2018-09-02 stsp return NULL;
3646 a3404814 2018-09-02 stsp max_lines--;
3649 89f1a395 2020-12-01 naddy s->eof = 0;
3650 145b6838 2022-06-16 stsp view->maxx = 0;
3651 826082fe 2020-12-10 stsp line = NULL;
3652 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3653 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3654 826082fe 2020-12-10 stsp if (linelen == -1) {
3655 826082fe 2020-12-10 stsp if (feof(s->f)) {
3656 826082fe 2020-12-10 stsp s->eof = 1;
3659 826082fe 2020-12-10 stsp free(line);
3660 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3663 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3664 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3665 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3666 1853e0f4 2022-06-16 stsp if (err) {
3667 1853e0f4 2022-06-16 stsp free(line);
3668 1853e0f4 2022-06-16 stsp return err;
3670 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3671 1853e0f4 2022-06-16 stsp free(wline);
3672 1853e0f4 2022-06-16 stsp wline = NULL;
3674 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3676 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3677 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3678 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3679 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3680 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3681 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3682 41605754 2020-11-12 stsp if (err) {
3683 41605754 2020-11-12 stsp free(line);
3684 41605754 2020-11-12 stsp return err;
3688 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3689 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3690 969c159c 2022-06-16 stsp if (err) {
3691 969c159c 2022-06-16 stsp free(line);
3692 969c159c 2022-06-16 stsp return err;
3694 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3695 969c159c 2022-06-16 stsp free(wline);
3696 41605754 2020-11-12 stsp wline = NULL;
3699 6d17833f 2019-11-08 stsp wattr_off(view->window,
3700 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3701 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3702 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3703 fe621944 2020-11-10 stsp nprinted++;
3705 826082fe 2020-12-10 stsp free(line);
3706 fe621944 2020-11-10 stsp if (nprinted >= 1)
3707 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3708 89f1a395 2020-12-01 naddy (nprinted - 1);
3710 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3712 9b058f45 2022-06-30 mark view_border(view);
3714 89f1a395 2020-12-01 naddy if (s->eof) {
3715 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3716 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3717 c3e9aa98 2019-05-13 jcs nprinted++;
3720 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3721 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3723 c3e9aa98 2019-05-13 jcs return err;
3726 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3727 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3728 e54cc94a 2020-11-11 stsp free(wline);
3729 e54cc94a 2020-11-11 stsp wline = NULL;
3730 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3733 26ed57b2 2018-05-19 stsp return NULL;
3736 abd2672a 2018-12-23 stsp static char *
3737 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3739 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3740 09867e48 2019-08-13 stsp char *p, *s;
3742 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3743 09867e48 2019-08-13 stsp if (tm == NULL)
3744 09867e48 2019-08-13 stsp return NULL;
3745 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3746 09867e48 2019-08-13 stsp if (s == NULL)
3747 09867e48 2019-08-13 stsp return NULL;
3748 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3750 abd2672a 2018-12-23 stsp *p = '\0';
3754 4ed7e80c 2018-05-20 stsp static const struct got_error *
3755 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3756 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3758 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3759 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3760 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3761 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3763 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3764 0208f208 2020-05-05 stsp if (qid != NULL) {
3765 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3766 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3767 d7b5a0e8 2022-04-20 stsp &qid->id);
3769 0208f208 2020-05-05 stsp return err;
3771 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3772 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3773 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3774 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3775 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3777 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3781 0208f208 2020-05-05 stsp if (tree_id1) {
3782 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3784 0208f208 2020-05-05 stsp goto done;
3787 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3788 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3790 0208f208 2020-05-05 stsp goto done;
3792 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3793 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3795 0208f208 2020-05-05 stsp if (tree1)
3796 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3797 0208f208 2020-05-05 stsp if (tree2)
3798 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3799 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3800 0208f208 2020-05-05 stsp return err;
3803 0208f208 2020-05-05 stsp static const struct got_error *
3804 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3808 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3809 fe621944 2020-11-10 stsp if (p == NULL)
3810 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3811 fe621944 2020-11-10 stsp *line_offsets = p;
3812 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3813 fe621944 2020-11-10 stsp (*nlines)++;
3814 fe621944 2020-11-10 stsp return NULL;
3817 fe621944 2020-11-10 stsp static const struct got_error *
3818 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3819 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3820 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3822 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3823 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3824 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3825 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3826 45d799e2 2018-12-23 stsp time_t committer_time;
3827 45d799e2 2018-12-23 stsp const char *author, *committer;
3828 8b473291 2019-02-21 stsp char *refs_str = NULL;
3829 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3830 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3831 fe621944 2020-11-10 stsp off_t outoff = 0;
3834 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3836 8b473291 2019-02-21 stsp if (refs) {
3837 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3839 8b473291 2019-02-21 stsp return err;
3842 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3844 abd2672a 2018-12-23 stsp return err;
3846 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3847 15a94983 2018-12-23 stsp if (err) {
3848 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3849 15a94983 2018-12-23 stsp goto done;
3852 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3854 fe621944 2020-11-10 stsp goto done;
3856 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3857 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3858 fe621944 2020-11-10 stsp if (n < 0) {
3859 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3860 abd2672a 2018-12-23 stsp goto done;
3862 fe621944 2020-11-10 stsp outoff += n;
3863 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3865 fe621944 2020-11-10 stsp goto done;
3867 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3868 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3869 fe621944 2020-11-10 stsp if (n < 0) {
3870 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3871 abd2672a 2018-12-23 stsp goto done;
3873 fe621944 2020-11-10 stsp outoff += n;
3874 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3876 fe621944 2020-11-10 stsp goto done;
3878 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3879 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3880 fe621944 2020-11-10 stsp if (datestr) {
3881 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3882 fe621944 2020-11-10 stsp if (n < 0) {
3883 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3884 fe621944 2020-11-10 stsp goto done;
3886 fe621944 2020-11-10 stsp outoff += n;
3887 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3889 fe621944 2020-11-10 stsp goto done;
3891 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3892 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3893 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3894 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3895 fe621944 2020-11-10 stsp if (n < 0) {
3896 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3897 fe621944 2020-11-10 stsp goto done;
3899 fe621944 2020-11-10 stsp outoff += n;
3900 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3902 fe621944 2020-11-10 stsp goto done;
3904 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3905 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3906 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3907 9f98ca05 2021-09-24 stsp int pn = 1;
3908 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3909 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3910 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3912 9f98ca05 2021-09-24 stsp goto done;
3913 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3914 9f98ca05 2021-09-24 stsp if (n < 0) {
3915 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3916 9f98ca05 2021-09-24 stsp goto done;
3918 9f98ca05 2021-09-24 stsp outoff += n;
3919 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3921 9f98ca05 2021-09-24 stsp goto done;
3922 9f98ca05 2021-09-24 stsp free(id_str);
3923 9f98ca05 2021-09-24 stsp id_str = NULL;
3927 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3929 5943eee2 2019-08-13 stsp goto done;
3930 fe621944 2020-11-10 stsp s = logmsg;
3931 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3932 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3933 fe621944 2020-11-10 stsp if (n < 0) {
3934 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3935 fe621944 2020-11-10 stsp goto done;
3937 fe621944 2020-11-10 stsp outoff += n;
3938 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3940 fe621944 2020-11-10 stsp goto done;
3943 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3945 0208f208 2020-05-05 stsp goto done;
3946 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3947 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3948 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3949 fe621944 2020-11-10 stsp if (n < 0) {
3950 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3951 fe621944 2020-11-10 stsp goto done;
3953 fe621944 2020-11-10 stsp outoff += n;
3954 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3956 fe621944 2020-11-10 stsp goto done;
3957 0208f208 2020-05-05 stsp free((char *)pe->path);
3958 0208f208 2020-05-05 stsp free(pe->data);
3961 0208f208 2020-05-05 stsp fputc('\n', outfile);
3963 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3965 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3966 abd2672a 2018-12-23 stsp free(id_str);
3967 5943eee2 2019-08-13 stsp free(logmsg);
3968 8b473291 2019-02-21 stsp free(refs_str);
3969 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3970 7510f233 2020-08-09 stsp if (err) {
3971 ae6a6978 2020-08-09 stsp free(*line_offsets);
3972 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3973 ae6a6978 2020-08-09 stsp *nlines = 0;
3975 fe621944 2020-11-10 stsp return err;
3978 abd2672a 2018-12-23 stsp static const struct got_error *
3979 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3981 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3982 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3983 15a94983 2018-12-23 stsp int obj_type;
3985 fe621944 2020-11-10 stsp free(s->line_offsets);
3986 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3987 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3988 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3989 fe621944 2020-11-10 stsp s->nlines = 0;
3991 511a516b 2018-05-19 stsp f = got_opentemp();
3992 48ae06ee 2018-10-18 stsp if (f == NULL) {
3993 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3994 48ae06ee 2018-10-18 stsp goto done;
3996 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3997 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3998 fb43ecf1 2019-02-11 stsp goto done;
4002 15a94983 2018-12-23 stsp if (s->id1)
4003 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4005 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4007 15a94983 2018-12-23 stsp goto done;
4009 15a94983 2018-12-23 stsp switch (obj_type) {
4010 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4011 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4012 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4013 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4014 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4016 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4017 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4018 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4019 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4020 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4022 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4023 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4024 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4025 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4026 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4028 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4030 3ffacbe1 2020-02-02 tracey goto done;
4031 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4032 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4033 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4034 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4035 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4036 f44b1f58 2020-02-02 tracey if (err)
4037 f44b1f58 2020-02-02 tracey goto done;
4038 f44b1f58 2020-02-02 tracey } else {
4039 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4040 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4041 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4042 fe621944 2020-11-10 stsp err = write_commit_info(
4043 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4044 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4045 f44b1f58 2020-02-02 tracey if (err)
4046 f44b1f58 2020-02-02 tracey goto done;
4051 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4053 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4054 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4055 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4056 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4060 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4063 f44b1f58 2020-02-02 tracey if (err)
4064 f44b1f58 2020-02-02 tracey goto done;
4066 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4067 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4068 48ae06ee 2018-10-18 stsp return err;
4071 f5215bb9 2019-02-22 stsp static void
4072 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4074 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4075 f5215bb9 2019-02-22 stsp update_panels();
4076 f5215bb9 2019-02-22 stsp doupdate();
4079 f44b1f58 2020-02-02 tracey static const struct got_error *
4080 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4082 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4084 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4085 f44b1f58 2020-02-02 tracey return NULL;
4088 f44b1f58 2020-02-02 tracey static const struct got_error *
4089 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4091 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4092 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4093 f44b1f58 2020-02-02 tracey int lineno;
4094 cb713507 2022-06-16 stsp char *line = NULL;
4095 826082fe 2020-12-10 stsp size_t linesize = 0;
4096 826082fe 2020-12-10 stsp ssize_t linelen;
4098 f44b1f58 2020-02-02 tracey if (!view->searching) {
4099 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4100 f44b1f58 2020-02-02 tracey return NULL;
4103 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4104 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4105 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4107 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4109 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4111 f44b1f58 2020-02-02 tracey while (1) {
4112 f44b1f58 2020-02-02 tracey off_t offset;
4114 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4115 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4116 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4120 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4121 f44b1f58 2020-02-02 tracey lineno = 1;
4123 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4126 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4127 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4128 f44b1f58 2020-02-02 tracey free(line);
4129 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4131 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4132 cb713507 2022-06-16 stsp if (linelen != -1) {
4133 cb713507 2022-06-16 stsp char *exstr;
4134 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4137 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4138 cb713507 2022-06-16 stsp &view->regmatch)) {
4139 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4140 cb713507 2022-06-16 stsp s->matched_line = lineno;
4141 cb713507 2022-06-16 stsp free(exstr);
4144 cb713507 2022-06-16 stsp free(exstr);
4146 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4147 f44b1f58 2020-02-02 tracey lineno++;
4149 f44b1f58 2020-02-02 tracey lineno--;
4151 826082fe 2020-12-10 stsp free(line);
4153 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4154 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4155 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4158 145b6838 2022-06-16 stsp return err;
4161 48ae06ee 2018-10-18 stsp static const struct got_error *
4162 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4164 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4165 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4167 b72706c3 2022-06-01 stsp free(s->id1);
4168 b72706c3 2022-06-01 stsp s->id1 = NULL;
4169 b72706c3 2022-06-01 stsp free(s->id2);
4170 b72706c3 2022-06-01 stsp s->id2 = NULL;
4171 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4172 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4173 b72706c3 2022-06-01 stsp s->f = NULL;
4174 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4175 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4176 b72706c3 2022-06-01 stsp s->f1 = NULL;
4177 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4178 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4179 b72706c3 2022-06-01 stsp s->f2 = NULL;
4180 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4181 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4182 f9d37699 2022-06-28 stsp s->fd1 = -1;
4183 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4184 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4185 f9d37699 2022-06-28 stsp s->fd2 = -1;
4186 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4187 b72706c3 2022-06-01 stsp free(s->line_offsets);
4188 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4189 b72706c3 2022-06-01 stsp s->nlines = 0;
4190 b72706c3 2022-06-01 stsp return err;
4193 b72706c3 2022-06-01 stsp static const struct got_error *
4194 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4195 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4196 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4197 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4199 48ae06ee 2018-10-18 stsp const struct got_error *err;
4200 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4202 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4203 f9d37699 2022-06-28 stsp s->fd1 = -1;
4204 f9d37699 2022-06-28 stsp s->fd2 = -1;
4206 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4207 15a94983 2018-12-23 stsp int type1, type2;
4208 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4210 15a94983 2018-12-23 stsp return err;
4211 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4213 15a94983 2018-12-23 stsp return err;
4215 15a94983 2018-12-23 stsp if (type1 != type2)
4216 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4218 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4219 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4220 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4221 f44b1f58 2020-02-02 tracey s->repo = repo;
4222 f44b1f58 2020-02-02 tracey s->id1 = id1;
4223 f44b1f58 2020-02-02 tracey s->id2 = id2;
4224 3dbaef42 2020-11-24 stsp s->label1 = label1;
4225 3dbaef42 2020-11-24 stsp s->label2 = label2;
4227 15a94983 2018-12-23 stsp if (id1) {
4228 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4229 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4230 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4232 5465d566 2020-02-01 tracey s->id1 = NULL;
4234 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4235 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4236 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4237 b72706c3 2022-06-01 stsp goto done;
4240 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4241 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4242 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4243 a00719e9 2022-06-17 stsp goto done;
4246 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4247 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4248 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4249 b72706c3 2022-06-01 stsp goto done;
4252 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4253 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4254 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4255 f9d37699 2022-06-28 stsp goto done;
4258 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4259 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4260 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4261 f9d37699 2022-06-28 stsp goto done;
4264 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4265 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4266 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4267 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4268 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4269 5465d566 2020-02-01 tracey s->log_view = log_view;
4270 5465d566 2020-02-01 tracey s->repo = repo;
4272 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4273 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4274 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4275 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4276 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4278 b72706c3 2022-06-01 stsp goto done;
4279 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4280 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4281 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4283 b72706c3 2022-06-01 stsp goto done;
4284 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4285 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4286 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4288 b72706c3 2022-06-01 stsp goto done;
4290 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4291 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4292 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4293 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4294 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4296 b72706c3 2022-06-01 stsp goto done;
4298 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4299 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4300 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4302 b72706c3 2022-06-01 stsp goto done;
4304 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4305 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4306 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4308 b72706c3 2022-06-01 stsp goto done;
4311 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4312 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4313 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4315 5465d566 2020-02-01 tracey err = create_diff(s);
4317 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4318 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4319 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4320 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4321 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4322 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4325 b72706c3 2022-06-01 stsp close_diff_view(view);
4326 e5a0f69f 2018-08-18 stsp return err;
4329 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4330 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4332 a3404814 2018-09-02 stsp const struct got_error *err;
4333 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4334 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4335 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4337 a3404814 2018-09-02 stsp if (s->id1) {
4338 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4340 a3404814 2018-09-02 stsp return err;
4341 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4343 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4345 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4347 a3404814 2018-09-02 stsp return err;
4348 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4350 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4351 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4352 a3404814 2018-09-02 stsp free(id_str1);
4353 a3404814 2018-09-02 stsp free(id_str2);
4354 a3404814 2018-09-02 stsp return err;
4356 a3404814 2018-09-02 stsp free(id_str1);
4357 a3404814 2018-09-02 stsp free(id_str2);
4359 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4360 267bb3b8 2021-08-01 stsp free(header);
4361 267bb3b8 2021-08-01 stsp return err;
4364 15a087fe 2019-02-21 stsp static const struct got_error *
4365 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4366 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4368 d7a04538 2019-02-21 stsp const struct got_error *err;
4369 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4370 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4371 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4373 15a087fe 2019-02-21 stsp free(s->id2);
4374 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4375 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4376 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4378 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4380 d7a04538 2019-02-21 stsp return err;
4381 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4382 15a087fe 2019-02-21 stsp free(s->id1);
4383 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4384 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4385 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4386 15a087fe 2019-02-21 stsp return NULL;
4389 0cf4efb1 2018-09-29 stsp static const struct got_error *
4390 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4392 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4394 917d79a7 2022-07-01 stsp view->count = 0;
4395 917d79a7 2022-07-01 stsp wclear(view->window);
4396 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4397 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4398 917d79a7 2022-07-01 stsp s->matched_line = 0;
4399 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4400 917d79a7 2022-07-01 stsp return create_diff(s);
4403 917d79a7 2022-07-01 stsp static const struct got_error *
4404 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4406 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4407 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4408 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4409 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4410 826082fe 2020-12-10 stsp char *line = NULL;
4411 826082fe 2020-12-10 stsp size_t linesize = 0;
4412 826082fe 2020-12-10 stsp ssize_t linelen;
4413 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4415 e5a0f69f 2018-08-18 stsp switch (ch) {
4417 145b6838 2022-06-16 stsp view->x = 0;
4420 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4421 640cd7ff 2022-06-22 mark view->count = 0;
4423 145b6838 2022-06-16 stsp case KEY_RIGHT:
4425 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4426 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4428 640cd7ff 2022-06-22 mark view->count = 0;
4430 145b6838 2022-06-16 stsp case KEY_LEFT:
4432 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4433 640cd7ff 2022-06-22 mark if (view->x <= 0)
4434 640cd7ff 2022-06-22 mark view->count = 0;
4438 3dbaef42 2020-11-24 stsp if (ch == 'a')
4439 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4440 3dbaef42 2020-11-24 stsp if (ch == 'w')
4441 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4442 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4445 912a3f79 2021-08-30 j case KEY_HOME:
4446 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4447 640cd7ff 2022-06-22 mark view->count = 0;
4450 912a3f79 2021-08-30 j case KEY_END:
4451 640cd7ff 2022-06-22 mark view->count = 0;
4455 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4459 1e37a5c2 2019-05-12 jcs case KEY_UP:
4460 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4461 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4462 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4464 640cd7ff 2022-06-22 mark view->count = 0;
4466 83cc4199 2022-06-13 stsp case CTRL('u'):
4468 83cc4199 2022-06-13 stsp nscroll /= 2;
4469 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4470 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4471 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4473 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4474 640cd7ff 2022-06-22 mark view->count = 0;
4478 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4479 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4482 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4483 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4484 1e37a5c2 2019-05-12 jcs if (!s->eof)
4485 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4487 640cd7ff 2022-06-22 mark view->count = 0;
4489 83cc4199 2022-06-13 stsp case CTRL('d'):
4491 83cc4199 2022-06-13 stsp nscroll /= 2;
4492 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4493 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4494 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4497 640cd7ff 2022-06-22 mark if (s->eof) {
4498 640cd7ff 2022-06-22 mark view->count = 0;
4502 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4503 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4504 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4505 826082fe 2020-12-10 stsp if (linelen == -1) {
4506 826082fe 2020-12-10 stsp if (feof(s->f)) {
4507 826082fe 2020-12-10 stsp s->eof = 1;
4509 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4513 826082fe 2020-12-10 stsp free(line);
4516 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4517 1e37a5c2 2019-05-12 jcs s->diff_context--;
4518 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4519 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4520 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4521 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4522 27829c9e 2020-11-21 stsp s->nlines) {
4523 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4524 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4527 640cd7ff 2022-06-22 mark view->count = 0;
4530 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4531 1e37a5c2 2019-05-12 jcs s->diff_context++;
4532 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4533 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4534 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4536 640cd7ff 2022-06-22 mark view->count = 0;
4540 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4541 640cd7ff 2022-06-22 mark view->count = 0;
4544 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4545 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4547 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4548 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4552 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4555 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4559 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4560 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4561 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4562 145b6838 2022-06-16 stsp view->x = 0;
4564 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4565 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4569 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4570 640cd7ff 2022-06-22 mark view->count = 0;
4573 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4574 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4576 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4577 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4581 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4584 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4588 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4589 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4590 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4591 145b6838 2022-06-16 stsp view->x = 0;
4593 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4594 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4597 640cd7ff 2022-06-22 mark view->count = 0;
4601 bcbd79e2 2018-08-19 stsp return err;
4604 4ed7e80c 2018-05-20 stsp static const struct got_error *
4605 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4607 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4608 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4609 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4610 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4611 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4612 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4613 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4614 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4615 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4616 3dbaef42 2020-11-24 stsp const char *errstr;
4617 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4618 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4620 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4621 26ed57b2 2018-05-19 stsp switch (ch) {
4623 64453f7e 2020-11-21 stsp force_text_diff = 1;
4626 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4628 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4629 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4630 5a20d08d 2022-02-09 op errstr, errstr);
4632 09b5bff8 2020-02-23 naddy case 'r':
4633 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4634 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4635 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4637 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4640 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4643 17020d27 2019-03-07 stsp usage_diff();
4644 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4648 26ed57b2 2018-05-19 stsp argc -= optind;
4649 26ed57b2 2018-05-19 stsp argv += optind;
4651 26ed57b2 2018-05-19 stsp if (argc == 0) {
4652 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4653 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4654 15a94983 2018-12-23 stsp id_str1 = argv[0];
4655 15a94983 2018-12-23 stsp id_str2 = argv[1];
4657 26ed57b2 2018-05-19 stsp usage_diff();
4659 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4660 0ae84acc 2022-06-15 tracey if (error)
4661 0ae84acc 2022-06-15 tracey goto done;
4663 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4664 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4665 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4666 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4667 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4668 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4669 c156c7a4 2020-12-18 stsp goto done;
4670 a273ac94 2020-02-23 naddy if (worktree)
4671 a273ac94 2020-02-23 naddy repo_path =
4672 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4674 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4675 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4676 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4677 c156c7a4 2020-12-18 stsp goto done;
4681 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4682 eb6600df 2019-01-04 stsp if (error)
4683 eb6600df 2019-01-04 stsp goto done;
4685 a273ac94 2020-02-23 naddy init_curses();
4687 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4688 51a10b52 2020-12-26 stsp if (error)
4689 51a10b52 2020-12-26 stsp goto done;
4691 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4692 26ed57b2 2018-05-19 stsp if (error)
4693 26ed57b2 2018-05-19 stsp goto done;
4695 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4696 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4697 26ed57b2 2018-05-19 stsp if (error)
4698 26ed57b2 2018-05-19 stsp goto done;
4700 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4701 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4702 26ed57b2 2018-05-19 stsp if (error)
4703 26ed57b2 2018-05-19 stsp goto done;
4705 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4706 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4707 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4708 ea5e7bb5 2018-08-01 stsp goto done;
4710 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4711 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4712 5dc9f4bc 2018-08-04 stsp if (error)
4713 5dc9f4bc 2018-08-04 stsp goto done;
4714 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4716 3dbaef42 2020-11-24 stsp free(label1);
4717 3dbaef42 2020-11-24 stsp free(label2);
4718 c02c541e 2019-03-29 stsp free(repo_path);
4719 a273ac94 2020-02-23 naddy free(cwd);
4720 1d0f4054 2021-06-17 stsp if (repo) {
4721 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4722 1d0f4054 2021-06-17 stsp if (error == NULL)
4723 1d0f4054 2021-06-17 stsp error = close_err;
4725 a273ac94 2020-02-23 naddy if (worktree)
4726 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4727 0ae84acc 2022-06-15 tracey if (pack_fds) {
4728 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4729 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4730 0ae84acc 2022-06-15 tracey if (error == NULL)
4731 0ae84acc 2022-06-15 tracey error = pack_err;
4733 51a10b52 2020-12-26 stsp tog_free_refs();
4734 26ed57b2 2018-05-19 stsp return error;
4737 4ed7e80c 2018-05-20 stsp __dead static void
4738 9f7d7167 2018-04-29 stsp usage_blame(void)
4741 87411fa9 2022-06-16 stsp fprintf(stderr,
4742 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4743 9f7d7167 2018-04-29 stsp getprogname());
4747 84451b3e 2018-07-10 stsp struct tog_blame_line {
4748 84451b3e 2018-07-10 stsp int annotated;
4749 84451b3e 2018-07-10 stsp struct got_object_id *id;
4752 4ed7e80c 2018-05-20 stsp static const struct got_error *
4753 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4755 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4756 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4757 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4758 84451b3e 2018-07-10 stsp const struct got_error *err;
4759 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4760 826082fe 2020-12-10 stsp char *line = NULL;
4761 826082fe 2020-12-10 stsp size_t linesize = 0;
4762 826082fe 2020-12-10 stsp ssize_t linelen;
4763 84451b3e 2018-07-10 stsp wchar_t *wline;
4764 27a741e5 2019-09-11 stsp int width;
4765 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4766 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4767 ab089a2a 2018-07-12 stsp char *id_str;
4768 11b20872 2019-11-08 stsp struct tog_color *tc;
4770 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4772 ab089a2a 2018-07-12 stsp return err;
4774 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4775 f7d12f7e 2018-08-01 stsp werase(view->window);
4777 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4778 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4779 ab089a2a 2018-07-12 stsp free(id_str);
4780 ab089a2a 2018-07-12 stsp return err;
4783 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4784 ab089a2a 2018-07-12 stsp free(line);
4785 2550e4c3 2018-07-13 stsp line = NULL;
4787 1cae65b4 2019-09-22 stsp return err;
4788 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4789 a3404814 2018-09-02 stsp wstandout(view->window);
4790 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4792 11b20872 2019-11-08 stsp wattr_on(view->window,
4793 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4794 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4796 11b20872 2019-11-08 stsp wattr_off(view->window,
4797 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4798 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4799 a3404814 2018-09-02 stsp wstandend(view->window);
4800 2550e4c3 2018-07-13 stsp free(wline);
4801 2550e4c3 2018-07-13 stsp wline = NULL;
4802 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4803 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4805 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4806 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4807 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4808 ab089a2a 2018-07-12 stsp free(id_str);
4809 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4811 ab089a2a 2018-07-12 stsp free(id_str);
4812 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4813 3f60a8ef 2018-07-10 stsp free(line);
4814 2550e4c3 2018-07-13 stsp line = NULL;
4816 3f60a8ef 2018-07-10 stsp return err;
4817 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4818 2550e4c3 2018-07-13 stsp free(wline);
4819 2550e4c3 2018-07-13 stsp wline = NULL;
4820 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4821 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4823 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4824 145b6838 2022-06-16 stsp view->maxx = 0;
4825 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4826 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4827 826082fe 2020-12-10 stsp if (linelen == -1) {
4828 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4829 826082fe 2020-12-10 stsp s->eof = 1;
4832 84451b3e 2018-07-10 stsp free(line);
4833 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4835 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4838 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4839 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4840 1853e0f4 2022-06-16 stsp if (err) {
4841 1853e0f4 2022-06-16 stsp free(line);
4842 1853e0f4 2022-06-16 stsp return err;
4844 1853e0f4 2022-06-16 stsp free(wline);
4845 1853e0f4 2022-06-16 stsp wline = NULL;
4846 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4848 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4849 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4851 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4852 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4853 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4854 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4855 27a741e5 2019-09-11 stsp !(view->focussed &&
4856 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4857 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4858 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4859 8d0fe45a 2019-08-12 stsp char *id_str;
4860 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4861 87411fa9 2022-06-16 stsp blame_line->id);
4862 8d0fe45a 2019-08-12 stsp if (err) {
4863 8d0fe45a 2019-08-12 stsp free(line);
4864 8d0fe45a 2019-08-12 stsp return err;
4866 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4868 11b20872 2019-11-08 stsp wattr_on(view->window,
4869 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4870 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4872 11b20872 2019-11-08 stsp wattr_off(view->window,
4873 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4874 8d0fe45a 2019-08-12 stsp free(id_str);
4875 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4877 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4878 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4881 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4882 ee41ec32 2018-07-10 stsp prev_id = NULL;
4885 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4886 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4887 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4889 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4890 41605754 2020-11-12 stsp width = 9;
4891 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4892 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4893 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4894 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4895 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4896 41605754 2020-11-12 stsp if (err) {
4897 41605754 2020-11-12 stsp free(line);
4898 41605754 2020-11-12 stsp return err;
4900 41605754 2020-11-12 stsp width += 9;
4903 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4904 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4905 4cb9d869 2022-06-16 stsp if (err) {
4906 4cb9d869 2022-06-16 stsp free(line);
4907 4cb9d869 2022-06-16 stsp return err;
4909 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4910 a75b3e2d 2022-06-16 stsp width += 9;
4911 41605754 2020-11-12 stsp free(wline);
4912 41605754 2020-11-12 stsp wline = NULL;
4915 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4916 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4917 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4918 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4920 826082fe 2020-12-10 stsp free(line);
4921 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4923 9b058f45 2022-06-30 mark view_border(view);
4925 84451b3e 2018-07-10 stsp return NULL;
4928 84451b3e 2018-07-10 stsp static const struct got_error *
4929 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4930 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4932 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4933 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4934 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4935 1a76625f 2018-10-22 stsp int errcode;
4937 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4938 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4939 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4941 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4942 1a76625f 2018-10-22 stsp if (errcode)
4943 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4945 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4946 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4947 d68a0a7d 2018-07-10 stsp goto done;
4950 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4951 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4953 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4954 d68a0a7d 2018-07-10 stsp if (line->annotated)
4955 d68a0a7d 2018-07-10 stsp goto done;
4957 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4958 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4959 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4960 84451b3e 2018-07-10 stsp goto done;
4962 84451b3e 2018-07-10 stsp line->annotated = 1;
4964 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4965 1a76625f 2018-10-22 stsp if (errcode)
4966 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4967 84451b3e 2018-07-10 stsp return err;
4970 84451b3e 2018-07-10 stsp static void *
4971 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4973 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4974 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4975 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4976 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
4977 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
4979 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
4980 e6e73e55 2022-06-30 tracey if (fd1 == -1)
4981 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
4983 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
4984 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
4985 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
4986 e6e73e55 2022-06-30 tracey goto done;
4989 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
4990 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
4991 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4992 e6e73e55 2022-06-30 tracey goto done;
4994 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
4995 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
4996 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4997 e6e73e55 2022-06-30 tracey goto done;
5000 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5002 e6e73e55 2022-06-30 tracey goto done;
5004 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5005 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5006 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5007 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5008 fc06ba56 2019-08-22 stsp err = NULL;
5010 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5011 e6e73e55 2022-06-30 tracey if (errcode) {
5012 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5013 e6e73e55 2022-06-30 tracey goto done;
5016 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5017 1d0f4054 2021-06-17 stsp if (err == NULL)
5018 1d0f4054 2021-06-17 stsp err = close_err;
5019 c9beca56 2018-07-22 stsp ta->repo = NULL;
5020 c9beca56 2018-07-22 stsp *ta->complete = 1;
5022 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5023 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5024 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5027 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5028 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5029 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5030 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5031 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5032 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5033 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5034 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5036 18430de3 2018-07-10 stsp return (void *)err;
5039 245d91c1 2018-07-12 stsp static struct got_object_id *
5040 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5041 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5043 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5045 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5046 8d0fe45a 2019-08-12 stsp return NULL;
5048 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5049 245d91c1 2018-07-12 stsp if (!line->annotated)
5050 245d91c1 2018-07-12 stsp return NULL;
5052 245d91c1 2018-07-12 stsp return line->id;
5055 b880a918 2018-07-10 stsp static const struct got_error *
5056 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5058 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5061 245d91c1 2018-07-12 stsp if (blame->thread) {
5062 1a76625f 2018-10-22 stsp int errcode;
5063 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5064 1a76625f 2018-10-22 stsp if (errcode)
5065 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5066 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5067 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5068 1a76625f 2018-10-22 stsp if (errcode)
5069 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5070 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5071 1a76625f 2018-10-22 stsp if (errcode)
5072 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5073 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5074 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5075 245d91c1 2018-07-12 stsp err = NULL;
5076 245d91c1 2018-07-12 stsp blame->thread = NULL;
5078 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5079 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5080 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5081 1d0f4054 2021-06-17 stsp if (err == NULL)
5082 1d0f4054 2021-06-17 stsp err = close_err;
5083 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5085 245d91c1 2018-07-12 stsp if (blame->f) {
5086 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5087 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5088 245d91c1 2018-07-12 stsp blame->f = NULL;
5090 57670559 2018-12-23 stsp if (blame->lines) {
5091 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5092 57670559 2018-12-23 stsp free(blame->lines[i].id);
5093 57670559 2018-12-23 stsp free(blame->lines);
5094 57670559 2018-12-23 stsp blame->lines = NULL;
5096 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5097 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5098 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5099 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5100 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5101 0ae84acc 2022-06-15 tracey if (err == NULL)
5102 0ae84acc 2022-06-15 tracey err = pack_err;
5103 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5105 245d91c1 2018-07-12 stsp return err;
5108 245d91c1 2018-07-12 stsp static const struct got_error *
5109 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5111 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5112 fc06ba56 2019-08-22 stsp int *done = arg;
5113 fc06ba56 2019-08-22 stsp int errcode;
5115 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5116 fc06ba56 2019-08-22 stsp if (errcode)
5117 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5118 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5120 fc06ba56 2019-08-22 stsp if (*done)
5121 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5123 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5124 fc06ba56 2019-08-22 stsp if (errcode)
5125 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5126 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5128 fc06ba56 2019-08-22 stsp return err;
5131 fc06ba56 2019-08-22 stsp static const struct got_error *
5132 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5134 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5135 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5136 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5137 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5138 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5139 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5140 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5141 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5142 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5144 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5145 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5147 15a94983 2018-12-23 stsp return err;
5149 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5150 eb81bc23 2022-06-28 tracey if (fd == -1) {
5151 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5152 eb81bc23 2022-06-28 tracey goto done;
5155 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5157 a44927cc 2022-04-07 stsp goto done;
5159 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5161 84451b3e 2018-07-10 stsp goto done;
5163 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5164 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5165 84451b3e 2018-07-10 stsp goto done;
5168 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5170 a70480e0 2018-06-23 stsp goto done;
5171 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5172 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5173 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5174 84451b3e 2018-07-10 stsp goto done;
5176 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5177 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5179 1fddf795 2021-01-20 stsp goto done;
5180 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5181 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5182 84451b3e 2018-07-10 stsp goto done;
5185 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5186 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5187 b02560ec 2019-08-19 stsp blame->nlines--;
5189 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5190 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5191 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5192 84451b3e 2018-07-10 stsp goto done;
5195 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5197 bd24772e 2018-07-11 stsp goto done;
5198 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5199 0ae84acc 2022-06-15 tracey pack_fds);
5200 0ae84acc 2022-06-15 tracey if (err)
5201 0ae84acc 2022-06-15 tracey goto done;
5203 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5204 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5205 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5206 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5207 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5208 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5209 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5210 245d91c1 2018-07-12 stsp goto done;
5212 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5214 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5215 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5216 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5217 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5218 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5219 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5220 a5388363 2020-12-01 naddy s->blame_complete = 0;
5222 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5223 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5224 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5225 f5a09613 2020-12-13 naddy s->selected_line = 1;
5227 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5230 a44927cc 2022-04-07 stsp if (commit)
5231 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5232 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5233 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5235 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5236 27d434c2 2018-09-15 stsp free(obj_id);
5238 245d91c1 2018-07-12 stsp stop_blame(blame);
5239 245d91c1 2018-07-12 stsp return err;
5242 245d91c1 2018-07-12 stsp static const struct got_error *
5243 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5244 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5246 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5247 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5249 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5251 c4843652 2019-08-12 stsp s->path = strdup(path);
5252 c4843652 2019-08-12 stsp if (s->path == NULL)
5253 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5255 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5256 c4843652 2019-08-12 stsp if (err) {
5257 c4843652 2019-08-12 stsp free(s->path);
5258 7cbe629d 2018-08-04 stsp return err;
5261 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5262 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5263 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5264 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5265 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5266 fb2756b9 2018-08-04 stsp s->repo = repo;
5267 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5268 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5270 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5271 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5272 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5273 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5275 11b20872 2019-11-08 stsp return err;
5278 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5279 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5280 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5281 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5282 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5283 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5285 a5388363 2020-12-01 naddy return run_blame(view);
5288 e5a0f69f 2018-08-18 stsp static const struct got_error *
5289 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5291 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5292 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5294 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5295 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5297 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5298 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5299 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5300 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5301 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5304 e5a0f69f 2018-08-18 stsp free(s->path);
5305 11b20872 2019-11-08 stsp free_colors(&s->colors);
5306 e5a0f69f 2018-08-18 stsp return err;
5309 7cbe629d 2018-08-04 stsp static const struct got_error *
5310 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5312 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5314 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5315 6c4c42e0 2019-06-24 stsp return NULL;
5318 6c4c42e0 2019-06-24 stsp static const struct got_error *
5319 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5321 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5322 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5323 6c4c42e0 2019-06-24 stsp int lineno;
5324 cb713507 2022-06-16 stsp char *line = NULL;
5325 826082fe 2020-12-10 stsp size_t linesize = 0;
5326 826082fe 2020-12-10 stsp ssize_t linelen;
5328 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5329 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5330 6c4c42e0 2019-06-24 stsp return NULL;
5333 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5334 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5335 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5337 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5339 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5341 6c4c42e0 2019-06-24 stsp while (1) {
5342 6c4c42e0 2019-06-24 stsp off_t offset;
5344 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5345 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5346 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5350 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5351 6c4c42e0 2019-06-24 stsp lineno = 1;
5353 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5356 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5357 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5358 6c4c42e0 2019-06-24 stsp free(line);
5359 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5361 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5362 cb713507 2022-06-16 stsp if (linelen != -1) {
5363 cb713507 2022-06-16 stsp char *exstr;
5364 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5367 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5368 cb713507 2022-06-16 stsp &view->regmatch)) {
5369 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5370 cb713507 2022-06-16 stsp s->matched_line = lineno;
5371 cb713507 2022-06-16 stsp free(exstr);
5374 cb713507 2022-06-16 stsp free(exstr);
5376 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5381 826082fe 2020-12-10 stsp free(line);
5383 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5384 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5385 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5388 145b6838 2022-06-16 stsp return err;
5391 6c4c42e0 2019-06-24 stsp static const struct got_error *
5392 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5394 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5395 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5396 2b380cc8 2018-10-24 stsp int errcode;
5398 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5399 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5400 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5401 2b380cc8 2018-10-24 stsp if (errcode)
5402 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5404 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5407 51fe7530 2019-08-19 stsp if (s->blame_complete)
5408 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5410 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5412 9b058f45 2022-06-30 mark view_border(view);
5413 e5a0f69f 2018-08-18 stsp return err;
5416 e5a0f69f 2018-08-18 stsp static const struct got_error *
5417 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5419 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5420 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5421 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5422 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5424 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5425 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5426 9b058f45 2022-06-30 mark --eos; /* border */
5428 e5a0f69f 2018-08-18 stsp switch (ch) {
5430 145b6838 2022-06-16 stsp view->x = 0;
5433 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5434 640cd7ff 2022-06-22 mark view->count = 0;
5436 145b6838 2022-06-16 stsp case KEY_RIGHT:
5438 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5439 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5441 640cd7ff 2022-06-22 mark view->count = 0;
5443 145b6838 2022-06-16 stsp case KEY_LEFT:
5445 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5446 640cd7ff 2022-06-22 mark if (view->x <= 0)
5447 640cd7ff 2022-06-22 mark view->count = 0;
5450 1e37a5c2 2019-05-12 jcs s->done = 1;
5452 4deef56f 2021-09-02 naddy case 'g':
5453 4deef56f 2021-09-02 naddy case KEY_HOME:
5454 4deef56f 2021-09-02 naddy s->selected_line = 1;
5455 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5456 640cd7ff 2022-06-22 mark view->count = 0;
5458 4deef56f 2021-09-02 naddy case 'G':
5459 4deef56f 2021-09-02 naddy case KEY_END:
5460 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5461 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5462 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5464 9b058f45 2022-06-30 mark s->selected_line = eos;
5465 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5467 640cd7ff 2022-06-22 mark view->count = 0;
5470 1e37a5c2 2019-05-12 jcs case KEY_UP:
5471 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5472 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5473 1e37a5c2 2019-05-12 jcs s->selected_line--;
5474 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5475 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5476 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5478 640cd7ff 2022-06-22 mark view->count = 0;
5480 83cc4199 2022-06-13 stsp case CTRL('u'):
5482 83cc4199 2022-06-13 stsp nscroll /= 2;
5483 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5484 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5485 ea025d1d 2020-02-22 naddy case CTRL('b'):
5487 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5488 640cd7ff 2022-06-22 mark if (view->count > 1)
5489 640cd7ff 2022-06-22 mark nscroll += nscroll;
5490 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5491 640cd7ff 2022-06-22 mark view->count = 0;
5494 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5495 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5497 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5500 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5501 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5502 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5503 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5504 1e37a5c2 2019-05-12 jcs s->selected_line++;
5505 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5506 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5508 640cd7ff 2022-06-22 mark view->count = 0;
5511 1e37a5c2 2019-05-12 jcs case 'p': {
5512 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5514 640cd7ff 2022-06-22 mark view->count = 0;
5515 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5516 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5517 1e37a5c2 2019-05-12 jcs if (id == NULL)
5519 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5520 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5521 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5522 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5523 1e37a5c2 2019-05-12 jcs int obj_type;
5524 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5525 1e37a5c2 2019-05-12 jcs s->repo, id);
5528 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5529 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5530 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5531 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5534 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5535 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5536 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5539 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5540 a44927cc 2022-04-07 stsp pcommit, s->path);
5541 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5542 e5a0f69f 2018-08-18 stsp if (err) {
5543 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5544 1e37a5c2 2019-05-12 jcs err = NULL;
5545 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5548 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5550 1e37a5c2 2019-05-12 jcs free(blob_id);
5551 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5552 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5553 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5556 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5557 d7b5a0e8 2022-04-20 stsp &pid->id);
5558 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5560 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5561 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5563 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5568 1e37a5c2 2019-05-12 jcs s->done = 1;
5569 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5570 1e37a5c2 2019-05-12 jcs s->done = 0;
5571 1e37a5c2 2019-05-12 jcs if (thread_err)
5573 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5574 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5575 a5388363 2020-12-01 naddy err = run_blame(view);
5580 61417565 2022-06-20 mark case 'C': {
5581 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5583 640cd7ff 2022-06-22 mark view->count = 0;
5584 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5585 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5587 1e37a5c2 2019-05-12 jcs s->done = 1;
5588 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5589 1e37a5c2 2019-05-12 jcs s->done = 0;
5590 1e37a5c2 2019-05-12 jcs if (thread_err)
5592 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5593 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5594 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5595 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5596 a5388363 2020-12-01 naddy err = run_blame(view);
5601 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5602 1e37a5c2 2019-05-12 jcs case '\r': {
5603 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5604 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5605 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5607 640cd7ff 2022-06-22 mark view->count = 0;
5608 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5609 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5610 1e37a5c2 2019-05-12 jcs if (id == NULL)
5612 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5615 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5616 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5617 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
5619 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5620 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5621 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5622 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5625 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5626 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5627 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5629 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5632 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
5633 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5634 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5639 e78dc838 2020-12-04 stsp view->focussed = 0;
5640 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5641 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5642 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5643 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5644 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5647 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5650 e78dc838 2020-12-04 stsp view->focus_child = 1;
5652 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5657 83cc4199 2022-06-13 stsp case CTRL('d'):
5659 83cc4199 2022-06-13 stsp nscroll /= 2;
5660 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5661 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5662 ea025d1d 2020-02-22 naddy case CTRL('f'):
5665 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5666 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5667 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5668 640cd7ff 2022-06-22 mark view->count = 0;
5671 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5672 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5673 83cc4199 2022-06-13 stsp s->selected_line +=
5674 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5675 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5677 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5678 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5680 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5681 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5683 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5684 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5685 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5686 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5690 640cd7ff 2022-06-22 mark view->count = 0;
5693 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5696 917d79a7 2022-07-01 stsp static const struct got_error *
5697 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
5699 917d79a7 2022-07-01 stsp const struct got_error *err;
5700 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
5702 917d79a7 2022-07-01 stsp view->count = 0;
5703 917d79a7 2022-07-01 stsp s->done = 1;
5704 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
5705 917d79a7 2022-07-01 stsp s->done = 0;
5707 917d79a7 2022-07-01 stsp return err;
5708 917d79a7 2022-07-01 stsp return run_blame(view);
5711 a70480e0 2018-06-23 stsp static const struct got_error *
5712 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5714 a70480e0 2018-06-23 stsp const struct got_error *error;
5715 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5716 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5717 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5718 0587e10c 2020-07-23 stsp char *link_target = NULL;
5719 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5720 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5721 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5723 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5724 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5726 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5727 a70480e0 2018-06-23 stsp switch (ch) {
5729 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5732 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5733 69069811 2018-08-02 stsp if (repo_path == NULL)
5734 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5738 17020d27 2019-03-07 stsp usage_blame();
5739 a70480e0 2018-06-23 stsp /* NOTREACHED */
5743 a70480e0 2018-06-23 stsp argc -= optind;
5744 a70480e0 2018-06-23 stsp argv += optind;
5746 f135c941 2020-02-20 stsp if (argc != 1)
5747 a70480e0 2018-06-23 stsp usage_blame();
5749 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5750 0ae84acc 2022-06-15 tracey if (error != NULL)
5751 0ae84acc 2022-06-15 tracey goto done;
5753 69069811 2018-08-02 stsp if (repo_path == NULL) {
5754 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5755 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5756 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5757 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5758 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5759 c156c7a4 2020-12-18 stsp goto done;
5760 f135c941 2020-02-20 stsp if (worktree)
5761 eb41ed75 2019-02-05 stsp repo_path =
5762 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5764 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5765 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5766 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5767 c156c7a4 2020-12-18 stsp goto done;
5771 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5772 c02c541e 2019-03-29 stsp if (error != NULL)
5773 8e94dd5b 2019-01-04 stsp goto done;
5775 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5776 f135c941 2020-02-20 stsp worktree);
5777 c02c541e 2019-03-29 stsp if (error)
5778 92205607 2019-01-04 stsp goto done;
5780 f135c941 2020-02-20 stsp init_curses();
5782 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5783 51a10b52 2020-12-26 stsp if (error)
5784 51a10b52 2020-12-26 stsp goto done;
5786 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5787 eb41ed75 2019-02-05 stsp if (error)
5788 69069811 2018-08-02 stsp goto done;
5790 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5791 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5792 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5793 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5794 a70480e0 2018-06-23 stsp if (error != NULL)
5795 66b4983c 2018-06-23 stsp goto done;
5796 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5797 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5799 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5800 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5802 a19e88aa 2018-06-23 stsp if (error != NULL)
5803 8b473291 2019-02-21 stsp goto done;
5805 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5806 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5807 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5808 e1cd8fed 2018-08-01 stsp goto done;
5811 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5812 a44927cc 2022-04-07 stsp if (error)
5813 a44927cc 2022-04-07 stsp goto done;
5815 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5816 a44927cc 2022-04-07 stsp commit, repo);
5817 7cbe629d 2018-08-04 stsp if (error)
5818 7cbe629d 2018-08-04 stsp goto done;
5820 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5821 78756c87 2020-11-24 stsp commit_id, repo);
5822 0587e10c 2020-07-23 stsp if (error)
5823 0587e10c 2020-07-23 stsp goto done;
5824 12314ad4 2019-08-31 stsp if (worktree) {
5825 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5826 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5827 12314ad4 2019-08-31 stsp worktree = NULL;
5829 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5831 69069811 2018-08-02 stsp free(repo_path);
5832 f135c941 2020-02-20 stsp free(in_repo_path);
5833 0587e10c 2020-07-23 stsp free(link_target);
5834 69069811 2018-08-02 stsp free(cwd);
5835 a70480e0 2018-06-23 stsp free(commit_id);
5836 a44927cc 2022-04-07 stsp if (commit)
5837 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5838 eb41ed75 2019-02-05 stsp if (worktree)
5839 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5840 1d0f4054 2021-06-17 stsp if (repo) {
5841 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5842 1d0f4054 2021-06-17 stsp if (error == NULL)
5843 1d0f4054 2021-06-17 stsp error = close_err;
5845 0ae84acc 2022-06-15 tracey if (pack_fds) {
5846 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5847 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5848 0ae84acc 2022-06-15 tracey if (error == NULL)
5849 0ae84acc 2022-06-15 tracey error = pack_err;
5851 51a10b52 2020-12-26 stsp tog_free_refs();
5852 a70480e0 2018-06-23 stsp return error;
5855 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5856 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5858 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5859 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5860 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5861 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5862 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5863 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5864 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5866 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5867 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5868 9b058f45 2022-06-30 mark --limit; /* border */
5870 f7d12f7e 2018-08-01 stsp werase(view->window);
5872 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5873 ffd1d5e5 2018-06-23 stsp return NULL;
5875 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5878 ffd1d5e5 2018-06-23 stsp return err;
5879 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5880 a3404814 2018-09-02 stsp wstandout(view->window);
5881 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5883 11b20872 2019-11-08 stsp wattr_on(view->window,
5884 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5885 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5887 11b20872 2019-11-08 stsp wattr_off(view->window,
5888 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5889 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5890 a3404814 2018-09-02 stsp wstandend(view->window);
5891 2550e4c3 2018-07-13 stsp free(wline);
5892 2550e4c3 2018-07-13 stsp wline = NULL;
5893 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5894 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5895 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5896 ffd1d5e5 2018-06-23 stsp return NULL;
5897 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5900 ce52c690 2018-06-23 stsp return err;
5901 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5902 2550e4c3 2018-07-13 stsp free(wline);
5903 2550e4c3 2018-07-13 stsp wline = NULL;
5904 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5905 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5906 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5907 ffd1d5e5 2018-06-23 stsp return NULL;
5908 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5909 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5910 a1eca9bb 2018-06-23 stsp return NULL;
5912 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5913 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5914 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5915 0cf4efb1 2018-09-29 stsp if (view->focussed)
5916 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5917 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5919 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5920 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5921 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5922 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5923 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5924 ffd1d5e5 2018-06-23 stsp return NULL;
5928 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5931 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5932 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5933 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5934 848d6979 2019-08-12 stsp const char *modestr = "";
5935 56e0773d 2019-11-28 stsp mode_t mode;
5937 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5938 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5940 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5941 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5942 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5944 638f9024 2019-05-13 stsp return got_error_from_errno(
5945 230a42bd 2019-05-11 jcs "got_object_id_str");
5947 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5948 63c5ca5d 2019-08-24 stsp modestr = "$";
5949 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5952 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5953 d86d3b18 2020-12-01 naddy te, s->repo);
5954 0d6c6ee3 2020-05-20 stsp if (err) {
5955 0d6c6ee3 2020-05-20 stsp free(id_str);
5956 0d6c6ee3 2020-05-20 stsp return err;
5958 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5959 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5960 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5962 848d6979 2019-08-12 stsp modestr = "@";
5964 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5965 848d6979 2019-08-12 stsp modestr = "/";
5966 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5967 848d6979 2019-08-12 stsp modestr = "*";
5968 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5969 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5970 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5971 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5972 1d13200f 2018-07-12 stsp free(id_str);
5973 0d6c6ee3 2020-05-20 stsp free(link_target);
5974 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5976 1d13200f 2018-07-12 stsp free(id_str);
5977 0d6c6ee3 2020-05-20 stsp free(link_target);
5978 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5980 ffd1d5e5 2018-06-23 stsp if (err) {
5981 ffd1d5e5 2018-06-23 stsp free(line);
5984 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5985 0cf4efb1 2018-09-29 stsp if (view->focussed)
5986 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5987 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5989 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5991 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5992 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5993 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5995 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5996 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5997 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5998 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5999 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6000 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6001 ffd1d5e5 2018-06-23 stsp free(line);
6002 2550e4c3 2018-07-13 stsp free(wline);
6003 2550e4c3 2018-07-13 stsp wline = NULL;
6005 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6006 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6007 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6011 ffd1d5e5 2018-06-23 stsp return err;
6014 ffd1d5e5 2018-06-23 stsp static void
6015 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6017 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6018 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6019 fa86c4bf 2020-11-29 stsp int i = 0;
6021 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6024 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6025 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6026 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6027 fa86c4bf 2020-11-29 stsp if (!isroot)
6028 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6031 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6032 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6036 9b058f45 2022-06-30 mark static const struct got_error *
6037 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6039 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6040 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6041 ffd1d5e5 2018-06-23 stsp int n = 0;
6043 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6044 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6045 694d3271 2020-12-01 naddy s->first_displayed_entry);
6047 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6049 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6050 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6052 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6053 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6054 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6055 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6059 9b058f45 2022-06-30 mark return NULL;
6062 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6063 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6064 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6066 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6067 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6068 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6070 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6071 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6072 56e0773d 2019-11-28 stsp + 1 /* slash */;
6074 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6076 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6077 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6078 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6080 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6081 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6082 d9765a41 2018-06-23 stsp while (pt) {
6083 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6084 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6085 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6086 cb2ebc8a 2018-06-23 stsp goto done;
6088 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6089 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6090 cb2ebc8a 2018-06-23 stsp goto done;
6092 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6095 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6096 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6097 ce52c690 2018-06-23 stsp goto done;
6101 ce52c690 2018-06-23 stsp if (err) {
6102 ce52c690 2018-06-23 stsp free(*path);
6103 ce52c690 2018-06-23 stsp *path = NULL;
6105 ce52c690 2018-06-23 stsp return err;
6108 ce52c690 2018-06-23 stsp static const struct got_error *
6109 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6110 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6111 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6113 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6114 ce52c690 2018-06-23 stsp char *path;
6115 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6117 a0de39f3 2019-08-09 stsp *new_view = NULL;
6119 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6121 ce52c690 2018-06-23 stsp return err;
6123 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6124 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6125 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6126 83ce39e3 2019-08-12 stsp goto done;
6129 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6130 e5a0f69f 2018-08-18 stsp if (err) {
6131 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6132 fc06ba56 2019-08-22 stsp err = NULL;
6133 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6135 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6137 83ce39e3 2019-08-12 stsp free(path);
6138 69efd4c4 2018-07-18 stsp return err;
6141 69efd4c4 2018-07-18 stsp static const struct got_error *
6142 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6143 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6145 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6146 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6147 69efd4c4 2018-07-18 stsp char *path;
6149 a0de39f3 2019-08-09 stsp *new_view = NULL;
6151 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6152 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6153 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6155 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6157 69efd4c4 2018-07-18 stsp return err;
6159 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6162 e5a0f69f 2018-08-18 stsp view_close(log_view);
6164 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6165 cb2ebc8a 2018-06-23 stsp free(path);
6166 cb2ebc8a 2018-06-23 stsp return err;
6169 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6170 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6171 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6173 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6174 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6175 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6176 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6178 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6179 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6181 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6182 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6183 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6185 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6187 bc573f3b 2021-07-10 stsp goto done;
6190 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6191 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6192 bc573f3b 2021-07-10 stsp * closed on demand.
6194 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6195 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6197 bc573f3b 2021-07-10 stsp goto done;
6198 bc573f3b 2021-07-10 stsp s->tree = s->root;
6200 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6201 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6202 ffd1d5e5 2018-06-23 stsp goto done;
6204 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6205 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6206 ffd1d5e5 2018-06-23 stsp goto done;
6209 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6210 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6211 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6212 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6213 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6214 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6215 9cd7cbd1 2020-12-07 stsp goto done;
6218 fb2756b9 2018-08-04 stsp s->repo = repo;
6220 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6221 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6222 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6223 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6225 c0b01bdb 2019-11-08 stsp goto done;
6226 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6227 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6229 c0b01bdb 2019-11-08 stsp goto done;
6230 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6231 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6232 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6234 c0b01bdb 2019-11-08 stsp goto done;
6236 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6237 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6238 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6240 11b20872 2019-11-08 stsp goto done;
6242 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6243 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6245 c0b01bdb 2019-11-08 stsp goto done;
6248 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6249 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6250 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6251 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6252 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6254 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6255 bc573f3b 2021-07-10 stsp if (commit)
6256 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6258 bc573f3b 2021-07-10 stsp close_tree_view(view);
6259 ad80ab7b 2018-08-04 stsp return err;
6262 e5a0f69f 2018-08-18 stsp static const struct got_error *
6263 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6265 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6267 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6268 fb2756b9 2018-08-04 stsp free(s->tree_label);
6269 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6270 6484ec90 2018-09-29 stsp free(s->commit_id);
6271 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6272 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6273 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6274 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6275 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6276 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6277 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6278 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6279 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6280 ad80ab7b 2018-08-04 stsp free(parent);
6283 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6284 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6285 bc573f3b 2021-07-10 stsp if (s->root)
6286 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6287 7c32bd05 2019-06-22 stsp return NULL;
6290 7c32bd05 2019-06-22 stsp static const struct got_error *
6291 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6293 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6295 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6296 7c32bd05 2019-06-22 stsp return NULL;
6299 7c32bd05 2019-06-22 stsp static int
6300 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6302 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6304 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6308 7c32bd05 2019-06-22 stsp static const struct got_error *
6309 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6311 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6312 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6314 7c32bd05 2019-06-22 stsp if (!view->searching) {
6315 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6316 7c32bd05 2019-06-22 stsp return NULL;
6319 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6320 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6321 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6322 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6323 56e0773d 2019-11-28 stsp s->selected_entry);
6325 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6327 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6328 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6330 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6331 56e0773d 2019-11-28 stsp s->selected_entry);
6334 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6335 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6336 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6337 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6339 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6342 7c32bd05 2019-06-22 stsp while (1) {
6343 56e0773d 2019-11-28 stsp if (te == NULL) {
6344 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6345 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6346 ac66afb8 2019-06-24 stsp return NULL;
6348 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6349 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6351 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6354 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6355 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6356 56e0773d 2019-11-28 stsp s->matched_entry = te;
6360 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6361 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6363 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6366 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6367 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6368 7c32bd05 2019-06-22 stsp s->selected = 0;
6371 e5a0f69f 2018-08-18 stsp return NULL;
6374 ad80ab7b 2018-08-04 stsp static const struct got_error *
6375 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6377 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6378 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6379 e5a0f69f 2018-08-18 stsp char *parent_path;
6381 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6383 e5a0f69f 2018-08-18 stsp return err;
6385 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6386 e5a0f69f 2018-08-18 stsp free(parent_path);
6388 9b058f45 2022-06-30 mark view_border(view);
6389 e5a0f69f 2018-08-18 stsp return err;
6392 e5a0f69f 2018-08-18 stsp static const struct got_error *
6393 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6395 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6396 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6397 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6398 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6399 49b24ee5 2022-07-03 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6401 e5a0f69f 2018-08-18 stsp switch (ch) {
6403 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6404 640cd7ff 2022-06-22 mark view->count = 0;
6407 640cd7ff 2022-06-22 mark view->count = 0;
6408 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6410 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6411 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6412 49b24ee5 2022-07-03 mark err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6413 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6414 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6415 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6419 e78dc838 2020-12-04 stsp view->focussed = 0;
6420 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6421 49b24ee5 2022-07-03 mark log_view->mode = view->mode;
6422 49b24ee5 2022-07-03 mark log_view->nlines = view->lines - begin_y;
6423 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6424 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6426 1e37a5c2 2019-05-12 jcs return err;
6427 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6430 e78dc838 2020-12-04 stsp view->focus_child = 1;
6432 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6435 640cd7ff 2022-06-22 mark view->count = 0;
6436 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6437 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6438 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6439 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6440 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6441 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6442 152c1c93 2020-11-29 stsp if (err) {
6443 152c1c93 2020-11-29 stsp view_close(ref_view);
6444 152c1c93 2020-11-29 stsp return err;
6446 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6447 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6448 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6452 e78dc838 2020-12-04 stsp view->focussed = 0;
6453 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6454 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
6455 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
6456 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6457 152c1c93 2020-11-29 stsp err = view_close_child(view);
6459 152c1c93 2020-11-29 stsp return err;
6460 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6463 e78dc838 2020-12-04 stsp view->focus_child = 1;
6465 152c1c93 2020-11-29 stsp *new_view = ref_view;
6467 e4526bf5 2021-09-03 naddy case 'g':
6468 e4526bf5 2021-09-03 naddy case KEY_HOME:
6469 e4526bf5 2021-09-03 naddy s->selected = 0;
6470 640cd7ff 2022-06-22 mark view->count = 0;
6471 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6472 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6473 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6475 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6477 e4526bf5 2021-09-03 naddy case 'G':
6478 9b058f45 2022-06-30 mark case KEY_END: {
6479 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6481 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6482 9b058f45 2022-06-30 mark --eos; /* border */
6483 e4526bf5 2021-09-03 naddy s->selected = 0;
6484 640cd7ff 2022-06-22 mark view->count = 0;
6485 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6486 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6487 e4526bf5 2021-09-03 naddy if (te == NULL) {
6488 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6489 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6494 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6495 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6497 e4526bf5 2021-09-03 naddy if (n > 0)
6498 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6502 1e37a5c2 2019-05-12 jcs case KEY_UP:
6503 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6504 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6505 1e37a5c2 2019-05-12 jcs s->selected--;
6508 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6509 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6510 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6511 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6512 640cd7ff 2022-06-22 mark view->count = 0;
6514 83cc4199 2022-06-13 stsp case CTRL('u'):
6516 83cc4199 2022-06-13 stsp nscroll /= 2;
6517 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6518 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6519 ea025d1d 2020-02-22 naddy case CTRL('b'):
6521 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6522 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6523 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6524 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6526 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6527 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6529 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6530 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6531 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6532 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6533 640cd7ff 2022-06-22 mark view->count = 0;
6536 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6537 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6538 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6539 1e37a5c2 2019-05-12 jcs s->selected++;
6542 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6543 640cd7ff 2022-06-22 mark == NULL) {
6544 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6545 640cd7ff 2022-06-22 mark view->count = 0;
6548 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6550 83cc4199 2022-06-13 stsp case CTRL('d'):
6552 83cc4199 2022-06-13 stsp nscroll /= 2;
6553 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6554 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6555 ea025d1d 2020-02-22 naddy case CTRL('f'):
6557 48bb96f0 2022-06-20 naddy case ' ':
6558 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6560 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6561 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6562 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6563 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6565 640cd7ff 2022-06-22 mark view->count = 0;
6568 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6570 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6572 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6573 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6574 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6575 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6576 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6577 640cd7ff 2022-06-22 mark view->count = 0;
6580 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6581 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6583 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6584 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6585 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6586 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6587 1e37a5c2 2019-05-12 jcs s->selected_entry =
6588 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6589 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6590 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6591 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6595 1e37a5c2 2019-05-12 jcs free(parent);
6596 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6597 56e0773d 2019-11-28 stsp s->selected_entry))) {
6598 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6599 640cd7ff 2022-06-22 mark view->count = 0;
6600 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6601 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6604 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6605 941e9f74 2019-05-21 stsp if (err) {
6606 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6609 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6610 56e0773d 2019-11-28 stsp s->selected_entry))) {
6611 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6612 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6614 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6615 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6617 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6618 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6619 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6623 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6624 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6625 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6630 640cd7ff 2022-06-22 mark view->count = 0;
6631 e78dc838 2020-12-04 stsp view->focussed = 0;
6632 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6633 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6634 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6635 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6636 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6638 669b5ffa 2018-10-07 stsp return err;
6639 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6642 e78dc838 2020-12-04 stsp view->focus_child = 1;
6644 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6647 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6648 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6649 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6650 640cd7ff 2022-06-22 mark view->count = 0;
6653 640cd7ff 2022-06-22 mark view->count = 0;
6657 ffd1d5e5 2018-06-23 stsp return err;
6660 ffd1d5e5 2018-06-23 stsp __dead static void
6661 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6664 87411fa9 2022-06-16 stsp fprintf(stderr,
6665 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6666 ffd1d5e5 2018-06-23 stsp getprogname());
6670 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6671 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6673 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6674 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6675 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6676 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6677 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6678 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6679 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6680 4e97c21c 2020-12-06 stsp char *label = NULL;
6681 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6682 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6684 5221c383 2018-08-01 stsp struct tog_view *view;
6685 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6687 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6688 ffd1d5e5 2018-06-23 stsp switch (ch) {
6690 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6693 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6694 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6695 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6699 e99e2d15 2020-11-24 naddy usage_tree();
6700 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6704 ffd1d5e5 2018-06-23 stsp argc -= optind;
6705 ffd1d5e5 2018-06-23 stsp argv += optind;
6707 55cccc34 2020-02-20 stsp if (argc > 1)
6708 e99e2d15 2020-11-24 naddy usage_tree();
6710 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6711 0ae84acc 2022-06-15 tracey if (error != NULL)
6712 0ae84acc 2022-06-15 tracey goto done;
6714 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6715 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6716 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6717 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6718 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6719 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6720 c156c7a4 2020-12-18 stsp goto done;
6721 55cccc34 2020-02-20 stsp if (worktree)
6722 52185f70 2019-02-05 stsp repo_path =
6723 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6725 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6726 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6727 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6728 c156c7a4 2020-12-18 stsp goto done;
6732 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6733 c02c541e 2019-03-29 stsp if (error != NULL)
6734 52185f70 2019-02-05 stsp goto done;
6736 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6737 55cccc34 2020-02-20 stsp repo, worktree);
6738 55cccc34 2020-02-20 stsp if (error)
6739 55cccc34 2020-02-20 stsp goto done;
6741 55cccc34 2020-02-20 stsp init_curses();
6743 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6744 c02c541e 2019-03-29 stsp if (error)
6745 52185f70 2019-02-05 stsp goto done;
6747 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6748 51a10b52 2020-12-26 stsp if (error)
6749 51a10b52 2020-12-26 stsp goto done;
6751 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6752 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6753 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6754 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6755 4e97c21c 2020-12-06 stsp if (error)
6756 4e97c21c 2020-12-06 stsp goto done;
6757 4e97c21c 2020-12-06 stsp head_ref_name = label;
6759 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6760 4e97c21c 2020-12-06 stsp if (error == NULL)
6761 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6762 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6763 4e97c21c 2020-12-06 stsp goto done;
6764 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6765 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6766 4e97c21c 2020-12-06 stsp if (error)
6767 4e97c21c 2020-12-06 stsp goto done;
6770 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6771 a44927cc 2022-04-07 stsp if (error)
6772 a44927cc 2022-04-07 stsp goto done;
6774 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6775 5221c383 2018-08-01 stsp if (view == NULL) {
6776 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6777 5221c383 2018-08-01 stsp goto done;
6779 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6780 ad80ab7b 2018-08-04 stsp if (error)
6781 ad80ab7b 2018-08-04 stsp goto done;
6782 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6783 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6784 d91faf3b 2020-12-01 naddy in_repo_path);
6785 55cccc34 2020-02-20 stsp if (error)
6786 55cccc34 2020-02-20 stsp goto done;
6789 55cccc34 2020-02-20 stsp if (worktree) {
6790 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6791 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6792 55cccc34 2020-02-20 stsp worktree = NULL;
6794 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6796 52185f70 2019-02-05 stsp free(repo_path);
6797 e4a0e26d 2020-02-20 stsp free(cwd);
6798 ffd1d5e5 2018-06-23 stsp free(commit_id);
6799 4e97c21c 2020-12-06 stsp free(label);
6801 486cd271 2020-12-06 stsp got_ref_close(ref);
6802 1d0f4054 2021-06-17 stsp if (repo) {
6803 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6804 1d0f4054 2021-06-17 stsp if (error == NULL)
6805 1d0f4054 2021-06-17 stsp error = close_err;
6807 0ae84acc 2022-06-15 tracey if (pack_fds) {
6808 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6809 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6810 0ae84acc 2022-06-15 tracey if (error == NULL)
6811 0ae84acc 2022-06-15 tracey error = pack_err;
6813 51a10b52 2020-12-26 stsp tog_free_refs();
6814 ffd1d5e5 2018-06-23 stsp return error;
6817 6458efa5 2020-11-24 stsp static const struct got_error *
6818 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6820 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6821 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6823 6458efa5 2020-11-24 stsp s->nrefs = 0;
6824 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6825 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6826 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6827 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6828 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6831 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6832 6458efa5 2020-11-24 stsp if (re == NULL)
6833 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6835 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6836 8924d611 2020-12-26 stsp if (re->ref == NULL)
6837 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6838 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6839 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6842 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6843 6458efa5 2020-11-24 stsp return NULL;
6847 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6849 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6851 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6852 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6853 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6854 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6859 6458efa5 2020-11-24 stsp static const struct got_error *
6860 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6862 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6863 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6865 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6866 6458efa5 2020-11-24 stsp s->repo = repo;
6868 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6869 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6871 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6873 6458efa5 2020-11-24 stsp return err;
6875 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6876 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6877 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6878 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6880 6458efa5 2020-11-24 stsp goto done;
6882 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6883 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6884 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6886 6458efa5 2020-11-24 stsp goto done;
6888 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6889 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6890 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6892 cc488aa7 2022-01-23 stsp goto done;
6894 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6895 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6896 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6898 6458efa5 2020-11-24 stsp goto done;
6901 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6902 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6903 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6904 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6905 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6908 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6909 6458efa5 2020-11-24 stsp return err;
6912 6458efa5 2020-11-24 stsp static const struct got_error *
6913 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6915 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6917 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6918 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6920 6458efa5 2020-11-24 stsp return NULL;
6923 6458efa5 2020-11-24 stsp static const struct got_error *
6924 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6925 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6927 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6928 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6929 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6930 6458efa5 2020-11-24 stsp int obj_type;
6932 c42c9805 2020-11-24 stsp *commit_id = NULL;
6934 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6936 6458efa5 2020-11-24 stsp return err;
6938 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6940 6458efa5 2020-11-24 stsp goto done;
6942 6458efa5 2020-11-24 stsp switch (obj_type) {
6943 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6944 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6946 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6947 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6949 6458efa5 2020-11-24 stsp goto done;
6950 c42c9805 2020-11-24 stsp free(obj_id);
6951 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6952 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6954 6458efa5 2020-11-24 stsp goto done;
6955 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6956 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6957 c42c9805 2020-11-24 stsp goto done;
6959 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6960 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6961 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6962 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6963 c42c9805 2020-11-24 stsp goto done;
6967 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6973 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6974 c42c9805 2020-11-24 stsp if (err) {
6975 c42c9805 2020-11-24 stsp free(*commit_id);
6976 c42c9805 2020-11-24 stsp *commit_id = NULL;
6978 c42c9805 2020-11-24 stsp return err;
6981 c42c9805 2020-11-24 stsp static const struct got_error *
6982 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6983 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6985 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6986 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6987 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6989 c42c9805 2020-11-24 stsp *new_view = NULL;
6991 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6992 c42c9805 2020-11-24 stsp if (err) {
6993 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6994 c42c9805 2020-11-24 stsp return err;
6996 c42c9805 2020-11-24 stsp return NULL;
6999 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7000 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7001 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7002 6458efa5 2020-11-24 stsp goto done;
7005 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7006 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7009 6458efa5 2020-11-24 stsp view_close(log_view);
7011 6458efa5 2020-11-24 stsp *new_view = log_view;
7012 c42c9805 2020-11-24 stsp free(commit_id);
7013 6458efa5 2020-11-24 stsp return err;
7016 ce5b7c56 2019-07-09 stsp static void
7017 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7019 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7020 34ba6917 2020-11-30 stsp int i = 0;
7022 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7025 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7026 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7027 34ba6917 2020-11-30 stsp if (re == NULL)
7029 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7030 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7034 9b058f45 2022-06-30 mark static const struct got_error *
7035 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7037 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7038 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7039 6458efa5 2020-11-24 stsp int n = 0;
7041 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7042 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7044 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7046 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7047 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7049 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7050 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7051 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7052 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7056 9b058f45 2022-06-30 mark return NULL;
7059 6458efa5 2020-11-24 stsp static const struct got_error *
7060 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7062 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7064 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7065 6458efa5 2020-11-24 stsp return NULL;
7068 6458efa5 2020-11-24 stsp static int
7069 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7071 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7073 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7077 6458efa5 2020-11-24 stsp static const struct got_error *
7078 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7080 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7081 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7083 6458efa5 2020-11-24 stsp if (!view->searching) {
7084 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7085 6458efa5 2020-11-24 stsp return NULL;
7088 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7089 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7090 6458efa5 2020-11-24 stsp if (s->selected_entry)
7091 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7093 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7094 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7096 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7097 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7099 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7100 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7103 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7104 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7105 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7106 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7108 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7111 6458efa5 2020-11-24 stsp while (1) {
7112 6458efa5 2020-11-24 stsp if (re == NULL) {
7113 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7114 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7115 6458efa5 2020-11-24 stsp return NULL;
7117 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7118 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7120 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7123 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7124 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7125 6458efa5 2020-11-24 stsp s->matched_entry = re;
7129 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7130 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7132 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7135 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7136 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7137 6458efa5 2020-11-24 stsp s->selected = 0;
7140 6458efa5 2020-11-24 stsp return NULL;
7143 6458efa5 2020-11-24 stsp static const struct got_error *
7144 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7146 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7147 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7148 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7149 6458efa5 2020-11-24 stsp char *line = NULL;
7150 6458efa5 2020-11-24 stsp wchar_t *wline;
7151 6458efa5 2020-11-24 stsp struct tog_color *tc;
7152 6458efa5 2020-11-24 stsp int width, n;
7153 6458efa5 2020-11-24 stsp int limit = view->nlines;
7155 6458efa5 2020-11-24 stsp werase(view->window);
7157 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7158 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7159 9b058f45 2022-06-30 mark --limit; /* border */
7161 6458efa5 2020-11-24 stsp if (limit == 0)
7162 6458efa5 2020-11-24 stsp return NULL;
7164 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7166 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7167 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7168 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7170 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7171 6458efa5 2020-11-24 stsp if (err) {
7172 6458efa5 2020-11-24 stsp free(line);
7173 6458efa5 2020-11-24 stsp return err;
7175 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7176 6458efa5 2020-11-24 stsp wstandout(view->window);
7177 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7178 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7179 6458efa5 2020-11-24 stsp wstandend(view->window);
7180 6458efa5 2020-11-24 stsp free(wline);
7181 6458efa5 2020-11-24 stsp wline = NULL;
7182 6458efa5 2020-11-24 stsp free(line);
7183 6458efa5 2020-11-24 stsp line = NULL;
7184 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7185 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7186 6458efa5 2020-11-24 stsp if (--limit <= 0)
7187 6458efa5 2020-11-24 stsp return NULL;
7190 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7191 6458efa5 2020-11-24 stsp char *line = NULL;
7192 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7194 b4996bee 2022-06-16 stsp if (s->show_date) {
7195 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7196 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7197 b4996bee 2022-06-16 stsp struct got_object_id *id;
7198 b4996bee 2022-06-16 stsp struct tm tm;
7201 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7203 b4996bee 2022-06-16 stsp return err;
7204 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7205 b4996bee 2022-06-16 stsp if (err) {
7206 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7208 b4996bee 2022-06-16 stsp return err;
7210 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7212 b4996bee 2022-06-16 stsp if (err) {
7214 b4996bee 2022-06-16 stsp return err;
7216 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7217 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7219 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7220 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7223 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7224 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7225 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7226 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7228 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7229 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7230 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7231 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7232 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7233 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7234 6458efa5 2020-11-24 stsp struct got_object_id *id;
7235 6458efa5 2020-11-24 stsp char *id_str;
7236 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7238 6458efa5 2020-11-24 stsp return err;
7239 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7240 6458efa5 2020-11-24 stsp if (err) {
7242 6458efa5 2020-11-24 stsp return err;
7244 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7245 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7246 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7248 6458efa5 2020-11-24 stsp free(id_str);
7249 6458efa5 2020-11-24 stsp return err;
7252 6458efa5 2020-11-24 stsp free(id_str);
7253 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7254 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7255 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7257 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7259 6458efa5 2020-11-24 stsp if (err) {
7260 6458efa5 2020-11-24 stsp free(line);
7261 6458efa5 2020-11-24 stsp return err;
7263 6458efa5 2020-11-24 stsp if (n == s->selected) {
7264 6458efa5 2020-11-24 stsp if (view->focussed)
7265 6458efa5 2020-11-24 stsp wstandout(view->window);
7266 6458efa5 2020-11-24 stsp s->selected_entry = re;
7268 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7270 6458efa5 2020-11-24 stsp wattr_on(view->window,
7271 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7272 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7274 6458efa5 2020-11-24 stsp wattr_off(view->window,
7275 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7276 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7277 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7278 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7279 6458efa5 2020-11-24 stsp wstandend(view->window);
7280 6458efa5 2020-11-24 stsp free(line);
7281 6458efa5 2020-11-24 stsp free(wline);
7282 6458efa5 2020-11-24 stsp wline = NULL;
7284 6458efa5 2020-11-24 stsp s->ndisplayed++;
7285 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7288 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7291 9b058f45 2022-06-30 mark view_border(view);
7292 6458efa5 2020-11-24 stsp return err;
7295 6458efa5 2020-11-24 stsp static const struct got_error *
7296 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7297 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7299 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7300 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7301 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7303 c42c9805 2020-11-24 stsp *new_view = NULL;
7305 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7306 c42c9805 2020-11-24 stsp if (err) {
7307 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7308 c42c9805 2020-11-24 stsp return err;
7310 c42c9805 2020-11-24 stsp return NULL;
7314 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7315 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7316 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7317 c42c9805 2020-11-24 stsp goto done;
7320 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7321 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7323 c42c9805 2020-11-24 stsp goto done;
7325 c42c9805 2020-11-24 stsp *new_view = tree_view;
7327 c42c9805 2020-11-24 stsp free(commit_id);
7328 c42c9805 2020-11-24 stsp return err;
7330 c42c9805 2020-11-24 stsp static const struct got_error *
7331 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7333 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7334 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7335 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7336 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7337 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7339 6458efa5 2020-11-24 stsp switch (ch) {
7341 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7342 640cd7ff 2022-06-22 mark view->count = 0;
7345 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7346 640cd7ff 2022-06-22 mark view->count = 0;
7349 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7350 640cd7ff 2022-06-22 mark view->count = 0;
7351 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7352 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7353 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7356 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7357 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7358 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7361 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7362 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7364 6458efa5 2020-11-24 stsp case KEY_ENTER:
7365 6458efa5 2020-11-24 stsp case '\r':
7366 640cd7ff 2022-06-22 mark view->count = 0;
7367 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7369 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7370 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7372 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7373 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7377 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7378 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7379 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7384 e78dc838 2020-12-04 stsp view->focussed = 0;
7385 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7386 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7387 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7388 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7389 6458efa5 2020-11-24 stsp err = view_close_child(view);
7391 6458efa5 2020-11-24 stsp return err;
7392 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7395 e78dc838 2020-12-04 stsp view->focus_child = 1;
7397 6458efa5 2020-11-24 stsp *new_view = log_view;
7400 640cd7ff 2022-06-22 mark view->count = 0;
7401 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7403 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7404 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
7405 49b24ee5 2022-07-03 mark err = browse_ref_tree(&tree_view, begin_y, begin_x,
7406 49b24ee5 2022-07-03 mark s->selected_entry, s->repo);
7407 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7409 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7410 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7411 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
7415 e78dc838 2020-12-04 stsp view->focussed = 0;
7416 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7417 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
7418 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
7419 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7420 c42c9805 2020-11-24 stsp err = view_close_child(view);
7422 c42c9805 2020-11-24 stsp return err;
7423 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7426 e78dc838 2020-12-04 stsp view->focus_child = 1;
7428 c42c9805 2020-11-24 stsp *new_view = tree_view;
7430 e4526bf5 2021-09-03 naddy case 'g':
7431 e4526bf5 2021-09-03 naddy case KEY_HOME:
7432 e4526bf5 2021-09-03 naddy s->selected = 0;
7433 640cd7ff 2022-06-22 mark view->count = 0;
7434 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7436 e4526bf5 2021-09-03 naddy case 'G':
7437 9b058f45 2022-06-30 mark case KEY_END: {
7438 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7440 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7441 9b058f45 2022-06-30 mark --eos; /* border */
7442 e4526bf5 2021-09-03 naddy s->selected = 0;
7443 640cd7ff 2022-06-22 mark view->count = 0;
7444 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7445 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7446 e4526bf5 2021-09-03 naddy if (re == NULL)
7448 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7449 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7451 e4526bf5 2021-09-03 naddy if (n > 0)
7452 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7456 6458efa5 2020-11-24 stsp case KEY_UP:
7457 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7458 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7459 6458efa5 2020-11-24 stsp s->selected--;
7462 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7463 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7464 640cd7ff 2022-06-22 mark view->count = 0;
7466 83cc4199 2022-06-13 stsp case CTRL('u'):
7468 83cc4199 2022-06-13 stsp nscroll /= 2;
7469 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7470 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7471 6458efa5 2020-11-24 stsp case CTRL('b'):
7473 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7474 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7475 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7476 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7477 640cd7ff 2022-06-22 mark view->count = 0;
7480 6458efa5 2020-11-24 stsp case KEY_DOWN:
7481 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7482 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7483 6458efa5 2020-11-24 stsp s->selected++;
7486 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7487 6458efa5 2020-11-24 stsp /* can't scroll any further */
7488 640cd7ff 2022-06-22 mark view->count = 0;
7491 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7493 83cc4199 2022-06-13 stsp case CTRL('d'):
7495 83cc4199 2022-06-13 stsp nscroll /= 2;
7496 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7497 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7498 6458efa5 2020-11-24 stsp case CTRL('f'):
7500 48bb96f0 2022-06-20 naddy case ' ':
7501 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7502 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7503 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7504 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7505 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7506 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7507 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7508 640cd7ff 2022-06-22 mark view->count = 0;
7511 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7513 6458efa5 2020-11-24 stsp case CTRL('l'):
7514 640cd7ff 2022-06-22 mark view->count = 0;
7515 8924d611 2020-12-26 stsp tog_free_refs();
7516 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7519 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7520 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7522 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7523 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7524 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7527 640cd7ff 2022-06-22 mark view->count = 0;
7531 6458efa5 2020-11-24 stsp return err;
7534 6458efa5 2020-11-24 stsp __dead static void
7535 6458efa5 2020-11-24 stsp usage_ref(void)
7538 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7539 6458efa5 2020-11-24 stsp getprogname());
7543 6458efa5 2020-11-24 stsp static const struct got_error *
7544 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7546 6458efa5 2020-11-24 stsp const struct got_error *error;
7547 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7548 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7549 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7551 6458efa5 2020-11-24 stsp struct tog_view *view;
7552 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7554 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7555 6458efa5 2020-11-24 stsp switch (ch) {
7557 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7558 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7559 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7563 e99e2d15 2020-11-24 naddy usage_ref();
7564 6458efa5 2020-11-24 stsp /* NOTREACHED */
7568 6458efa5 2020-11-24 stsp argc -= optind;
7569 6458efa5 2020-11-24 stsp argv += optind;
7571 6458efa5 2020-11-24 stsp if (argc > 1)
7572 e99e2d15 2020-11-24 naddy usage_ref();
7574 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7575 0ae84acc 2022-06-15 tracey if (error != NULL)
7576 0ae84acc 2022-06-15 tracey goto done;
7578 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7579 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7580 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7581 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7582 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7583 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7584 c156c7a4 2020-12-18 stsp goto done;
7585 6458efa5 2020-11-24 stsp if (worktree)
7586 6458efa5 2020-11-24 stsp repo_path =
7587 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7589 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7590 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7591 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7592 c156c7a4 2020-12-18 stsp goto done;
7596 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7597 6458efa5 2020-11-24 stsp if (error != NULL)
7598 6458efa5 2020-11-24 stsp goto done;
7600 6458efa5 2020-11-24 stsp init_curses();
7602 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7603 51a10b52 2020-12-26 stsp if (error)
7604 51a10b52 2020-12-26 stsp goto done;
7606 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7607 6458efa5 2020-11-24 stsp if (error)
7608 6458efa5 2020-11-24 stsp goto done;
7610 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7611 6458efa5 2020-11-24 stsp if (view == NULL) {
7612 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7613 6458efa5 2020-11-24 stsp goto done;
7616 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7617 6458efa5 2020-11-24 stsp if (error)
7618 6458efa5 2020-11-24 stsp goto done;
7620 6458efa5 2020-11-24 stsp if (worktree) {
7621 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7622 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7623 6458efa5 2020-11-24 stsp worktree = NULL;
7625 6458efa5 2020-11-24 stsp error = view_loop(view);
7627 6458efa5 2020-11-24 stsp free(repo_path);
7628 6458efa5 2020-11-24 stsp free(cwd);
7629 1d0f4054 2021-06-17 stsp if (repo) {
7630 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7631 1d0f4054 2021-06-17 stsp if (close_err)
7632 1d0f4054 2021-06-17 stsp error = close_err;
7634 0ae84acc 2022-06-15 tracey if (pack_fds) {
7635 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7636 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7637 0ae84acc 2022-06-15 tracey if (error == NULL)
7638 0ae84acc 2022-06-15 tracey error = pack_err;
7640 51a10b52 2020-12-26 stsp tog_free_refs();
7641 6458efa5 2020-11-24 stsp return error;
7645 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7646 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7648 6458efa5 2020-11-24 stsp static void
7649 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7651 9b058f45 2022-06-30 mark switch (view->type) {
7652 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7653 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7654 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7655 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7659 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7660 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7662 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7663 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7666 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7667 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7668 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7670 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7671 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7672 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7674 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7675 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7676 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7682 9b058f45 2022-06-30 mark view->offset = 0;
7686 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7687 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7689 9b058f45 2022-06-30 mark static const struct got_error *
7690 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7692 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7693 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7694 9b058f45 2022-06-30 mark int *selected = NULL;
7695 9b058f45 2022-06-30 mark int header, offset;
7697 9b058f45 2022-06-30 mark switch (view->type) {
7698 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7699 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7700 9b058f45 2022-06-30 mark header = 3;
7701 9b058f45 2022-06-30 mark scrolld = NULL;
7702 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7703 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7704 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7705 9b058f45 2022-06-30 mark s->selected_line -= offset;
7706 9b058f45 2022-06-30 mark view->offset = offset;
7710 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7711 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7712 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7713 9b058f45 2022-06-30 mark header = 3;
7714 9b058f45 2022-06-30 mark selected = &s->selected;
7717 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7718 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7719 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7720 9b058f45 2022-06-30 mark header = 3;
7721 9b058f45 2022-06-30 mark selected = &s->selected;
7724 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7725 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7726 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7727 9b058f45 2022-06-30 mark header = 5;
7728 9b058f45 2022-06-30 mark selected = &s->selected;
7732 9b058f45 2022-06-30 mark selected = NULL;
7733 9b058f45 2022-06-30 mark scrolld = NULL;
7734 9b058f45 2022-06-30 mark header = 0;
7738 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7739 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7740 9b058f45 2022-06-30 mark view->offset = offset;
7741 9b058f45 2022-06-30 mark if (scrolld && offset) {
7742 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7743 9b058f45 2022-06-30 mark *selected -= offset;
7747 9b058f45 2022-06-30 mark return err;
7750 9b058f45 2022-06-30 mark static void
7751 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7755 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7756 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7757 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7758 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7760 6879ba42 2020-10-01 naddy fputc('\n', fp);
7763 4ed7e80c 2018-05-20 stsp __dead static void
7764 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7766 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7768 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7769 6879ba42 2020-10-01 naddy getprogname());
7770 6879ba42 2020-10-01 naddy if (hflag) {
7771 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7772 6879ba42 2020-10-01 naddy list_commands(fp);
7774 6879ba42 2020-10-01 naddy exit(status);
7777 c2301be8 2018-04-30 stsp static char **
7778 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7780 ee85c5e8 2020-02-29 stsp va_list ap;
7781 c2301be8 2018-04-30 stsp char **argv;
7784 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7786 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7787 c2301be8 2018-04-30 stsp if (argv == NULL)
7788 c2301be8 2018-04-30 stsp err(1, "calloc");
7789 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7790 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7791 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7792 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7795 ee85c5e8 2020-02-29 stsp va_end(ap);
7796 c2301be8 2018-04-30 stsp return argv;
7800 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7801 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7802 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7803 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7804 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7806 ee85c5e8 2020-02-29 stsp static const struct got_error *
7807 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7809 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7810 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7811 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7812 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7813 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7814 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7815 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7816 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7817 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7819 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7820 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7821 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7823 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7824 0ae84acc 2022-06-15 tracey if (error != NULL)
7825 0ae84acc 2022-06-15 tracey goto done;
7827 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7828 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7829 ee85c5e8 2020-02-29 stsp goto done;
7831 ee85c5e8 2020-02-29 stsp if (worktree)
7832 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7834 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7835 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7836 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7837 ee85c5e8 2020-02-29 stsp goto done;
7840 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7841 ee85c5e8 2020-02-29 stsp if (error != NULL)
7842 ee85c5e8 2020-02-29 stsp goto done;
7844 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7845 ee85c5e8 2020-02-29 stsp repo, worktree);
7846 ee85c5e8 2020-02-29 stsp if (error)
7847 ee85c5e8 2020-02-29 stsp goto done;
7849 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7850 84de9106 2020-12-26 stsp if (error)
7851 84de9106 2020-12-26 stsp goto done;
7852 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7853 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7854 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7855 ee85c5e8 2020-02-29 stsp if (error)
7856 ee85c5e8 2020-02-29 stsp goto done;
7858 ee85c5e8 2020-02-29 stsp if (worktree) {
7859 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7860 ee85c5e8 2020-02-29 stsp worktree = NULL;
7863 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7864 a44927cc 2022-04-07 stsp if (error)
7865 a44927cc 2022-04-07 stsp goto done;
7867 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7868 ee85c5e8 2020-02-29 stsp if (error) {
7869 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7870 ee85c5e8 2020-02-29 stsp goto done;
7871 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7872 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7873 6879ba42 2020-10-01 naddy usage(1, 1);
7874 ee85c5e8 2020-02-29 stsp /* not reached */
7877 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7878 1d0f4054 2021-06-17 stsp if (error == NULL)
7879 1d0f4054 2021-06-17 stsp error = close_err;
7880 ee85c5e8 2020-02-29 stsp repo = NULL;
7882 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7883 ee85c5e8 2020-02-29 stsp if (error)
7884 ee85c5e8 2020-02-29 stsp goto done;
7886 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7888 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7889 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7891 1d0f4054 2021-06-17 stsp if (repo) {
7892 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7893 1d0f4054 2021-06-17 stsp if (error == NULL)
7894 1d0f4054 2021-06-17 stsp error = close_err;
7896 a44927cc 2022-04-07 stsp if (commit)
7897 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7898 ee85c5e8 2020-02-29 stsp if (worktree)
7899 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7900 0ae84acc 2022-06-15 tracey if (pack_fds) {
7901 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7902 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7903 0ae84acc 2022-06-15 tracey if (error == NULL)
7904 0ae84acc 2022-06-15 tracey error = pack_err;
7907 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7908 ee85c5e8 2020-02-29 stsp free(commit_id);
7909 ee85c5e8 2020-02-29 stsp free(cwd);
7910 ee85c5e8 2020-02-29 stsp free(repo_path);
7911 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7912 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7914 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7915 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7916 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7918 87670572 2020-12-26 naddy tog_free_refs();
7919 ee85c5e8 2020-02-29 stsp return error;
7923 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7925 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7926 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7927 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7928 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7929 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7930 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7931 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7933 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
7935 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7937 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7938 9f7d7167 2018-04-29 stsp switch (ch) {
7940 9f7d7167 2018-04-29 stsp hflag = 1;
7943 53ccebc2 2019-07-30 stsp Vflag = 1;
7946 6879ba42 2020-10-01 naddy usage(hflag, 1);
7947 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7951 9f7d7167 2018-04-29 stsp argc -= optind;
7952 9f7d7167 2018-04-29 stsp argv += optind;
7953 9814e6a3 2020-09-27 naddy optind = 1;
7954 c2301be8 2018-04-30 stsp optreset = 1;
7956 53ccebc2 2019-07-30 stsp if (Vflag) {
7957 53ccebc2 2019-07-30 stsp got_version_print_str();
7958 6879ba42 2020-10-01 naddy return 0;
7961 4010e238 2020-12-04 stsp #ifndef PROFILE
7962 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7963 4010e238 2020-12-04 stsp NULL) == -1)
7964 4010e238 2020-12-04 stsp err(1, "pledge");
7967 c2301be8 2018-04-30 stsp if (argc == 0) {
7968 f29d3e89 2018-06-23 stsp if (hflag)
7969 6879ba42 2020-10-01 naddy usage(hflag, 0);
7970 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7971 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7973 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7977 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7978 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7979 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7980 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7981 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7987 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7988 917d79a7 2022-07-01 stsp if (diff_algo_str) {
7989 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
7990 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7991 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
7992 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7995 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7996 ee85c5e8 2020-02-29 stsp if (argc != 1)
7997 6879ba42 2020-10-01 naddy usage(0, 1);
7998 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7999 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8001 ee85c5e8 2020-02-29 stsp if (hflag)
8002 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8004 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8008 b46c1e04 2020-09-20 naddy putchar('\n');
8009 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8011 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8012 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8013 a2f4a359 2020-02-28 stsp free(cmd_argv);
8016 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8017 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);