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,
106 ec2a9698 2022-09-15 mark TOG_VIEW_HELP
109 ec2a9698 2022-09-15 mark /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
110 ec2a9698 2022-09-15 mark enum tog_keymap_type {
111 ec2a9698 2022-09-15 mark TOG_KEYMAP_KEYS = -2,
112 ec2a9698 2022-09-15 mark TOG_KEYMAP_GLOBAL,
113 ec2a9698 2022-09-15 mark TOG_KEYMAP_DIFF,
114 ec2a9698 2022-09-15 mark TOG_KEYMAP_LOG,
115 ec2a9698 2022-09-15 mark TOG_KEYMAP_BLAME,
116 ec2a9698 2022-09-15 mark TOG_KEYMAP_TREE,
117 ec2a9698 2022-09-15 mark TOG_KEYMAP_REF,
118 ec2a9698 2022-09-15 mark TOG_KEYMAP_HELP
121 9b058f45 2022-06-30 mark enum tog_view_mode {
122 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
123 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
124 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
127 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
129 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
131 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
132 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
133 ba4f502b 2018-08-04 stsp struct got_object_id *id;
134 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
137 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
138 ba4f502b 2018-08-04 stsp struct commit_queue {
139 ba4f502b 2018-08-04 stsp int ncommits;
140 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
143 f26dddb7 2019-11-08 stsp struct tog_color {
144 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
145 6d17833f 2019-11-08 stsp regex_t regex;
146 6d17833f 2019-11-08 stsp short colorpair;
148 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
150 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
151 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
152 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
154 cc488aa7 2022-01-23 stsp static const struct got_error *
155 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
156 cc488aa7 2022-01-23 stsp struct got_reference* re2)
158 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
159 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
160 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
162 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
163 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
164 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
165 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
167 cc488aa7 2022-01-23 stsp return NULL;
168 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
170 cc488aa7 2022-01-23 stsp return NULL;
173 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
174 cc488aa7 2022-01-23 stsp return NULL;
177 11b20872 2019-11-08 stsp static const struct got_error *
178 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
180 51a10b52 2020-12-26 stsp const struct got_error *err;
182 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
183 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
186 51a10b52 2020-12-26 stsp return err;
188 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
192 51a10b52 2020-12-26 stsp static void
193 51a10b52 2020-12-26 stsp tog_free_refs(void)
195 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
196 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
197 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
199 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
202 51a10b52 2020-12-26 stsp static const struct got_error *
203 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
204 11b20872 2019-11-08 stsp int idx, short color)
206 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
207 11b20872 2019-11-08 stsp struct tog_color *tc;
208 11b20872 2019-11-08 stsp int regerr = 0;
210 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
211 11b20872 2019-11-08 stsp return NULL;
213 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
215 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
216 11b20872 2019-11-08 stsp if (tc == NULL)
217 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
218 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
219 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
220 11b20872 2019-11-08 stsp if (regerr) {
221 11b20872 2019-11-08 stsp static char regerr_msg[512];
222 11b20872 2019-11-08 stsp static char err_msg[512];
223 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
224 11b20872 2019-11-08 stsp sizeof(regerr_msg));
225 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
226 11b20872 2019-11-08 stsp regerr_msg);
227 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
229 11b20872 2019-11-08 stsp return err;
231 11b20872 2019-11-08 stsp tc->colorpair = idx;
232 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
233 11b20872 2019-11-08 stsp return NULL;
236 11b20872 2019-11-08 stsp static void
237 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
239 11b20872 2019-11-08 stsp struct tog_color *tc;
241 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
242 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
243 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
244 11b20872 2019-11-08 stsp regfree(&tc->regex);
249 336075a4 2022-06-25 op static struct tog_color *
250 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
252 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
254 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
255 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
259 11b20872 2019-11-08 stsp return NULL;
263 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
268 11b20872 2019-11-08 stsp return COLOR_CYAN;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
270 11b20872 2019-11-08 stsp return COLOR_YELLOW;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
274 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
276 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
278 91b8c405 2020-01-25 stsp return COLOR_CYAN;
279 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
280 11b20872 2019-11-08 stsp return COLOR_GREEN;
281 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
282 11b20872 2019-11-08 stsp return COLOR_GREEN;
283 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
284 11b20872 2019-11-08 stsp return COLOR_CYAN;
285 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
286 11b20872 2019-11-08 stsp return COLOR_YELLOW;
287 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
288 6458efa5 2020-11-24 stsp return COLOR_GREEN;
289 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
290 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
291 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
292 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
293 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
294 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
300 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
302 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
304 11b20872 2019-11-08 stsp if (val == NULL)
305 11b20872 2019-11-08 stsp return default_color_value(envvar);
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLACK;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
310 11b20872 2019-11-08 stsp return COLOR_RED;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
312 11b20872 2019-11-08 stsp return COLOR_GREEN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
314 11b20872 2019-11-08 stsp return COLOR_YELLOW;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
316 11b20872 2019-11-08 stsp return COLOR_BLUE;
317 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
318 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
319 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
320 11b20872 2019-11-08 stsp return COLOR_CYAN;
321 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
322 11b20872 2019-11-08 stsp return COLOR_WHITE;
323 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
326 11b20872 2019-11-08 stsp return default_color_value(envvar);
329 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
330 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
331 3dbaef42 2020-11-24 stsp const char *label1, *label2;
332 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
333 f9d37699 2022-06-28 stsp int fd1, fd2;
334 c7d5c43c 2022-08-04 mark int lineno;
335 15a087fe 2019-02-21 stsp int first_displayed_line;
336 15a087fe 2019-02-21 stsp int last_displayed_line;
338 15a087fe 2019-02-21 stsp int diff_context;
339 3dbaef42 2020-11-24 stsp int ignore_whitespace;
340 64453f7e 2020-11-21 stsp int force_text_diff;
341 15a087fe 2019-02-21 stsp struct got_repository *repo;
342 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
343 fe621944 2020-11-10 stsp size_t nlines;
344 f44b1f58 2020-02-02 tracey int matched_line;
345 f44b1f58 2020-02-02 tracey int selected_line;
347 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
348 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
351 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
352 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
354 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
355 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
356 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
357 1a76625f 2018-10-22 stsp int commits_needed;
358 fb280deb 2021-08-30 stsp int load_all;
359 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
360 568eae95 2022-09-11 mark struct commit_queue *real_commits;
361 1a76625f 2018-10-22 stsp const char *in_repo_path;
362 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
363 1a76625f 2018-10-22 stsp struct got_repository *repo;
364 74467cc8 2022-06-15 stsp int *pack_fds;
365 1a76625f 2018-10-22 stsp int log_complete;
366 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
367 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
368 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
369 13add988 2019-10-15 stsp int *searching;
370 13add988 2019-10-15 stsp int *search_next_done;
371 13add988 2019-10-15 stsp regex_t *regex;
372 568eae95 2022-09-11 mark int *limiting;
373 568eae95 2022-09-11 mark int limit_match;
374 568eae95 2022-09-11 mark regex_t *limit_regex;
375 568eae95 2022-09-11 mark struct commit_queue *limit_commits;
378 1a76625f 2018-10-22 stsp struct tog_log_view_state {
379 568eae95 2022-09-11 mark struct commit_queue *commits;
380 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
381 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
382 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
383 568eae95 2022-09-11 mark struct commit_queue real_commits;
384 b01e7d3b 2018-08-04 stsp int selected;
385 b01e7d3b 2018-08-04 stsp char *in_repo_path;
386 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
387 b672a97a 2020-01-27 stsp int log_branches;
388 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
389 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
390 1a76625f 2018-10-22 stsp sig_atomic_t quit;
391 1a76625f 2018-10-22 stsp pthread_t thread;
392 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
393 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
394 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
395 11b20872 2019-11-08 stsp struct tog_colors colors;
396 10aab77f 2022-07-19 op int use_committer;
397 568eae95 2022-09-11 mark int limit_view;
398 568eae95 2022-09-11 mark regex_t limit_regex;
399 568eae95 2022-09-11 mark struct commit_queue limit_commits;
402 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
403 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
404 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
405 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
406 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
407 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
408 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
409 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
410 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
411 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
412 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
413 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
414 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
415 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
416 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
418 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
419 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
420 e9424729 2018-08-04 stsp int nlines;
422 e9424729 2018-08-04 stsp struct tog_view *view;
423 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
427 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
428 e9424729 2018-08-04 stsp const char *path;
429 e9424729 2018-08-04 stsp struct got_repository *repo;
430 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
431 e9424729 2018-08-04 stsp int *complete;
432 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
433 fc06ba56 2019-08-22 stsp void *cancel_arg;
436 e9424729 2018-08-04 stsp struct tog_blame {
438 be659d10 2020-11-18 stsp off_t filesize;
439 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
440 6fcac457 2018-11-19 stsp int nlines;
441 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
442 e9424729 2018-08-04 stsp pthread_t thread;
443 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
444 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
445 e9424729 2018-08-04 stsp const char *path;
446 0ae84acc 2022-06-15 tracey int *pack_fds;
449 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
450 7cbe629d 2018-08-04 stsp int first_displayed_line;
451 7cbe629d 2018-08-04 stsp int last_displayed_line;
452 7cbe629d 2018-08-04 stsp int selected_line;
453 c0f61fa4 2022-07-11 mark int last_diffed_line;
454 7cbe629d 2018-08-04 stsp int blame_complete;
457 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
458 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
459 e5a0f69f 2018-08-18 stsp char *path;
460 7cbe629d 2018-08-04 stsp struct got_repository *repo;
461 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
462 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
463 e9424729 2018-08-04 stsp struct tog_blame blame;
464 6c4c42e0 2019-06-24 stsp int matched_line;
465 11b20872 2019-11-08 stsp struct tog_colors colors;
468 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
469 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
470 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
471 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
472 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
473 ad80ab7b 2018-08-04 stsp int selected;
476 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
478 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
479 ad80ab7b 2018-08-04 stsp char *tree_label;
480 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
481 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
482 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
483 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
484 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
486 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
487 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
488 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
489 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
490 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
491 6458efa5 2020-11-24 stsp struct tog_colors colors;
494 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
495 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
496 6458efa5 2020-11-24 stsp struct got_reference *ref;
500 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
502 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
503 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
504 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
506 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
507 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
508 6458efa5 2020-11-24 stsp struct got_repository *repo;
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
510 bddb1296 2019-11-08 stsp struct tog_colors colors;
513 ec2a9698 2022-09-15 mark struct tog_help_view_state {
515 ec2a9698 2022-09-15 mark off_t *line_offsets;
516 ec2a9698 2022-09-15 mark size_t nlines;
517 ec2a9698 2022-09-15 mark int lineno;
518 ec2a9698 2022-09-15 mark int first_displayed_line;
519 ec2a9698 2022-09-15 mark int last_displayed_line;
521 ec2a9698 2022-09-15 mark int matched_line;
522 ec2a9698 2022-09-15 mark int selected_line;
524 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
527 ec2a9698 2022-09-15 mark #define GENERATE_HELP \
528 ec2a9698 2022-09-15 mark KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
529 ec2a9698 2022-09-15 mark KEY_("H F1", "Open view-specific help (double tap for all help)"), \
530 ec2a9698 2022-09-15 mark KEY_("k C-p Up", "Move cursor or page up one line"), \
531 ec2a9698 2022-09-15 mark KEY_("j C-n Down", "Move cursor or page down one line"), \
532 ec2a9698 2022-09-15 mark KEY_("C-b b PgUp", "Scroll the view up one page"), \
533 ec2a9698 2022-09-15 mark KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
534 ec2a9698 2022-09-15 mark KEY_("C-u u", "Scroll the view up one half page"), \
535 ec2a9698 2022-09-15 mark KEY_("C-d d", "Scroll the view down one half page"), \
536 0b3f028d 2023-01-09 mark KEY_("g", "Go to line N (default: first line)"), \
537 0b3f028d 2023-01-09 mark KEY_("Home =", "Go to the first line"), \
538 0b3f028d 2023-01-09 mark KEY_("G", "Go to line N (default: last line)"), \
539 0b3f028d 2023-01-09 mark KEY_("End *", "Go to the last line"), \
540 ec2a9698 2022-09-15 mark KEY_("l Right", "Scroll the view right"), \
541 ec2a9698 2022-09-15 mark KEY_("h Left", "Scroll the view left"), \
542 ec2a9698 2022-09-15 mark KEY_("$", "Scroll view to the rightmost position"), \
543 ec2a9698 2022-09-15 mark KEY_("0", "Scroll view to the leftmost position"), \
544 ec2a9698 2022-09-15 mark KEY_("-", "Decrease size of the focussed split"), \
545 ec2a9698 2022-09-15 mark KEY_("+", "Increase size of the focussed split"), \
546 ec2a9698 2022-09-15 mark KEY_("Tab", "Switch focus between views"), \
547 ec2a9698 2022-09-15 mark KEY_("F", "Toggle fullscreen mode"), \
548 ec2a9698 2022-09-15 mark KEY_("/", "Open prompt to enter search term"), \
549 ec2a9698 2022-09-15 mark KEY_("n", "Find next line/token matching the current search term"), \
550 ec2a9698 2022-09-15 mark KEY_("N", "Find previous line/token matching the current search term"),\
551 16a2d4f0 2022-09-19 stsp KEY_("q", "Quit the focussed view; Quit help screen"), \
552 ec2a9698 2022-09-15 mark KEY_("Q", "Quit tog"), \
554 fd6badaa 2022-09-23 stsp KEYMAP_("Log view", TOG_KEYMAP_LOG), \
555 ec2a9698 2022-09-15 mark KEY_("< ,", "Move cursor up one commit"), \
556 ec2a9698 2022-09-15 mark KEY_("> .", "Move cursor down one commit"), \
557 ec2a9698 2022-09-15 mark KEY_("Enter", "Open diff view of the selected commit"), \
558 ec2a9698 2022-09-15 mark KEY_("B", "Reload the log view and toggle display of merged commits"), \
559 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
560 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the repository from the selected" \
561 ec2a9698 2022-09-15 mark " commit"), \
562 ec2a9698 2022-09-15 mark KEY_("@", "Toggle between displaying author and committer name"), \
563 ec2a9698 2022-09-15 mark KEY_("&", "Open prompt to enter term to limit commits displayed"), \
564 ec2a9698 2022-09-15 mark KEY_("C-g Backspace", "Cancel current search or log operation"), \
565 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload the log view with new commits in the repository"), \
567 fd6badaa 2022-09-23 stsp KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
568 ec2a9698 2022-09-15 mark KEY_("K < ,", "Display diff of next line in the file/log entry"), \
569 ec2a9698 2022-09-15 mark KEY_("J > .", "Display diff of previous line in the file/log entry"), \
570 ec2a9698 2022-09-15 mark KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
571 ec2a9698 2022-09-15 mark KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
572 ec2a9698 2022-09-15 mark " data"), \
573 ec2a9698 2022-09-15 mark KEY_("(", "Go to the previous file in the diff"), \
574 ec2a9698 2022-09-15 mark KEY_(")", "Go to the next file in the diff"), \
575 ec2a9698 2022-09-15 mark KEY_("{", "Go to the previous hunk in the diff"), \
576 ec2a9698 2022-09-15 mark KEY_("}", "Go to the next hunk in the diff"), \
577 ec2a9698 2022-09-15 mark KEY_("[", "Decrease the number of context lines"), \
578 ec2a9698 2022-09-15 mark KEY_("]", "Increase the number of context lines"), \
579 ec2a9698 2022-09-15 mark KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
581 fd6badaa 2022-09-23 stsp KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
582 ec2a9698 2022-09-15 mark KEY_("Enter", "Display diff view of the selected line's commit"), \
583 ec2a9698 2022-09-15 mark KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
584 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the currently selected annotated line"), \
585 ec2a9698 2022-09-15 mark KEY_("C", "Reload view with the previously blamed commit"), \
586 ec2a9698 2022-09-15 mark KEY_("c", "Reload view with the version of the file found in the" \
587 ec2a9698 2022-09-15 mark " selected line's commit"), \
588 ec2a9698 2022-09-15 mark KEY_("p", "Reload view with the version of the file found in the" \
589 ec2a9698 2022-09-15 mark " selected line's parent commit"), \
591 fd6badaa 2022-09-23 stsp KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
592 ec2a9698 2022-09-15 mark KEY_("Enter", "Enter selected directory or open blame view of the" \
593 ec2a9698 2022-09-15 mark " selected file"), \
594 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the selected entry"), \
595 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
596 ec2a9698 2022-09-15 mark KEY_("i", "Show object IDs for all tree entries"), \
597 ec2a9698 2022-09-15 mark KEY_("Backspace", "Return to the parent directory"), \
599 fd6badaa 2022-09-23 stsp KEYMAP_("Ref view", TOG_KEYMAP_REF), \
600 ec2a9698 2022-09-15 mark KEY_("Enter", "Display log view of the selected reference"), \
601 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the selected reference"), \
602 ec2a9698 2022-09-15 mark KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
603 ec2a9698 2022-09-15 mark KEY_("m", "Toggle display of last modified date for each reference"), \
604 ec2a9698 2022-09-15 mark KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
605 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload view with all repository references")
607 ec2a9698 2022-09-15 mark struct tog_key_map {
608 ec2a9698 2022-09-15 mark const char *keys;
609 ec2a9698 2022-09-15 mark const char *info;
610 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
614 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
616 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
617 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
618 669b5ffa 2018-10-07 stsp * there is enough screen estate.
620 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
621 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
623 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
624 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
625 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
627 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
628 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
630 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
632 cc3c9aac 2018-08-01 stsp struct tog_view {
633 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
634 26ed57b2 2018-05-19 stsp WINDOW *window;
635 26ed57b2 2018-05-19 stsp PANEL *panel;
636 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
637 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
638 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
639 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
640 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
641 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
642 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
643 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
644 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
646 669b5ffa 2018-10-07 stsp struct tog_view *parent;
647 669b5ffa 2018-10-07 stsp struct tog_view *child;
650 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
651 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
652 e78dc838 2020-12-04 stsp * between parent and child.
653 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
654 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
655 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
656 e78dc838 2020-12-04 stsp * situations.
658 e78dc838 2020-12-04 stsp int focus_child;
660 9b058f45 2022-06-30 mark enum tog_view_mode mode;
661 5dc9f4bc 2018-08-04 stsp /* type-specific state */
662 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
664 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
665 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
666 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
667 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
668 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
669 ec2a9698 2022-09-15 mark struct tog_help_view_state help;
672 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
673 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
674 e78dc838 2020-12-04 stsp struct tog_view *, int);
675 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
676 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
677 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
679 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
680 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
681 eaf2c13e 2022-09-17 mark void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
682 eaf2c13e 2022-09-17 mark int **, int **, int **, int **);
683 c0c4acc8 2021-01-24 stsp int search_started;
684 60493ae3 2019-06-20 stsp int searching;
685 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
686 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
687 60493ae3 2019-06-20 stsp int search_next_done;
688 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
689 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
690 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
691 1803e47f 2019-06-22 stsp regex_t regex;
692 41605754 2020-11-12 stsp regmatch_t regmatch;
695 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
696 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
697 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
698 78756c87 2020-11-24 stsp struct got_repository *);
699 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
700 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
701 e78dc838 2020-12-04 stsp struct tog_view *, int);
702 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
703 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
704 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
705 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
706 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
707 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
709 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
710 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
711 78756c87 2020-11-24 stsp const char *, const char *, int);
712 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
713 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
714 e78dc838 2020-12-04 stsp struct tog_view *, int);
715 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
716 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
717 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
718 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
720 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
721 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
722 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
723 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
724 e78dc838 2020-12-04 stsp struct tog_view *, int);
725 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
726 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
727 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
728 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
729 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
731 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
732 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
733 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
734 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
735 e78dc838 2020-12-04 stsp struct tog_view *, int);
736 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
737 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
738 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
740 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
741 6458efa5 2020-11-24 stsp struct got_repository *);
742 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
743 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
744 e78dc838 2020-12-04 stsp struct tog_view *, int);
745 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
746 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
747 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
749 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
750 eaf2c13e 2022-09-17 mark struct tog_view *);
751 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
752 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
753 eaf2c13e 2022-09-17 mark struct tog_view *, int);
754 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
755 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
756 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
757 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
758 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
760 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
761 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
762 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
763 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
764 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
766 25791caa 2018-10-24 stsp static void
767 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
769 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
772 83baff54 2019-08-12 stsp static void
773 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
775 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
778 61266923 2020-01-14 stsp static void
779 61266923 2020-01-14 stsp tog_sigcont(int signo)
781 61266923 2020-01-14 stsp tog_sigcont_received = 1;
784 2497f032 2022-05-31 stsp static void
785 2497f032 2022-05-31 stsp tog_sigint(int signo)
787 2497f032 2022-05-31 stsp tog_sigint_received = 1;
790 2497f032 2022-05-31 stsp static void
791 2497f032 2022-05-31 stsp tog_sigterm(int signo)
793 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
797 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
799 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
800 f044c841 2022-10-24 stsp tog_sigint_received || tog_sigterm_received);
803 e5a0f69f 2018-08-18 stsp static const struct got_error *
804 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
806 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
808 669b5ffa 2018-10-07 stsp if (view->child) {
809 5629093a 2022-07-11 stsp child_err = view_close(view->child);
810 669b5ffa 2018-10-07 stsp view->child = NULL;
812 e5a0f69f 2018-08-18 stsp if (view->close)
813 e5a0f69f 2018-08-18 stsp err = view->close(view);
814 ea5e7bb5 2018-08-01 stsp if (view->panel)
815 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
816 ea5e7bb5 2018-08-01 stsp if (view->window)
817 ea5e7bb5 2018-08-01 stsp delwin(view->window);
818 ea5e7bb5 2018-08-01 stsp free(view);
819 5629093a 2022-07-11 stsp return err ? err : child_err;
822 ea5e7bb5 2018-08-01 stsp static struct tog_view *
823 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
824 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
826 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
828 ea5e7bb5 2018-08-01 stsp if (view == NULL)
829 ea5e7bb5 2018-08-01 stsp return NULL;
831 d6b05b5b 2018-08-04 stsp view->type = type;
832 f7d12f7e 2018-08-01 stsp view->lines = LINES;
833 f7d12f7e 2018-08-01 stsp view->cols = COLS;
834 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
835 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
836 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
837 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
838 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
839 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
840 96a765a8 2018-08-04 stsp view_close(view);
841 ea5e7bb5 2018-08-01 stsp return NULL;
843 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
844 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
845 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
846 96a765a8 2018-08-04 stsp view_close(view);
847 ea5e7bb5 2018-08-01 stsp return NULL;
850 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
851 ea5e7bb5 2018-08-01 stsp return view;
855 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
857 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
859 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
862 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
864 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
866 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
869 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
871 5c60c32a 2018-10-18 stsp static const struct got_error *
872 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
874 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
876 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
877 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
878 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
880 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
881 9b058f45 2022-06-30 mark view->begin_x = 0;
882 571ccd73 2022-07-19 mark } else if (!view->resized) {
883 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
884 3c1dfe12 2022-07-08 mark view->cols > 119)
885 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
887 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
888 9b058f45 2022-06-30 mark view->begin_y = 0;
890 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
891 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
892 5c60c32a 2018-10-18 stsp view->lines = LINES;
893 5c60c32a 2018-10-18 stsp view->cols = COLS;
894 5c60c32a 2018-10-18 stsp err = view_resize(view);
896 5c60c32a 2018-10-18 stsp return err;
898 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
899 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
901 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
902 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
904 5c60c32a 2018-10-18 stsp return NULL;
907 5c60c32a 2018-10-18 stsp static const struct got_error *
908 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
910 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
912 5c60c32a 2018-10-18 stsp view->begin_x = 0;
913 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
914 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
915 5c60c32a 2018-10-18 stsp view->ncols = COLS;
916 5c60c32a 2018-10-18 stsp view->lines = LINES;
917 5c60c32a 2018-10-18 stsp view->cols = COLS;
918 5c60c32a 2018-10-18 stsp err = view_resize(view);
920 5c60c32a 2018-10-18 stsp return err;
922 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
923 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
925 5c60c32a 2018-10-18 stsp return NULL;
929 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
931 5c60c32a 2018-10-18 stsp return view->parent == NULL;
935 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
937 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
941 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
943 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
947 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
949 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
950 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
953 9b058f45 2022-06-30 mark static void
954 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
956 9b058f45 2022-06-30 mark PANEL *panel;
957 9b058f45 2022-06-30 mark const struct tog_view *view_above;
959 9b058f45 2022-06-30 mark if (view->parent)
960 9b058f45 2022-06-30 mark return view_border(view->parent);
962 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
963 9b058f45 2022-06-30 mark if (panel == NULL)
966 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
967 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
968 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
969 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
970 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
972 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
973 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
976 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
977 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
978 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
979 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
980 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
982 4d8c2215 2018-08-19 stsp static const struct got_error *
983 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
985 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
986 9b058f45 2022-06-30 mark int dif, nlines, ncols;
988 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
990 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
991 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
993 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
994 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
995 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
997 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
999 4dd27a72 2022-06-29 stsp if (view->child) {
1000 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
1002 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1003 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1004 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1005 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1006 0dbbbe90 2022-06-17 op ncols = COLS;
1008 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1009 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1010 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1012 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1014 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1016 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1017 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1020 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1021 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1022 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1025 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1027 9b058f45 2022-06-30 mark return err;
1028 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1029 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1031 9b058f45 2022-06-30 mark return err;
1033 9b058f45 2022-06-30 mark view_border(view);
1034 9b058f45 2022-06-30 mark update_panels();
1035 9b058f45 2022-06-30 mark doupdate();
1036 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1037 9b058f45 2022-06-30 mark nlines = view->nlines;
1039 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1040 0dbbbe90 2022-06-17 op ncols = COLS;
1042 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1043 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1045 571ccd73 2022-07-19 mark return err;
1048 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1049 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1050 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1051 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1052 0dbbbe90 2022-06-17 op wclear(view->window);
1054 0dbbbe90 2022-06-17 op view->nlines = nlines;
1055 0dbbbe90 2022-06-17 op view->ncols = ncols;
1056 0dbbbe90 2022-06-17 op view->lines = LINES;
1057 0dbbbe90 2022-06-17 op view->cols = COLS;
1059 5c60c32a 2018-10-18 stsp return NULL;
1062 571ccd73 2022-07-19 mark static const struct got_error *
1063 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1065 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1066 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1067 6fe51fee 2022-07-22 mark int n = 0;
1069 6fe51fee 2022-07-22 mark if (s->selected_entry)
1070 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1073 571ccd73 2022-07-19 mark * Request commits to account for the increased
1074 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1076 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1077 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1078 571ccd73 2022-07-19 mark err = request_log_commits(view);
1081 571ccd73 2022-07-19 mark return err;
1084 d9a7ab53 2022-07-11 mark static void
1085 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1087 d9a7ab53 2022-07-11 mark if (n == 0)
1090 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1091 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1092 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1094 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1095 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1096 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1097 d9a7ab53 2022-07-11 mark view->offset -= n;
1099 d9a7ab53 2022-07-11 mark view->offset = 0;
1103 3c1dfe12 2022-07-08 mark static const struct got_error *
1104 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1106 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1107 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1109 3c1dfe12 2022-07-08 mark if (view->parent)
1110 3c1dfe12 2022-07-08 mark v = view->parent;
1114 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1115 3c1dfe12 2022-07-08 mark return NULL;
1117 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1119 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1120 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1121 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1122 3c1dfe12 2022-07-08 mark if (view->parent)
1123 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1125 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1126 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1127 3c1dfe12 2022-07-08 mark view->count = 0;
1128 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1129 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1130 3c1dfe12 2022-07-08 mark view->count = 0;
1131 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1133 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1134 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1135 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1136 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1138 3c1dfe12 2022-07-08 mark return err;
1139 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1141 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1142 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1143 3c1dfe12 2022-07-08 mark if (view->parent)
1144 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1146 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1147 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1148 3c1dfe12 2022-07-08 mark view->count = 0;
1149 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1150 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1151 3c1dfe12 2022-07-08 mark view->count = 0;
1152 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1154 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1157 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1158 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1159 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1160 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1162 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1164 3c1dfe12 2022-07-08 mark return err;
1165 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1167 3c1dfe12 2022-07-08 mark return err;
1169 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1170 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1172 3c1dfe12 2022-07-08 mark return err;
1175 6fe51fee 2022-07-22 mark if (v->resize)
1176 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1177 6fe51fee 2022-07-22 mark else if (v->child->resize)
1178 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1180 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1182 3c1dfe12 2022-07-08 mark return err;
1185 3c1dfe12 2022-07-08 mark static void
1186 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1188 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1190 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1191 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1194 669b5ffa 2018-10-07 stsp static const struct got_error *
1195 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1197 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1199 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1200 669b5ffa 2018-10-07 stsp return NULL;
1202 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1203 669b5ffa 2018-10-07 stsp view->child = NULL;
1204 669b5ffa 2018-10-07 stsp return err;
1207 0dbbbe90 2022-06-17 op static const struct got_error *
1208 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1210 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1212 669b5ffa 2018-10-07 stsp view->child = child;
1213 669b5ffa 2018-10-07 stsp child->parent = view;
1215 3c1dfe12 2022-07-08 mark err = view_resize(view);
1217 3c1dfe12 2022-07-08 mark return err;
1219 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1220 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1222 3c1dfe12 2022-07-08 mark return err;
1225 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1226 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1228 136e2bd4 2022-07-23 mark static const struct got_error *
1229 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1230 136e2bd4 2022-07-23 mark enum tog_view_type request)
1232 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1233 136e2bd4 2022-07-23 mark const struct got_error *err;
1234 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1236 136e2bd4 2022-07-23 mark *requested = NULL;
1238 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1239 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1241 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1243 136e2bd4 2022-07-23 mark return err;
1245 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1246 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1247 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1249 136e2bd4 2022-07-23 mark return err;
1252 136e2bd4 2022-07-23 mark view->focussed = 0;
1253 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1254 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1255 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1256 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1258 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1259 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1260 136e2bd4 2022-07-23 mark err = view_close_child(view);
1262 136e2bd4 2022-07-23 mark return err;
1263 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1265 136e2bd4 2022-07-23 mark return err;
1266 136e2bd4 2022-07-23 mark view->focus_child = 1;
1268 136e2bd4 2022-07-23 mark *requested = new_view;
1270 136e2bd4 2022-07-23 mark return NULL;
1273 34bc9ec9 2019-02-22 stsp static void
1274 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1276 25791caa 2018-10-24 stsp int cols, lines;
1277 25791caa 2018-10-24 stsp struct winsize size;
1279 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1280 25791caa 2018-10-24 stsp cols = 80; /* Default */
1281 25791caa 2018-10-24 stsp lines = 24;
1283 25791caa 2018-10-24 stsp cols = size.ws_col;
1284 25791caa 2018-10-24 stsp lines = size.ws_row;
1286 25791caa 2018-10-24 stsp resize_term(lines, cols);
1289 2b49a8ae 2019-06-22 stsp static const struct got_error *
1290 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1292 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1293 9b058f45 2022-06-30 mark struct tog_view *v = view;
1294 2b49a8ae 2019-06-22 stsp char pattern[1024];
1297 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1298 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1299 c0c4acc8 2021-01-24 stsp view->searching = 0;
1300 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1302 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1304 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1305 2b49a8ae 2019-06-22 stsp return NULL;
1307 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1308 9b058f45 2022-06-30 mark v = view->child;
1309 9d0feb8b 2022-12-27 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1310 9d0feb8b 2022-12-27 mark v = view->parent;
1312 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1313 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1315 092a9f9c 2022-12-27 mark nodelay(v->window, FALSE); /* block for search term input */
1316 2b49a8ae 2019-06-22 stsp nocbreak();
1318 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1319 9b058f45 2022-06-30 mark wrefresh(v->window);
1322 092a9f9c 2022-12-27 mark nodelay(v->window, TRUE);
1323 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1324 2b49a8ae 2019-06-22 stsp return NULL;
1326 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1327 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1328 7c32bd05 2019-06-22 stsp if (err) {
1329 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1330 7c32bd05 2019-06-22 stsp return err;
1332 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1333 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1334 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1335 2b49a8ae 2019-06-22 stsp view->search_next(view);
1338 2b49a8ae 2019-06-22 stsp return NULL;
1341 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1342 d2366e29 2022-07-07 mark static const struct got_error *
1343 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1345 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1346 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1348 d2366e29 2022-07-07 mark if (view->parent)
1349 d2366e29 2022-07-07 mark v = view->parent;
1353 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1354 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1356 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1358 7532ccda 2022-07-11 mark if (!v->child)
1359 7532ccda 2022-07-11 mark return NULL;
1360 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1361 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1363 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1364 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1365 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1366 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1367 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1370 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1371 d2366e29 2022-07-07 mark v->ncols = COLS;
1372 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1373 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1375 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1377 d2366e29 2022-07-07 mark return err;
1379 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1380 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1381 d2366e29 2022-07-07 mark v->focus_child = 1;
1383 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1385 d2366e29 2022-07-07 mark return err;
1386 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1388 d2366e29 2022-07-07 mark return err;
1390 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1391 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1392 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1393 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1395 279d2047 2022-07-29 stsp return err;
1396 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1398 279d2047 2022-07-29 stsp return err;
1400 7532ccda 2022-07-11 mark offset_selection_up(v);
1401 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1403 dff91825 2022-07-22 mark if (v->resize)
1404 dff91825 2022-07-22 mark err = v->resize(v, 0);
1405 dff91825 2022-07-22 mark else if (v->child->resize)
1406 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1408 d2366e29 2022-07-07 mark return err;
1412 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1413 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1415 640cd7ff 2022-06-22 mark static int
1416 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1418 9b058f45 2022-06-30 mark struct tog_view *v = view;
1419 9b058f45 2022-06-30 mark int x, n = 0;
1421 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1422 9b058f45 2022-06-30 mark v = view->child;
1423 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1424 9b058f45 2022-06-30 mark v = view->parent;
1426 640cd7ff 2022-06-22 mark view->count = 0;
1427 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1428 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1429 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1430 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1431 9b058f45 2022-06-30 mark waddch(v->window, ':');
1434 9b058f45 2022-06-30 mark x = getcurx(v->window);
1435 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1436 9b058f45 2022-06-30 mark waddch(v->window, c);
1437 9b058f45 2022-06-30 mark wrefresh(v->window);
1441 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1442 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1444 640cd7ff 2022-06-22 mark if (n >= 9999999)
1445 640cd7ff 2022-06-22 mark n = 9999999;
1447 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1448 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1450 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1451 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1456 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1457 640cd7ff 2022-06-22 mark view->count = n;
1462 0cf4efb1 2018-09-29 stsp static const struct got_error *
1463 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1464 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1466 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1467 669b5ffa 2018-10-07 stsp struct tog_view *v;
1468 1a76625f 2018-10-22 stsp int ch, errcode;
1470 e5a0f69f 2018-08-18 stsp *new = NULL;
1472 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1473 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1474 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1475 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1476 640cd7ff 2022-06-22 mark view->count = 0;
1479 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1480 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1481 82954512 2020-02-03 stsp if (errcode)
1482 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1483 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1484 3da8ef85 2021-09-21 stsp sched_yield();
1485 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1486 82954512 2020-02-03 stsp if (errcode)
1487 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1488 82954512 2020-02-03 stsp "pthread_mutex_lock");
1489 60493ae3 2019-06-20 stsp view->search_next(view);
1490 60493ae3 2019-06-20 stsp return NULL;
1493 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1494 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1495 1a76625f 2018-10-22 stsp if (errcode)
1496 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1497 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1498 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1500 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1501 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1502 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1503 a6d37fac 2022-07-03 mark view->count = 0;
1505 a6d37fac 2022-07-03 mark ch = view->ch;
1507 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1508 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1509 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1511 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1512 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1513 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1514 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1515 1a76625f 2018-10-22 stsp if (errcode)
1516 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1518 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1519 25791caa 2018-10-24 stsp tog_resizeterm();
1520 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1521 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1522 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1523 25791caa 2018-10-24 stsp err = view_resize(v);
1525 25791caa 2018-10-24 stsp return err;
1526 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1528 25791caa 2018-10-24 stsp return err;
1529 cdfcfb03 2020-12-06 stsp if (v->child) {
1530 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1532 cdfcfb03 2020-12-06 stsp return err;
1533 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1534 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1536 cdfcfb03 2020-12-06 stsp return err;
1537 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1538 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1540 3c1dfe12 2022-07-08 mark return err;
1546 e5a0f69f 2018-08-18 stsp switch (ch) {
1549 ec2a9698 2022-09-15 mark case KEY_F(1):
1550 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1551 ec2a9698 2022-09-15 mark err = view->reset(view);
1553 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1556 640cd7ff 2022-06-22 mark view->count = 0;
1557 1e37a5c2 2019-05-12 jcs if (view->child) {
1558 e78dc838 2020-12-04 stsp view->focussed = 0;
1559 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1560 e78dc838 2020-12-04 stsp view->focus_child = 1;
1561 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1562 e78dc838 2020-12-04 stsp view->focussed = 0;
1563 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1564 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1565 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1566 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1567 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1570 9b058f45 2022-06-30 mark return err;
1572 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1573 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1575 9b058f45 2022-06-30 mark return err;
1580 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1581 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1582 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1583 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1587 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1589 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1590 9970f7fc 2020-12-03 stsp view->dying = 1;
1596 640cd7ff 2022-06-22 mark view->count = 0;
1597 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1598 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1600 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1601 e78dc838 2020-12-04 stsp view->focussed = 0;
1602 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1603 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1605 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1607 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1611 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1612 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1614 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1615 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1616 e78dc838 2020-12-04 stsp view->focussed = 1;
1617 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1619 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1620 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1621 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1623 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1627 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1631 6fe51fee 2022-07-22 mark if (view->resize) {
1632 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1636 9b058f45 2022-06-30 mark if (view->parent)
1637 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1639 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1642 3c1dfe12 2022-07-08 mark view->count = 0;
1643 d2366e29 2022-07-07 mark err = switch_split(view);
1646 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1649 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1651 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1654 640cd7ff 2022-06-22 mark view->count = 0;
1655 60493ae3 2019-06-20 stsp if (view->search_start)
1656 2b49a8ae 2019-06-22 stsp view_search_start(view);
1658 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1662 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1663 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1664 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1665 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1666 60493ae3 2019-06-20 stsp view->search_next(view);
1668 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1671 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1672 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1674 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1675 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1676 917d79a7 2022-07-01 stsp if (v->reset) {
1677 917d79a7 2022-07-01 stsp err = v->reset(v);
1679 917d79a7 2022-07-01 stsp return err;
1681 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1682 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1684 917d79a7 2022-07-01 stsp return err;
1689 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1693 e5a0f69f 2018-08-18 stsp return err;
1697 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1699 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1700 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1702 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1704 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1707 669b5ffa 2018-10-07 stsp return view->focussed;
1710 bcbd79e2 2018-08-19 stsp static const struct got_error *
1711 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1713 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1714 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1715 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1716 d2366e29 2022-07-07 mark char *mode;
1717 fd823528 2018-10-22 stsp int fast_refresh = 10;
1718 1a76625f 2018-10-22 stsp int done = 0, errcode;
1720 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1721 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1722 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1724 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1726 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1727 1a76625f 2018-10-22 stsp if (errcode)
1728 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1730 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1731 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1733 1004088d 2018-09-29 stsp view->focussed = 1;
1734 878940b7 2018-09-29 stsp err = view->show(view);
1736 0cf4efb1 2018-09-29 stsp return err;
1737 0cf4efb1 2018-09-29 stsp update_panels();
1738 0cf4efb1 2018-09-29 stsp doupdate();
1739 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1740 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1741 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1742 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1743 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1745 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1748 9970f7fc 2020-12-03 stsp if (view->dying) {
1749 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1751 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1752 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1754 e78dc838 2020-12-04 stsp else if (view->parent)
1755 669b5ffa 2018-10-07 stsp prev = view->parent;
1757 e78dc838 2020-12-04 stsp if (view->parent) {
1758 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1759 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1760 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1761 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1762 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1765 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1766 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1768 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1770 9970f7fc 2020-12-03 stsp err = view_close(view);
1772 e5a0f69f 2018-08-18 stsp goto done;
1774 e78dc838 2020-12-04 stsp view = NULL;
1775 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1776 e78dc838 2020-12-04 stsp if (v->focussed)
1779 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1780 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1782 e78dc838 2020-12-04 stsp view = prev;
1783 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1784 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1785 e78dc838 2020-12-04 stsp tog_view_list_head);
1787 e78dc838 2020-12-04 stsp if (view) {
1788 e78dc838 2020-12-04 stsp if (view->focus_child) {
1789 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1790 e78dc838 2020-12-04 stsp view = view->child;
1792 e78dc838 2020-12-04 stsp view->focussed = 1;
1796 bcbd79e2 2018-08-19 stsp if (new_view) {
1797 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1798 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1799 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1800 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1802 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1803 86c66b02 2018-10-18 stsp err = view_close(v);
1805 86c66b02 2018-10-18 stsp goto done;
1808 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1809 fed7eaa8 2018-10-24 stsp view = new_view;
1811 669b5ffa 2018-10-07 stsp if (view) {
1812 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1813 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1814 e78dc838 2020-12-04 stsp view = view->child;
1816 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1817 e78dc838 2020-12-04 stsp view = view->parent;
1819 e78dc838 2020-12-04 stsp show_panel(view->panel);
1820 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1821 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1822 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1823 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1825 1a76625f 2018-10-22 stsp goto done;
1827 669b5ffa 2018-10-07 stsp err = view->show(view);
1829 1a76625f 2018-10-22 stsp goto done;
1830 669b5ffa 2018-10-07 stsp if (view->child) {
1831 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1833 1a76625f 2018-10-22 stsp goto done;
1835 1a76625f 2018-10-22 stsp update_panels();
1836 1a76625f 2018-10-22 stsp doupdate();
1840 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1841 5629093a 2022-07-11 stsp const struct got_error *close_err;
1842 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1843 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1844 5629093a 2022-07-11 stsp close_err = view_close(view);
1845 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1846 5629093a 2022-07-11 stsp err = close_err;
1849 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1850 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1851 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1853 e5a0f69f 2018-08-18 stsp return err;
1856 4ed7e80c 2018-05-20 stsp __dead static void
1857 9f7d7167 2018-04-29 stsp usage_log(void)
1860 c70c5802 2018-08-01 stsp fprintf(stderr,
1861 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1862 9f7d7167 2018-04-29 stsp getprogname());
1866 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1867 80ddbec8 2018-04-29 stsp static const struct got_error *
1868 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1870 00dfcb92 2018-06-11 stsp char *vis = NULL;
1871 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1873 963b370f 2018-05-20 stsp *ws = NULL;
1874 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1875 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1876 00dfcb92 2018-06-11 stsp int vislen;
1877 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1878 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1880 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1881 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1883 00dfcb92 2018-06-11 stsp return err;
1884 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1885 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1886 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1887 a7f50699 2018-06-11 stsp goto done;
1891 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1892 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1893 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1894 a7f50699 2018-06-11 stsp goto done;
1897 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1898 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1900 00dfcb92 2018-06-11 stsp free(vis);
1901 963b370f 2018-05-20 stsp if (err) {
1902 963b370f 2018-05-20 stsp free(*ws);
1903 963b370f 2018-05-20 stsp *ws = NULL;
1904 963b370f 2018-05-20 stsp *wlen = 0;
1906 963b370f 2018-05-20 stsp return err;
1909 145b6838 2022-06-16 stsp static const struct got_error *
1910 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1912 145b6838 2022-06-16 stsp char *dst;
1913 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1915 145b6838 2022-06-16 stsp *ptr = NULL;
1916 145b6838 2022-06-16 stsp n = len = strlen(src);
1917 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1918 145b6838 2022-06-16 stsp if (dst == NULL)
1919 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1921 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1922 145b6838 2022-06-16 stsp const char c = src[idx];
1924 145b6838 2022-06-16 stsp if (c == '\t') {
1925 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1928 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1929 6e1c41ad 2022-06-16 mark if (p == NULL) {
1930 6e1c41ad 2022-06-16 mark free(dst);
1931 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1936 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1939 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1943 145b6838 2022-06-16 stsp dst[sz] = '\0';
1944 145b6838 2022-06-16 stsp *ptr = dst;
1945 145b6838 2022-06-16 stsp return NULL;
1949 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1950 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1951 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1955 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1957 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1959 4e4a9ac8 2022-06-17 op if (n == 0) {
1960 4e4a9ac8 2022-06-17 op *rcol = cols;
1964 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1965 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1966 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1968 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1970 4e4a9ac8 2022-06-17 op if (width == -1) {
1972 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1975 4e4a9ac8 2022-06-17 op if (cols + width > n)
1977 4e4a9ac8 2022-06-17 op cols += width;
1980 4e4a9ac8 2022-06-17 op *rcol = cols;
1985 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1986 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1987 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1989 ccda2f4d 2022-06-16 stsp static const struct got_error *
1990 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1991 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1993 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1995 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1996 145b6838 2022-06-16 stsp char *exstr = NULL;
1997 963b370f 2018-05-20 stsp size_t wlen;
1998 4e4a9ac8 2022-06-17 op int i, scrollx;
2000 963b370f 2018-05-20 stsp *wlinep = NULL;
2001 b700b5d6 2018-07-10 stsp *widthp = 0;
2003 145b6838 2022-06-16 stsp if (expand) {
2004 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2006 145b6838 2022-06-16 stsp return err;
2009 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2010 145b6838 2022-06-16 stsp free(exstr);
2012 963b370f 2018-05-20 stsp return err;
2014 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2016 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2017 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2020 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2021 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2025 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2026 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2028 b700b5d6 2018-07-10 stsp if (widthp)
2029 b700b5d6 2018-07-10 stsp *widthp = cols;
2030 ccda2f4d 2022-06-16 stsp if (scrollxp)
2031 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2033 963b370f 2018-05-20 stsp free(wline);
2035 963b370f 2018-05-20 stsp *wlinep = wline;
2036 963b370f 2018-05-20 stsp return err;
2039 8b473291 2019-02-21 stsp static const struct got_error*
2040 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2041 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2043 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2044 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2046 8b473291 2019-02-21 stsp const char *name;
2048 8b473291 2019-02-21 stsp *refs_str = NULL;
2050 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2051 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2052 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2055 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2056 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2058 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2059 8b473291 2019-02-21 stsp name += 5;
2060 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2061 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2063 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2064 8b473291 2019-02-21 stsp name += 6;
2065 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2066 8b473291 2019-02-21 stsp name += 8;
2067 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2068 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2071 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2074 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2075 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2076 5d844a1e 2019-08-13 stsp if (err) {
2077 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2078 48cae60d 2020-09-22 stsp free(ref_id);
2081 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2082 5d844a1e 2019-08-13 stsp err = NULL;
2083 5d844a1e 2019-08-13 stsp tag = NULL;
2086 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2087 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2088 48cae60d 2020-09-22 stsp free(ref_id);
2090 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2091 52b5abe1 2019-08-13 stsp if (cmp != 0)
2093 8b473291 2019-02-21 stsp s = *refs_str;
2094 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2095 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2096 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2098 8b473291 2019-02-21 stsp *refs_str = NULL;
2104 8b473291 2019-02-21 stsp return err;
2107 963b370f 2018-05-20 stsp static const struct got_error *
2108 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2109 27a741e5 2019-09-11 stsp int col_tab_align)
2111 e6b8b890 2020-12-29 naddy char *smallerthan;
2113 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2114 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2115 5813d178 2019-03-09 stsp author = smallerthan + 1;
2116 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2117 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2118 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2121 5813d178 2019-03-09 stsp static const struct got_error *
2122 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2123 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2124 8fdc79fe 2020-12-01 naddy int author_display_cols)
2126 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2127 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2128 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2129 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2130 5813d178 2019-03-09 stsp char *author = NULL;
2131 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2132 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2133 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2134 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2135 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2136 ccb26ccd 2018-11-05 stsp struct tm tm;
2137 45d799e2 2018-12-23 stsp time_t committer_time;
2138 11b20872 2019-11-08 stsp struct tog_color *tc;
2140 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2141 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2142 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2143 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2144 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2146 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2147 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2149 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2150 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2152 11b20872 2019-11-08 stsp wattr_on(view->window,
2153 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2154 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2156 11b20872 2019-11-08 stsp wattr_off(view->window,
2157 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2158 27a741e5 2019-09-11 stsp col = limit;
2159 b39d25c7 2018-07-10 stsp if (col > avail)
2160 b39d25c7 2018-07-10 stsp goto done;
2162 6570a66d 2019-11-08 stsp if (avail >= 120) {
2163 6570a66d 2019-11-08 stsp char *id_str;
2164 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2166 6570a66d 2019-11-08 stsp goto done;
2167 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2169 11b20872 2019-11-08 stsp wattr_on(view->window,
2170 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2171 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2173 11b20872 2019-11-08 stsp wattr_off(view->window,
2174 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2175 6570a66d 2019-11-08 stsp free(id_str);
2177 6570a66d 2019-11-08 stsp if (col > avail)
2178 6570a66d 2019-11-08 stsp goto done;
2181 10aab77f 2022-07-19 op if (s->use_committer)
2182 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2184 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2185 5813d178 2019-03-09 stsp if (author == NULL) {
2186 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2187 80ddbec8 2018-04-29 stsp goto done;
2189 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2191 bb737323 2018-05-20 stsp goto done;
2192 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2194 11b20872 2019-11-08 stsp wattr_on(view->window,
2195 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2196 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2197 bb737323 2018-05-20 stsp col += author_width;
2198 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2199 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2201 bb737323 2018-05-20 stsp author_width++;
2204 f0f62654 2022-09-08 mark wattr_off(view->window,
2205 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2206 9c2eaf34 2018-05-20 stsp if (col > avail)
2207 9c2eaf34 2018-05-20 stsp goto done;
2209 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2211 6d9fbc00 2018-04-29 stsp goto done;
2212 bb737323 2018-05-20 stsp logmsg = logmsg0;
2213 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2215 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2216 bb737323 2018-05-20 stsp if (newline)
2217 bb737323 2018-05-20 stsp *newline = '\0';
2218 ccda2f4d 2022-06-16 stsp limit = avail - col;
2219 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2220 4d1f6af3 2022-06-17 op limit--; /* for the border */
2221 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2222 ccda2f4d 2022-06-16 stsp limit, col, 1);
2224 29688b02 2022-06-16 stsp goto done;
2225 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2226 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2227 27a741e5 2019-09-11 stsp while (col < avail) {
2228 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2232 80ddbec8 2018-04-29 stsp free(logmsg0);
2233 bb737323 2018-05-20 stsp free(wlogmsg);
2234 5813d178 2019-03-09 stsp free(author);
2235 bb737323 2018-05-20 stsp free(wauthor);
2236 80ddbec8 2018-04-29 stsp free(line);
2237 80ddbec8 2018-04-29 stsp return err;
2240 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2241 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2242 899d86c2 2018-05-10 stsp struct got_object_id *id)
2244 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2245 e15c42de 2022-09-05 op struct got_object_id *dup;
2247 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2248 80ddbec8 2018-04-29 stsp if (entry == NULL)
2249 899d86c2 2018-05-10 stsp return NULL;
2251 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2252 e15c42de 2022-09-05 op if (dup == NULL) {
2253 e15c42de 2022-09-05 op free(entry);
2254 e15c42de 2022-09-05 op return NULL;
2257 e15c42de 2022-09-05 op entry->id = dup;
2258 99db9666 2018-05-07 stsp entry->commit = commit;
2259 899d86c2 2018-05-10 stsp return entry;
2262 99db9666 2018-05-07 stsp static void
2263 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2265 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2267 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2268 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2269 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2270 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2271 e15c42de 2022-09-05 op free(entry->id);
2272 99db9666 2018-05-07 stsp free(entry);
2275 99db9666 2018-05-07 stsp static void
2276 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2278 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2279 99db9666 2018-05-07 stsp pop_commit(commits);
2282 c4972b91 2018-05-07 stsp static const struct got_error *
2283 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2284 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2286 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2287 13add988 2019-10-15 stsp regmatch_t regmatch;
2288 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2290 13add988 2019-10-15 stsp *have_match = 0;
2292 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2294 13add988 2019-10-15 stsp return err;
2296 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2298 13add988 2019-10-15 stsp goto done;
2300 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2301 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2302 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2303 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2304 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2305 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2306 13add988 2019-10-15 stsp *have_match = 1;
2308 13add988 2019-10-15 stsp free(id_str);
2309 13add988 2019-10-15 stsp free(logmsg);
2310 13add988 2019-10-15 stsp return err;
2313 13add988 2019-10-15 stsp static const struct got_error *
2314 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2316 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2319 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2320 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2321 1a76625f 2018-10-22 stsp * while updating the display.
2324 d9787ed8 2022-09-10 op struct got_object_id id;
2325 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2326 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2327 568eae95 2022-09-11 mark int limit_match = 0;
2328 1a76625f 2018-10-22 stsp int errcode;
2330 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2331 4e0d2870 2020-12-07 naddy NULL, NULL);
2335 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2338 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2339 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2340 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2344 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2345 1a76625f 2018-10-22 stsp if (errcode) {
2346 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2347 13add988 2019-10-15 stsp "pthread_mutex_lock");
2351 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2352 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2353 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2355 568eae95 2022-09-11 mark if (*a->limiting) {
2356 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2357 568eae95 2022-09-11 mark a->limit_regex);
2361 568eae95 2022-09-11 mark if (limit_match) {
2362 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2364 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2365 568eae95 2022-09-11 mark entry->commit, entry->id);
2366 568eae95 2022-09-11 mark if (matched == NULL) {
2367 568eae95 2022-09-11 mark err = got_error_from_errno(
2368 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2371 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2372 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2374 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2375 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2376 568eae95 2022-09-11 mark matched, entry);
2377 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2381 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2382 568eae95 2022-09-11 mark * have found a match, and that it should be
2383 568eae95 2022-09-11 mark * counted as a new entry for the view.
2385 568eae95 2022-09-11 mark a->limit_match = limit_match;
2388 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2389 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2390 7c1452c1 2020-03-26 stsp int have_match;
2391 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2395 568eae95 2022-09-11 mark if (*a->limiting) {
2396 568eae95 2022-09-11 mark if (limit_match && have_match)
2397 568eae95 2022-09-11 mark *a->search_next_done =
2398 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2399 568eae95 2022-09-11 mark } else if (have_match)
2400 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2403 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2404 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2405 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2406 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2409 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2411 9ba79e04 2018-06-11 stsp return err;
2414 2b779855 2020-12-05 naddy static void
2415 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2417 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2418 2b779855 2020-12-05 naddy int ncommits = 0;
2420 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2421 2b779855 2020-12-05 naddy while (entry) {
2422 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2423 2b779855 2020-12-05 naddy s->selected_entry = entry;
2426 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2427 2b779855 2020-12-05 naddy ncommits++;
2431 0553a4e3 2018-04-30 stsp static const struct got_error *
2432 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2434 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2435 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2436 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2437 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2438 60493ae3 2019-06-20 stsp int width;
2439 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2440 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2441 8b473291 2019-02-21 stsp char *refs_str = NULL;
2442 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2443 11b20872 2019-11-08 stsp struct tog_color *tc;
2444 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2446 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2447 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2449 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2450 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2451 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2452 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2454 ecb28ae0 2018-07-16 stsp return err;
2455 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2456 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2457 d2075bf3 2020-12-25 stsp if (refs) {
2458 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2459 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2461 d2075bf3 2020-12-25 stsp goto done;
2465 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2466 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2468 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2469 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2470 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2471 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2472 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2473 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2474 8f4ed634 2020-03-26 stsp goto done;
2477 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2478 568eae95 2022-09-11 mark const char *limit_str = NULL;
2480 f9686aa5 2020-03-27 stsp if (view->searching) {
2481 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2482 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2483 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2484 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2485 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2486 f9686aa5 2020-03-27 stsp search_str = "searching...";
2489 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2490 568eae95 2022-09-11 mark limit_str = "no matches found";
2492 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2493 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2494 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2495 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2496 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2497 8f4ed634 2020-03-26 stsp goto done;
2501 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2502 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2503 87411fa9 2022-06-16 stsp "........................................",
2504 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2505 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2506 1a76625f 2018-10-22 stsp header = NULL;
2507 1a76625f 2018-10-22 stsp goto done;
2509 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2510 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2511 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2512 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2513 1a76625f 2018-10-22 stsp header = NULL;
2514 1a76625f 2018-10-22 stsp goto done;
2516 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2518 1a76625f 2018-10-22 stsp goto done;
2520 2814baeb 2018-08-01 stsp werase(view->window);
2522 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2523 a3404814 2018-09-02 stsp wstandout(view->window);
2524 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2526 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2527 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2528 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2529 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2533 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2534 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2535 a3404814 2018-09-02 stsp wstandend(view->window);
2536 ecb28ae0 2018-07-16 stsp free(wline);
2537 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2538 1a76625f 2018-10-22 stsp goto done;
2540 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2541 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2542 5813d178 2019-03-09 stsp ncommits = 0;
2543 145b6838 2022-06-16 stsp view->maxx = 0;
2544 5813d178 2019-03-09 stsp while (entry) {
2545 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2546 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2547 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2548 5813d178 2019-03-09 stsp int width;
2549 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2551 10aab77f 2022-07-19 op if (s->use_committer)
2552 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2554 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2555 5813d178 2019-03-09 stsp if (author == NULL) {
2556 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2557 5813d178 2019-03-09 stsp goto done;
2559 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2560 27a741e5 2019-09-11 stsp date_display_cols);
2561 5813d178 2019-03-09 stsp if (author_cols < width)
2562 5813d178 2019-03-09 stsp author_cols = width;
2563 5813d178 2019-03-09 stsp free(wauthor);
2564 5813d178 2019-03-09 stsp free(author);
2565 a310d9c3 2022-07-21 florian if (err)
2566 a310d9c3 2022-07-21 florian goto done;
2567 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2569 145b6838 2022-06-16 stsp goto done;
2570 145b6838 2022-06-16 stsp msg = msg0;
2571 145b6838 2022-06-16 stsp while (*msg == '\n')
2573 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2574 29688b02 2022-06-16 stsp *eol = '\0';
2575 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2576 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2578 29688b02 2022-06-16 stsp goto done;
2579 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2580 145b6838 2022-06-16 stsp free(msg0);
2581 29688b02 2022-06-16 stsp free(wmsg);
2582 7ca04879 2019-10-19 stsp ncommits++;
2583 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2586 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2587 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2588 867c6645 2018-07-10 stsp ncommits = 0;
2589 899d86c2 2018-05-10 stsp while (entry) {
2590 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2592 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2593 2814baeb 2018-08-01 stsp wstandout(view->window);
2594 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2595 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2596 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2597 2814baeb 2018-08-01 stsp wstandend(view->window);
2599 60493ae3 2019-06-20 stsp goto done;
2600 0553a4e3 2018-04-30 stsp ncommits++;
2601 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2602 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2605 9b058f45 2022-06-30 mark view_border(view);
2607 1a76625f 2018-10-22 stsp free(id_str);
2608 8b473291 2019-02-21 stsp free(refs_str);
2609 1a76625f 2018-10-22 stsp free(ncommits_str);
2610 1a76625f 2018-10-22 stsp free(header);
2611 80ddbec8 2018-04-29 stsp return err;
2614 07b55e75 2018-05-10 stsp static void
2615 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2617 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2618 07b55e75 2018-05-10 stsp int nscrolled = 0;
2620 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2621 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2624 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2625 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2626 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2627 07b55e75 2018-05-10 stsp if (entry) {
2628 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2629 07b55e75 2018-05-10 stsp nscrolled++;
2634 aa075928 2018-05-10 stsp static const struct got_error *
2635 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2637 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2638 5e224a3e 2019-02-22 stsp int errcode;
2640 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2642 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2643 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2644 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2645 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2646 7aafa0d1 2019-02-22 stsp if (errcode)
2647 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2648 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2651 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2652 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2654 7c1452c1 2020-03-26 stsp if (!wait)
2657 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2658 ffe38506 2020-12-01 naddy show_log_view(view);
2659 7c1452c1 2020-03-26 stsp update_panels();
2660 7c1452c1 2020-03-26 stsp doupdate();
2662 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2663 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2664 82954512 2020-02-03 stsp if (errcode)
2665 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2666 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2668 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2669 ffe38506 2020-12-01 naddy show_log_view(view);
2670 7c1452c1 2020-03-26 stsp update_panels();
2671 7c1452c1 2020-03-26 stsp doupdate();
2674 5e224a3e 2019-02-22 stsp return NULL;
2677 5e224a3e 2019-02-22 stsp static const struct got_error *
2678 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2680 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2681 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2683 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2684 2525dccb 2022-07-13 mark return NULL;
2686 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2687 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2688 9b058f45 2022-06-30 mark view->nscrolled = 0;
2690 9b058f45 2022-06-30 mark return err;
2693 9b058f45 2022-06-30 mark static const struct got_error *
2694 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2696 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2697 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2698 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2699 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2701 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2702 5e224a3e 2019-02-22 stsp return NULL;
2704 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2705 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2706 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2708 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2710 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2711 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2712 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2714 5e224a3e 2019-02-22 stsp return err;
2718 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2719 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2722 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2723 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
2725 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2726 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2728 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2729 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2731 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2732 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2734 9b058f45 2022-06-30 mark view->nscrolled = 0;
2736 dd0a52c1 2018-05-20 stsp return err;
2739 cd0acaa7 2018-05-20 stsp static const struct got_error *
2740 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2741 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2742 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2744 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2745 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2746 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2748 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2749 15a94983 2018-12-23 stsp if (diff_view == NULL)
2750 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2752 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2753 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2754 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2755 e5a0f69f 2018-08-18 stsp if (err == NULL)
2756 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2757 cd0acaa7 2018-05-20 stsp return err;
2760 80ddbec8 2018-04-29 stsp static const struct got_error *
2761 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2762 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2764 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2766 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2767 941e9f74 2019-05-21 stsp if (parent == NULL)
2768 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2770 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2771 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2772 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2773 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2774 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2775 941e9f74 2019-05-21 stsp s->tree = subtree;
2776 941e9f74 2019-05-21 stsp s->selected = 0;
2777 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2778 941e9f74 2019-05-21 stsp return NULL;
2781 941e9f74 2019-05-21 stsp static const struct got_error *
2782 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2783 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2785 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2786 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2787 941e9f74 2019-05-21 stsp const char *p;
2788 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2790 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2792 941e9f74 2019-05-21 stsp while (*p) {
2793 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2794 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2795 56e0773d 2019-11-28 stsp char *te_name;
2797 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2800 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2801 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2802 941e9f74 2019-05-21 stsp if (slash == NULL)
2803 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2805 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2806 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2807 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2810 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2811 56e0773d 2019-11-28 stsp if (te == NULL) {
2812 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2813 56e0773d 2019-11-28 stsp free(te_name);
2816 56e0773d 2019-11-28 stsp free(te_name);
2817 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2819 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2820 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2822 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2823 941e9f74 2019-05-21 stsp if (slash)
2824 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2826 941e9f74 2019-05-21 stsp subpath = strdup(path);
2827 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2828 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2832 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2837 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2838 941e9f74 2019-05-21 stsp free(tree_id);
2842 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2843 941e9f74 2019-05-21 stsp if (err) {
2844 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2847 941e9f74 2019-05-21 stsp if (slash == NULL)
2849 941e9f74 2019-05-21 stsp free(subpath);
2850 941e9f74 2019-05-21 stsp subpath = NULL;
2851 941e9f74 2019-05-21 stsp p = slash;
2854 941e9f74 2019-05-21 stsp free(subpath);
2855 1a76625f 2018-10-22 stsp return err;
2858 61266923 2020-01-14 stsp static const struct got_error *
2859 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2860 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2861 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2863 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2864 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2865 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2867 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2868 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2869 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2871 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2873 55cccc34 2020-02-20 stsp return err;
2874 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2876 55cccc34 2020-02-20 stsp *new_view = tree_view;
2878 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2879 55cccc34 2020-02-20 stsp return NULL;
2881 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2884 55cccc34 2020-02-20 stsp static const struct got_error *
2885 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2887 61266923 2020-01-14 stsp sigset_t sigset;
2888 61266923 2020-01-14 stsp int errcode;
2890 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2891 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2893 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2894 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2895 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2896 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2897 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2898 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2899 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2900 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2901 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2903 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2904 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2905 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2907 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2908 61266923 2020-01-14 stsp if (errcode)
2909 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2911 61266923 2020-01-14 stsp return NULL;
2914 1a76625f 2018-10-22 stsp static void *
2915 1a76625f 2018-10-22 stsp log_thread(void *arg)
2917 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2918 1a76625f 2018-10-22 stsp int errcode = 0;
2919 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2920 1a76625f 2018-10-22 stsp int done = 0;
2923 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2924 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2926 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2927 5629093a 2022-07-11 stsp if (errcode) {
2928 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2929 61266923 2020-01-14 stsp return (void *)err;
2932 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2933 5629093a 2022-07-11 stsp if (err) {
2934 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2935 5629093a 2022-07-11 stsp goto done;
2938 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2939 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2940 5629093a 2022-07-11 stsp if (errcode) {
2941 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2942 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2943 5629093a 2022-07-11 stsp goto done;
2945 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2946 1a76625f 2018-10-22 stsp if (err) {
2947 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2948 5629093a 2022-07-11 stsp goto done;
2949 1a76625f 2018-10-22 stsp err = NULL;
2951 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
2952 568eae95 2022-09-11 mark if (*a->limiting) {
2953 568eae95 2022-09-11 mark if (a->limit_match)
2954 568eae95 2022-09-11 mark a->commits_needed--;
2956 568eae95 2022-09-11 mark a->commits_needed--;
2959 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2960 3abe8080 2019-04-10 stsp if (errcode) {
2961 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2962 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2963 5629093a 2022-07-11 stsp goto done;
2964 3abe8080 2019-04-10 stsp } else if (*a->quit)
2966 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
2967 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2968 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
2969 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2970 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
2971 568eae95 2022-09-11 mark *a->first_displayed_entry =
2972 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
2973 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
2976 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2977 7c1452c1 2020-03-26 stsp if (errcode) {
2978 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2979 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2980 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2981 5629093a 2022-07-11 stsp goto done;
2985 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2987 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2988 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2989 7c1452c1 2020-03-26 stsp &tog_mutex);
2990 5629093a 2022-07-11 stsp if (errcode) {
2991 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2992 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2993 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2994 5629093a 2022-07-11 stsp goto done;
2996 21355643 2020-12-06 stsp if (*a->quit)
3001 3abe8080 2019-04-10 stsp a->log_complete = 1;
3002 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3003 5629093a 2022-07-11 stsp if (errcode)
3004 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3006 5629093a 2022-07-11 stsp if (err) {
3007 5629093a 2022-07-11 stsp tog_thread_error = 1;
3008 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3010 1a76625f 2018-10-22 stsp return (void *)err;
3013 1a76625f 2018-10-22 stsp static const struct got_error *
3014 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3016 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3017 1a76625f 2018-10-22 stsp int errcode;
3019 1a76625f 2018-10-22 stsp if (s->thread) {
3020 1a76625f 2018-10-22 stsp s->quit = 1;
3021 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3022 1a76625f 2018-10-22 stsp if (errcode)
3023 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3024 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3025 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3026 1a76625f 2018-10-22 stsp if (errcode)
3027 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3028 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3029 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3030 1a76625f 2018-10-22 stsp if (errcode)
3031 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3032 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3033 1a76625f 2018-10-22 stsp if (errcode)
3034 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3035 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3036 1a76625f 2018-10-22 stsp s->thread = NULL;
3039 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3040 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3041 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3044 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3045 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3046 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3047 74467cc8 2022-06-15 stsp if (err == NULL)
3048 74467cc8 2022-06-15 stsp err = pack_err;
3049 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3052 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3053 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3054 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3057 5629093a 2022-07-11 stsp return err ? err : thread_err;
3060 9343a5fb 2018-06-23 stsp static const struct got_error *
3061 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3063 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3064 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3065 276b94a1 2020-11-13 naddy int errcode;
3067 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3069 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3070 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3071 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3073 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3074 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3075 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3077 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3078 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3079 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3080 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3081 1a76625f 2018-10-22 stsp free(s->start_id);
3082 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3083 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3084 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3085 1a76625f 2018-10-22 stsp return err;
3089 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3090 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3091 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3092 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3093 568eae95 2022-09-11 mark * works with very slight change.
3095 1a76625f 2018-10-22 stsp static const struct got_error *
3096 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3098 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3099 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3100 568eae95 2022-09-11 mark struct tog_view *v = view;
3101 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3102 568eae95 2022-09-11 mark char pattern[1024];
3105 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3106 568eae95 2022-09-11 mark v = view->child;
3107 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3108 568eae95 2022-09-11 mark v = view->parent;
3110 568eae95 2022-09-11 mark /* Get the pattern */
3111 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3112 568eae95 2022-09-11 mark wclrtoeol(v->window);
3113 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3114 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3115 568eae95 2022-09-11 mark nocbreak();
3117 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3120 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3121 568eae95 2022-09-11 mark if (ret == ERR)
3122 568eae95 2022-09-11 mark return NULL;
3124 568eae95 2022-09-11 mark if (*pattern == '\0') {
3126 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3127 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3129 568eae95 2022-09-11 mark if (!s->limit_view)
3130 568eae95 2022-09-11 mark return NULL;
3133 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3134 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3135 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3136 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3137 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3138 568eae95 2022-09-11 mark * the queue.
3140 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3141 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3142 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3143 568eae95 2022-09-11 mark s->selected = 0;
3144 568eae95 2022-09-11 mark s->limit_view = 0;
3146 568eae95 2022-09-11 mark return NULL;
3149 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3150 568eae95 2022-09-11 mark return NULL;
3152 568eae95 2022-09-11 mark s->limit_view = 1;
3154 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3155 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3156 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3157 568eae95 2022-09-11 mark s->selected_entry = NULL;
3158 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3160 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3161 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3162 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3164 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3165 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3166 568eae95 2022-09-11 mark int have_match = 0;
3168 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3169 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3171 568eae95 2022-09-11 mark return err;
3173 568eae95 2022-09-11 mark if (have_match) {
3174 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3176 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3177 568eae95 2022-09-11 mark entry->id);
3178 568eae95 2022-09-11 mark if (matched == NULL) {
3179 568eae95 2022-09-11 mark err = got_error_from_errno(
3180 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3183 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3184 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3186 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3187 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3188 568eae95 2022-09-11 mark matched, entry);
3189 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3193 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3194 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3195 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3196 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3197 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3198 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3200 568eae95 2022-09-11 mark return err;
3203 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3204 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3205 568eae95 2022-09-11 mark s->selected = 0;
3207 568eae95 2022-09-11 mark return NULL;
3210 568eae95 2022-09-11 mark static const struct got_error *
3211 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3213 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3215 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3216 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3217 60493ae3 2019-06-20 stsp return NULL;
3220 60493ae3 2019-06-20 stsp static const struct got_error *
3221 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3223 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3224 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3225 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3227 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3228 f9686aa5 2020-03-27 stsp show_log_view(view);
3229 f9686aa5 2020-03-27 stsp update_panels();
3230 f9686aa5 2020-03-27 stsp doupdate();
3232 96e2b566 2019-07-08 stsp if (s->search_entry) {
3233 13add988 2019-10-15 stsp int errcode, ch;
3234 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3235 13add988 2019-10-15 stsp if (errcode)
3236 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3237 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3238 13add988 2019-10-15 stsp ch = wgetch(view->window);
3239 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3240 13add988 2019-10-15 stsp if (errcode)
3241 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3242 13add988 2019-10-15 stsp "pthread_mutex_lock");
3243 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3244 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3245 678cbce5 2019-07-28 stsp return NULL;
3247 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3248 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3250 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3251 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3252 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3254 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3255 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3256 f704b35c 2022-06-18 stsp * might have changed.
3258 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3259 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3261 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3264 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3267 60493ae3 2019-06-20 stsp while (1) {
3268 13add988 2019-10-15 stsp int have_match = 0;
3270 60493ae3 2019-06-20 stsp if (entry == NULL) {
3271 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3272 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3273 f9967bca 2020-03-27 stsp view->search_next_done =
3274 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3275 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3276 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3277 f801134a 2019-06-25 stsp return NULL;
3280 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3281 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3282 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3284 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3285 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3288 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3289 13add988 2019-10-15 stsp &view->regex);
3292 13add988 2019-10-15 stsp if (have_match) {
3293 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3294 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3298 96e2b566 2019-07-08 stsp s->search_entry = entry;
3299 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3300 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3302 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3305 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3306 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3307 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3308 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3310 60493ae3 2019-06-20 stsp return err;
3313 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3314 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3316 60493ae3 2019-06-20 stsp return err;
3321 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3323 60493ae3 2019-06-20 stsp return NULL;
3326 60493ae3 2019-06-20 stsp static const struct got_error *
3327 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3328 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3329 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3331 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3332 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3333 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3334 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3335 1a76625f 2018-10-22 stsp int errcode;
3337 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3338 f135c941 2020-02-20 stsp free(s->in_repo_path);
3339 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3340 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3341 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3344 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3345 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3346 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3347 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3349 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3350 568eae95 2022-09-11 mark s->limit_view = 0;
3351 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3353 fb2756b9 2018-08-04 stsp s->repo = repo;
3354 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3355 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3356 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3357 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3358 9cd7cbd1 2020-12-07 stsp goto done;
3361 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3362 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3363 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3364 5036bf37 2018-09-24 stsp goto done;
3366 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3367 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3369 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3370 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3371 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3372 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3374 11b20872 2019-11-08 stsp goto done;
3375 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3376 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3377 11b20872 2019-11-08 stsp if (err) {
3378 11b20872 2019-11-08 stsp free_colors(&s->colors);
3379 11b20872 2019-11-08 stsp goto done;
3381 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3382 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3383 11b20872 2019-11-08 stsp if (err) {
3384 11b20872 2019-11-08 stsp free_colors(&s->colors);
3385 11b20872 2019-11-08 stsp goto done;
3389 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3390 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3391 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3392 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3393 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3394 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3396 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3397 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3399 74467cc8 2022-06-15 stsp goto done;
3401 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3402 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3403 0ae84acc 2022-06-15 tracey if (err)
3404 0ae84acc 2022-06-15 tracey goto done;
3405 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3406 b672a97a 2020-01-27 stsp !s->log_branches);
3408 1a76625f 2018-10-22 stsp goto done;
3409 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3410 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3412 c5b78334 2020-01-12 stsp goto done;
3414 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3415 1a76625f 2018-10-22 stsp if (errcode) {
3416 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3417 1a76625f 2018-10-22 stsp goto done;
3419 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3420 7c1452c1 2020-03-26 stsp if (errcode) {
3421 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3422 7c1452c1 2020-03-26 stsp goto done;
3425 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3426 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3427 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3428 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3429 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3430 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3431 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3432 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3433 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3434 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3435 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3436 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3437 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3438 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3439 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3440 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3441 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3444 1a76625f 2018-10-22 stsp close_log_view(view);
3445 ba4f502b 2018-08-04 stsp return err;
3448 e5a0f69f 2018-08-18 stsp static const struct got_error *
3449 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3451 f2f6d207 2020-11-24 stsp const struct got_error *err;
3452 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3454 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3455 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3456 2b380cc8 2018-10-24 stsp &s->thread_args);
3457 2b380cc8 2018-10-24 stsp if (errcode)
3458 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3459 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3460 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3462 f2f6d207 2020-11-24 stsp return err;
3466 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3469 b880cc75 2022-06-30 mark static void
3470 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3472 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3474 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3476 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3477 568eae95 2022-09-11 mark view->count = 0;
3479 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3481 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3483 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3484 b880cc75 2022-06-30 mark --s->selected;
3486 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3488 b880cc75 2022-06-30 mark select_commit(s);
3492 b880cc75 2022-06-30 mark static const struct got_error *
3493 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3495 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3496 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3497 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3499 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3500 568eae95 2022-09-11 mark return NULL;
3502 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3503 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3504 b880cc75 2022-06-30 mark return NULL;
3506 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3507 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3509 631e7531 2022-08-12 mark if (!page) {
3510 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3511 b880cc75 2022-06-30 mark ++s->selected;
3513 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3514 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3515 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3518 631e7531 2022-08-12 mark s->selected = 0;
3519 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3520 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3521 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3522 631e7531 2022-08-12 mark if (entry == NULL)
3524 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3525 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3527 631e7531 2022-08-12 mark if (n > 0)
3528 631e7531 2022-08-12 mark s->selected = n - 1;
3530 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3531 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3532 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3533 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3535 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3538 b880cc75 2022-06-30 mark return err;
3541 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3542 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3544 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3545 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3546 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3547 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3550 b880cc75 2022-06-30 mark select_commit(s);
3552 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3553 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3554 b880cc75 2022-06-30 mark view->count = 0;
3556 b880cc75 2022-06-30 mark return NULL;
3559 9b058f45 2022-06-30 mark static void
3560 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3565 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3566 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3567 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3568 7532ccda 2022-07-11 mark else if (view->resized_y)
3569 7532ccda 2022-07-11 mark *y = view->resized_y;
3571 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3572 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3573 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3574 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3575 7532ccda 2022-07-11 mark else if (view->resized_x)
3576 7532ccda 2022-07-11 mark *x = view->resized_x;
3578 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3582 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3583 e5a0f69f 2018-08-18 stsp static const struct got_error *
3584 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3586 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3588 9b058f45 2022-06-30 mark view->nlines = y;
3589 d2366e29 2022-07-07 mark view->ncols = COLS;
3590 9b058f45 2022-06-30 mark err = view_resize(view);
3592 9b058f45 2022-06-30 mark return err;
3594 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3596 9b058f45 2022-06-30 mark return err;
3599 94b80cfa 2022-08-01 mark static const struct got_error *
3600 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3602 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3603 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3604 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3606 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3607 374f69dd 2022-08-13 stsp return NULL;
3609 94b80cfa 2022-08-01 mark g = view->gline;
3610 94b80cfa 2022-08-01 mark view->gline = 0;
3612 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3613 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3614 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3615 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3616 94b80cfa 2022-08-01 mark select_commit(s);
3617 94b80cfa 2022-08-01 mark return NULL;
3620 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3621 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3622 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3623 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3624 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3626 94b80cfa 2022-08-01 mark return err;
3627 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3628 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3630 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3631 94b80cfa 2022-08-01 mark s->selected = g - 1;
3633 94b80cfa 2022-08-01 mark select_commit(s);
3634 94b80cfa 2022-08-01 mark return NULL;
3638 9b058f45 2022-06-30 mark static const struct got_error *
3639 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3641 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3642 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3643 631e7531 2022-08-12 mark int eos, nscroll;
3645 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3646 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3647 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3648 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3649 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3650 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3653 11edf34c 2022-08-12 mark return err;
3656 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3657 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3658 0dca135e 2022-06-30 mark --eos; /* border */
3660 94b80cfa 2022-08-01 mark if (view->gline)
3661 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3663 528dedf3 2021-08-30 stsp switch (ch) {
3665 568eae95 2022-09-11 mark err = limit_log_view(view);
3668 1e37a5c2 2019-05-12 jcs s->quit = 1;
3671 145b6838 2022-06-16 stsp view->x = 0;
3674 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3675 640cd7ff 2022-06-22 mark view->count = 0;
3677 145b6838 2022-06-16 stsp case KEY_RIGHT:
3679 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3680 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3682 640cd7ff 2022-06-22 mark view->count = 0;
3684 145b6838 2022-06-16 stsp case KEY_LEFT:
3686 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3687 640cd7ff 2022-06-22 mark if (view->x <= 0)
3688 640cd7ff 2022-06-22 mark view->count = 0;
3691 1e37a5c2 2019-05-12 jcs case KEY_UP:
3694 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3695 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3699 912a3f79 2021-08-30 j case KEY_HOME:
3700 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3701 640cd7ff 2022-06-22 mark view->count = 0;
3703 83cc4199 2022-06-13 stsp case CTRL('u'):
3705 83cc4199 2022-06-13 stsp nscroll /= 2;
3706 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3707 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3708 a4292ac5 2019-05-12 jcs case CTRL('b'):
3710 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3713 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3716 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3717 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3720 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3724 912a3f79 2021-08-30 j case KEY_END: {
3725 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3726 912a3f79 2021-08-30 j * traverse them all. */
3727 640cd7ff 2022-06-22 mark view->count = 0;
3728 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3729 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3730 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3731 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3732 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3735 80b7e8da 2022-06-11 stsp case CTRL('d'):
3737 83cc4199 2022-06-13 stsp nscroll /= 2;
3738 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3739 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3740 61417565 2022-06-20 mark case CTRL('f'):
3741 48bb96f0 2022-06-20 naddy case 'f':
3743 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3745 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3746 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3747 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3748 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3749 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3750 2b779855 2020-12-05 naddy select_commit(s);
3751 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3752 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3753 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3754 568eae95 2022-09-11 mark s->commits->ncommits;
3755 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3758 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3759 49b24ee5 2022-07-03 mark case '\r':
3760 640cd7ff 2022-06-22 mark view->count = 0;
3761 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3763 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3766 640cd7ff 2022-06-22 mark view->count = 0;
3767 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3769 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3771 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3772 21355643 2020-12-06 stsp case CTRL('l'):
3774 640cd7ff 2022-06-22 mark view->count = 0;
3775 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3776 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3778 21355643 2020-12-06 stsp err = stop_log_thread(s);
3780 74cfe85e 2020-10-20 stsp return err;
3781 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3782 21355643 2020-12-06 stsp char *parent_path;
3783 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3785 21355643 2020-12-06 stsp return err;
3786 21355643 2020-12-06 stsp free(s->in_repo_path);
3787 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3788 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3789 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3790 21355643 2020-12-06 stsp struct got_object_id *start_id;
3791 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3792 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3793 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3794 ea371198 2022-11-17 stsp if (err) {
3795 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
3796 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
3797 ea371198 2022-11-17 stsp return err;
3798 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
3799 ea371198 2022-11-17 stsp free(s->head_ref_name);
3800 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
3801 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
3802 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3803 ea371198 2022-11-17 stsp &tog_refs, s->repo);
3805 ea371198 2022-11-17 stsp return err;
3807 21355643 2020-12-06 stsp free(s->start_id);
3808 21355643 2020-12-06 stsp s->start_id = start_id;
3809 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3810 21355643 2020-12-06 stsp } else /* 'B' */
3811 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3813 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3814 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3816 b0dd8d27 2022-06-16 stsp return err;
3818 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3819 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3820 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3822 21355643 2020-12-06 stsp return err;
3823 51a10b52 2020-12-26 stsp tog_free_refs();
3824 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3826 ca51c541 2020-12-07 stsp return err;
3827 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3828 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3830 d01904d4 2019-06-25 stsp return err;
3831 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3832 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3834 b672a97a 2020-01-27 stsp return err;
3835 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3836 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3837 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3838 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3839 21355643 2020-12-06 stsp s->selected_entry = NULL;
3840 21355643 2020-12-06 stsp s->selected = 0;
3841 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3842 21355643 2020-12-06 stsp s->quit = 0;
3843 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3844 dfee752e 2022-06-18 op s->matched_entry = NULL;
3845 dfee752e 2022-06-18 op s->search_entry = NULL;
3846 01a7bcaf 2022-07-22 mark view->offset = 0;
3849 640cd7ff 2022-06-22 mark view->count = 0;
3850 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3853 640cd7ff 2022-06-22 mark view->count = 0;
3857 80ddbec8 2018-04-29 stsp return err;
3860 4ed7e80c 2018-05-20 stsp static const struct got_error *
3861 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3863 c2db6724 2019-01-04 stsp const struct got_error *error;
3865 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3866 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3867 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3869 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3870 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3872 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3873 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3875 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3876 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3878 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3879 c2db6724 2019-01-04 stsp if (error != NULL)
3880 c2db6724 2019-01-04 stsp return error;
3882 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3883 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3885 c2db6724 2019-01-04 stsp return NULL;
3888 a915003a 2019-02-05 stsp static void
3889 a915003a 2019-02-05 stsp init_curses(void)
3892 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3893 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3894 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3896 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3897 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3898 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3899 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3900 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3902 a915003a 2019-02-05 stsp initscr();
3904 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3907 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3908 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3909 a915003a 2019-02-05 stsp curs_set(0);
3910 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3911 6d17833f 2019-11-08 stsp start_color();
3912 6d17833f 2019-11-08 stsp use_default_colors();
3916 c2db6724 2019-01-04 stsp static const struct got_error *
3917 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3918 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3920 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3922 f135c941 2020-02-20 stsp if (argc == 0) {
3923 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3924 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3925 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3926 f135c941 2020-02-20 stsp return NULL;
3929 f135c941 2020-02-20 stsp if (worktree) {
3930 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3933 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3935 f135c941 2020-02-20 stsp return err;
3936 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3937 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3938 bfd61697 2020-11-14 stsp p) == -1) {
3939 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3940 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3944 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3946 f135c941 2020-02-20 stsp return err;
3949 f135c941 2020-02-20 stsp static const struct got_error *
3950 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3952 80ddbec8 2018-04-29 stsp const struct got_error *error;
3953 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3954 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3955 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3956 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3957 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3958 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3959 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3960 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3961 04cc582a 2018-08-01 stsp struct tog_view *view;
3962 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3964 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3965 80ddbec8 2018-04-29 stsp switch (ch) {
3967 b672a97a 2020-01-27 stsp log_branches = 1;
3970 80ddbec8 2018-04-29 stsp start_commit = optarg;
3973 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3974 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3975 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3979 17020d27 2019-03-07 stsp usage_log();
3980 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3984 80ddbec8 2018-04-29 stsp argc -= optind;
3985 80ddbec8 2018-04-29 stsp argv += optind;
3987 f135c941 2020-02-20 stsp if (argc > 1)
3988 f135c941 2020-02-20 stsp usage_log();
3990 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3991 0ae84acc 2022-06-15 tracey if (error != NULL)
3992 0ae84acc 2022-06-15 tracey goto done;
3994 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3995 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3996 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3997 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3998 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3999 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4000 c156c7a4 2020-12-18 stsp goto done;
4001 a1fbf39a 2019-08-11 stsp if (worktree)
4002 6962eb72 2020-02-20 stsp repo_path =
4003 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4005 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4006 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4007 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4008 c156c7a4 2020-12-18 stsp goto done;
4012 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4013 80ddbec8 2018-04-29 stsp if (error != NULL)
4014 ecb28ae0 2018-07-16 stsp goto done;
4016 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4017 f135c941 2020-02-20 stsp repo, worktree);
4018 f135c941 2020-02-20 stsp if (error)
4019 f135c941 2020-02-20 stsp goto done;
4021 f135c941 2020-02-20 stsp init_curses();
4023 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4024 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4025 c02c541e 2019-03-29 stsp if (error)
4026 c02c541e 2019-03-29 stsp goto done;
4028 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4029 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4030 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4031 87670572 2020-12-26 naddy if (error)
4032 87670572 2020-12-26 naddy goto done;
4035 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4036 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4037 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4038 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4039 d8f38dc4 2020-12-05 stsp if (error)
4040 d8f38dc4 2020-12-05 stsp goto done;
4041 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4043 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4044 d8f38dc4 2020-12-05 stsp if (error == NULL)
4045 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4046 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4047 d8f38dc4 2020-12-05 stsp goto done;
4048 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4049 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4050 d8f38dc4 2020-12-05 stsp if (error)
4051 d8f38dc4 2020-12-05 stsp goto done;
4054 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4055 04cc582a 2018-08-01 stsp if (view == NULL) {
4056 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4057 04cc582a 2018-08-01 stsp goto done;
4059 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4060 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4061 ba4f502b 2018-08-04 stsp if (error)
4062 ba4f502b 2018-08-04 stsp goto done;
4063 2fc00ff4 2019-08-31 stsp if (worktree) {
4064 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4065 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4066 2fc00ff4 2019-08-31 stsp worktree = NULL;
4068 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4070 f135c941 2020-02-20 stsp free(in_repo_path);
4071 ecb28ae0 2018-07-16 stsp free(repo_path);
4072 ecb28ae0 2018-07-16 stsp free(cwd);
4073 899d86c2 2018-05-10 stsp free(start_id);
4074 d8f38dc4 2020-12-05 stsp free(label);
4076 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4077 1d0f4054 2021-06-17 stsp if (repo) {
4078 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4079 1d0f4054 2021-06-17 stsp if (error == NULL)
4080 1d0f4054 2021-06-17 stsp error = close_err;
4082 ec142235 2019-03-07 stsp if (worktree)
4083 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4084 0ae84acc 2022-06-15 tracey if (pack_fds) {
4085 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4086 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4087 0ae84acc 2022-06-15 tracey if (error == NULL)
4088 0ae84acc 2022-06-15 tracey error = pack_err;
4090 51a10b52 2020-12-26 stsp tog_free_refs();
4091 80ddbec8 2018-04-29 stsp return error;
4094 4ed7e80c 2018-05-20 stsp __dead static void
4095 9f7d7167 2018-04-29 stsp usage_diff(void)
4098 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4099 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4103 6d17833f 2019-11-08 stsp static int
4104 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4105 41605754 2020-11-12 stsp regmatch_t *regmatch)
4107 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4110 336075a4 2022-06-25 op static struct tog_color *
4111 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4113 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4115 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4116 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4117 6d17833f 2019-11-08 stsp return tc;
4120 6d17833f 2019-11-08 stsp return NULL;
4123 4ed7e80c 2018-05-20 stsp static const struct got_error *
4124 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4125 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4127 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4128 44a87665 2022-06-16 stsp char *exstr = NULL;
4129 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4130 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4131 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4132 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4134 41605754 2020-11-12 stsp *wtotal = 0;
4136 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4137 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4139 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4141 44a87665 2022-06-16 stsp return err;
4143 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4144 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4145 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4146 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4147 44a87665 2022-06-16 stsp goto done;
4149 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4150 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4151 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4152 1853e0f4 2022-06-16 stsp goto done;
4154 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4155 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4156 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4157 1853e0f4 2022-06-16 stsp goto done;
4160 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4161 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4162 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4164 1853e0f4 2022-06-16 stsp goto done;
4165 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4167 1853e0f4 2022-06-16 stsp free(wline);
4168 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4169 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4171 1853e0f4 2022-06-16 stsp goto done;
4172 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4173 1853e0f4 2022-06-16 stsp wlimit -= width;
4174 1853e0f4 2022-06-16 stsp *wtotal += width;
4177 41605754 2020-11-12 stsp if (wlimit > 0) {
4178 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4179 1853e0f4 2022-06-16 stsp size_t wlen;
4181 1853e0f4 2022-06-16 stsp free(wline);
4182 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4183 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4185 1853e0f4 2022-06-16 stsp goto done;
4186 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4187 1853e0f4 2022-06-16 stsp while (i < wlen) {
4188 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4189 1853e0f4 2022-06-16 stsp if (width == -1) {
4190 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4191 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4192 1853e0f4 2022-06-16 stsp goto done;
4194 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4196 61417565 2022-06-20 mark w += width;
4199 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4200 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4201 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4202 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4203 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4204 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4205 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4209 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4210 1853e0f4 2022-06-16 stsp free(wline);
4211 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4212 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4213 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4214 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4216 1853e0f4 2022-06-16 stsp goto done;
4217 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4219 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4220 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4222 1853e0f4 2022-06-16 stsp goto done;
4223 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4225 1853e0f4 2022-06-16 stsp *wtotal += width2;
4228 145b6838 2022-06-16 stsp free(wline);
4229 44a87665 2022-06-16 stsp free(exstr);
4230 1853e0f4 2022-06-16 stsp free(seg0);
4231 1853e0f4 2022-06-16 stsp free(seg1);
4232 1853e0f4 2022-06-16 stsp free(seg2);
4233 1853e0f4 2022-06-16 stsp return err;
4236 94b80cfa 2022-08-01 mark static int
4237 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4239 94b80cfa 2022-08-01 mark FILE *f = NULL;
4240 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4242 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4243 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4245 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4246 ec2a9698 2022-09-15 mark selected = first;
4247 ec2a9698 2022-09-15 mark eof = &s->eof;
4249 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4250 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4252 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4253 94b80cfa 2022-08-01 mark selected = first;
4254 94b80cfa 2022-08-01 mark eof = &s->eof;
4256 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4257 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4259 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4260 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4261 94b80cfa 2022-08-01 mark eof = &s->eof;
4262 94b80cfa 2022-08-01 mark f = s->blame.f;
4266 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4267 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4269 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4270 94b80cfa 2022-08-01 mark rewind(f);
4272 94b80cfa 2022-08-01 mark *first = 1;
4273 94b80cfa 2022-08-01 mark *lineno = 0;
4274 94b80cfa 2022-08-01 mark *nprinted = 0;
4278 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4279 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4280 94b80cfa 2022-08-01 mark view->gline = 0;
4285 41605754 2020-11-12 stsp static const struct got_error *
4286 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4288 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4289 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4290 61e69b96 2018-05-20 stsp const struct got_error *err;
4291 c7d5c43c 2022-08-04 mark int nprinted = 0;
4292 b304db33 2018-05-20 stsp char *line;
4293 826082fe 2020-12-10 stsp size_t linesize = 0;
4294 826082fe 2020-12-10 stsp ssize_t linelen;
4295 61e69b96 2018-05-20 stsp wchar_t *wline;
4296 e0b650dd 2018-05-20 stsp int width;
4297 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4298 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4299 fe621944 2020-11-10 stsp off_t line_offset;
4301 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4302 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4303 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4304 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4306 f7d12f7e 2018-08-01 stsp werase(view->window);
4308 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4309 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4311 a3404814 2018-09-02 stsp if (header) {
4312 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4313 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4314 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4316 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4317 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4318 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4320 135a2da0 2020-11-11 stsp free(line);
4322 a3404814 2018-09-02 stsp return err;
4324 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4325 a3404814 2018-09-02 stsp wstandout(view->window);
4326 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4327 e54cc94a 2020-11-11 stsp free(wline);
4328 e54cc94a 2020-11-11 stsp wline = NULL;
4329 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4330 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4331 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4332 a3404814 2018-09-02 stsp wstandend(view->window);
4334 a3404814 2018-09-02 stsp if (max_lines <= 1)
4335 a3404814 2018-09-02 stsp return NULL;
4336 a3404814 2018-09-02 stsp max_lines--;
4339 89f1a395 2020-12-01 naddy s->eof = 0;
4340 145b6838 2022-06-16 stsp view->maxx = 0;
4341 826082fe 2020-12-10 stsp line = NULL;
4342 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4343 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4344 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4346 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4347 826082fe 2020-12-10 stsp if (linelen == -1) {
4348 826082fe 2020-12-10 stsp if (feof(s->f)) {
4349 826082fe 2020-12-10 stsp s->eof = 1;
4352 826082fe 2020-12-10 stsp free(line);
4353 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4356 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4358 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4360 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4361 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4363 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4364 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4365 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4366 1853e0f4 2022-06-16 stsp if (err) {
4367 1853e0f4 2022-06-16 stsp free(line);
4368 1853e0f4 2022-06-16 stsp return err;
4370 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4371 1853e0f4 2022-06-16 stsp free(wline);
4372 1853e0f4 2022-06-16 stsp wline = NULL;
4374 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4375 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4376 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4377 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4379 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4380 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4381 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4382 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4383 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4384 41605754 2020-11-12 stsp if (err) {
4385 41605754 2020-11-12 stsp free(line);
4386 41605754 2020-11-12 stsp return err;
4390 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4391 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4392 969c159c 2022-06-16 stsp if (err) {
4393 969c159c 2022-06-16 stsp free(line);
4394 969c159c 2022-06-16 stsp return err;
4396 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4397 969c159c 2022-06-16 stsp free(wline);
4398 41605754 2020-11-12 stsp wline = NULL;
4400 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4401 94b80cfa 2022-08-01 mark /* highlight full gline length */
4402 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4403 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4405 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4406 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4409 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4410 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4411 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4413 826082fe 2020-12-10 stsp free(line);
4414 fe621944 2020-11-10 stsp if (nprinted >= 1)
4415 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4416 89f1a395 2020-12-01 naddy (nprinted - 1);
4418 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4420 9b058f45 2022-06-30 mark view_border(view);
4422 89f1a395 2020-12-01 naddy if (s->eof) {
4423 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4424 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4425 c3e9aa98 2019-05-13 jcs nprinted++;
4428 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4429 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4431 c3e9aa98 2019-05-13 jcs return err;
4434 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4435 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4436 e54cc94a 2020-11-11 stsp free(wline);
4437 e54cc94a 2020-11-11 stsp wline = NULL;
4438 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4441 26ed57b2 2018-05-19 stsp return NULL;
4444 abd2672a 2018-12-23 stsp static char *
4445 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4447 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4448 09867e48 2019-08-13 stsp char *p, *s;
4450 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4451 09867e48 2019-08-13 stsp if (tm == NULL)
4452 09867e48 2019-08-13 stsp return NULL;
4453 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4454 09867e48 2019-08-13 stsp if (s == NULL)
4455 09867e48 2019-08-13 stsp return NULL;
4456 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4458 abd2672a 2018-12-23 stsp *p = '\0';
4462 4ed7e80c 2018-05-20 stsp static const struct got_error *
4463 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4464 5191b70b 2023-01-07 mark struct got_commit_object *commit, struct got_repository *repo,
4465 5191b70b 2023-01-07 mark struct got_diffstat_cb_arg *dsa)
4467 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4468 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4469 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4470 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4471 5191b70b 2023-01-07 mark FILE *f1 = NULL, *f2 = NULL;
4472 5191b70b 2023-01-07 mark int fd1 = -1, fd2 = -1;
4474 5191b70b 2023-01-07 mark f1 = got_opentemp();
4475 5191b70b 2023-01-07 mark if (f1 == NULL) {
4476 5191b70b 2023-01-07 mark err = got_error_from_errno("got_opentemp");
4477 5191b70b 2023-01-07 mark goto done;
4479 5191b70b 2023-01-07 mark f2 = got_opentemp();
4480 5191b70b 2023-01-07 mark if (f2 == NULL) {
4481 5191b70b 2023-01-07 mark err = got_error_from_errno("got_opentemp");
4482 5191b70b 2023-01-07 mark goto done;
4485 5191b70b 2023-01-07 mark fd1 = got_opentempfd();
4486 5191b70b 2023-01-07 mark if (fd1 == -1) {
4487 5191b70b 2023-01-07 mark err = got_error_from_errno("got_opentempfd");
4488 5191b70b 2023-01-07 mark goto done;
4490 5191b70b 2023-01-07 mark fd2 = got_opentempfd();
4491 5191b70b 2023-01-07 mark if (fd2 == -1) {
4492 5191b70b 2023-01-07 mark err = got_error_from_errno("got_opentempfd");
4493 5191b70b 2023-01-07 mark goto done;
4496 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4497 0208f208 2020-05-05 stsp if (qid != NULL) {
4498 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4499 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4500 d7b5a0e8 2022-04-20 stsp &qid->id);
4502 0208f208 2020-05-05 stsp return err;
4504 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4505 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4506 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4507 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4508 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4510 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4514 0208f208 2020-05-05 stsp if (tree_id1) {
4515 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4517 0208f208 2020-05-05 stsp goto done;
4520 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4521 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4523 0208f208 2020-05-05 stsp goto done;
4525 5191b70b 2023-01-07 mark err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
4526 5191b70b 2023-01-07 mark got_diff_tree_compute_diffstat, dsa, 1);
4528 0208f208 2020-05-05 stsp if (tree1)
4529 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4530 0208f208 2020-05-05 stsp if (tree2)
4531 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4532 5191b70b 2023-01-07 mark if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4533 5191b70b 2023-01-07 mark err = got_error_from_errno("close");
4534 5191b70b 2023-01-07 mark if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4535 5191b70b 2023-01-07 mark err = got_error_from_errno("close");
4536 5191b70b 2023-01-07 mark if (f1 && fclose(f1) == EOF && err == NULL)
4537 5191b70b 2023-01-07 mark err = got_error_from_errno("fclose");
4538 5191b70b 2023-01-07 mark if (f2 && fclose(f2) == EOF && err == NULL)
4539 5191b70b 2023-01-07 mark err = got_error_from_errno("fclose");
4540 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4541 0208f208 2020-05-05 stsp return err;
4544 0208f208 2020-05-05 stsp static const struct got_error *
4545 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4546 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4548 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4550 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4551 fe621944 2020-11-10 stsp if (p == NULL)
4552 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4553 c7d5c43c 2022-08-04 mark *lines = p;
4554 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4555 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4556 fe621944 2020-11-10 stsp (*nlines)++;
4558 fe621944 2020-11-10 stsp return NULL;
4561 fe621944 2020-11-10 stsp static const struct got_error *
4562 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4563 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4564 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4565 5191b70b 2023-01-07 mark FILE *outfile)
4567 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4568 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4569 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4570 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4571 45d799e2 2018-12-23 stsp time_t committer_time;
4572 45d799e2 2018-12-23 stsp const char *author, *committer;
4573 8b473291 2019-02-21 stsp char *refs_str = NULL;
4574 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4575 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4576 5191b70b 2023-01-07 mark struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0, &changed_paths,
4577 5191b70b 2023-01-07 mark ignore_ws, force_text_diff, tog_diff_algo };
4578 fe621944 2020-11-10 stsp off_t outoff = 0;
4581 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4583 8b473291 2019-02-21 stsp if (refs) {
4584 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4586 8b473291 2019-02-21 stsp return err;
4589 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4591 abd2672a 2018-12-23 stsp return err;
4593 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4594 15a94983 2018-12-23 stsp if (err) {
4595 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4596 15a94983 2018-12-23 stsp goto done;
4599 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4601 fe621944 2020-11-10 stsp goto done;
4603 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4604 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4605 fe621944 2020-11-10 stsp if (n < 0) {
4606 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4607 abd2672a 2018-12-23 stsp goto done;
4609 fe621944 2020-11-10 stsp outoff += n;
4610 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4612 fe621944 2020-11-10 stsp goto done;
4614 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4615 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4616 fe621944 2020-11-10 stsp if (n < 0) {
4617 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4618 abd2672a 2018-12-23 stsp goto done;
4620 fe621944 2020-11-10 stsp outoff += n;
4621 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4623 fe621944 2020-11-10 stsp goto done;
4625 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4626 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4627 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4628 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4629 fe621944 2020-11-10 stsp if (n < 0) {
4630 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4631 fe621944 2020-11-10 stsp goto done;
4633 fe621944 2020-11-10 stsp outoff += n;
4634 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4635 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4637 fe621944 2020-11-10 stsp goto done;
4639 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4640 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4641 1a99e0b4 2023-01-07 stsp if (datestr) {
4642 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4643 fe621944 2020-11-10 stsp if (n < 0) {
4644 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4645 fe621944 2020-11-10 stsp goto done;
4647 fe621944 2020-11-10 stsp outoff += n;
4648 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4649 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4651 fe621944 2020-11-10 stsp goto done;
4653 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4654 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4655 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4656 9f98ca05 2021-09-24 stsp int pn = 1;
4657 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4658 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4659 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4661 9f98ca05 2021-09-24 stsp goto done;
4662 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4663 9f98ca05 2021-09-24 stsp if (n < 0) {
4664 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4665 9f98ca05 2021-09-24 stsp goto done;
4667 9f98ca05 2021-09-24 stsp outoff += n;
4668 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4669 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4671 9f98ca05 2021-09-24 stsp goto done;
4672 9f98ca05 2021-09-24 stsp free(id_str);
4673 9f98ca05 2021-09-24 stsp id_str = NULL;
4677 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4679 5943eee2 2019-08-13 stsp goto done;
4680 fe621944 2020-11-10 stsp s = logmsg;
4681 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4682 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4683 fe621944 2020-11-10 stsp if (n < 0) {
4684 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4685 fe621944 2020-11-10 stsp goto done;
4687 fe621944 2020-11-10 stsp outoff += n;
4688 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4689 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4691 fe621944 2020-11-10 stsp goto done;
4694 5191b70b 2023-01-07 mark err = get_changed_paths(&changed_paths, commit, repo, &dsa);
4696 0208f208 2020-05-05 stsp goto done;
4698 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4699 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4700 5191b70b 2023-01-07 mark int pad = dsa.max_path_len - pe->path_len + 1;
4702 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4703 5191b70b 2023-01-07 mark pe->path, pad, ' ', dsa.add_cols + 1, cp->add,
4704 5191b70b 2023-01-07 mark dsa.rm_cols + 1, cp->rm);
4705 fe621944 2020-11-10 stsp if (n < 0) {
4706 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4707 fe621944 2020-11-10 stsp goto done;
4709 fe621944 2020-11-10 stsp outoff += n;
4710 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4711 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4713 fe621944 2020-11-10 stsp goto done;
4716 0208f208 2020-05-05 stsp fputc('\n', outfile);
4718 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4720 5191b70b 2023-01-07 mark goto done;
4722 5191b70b 2023-01-07 mark n = fprintf(outfile,
4723 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4724 65dedee0 2023-01-15 mark dsa.nfiles, dsa.nfiles > 1 ? "s" : "", dsa.ins,
4725 65dedee0 2023-01-15 mark dsa.ins != 1 ? "s" : "", dsa.del, dsa.del != 1 ? "s" : "");
4726 5191b70b 2023-01-07 mark if (n < 0) {
4727 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
4728 5191b70b 2023-01-07 mark goto done;
4730 5191b70b 2023-01-07 mark outoff += n;
4731 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4733 5191b70b 2023-01-07 mark goto done;
4735 5191b70b 2023-01-07 mark fputc('\n', outfile);
4737 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4739 d8bacb93 2023-01-10 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4740 abd2672a 2018-12-23 stsp free(id_str);
4741 5943eee2 2019-08-13 stsp free(logmsg);
4742 8b473291 2019-02-21 stsp free(refs_str);
4743 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4744 7510f233 2020-08-09 stsp if (err) {
4745 c7d5c43c 2022-08-04 mark free(*lines);
4746 c7d5c43c 2022-08-04 mark *lines = NULL;
4747 ae6a6978 2020-08-09 stsp *nlines = 0;
4749 fe621944 2020-11-10 stsp return err;
4752 abd2672a 2018-12-23 stsp static const struct got_error *
4753 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4755 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4756 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4757 15a94983 2018-12-23 stsp int obj_type;
4759 c7d5c43c 2022-08-04 mark free(s->lines);
4760 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4761 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4762 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4763 fe621944 2020-11-10 stsp s->nlines = 0;
4765 511a516b 2018-05-19 stsp f = got_opentemp();
4766 48ae06ee 2018-10-18 stsp if (f == NULL) {
4767 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4768 48ae06ee 2018-10-18 stsp goto done;
4770 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4771 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4772 fb43ecf1 2019-02-11 stsp goto done;
4776 15a94983 2018-12-23 stsp if (s->id1)
4777 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4779 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4781 15a94983 2018-12-23 stsp goto done;
4783 15a94983 2018-12-23 stsp switch (obj_type) {
4784 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4785 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4786 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4787 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4788 a76e88e5 2023-01-10 mark s->ignore_whitespace, s->force_text_diff, 0, NULL, s->repo,
4791 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4792 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4793 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4794 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4795 a76e88e5 2023-01-10 mark s->force_text_diff, 0, NULL, s->repo, s->f);
4797 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4798 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4799 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4800 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4801 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4803 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4805 3ffacbe1 2020-02-02 tracey goto done;
4806 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4807 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4808 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4809 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4810 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
4811 5191b70b 2023-01-07 mark s->force_text_diff, s->f);
4812 f44b1f58 2020-02-02 tracey if (err)
4813 f44b1f58 2020-02-02 tracey goto done;
4814 f44b1f58 2020-02-02 tracey } else {
4815 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4816 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4817 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4818 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4819 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4820 5191b70b 2023-01-07 mark s->ignore_whitespace,
4821 5191b70b 2023-01-07 mark s->force_text_diff, s->f);
4822 f44b1f58 2020-02-02 tracey if (err)
4823 f44b1f58 2020-02-02 tracey goto done;
4828 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4830 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4831 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4832 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4833 a76e88e5 2023-01-10 mark s->force_text_diff, 0, NULL, s->repo, s->f);
4837 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4841 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4842 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4843 48ae06ee 2018-10-18 stsp return err;
4846 f5215bb9 2019-02-22 stsp static void
4847 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4849 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4850 f5215bb9 2019-02-22 stsp update_panels();
4851 f5215bb9 2019-02-22 stsp doupdate();
4854 f44b1f58 2020-02-02 tracey static const struct got_error *
4855 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4857 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4859 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4860 f44b1f58 2020-02-02 tracey return NULL;
4863 eaf2c13e 2022-09-17 mark static void
4864 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4865 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4867 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4869 eaf2c13e 2022-09-17 mark *f = s->f;
4870 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4871 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4872 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4873 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4874 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4875 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4878 ec2a9698 2022-09-15 mark static const struct got_error *
4879 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4881 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4883 f44b1f58 2020-02-02 tracey int lineno;
4884 cb713507 2022-06-16 stsp char *line = NULL;
4885 826082fe 2020-12-10 stsp size_t linesize = 0;
4886 826082fe 2020-12-10 stsp ssize_t linelen;
4887 ec2a9698 2022-09-15 mark off_t *line_offsets;
4888 ec2a9698 2022-09-15 mark size_t nlines = 0;
4889 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4891 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4892 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4893 eaf2c13e 2022-09-17 mark "view search not supported");
4894 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4895 ec2a9698 2022-09-15 mark &match, &selected);
4897 f44b1f58 2020-02-02 tracey if (!view->searching) {
4898 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4899 f44b1f58 2020-02-02 tracey return NULL;
4902 ec2a9698 2022-09-15 mark if (*match) {
4903 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4904 ec2a9698 2022-09-15 mark lineno = *match + 1;
4906 ec2a9698 2022-09-15 mark lineno = *match - 1;
4908 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4910 f44b1f58 2020-02-02 tracey while (1) {
4911 f44b1f58 2020-02-02 tracey off_t offset;
4913 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4914 ec2a9698 2022-09-15 mark if (*match == 0) {
4915 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4919 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4920 f44b1f58 2020-02-02 tracey lineno = 1;
4922 ec2a9698 2022-09-15 mark lineno = nlines;
4925 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
4926 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
4927 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
4928 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
4929 f44b1f58 2020-02-02 tracey free(line);
4930 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4932 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
4933 cb713507 2022-06-16 stsp if (linelen != -1) {
4934 cb713507 2022-06-16 stsp char *exstr;
4935 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4938 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4939 cb713507 2022-06-16 stsp &view->regmatch)) {
4940 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4941 ec2a9698 2022-09-15 mark *match = lineno;
4942 cb713507 2022-06-16 stsp free(exstr);
4945 cb713507 2022-06-16 stsp free(exstr);
4947 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4948 f44b1f58 2020-02-02 tracey lineno++;
4950 f44b1f58 2020-02-02 tracey lineno--;
4952 826082fe 2020-12-10 stsp free(line);
4954 ec2a9698 2022-09-15 mark if (*match) {
4955 ec2a9698 2022-09-15 mark *first = *match;
4956 ec2a9698 2022-09-15 mark *selected = 1;
4959 145b6838 2022-06-16 stsp return err;
4962 48ae06ee 2018-10-18 stsp static const struct got_error *
4963 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4965 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4966 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4968 b72706c3 2022-06-01 stsp free(s->id1);
4969 b72706c3 2022-06-01 stsp s->id1 = NULL;
4970 b72706c3 2022-06-01 stsp free(s->id2);
4971 b72706c3 2022-06-01 stsp s->id2 = NULL;
4972 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4973 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4974 b72706c3 2022-06-01 stsp s->f = NULL;
4975 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4976 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4977 b72706c3 2022-06-01 stsp s->f1 = NULL;
4978 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4979 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4980 b72706c3 2022-06-01 stsp s->f2 = NULL;
4981 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4982 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4983 f9d37699 2022-06-28 stsp s->fd1 = -1;
4984 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4985 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4986 f9d37699 2022-06-28 stsp s->fd2 = -1;
4987 c7d5c43c 2022-08-04 mark free(s->lines);
4988 c7d5c43c 2022-08-04 mark s->lines = NULL;
4989 b72706c3 2022-06-01 stsp s->nlines = 0;
4990 b72706c3 2022-06-01 stsp return err;
4993 b72706c3 2022-06-01 stsp static const struct got_error *
4994 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4995 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4996 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4997 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4999 48ae06ee 2018-10-18 stsp const struct got_error *err;
5000 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5002 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
5003 f9d37699 2022-06-28 stsp s->fd1 = -1;
5004 f9d37699 2022-06-28 stsp s->fd2 = -1;
5006 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5007 15a94983 2018-12-23 stsp int type1, type2;
5008 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5010 15a94983 2018-12-23 stsp return err;
5011 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5013 15a94983 2018-12-23 stsp return err;
5015 15a94983 2018-12-23 stsp if (type1 != type2)
5016 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5018 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5019 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5020 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5021 f44b1f58 2020-02-02 tracey s->repo = repo;
5022 f44b1f58 2020-02-02 tracey s->id1 = id1;
5023 f44b1f58 2020-02-02 tracey s->id2 = id2;
5024 3dbaef42 2020-11-24 stsp s->label1 = label1;
5025 3dbaef42 2020-11-24 stsp s->label2 = label2;
5027 15a94983 2018-12-23 stsp if (id1) {
5028 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5029 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5030 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5032 5465d566 2020-02-01 tracey s->id1 = NULL;
5034 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5035 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5036 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5037 b72706c3 2022-06-01 stsp goto done;
5040 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5041 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5042 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5043 a00719e9 2022-06-17 stsp goto done;
5046 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5047 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5048 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5049 b72706c3 2022-06-01 stsp goto done;
5052 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5053 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5054 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5055 f9d37699 2022-06-28 stsp goto done;
5058 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5059 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5060 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5061 f9d37699 2022-06-28 stsp goto done;
5064 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5065 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5066 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5067 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5068 5465d566 2020-02-01 tracey s->repo = repo;
5070 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5073 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5074 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5075 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5076 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5077 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5078 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5079 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5080 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5081 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5082 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5083 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5084 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5085 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5086 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5087 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5088 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5089 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5090 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5091 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5092 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5093 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5094 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5095 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5096 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5097 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5098 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5099 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5100 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5101 b72706c3 2022-06-01 stsp goto done;
5105 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5106 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5107 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5108 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5110 5465d566 2020-02-01 tracey err = create_diff(s);
5112 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5113 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5114 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5115 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5116 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5117 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5118 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5121 b72706c3 2022-06-01 stsp close_diff_view(view);
5122 e5a0f69f 2018-08-18 stsp return err;
5125 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5126 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5128 a3404814 2018-09-02 stsp const struct got_error *err;
5129 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5130 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5131 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5133 a3404814 2018-09-02 stsp if (s->id1) {
5134 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5136 a3404814 2018-09-02 stsp return err;
5137 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5139 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5141 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5143 a3404814 2018-09-02 stsp return err;
5144 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5146 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5147 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5148 a3404814 2018-09-02 stsp free(id_str1);
5149 a3404814 2018-09-02 stsp free(id_str2);
5150 a3404814 2018-09-02 stsp return err;
5152 a3404814 2018-09-02 stsp free(id_str1);
5153 a3404814 2018-09-02 stsp free(id_str2);
5155 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5156 267bb3b8 2021-08-01 stsp free(header);
5157 267bb3b8 2021-08-01 stsp return err;
5160 15a087fe 2019-02-21 stsp static const struct got_error *
5161 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5162 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5164 d7a04538 2019-02-21 stsp const struct got_error *err;
5165 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5166 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5167 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5169 15a087fe 2019-02-21 stsp free(s->id2);
5170 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5171 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5172 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5174 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5176 d7a04538 2019-02-21 stsp return err;
5177 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5178 15a087fe 2019-02-21 stsp free(s->id1);
5179 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5180 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5181 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5182 15a087fe 2019-02-21 stsp return NULL;
5185 0cf4efb1 2018-09-29 stsp static const struct got_error *
5186 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5188 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5190 917d79a7 2022-07-01 stsp view->count = 0;
5191 917d79a7 2022-07-01 stsp wclear(view->window);
5192 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5193 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5194 917d79a7 2022-07-01 stsp s->matched_line = 0;
5195 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5196 917d79a7 2022-07-01 stsp return create_diff(s);
5199 c7d5c43c 2022-08-04 mark static void
5200 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5202 c7d5c43c 2022-08-04 mark int start, i;
5204 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5206 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5207 c7d5c43c 2022-08-04 mark if (i == 0)
5208 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5209 c7d5c43c 2022-08-04 mark if (--i == start)
5210 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5213 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5214 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5217 c7d5c43c 2022-08-04 mark static void
5218 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5220 c7d5c43c 2022-08-04 mark int start, i;
5222 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5224 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5225 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5227 c7d5c43c 2022-08-04 mark if (++i == start)
5228 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5231 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5232 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5235 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5236 c0f61fa4 2022-07-11 mark int, int, int);
5237 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5238 c0f61fa4 2022-07-11 mark int, int);
5240 917d79a7 2022-07-01 stsp static const struct got_error *
5241 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5243 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5244 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5245 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5246 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5247 826082fe 2020-12-10 stsp char *line = NULL;
5248 826082fe 2020-12-10 stsp size_t linesize = 0;
5249 826082fe 2020-12-10 stsp ssize_t linelen;
5250 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5252 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5254 e5a0f69f 2018-08-18 stsp switch (ch) {
5256 145b6838 2022-06-16 stsp view->x = 0;
5259 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5260 640cd7ff 2022-06-22 mark view->count = 0;
5262 145b6838 2022-06-16 stsp case KEY_RIGHT:
5264 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5265 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5267 640cd7ff 2022-06-22 mark view->count = 0;
5269 145b6838 2022-06-16 stsp case KEY_LEFT:
5271 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5272 640cd7ff 2022-06-22 mark if (view->x <= 0)
5273 640cd7ff 2022-06-22 mark view->count = 0;
5277 3dbaef42 2020-11-24 stsp if (ch == 'a')
5278 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5279 5191b70b 2023-01-07 mark else if (ch == 'w')
5280 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5281 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5284 912a3f79 2021-08-30 j case KEY_HOME:
5285 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5286 640cd7ff 2022-06-22 mark view->count = 0;
5289 912a3f79 2021-08-30 j case KEY_END:
5290 640cd7ff 2022-06-22 mark view->count = 0;
5294 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5298 1e37a5c2 2019-05-12 jcs case KEY_UP:
5299 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5300 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5301 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5303 640cd7ff 2022-06-22 mark view->count = 0;
5305 83cc4199 2022-06-13 stsp case CTRL('u'):
5307 83cc4199 2022-06-13 stsp nscroll /= 2;
5308 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5309 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5310 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5312 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5313 640cd7ff 2022-06-22 mark view->count = 0;
5317 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5318 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5321 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5322 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5323 1e37a5c2 2019-05-12 jcs if (!s->eof)
5324 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5326 640cd7ff 2022-06-22 mark view->count = 0;
5328 83cc4199 2022-06-13 stsp case CTRL('d'):
5330 83cc4199 2022-06-13 stsp nscroll /= 2;
5331 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5332 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5333 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5336 640cd7ff 2022-06-22 mark if (s->eof) {
5337 640cd7ff 2022-06-22 mark view->count = 0;
5341 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5342 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5343 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5344 826082fe 2020-12-10 stsp if (linelen == -1) {
5345 826082fe 2020-12-10 stsp if (feof(s->f)) {
5346 826082fe 2020-12-10 stsp s->eof = 1;
5348 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5352 826082fe 2020-12-10 stsp free(line);
5355 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5358 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5361 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5364 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5367 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5368 1e37a5c2 2019-05-12 jcs s->diff_context--;
5369 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5370 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5371 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5372 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5373 27829c9e 2020-11-21 stsp s->nlines) {
5374 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5375 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5378 640cd7ff 2022-06-22 mark view->count = 0;
5381 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5382 1e37a5c2 2019-05-12 jcs s->diff_context++;
5383 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5384 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5385 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5387 640cd7ff 2022-06-22 mark view->count = 0;
5393 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5397 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5398 640cd7ff 2022-06-22 mark view->count = 0;
5401 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5403 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5404 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5405 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5407 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5408 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5411 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5413 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5416 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5419 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5420 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5421 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5423 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5424 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5425 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5427 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5428 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5431 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5433 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5435 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5436 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5437 c0f61fa4 2022-07-11 mark bs->selected_line);
5438 c0f61fa4 2022-07-11 mark if (id == NULL)
5441 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5444 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5448 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5449 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5450 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5451 145b6838 2022-06-16 stsp view->x = 0;
5453 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5454 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5457 640cd7ff 2022-06-22 mark view->count = 0;
5461 bcbd79e2 2018-08-19 stsp return err;
5464 4ed7e80c 2018-05-20 stsp static const struct got_error *
5465 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5467 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5468 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5469 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5470 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5471 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5472 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5473 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5474 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5475 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5476 3dbaef42 2020-11-24 stsp const char *errstr;
5477 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5478 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5480 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5481 26ed57b2 2018-05-19 stsp switch (ch) {
5483 64453f7e 2020-11-21 stsp force_text_diff = 1;
5486 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5488 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5489 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5490 5a20d08d 2022-02-09 op errstr, errstr);
5492 09b5bff8 2020-02-23 naddy case 'r':
5493 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5494 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5495 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5497 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5500 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5503 17020d27 2019-03-07 stsp usage_diff();
5504 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5508 26ed57b2 2018-05-19 stsp argc -= optind;
5509 26ed57b2 2018-05-19 stsp argv += optind;
5511 26ed57b2 2018-05-19 stsp if (argc == 0) {
5512 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5513 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5514 15a94983 2018-12-23 stsp id_str1 = argv[0];
5515 15a94983 2018-12-23 stsp id_str2 = argv[1];
5517 26ed57b2 2018-05-19 stsp usage_diff();
5519 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5520 0ae84acc 2022-06-15 tracey if (error)
5521 0ae84acc 2022-06-15 tracey goto done;
5523 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5524 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5525 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5526 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5527 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5528 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5529 c156c7a4 2020-12-18 stsp goto done;
5530 a273ac94 2020-02-23 naddy if (worktree)
5531 a273ac94 2020-02-23 naddy repo_path =
5532 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5534 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5535 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5536 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5537 c156c7a4 2020-12-18 stsp goto done;
5541 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5542 eb6600df 2019-01-04 stsp if (error)
5543 eb6600df 2019-01-04 stsp goto done;
5545 a273ac94 2020-02-23 naddy init_curses();
5547 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5548 51a10b52 2020-12-26 stsp if (error)
5549 51a10b52 2020-12-26 stsp goto done;
5551 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5552 26ed57b2 2018-05-19 stsp if (error)
5553 26ed57b2 2018-05-19 stsp goto done;
5555 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5556 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5557 26ed57b2 2018-05-19 stsp if (error)
5558 26ed57b2 2018-05-19 stsp goto done;
5560 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5561 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5562 26ed57b2 2018-05-19 stsp if (error)
5563 26ed57b2 2018-05-19 stsp goto done;
5565 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5566 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5567 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5568 ea5e7bb5 2018-08-01 stsp goto done;
5570 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5571 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5572 5dc9f4bc 2018-08-04 stsp if (error)
5573 5dc9f4bc 2018-08-04 stsp goto done;
5574 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5576 3dbaef42 2020-11-24 stsp free(label1);
5577 3dbaef42 2020-11-24 stsp free(label2);
5578 c02c541e 2019-03-29 stsp free(repo_path);
5579 a273ac94 2020-02-23 naddy free(cwd);
5580 1d0f4054 2021-06-17 stsp if (repo) {
5581 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5582 1d0f4054 2021-06-17 stsp if (error == NULL)
5583 1d0f4054 2021-06-17 stsp error = close_err;
5585 a273ac94 2020-02-23 naddy if (worktree)
5586 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5587 0ae84acc 2022-06-15 tracey if (pack_fds) {
5588 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5589 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5590 0ae84acc 2022-06-15 tracey if (error == NULL)
5591 0ae84acc 2022-06-15 tracey error = pack_err;
5593 51a10b52 2020-12-26 stsp tog_free_refs();
5594 26ed57b2 2018-05-19 stsp return error;
5597 4ed7e80c 2018-05-20 stsp __dead static void
5598 9f7d7167 2018-04-29 stsp usage_blame(void)
5601 87411fa9 2022-06-16 stsp fprintf(stderr,
5602 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5603 9f7d7167 2018-04-29 stsp getprogname());
5607 84451b3e 2018-07-10 stsp struct tog_blame_line {
5608 84451b3e 2018-07-10 stsp int annotated;
5609 84451b3e 2018-07-10 stsp struct got_object_id *id;
5612 4ed7e80c 2018-05-20 stsp static const struct got_error *
5613 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5615 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5616 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5617 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5618 84451b3e 2018-07-10 stsp const struct got_error *err;
5619 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5620 826082fe 2020-12-10 stsp char *line = NULL;
5621 826082fe 2020-12-10 stsp size_t linesize = 0;
5622 826082fe 2020-12-10 stsp ssize_t linelen;
5623 84451b3e 2018-07-10 stsp wchar_t *wline;
5624 27a741e5 2019-09-11 stsp int width;
5625 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5626 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5627 ab089a2a 2018-07-12 stsp char *id_str;
5628 11b20872 2019-11-08 stsp struct tog_color *tc;
5630 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5632 ab089a2a 2018-07-12 stsp return err;
5634 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5635 f7d12f7e 2018-08-01 stsp werase(view->window);
5637 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5638 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5639 ab089a2a 2018-07-12 stsp free(id_str);
5640 ab089a2a 2018-07-12 stsp return err;
5643 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5644 ab089a2a 2018-07-12 stsp free(line);
5645 2550e4c3 2018-07-13 stsp line = NULL;
5647 1cae65b4 2019-09-22 stsp return err;
5648 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5649 a3404814 2018-09-02 stsp wstandout(view->window);
5650 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5652 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5653 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5654 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5655 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5657 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5658 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5659 a3404814 2018-09-02 stsp wstandend(view->window);
5660 2550e4c3 2018-07-13 stsp free(wline);
5661 2550e4c3 2018-07-13 stsp wline = NULL;
5663 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5664 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5666 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5667 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5668 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5669 ab089a2a 2018-07-12 stsp free(id_str);
5670 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5672 ab089a2a 2018-07-12 stsp free(id_str);
5673 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5674 3f60a8ef 2018-07-10 stsp free(line);
5675 2550e4c3 2018-07-13 stsp line = NULL;
5677 3f60a8ef 2018-07-10 stsp return err;
5678 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5679 2550e4c3 2018-07-13 stsp free(wline);
5680 2550e4c3 2018-07-13 stsp wline = NULL;
5681 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5682 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5684 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5685 145b6838 2022-06-16 stsp view->maxx = 0;
5686 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5687 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5688 826082fe 2020-12-10 stsp if (linelen == -1) {
5689 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5690 826082fe 2020-12-10 stsp s->eof = 1;
5693 84451b3e 2018-07-10 stsp free(line);
5694 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5696 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5698 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5701 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5702 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5703 1853e0f4 2022-06-16 stsp if (err) {
5704 1853e0f4 2022-06-16 stsp free(line);
5705 1853e0f4 2022-06-16 stsp return err;
5707 1853e0f4 2022-06-16 stsp free(wline);
5708 1853e0f4 2022-06-16 stsp wline = NULL;
5709 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5711 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5712 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5714 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5715 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5716 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5717 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5718 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5719 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5720 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5721 8d0fe45a 2019-08-12 stsp char *id_str;
5722 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5723 87411fa9 2022-06-16 stsp blame_line->id);
5724 8d0fe45a 2019-08-12 stsp if (err) {
5725 8d0fe45a 2019-08-12 stsp free(line);
5726 8d0fe45a 2019-08-12 stsp return err;
5728 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5730 11b20872 2019-11-08 stsp wattr_on(view->window,
5731 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5732 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5734 11b20872 2019-11-08 stsp wattr_off(view->window,
5735 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5736 8d0fe45a 2019-08-12 stsp free(id_str);
5737 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5739 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5740 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5743 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5744 ee41ec32 2018-07-10 stsp prev_id = NULL;
5747 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5748 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5749 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5751 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5752 41605754 2020-11-12 stsp width = 9;
5753 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5754 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5755 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5756 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5757 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5758 41605754 2020-11-12 stsp if (err) {
5759 41605754 2020-11-12 stsp free(line);
5760 41605754 2020-11-12 stsp return err;
5762 41605754 2020-11-12 stsp width += 9;
5765 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5766 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5767 4cb9d869 2022-06-16 stsp if (err) {
5768 4cb9d869 2022-06-16 stsp free(line);
5769 4cb9d869 2022-06-16 stsp return err;
5771 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5772 a75b3e2d 2022-06-16 stsp width += 9;
5773 41605754 2020-11-12 stsp free(wline);
5774 41605754 2020-11-12 stsp wline = NULL;
5777 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5778 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5779 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5780 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5782 826082fe 2020-12-10 stsp free(line);
5783 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5785 9b058f45 2022-06-30 mark view_border(view);
5787 84451b3e 2018-07-10 stsp return NULL;
5790 84451b3e 2018-07-10 stsp static const struct got_error *
5791 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5792 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5794 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5795 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5796 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5797 1a76625f 2018-10-22 stsp int errcode;
5799 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5800 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5801 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5803 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5804 1a76625f 2018-10-22 stsp if (errcode)
5805 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5807 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5808 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5809 d68a0a7d 2018-07-10 stsp goto done;
5812 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5813 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5815 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5816 d68a0a7d 2018-07-10 stsp if (line->annotated)
5817 d68a0a7d 2018-07-10 stsp goto done;
5819 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5820 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5821 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5822 84451b3e 2018-07-10 stsp goto done;
5824 84451b3e 2018-07-10 stsp line->annotated = 1;
5826 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5827 1a76625f 2018-10-22 stsp if (errcode)
5828 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5829 84451b3e 2018-07-10 stsp return err;
5832 84451b3e 2018-07-10 stsp static void *
5833 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5835 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5836 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5837 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5838 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5839 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5841 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5842 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5843 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5845 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5846 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5847 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5848 e6e73e55 2022-06-30 tracey goto done;
5851 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5852 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5853 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5854 e6e73e55 2022-06-30 tracey goto done;
5856 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5857 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5858 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5859 e6e73e55 2022-06-30 tracey goto done;
5862 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5864 e6e73e55 2022-06-30 tracey goto done;
5866 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5867 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5868 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5869 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5870 fc06ba56 2019-08-22 stsp err = NULL;
5872 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5873 e6e73e55 2022-06-30 tracey if (errcode) {
5874 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5875 e6e73e55 2022-06-30 tracey goto done;
5878 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5879 1d0f4054 2021-06-17 stsp if (err == NULL)
5880 1d0f4054 2021-06-17 stsp err = close_err;
5881 c9beca56 2018-07-22 stsp ta->repo = NULL;
5882 c9beca56 2018-07-22 stsp *ta->complete = 1;
5884 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5885 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5886 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5889 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5890 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5891 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5892 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5893 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5894 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5895 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5896 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5898 18430de3 2018-07-10 stsp return (void *)err;
5901 245d91c1 2018-07-12 stsp static struct got_object_id *
5902 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5903 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5905 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5907 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5908 8d0fe45a 2019-08-12 stsp return NULL;
5910 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5911 c0f61fa4 2022-07-11 mark if (!line->annotated)
5912 c0f61fa4 2022-07-11 mark return NULL;
5914 c0f61fa4 2022-07-11 mark return line->id;
5917 c0f61fa4 2022-07-11 mark static struct got_object_id *
5918 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5919 c0f61fa4 2022-07-11 mark int lineno)
5921 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5923 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5924 c0f61fa4 2022-07-11 mark return NULL;
5926 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5927 245d91c1 2018-07-12 stsp if (!line->annotated)
5928 245d91c1 2018-07-12 stsp return NULL;
5930 245d91c1 2018-07-12 stsp return line->id;
5933 b880a918 2018-07-10 stsp static const struct got_error *
5934 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5936 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5939 245d91c1 2018-07-12 stsp if (blame->thread) {
5940 1a76625f 2018-10-22 stsp int errcode;
5941 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5942 1a76625f 2018-10-22 stsp if (errcode)
5943 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5944 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5945 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5946 1a76625f 2018-10-22 stsp if (errcode)
5947 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5948 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5949 1a76625f 2018-10-22 stsp if (errcode)
5950 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5951 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5952 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5953 245d91c1 2018-07-12 stsp err = NULL;
5954 245d91c1 2018-07-12 stsp blame->thread = NULL;
5956 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5957 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5958 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5959 1d0f4054 2021-06-17 stsp if (err == NULL)
5960 1d0f4054 2021-06-17 stsp err = close_err;
5961 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5963 245d91c1 2018-07-12 stsp if (blame->f) {
5964 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5965 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5966 245d91c1 2018-07-12 stsp blame->f = NULL;
5968 57670559 2018-12-23 stsp if (blame->lines) {
5969 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5970 57670559 2018-12-23 stsp free(blame->lines[i].id);
5971 57670559 2018-12-23 stsp free(blame->lines);
5972 57670559 2018-12-23 stsp blame->lines = NULL;
5974 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5975 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5976 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5977 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5978 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5979 0ae84acc 2022-06-15 tracey if (err == NULL)
5980 0ae84acc 2022-06-15 tracey err = pack_err;
5981 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5983 245d91c1 2018-07-12 stsp return err;
5986 245d91c1 2018-07-12 stsp static const struct got_error *
5987 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5989 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5990 fc06ba56 2019-08-22 stsp int *done = arg;
5991 fc06ba56 2019-08-22 stsp int errcode;
5993 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5994 fc06ba56 2019-08-22 stsp if (errcode)
5995 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5996 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5998 fc06ba56 2019-08-22 stsp if (*done)
5999 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6001 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6002 fc06ba56 2019-08-22 stsp if (errcode)
6003 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6004 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6006 fc06ba56 2019-08-22 stsp return err;
6009 fc06ba56 2019-08-22 stsp static const struct got_error *
6010 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6012 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6013 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6014 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6015 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6016 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6017 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6018 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6019 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6020 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6022 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6023 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6025 15a94983 2018-12-23 stsp return err;
6027 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6028 eb81bc23 2022-06-28 tracey if (fd == -1) {
6029 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6030 eb81bc23 2022-06-28 tracey goto done;
6033 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6035 a44927cc 2022-04-07 stsp goto done;
6037 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6039 84451b3e 2018-07-10 stsp goto done;
6041 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6042 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6043 84451b3e 2018-07-10 stsp goto done;
6046 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6048 a70480e0 2018-06-23 stsp goto done;
6049 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6050 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6051 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6052 84451b3e 2018-07-10 stsp goto done;
6054 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6055 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6057 1fddf795 2021-01-20 stsp goto done;
6058 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6059 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6060 84451b3e 2018-07-10 stsp goto done;
6063 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6064 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6065 b02560ec 2019-08-19 stsp blame->nlines--;
6067 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6068 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6069 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6070 84451b3e 2018-07-10 stsp goto done;
6073 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6075 bd24772e 2018-07-11 stsp goto done;
6076 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6077 0ae84acc 2022-06-15 tracey pack_fds);
6078 0ae84acc 2022-06-15 tracey if (err)
6079 0ae84acc 2022-06-15 tracey goto done;
6081 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6082 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6083 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6084 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6085 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6086 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6087 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6088 245d91c1 2018-07-12 stsp goto done;
6090 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6092 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6093 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6094 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6095 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6096 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6097 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6098 a5388363 2020-12-01 naddy s->blame_complete = 0;
6100 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6101 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6102 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6103 f5a09613 2020-12-13 naddy s->selected_line = 1;
6105 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6108 a44927cc 2022-04-07 stsp if (commit)
6109 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6110 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6111 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6113 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6114 27d434c2 2018-09-15 stsp free(obj_id);
6116 245d91c1 2018-07-12 stsp stop_blame(blame);
6117 245d91c1 2018-07-12 stsp return err;
6120 245d91c1 2018-07-12 stsp static const struct got_error *
6121 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6122 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6124 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6125 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6127 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6129 c4843652 2019-08-12 stsp s->path = strdup(path);
6130 c4843652 2019-08-12 stsp if (s->path == NULL)
6131 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6133 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6134 c4843652 2019-08-12 stsp if (err) {
6135 c4843652 2019-08-12 stsp free(s->path);
6136 7cbe629d 2018-08-04 stsp return err;
6139 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6140 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6141 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6142 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6143 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6144 fb2756b9 2018-08-04 stsp s->repo = repo;
6145 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6146 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6148 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6149 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6150 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6151 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6153 11b20872 2019-11-08 stsp return err;
6156 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6157 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6158 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6159 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6160 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6161 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6162 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6164 a5388363 2020-12-01 naddy return run_blame(view);
6167 e5a0f69f 2018-08-18 stsp static const struct got_error *
6168 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6170 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6171 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6173 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6174 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6176 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6177 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6178 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6179 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6180 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6183 e5a0f69f 2018-08-18 stsp free(s->path);
6184 11b20872 2019-11-08 stsp free_colors(&s->colors);
6185 e5a0f69f 2018-08-18 stsp return err;
6188 7cbe629d 2018-08-04 stsp static const struct got_error *
6189 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6191 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6193 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6194 6c4c42e0 2019-06-24 stsp return NULL;
6197 eaf2c13e 2022-09-17 mark static void
6198 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6199 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6201 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6203 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6204 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6205 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6206 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6207 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6208 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6209 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6212 6c4c42e0 2019-06-24 stsp static const struct got_error *
6213 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6215 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6216 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6217 2b380cc8 2018-10-24 stsp int errcode;
6219 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6220 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6221 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6222 2b380cc8 2018-10-24 stsp if (errcode)
6223 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6225 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6228 51fe7530 2019-08-19 stsp if (s->blame_complete)
6229 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6231 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6233 9b058f45 2022-06-30 mark view_border(view);
6234 e5a0f69f 2018-08-18 stsp return err;
6237 e5a0f69f 2018-08-18 stsp static const struct got_error *
6238 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6239 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6241 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6242 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6244 05f04cdf 2022-07-20 mark *new_view = NULL;
6246 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6247 05f04cdf 2022-07-20 mark if (log_view == NULL)
6248 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6250 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6252 05f04cdf 2022-07-20 mark view_close(log_view);
6254 05f04cdf 2022-07-20 mark *new_view = log_view;
6256 05f04cdf 2022-07-20 mark return err;
6259 05f04cdf 2022-07-20 mark static const struct got_error *
6260 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6262 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6263 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6264 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6265 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6267 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6268 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6269 9b058f45 2022-06-30 mark --eos; /* border */
6271 e5a0f69f 2018-08-18 stsp switch (ch) {
6273 145b6838 2022-06-16 stsp view->x = 0;
6276 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
6277 640cd7ff 2022-06-22 mark view->count = 0;
6279 145b6838 2022-06-16 stsp case KEY_RIGHT:
6281 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
6282 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
6284 640cd7ff 2022-06-22 mark view->count = 0;
6286 145b6838 2022-06-16 stsp case KEY_LEFT:
6288 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
6289 640cd7ff 2022-06-22 mark if (view->x <= 0)
6290 640cd7ff 2022-06-22 mark view->count = 0;
6293 1e37a5c2 2019-05-12 jcs s->done = 1;
6295 4deef56f 2021-09-02 naddy case 'g':
6296 4deef56f 2021-09-02 naddy case KEY_HOME:
6297 4deef56f 2021-09-02 naddy s->selected_line = 1;
6298 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6299 640cd7ff 2022-06-22 mark view->count = 0;
6301 4deef56f 2021-09-02 naddy case 'G':
6302 4deef56f 2021-09-02 naddy case KEY_END:
6303 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6304 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6305 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6307 9b058f45 2022-06-30 mark s->selected_line = eos;
6308 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6310 640cd7ff 2022-06-22 mark view->count = 0;
6313 1e37a5c2 2019-05-12 jcs case KEY_UP:
6314 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6315 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6316 1e37a5c2 2019-05-12 jcs s->selected_line--;
6317 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6318 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6319 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6321 640cd7ff 2022-06-22 mark view->count = 0;
6323 83cc4199 2022-06-13 stsp case CTRL('u'):
6325 83cc4199 2022-06-13 stsp nscroll /= 2;
6326 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6327 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6328 ea025d1d 2020-02-22 naddy case CTRL('b'):
6330 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6331 640cd7ff 2022-06-22 mark if (view->count > 1)
6332 640cd7ff 2022-06-22 mark nscroll += nscroll;
6333 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6334 640cd7ff 2022-06-22 mark view->count = 0;
6337 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6338 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6340 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6343 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6344 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6345 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6346 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6347 1e37a5c2 2019-05-12 jcs s->selected_line++;
6348 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6349 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6351 640cd7ff 2022-06-22 mark view->count = 0;
6354 1e37a5c2 2019-05-12 jcs case 'p': {
6355 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6357 640cd7ff 2022-06-22 mark view->count = 0;
6358 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6359 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6360 1e37a5c2 2019-05-12 jcs if (id == NULL)
6362 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6363 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6364 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6365 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6366 1e37a5c2 2019-05-12 jcs int obj_type;
6367 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6368 1e37a5c2 2019-05-12 jcs s->repo, id);
6371 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6372 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6373 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6374 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6377 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6378 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6379 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6382 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6383 a44927cc 2022-04-07 stsp pcommit, s->path);
6384 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6385 e5a0f69f 2018-08-18 stsp if (err) {
6386 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6387 1e37a5c2 2019-05-12 jcs err = NULL;
6388 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6391 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6393 1e37a5c2 2019-05-12 jcs free(blob_id);
6394 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6395 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6396 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6399 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6400 d7b5a0e8 2022-04-20 stsp &pid->id);
6401 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6403 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6404 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6406 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6411 1e37a5c2 2019-05-12 jcs s->done = 1;
6412 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6413 1e37a5c2 2019-05-12 jcs s->done = 0;
6414 1e37a5c2 2019-05-12 jcs if (thread_err)
6416 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6417 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6418 a5388363 2020-12-01 naddy err = run_blame(view);
6423 61417565 2022-06-20 mark case 'C': {
6424 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6426 640cd7ff 2022-06-22 mark view->count = 0;
6427 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6428 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6430 1e37a5c2 2019-05-12 jcs s->done = 1;
6431 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6432 1e37a5c2 2019-05-12 jcs s->done = 0;
6433 1e37a5c2 2019-05-12 jcs if (thread_err)
6435 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6436 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6437 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6438 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6439 a5388363 2020-12-01 naddy err = run_blame(view);
6445 05f04cdf 2022-07-20 mark view->count = 0;
6446 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6447 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6448 136e2bd4 2022-07-23 mark if (s->id_to_log)
6449 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6451 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6452 1e37a5c2 2019-05-12 jcs case '\r': {
6453 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6454 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6455 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6457 640cd7ff 2022-06-22 mark view->count = 0;
6458 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6459 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6460 1e37a5c2 2019-05-12 jcs if (id == NULL)
6462 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6465 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6466 c0f61fa4 2022-07-11 mark if (*new_view) {
6467 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6468 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6471 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6473 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6474 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6476 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6477 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6478 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6479 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6480 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6484 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6485 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6486 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6488 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6491 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6492 c0f61fa4 2022-07-11 mark s->selected_line;
6493 c0f61fa4 2022-07-11 mark if (*new_view)
6494 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6495 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6496 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6497 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6502 e78dc838 2020-12-04 stsp view->focussed = 0;
6503 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6504 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6505 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6506 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6507 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6508 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6511 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6514 e78dc838 2020-12-04 stsp view->focus_child = 1;
6516 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6521 83cc4199 2022-06-13 stsp case CTRL('d'):
6523 83cc4199 2022-06-13 stsp nscroll /= 2;
6524 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6525 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6526 ea025d1d 2020-02-22 naddy case CTRL('f'):
6529 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6530 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6531 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6532 640cd7ff 2022-06-22 mark view->count = 0;
6535 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6536 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6537 83cc4199 2022-06-13 stsp s->selected_line +=
6538 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6539 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6541 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6542 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6544 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6545 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6547 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6548 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6549 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6550 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6554 640cd7ff 2022-06-22 mark view->count = 0;
6557 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6560 917d79a7 2022-07-01 stsp static const struct got_error *
6561 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6563 917d79a7 2022-07-01 stsp const struct got_error *err;
6564 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6566 917d79a7 2022-07-01 stsp view->count = 0;
6567 917d79a7 2022-07-01 stsp s->done = 1;
6568 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6569 917d79a7 2022-07-01 stsp s->done = 0;
6571 917d79a7 2022-07-01 stsp return err;
6572 917d79a7 2022-07-01 stsp return run_blame(view);
6575 a70480e0 2018-06-23 stsp static const struct got_error *
6576 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6578 a70480e0 2018-06-23 stsp const struct got_error *error;
6579 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6580 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6581 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6582 0587e10c 2020-07-23 stsp char *link_target = NULL;
6583 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6584 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6585 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6587 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6588 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6590 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6591 a70480e0 2018-06-23 stsp switch (ch) {
6593 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6596 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6597 69069811 2018-08-02 stsp if (repo_path == NULL)
6598 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6602 17020d27 2019-03-07 stsp usage_blame();
6603 a70480e0 2018-06-23 stsp /* NOTREACHED */
6607 a70480e0 2018-06-23 stsp argc -= optind;
6608 a70480e0 2018-06-23 stsp argv += optind;
6610 f135c941 2020-02-20 stsp if (argc != 1)
6611 a70480e0 2018-06-23 stsp usage_blame();
6613 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6614 0ae84acc 2022-06-15 tracey if (error != NULL)
6615 0ae84acc 2022-06-15 tracey goto done;
6617 69069811 2018-08-02 stsp if (repo_path == NULL) {
6618 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6619 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6620 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6621 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6622 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6623 c156c7a4 2020-12-18 stsp goto done;
6624 f135c941 2020-02-20 stsp if (worktree)
6625 eb41ed75 2019-02-05 stsp repo_path =
6626 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6628 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6629 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6630 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6631 c156c7a4 2020-12-18 stsp goto done;
6635 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6636 c02c541e 2019-03-29 stsp if (error != NULL)
6637 8e94dd5b 2019-01-04 stsp goto done;
6639 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6640 f135c941 2020-02-20 stsp worktree);
6641 c02c541e 2019-03-29 stsp if (error)
6642 92205607 2019-01-04 stsp goto done;
6644 f135c941 2020-02-20 stsp init_curses();
6646 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6647 51a10b52 2020-12-26 stsp if (error)
6648 51a10b52 2020-12-26 stsp goto done;
6650 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6651 eb41ed75 2019-02-05 stsp if (error)
6652 69069811 2018-08-02 stsp goto done;
6654 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6655 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6656 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6657 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6658 a70480e0 2018-06-23 stsp if (error != NULL)
6659 66b4983c 2018-06-23 stsp goto done;
6660 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6661 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6663 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6664 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6666 a19e88aa 2018-06-23 stsp if (error != NULL)
6667 8b473291 2019-02-21 stsp goto done;
6669 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6670 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6671 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6672 e1cd8fed 2018-08-01 stsp goto done;
6675 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6676 a44927cc 2022-04-07 stsp if (error)
6677 a44927cc 2022-04-07 stsp goto done;
6679 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6680 a44927cc 2022-04-07 stsp commit, repo);
6681 7cbe629d 2018-08-04 stsp if (error)
6682 7cbe629d 2018-08-04 stsp goto done;
6684 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6685 78756c87 2020-11-24 stsp commit_id, repo);
6686 0587e10c 2020-07-23 stsp if (error)
6687 0587e10c 2020-07-23 stsp goto done;
6688 12314ad4 2019-08-31 stsp if (worktree) {
6689 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6690 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6691 12314ad4 2019-08-31 stsp worktree = NULL;
6693 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6695 69069811 2018-08-02 stsp free(repo_path);
6696 f135c941 2020-02-20 stsp free(in_repo_path);
6697 0587e10c 2020-07-23 stsp free(link_target);
6698 69069811 2018-08-02 stsp free(cwd);
6699 a70480e0 2018-06-23 stsp free(commit_id);
6700 a44927cc 2022-04-07 stsp if (commit)
6701 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6702 eb41ed75 2019-02-05 stsp if (worktree)
6703 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6704 1d0f4054 2021-06-17 stsp if (repo) {
6705 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6706 1d0f4054 2021-06-17 stsp if (error == NULL)
6707 1d0f4054 2021-06-17 stsp error = close_err;
6709 0ae84acc 2022-06-15 tracey if (pack_fds) {
6710 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6711 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6712 0ae84acc 2022-06-15 tracey if (error == NULL)
6713 0ae84acc 2022-06-15 tracey error = pack_err;
6715 51a10b52 2020-12-26 stsp tog_free_refs();
6716 a70480e0 2018-06-23 stsp return error;
6719 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6720 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6722 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6723 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6724 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6725 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6726 0c6ad1bc 2022-09-09 mark char *index = NULL;
6727 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6728 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6729 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6731 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6732 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6733 9b058f45 2022-06-30 mark --limit; /* border */
6735 f7d12f7e 2018-08-01 stsp werase(view->window);
6737 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6738 ffd1d5e5 2018-06-23 stsp return NULL;
6740 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6743 ffd1d5e5 2018-06-23 stsp return err;
6744 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6745 a3404814 2018-09-02 stsp wstandout(view->window);
6746 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6748 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6749 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6750 0c6ad1bc 2022-09-09 mark free(wline);
6751 0c6ad1bc 2022-09-09 mark wline = NULL;
6752 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6753 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6755 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6756 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6757 a3404814 2018-09-02 stsp wstandend(view->window);
6758 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6759 0c6ad1bc 2022-09-09 mark return NULL;
6761 df68a56b 2022-08-12 mark i += s->selected;
6762 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6763 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6764 df68a56b 2022-08-12 mark if (s->tree != s->root)
6765 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6767 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6768 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6769 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6770 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6771 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6772 0c6ad1bc 2022-09-09 mark free(index);
6774 ce52c690 2018-06-23 stsp return err;
6775 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6776 2550e4c3 2018-07-13 stsp free(wline);
6777 2550e4c3 2018-07-13 stsp wline = NULL;
6778 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6779 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6780 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6781 ffd1d5e5 2018-06-23 stsp return NULL;
6782 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6783 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6784 a1eca9bb 2018-06-23 stsp return NULL;
6786 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6787 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6788 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6789 0cf4efb1 2018-09-29 stsp if (view->focussed)
6790 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6791 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6793 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6794 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6795 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6796 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6797 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6798 ffd1d5e5 2018-06-23 stsp return NULL;
6802 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6805 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6806 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6807 848d6979 2019-08-12 stsp const char *modestr = "";
6808 56e0773d 2019-11-28 stsp mode_t mode;
6810 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6811 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6813 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6814 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6815 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6817 638f9024 2019-05-13 stsp return got_error_from_errno(
6818 230a42bd 2019-05-11 jcs "got_object_id_str");
6820 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6821 63c5ca5d 2019-08-24 stsp modestr = "$";
6822 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6825 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6826 d86d3b18 2020-12-01 naddy te, s->repo);
6827 0d6c6ee3 2020-05-20 stsp if (err) {
6828 0d6c6ee3 2020-05-20 stsp free(id_str);
6829 0d6c6ee3 2020-05-20 stsp return err;
6831 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6832 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6833 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6835 848d6979 2019-08-12 stsp modestr = "@";
6837 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6838 848d6979 2019-08-12 stsp modestr = "/";
6839 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6840 848d6979 2019-08-12 stsp modestr = "*";
6841 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6842 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6843 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6844 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6845 1d13200f 2018-07-12 stsp free(id_str);
6846 0d6c6ee3 2020-05-20 stsp free(link_target);
6847 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6849 1d13200f 2018-07-12 stsp free(id_str);
6850 0d6c6ee3 2020-05-20 stsp free(link_target);
6851 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6853 ffd1d5e5 2018-06-23 stsp if (err) {
6854 ffd1d5e5 2018-06-23 stsp free(line);
6857 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6858 0cf4efb1 2018-09-29 stsp if (view->focussed)
6859 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6860 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6862 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6864 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6865 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6866 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6868 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6869 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6870 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6871 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6872 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6873 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6874 ffd1d5e5 2018-06-23 stsp free(line);
6875 2550e4c3 2018-07-13 stsp free(wline);
6876 2550e4c3 2018-07-13 stsp wline = NULL;
6878 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6879 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6880 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6884 ffd1d5e5 2018-06-23 stsp return err;
6887 ffd1d5e5 2018-06-23 stsp static void
6888 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6890 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6891 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6892 fa86c4bf 2020-11-29 stsp int i = 0;
6894 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6897 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6898 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6899 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6900 fa86c4bf 2020-11-29 stsp if (!isroot)
6901 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6904 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6905 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6909 9b058f45 2022-06-30 mark static const struct got_error *
6910 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6912 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6913 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6914 ffd1d5e5 2018-06-23 stsp int n = 0;
6916 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6917 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6918 694d3271 2020-12-01 naddy s->first_displayed_entry);
6920 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6922 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6923 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6924 94b80cfa 2022-08-01 mark if (last) {
6925 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6926 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6928 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6929 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6930 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6934 9b058f45 2022-06-30 mark return NULL;
6937 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6938 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6939 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6941 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6942 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6943 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6945 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6946 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6947 56e0773d 2019-11-28 stsp + 1 /* slash */;
6949 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6951 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6952 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6953 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6955 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6956 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6957 d9765a41 2018-06-23 stsp while (pt) {
6958 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6959 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6960 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6961 cb2ebc8a 2018-06-23 stsp goto done;
6963 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6964 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6965 cb2ebc8a 2018-06-23 stsp goto done;
6967 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6970 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6971 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6972 ce52c690 2018-06-23 stsp goto done;
6976 ce52c690 2018-06-23 stsp if (err) {
6977 ce52c690 2018-06-23 stsp free(*path);
6978 ce52c690 2018-06-23 stsp *path = NULL;
6980 ce52c690 2018-06-23 stsp return err;
6983 ce52c690 2018-06-23 stsp static const struct got_error *
6984 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6985 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6986 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6988 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6989 ce52c690 2018-06-23 stsp char *path;
6990 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6992 a0de39f3 2019-08-09 stsp *new_view = NULL;
6994 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6996 ce52c690 2018-06-23 stsp return err;
6998 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6999 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7000 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7001 83ce39e3 2019-08-12 stsp goto done;
7004 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7005 e5a0f69f 2018-08-18 stsp if (err) {
7006 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7007 fc06ba56 2019-08-22 stsp err = NULL;
7008 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7010 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7012 83ce39e3 2019-08-12 stsp free(path);
7013 69efd4c4 2018-07-18 stsp return err;
7016 69efd4c4 2018-07-18 stsp static const struct got_error *
7017 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7018 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7020 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7021 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7022 69efd4c4 2018-07-18 stsp char *path;
7024 a0de39f3 2019-08-09 stsp *new_view = NULL;
7026 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7027 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7028 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7030 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7032 69efd4c4 2018-07-18 stsp return err;
7034 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7037 e5a0f69f 2018-08-18 stsp view_close(log_view);
7039 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7040 cb2ebc8a 2018-06-23 stsp free(path);
7041 cb2ebc8a 2018-06-23 stsp return err;
7044 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7045 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7046 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7048 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7049 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7050 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7051 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7053 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7054 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7056 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7057 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7058 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7060 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7062 bc573f3b 2021-07-10 stsp goto done;
7065 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7066 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7067 bc573f3b 2021-07-10 stsp * closed on demand.
7069 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7070 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7072 bc573f3b 2021-07-10 stsp goto done;
7073 bc573f3b 2021-07-10 stsp s->tree = s->root;
7075 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7076 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7077 ffd1d5e5 2018-06-23 stsp goto done;
7079 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7080 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7081 ffd1d5e5 2018-06-23 stsp goto done;
7084 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7085 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7086 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7087 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7088 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7089 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7090 9cd7cbd1 2020-12-07 stsp goto done;
7093 fb2756b9 2018-08-04 stsp s->repo = repo;
7095 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7096 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7097 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7098 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7100 c0b01bdb 2019-11-08 stsp goto done;
7101 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7102 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7104 c0b01bdb 2019-11-08 stsp goto done;
7105 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7106 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7107 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7109 c0b01bdb 2019-11-08 stsp goto done;
7111 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7112 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7113 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7115 11b20872 2019-11-08 stsp goto done;
7117 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7118 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7120 c0b01bdb 2019-11-08 stsp goto done;
7123 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7124 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7125 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7126 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7127 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7129 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7130 bc573f3b 2021-07-10 stsp if (commit)
7131 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7133 bc573f3b 2021-07-10 stsp close_tree_view(view);
7134 ad80ab7b 2018-08-04 stsp return err;
7137 e5a0f69f 2018-08-18 stsp static const struct got_error *
7138 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7140 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7142 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7143 fb2756b9 2018-08-04 stsp free(s->tree_label);
7144 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7145 6484ec90 2018-09-29 stsp free(s->commit_id);
7146 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7147 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7148 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7149 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7150 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7151 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7152 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7153 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7154 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7155 ad80ab7b 2018-08-04 stsp free(parent);
7158 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7159 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7160 bc573f3b 2021-07-10 stsp if (s->root)
7161 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7162 7c32bd05 2019-06-22 stsp return NULL;
7165 7c32bd05 2019-06-22 stsp static const struct got_error *
7166 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7168 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7170 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7171 7c32bd05 2019-06-22 stsp return NULL;
7174 7c32bd05 2019-06-22 stsp static int
7175 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7177 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7179 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7183 7c32bd05 2019-06-22 stsp static const struct got_error *
7184 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7186 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7187 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7189 7c32bd05 2019-06-22 stsp if (!view->searching) {
7190 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7191 7c32bd05 2019-06-22 stsp return NULL;
7194 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7195 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7196 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7197 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7198 56e0773d 2019-11-28 stsp s->selected_entry);
7200 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7202 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7203 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7205 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7206 56e0773d 2019-11-28 stsp s->selected_entry);
7209 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7210 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7211 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7212 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7214 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7217 7c32bd05 2019-06-22 stsp while (1) {
7218 56e0773d 2019-11-28 stsp if (te == NULL) {
7219 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7220 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7221 ac66afb8 2019-06-24 stsp return NULL;
7223 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7224 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7226 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7229 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7230 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7231 56e0773d 2019-11-28 stsp s->matched_entry = te;
7235 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7236 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7238 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7241 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7242 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7243 7c32bd05 2019-06-22 stsp s->selected = 0;
7246 e5a0f69f 2018-08-18 stsp return NULL;
7249 ad80ab7b 2018-08-04 stsp static const struct got_error *
7250 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7252 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7253 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7254 e5a0f69f 2018-08-18 stsp char *parent_path;
7256 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7258 e5a0f69f 2018-08-18 stsp return err;
7260 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7261 e5a0f69f 2018-08-18 stsp free(parent_path);
7263 9b058f45 2022-06-30 mark view_border(view);
7264 e5a0f69f 2018-08-18 stsp return err;
7267 94b80cfa 2022-08-01 mark static const struct got_error *
7268 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7270 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7271 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7272 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7273 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7274 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7275 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7277 94b80cfa 2022-08-01 mark g = view->gline;
7278 94b80cfa 2022-08-01 mark view->gline = 0;
7280 94b80cfa 2022-08-01 mark if (g == 0)
7282 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7283 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7285 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7286 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7287 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7289 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7290 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7291 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7293 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7294 94b80cfa 2022-08-01 mark last += off;
7296 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7297 94b80cfa 2022-08-01 mark s->selected = g - first;
7298 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7301 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7302 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7306 94b80cfa 2022-08-01 mark if (i < g) {
7307 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7309 94b80cfa 2022-08-01 mark return err;
7310 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7311 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7312 94b80cfa 2022-08-01 mark first + s->selected < g &&
7313 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7314 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7315 94b80cfa 2022-08-01 mark first += off;
7316 94b80cfa 2022-08-01 mark s->selected = g - first;
7318 94b80cfa 2022-08-01 mark } else if (i > g)
7319 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7321 94b80cfa 2022-08-01 mark if (g < nlines &&
7322 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7323 94b80cfa 2022-08-01 mark s->selected = g - 1;
7325 94b80cfa 2022-08-01 mark return NULL;
7328 e5a0f69f 2018-08-18 stsp static const struct got_error *
7329 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7331 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7332 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7333 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7334 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7336 94b80cfa 2022-08-01 mark if (view->gline)
7337 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7339 e5a0f69f 2018-08-18 stsp switch (ch) {
7341 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7342 640cd7ff 2022-06-22 mark view->count = 0;
7345 640cd7ff 2022-06-22 mark view->count = 0;
7346 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7348 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7351 640cd7ff 2022-06-22 mark view->count = 0;
7352 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7354 e4526bf5 2021-09-03 naddy case 'g':
7356 e4526bf5 2021-09-03 naddy case KEY_HOME:
7357 e4526bf5 2021-09-03 naddy s->selected = 0;
7358 640cd7ff 2022-06-22 mark view->count = 0;
7359 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7360 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7361 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7363 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7365 e4526bf5 2021-09-03 naddy case 'G':
7367 9b058f45 2022-06-30 mark case KEY_END: {
7368 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7370 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7371 9b058f45 2022-06-30 mark --eos; /* border */
7372 e4526bf5 2021-09-03 naddy s->selected = 0;
7373 640cd7ff 2022-06-22 mark view->count = 0;
7374 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7375 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7376 e4526bf5 2021-09-03 naddy if (te == NULL) {
7377 ad055527 2022-07-16 mark if (s->tree != s->root) {
7378 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7383 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7384 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7386 e4526bf5 2021-09-03 naddy if (n > 0)
7387 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7391 1e37a5c2 2019-05-12 jcs case KEY_UP:
7392 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7393 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7394 1e37a5c2 2019-05-12 jcs s->selected--;
7397 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7398 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7399 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7400 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7401 640cd7ff 2022-06-22 mark view->count = 0;
7403 83cc4199 2022-06-13 stsp case CTRL('u'):
7405 83cc4199 2022-06-13 stsp nscroll /= 2;
7406 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7407 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7408 ea025d1d 2020-02-22 naddy case CTRL('b'):
7410 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7411 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7412 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7413 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7415 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7416 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7418 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7419 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7420 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7421 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7422 640cd7ff 2022-06-22 mark view->count = 0;
7425 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7426 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7427 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7428 1e37a5c2 2019-05-12 jcs s->selected++;
7431 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7432 640cd7ff 2022-06-22 mark == NULL) {
7433 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7434 640cd7ff 2022-06-22 mark view->count = 0;
7437 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7439 83cc4199 2022-06-13 stsp case CTRL('d'):
7441 83cc4199 2022-06-13 stsp nscroll /= 2;
7442 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7443 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7444 ea025d1d 2020-02-22 naddy case CTRL('f'):
7446 48bb96f0 2022-06-20 naddy case ' ':
7447 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7449 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7450 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7451 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7452 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7454 640cd7ff 2022-06-22 mark view->count = 0;
7457 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7459 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7461 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7462 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7463 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7464 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7465 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7466 640cd7ff 2022-06-22 mark view->count = 0;
7469 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7470 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7472 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7473 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7474 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7475 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7476 1e37a5c2 2019-05-12 jcs s->selected_entry =
7477 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7478 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7479 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7480 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7484 1e37a5c2 2019-05-12 jcs free(parent);
7485 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7486 56e0773d 2019-11-28 stsp s->selected_entry))) {
7487 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7488 640cd7ff 2022-06-22 mark view->count = 0;
7489 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7490 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7493 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7494 941e9f74 2019-05-21 stsp if (err) {
7495 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7498 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7499 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7501 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7502 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7503 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7504 640cd7ff 2022-06-22 mark view->count = 0;
7507 640cd7ff 2022-06-22 mark view->count = 0;
7511 ffd1d5e5 2018-06-23 stsp return err;
7514 ffd1d5e5 2018-06-23 stsp __dead static void
7515 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7518 87411fa9 2022-06-16 stsp fprintf(stderr,
7519 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7520 ffd1d5e5 2018-06-23 stsp getprogname());
7524 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7525 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7527 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7528 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7529 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7530 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7531 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7532 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7533 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7534 4e97c21c 2020-12-06 stsp char *label = NULL;
7535 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7536 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7538 5221c383 2018-08-01 stsp struct tog_view *view;
7539 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7541 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7542 ffd1d5e5 2018-06-23 stsp switch (ch) {
7544 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7547 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7548 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7549 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7553 e99e2d15 2020-11-24 naddy usage_tree();
7554 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7558 ffd1d5e5 2018-06-23 stsp argc -= optind;
7559 ffd1d5e5 2018-06-23 stsp argv += optind;
7561 55cccc34 2020-02-20 stsp if (argc > 1)
7562 e99e2d15 2020-11-24 naddy usage_tree();
7564 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7565 0ae84acc 2022-06-15 tracey if (error != NULL)
7566 0ae84acc 2022-06-15 tracey goto done;
7568 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7569 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7570 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7571 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7572 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7573 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7574 c156c7a4 2020-12-18 stsp goto done;
7575 55cccc34 2020-02-20 stsp if (worktree)
7576 52185f70 2019-02-05 stsp repo_path =
7577 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7579 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7580 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7581 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7582 c156c7a4 2020-12-18 stsp goto done;
7586 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7587 c02c541e 2019-03-29 stsp if (error != NULL)
7588 52185f70 2019-02-05 stsp goto done;
7590 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7591 55cccc34 2020-02-20 stsp repo, worktree);
7592 55cccc34 2020-02-20 stsp if (error)
7593 55cccc34 2020-02-20 stsp goto done;
7595 55cccc34 2020-02-20 stsp init_curses();
7597 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7598 c02c541e 2019-03-29 stsp if (error)
7599 52185f70 2019-02-05 stsp goto done;
7601 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7602 51a10b52 2020-12-26 stsp if (error)
7603 51a10b52 2020-12-26 stsp goto done;
7605 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7606 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7607 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7608 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7609 4e97c21c 2020-12-06 stsp if (error)
7610 4e97c21c 2020-12-06 stsp goto done;
7611 4e97c21c 2020-12-06 stsp head_ref_name = label;
7613 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7614 4e97c21c 2020-12-06 stsp if (error == NULL)
7615 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7616 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7617 4e97c21c 2020-12-06 stsp goto done;
7618 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7619 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7620 4e97c21c 2020-12-06 stsp if (error)
7621 4e97c21c 2020-12-06 stsp goto done;
7624 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7625 a44927cc 2022-04-07 stsp if (error)
7626 a44927cc 2022-04-07 stsp goto done;
7628 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7629 5221c383 2018-08-01 stsp if (view == NULL) {
7630 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7631 5221c383 2018-08-01 stsp goto done;
7633 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7634 ad80ab7b 2018-08-04 stsp if (error)
7635 ad80ab7b 2018-08-04 stsp goto done;
7636 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7637 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7638 d91faf3b 2020-12-01 naddy in_repo_path);
7639 55cccc34 2020-02-20 stsp if (error)
7640 55cccc34 2020-02-20 stsp goto done;
7643 55cccc34 2020-02-20 stsp if (worktree) {
7644 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7645 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7646 55cccc34 2020-02-20 stsp worktree = NULL;
7648 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7650 52185f70 2019-02-05 stsp free(repo_path);
7651 e4a0e26d 2020-02-20 stsp free(cwd);
7652 ffd1d5e5 2018-06-23 stsp free(commit_id);
7653 4e97c21c 2020-12-06 stsp free(label);
7655 486cd271 2020-12-06 stsp got_ref_close(ref);
7656 1d0f4054 2021-06-17 stsp if (repo) {
7657 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7658 1d0f4054 2021-06-17 stsp if (error == NULL)
7659 1d0f4054 2021-06-17 stsp error = close_err;
7661 0ae84acc 2022-06-15 tracey if (pack_fds) {
7662 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7663 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7664 0ae84acc 2022-06-15 tracey if (error == NULL)
7665 0ae84acc 2022-06-15 tracey error = pack_err;
7667 51a10b52 2020-12-26 stsp tog_free_refs();
7668 ffd1d5e5 2018-06-23 stsp return error;
7671 6458efa5 2020-11-24 stsp static const struct got_error *
7672 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7674 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7675 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7677 6458efa5 2020-11-24 stsp s->nrefs = 0;
7678 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7679 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7680 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7681 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7682 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7685 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7686 6458efa5 2020-11-24 stsp if (re == NULL)
7687 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7689 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7690 8924d611 2020-12-26 stsp if (re->ref == NULL)
7691 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7692 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7693 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7696 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7697 6458efa5 2020-11-24 stsp return NULL;
7701 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7703 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7705 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7706 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7707 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7708 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7713 6458efa5 2020-11-24 stsp static const struct got_error *
7714 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7716 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7717 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7719 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7720 6458efa5 2020-11-24 stsp s->repo = repo;
7722 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7723 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7725 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7727 6458efa5 2020-11-24 stsp return err;
7729 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7730 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7731 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7732 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7734 6458efa5 2020-11-24 stsp goto done;
7736 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7737 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7738 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7740 6458efa5 2020-11-24 stsp goto done;
7742 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7743 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7744 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7746 cc488aa7 2022-01-23 stsp goto done;
7748 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7749 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7750 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7752 6458efa5 2020-11-24 stsp goto done;
7755 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7756 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7757 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7758 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7759 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7762 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7763 6458efa5 2020-11-24 stsp return err;
7766 6458efa5 2020-11-24 stsp static const struct got_error *
7767 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7769 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7771 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7772 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7774 6458efa5 2020-11-24 stsp return NULL;
7777 6458efa5 2020-11-24 stsp static const struct got_error *
7778 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7779 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7781 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7782 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7783 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7784 6458efa5 2020-11-24 stsp int obj_type;
7786 c42c9805 2020-11-24 stsp *commit_id = NULL;
7788 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7790 6458efa5 2020-11-24 stsp return err;
7792 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7794 6458efa5 2020-11-24 stsp goto done;
7796 6458efa5 2020-11-24 stsp switch (obj_type) {
7797 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7798 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7800 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7801 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7803 6458efa5 2020-11-24 stsp goto done;
7804 c42c9805 2020-11-24 stsp free(obj_id);
7805 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7806 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7808 6458efa5 2020-11-24 stsp goto done;
7809 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7810 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7811 c42c9805 2020-11-24 stsp goto done;
7813 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7814 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7815 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7816 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7817 c42c9805 2020-11-24 stsp goto done;
7821 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7827 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7828 c42c9805 2020-11-24 stsp if (err) {
7829 c42c9805 2020-11-24 stsp free(*commit_id);
7830 c42c9805 2020-11-24 stsp *commit_id = NULL;
7832 c42c9805 2020-11-24 stsp return err;
7835 c42c9805 2020-11-24 stsp static const struct got_error *
7836 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7837 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7839 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7840 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7841 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7843 c42c9805 2020-11-24 stsp *new_view = NULL;
7845 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7846 c42c9805 2020-11-24 stsp if (err) {
7847 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7848 c42c9805 2020-11-24 stsp return err;
7850 c42c9805 2020-11-24 stsp return NULL;
7853 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7854 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7855 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7856 6458efa5 2020-11-24 stsp goto done;
7859 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7860 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7863 6458efa5 2020-11-24 stsp view_close(log_view);
7865 6458efa5 2020-11-24 stsp *new_view = log_view;
7866 c42c9805 2020-11-24 stsp free(commit_id);
7867 6458efa5 2020-11-24 stsp return err;
7870 ce5b7c56 2019-07-09 stsp static void
7871 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7873 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7874 34ba6917 2020-11-30 stsp int i = 0;
7876 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7879 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7880 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7881 34ba6917 2020-11-30 stsp if (re == NULL)
7883 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7884 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7888 9b058f45 2022-06-30 mark static const struct got_error *
7889 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7891 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7892 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7893 6458efa5 2020-11-24 stsp int n = 0;
7895 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7896 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7898 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7900 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7901 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7902 94b80cfa 2022-08-01 mark if (last) {
7903 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7904 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7906 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7907 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7908 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7912 9b058f45 2022-06-30 mark return NULL;
7915 6458efa5 2020-11-24 stsp static const struct got_error *
7916 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7918 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7920 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7921 6458efa5 2020-11-24 stsp return NULL;
7924 6458efa5 2020-11-24 stsp static int
7925 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7927 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7929 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7933 6458efa5 2020-11-24 stsp static const struct got_error *
7934 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7936 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7937 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7939 6458efa5 2020-11-24 stsp if (!view->searching) {
7940 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7941 6458efa5 2020-11-24 stsp return NULL;
7944 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7945 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7946 6458efa5 2020-11-24 stsp if (s->selected_entry)
7947 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7949 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7950 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7952 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7953 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7955 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7956 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7959 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7960 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7961 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7962 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7964 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7967 6458efa5 2020-11-24 stsp while (1) {
7968 6458efa5 2020-11-24 stsp if (re == NULL) {
7969 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7970 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7971 6458efa5 2020-11-24 stsp return NULL;
7973 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7974 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7976 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7979 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7980 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7981 6458efa5 2020-11-24 stsp s->matched_entry = re;
7985 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7986 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7988 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7991 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7992 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7993 6458efa5 2020-11-24 stsp s->selected = 0;
7996 6458efa5 2020-11-24 stsp return NULL;
7999 6458efa5 2020-11-24 stsp static const struct got_error *
8000 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8002 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8003 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8004 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8005 6458efa5 2020-11-24 stsp char *line = NULL;
8006 6458efa5 2020-11-24 stsp wchar_t *wline;
8007 6458efa5 2020-11-24 stsp struct tog_color *tc;
8008 6458efa5 2020-11-24 stsp int width, n;
8009 6458efa5 2020-11-24 stsp int limit = view->nlines;
8011 6458efa5 2020-11-24 stsp werase(view->window);
8013 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8014 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8015 9b058f45 2022-06-30 mark --limit; /* border */
8017 6458efa5 2020-11-24 stsp if (limit == 0)
8018 6458efa5 2020-11-24 stsp return NULL;
8020 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8022 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8023 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8024 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8026 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8027 6458efa5 2020-11-24 stsp if (err) {
8028 6458efa5 2020-11-24 stsp free(line);
8029 6458efa5 2020-11-24 stsp return err;
8031 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8032 6458efa5 2020-11-24 stsp wstandout(view->window);
8033 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8034 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8035 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8036 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8037 6458efa5 2020-11-24 stsp wstandend(view->window);
8038 6458efa5 2020-11-24 stsp free(wline);
8039 6458efa5 2020-11-24 stsp wline = NULL;
8040 6458efa5 2020-11-24 stsp free(line);
8041 6458efa5 2020-11-24 stsp line = NULL;
8042 6458efa5 2020-11-24 stsp if (--limit <= 0)
8043 6458efa5 2020-11-24 stsp return NULL;
8046 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8047 6458efa5 2020-11-24 stsp char *line = NULL;
8048 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8050 b4996bee 2022-06-16 stsp if (s->show_date) {
8051 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8052 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8053 b4996bee 2022-06-16 stsp struct got_object_id *id;
8054 b4996bee 2022-06-16 stsp struct tm tm;
8057 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8059 b4996bee 2022-06-16 stsp return err;
8060 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8061 b4996bee 2022-06-16 stsp if (err) {
8062 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8064 b4996bee 2022-06-16 stsp return err;
8066 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8068 b4996bee 2022-06-16 stsp if (err) {
8070 b4996bee 2022-06-16 stsp return err;
8072 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8073 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8075 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8076 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8079 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8080 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8081 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8082 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8084 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8085 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8086 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8087 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8088 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8089 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8090 6458efa5 2020-11-24 stsp struct got_object_id *id;
8091 6458efa5 2020-11-24 stsp char *id_str;
8092 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8094 6458efa5 2020-11-24 stsp return err;
8095 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8096 6458efa5 2020-11-24 stsp if (err) {
8098 6458efa5 2020-11-24 stsp return err;
8100 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8101 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8102 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8104 6458efa5 2020-11-24 stsp free(id_str);
8105 6458efa5 2020-11-24 stsp return err;
8108 6458efa5 2020-11-24 stsp free(id_str);
8109 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8110 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8111 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8113 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8115 6458efa5 2020-11-24 stsp if (err) {
8116 6458efa5 2020-11-24 stsp free(line);
8117 6458efa5 2020-11-24 stsp return err;
8119 6458efa5 2020-11-24 stsp if (n == s->selected) {
8120 6458efa5 2020-11-24 stsp if (view->focussed)
8121 6458efa5 2020-11-24 stsp wstandout(view->window);
8122 6458efa5 2020-11-24 stsp s->selected_entry = re;
8124 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8126 6458efa5 2020-11-24 stsp wattr_on(view->window,
8127 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8128 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8130 6458efa5 2020-11-24 stsp wattr_off(view->window,
8131 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8132 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8133 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8134 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8135 6458efa5 2020-11-24 stsp wstandend(view->window);
8136 6458efa5 2020-11-24 stsp free(line);
8137 6458efa5 2020-11-24 stsp free(wline);
8138 6458efa5 2020-11-24 stsp wline = NULL;
8140 6458efa5 2020-11-24 stsp s->ndisplayed++;
8141 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8144 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8147 9b058f45 2022-06-30 mark view_border(view);
8148 6458efa5 2020-11-24 stsp return err;
8151 6458efa5 2020-11-24 stsp static const struct got_error *
8152 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8153 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8155 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8156 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8157 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8159 c42c9805 2020-11-24 stsp *new_view = NULL;
8161 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8162 c42c9805 2020-11-24 stsp if (err) {
8163 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8164 c42c9805 2020-11-24 stsp return err;
8166 c42c9805 2020-11-24 stsp return NULL;
8170 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8171 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8172 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8173 c42c9805 2020-11-24 stsp goto done;
8176 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8177 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8179 c42c9805 2020-11-24 stsp goto done;
8181 c42c9805 2020-11-24 stsp *new_view = tree_view;
8183 c42c9805 2020-11-24 stsp free(commit_id);
8184 c42c9805 2020-11-24 stsp return err;
8187 94b80cfa 2022-08-01 mark static const struct got_error *
8188 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8190 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8191 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8192 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8194 94b80cfa 2022-08-01 mark g = view->gline;
8195 94b80cfa 2022-08-01 mark view->gline = 0;
8197 94b80cfa 2022-08-01 mark if (g == 0)
8199 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8200 94b80cfa 2022-08-01 mark g = s->nrefs;
8202 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8203 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8204 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8205 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8206 94b80cfa 2022-08-01 mark return NULL;
8209 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8210 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8212 94b80cfa 2022-08-01 mark return err;
8213 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8214 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8215 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8216 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8217 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8218 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8220 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8221 94b80cfa 2022-08-01 mark s->selected = g - 1;
8223 94b80cfa 2022-08-01 mark return NULL;
8227 c42c9805 2020-11-24 stsp static const struct got_error *
8228 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8230 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8231 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8232 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8233 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8235 94b80cfa 2022-08-01 mark if (view->gline)
8236 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8238 6458efa5 2020-11-24 stsp switch (ch) {
8240 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8241 640cd7ff 2022-06-22 mark view->count = 0;
8244 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8245 640cd7ff 2022-06-22 mark view->count = 0;
8248 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8249 640cd7ff 2022-06-22 mark view->count = 0;
8250 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8251 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8252 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8255 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8256 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8257 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8260 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8261 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8263 6458efa5 2020-11-24 stsp case KEY_ENTER:
8264 6458efa5 2020-11-24 stsp case '\r':
8265 640cd7ff 2022-06-22 mark view->count = 0;
8266 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8268 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8271 640cd7ff 2022-06-22 mark view->count = 0;
8272 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8274 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8276 e4526bf5 2021-09-03 naddy case 'g':
8278 e4526bf5 2021-09-03 naddy case KEY_HOME:
8279 e4526bf5 2021-09-03 naddy s->selected = 0;
8280 640cd7ff 2022-06-22 mark view->count = 0;
8281 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8283 e4526bf5 2021-09-03 naddy case 'G':
8285 9b058f45 2022-06-30 mark case KEY_END: {
8286 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8288 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8289 9b058f45 2022-06-30 mark --eos; /* border */
8290 e4526bf5 2021-09-03 naddy s->selected = 0;
8291 640cd7ff 2022-06-22 mark view->count = 0;
8292 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8293 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8294 e4526bf5 2021-09-03 naddy if (re == NULL)
8296 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8297 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8299 e4526bf5 2021-09-03 naddy if (n > 0)
8300 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8304 6458efa5 2020-11-24 stsp case KEY_UP:
8305 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8306 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8307 6458efa5 2020-11-24 stsp s->selected--;
8310 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8311 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8312 640cd7ff 2022-06-22 mark view->count = 0;
8314 83cc4199 2022-06-13 stsp case CTRL('u'):
8316 83cc4199 2022-06-13 stsp nscroll /= 2;
8317 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8318 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8319 6458efa5 2020-11-24 stsp case CTRL('b'):
8321 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8322 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8323 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8324 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8325 640cd7ff 2022-06-22 mark view->count = 0;
8328 6458efa5 2020-11-24 stsp case KEY_DOWN:
8329 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8330 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8331 6458efa5 2020-11-24 stsp s->selected++;
8334 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8335 6458efa5 2020-11-24 stsp /* can't scroll any further */
8336 640cd7ff 2022-06-22 mark view->count = 0;
8339 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8341 83cc4199 2022-06-13 stsp case CTRL('d'):
8343 83cc4199 2022-06-13 stsp nscroll /= 2;
8344 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8345 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8346 6458efa5 2020-11-24 stsp case CTRL('f'):
8348 48bb96f0 2022-06-20 naddy case ' ':
8349 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8350 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8351 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8352 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8353 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8354 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8355 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8356 640cd7ff 2022-06-22 mark view->count = 0;
8359 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8361 6458efa5 2020-11-24 stsp case CTRL('l'):
8362 640cd7ff 2022-06-22 mark view->count = 0;
8363 8924d611 2020-12-26 stsp tog_free_refs();
8364 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8367 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8368 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8370 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8371 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8372 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8375 640cd7ff 2022-06-22 mark view->count = 0;
8379 6458efa5 2020-11-24 stsp return err;
8382 6458efa5 2020-11-24 stsp __dead static void
8383 6458efa5 2020-11-24 stsp usage_ref(void)
8386 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8387 6458efa5 2020-11-24 stsp getprogname());
8391 6458efa5 2020-11-24 stsp static const struct got_error *
8392 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8394 6458efa5 2020-11-24 stsp const struct got_error *error;
8395 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8396 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8397 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8399 6458efa5 2020-11-24 stsp struct tog_view *view;
8400 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8402 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8403 6458efa5 2020-11-24 stsp switch (ch) {
8405 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8406 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8407 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8411 e99e2d15 2020-11-24 naddy usage_ref();
8412 6458efa5 2020-11-24 stsp /* NOTREACHED */
8416 6458efa5 2020-11-24 stsp argc -= optind;
8417 6458efa5 2020-11-24 stsp argv += optind;
8419 6458efa5 2020-11-24 stsp if (argc > 1)
8420 e99e2d15 2020-11-24 naddy usage_ref();
8422 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8423 0ae84acc 2022-06-15 tracey if (error != NULL)
8424 0ae84acc 2022-06-15 tracey goto done;
8426 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8427 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8428 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8429 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8430 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8431 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8432 c156c7a4 2020-12-18 stsp goto done;
8433 6458efa5 2020-11-24 stsp if (worktree)
8434 6458efa5 2020-11-24 stsp repo_path =
8435 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8437 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8438 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8439 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8440 c156c7a4 2020-12-18 stsp goto done;
8444 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8445 6458efa5 2020-11-24 stsp if (error != NULL)
8446 6458efa5 2020-11-24 stsp goto done;
8448 6458efa5 2020-11-24 stsp init_curses();
8450 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8451 51a10b52 2020-12-26 stsp if (error)
8452 51a10b52 2020-12-26 stsp goto done;
8454 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8455 6458efa5 2020-11-24 stsp if (error)
8456 6458efa5 2020-11-24 stsp goto done;
8458 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8459 6458efa5 2020-11-24 stsp if (view == NULL) {
8460 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8461 6458efa5 2020-11-24 stsp goto done;
8464 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8465 6458efa5 2020-11-24 stsp if (error)
8466 6458efa5 2020-11-24 stsp goto done;
8468 6458efa5 2020-11-24 stsp if (worktree) {
8469 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8470 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8471 6458efa5 2020-11-24 stsp worktree = NULL;
8473 6458efa5 2020-11-24 stsp error = view_loop(view);
8475 6458efa5 2020-11-24 stsp free(repo_path);
8476 6458efa5 2020-11-24 stsp free(cwd);
8477 1d0f4054 2021-06-17 stsp if (repo) {
8478 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8479 1d0f4054 2021-06-17 stsp if (close_err)
8480 1d0f4054 2021-06-17 stsp error = close_err;
8482 0ae84acc 2022-06-15 tracey if (pack_fds) {
8483 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8484 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8485 0ae84acc 2022-06-15 tracey if (error == NULL)
8486 0ae84acc 2022-06-15 tracey error = pack_err;
8488 51a10b52 2020-12-26 stsp tog_free_refs();
8489 6458efa5 2020-11-24 stsp return error;
8492 ec2a9698 2022-09-15 mark static const struct got_error*
8493 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8494 ec2a9698 2022-09-15 mark const char *str)
8496 ec2a9698 2022-09-15 mark size_t len;
8498 ec2a9698 2022-09-15 mark if (win == NULL)
8499 ec2a9698 2022-09-15 mark win = stdscr;
8501 ec2a9698 2022-09-15 mark len = strlen(str);
8502 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8504 ec2a9698 2022-09-15 mark if (focus)
8505 ec2a9698 2022-09-15 mark wstandout(win);
8506 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8507 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8508 ec2a9698 2022-09-15 mark if (focus)
8509 ec2a9698 2022-09-15 mark wstandend(win);
8511 ec2a9698 2022-09-15 mark return NULL;
8514 136e2bd4 2022-07-23 mark static const struct got_error *
8515 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8519 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8520 ec2a9698 2022-09-15 mark if (p == NULL) {
8521 ec2a9698 2022-09-15 mark free(*line_offsets);
8522 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8523 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8526 ec2a9698 2022-09-15 mark *line_offsets = p;
8527 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8528 ec2a9698 2022-09-15 mark ++(*nlines);
8529 ec2a9698 2022-09-15 mark return NULL;
8532 ec2a9698 2022-09-15 mark static const struct got_error *
8533 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8537 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8538 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8539 ec2a9698 2022-09-15 mark size_t len = 1;
8541 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8544 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8545 ec2a9698 2022-09-15 mark if (t0 == NULL)
8546 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8548 ec2a9698 2022-09-15 mark len += strlen(t);
8549 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8550 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8552 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8555 ec2a9698 2022-09-15 mark return NULL;
8559 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8560 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8561 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8563 ec2a9698 2022-09-15 mark static const struct got_error *
8564 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8566 ec2a9698 2022-09-15 mark int n, len = width;
8568 ec2a9698 2022-09-15 mark if (km->keys) {
8569 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8570 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8571 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8573 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8574 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8576 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8578 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8579 ec2a9698 2022-09-15 mark if (t0 == NULL)
8580 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8582 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8583 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8584 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8585 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8586 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8587 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8588 ec2a9698 2022-09-15 mark if (n < 0) {
8590 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8592 ec2a9698 2022-09-15 mark first = 0;
8593 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8594 ec2a9698 2022-09-15 mark *off += n;
8598 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8599 ec2a9698 2022-09-15 mark if (n < 0)
8600 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8601 ec2a9698 2022-09-15 mark *off += n;
8603 ec2a9698 2022-09-15 mark return NULL;
8606 ec2a9698 2022-09-15 mark static const struct got_error *
8607 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8609 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8610 ec2a9698 2022-09-15 mark off_t off = 0;
8611 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8612 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8613 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8614 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8615 ec2a9698 2022-09-15 mark GENERATE_HELP
8616 ec2a9698 2022-09-15 mark #undef KEYMAP_
8617 ec2a9698 2022-09-15 mark #undef KEY_
8620 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8622 ec2a9698 2022-09-15 mark return err;
8624 ec2a9698 2022-09-15 mark n = nitems(km);
8625 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8627 ec2a9698 2022-09-15 mark return err;
8629 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8630 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8631 ec2a9698 2022-09-15 mark show = s->all;
8632 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8633 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8636 ec2a9698 2022-09-15 mark if (show) {
8637 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8639 ec2a9698 2022-09-15 mark return err;
8640 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8642 ec2a9698 2022-09-15 mark return err;
8645 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8647 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8648 ec2a9698 2022-09-15 mark return err;
8651 ec2a9698 2022-09-15 mark static const struct got_error *
8652 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8655 ec2a9698 2022-09-15 mark const struct got_error *err;
8657 ec2a9698 2022-09-15 mark free(s->line_offsets);
8658 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8659 ec2a9698 2022-09-15 mark s->nlines = 0;
8661 ec2a9698 2022-09-15 mark f = got_opentemp();
8662 ec2a9698 2022-09-15 mark if (f == NULL)
8663 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8666 ec2a9698 2022-09-15 mark err = format_help(s);
8668 ec2a9698 2022-09-15 mark return err;
8670 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8671 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8673 ec2a9698 2022-09-15 mark return NULL;
8676 ec2a9698 2022-09-15 mark static const struct got_error *
8677 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8679 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8680 ec2a9698 2022-09-15 mark return NULL;
8683 eaf2c13e 2022-09-17 mark static void
8684 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8685 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8687 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8689 eaf2c13e 2022-09-17 mark *f = s->f;
8690 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8691 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8692 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8693 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8694 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8695 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8698 ec2a9698 2022-09-15 mark static const struct got_error *
8699 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8701 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8702 ec2a9698 2022-09-15 mark const struct got_error *err;
8703 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8704 ec2a9698 2022-09-15 mark wchar_t *wline;
8705 ec2a9698 2022-09-15 mark char *line;
8706 ec2a9698 2022-09-15 mark ssize_t linelen;
8707 ec2a9698 2022-09-15 mark size_t linesz = 0;
8708 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8709 ec2a9698 2022-09-15 mark int eos = view->nlines;
8711 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8712 ec2a9698 2022-09-15 mark --eos; /* account for border */
8714 ec2a9698 2022-09-15 mark s->lineno = 0;
8715 ec2a9698 2022-09-15 mark rewind(s->f);
8716 ec2a9698 2022-09-15 mark werase(view->window);
8718 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8719 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8721 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8722 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8723 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8725 ec2a9698 2022-09-15 mark return err;
8726 ec2a9698 2022-09-15 mark if (eos <= 1)
8727 ec2a9698 2022-09-15 mark return NULL;
8728 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8731 ec2a9698 2022-09-15 mark s->eof = 0;
8732 ec2a9698 2022-09-15 mark view->maxx = 0;
8733 ec2a9698 2022-09-15 mark line = NULL;
8734 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8735 ec2a9698 2022-09-15 mark attr_t attr = 0;
8737 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8738 ec2a9698 2022-09-15 mark if (linelen == -1) {
8739 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8740 ec2a9698 2022-09-15 mark free(line);
8741 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8743 ec2a9698 2022-09-15 mark s->eof = 1;
8746 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8748 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8750 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8751 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8753 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8754 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8755 ec2a9698 2022-09-15 mark if (err) {
8756 ec2a9698 2022-09-15 mark free(line);
8757 ec2a9698 2022-09-15 mark return err;
8759 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8760 ec2a9698 2022-09-15 mark free(wline);
8761 ec2a9698 2022-09-15 mark wline = NULL;
8764 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8765 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8766 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8767 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8768 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8769 ec2a9698 2022-09-15 mark if (err) {
8770 ec2a9698 2022-09-15 mark free(line);
8771 ec2a9698 2022-09-15 mark return err;
8776 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8777 ec2a9698 2022-09-15 mark view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8778 ec2a9698 2022-09-15 mark if (err) {
8779 ec2a9698 2022-09-15 mark free(line);
8780 ec2a9698 2022-09-15 mark return err;
8782 ec2a9698 2022-09-15 mark rc = waddwstr(view->window, &wline[skip]);
8783 ec2a9698 2022-09-15 mark free(wline);
8784 ec2a9698 2022-09-15 mark wline = NULL;
8785 ec2a9698 2022-09-15 mark if (rc == ERR)
8786 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "waddwstr");
8788 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8789 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8790 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8792 ec2a9698 2022-09-15 mark if (width <= view->ncols)
8793 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8796 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8797 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8798 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8800 ec2a9698 2022-09-15 mark free(line);
8801 ec2a9698 2022-09-15 mark if (nprinted > 0)
8802 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8804 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8806 ec2a9698 2022-09-15 mark view_border(view);
8808 ec2a9698 2022-09-15 mark if (s->eof) {
8809 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8810 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8811 ec2a9698 2022-09-15 mark view->ncols - 1);
8812 ec2a9698 2022-09-15 mark if (rc == ERR)
8813 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8815 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8816 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8817 ec2a9698 2022-09-15 mark wstandout(view->window);
8818 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8819 ec2a9698 2022-09-15 mark view->ncols - 1);
8820 ec2a9698 2022-09-15 mark if (rc == ERR)
8821 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8822 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8823 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8824 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8825 ec2a9698 2022-09-15 mark if (rc == ERR)
8826 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8828 ec2a9698 2022-09-15 mark wstandend(view->window);
8831 ec2a9698 2022-09-15 mark return NULL;
8834 ec2a9698 2022-09-15 mark static const struct got_error *
8835 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8837 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8838 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8839 ec2a9698 2022-09-15 mark char *line = NULL;
8840 ec2a9698 2022-09-15 mark ssize_t linelen;
8841 ec2a9698 2022-09-15 mark size_t linesz = 0;
8842 ec2a9698 2022-09-15 mark int eos, nscroll;
8844 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8845 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8846 ec2a9698 2022-09-15 mark --eos; /* border */
8848 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8850 ec2a9698 2022-09-15 mark switch (ch) {
8852 ec2a9698 2022-09-15 mark view->x = 0;
8855 ec2a9698 2022-09-15 mark view->x = MAX(view->maxx - view->ncols / 3, 0);
8856 ec2a9698 2022-09-15 mark view->count = 0;
8858 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8860 ec2a9698 2022-09-15 mark if (view->x + view->ncols / 3 < view->maxx)
8861 ec2a9698 2022-09-15 mark view->x += 2;
8863 ec2a9698 2022-09-15 mark view->count = 0;
8865 ec2a9698 2022-09-15 mark case KEY_LEFT:
8867 ec2a9698 2022-09-15 mark view->x -= MIN(view->x, 2);
8868 ec2a9698 2022-09-15 mark if (view->x <= 0)
8869 ec2a9698 2022-09-15 mark view->count = 0;
8872 ec2a9698 2022-09-15 mark case KEY_HOME:
8873 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8874 ec2a9698 2022-09-15 mark view->count = 0;
8877 ec2a9698 2022-09-15 mark case KEY_END:
8878 ec2a9698 2022-09-15 mark view->count = 0;
8879 ec2a9698 2022-09-15 mark if (s->eof)
8881 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8882 ec2a9698 2022-09-15 mark s->eof = 1;
8885 ec2a9698 2022-09-15 mark case KEY_UP:
8886 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8887 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8889 ec2a9698 2022-09-15 mark view->count = 0;
8891 ec2a9698 2022-09-15 mark case CTRL('u'):
8893 ec2a9698 2022-09-15 mark nscroll /= 2;
8894 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8895 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8896 ec2a9698 2022-09-15 mark case CTRL('b'):
8898 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8899 ec2a9698 2022-09-15 mark view->count = 0;
8902 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8903 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8906 ec2a9698 2022-09-15 mark case KEY_DOWN:
8907 ec2a9698 2022-09-15 mark case CTRL('n'):
8908 ec2a9698 2022-09-15 mark if (!s->eof)
8909 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8911 ec2a9698 2022-09-15 mark view->count = 0;
8913 ec2a9698 2022-09-15 mark case CTRL('d'):
8915 ec2a9698 2022-09-15 mark nscroll /= 2;
8916 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8917 ec2a9698 2022-09-15 mark case KEY_NPAGE:
8918 ec2a9698 2022-09-15 mark case CTRL('f'):
8921 ec2a9698 2022-09-15 mark if (s->eof) {
8922 ec2a9698 2022-09-15 mark view->count = 0;
8925 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
8926 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8927 ec2a9698 2022-09-15 mark s->first_displayed_line++;
8928 ec2a9698 2022-09-15 mark if (linelen == -1) {
8929 ec2a9698 2022-09-15 mark if (feof(s->f))
8930 ec2a9698 2022-09-15 mark s->eof = 1;
8932 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
8936 ec2a9698 2022-09-15 mark free(line);
8939 ec2a9698 2022-09-15 mark view->count = 0;
8943 ec2a9698 2022-09-15 mark return err;
8946 ec2a9698 2022-09-15 mark static const struct got_error *
8947 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
8949 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8951 ec2a9698 2022-09-15 mark free(s->line_offsets);
8952 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8953 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
8954 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8956 ec2a9698 2022-09-15 mark return NULL;
8959 ec2a9698 2022-09-15 mark static const struct got_error *
8960 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
8962 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8965 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
8966 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8968 ec2a9698 2022-09-15 mark wclear(view->window);
8969 ec2a9698 2022-09-15 mark view->count = 0;
8970 ec2a9698 2022-09-15 mark view->x = 0;
8971 ec2a9698 2022-09-15 mark s->all = !s->all;
8972 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8973 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8974 ec2a9698 2022-09-15 mark s->matched_line = 0;
8976 ec2a9698 2022-09-15 mark return create_help(s);
8979 ec2a9698 2022-09-15 mark static const struct got_error *
8980 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
8982 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8983 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8985 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
8986 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8987 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8988 ec2a9698 2022-09-15 mark s->selected_line = 1;
8990 ec2a9698 2022-09-15 mark view->show = show_help_view;
8991 ec2a9698 2022-09-15 mark view->input = input_help_view;
8992 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
8993 ec2a9698 2022-09-15 mark view->close = close_help_view;
8994 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
8995 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
8996 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
8998 ec2a9698 2022-09-15 mark err = create_help(s);
8999 ec2a9698 2022-09-15 mark return err;
9002 ec2a9698 2022-09-15 mark static const struct got_error *
9003 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9004 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
9006 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
9008 136e2bd4 2022-07-23 mark *new_view = NULL;
9010 136e2bd4 2022-07-23 mark switch (request) {
9011 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
9012 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9013 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9015 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9016 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9017 136e2bd4 2022-07-23 mark view, s->repo);
9019 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9020 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9022 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9023 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9024 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9026 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9027 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9030 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9031 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9033 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9034 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9035 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9036 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9037 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9038 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9039 136e2bd4 2022-07-23 mark &view->state.tree);
9040 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9041 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9042 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9043 136e2bd4 2022-07-23 mark view->state.ref.repo);
9045 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9046 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9048 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9049 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9050 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9051 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9052 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9053 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9054 136e2bd4 2022-07-23 mark view->state.log.repo);
9055 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9056 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9057 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9058 136e2bd4 2022-07-23 mark view->state.ref.repo);
9060 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9061 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9063 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9064 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9065 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9066 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9067 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9068 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9069 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9070 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9072 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9073 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9075 136e2bd4 2022-07-23 mark view_close(*new_view);
9077 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9078 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9079 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9080 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9081 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9083 ec2a9698 2022-09-15 mark view_close(*new_view);
9086 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9089 136e2bd4 2022-07-23 mark return err;
9093 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9094 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9096 6458efa5 2020-11-24 stsp static void
9097 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9099 9b058f45 2022-06-30 mark switch (view->type) {
9100 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9101 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9102 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9103 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9107 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9108 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9110 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9111 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9114 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9115 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9116 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9118 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9119 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9120 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9122 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9123 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9124 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9130 9b058f45 2022-06-30 mark view->offset = 0;
9134 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9135 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9137 9b058f45 2022-06-30 mark static const struct got_error *
9138 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9140 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9141 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9142 9b058f45 2022-06-30 mark int *selected = NULL;
9143 9b058f45 2022-06-30 mark int header, offset;
9145 9b058f45 2022-06-30 mark switch (view->type) {
9146 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9147 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9148 9b058f45 2022-06-30 mark header = 3;
9149 9b058f45 2022-06-30 mark scrolld = NULL;
9150 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9151 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9152 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9153 9b058f45 2022-06-30 mark s->selected_line -= offset;
9154 9b058f45 2022-06-30 mark view->offset = offset;
9158 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9159 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9160 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9161 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9162 9b058f45 2022-06-30 mark selected = &s->selected;
9165 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9166 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9167 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9168 9b058f45 2022-06-30 mark header = 3;
9169 9b058f45 2022-06-30 mark selected = &s->selected;
9172 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9173 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9174 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9175 9b058f45 2022-06-30 mark header = 5;
9176 9b058f45 2022-06-30 mark selected = &s->selected;
9180 9b058f45 2022-06-30 mark selected = NULL;
9181 9b058f45 2022-06-30 mark scrolld = NULL;
9182 9b058f45 2022-06-30 mark header = 0;
9186 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9187 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9188 9b058f45 2022-06-30 mark view->offset = offset;
9189 9b058f45 2022-06-30 mark if (scrolld && offset) {
9190 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9191 9b058f45 2022-06-30 mark *selected -= offset;
9195 9b058f45 2022-06-30 mark return err;
9198 9b058f45 2022-06-30 mark static void
9199 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9203 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9204 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9205 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9206 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9208 6879ba42 2020-10-01 naddy fputc('\n', fp);
9211 4ed7e80c 2018-05-20 stsp __dead static void
9212 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9214 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9216 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9217 6879ba42 2020-10-01 naddy getprogname());
9218 6879ba42 2020-10-01 naddy if (hflag) {
9219 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9220 6879ba42 2020-10-01 naddy list_commands(fp);
9222 6879ba42 2020-10-01 naddy exit(status);
9225 c2301be8 2018-04-30 stsp static char **
9226 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9228 ee85c5e8 2020-02-29 stsp va_list ap;
9229 c2301be8 2018-04-30 stsp char **argv;
9232 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9234 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9235 c2301be8 2018-04-30 stsp if (argv == NULL)
9236 c2301be8 2018-04-30 stsp err(1, "calloc");
9237 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9238 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9239 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9240 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9243 ee85c5e8 2020-02-29 stsp va_end(ap);
9244 c2301be8 2018-04-30 stsp return argv;
9248 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9249 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9250 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9251 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9252 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9254 ee85c5e8 2020-02-29 stsp static const struct got_error *
9255 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9257 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9258 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9259 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9260 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9261 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9262 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9263 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9264 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9265 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9267 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9268 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9269 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9271 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9272 0ae84acc 2022-06-15 tracey if (error != NULL)
9273 0ae84acc 2022-06-15 tracey goto done;
9275 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9276 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9277 ee85c5e8 2020-02-29 stsp goto done;
9279 ee85c5e8 2020-02-29 stsp if (worktree)
9280 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9282 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9283 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9284 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9285 ee85c5e8 2020-02-29 stsp goto done;
9288 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9289 ee85c5e8 2020-02-29 stsp if (error != NULL)
9290 ee85c5e8 2020-02-29 stsp goto done;
9292 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9293 ee85c5e8 2020-02-29 stsp repo, worktree);
9294 ee85c5e8 2020-02-29 stsp if (error)
9295 ee85c5e8 2020-02-29 stsp goto done;
9297 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9298 84de9106 2020-12-26 stsp if (error)
9299 84de9106 2020-12-26 stsp goto done;
9300 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9301 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9302 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9303 ee85c5e8 2020-02-29 stsp if (error)
9304 ee85c5e8 2020-02-29 stsp goto done;
9306 ee85c5e8 2020-02-29 stsp if (worktree) {
9307 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9308 ee85c5e8 2020-02-29 stsp worktree = NULL;
9311 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9312 a44927cc 2022-04-07 stsp if (error)
9313 a44927cc 2022-04-07 stsp goto done;
9315 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9316 ee85c5e8 2020-02-29 stsp if (error) {
9317 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9318 ee85c5e8 2020-02-29 stsp goto done;
9319 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9320 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9321 6879ba42 2020-10-01 naddy usage(1, 1);
9322 ee85c5e8 2020-02-29 stsp /* not reached */
9325 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9326 ee85c5e8 2020-02-29 stsp if (error)
9327 ee85c5e8 2020-02-29 stsp goto done;
9329 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9331 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9332 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9334 1d0f4054 2021-06-17 stsp if (repo) {
9335 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9336 1d0f4054 2021-06-17 stsp if (error == NULL)
9337 1d0f4054 2021-06-17 stsp error = close_err;
9339 a44927cc 2022-04-07 stsp if (commit)
9340 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9341 ee85c5e8 2020-02-29 stsp if (worktree)
9342 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9343 0ae84acc 2022-06-15 tracey if (pack_fds) {
9344 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9345 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9346 0ae84acc 2022-06-15 tracey if (error == NULL)
9347 0ae84acc 2022-06-15 tracey error = pack_err;
9350 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9351 ee85c5e8 2020-02-29 stsp free(commit_id);
9352 ee85c5e8 2020-02-29 stsp free(cwd);
9353 ee85c5e8 2020-02-29 stsp free(repo_path);
9354 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9355 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9357 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9358 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9359 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9361 87670572 2020-12-26 naddy tog_free_refs();
9362 ee85c5e8 2020-02-29 stsp return error;
9366 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9368 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9369 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9370 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9371 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9372 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9373 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9374 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9376 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9378 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9379 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9381 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9383 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9384 9f7d7167 2018-04-29 stsp switch (ch) {
9386 9f7d7167 2018-04-29 stsp hflag = 1;
9389 53ccebc2 2019-07-30 stsp Vflag = 1;
9392 6879ba42 2020-10-01 naddy usage(hflag, 1);
9393 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9397 9f7d7167 2018-04-29 stsp argc -= optind;
9398 9f7d7167 2018-04-29 stsp argv += optind;
9399 9814e6a3 2020-09-27 naddy optind = 1;
9400 c2301be8 2018-04-30 stsp optreset = 1;
9402 53ccebc2 2019-07-30 stsp if (Vflag) {
9403 53ccebc2 2019-07-30 stsp got_version_print_str();
9404 6879ba42 2020-10-01 naddy return 0;
9407 4010e238 2020-12-04 stsp #ifndef PROFILE
9408 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9409 4010e238 2020-12-04 stsp NULL) == -1)
9410 4010e238 2020-12-04 stsp err(1, "pledge");
9413 c2301be8 2018-04-30 stsp if (argc == 0) {
9414 f29d3e89 2018-06-23 stsp if (hflag)
9415 6879ba42 2020-10-01 naddy usage(hflag, 0);
9416 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9417 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9419 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9423 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9424 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9425 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9426 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9427 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9433 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9434 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9435 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9436 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9437 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9438 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9441 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9442 ee85c5e8 2020-02-29 stsp if (argc != 1)
9443 6879ba42 2020-10-01 naddy usage(0, 1);
9444 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9445 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9447 ee85c5e8 2020-02-29 stsp if (hflag)
9448 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9450 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9454 b46c1e04 2020-09-20 naddy putchar('\n');
9455 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9457 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9458 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9459 a2f4a359 2020-02-28 stsp free(cmd_argv);
9462 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9463 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9464 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9465 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9466 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9467 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);