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 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4464 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4466 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4468 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4469 fe621944 2020-11-10 stsp if (p == NULL)
4470 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4471 c7d5c43c 2022-08-04 mark *lines = p;
4472 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4473 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4474 fe621944 2020-11-10 stsp (*nlines)++;
4476 fe621944 2020-11-10 stsp return NULL;
4479 fe621944 2020-11-10 stsp static const struct got_error *
4480 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4481 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4483 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4484 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4485 1f3405c9 2023-01-17 mark size_t i, r;
4487 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4488 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4490 1f3405c9 2023-01-17 mark for (;;) {
4491 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4492 1f3405c9 2023-01-17 mark if (r == 0) {
4493 1f3405c9 2023-01-17 mark if (ferror(src))
4494 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4495 1f3405c9 2023-01-17 mark if (feof(src))
4498 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4499 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4503 1f3405c9 2023-01-17 mark * The diff driver initialises the first line at offset zero when the
4504 1f3405c9 2023-01-17 mark * array isn't prepopulated, skip it; we already have it in *d_lines.
4506 1f3405c9 2023-01-17 mark for (i = 1; i < s_nlines; ++i)
4507 1f3405c9 2023-01-17 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4509 1f3405c9 2023-01-17 mark --s_nlines;
4511 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4512 1f3405c9 2023-01-17 mark if (p == NULL) {
4513 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4514 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4517 1f3405c9 2023-01-17 mark *d_lines = p;
4519 1f3405c9 2023-01-17 mark memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4520 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4522 1f3405c9 2023-01-17 mark return NULL;
4525 1f3405c9 2023-01-17 mark static const struct got_error *
4526 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4527 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4528 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4529 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4531 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4532 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4533 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4534 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4535 45d799e2 2018-12-23 stsp time_t committer_time;
4536 45d799e2 2018-12-23 stsp const char *author, *committer;
4537 8b473291 2019-02-21 stsp char *refs_str = NULL;
4538 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4539 fe621944 2020-11-10 stsp off_t outoff = 0;
4542 8b473291 2019-02-21 stsp if (refs) {
4543 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4545 8b473291 2019-02-21 stsp return err;
4548 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4550 abd2672a 2018-12-23 stsp return err;
4552 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4553 15a94983 2018-12-23 stsp if (err) {
4554 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4555 15a94983 2018-12-23 stsp goto done;
4558 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4560 fe621944 2020-11-10 stsp goto done;
4562 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4563 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4564 fe621944 2020-11-10 stsp if (n < 0) {
4565 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4566 abd2672a 2018-12-23 stsp goto done;
4568 fe621944 2020-11-10 stsp outoff += n;
4569 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4571 fe621944 2020-11-10 stsp goto done;
4573 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4574 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4575 fe621944 2020-11-10 stsp if (n < 0) {
4576 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4577 abd2672a 2018-12-23 stsp goto done;
4579 fe621944 2020-11-10 stsp outoff += n;
4580 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4582 fe621944 2020-11-10 stsp goto done;
4584 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4585 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4586 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4587 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4588 fe621944 2020-11-10 stsp if (n < 0) {
4589 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4590 fe621944 2020-11-10 stsp goto done;
4592 fe621944 2020-11-10 stsp outoff += n;
4593 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4594 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4596 fe621944 2020-11-10 stsp goto done;
4598 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4599 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4600 1a99e0b4 2023-01-07 stsp if (datestr) {
4601 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4602 fe621944 2020-11-10 stsp if (n < 0) {
4603 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4604 fe621944 2020-11-10 stsp goto done;
4606 fe621944 2020-11-10 stsp outoff += n;
4607 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4608 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4610 fe621944 2020-11-10 stsp goto done;
4612 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4613 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4614 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4615 9f98ca05 2021-09-24 stsp int pn = 1;
4616 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4617 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4618 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4620 9f98ca05 2021-09-24 stsp goto done;
4621 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4622 9f98ca05 2021-09-24 stsp if (n < 0) {
4623 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4624 9f98ca05 2021-09-24 stsp goto done;
4626 9f98ca05 2021-09-24 stsp outoff += n;
4627 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4628 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4630 9f98ca05 2021-09-24 stsp goto done;
4631 9f98ca05 2021-09-24 stsp free(id_str);
4632 9f98ca05 2021-09-24 stsp id_str = NULL;
4636 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4638 5943eee2 2019-08-13 stsp goto done;
4639 fe621944 2020-11-10 stsp s = logmsg;
4640 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4641 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4642 fe621944 2020-11-10 stsp if (n < 0) {
4643 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4644 fe621944 2020-11-10 stsp goto done;
4646 fe621944 2020-11-10 stsp outoff += n;
4647 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4648 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4650 fe621944 2020-11-10 stsp goto done;
4653 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
4654 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4655 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
4657 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4658 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4659 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
4660 fe621944 2020-11-10 stsp if (n < 0) {
4661 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4662 fe621944 2020-11-10 stsp goto done;
4664 fe621944 2020-11-10 stsp outoff += n;
4665 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4666 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4668 fe621944 2020-11-10 stsp goto done;
4671 0208f208 2020-05-05 stsp fputc('\n', outfile);
4673 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4675 5191b70b 2023-01-07 mark goto done;
4677 5191b70b 2023-01-07 mark n = fprintf(outfile,
4678 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4679 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4680 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4681 5191b70b 2023-01-07 mark if (n < 0) {
4682 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
4683 5191b70b 2023-01-07 mark goto done;
4685 5191b70b 2023-01-07 mark outoff += n;
4686 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4688 5191b70b 2023-01-07 mark goto done;
4690 5191b70b 2023-01-07 mark fputc('\n', outfile);
4692 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4694 abd2672a 2018-12-23 stsp free(id_str);
4695 5943eee2 2019-08-13 stsp free(logmsg);
4696 8b473291 2019-02-21 stsp free(refs_str);
4697 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4698 7510f233 2020-08-09 stsp if (err) {
4699 c7d5c43c 2022-08-04 mark free(*lines);
4700 c7d5c43c 2022-08-04 mark *lines = NULL;
4701 ae6a6978 2020-08-09 stsp *nlines = 0;
4703 fe621944 2020-11-10 stsp return err;
4706 abd2672a 2018-12-23 stsp static const struct got_error *
4707 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4709 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4710 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
4711 15a94983 2018-12-23 stsp int obj_type;
4712 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
4713 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
4715 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
4717 c7d5c43c 2022-08-04 mark free(s->lines);
4718 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4719 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4720 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4721 fe621944 2020-11-10 stsp s->nlines = 0;
4723 511a516b 2018-05-19 stsp f = got_opentemp();
4724 48ae06ee 2018-10-18 stsp if (f == NULL) {
4725 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4726 48ae06ee 2018-10-18 stsp goto done;
4728 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
4729 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
4730 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
4731 1f3405c9 2023-01-17 mark goto done;
4733 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4734 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4735 fb43ecf1 2019-02-11 stsp goto done;
4739 15a94983 2018-12-23 stsp if (s->id1)
4740 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4742 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4744 15a94983 2018-12-23 stsp goto done;
4746 15a94983 2018-12-23 stsp switch (obj_type) {
4747 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4748 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4749 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4750 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4751 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4754 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4755 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4756 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4757 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4758 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
4760 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4761 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4762 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4763 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4764 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4765 1f3405c9 2023-01-17 mark size_t nlines = 0;
4766 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
4767 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
4768 1f3405c9 2023-01-17 mark &changed_paths,
4769 1f3405c9 2023-01-17 mark s->ignore_whitespace,
4770 1f3405c9 2023-01-17 mark s->force_text_diff,
4771 1f3405c9 2023-01-17 mark tog_diff_algo
4774 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
4775 1f3405c9 2023-01-17 mark if (lines == NULL) {
4776 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
4777 1f3405c9 2023-01-17 mark goto done;
4780 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
4781 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
4782 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4783 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
4784 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4788 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4790 3ffacbe1 2020-02-02 tracey goto done;
4791 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4792 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4793 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4794 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4795 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
4796 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4797 f44b1f58 2020-02-02 tracey if (err)
4798 f44b1f58 2020-02-02 tracey goto done;
4799 f44b1f58 2020-02-02 tracey } else {
4800 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4801 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4802 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4803 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4804 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4805 5191b70b 2023-01-07 mark s->ignore_whitespace,
4806 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4807 f44b1f58 2020-02-02 tracey if (err)
4808 f44b1f58 2020-02-02 tracey goto done;
4813 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4815 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4816 1f3405c9 2023-01-17 mark lines, nlines);
4820 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4824 1f3405c9 2023-01-17 mark free(lines);
4825 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4826 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4827 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4828 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4829 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
4830 48ae06ee 2018-10-18 stsp return err;
4833 f5215bb9 2019-02-22 stsp static void
4834 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4836 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4837 f5215bb9 2019-02-22 stsp update_panels();
4838 f5215bb9 2019-02-22 stsp doupdate();
4841 f44b1f58 2020-02-02 tracey static const struct got_error *
4842 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4844 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4846 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4847 f44b1f58 2020-02-02 tracey return NULL;
4850 eaf2c13e 2022-09-17 mark static void
4851 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4852 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4854 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4856 eaf2c13e 2022-09-17 mark *f = s->f;
4857 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4858 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4859 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4860 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4861 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4862 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4865 ec2a9698 2022-09-15 mark static const struct got_error *
4866 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4868 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4870 f44b1f58 2020-02-02 tracey int lineno;
4871 cb713507 2022-06-16 stsp char *line = NULL;
4872 826082fe 2020-12-10 stsp size_t linesize = 0;
4873 826082fe 2020-12-10 stsp ssize_t linelen;
4874 ec2a9698 2022-09-15 mark off_t *line_offsets;
4875 ec2a9698 2022-09-15 mark size_t nlines = 0;
4876 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4878 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4879 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4880 eaf2c13e 2022-09-17 mark "view search not supported");
4881 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4882 ec2a9698 2022-09-15 mark &match, &selected);
4884 f44b1f58 2020-02-02 tracey if (!view->searching) {
4885 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4886 f44b1f58 2020-02-02 tracey return NULL;
4889 ec2a9698 2022-09-15 mark if (*match) {
4890 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4891 ec2a9698 2022-09-15 mark lineno = *match + 1;
4893 ec2a9698 2022-09-15 mark lineno = *match - 1;
4895 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4897 f44b1f58 2020-02-02 tracey while (1) {
4898 f44b1f58 2020-02-02 tracey off_t offset;
4900 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4901 ec2a9698 2022-09-15 mark if (*match == 0) {
4902 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4906 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4907 f44b1f58 2020-02-02 tracey lineno = 1;
4909 ec2a9698 2022-09-15 mark lineno = nlines;
4912 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
4913 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
4914 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
4915 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
4916 f44b1f58 2020-02-02 tracey free(line);
4917 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4919 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
4920 cb713507 2022-06-16 stsp if (linelen != -1) {
4921 cb713507 2022-06-16 stsp char *exstr;
4922 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4925 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4926 cb713507 2022-06-16 stsp &view->regmatch)) {
4927 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4928 ec2a9698 2022-09-15 mark *match = lineno;
4929 cb713507 2022-06-16 stsp free(exstr);
4932 cb713507 2022-06-16 stsp free(exstr);
4934 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4935 f44b1f58 2020-02-02 tracey lineno++;
4937 f44b1f58 2020-02-02 tracey lineno--;
4939 826082fe 2020-12-10 stsp free(line);
4941 ec2a9698 2022-09-15 mark if (*match) {
4942 ec2a9698 2022-09-15 mark *first = *match;
4943 ec2a9698 2022-09-15 mark *selected = 1;
4946 145b6838 2022-06-16 stsp return err;
4949 48ae06ee 2018-10-18 stsp static const struct got_error *
4950 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4952 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4953 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4955 b72706c3 2022-06-01 stsp free(s->id1);
4956 b72706c3 2022-06-01 stsp s->id1 = NULL;
4957 b72706c3 2022-06-01 stsp free(s->id2);
4958 b72706c3 2022-06-01 stsp s->id2 = NULL;
4959 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4960 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4961 b72706c3 2022-06-01 stsp s->f = NULL;
4962 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4963 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4964 b72706c3 2022-06-01 stsp s->f1 = NULL;
4965 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4966 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4967 b72706c3 2022-06-01 stsp s->f2 = NULL;
4968 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4969 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4970 f9d37699 2022-06-28 stsp s->fd1 = -1;
4971 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4972 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4973 f9d37699 2022-06-28 stsp s->fd2 = -1;
4974 c7d5c43c 2022-08-04 mark free(s->lines);
4975 c7d5c43c 2022-08-04 mark s->lines = NULL;
4976 b72706c3 2022-06-01 stsp s->nlines = 0;
4977 b72706c3 2022-06-01 stsp return err;
4980 b72706c3 2022-06-01 stsp static const struct got_error *
4981 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4982 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4983 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4984 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4986 48ae06ee 2018-10-18 stsp const struct got_error *err;
4987 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4989 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4990 f9d37699 2022-06-28 stsp s->fd1 = -1;
4991 f9d37699 2022-06-28 stsp s->fd2 = -1;
4993 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4994 15a94983 2018-12-23 stsp int type1, type2;
4995 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4997 15a94983 2018-12-23 stsp return err;
4998 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5000 15a94983 2018-12-23 stsp return err;
5002 15a94983 2018-12-23 stsp if (type1 != type2)
5003 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5005 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5006 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5007 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5008 f44b1f58 2020-02-02 tracey s->repo = repo;
5009 f44b1f58 2020-02-02 tracey s->id1 = id1;
5010 f44b1f58 2020-02-02 tracey s->id2 = id2;
5011 3dbaef42 2020-11-24 stsp s->label1 = label1;
5012 3dbaef42 2020-11-24 stsp s->label2 = label2;
5014 15a94983 2018-12-23 stsp if (id1) {
5015 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5016 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5017 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5019 5465d566 2020-02-01 tracey s->id1 = NULL;
5021 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5022 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5023 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5024 b72706c3 2022-06-01 stsp goto done;
5027 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5028 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5029 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5030 a00719e9 2022-06-17 stsp goto done;
5033 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5034 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5035 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5036 b72706c3 2022-06-01 stsp goto done;
5039 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5040 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5041 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5042 f9d37699 2022-06-28 stsp goto done;
5045 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5046 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5047 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5048 f9d37699 2022-06-28 stsp goto done;
5051 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5052 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5053 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5054 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5055 5465d566 2020-02-01 tracey s->repo = repo;
5057 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5060 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5061 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5062 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5063 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5064 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5065 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5066 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5067 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5068 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5069 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5070 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5071 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5072 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5073 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5074 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5075 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5076 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5077 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5078 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5079 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5080 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5081 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5082 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5083 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5084 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5085 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5086 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5087 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5088 b72706c3 2022-06-01 stsp goto done;
5092 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5093 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5094 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5095 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5097 5465d566 2020-02-01 tracey err = create_diff(s);
5099 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5100 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5101 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5102 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5103 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5104 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5105 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5108 b72706c3 2022-06-01 stsp close_diff_view(view);
5109 e5a0f69f 2018-08-18 stsp return err;
5112 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5113 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5115 a3404814 2018-09-02 stsp const struct got_error *err;
5116 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5117 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5118 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5120 a3404814 2018-09-02 stsp if (s->id1) {
5121 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5123 a3404814 2018-09-02 stsp return err;
5124 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5126 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5128 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5130 a3404814 2018-09-02 stsp return err;
5131 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5133 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5134 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5135 a3404814 2018-09-02 stsp free(id_str1);
5136 a3404814 2018-09-02 stsp free(id_str2);
5137 a3404814 2018-09-02 stsp return err;
5139 a3404814 2018-09-02 stsp free(id_str1);
5140 a3404814 2018-09-02 stsp free(id_str2);
5142 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5143 267bb3b8 2021-08-01 stsp free(header);
5144 267bb3b8 2021-08-01 stsp return err;
5147 15a087fe 2019-02-21 stsp static const struct got_error *
5148 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5149 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5151 d7a04538 2019-02-21 stsp const struct got_error *err;
5152 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5153 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5154 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5156 15a087fe 2019-02-21 stsp free(s->id2);
5157 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5158 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5159 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5161 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5163 d7a04538 2019-02-21 stsp return err;
5164 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5165 15a087fe 2019-02-21 stsp free(s->id1);
5166 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5167 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5168 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5169 15a087fe 2019-02-21 stsp return NULL;
5172 0cf4efb1 2018-09-29 stsp static const struct got_error *
5173 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5175 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5177 917d79a7 2022-07-01 stsp view->count = 0;
5178 917d79a7 2022-07-01 stsp wclear(view->window);
5179 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5180 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5181 917d79a7 2022-07-01 stsp s->matched_line = 0;
5182 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5183 917d79a7 2022-07-01 stsp return create_diff(s);
5186 c7d5c43c 2022-08-04 mark static void
5187 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5189 c7d5c43c 2022-08-04 mark int start, i;
5191 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5193 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5194 c7d5c43c 2022-08-04 mark if (i == 0)
5195 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5196 c7d5c43c 2022-08-04 mark if (--i == start)
5197 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5200 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5201 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5204 c7d5c43c 2022-08-04 mark static void
5205 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5207 c7d5c43c 2022-08-04 mark int start, i;
5209 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5211 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5212 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5214 c7d5c43c 2022-08-04 mark if (++i == start)
5215 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5218 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5219 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5222 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5223 c0f61fa4 2022-07-11 mark int, int, int);
5224 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5225 c0f61fa4 2022-07-11 mark int, int);
5227 917d79a7 2022-07-01 stsp static const struct got_error *
5228 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5230 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5231 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5232 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5233 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5234 826082fe 2020-12-10 stsp char *line = NULL;
5235 826082fe 2020-12-10 stsp size_t linesize = 0;
5236 826082fe 2020-12-10 stsp ssize_t linelen;
5237 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5239 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5241 e5a0f69f 2018-08-18 stsp switch (ch) {
5243 145b6838 2022-06-16 stsp view->x = 0;
5246 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5247 640cd7ff 2022-06-22 mark view->count = 0;
5249 145b6838 2022-06-16 stsp case KEY_RIGHT:
5251 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5252 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5254 640cd7ff 2022-06-22 mark view->count = 0;
5256 145b6838 2022-06-16 stsp case KEY_LEFT:
5258 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5259 640cd7ff 2022-06-22 mark if (view->x <= 0)
5260 640cd7ff 2022-06-22 mark view->count = 0;
5264 3dbaef42 2020-11-24 stsp if (ch == 'a')
5265 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5266 5191b70b 2023-01-07 mark else if (ch == 'w')
5267 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5268 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5271 912a3f79 2021-08-30 j case KEY_HOME:
5272 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5273 640cd7ff 2022-06-22 mark view->count = 0;
5276 912a3f79 2021-08-30 j case KEY_END:
5277 640cd7ff 2022-06-22 mark view->count = 0;
5281 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5285 1e37a5c2 2019-05-12 jcs case KEY_UP:
5286 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5287 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5288 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5290 640cd7ff 2022-06-22 mark view->count = 0;
5292 83cc4199 2022-06-13 stsp case CTRL('u'):
5294 83cc4199 2022-06-13 stsp nscroll /= 2;
5295 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5296 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5297 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5299 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5300 640cd7ff 2022-06-22 mark view->count = 0;
5304 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5305 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5308 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5309 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5310 1e37a5c2 2019-05-12 jcs if (!s->eof)
5311 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5313 640cd7ff 2022-06-22 mark view->count = 0;
5315 83cc4199 2022-06-13 stsp case CTRL('d'):
5317 83cc4199 2022-06-13 stsp nscroll /= 2;
5318 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5319 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5320 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5323 640cd7ff 2022-06-22 mark if (s->eof) {
5324 640cd7ff 2022-06-22 mark view->count = 0;
5328 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5329 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5330 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5331 826082fe 2020-12-10 stsp if (linelen == -1) {
5332 826082fe 2020-12-10 stsp if (feof(s->f)) {
5333 826082fe 2020-12-10 stsp s->eof = 1;
5335 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5339 826082fe 2020-12-10 stsp free(line);
5342 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5345 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5348 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5351 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5354 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5355 1e37a5c2 2019-05-12 jcs s->diff_context--;
5356 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5357 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5358 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5359 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5360 27829c9e 2020-11-21 stsp s->nlines) {
5361 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5362 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5365 640cd7ff 2022-06-22 mark view->count = 0;
5368 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5369 1e37a5c2 2019-05-12 jcs s->diff_context++;
5370 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5371 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5372 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5374 640cd7ff 2022-06-22 mark view->count = 0;
5380 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5384 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5385 640cd7ff 2022-06-22 mark view->count = 0;
5388 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5390 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5391 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5392 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5394 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5395 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5398 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5400 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5403 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5406 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5407 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5408 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5410 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5411 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5412 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5414 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5415 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5418 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5420 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5422 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5423 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5424 c0f61fa4 2022-07-11 mark bs->selected_line);
5425 c0f61fa4 2022-07-11 mark if (id == NULL)
5428 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5431 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5435 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5436 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5437 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5438 145b6838 2022-06-16 stsp view->x = 0;
5440 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5441 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5444 640cd7ff 2022-06-22 mark view->count = 0;
5448 bcbd79e2 2018-08-19 stsp return err;
5451 4ed7e80c 2018-05-20 stsp static const struct got_error *
5452 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5454 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5455 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5456 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5457 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5458 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5459 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5460 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5461 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5462 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5463 3dbaef42 2020-11-24 stsp const char *errstr;
5464 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5465 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5467 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5468 26ed57b2 2018-05-19 stsp switch (ch) {
5470 64453f7e 2020-11-21 stsp force_text_diff = 1;
5473 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5475 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5476 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5477 5a20d08d 2022-02-09 op errstr, errstr);
5479 09b5bff8 2020-02-23 naddy case 'r':
5480 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5481 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5482 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5484 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5487 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5490 17020d27 2019-03-07 stsp usage_diff();
5491 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5495 26ed57b2 2018-05-19 stsp argc -= optind;
5496 26ed57b2 2018-05-19 stsp argv += optind;
5498 26ed57b2 2018-05-19 stsp if (argc == 0) {
5499 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5500 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5501 15a94983 2018-12-23 stsp id_str1 = argv[0];
5502 15a94983 2018-12-23 stsp id_str2 = argv[1];
5504 26ed57b2 2018-05-19 stsp usage_diff();
5506 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5507 0ae84acc 2022-06-15 tracey if (error)
5508 0ae84acc 2022-06-15 tracey goto done;
5510 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5511 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5512 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5513 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5514 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5515 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5516 c156c7a4 2020-12-18 stsp goto done;
5517 a273ac94 2020-02-23 naddy if (worktree)
5518 a273ac94 2020-02-23 naddy repo_path =
5519 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5521 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5522 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5523 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5524 c156c7a4 2020-12-18 stsp goto done;
5528 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5529 eb6600df 2019-01-04 stsp if (error)
5530 eb6600df 2019-01-04 stsp goto done;
5532 a273ac94 2020-02-23 naddy init_curses();
5534 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5535 51a10b52 2020-12-26 stsp if (error)
5536 51a10b52 2020-12-26 stsp goto done;
5538 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5539 26ed57b2 2018-05-19 stsp if (error)
5540 26ed57b2 2018-05-19 stsp goto done;
5542 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5543 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5544 26ed57b2 2018-05-19 stsp if (error)
5545 26ed57b2 2018-05-19 stsp goto done;
5547 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5548 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5549 26ed57b2 2018-05-19 stsp if (error)
5550 26ed57b2 2018-05-19 stsp goto done;
5552 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5553 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5554 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5555 ea5e7bb5 2018-08-01 stsp goto done;
5557 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5558 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5559 5dc9f4bc 2018-08-04 stsp if (error)
5560 5dc9f4bc 2018-08-04 stsp goto done;
5561 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5563 3dbaef42 2020-11-24 stsp free(label1);
5564 3dbaef42 2020-11-24 stsp free(label2);
5565 c02c541e 2019-03-29 stsp free(repo_path);
5566 a273ac94 2020-02-23 naddy free(cwd);
5567 1d0f4054 2021-06-17 stsp if (repo) {
5568 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5569 1d0f4054 2021-06-17 stsp if (error == NULL)
5570 1d0f4054 2021-06-17 stsp error = close_err;
5572 a273ac94 2020-02-23 naddy if (worktree)
5573 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5574 0ae84acc 2022-06-15 tracey if (pack_fds) {
5575 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5576 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5577 0ae84acc 2022-06-15 tracey if (error == NULL)
5578 0ae84acc 2022-06-15 tracey error = pack_err;
5580 51a10b52 2020-12-26 stsp tog_free_refs();
5581 26ed57b2 2018-05-19 stsp return error;
5584 4ed7e80c 2018-05-20 stsp __dead static void
5585 9f7d7167 2018-04-29 stsp usage_blame(void)
5588 87411fa9 2022-06-16 stsp fprintf(stderr,
5589 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5590 9f7d7167 2018-04-29 stsp getprogname());
5594 84451b3e 2018-07-10 stsp struct tog_blame_line {
5595 84451b3e 2018-07-10 stsp int annotated;
5596 84451b3e 2018-07-10 stsp struct got_object_id *id;
5599 4ed7e80c 2018-05-20 stsp static const struct got_error *
5600 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5602 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5603 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5604 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5605 84451b3e 2018-07-10 stsp const struct got_error *err;
5606 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5607 826082fe 2020-12-10 stsp char *line = NULL;
5608 826082fe 2020-12-10 stsp size_t linesize = 0;
5609 826082fe 2020-12-10 stsp ssize_t linelen;
5610 84451b3e 2018-07-10 stsp wchar_t *wline;
5611 27a741e5 2019-09-11 stsp int width;
5612 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5613 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5614 ab089a2a 2018-07-12 stsp char *id_str;
5615 11b20872 2019-11-08 stsp struct tog_color *tc;
5617 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5619 ab089a2a 2018-07-12 stsp return err;
5621 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5622 f7d12f7e 2018-08-01 stsp werase(view->window);
5624 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5625 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5626 ab089a2a 2018-07-12 stsp free(id_str);
5627 ab089a2a 2018-07-12 stsp return err;
5630 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5631 ab089a2a 2018-07-12 stsp free(line);
5632 2550e4c3 2018-07-13 stsp line = NULL;
5634 1cae65b4 2019-09-22 stsp return err;
5635 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5636 a3404814 2018-09-02 stsp wstandout(view->window);
5637 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5639 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5640 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5641 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5642 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5644 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5645 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5646 a3404814 2018-09-02 stsp wstandend(view->window);
5647 2550e4c3 2018-07-13 stsp free(wline);
5648 2550e4c3 2018-07-13 stsp wline = NULL;
5650 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5651 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5653 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5654 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5655 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5656 ab089a2a 2018-07-12 stsp free(id_str);
5657 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5659 ab089a2a 2018-07-12 stsp free(id_str);
5660 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5661 3f60a8ef 2018-07-10 stsp free(line);
5662 2550e4c3 2018-07-13 stsp line = NULL;
5664 3f60a8ef 2018-07-10 stsp return err;
5665 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5666 2550e4c3 2018-07-13 stsp free(wline);
5667 2550e4c3 2018-07-13 stsp wline = NULL;
5668 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5669 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5671 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5672 145b6838 2022-06-16 stsp view->maxx = 0;
5673 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5674 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5675 826082fe 2020-12-10 stsp if (linelen == -1) {
5676 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5677 826082fe 2020-12-10 stsp s->eof = 1;
5680 84451b3e 2018-07-10 stsp free(line);
5681 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5683 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5685 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5688 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5689 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5690 1853e0f4 2022-06-16 stsp if (err) {
5691 1853e0f4 2022-06-16 stsp free(line);
5692 1853e0f4 2022-06-16 stsp return err;
5694 1853e0f4 2022-06-16 stsp free(wline);
5695 1853e0f4 2022-06-16 stsp wline = NULL;
5696 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5698 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5699 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5701 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5702 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5703 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5704 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5705 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5706 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5707 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5708 8d0fe45a 2019-08-12 stsp char *id_str;
5709 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5710 87411fa9 2022-06-16 stsp blame_line->id);
5711 8d0fe45a 2019-08-12 stsp if (err) {
5712 8d0fe45a 2019-08-12 stsp free(line);
5713 8d0fe45a 2019-08-12 stsp return err;
5715 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5717 11b20872 2019-11-08 stsp wattr_on(view->window,
5718 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5719 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5721 11b20872 2019-11-08 stsp wattr_off(view->window,
5722 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5723 8d0fe45a 2019-08-12 stsp free(id_str);
5724 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5726 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5727 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5730 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5731 ee41ec32 2018-07-10 stsp prev_id = NULL;
5734 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5735 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5736 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5738 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5739 41605754 2020-11-12 stsp width = 9;
5740 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5741 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5742 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5743 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5744 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5745 41605754 2020-11-12 stsp if (err) {
5746 41605754 2020-11-12 stsp free(line);
5747 41605754 2020-11-12 stsp return err;
5749 41605754 2020-11-12 stsp width += 9;
5752 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5753 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5754 4cb9d869 2022-06-16 stsp if (err) {
5755 4cb9d869 2022-06-16 stsp free(line);
5756 4cb9d869 2022-06-16 stsp return err;
5758 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5759 a75b3e2d 2022-06-16 stsp width += 9;
5760 41605754 2020-11-12 stsp free(wline);
5761 41605754 2020-11-12 stsp wline = NULL;
5764 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5765 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5766 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5767 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5769 826082fe 2020-12-10 stsp free(line);
5770 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5772 9b058f45 2022-06-30 mark view_border(view);
5774 84451b3e 2018-07-10 stsp return NULL;
5777 84451b3e 2018-07-10 stsp static const struct got_error *
5778 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5779 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5781 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5782 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5783 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5784 1a76625f 2018-10-22 stsp int errcode;
5786 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5787 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5788 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5790 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5791 1a76625f 2018-10-22 stsp if (errcode)
5792 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5794 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5795 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5796 d68a0a7d 2018-07-10 stsp goto done;
5799 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5800 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5802 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5803 d68a0a7d 2018-07-10 stsp if (line->annotated)
5804 d68a0a7d 2018-07-10 stsp goto done;
5806 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5807 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5808 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5809 84451b3e 2018-07-10 stsp goto done;
5811 84451b3e 2018-07-10 stsp line->annotated = 1;
5813 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5814 1a76625f 2018-10-22 stsp if (errcode)
5815 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5816 84451b3e 2018-07-10 stsp return err;
5819 84451b3e 2018-07-10 stsp static void *
5820 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5822 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5823 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5824 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5825 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5826 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5828 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5829 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5830 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5832 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5833 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5834 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5835 e6e73e55 2022-06-30 tracey goto done;
5838 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5839 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5840 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5841 e6e73e55 2022-06-30 tracey goto done;
5843 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5844 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5845 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5846 e6e73e55 2022-06-30 tracey goto done;
5849 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5851 e6e73e55 2022-06-30 tracey goto done;
5853 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5854 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5855 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5856 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5857 fc06ba56 2019-08-22 stsp err = NULL;
5859 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5860 e6e73e55 2022-06-30 tracey if (errcode) {
5861 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5862 e6e73e55 2022-06-30 tracey goto done;
5865 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5866 1d0f4054 2021-06-17 stsp if (err == NULL)
5867 1d0f4054 2021-06-17 stsp err = close_err;
5868 c9beca56 2018-07-22 stsp ta->repo = NULL;
5869 c9beca56 2018-07-22 stsp *ta->complete = 1;
5871 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5872 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5873 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5876 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5877 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5878 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5879 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5880 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5881 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5882 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5883 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5885 18430de3 2018-07-10 stsp return (void *)err;
5888 245d91c1 2018-07-12 stsp static struct got_object_id *
5889 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5890 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5892 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5894 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5895 8d0fe45a 2019-08-12 stsp return NULL;
5897 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5898 c0f61fa4 2022-07-11 mark if (!line->annotated)
5899 c0f61fa4 2022-07-11 mark return NULL;
5901 c0f61fa4 2022-07-11 mark return line->id;
5904 c0f61fa4 2022-07-11 mark static struct got_object_id *
5905 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5906 c0f61fa4 2022-07-11 mark int lineno)
5908 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5910 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5911 c0f61fa4 2022-07-11 mark return NULL;
5913 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5914 245d91c1 2018-07-12 stsp if (!line->annotated)
5915 245d91c1 2018-07-12 stsp return NULL;
5917 245d91c1 2018-07-12 stsp return line->id;
5920 b880a918 2018-07-10 stsp static const struct got_error *
5921 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5923 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5926 245d91c1 2018-07-12 stsp if (blame->thread) {
5927 1a76625f 2018-10-22 stsp int errcode;
5928 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5929 1a76625f 2018-10-22 stsp if (errcode)
5930 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5931 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5932 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5933 1a76625f 2018-10-22 stsp if (errcode)
5934 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5935 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5936 1a76625f 2018-10-22 stsp if (errcode)
5937 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5938 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5939 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5940 245d91c1 2018-07-12 stsp err = NULL;
5941 245d91c1 2018-07-12 stsp blame->thread = NULL;
5943 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5944 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5945 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5946 1d0f4054 2021-06-17 stsp if (err == NULL)
5947 1d0f4054 2021-06-17 stsp err = close_err;
5948 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5950 245d91c1 2018-07-12 stsp if (blame->f) {
5951 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5952 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5953 245d91c1 2018-07-12 stsp blame->f = NULL;
5955 57670559 2018-12-23 stsp if (blame->lines) {
5956 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5957 57670559 2018-12-23 stsp free(blame->lines[i].id);
5958 57670559 2018-12-23 stsp free(blame->lines);
5959 57670559 2018-12-23 stsp blame->lines = NULL;
5961 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5962 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5963 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5964 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5965 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5966 0ae84acc 2022-06-15 tracey if (err == NULL)
5967 0ae84acc 2022-06-15 tracey err = pack_err;
5968 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5970 245d91c1 2018-07-12 stsp return err;
5973 245d91c1 2018-07-12 stsp static const struct got_error *
5974 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5976 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5977 fc06ba56 2019-08-22 stsp int *done = arg;
5978 fc06ba56 2019-08-22 stsp int errcode;
5980 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5981 fc06ba56 2019-08-22 stsp if (errcode)
5982 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5983 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5985 fc06ba56 2019-08-22 stsp if (*done)
5986 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5988 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5989 fc06ba56 2019-08-22 stsp if (errcode)
5990 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5991 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5993 fc06ba56 2019-08-22 stsp return err;
5996 fc06ba56 2019-08-22 stsp static const struct got_error *
5997 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5999 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6000 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6001 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6002 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6003 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6004 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6005 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6006 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6007 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6009 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6010 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6012 15a94983 2018-12-23 stsp return err;
6014 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6015 eb81bc23 2022-06-28 tracey if (fd == -1) {
6016 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6017 eb81bc23 2022-06-28 tracey goto done;
6020 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6022 a44927cc 2022-04-07 stsp goto done;
6024 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6026 84451b3e 2018-07-10 stsp goto done;
6028 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6029 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6030 84451b3e 2018-07-10 stsp goto done;
6033 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6035 a70480e0 2018-06-23 stsp goto done;
6036 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6037 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6038 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6039 84451b3e 2018-07-10 stsp goto done;
6041 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6042 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6044 1fddf795 2021-01-20 stsp goto done;
6045 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6046 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6047 84451b3e 2018-07-10 stsp goto done;
6050 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6051 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6052 b02560ec 2019-08-19 stsp blame->nlines--;
6054 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6055 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6056 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6057 84451b3e 2018-07-10 stsp goto done;
6060 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6062 bd24772e 2018-07-11 stsp goto done;
6063 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6064 0ae84acc 2022-06-15 tracey pack_fds);
6065 0ae84acc 2022-06-15 tracey if (err)
6066 0ae84acc 2022-06-15 tracey goto done;
6068 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6069 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6070 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6071 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6072 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6073 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6074 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6075 245d91c1 2018-07-12 stsp goto done;
6077 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6079 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6080 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6081 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6082 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6083 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6084 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6085 a5388363 2020-12-01 naddy s->blame_complete = 0;
6087 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6088 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6089 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6090 f5a09613 2020-12-13 naddy s->selected_line = 1;
6092 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6095 a44927cc 2022-04-07 stsp if (commit)
6096 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6097 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6098 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6100 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6101 27d434c2 2018-09-15 stsp free(obj_id);
6103 245d91c1 2018-07-12 stsp stop_blame(blame);
6104 245d91c1 2018-07-12 stsp return err;
6107 245d91c1 2018-07-12 stsp static const struct got_error *
6108 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6109 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6111 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6112 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6114 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6116 c4843652 2019-08-12 stsp s->path = strdup(path);
6117 c4843652 2019-08-12 stsp if (s->path == NULL)
6118 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6120 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6121 c4843652 2019-08-12 stsp if (err) {
6122 c4843652 2019-08-12 stsp free(s->path);
6123 7cbe629d 2018-08-04 stsp return err;
6126 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6127 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6128 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6129 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6130 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6131 fb2756b9 2018-08-04 stsp s->repo = repo;
6132 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6133 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6135 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6136 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6137 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6138 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6140 11b20872 2019-11-08 stsp return err;
6143 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6144 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6145 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6146 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6147 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6148 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6149 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6151 a5388363 2020-12-01 naddy return run_blame(view);
6154 e5a0f69f 2018-08-18 stsp static const struct got_error *
6155 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6157 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6158 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6160 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6161 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6163 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6164 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6165 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6166 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6167 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6170 e5a0f69f 2018-08-18 stsp free(s->path);
6171 11b20872 2019-11-08 stsp free_colors(&s->colors);
6172 e5a0f69f 2018-08-18 stsp return err;
6175 7cbe629d 2018-08-04 stsp static const struct got_error *
6176 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6178 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6180 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6181 6c4c42e0 2019-06-24 stsp return NULL;
6184 eaf2c13e 2022-09-17 mark static void
6185 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6186 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6188 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6190 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6191 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6192 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6193 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6194 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6195 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6196 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6199 6c4c42e0 2019-06-24 stsp static const struct got_error *
6200 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6202 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6203 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6204 2b380cc8 2018-10-24 stsp int errcode;
6206 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6207 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6208 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6209 2b380cc8 2018-10-24 stsp if (errcode)
6210 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6212 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6215 51fe7530 2019-08-19 stsp if (s->blame_complete)
6216 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6218 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6220 9b058f45 2022-06-30 mark view_border(view);
6221 e5a0f69f 2018-08-18 stsp return err;
6224 e5a0f69f 2018-08-18 stsp static const struct got_error *
6225 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6226 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6228 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6229 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6231 05f04cdf 2022-07-20 mark *new_view = NULL;
6233 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6234 05f04cdf 2022-07-20 mark if (log_view == NULL)
6235 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6237 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6239 05f04cdf 2022-07-20 mark view_close(log_view);
6241 05f04cdf 2022-07-20 mark *new_view = log_view;
6243 05f04cdf 2022-07-20 mark return err;
6246 05f04cdf 2022-07-20 mark static const struct got_error *
6247 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6249 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6250 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6251 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6252 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6254 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6255 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6256 9b058f45 2022-06-30 mark --eos; /* border */
6258 e5a0f69f 2018-08-18 stsp switch (ch) {
6260 145b6838 2022-06-16 stsp view->x = 0;
6263 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
6264 640cd7ff 2022-06-22 mark view->count = 0;
6266 145b6838 2022-06-16 stsp case KEY_RIGHT:
6268 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
6269 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
6271 640cd7ff 2022-06-22 mark view->count = 0;
6273 145b6838 2022-06-16 stsp case KEY_LEFT:
6275 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
6276 640cd7ff 2022-06-22 mark if (view->x <= 0)
6277 640cd7ff 2022-06-22 mark view->count = 0;
6280 1e37a5c2 2019-05-12 jcs s->done = 1;
6282 4deef56f 2021-09-02 naddy case 'g':
6283 4deef56f 2021-09-02 naddy case KEY_HOME:
6284 4deef56f 2021-09-02 naddy s->selected_line = 1;
6285 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6286 640cd7ff 2022-06-22 mark view->count = 0;
6288 4deef56f 2021-09-02 naddy case 'G':
6289 4deef56f 2021-09-02 naddy case KEY_END:
6290 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6291 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6292 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6294 9b058f45 2022-06-30 mark s->selected_line = eos;
6295 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6297 640cd7ff 2022-06-22 mark view->count = 0;
6300 1e37a5c2 2019-05-12 jcs case KEY_UP:
6301 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6302 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6303 1e37a5c2 2019-05-12 jcs s->selected_line--;
6304 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6305 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6306 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6308 640cd7ff 2022-06-22 mark view->count = 0;
6310 83cc4199 2022-06-13 stsp case CTRL('u'):
6312 83cc4199 2022-06-13 stsp nscroll /= 2;
6313 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6314 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6315 ea025d1d 2020-02-22 naddy case CTRL('b'):
6317 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6318 640cd7ff 2022-06-22 mark if (view->count > 1)
6319 640cd7ff 2022-06-22 mark nscroll += nscroll;
6320 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6321 640cd7ff 2022-06-22 mark view->count = 0;
6324 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6325 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6327 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6330 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6331 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6332 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6333 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6334 1e37a5c2 2019-05-12 jcs s->selected_line++;
6335 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6336 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6338 640cd7ff 2022-06-22 mark view->count = 0;
6341 1e37a5c2 2019-05-12 jcs case 'p': {
6342 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6344 640cd7ff 2022-06-22 mark view->count = 0;
6345 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6346 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6347 1e37a5c2 2019-05-12 jcs if (id == NULL)
6349 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6350 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6351 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6352 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6353 1e37a5c2 2019-05-12 jcs int obj_type;
6354 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6355 1e37a5c2 2019-05-12 jcs s->repo, id);
6358 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6359 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6360 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6361 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6364 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6365 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6366 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6369 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6370 a44927cc 2022-04-07 stsp pcommit, s->path);
6371 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6372 e5a0f69f 2018-08-18 stsp if (err) {
6373 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6374 1e37a5c2 2019-05-12 jcs err = NULL;
6375 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6378 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6380 1e37a5c2 2019-05-12 jcs free(blob_id);
6381 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6382 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6383 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6386 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6387 d7b5a0e8 2022-04-20 stsp &pid->id);
6388 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6390 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6391 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6393 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6398 1e37a5c2 2019-05-12 jcs s->done = 1;
6399 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6400 1e37a5c2 2019-05-12 jcs s->done = 0;
6401 1e37a5c2 2019-05-12 jcs if (thread_err)
6403 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6404 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6405 a5388363 2020-12-01 naddy err = run_blame(view);
6410 61417565 2022-06-20 mark case 'C': {
6411 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6413 640cd7ff 2022-06-22 mark view->count = 0;
6414 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6415 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6417 1e37a5c2 2019-05-12 jcs s->done = 1;
6418 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6419 1e37a5c2 2019-05-12 jcs s->done = 0;
6420 1e37a5c2 2019-05-12 jcs if (thread_err)
6422 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6423 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6424 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6425 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6426 a5388363 2020-12-01 naddy err = run_blame(view);
6432 05f04cdf 2022-07-20 mark view->count = 0;
6433 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6434 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6435 136e2bd4 2022-07-23 mark if (s->id_to_log)
6436 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6438 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6439 1e37a5c2 2019-05-12 jcs case '\r': {
6440 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6441 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6442 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6444 640cd7ff 2022-06-22 mark view->count = 0;
6445 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6446 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6447 1e37a5c2 2019-05-12 jcs if (id == NULL)
6449 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6452 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6453 c0f61fa4 2022-07-11 mark if (*new_view) {
6454 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6455 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6458 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6460 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6461 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6463 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6464 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6465 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6466 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6467 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6471 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6472 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6473 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6475 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6478 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6479 c0f61fa4 2022-07-11 mark s->selected_line;
6480 c0f61fa4 2022-07-11 mark if (*new_view)
6481 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6482 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6483 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6484 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6489 e78dc838 2020-12-04 stsp view->focussed = 0;
6490 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6491 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6492 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6493 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6494 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6495 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6498 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6501 e78dc838 2020-12-04 stsp view->focus_child = 1;
6503 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6508 83cc4199 2022-06-13 stsp case CTRL('d'):
6510 83cc4199 2022-06-13 stsp nscroll /= 2;
6511 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6512 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6513 ea025d1d 2020-02-22 naddy case CTRL('f'):
6516 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6517 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6518 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6519 640cd7ff 2022-06-22 mark view->count = 0;
6522 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6523 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6524 83cc4199 2022-06-13 stsp s->selected_line +=
6525 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6526 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6528 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6529 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6531 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6532 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6534 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6535 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6536 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6537 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6541 640cd7ff 2022-06-22 mark view->count = 0;
6544 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6547 917d79a7 2022-07-01 stsp static const struct got_error *
6548 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6550 917d79a7 2022-07-01 stsp const struct got_error *err;
6551 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6553 917d79a7 2022-07-01 stsp view->count = 0;
6554 917d79a7 2022-07-01 stsp s->done = 1;
6555 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6556 917d79a7 2022-07-01 stsp s->done = 0;
6558 917d79a7 2022-07-01 stsp return err;
6559 917d79a7 2022-07-01 stsp return run_blame(view);
6562 a70480e0 2018-06-23 stsp static const struct got_error *
6563 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6565 a70480e0 2018-06-23 stsp const struct got_error *error;
6566 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6567 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6568 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6569 0587e10c 2020-07-23 stsp char *link_target = NULL;
6570 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6571 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6572 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6574 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6575 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6577 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6578 a70480e0 2018-06-23 stsp switch (ch) {
6580 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6583 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6584 69069811 2018-08-02 stsp if (repo_path == NULL)
6585 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6589 17020d27 2019-03-07 stsp usage_blame();
6590 a70480e0 2018-06-23 stsp /* NOTREACHED */
6594 a70480e0 2018-06-23 stsp argc -= optind;
6595 a70480e0 2018-06-23 stsp argv += optind;
6597 f135c941 2020-02-20 stsp if (argc != 1)
6598 a70480e0 2018-06-23 stsp usage_blame();
6600 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6601 0ae84acc 2022-06-15 tracey if (error != NULL)
6602 0ae84acc 2022-06-15 tracey goto done;
6604 69069811 2018-08-02 stsp if (repo_path == NULL) {
6605 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6606 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6607 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6608 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6609 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6610 c156c7a4 2020-12-18 stsp goto done;
6611 f135c941 2020-02-20 stsp if (worktree)
6612 eb41ed75 2019-02-05 stsp repo_path =
6613 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6615 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6616 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6617 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6618 c156c7a4 2020-12-18 stsp goto done;
6622 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6623 c02c541e 2019-03-29 stsp if (error != NULL)
6624 8e94dd5b 2019-01-04 stsp goto done;
6626 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6627 f135c941 2020-02-20 stsp worktree);
6628 c02c541e 2019-03-29 stsp if (error)
6629 92205607 2019-01-04 stsp goto done;
6631 f135c941 2020-02-20 stsp init_curses();
6633 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6634 51a10b52 2020-12-26 stsp if (error)
6635 51a10b52 2020-12-26 stsp goto done;
6637 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6638 eb41ed75 2019-02-05 stsp if (error)
6639 69069811 2018-08-02 stsp goto done;
6641 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6642 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6643 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6644 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6645 a70480e0 2018-06-23 stsp if (error != NULL)
6646 66b4983c 2018-06-23 stsp goto done;
6647 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6648 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6650 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6651 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6653 a19e88aa 2018-06-23 stsp if (error != NULL)
6654 8b473291 2019-02-21 stsp goto done;
6656 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6657 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6658 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6659 e1cd8fed 2018-08-01 stsp goto done;
6662 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6663 a44927cc 2022-04-07 stsp if (error)
6664 a44927cc 2022-04-07 stsp goto done;
6666 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6667 a44927cc 2022-04-07 stsp commit, repo);
6668 7cbe629d 2018-08-04 stsp if (error)
6669 7cbe629d 2018-08-04 stsp goto done;
6671 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6672 78756c87 2020-11-24 stsp commit_id, repo);
6673 0587e10c 2020-07-23 stsp if (error)
6674 0587e10c 2020-07-23 stsp goto done;
6675 12314ad4 2019-08-31 stsp if (worktree) {
6676 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6677 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6678 12314ad4 2019-08-31 stsp worktree = NULL;
6680 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6682 69069811 2018-08-02 stsp free(repo_path);
6683 f135c941 2020-02-20 stsp free(in_repo_path);
6684 0587e10c 2020-07-23 stsp free(link_target);
6685 69069811 2018-08-02 stsp free(cwd);
6686 a70480e0 2018-06-23 stsp free(commit_id);
6687 a44927cc 2022-04-07 stsp if (commit)
6688 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6689 eb41ed75 2019-02-05 stsp if (worktree)
6690 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6691 1d0f4054 2021-06-17 stsp if (repo) {
6692 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6693 1d0f4054 2021-06-17 stsp if (error == NULL)
6694 1d0f4054 2021-06-17 stsp error = close_err;
6696 0ae84acc 2022-06-15 tracey if (pack_fds) {
6697 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6698 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6699 0ae84acc 2022-06-15 tracey if (error == NULL)
6700 0ae84acc 2022-06-15 tracey error = pack_err;
6702 51a10b52 2020-12-26 stsp tog_free_refs();
6703 a70480e0 2018-06-23 stsp return error;
6706 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6707 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6709 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6710 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6711 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6712 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6713 0c6ad1bc 2022-09-09 mark char *index = NULL;
6714 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6715 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6716 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6718 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6719 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6720 9b058f45 2022-06-30 mark --limit; /* border */
6722 f7d12f7e 2018-08-01 stsp werase(view->window);
6724 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6725 ffd1d5e5 2018-06-23 stsp return NULL;
6727 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6730 ffd1d5e5 2018-06-23 stsp return err;
6731 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6732 a3404814 2018-09-02 stsp wstandout(view->window);
6733 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6735 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6736 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6737 0c6ad1bc 2022-09-09 mark free(wline);
6738 0c6ad1bc 2022-09-09 mark wline = NULL;
6739 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6740 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6742 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6743 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6744 a3404814 2018-09-02 stsp wstandend(view->window);
6745 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6746 0c6ad1bc 2022-09-09 mark return NULL;
6748 df68a56b 2022-08-12 mark i += s->selected;
6749 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6750 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6751 df68a56b 2022-08-12 mark if (s->tree != s->root)
6752 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6754 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6755 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6756 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6757 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6758 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6759 0c6ad1bc 2022-09-09 mark free(index);
6761 ce52c690 2018-06-23 stsp return err;
6762 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6763 2550e4c3 2018-07-13 stsp free(wline);
6764 2550e4c3 2018-07-13 stsp wline = NULL;
6765 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6766 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6767 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6768 ffd1d5e5 2018-06-23 stsp return NULL;
6769 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6770 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6771 a1eca9bb 2018-06-23 stsp return NULL;
6773 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6774 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6775 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6776 0cf4efb1 2018-09-29 stsp if (view->focussed)
6777 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6778 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6780 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6781 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6782 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6783 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6784 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6785 ffd1d5e5 2018-06-23 stsp return NULL;
6789 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6792 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6793 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6794 848d6979 2019-08-12 stsp const char *modestr = "";
6795 56e0773d 2019-11-28 stsp mode_t mode;
6797 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6798 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6800 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6801 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6802 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6804 638f9024 2019-05-13 stsp return got_error_from_errno(
6805 230a42bd 2019-05-11 jcs "got_object_id_str");
6807 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6808 63c5ca5d 2019-08-24 stsp modestr = "$";
6809 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6812 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6813 d86d3b18 2020-12-01 naddy te, s->repo);
6814 0d6c6ee3 2020-05-20 stsp if (err) {
6815 0d6c6ee3 2020-05-20 stsp free(id_str);
6816 0d6c6ee3 2020-05-20 stsp return err;
6818 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6819 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6820 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6822 848d6979 2019-08-12 stsp modestr = "@";
6824 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6825 848d6979 2019-08-12 stsp modestr = "/";
6826 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6827 848d6979 2019-08-12 stsp modestr = "*";
6828 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6829 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6830 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6831 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6832 1d13200f 2018-07-12 stsp free(id_str);
6833 0d6c6ee3 2020-05-20 stsp free(link_target);
6834 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6836 1d13200f 2018-07-12 stsp free(id_str);
6837 0d6c6ee3 2020-05-20 stsp free(link_target);
6838 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6840 ffd1d5e5 2018-06-23 stsp if (err) {
6841 ffd1d5e5 2018-06-23 stsp free(line);
6844 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6845 0cf4efb1 2018-09-29 stsp if (view->focussed)
6846 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6847 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6849 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6851 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6852 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6853 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6855 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6856 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6857 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6858 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6859 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6860 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6861 ffd1d5e5 2018-06-23 stsp free(line);
6862 2550e4c3 2018-07-13 stsp free(wline);
6863 2550e4c3 2018-07-13 stsp wline = NULL;
6865 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6866 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6867 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6871 ffd1d5e5 2018-06-23 stsp return err;
6874 ffd1d5e5 2018-06-23 stsp static void
6875 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6877 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6878 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6879 fa86c4bf 2020-11-29 stsp int i = 0;
6881 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6884 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6885 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6886 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6887 fa86c4bf 2020-11-29 stsp if (!isroot)
6888 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6891 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6892 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6896 9b058f45 2022-06-30 mark static const struct got_error *
6897 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6899 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6900 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6901 ffd1d5e5 2018-06-23 stsp int n = 0;
6903 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6904 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6905 694d3271 2020-12-01 naddy s->first_displayed_entry);
6907 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6909 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6910 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6911 94b80cfa 2022-08-01 mark if (last) {
6912 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6913 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6915 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6916 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6917 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6921 9b058f45 2022-06-30 mark return NULL;
6924 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6925 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6926 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6928 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6929 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6930 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6932 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6933 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6934 56e0773d 2019-11-28 stsp + 1 /* slash */;
6936 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6938 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6939 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6940 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6942 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6943 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6944 d9765a41 2018-06-23 stsp while (pt) {
6945 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6946 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6947 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6948 cb2ebc8a 2018-06-23 stsp goto done;
6950 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6951 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6952 cb2ebc8a 2018-06-23 stsp goto done;
6954 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6957 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6958 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6959 ce52c690 2018-06-23 stsp goto done;
6963 ce52c690 2018-06-23 stsp if (err) {
6964 ce52c690 2018-06-23 stsp free(*path);
6965 ce52c690 2018-06-23 stsp *path = NULL;
6967 ce52c690 2018-06-23 stsp return err;
6970 ce52c690 2018-06-23 stsp static const struct got_error *
6971 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6972 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6973 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6975 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6976 ce52c690 2018-06-23 stsp char *path;
6977 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6979 a0de39f3 2019-08-09 stsp *new_view = NULL;
6981 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6983 ce52c690 2018-06-23 stsp return err;
6985 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6986 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6987 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6988 83ce39e3 2019-08-12 stsp goto done;
6991 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6992 e5a0f69f 2018-08-18 stsp if (err) {
6993 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6994 fc06ba56 2019-08-22 stsp err = NULL;
6995 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6997 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6999 83ce39e3 2019-08-12 stsp free(path);
7000 69efd4c4 2018-07-18 stsp return err;
7003 69efd4c4 2018-07-18 stsp static const struct got_error *
7004 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7005 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7007 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7008 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7009 69efd4c4 2018-07-18 stsp char *path;
7011 a0de39f3 2019-08-09 stsp *new_view = NULL;
7013 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7014 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7015 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7017 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7019 69efd4c4 2018-07-18 stsp return err;
7021 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7024 e5a0f69f 2018-08-18 stsp view_close(log_view);
7026 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7027 cb2ebc8a 2018-06-23 stsp free(path);
7028 cb2ebc8a 2018-06-23 stsp return err;
7031 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7032 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7033 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7035 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7036 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7037 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7038 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7040 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7041 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7043 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7044 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7045 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7047 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7049 bc573f3b 2021-07-10 stsp goto done;
7052 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7053 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7054 bc573f3b 2021-07-10 stsp * closed on demand.
7056 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7057 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7059 bc573f3b 2021-07-10 stsp goto done;
7060 bc573f3b 2021-07-10 stsp s->tree = s->root;
7062 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7063 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7064 ffd1d5e5 2018-06-23 stsp goto done;
7066 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7067 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7068 ffd1d5e5 2018-06-23 stsp goto done;
7071 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7072 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7073 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7074 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7075 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7076 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7077 9cd7cbd1 2020-12-07 stsp goto done;
7080 fb2756b9 2018-08-04 stsp s->repo = repo;
7082 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7083 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7084 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7085 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7087 c0b01bdb 2019-11-08 stsp goto done;
7088 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7089 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7091 c0b01bdb 2019-11-08 stsp goto done;
7092 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7093 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7094 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7096 c0b01bdb 2019-11-08 stsp goto done;
7098 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7099 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7100 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7102 11b20872 2019-11-08 stsp goto done;
7104 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7105 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7107 c0b01bdb 2019-11-08 stsp goto done;
7110 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7111 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7112 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7113 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7114 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7116 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7117 bc573f3b 2021-07-10 stsp if (commit)
7118 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7120 bc573f3b 2021-07-10 stsp close_tree_view(view);
7121 ad80ab7b 2018-08-04 stsp return err;
7124 e5a0f69f 2018-08-18 stsp static const struct got_error *
7125 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7127 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7129 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7130 fb2756b9 2018-08-04 stsp free(s->tree_label);
7131 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7132 6484ec90 2018-09-29 stsp free(s->commit_id);
7133 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7134 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7135 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7136 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7137 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7138 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7139 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7140 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7141 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7142 ad80ab7b 2018-08-04 stsp free(parent);
7145 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7146 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7147 bc573f3b 2021-07-10 stsp if (s->root)
7148 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7149 7c32bd05 2019-06-22 stsp return NULL;
7152 7c32bd05 2019-06-22 stsp static const struct got_error *
7153 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7155 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7157 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7158 7c32bd05 2019-06-22 stsp return NULL;
7161 7c32bd05 2019-06-22 stsp static int
7162 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7164 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7166 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7170 7c32bd05 2019-06-22 stsp static const struct got_error *
7171 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7173 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7174 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7176 7c32bd05 2019-06-22 stsp if (!view->searching) {
7177 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7178 7c32bd05 2019-06-22 stsp return NULL;
7181 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7182 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7183 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7184 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7185 56e0773d 2019-11-28 stsp s->selected_entry);
7187 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7189 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7190 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7192 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7193 56e0773d 2019-11-28 stsp s->selected_entry);
7196 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7197 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7198 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7199 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7201 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7204 7c32bd05 2019-06-22 stsp while (1) {
7205 56e0773d 2019-11-28 stsp if (te == NULL) {
7206 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7207 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7208 ac66afb8 2019-06-24 stsp return NULL;
7210 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7211 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7213 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7216 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7217 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7218 56e0773d 2019-11-28 stsp s->matched_entry = te;
7222 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7223 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7225 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7228 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7229 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7230 7c32bd05 2019-06-22 stsp s->selected = 0;
7233 e5a0f69f 2018-08-18 stsp return NULL;
7236 ad80ab7b 2018-08-04 stsp static const struct got_error *
7237 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7239 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7240 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7241 e5a0f69f 2018-08-18 stsp char *parent_path;
7243 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7245 e5a0f69f 2018-08-18 stsp return err;
7247 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7248 e5a0f69f 2018-08-18 stsp free(parent_path);
7250 9b058f45 2022-06-30 mark view_border(view);
7251 e5a0f69f 2018-08-18 stsp return err;
7254 94b80cfa 2022-08-01 mark static const struct got_error *
7255 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7257 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7258 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7259 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7260 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7261 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7262 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7264 94b80cfa 2022-08-01 mark g = view->gline;
7265 94b80cfa 2022-08-01 mark view->gline = 0;
7267 94b80cfa 2022-08-01 mark if (g == 0)
7269 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7270 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7272 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7273 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7274 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7276 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7277 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7278 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7280 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7281 94b80cfa 2022-08-01 mark last += off;
7283 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7284 94b80cfa 2022-08-01 mark s->selected = g - first;
7285 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7288 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7289 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7293 94b80cfa 2022-08-01 mark if (i < g) {
7294 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7296 94b80cfa 2022-08-01 mark return err;
7297 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7298 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7299 94b80cfa 2022-08-01 mark first + s->selected < g &&
7300 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7301 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7302 94b80cfa 2022-08-01 mark first += off;
7303 94b80cfa 2022-08-01 mark s->selected = g - first;
7305 94b80cfa 2022-08-01 mark } else if (i > g)
7306 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7308 94b80cfa 2022-08-01 mark if (g < nlines &&
7309 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7310 94b80cfa 2022-08-01 mark s->selected = g - 1;
7312 94b80cfa 2022-08-01 mark return NULL;
7315 e5a0f69f 2018-08-18 stsp static const struct got_error *
7316 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7318 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7319 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7320 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7321 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7323 94b80cfa 2022-08-01 mark if (view->gline)
7324 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7326 e5a0f69f 2018-08-18 stsp switch (ch) {
7328 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7329 640cd7ff 2022-06-22 mark view->count = 0;
7332 640cd7ff 2022-06-22 mark view->count = 0;
7333 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7335 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7338 640cd7ff 2022-06-22 mark view->count = 0;
7339 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7341 e4526bf5 2021-09-03 naddy case 'g':
7343 e4526bf5 2021-09-03 naddy case KEY_HOME:
7344 e4526bf5 2021-09-03 naddy s->selected = 0;
7345 640cd7ff 2022-06-22 mark view->count = 0;
7346 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7347 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7348 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7350 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7352 e4526bf5 2021-09-03 naddy case 'G':
7354 9b058f45 2022-06-30 mark case KEY_END: {
7355 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7357 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7358 9b058f45 2022-06-30 mark --eos; /* border */
7359 e4526bf5 2021-09-03 naddy s->selected = 0;
7360 640cd7ff 2022-06-22 mark view->count = 0;
7361 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7362 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7363 e4526bf5 2021-09-03 naddy if (te == NULL) {
7364 ad055527 2022-07-16 mark if (s->tree != s->root) {
7365 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7370 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7371 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7373 e4526bf5 2021-09-03 naddy if (n > 0)
7374 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7378 1e37a5c2 2019-05-12 jcs case KEY_UP:
7379 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7380 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7381 1e37a5c2 2019-05-12 jcs s->selected--;
7384 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7385 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7386 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7387 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7388 640cd7ff 2022-06-22 mark view->count = 0;
7390 83cc4199 2022-06-13 stsp case CTRL('u'):
7392 83cc4199 2022-06-13 stsp nscroll /= 2;
7393 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7394 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7395 ea025d1d 2020-02-22 naddy case CTRL('b'):
7397 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7398 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7399 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7400 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7402 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7403 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7405 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7406 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7407 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7408 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7409 640cd7ff 2022-06-22 mark view->count = 0;
7412 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7413 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7414 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7415 1e37a5c2 2019-05-12 jcs s->selected++;
7418 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7419 640cd7ff 2022-06-22 mark == NULL) {
7420 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7421 640cd7ff 2022-06-22 mark view->count = 0;
7424 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7426 83cc4199 2022-06-13 stsp case CTRL('d'):
7428 83cc4199 2022-06-13 stsp nscroll /= 2;
7429 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7430 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7431 ea025d1d 2020-02-22 naddy case CTRL('f'):
7433 48bb96f0 2022-06-20 naddy case ' ':
7434 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7436 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7437 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7438 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7439 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7441 640cd7ff 2022-06-22 mark view->count = 0;
7444 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7446 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7448 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7449 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7450 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7451 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7452 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7453 640cd7ff 2022-06-22 mark view->count = 0;
7456 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7457 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7459 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7460 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7461 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7462 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7463 1e37a5c2 2019-05-12 jcs s->selected_entry =
7464 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7465 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7466 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7467 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7471 1e37a5c2 2019-05-12 jcs free(parent);
7472 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7473 56e0773d 2019-11-28 stsp s->selected_entry))) {
7474 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7475 640cd7ff 2022-06-22 mark view->count = 0;
7476 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7477 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7480 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7481 941e9f74 2019-05-21 stsp if (err) {
7482 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7485 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7486 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7488 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7489 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7490 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7491 640cd7ff 2022-06-22 mark view->count = 0;
7494 640cd7ff 2022-06-22 mark view->count = 0;
7498 ffd1d5e5 2018-06-23 stsp return err;
7501 ffd1d5e5 2018-06-23 stsp __dead static void
7502 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7505 87411fa9 2022-06-16 stsp fprintf(stderr,
7506 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7507 ffd1d5e5 2018-06-23 stsp getprogname());
7511 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7512 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7514 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7515 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7516 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7517 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7518 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7519 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7520 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7521 4e97c21c 2020-12-06 stsp char *label = NULL;
7522 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7523 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7525 5221c383 2018-08-01 stsp struct tog_view *view;
7526 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7528 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7529 ffd1d5e5 2018-06-23 stsp switch (ch) {
7531 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7534 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7535 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7536 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7540 e99e2d15 2020-11-24 naddy usage_tree();
7541 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7545 ffd1d5e5 2018-06-23 stsp argc -= optind;
7546 ffd1d5e5 2018-06-23 stsp argv += optind;
7548 55cccc34 2020-02-20 stsp if (argc > 1)
7549 e99e2d15 2020-11-24 naddy usage_tree();
7551 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7552 0ae84acc 2022-06-15 tracey if (error != NULL)
7553 0ae84acc 2022-06-15 tracey goto done;
7555 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7556 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7557 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7558 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7559 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7560 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7561 c156c7a4 2020-12-18 stsp goto done;
7562 55cccc34 2020-02-20 stsp if (worktree)
7563 52185f70 2019-02-05 stsp repo_path =
7564 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7566 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7567 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7568 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7569 c156c7a4 2020-12-18 stsp goto done;
7573 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7574 c02c541e 2019-03-29 stsp if (error != NULL)
7575 52185f70 2019-02-05 stsp goto done;
7577 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7578 55cccc34 2020-02-20 stsp repo, worktree);
7579 55cccc34 2020-02-20 stsp if (error)
7580 55cccc34 2020-02-20 stsp goto done;
7582 55cccc34 2020-02-20 stsp init_curses();
7584 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7585 c02c541e 2019-03-29 stsp if (error)
7586 52185f70 2019-02-05 stsp goto done;
7588 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7589 51a10b52 2020-12-26 stsp if (error)
7590 51a10b52 2020-12-26 stsp goto done;
7592 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7593 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7594 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7595 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7596 4e97c21c 2020-12-06 stsp if (error)
7597 4e97c21c 2020-12-06 stsp goto done;
7598 4e97c21c 2020-12-06 stsp head_ref_name = label;
7600 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7601 4e97c21c 2020-12-06 stsp if (error == NULL)
7602 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7603 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7604 4e97c21c 2020-12-06 stsp goto done;
7605 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7606 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7607 4e97c21c 2020-12-06 stsp if (error)
7608 4e97c21c 2020-12-06 stsp goto done;
7611 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7612 a44927cc 2022-04-07 stsp if (error)
7613 a44927cc 2022-04-07 stsp goto done;
7615 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7616 5221c383 2018-08-01 stsp if (view == NULL) {
7617 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7618 5221c383 2018-08-01 stsp goto done;
7620 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7621 ad80ab7b 2018-08-04 stsp if (error)
7622 ad80ab7b 2018-08-04 stsp goto done;
7623 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7624 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7625 d91faf3b 2020-12-01 naddy in_repo_path);
7626 55cccc34 2020-02-20 stsp if (error)
7627 55cccc34 2020-02-20 stsp goto done;
7630 55cccc34 2020-02-20 stsp if (worktree) {
7631 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7632 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7633 55cccc34 2020-02-20 stsp worktree = NULL;
7635 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7637 52185f70 2019-02-05 stsp free(repo_path);
7638 e4a0e26d 2020-02-20 stsp free(cwd);
7639 ffd1d5e5 2018-06-23 stsp free(commit_id);
7640 4e97c21c 2020-12-06 stsp free(label);
7642 486cd271 2020-12-06 stsp got_ref_close(ref);
7643 1d0f4054 2021-06-17 stsp if (repo) {
7644 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7645 1d0f4054 2021-06-17 stsp if (error == NULL)
7646 1d0f4054 2021-06-17 stsp error = close_err;
7648 0ae84acc 2022-06-15 tracey if (pack_fds) {
7649 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7650 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7651 0ae84acc 2022-06-15 tracey if (error == NULL)
7652 0ae84acc 2022-06-15 tracey error = pack_err;
7654 51a10b52 2020-12-26 stsp tog_free_refs();
7655 ffd1d5e5 2018-06-23 stsp return error;
7658 6458efa5 2020-11-24 stsp static const struct got_error *
7659 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7661 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7662 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7664 6458efa5 2020-11-24 stsp s->nrefs = 0;
7665 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7666 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7667 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7668 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7669 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7672 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7673 6458efa5 2020-11-24 stsp if (re == NULL)
7674 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7676 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7677 8924d611 2020-12-26 stsp if (re->ref == NULL)
7678 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7679 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7680 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7683 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7684 6458efa5 2020-11-24 stsp return NULL;
7688 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7690 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7692 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7693 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7694 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7695 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7700 6458efa5 2020-11-24 stsp static const struct got_error *
7701 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7703 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7704 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7706 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7707 6458efa5 2020-11-24 stsp s->repo = repo;
7709 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7710 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7712 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7714 6458efa5 2020-11-24 stsp return err;
7716 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7717 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7718 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7719 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7721 6458efa5 2020-11-24 stsp goto done;
7723 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7724 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7725 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7727 6458efa5 2020-11-24 stsp goto done;
7729 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7730 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7731 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7733 cc488aa7 2022-01-23 stsp goto done;
7735 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7736 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7737 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7739 6458efa5 2020-11-24 stsp goto done;
7742 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7743 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7744 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7745 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7746 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7749 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7750 6458efa5 2020-11-24 stsp return err;
7753 6458efa5 2020-11-24 stsp static const struct got_error *
7754 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7756 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7758 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7759 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7761 6458efa5 2020-11-24 stsp return NULL;
7764 6458efa5 2020-11-24 stsp static const struct got_error *
7765 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7766 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7768 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7769 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7770 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7771 6458efa5 2020-11-24 stsp int obj_type;
7773 c42c9805 2020-11-24 stsp *commit_id = NULL;
7775 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7777 6458efa5 2020-11-24 stsp return err;
7779 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7781 6458efa5 2020-11-24 stsp goto done;
7783 6458efa5 2020-11-24 stsp switch (obj_type) {
7784 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7785 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7787 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7788 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7790 6458efa5 2020-11-24 stsp goto done;
7791 c42c9805 2020-11-24 stsp free(obj_id);
7792 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7793 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7795 6458efa5 2020-11-24 stsp goto done;
7796 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7797 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7798 c42c9805 2020-11-24 stsp goto done;
7800 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7801 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7802 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7803 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7804 c42c9805 2020-11-24 stsp goto done;
7808 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7814 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7815 c42c9805 2020-11-24 stsp if (err) {
7816 c42c9805 2020-11-24 stsp free(*commit_id);
7817 c42c9805 2020-11-24 stsp *commit_id = NULL;
7819 c42c9805 2020-11-24 stsp return err;
7822 c42c9805 2020-11-24 stsp static const struct got_error *
7823 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7824 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7826 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7827 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7828 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7830 c42c9805 2020-11-24 stsp *new_view = NULL;
7832 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7833 c42c9805 2020-11-24 stsp if (err) {
7834 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7835 c42c9805 2020-11-24 stsp return err;
7837 c42c9805 2020-11-24 stsp return NULL;
7840 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7841 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7842 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7843 6458efa5 2020-11-24 stsp goto done;
7846 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7847 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7850 6458efa5 2020-11-24 stsp view_close(log_view);
7852 6458efa5 2020-11-24 stsp *new_view = log_view;
7853 c42c9805 2020-11-24 stsp free(commit_id);
7854 6458efa5 2020-11-24 stsp return err;
7857 ce5b7c56 2019-07-09 stsp static void
7858 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7860 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7861 34ba6917 2020-11-30 stsp int i = 0;
7863 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7866 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7867 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7868 34ba6917 2020-11-30 stsp if (re == NULL)
7870 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7871 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7875 9b058f45 2022-06-30 mark static const struct got_error *
7876 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7878 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7879 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7880 6458efa5 2020-11-24 stsp int n = 0;
7882 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7883 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7885 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7887 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7888 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7889 94b80cfa 2022-08-01 mark if (last) {
7890 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7891 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7893 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7894 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7895 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7899 9b058f45 2022-06-30 mark return NULL;
7902 6458efa5 2020-11-24 stsp static const struct got_error *
7903 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7905 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7907 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7908 6458efa5 2020-11-24 stsp return NULL;
7911 6458efa5 2020-11-24 stsp static int
7912 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7914 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7916 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7920 6458efa5 2020-11-24 stsp static const struct got_error *
7921 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7923 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7924 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7926 6458efa5 2020-11-24 stsp if (!view->searching) {
7927 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7928 6458efa5 2020-11-24 stsp return NULL;
7931 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7932 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7933 6458efa5 2020-11-24 stsp if (s->selected_entry)
7934 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7936 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7937 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7939 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7940 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7942 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7943 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7946 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7947 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7948 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7949 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7951 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7954 6458efa5 2020-11-24 stsp while (1) {
7955 6458efa5 2020-11-24 stsp if (re == NULL) {
7956 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7957 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7958 6458efa5 2020-11-24 stsp return NULL;
7960 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7961 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7963 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7966 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7967 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7968 6458efa5 2020-11-24 stsp s->matched_entry = re;
7972 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7973 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7975 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7978 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7979 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7980 6458efa5 2020-11-24 stsp s->selected = 0;
7983 6458efa5 2020-11-24 stsp return NULL;
7986 6458efa5 2020-11-24 stsp static const struct got_error *
7987 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7989 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7990 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7991 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7992 6458efa5 2020-11-24 stsp char *line = NULL;
7993 6458efa5 2020-11-24 stsp wchar_t *wline;
7994 6458efa5 2020-11-24 stsp struct tog_color *tc;
7995 6458efa5 2020-11-24 stsp int width, n;
7996 6458efa5 2020-11-24 stsp int limit = view->nlines;
7998 6458efa5 2020-11-24 stsp werase(view->window);
8000 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8001 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8002 9b058f45 2022-06-30 mark --limit; /* border */
8004 6458efa5 2020-11-24 stsp if (limit == 0)
8005 6458efa5 2020-11-24 stsp return NULL;
8007 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8009 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8010 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8011 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8013 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8014 6458efa5 2020-11-24 stsp if (err) {
8015 6458efa5 2020-11-24 stsp free(line);
8016 6458efa5 2020-11-24 stsp return err;
8018 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8019 6458efa5 2020-11-24 stsp wstandout(view->window);
8020 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8021 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8022 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8023 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8024 6458efa5 2020-11-24 stsp wstandend(view->window);
8025 6458efa5 2020-11-24 stsp free(wline);
8026 6458efa5 2020-11-24 stsp wline = NULL;
8027 6458efa5 2020-11-24 stsp free(line);
8028 6458efa5 2020-11-24 stsp line = NULL;
8029 6458efa5 2020-11-24 stsp if (--limit <= 0)
8030 6458efa5 2020-11-24 stsp return NULL;
8033 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8034 6458efa5 2020-11-24 stsp char *line = NULL;
8035 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8037 b4996bee 2022-06-16 stsp if (s->show_date) {
8038 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8039 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8040 b4996bee 2022-06-16 stsp struct got_object_id *id;
8041 b4996bee 2022-06-16 stsp struct tm tm;
8044 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8046 b4996bee 2022-06-16 stsp return err;
8047 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8048 b4996bee 2022-06-16 stsp if (err) {
8049 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8051 b4996bee 2022-06-16 stsp return err;
8053 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8055 b4996bee 2022-06-16 stsp if (err) {
8057 b4996bee 2022-06-16 stsp return err;
8059 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8060 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8062 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8063 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8066 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8067 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8068 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8069 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8071 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8072 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8073 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8074 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8075 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8076 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8077 6458efa5 2020-11-24 stsp struct got_object_id *id;
8078 6458efa5 2020-11-24 stsp char *id_str;
8079 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8081 6458efa5 2020-11-24 stsp return err;
8082 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8083 6458efa5 2020-11-24 stsp if (err) {
8085 6458efa5 2020-11-24 stsp return err;
8087 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8088 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8089 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8091 6458efa5 2020-11-24 stsp free(id_str);
8092 6458efa5 2020-11-24 stsp return err;
8095 6458efa5 2020-11-24 stsp free(id_str);
8096 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8097 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8098 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8100 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8102 6458efa5 2020-11-24 stsp if (err) {
8103 6458efa5 2020-11-24 stsp free(line);
8104 6458efa5 2020-11-24 stsp return err;
8106 6458efa5 2020-11-24 stsp if (n == s->selected) {
8107 6458efa5 2020-11-24 stsp if (view->focussed)
8108 6458efa5 2020-11-24 stsp wstandout(view->window);
8109 6458efa5 2020-11-24 stsp s->selected_entry = re;
8111 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8113 6458efa5 2020-11-24 stsp wattr_on(view->window,
8114 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8115 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8117 6458efa5 2020-11-24 stsp wattr_off(view->window,
8118 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8119 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8120 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8121 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8122 6458efa5 2020-11-24 stsp wstandend(view->window);
8123 6458efa5 2020-11-24 stsp free(line);
8124 6458efa5 2020-11-24 stsp free(wline);
8125 6458efa5 2020-11-24 stsp wline = NULL;
8127 6458efa5 2020-11-24 stsp s->ndisplayed++;
8128 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8131 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8134 9b058f45 2022-06-30 mark view_border(view);
8135 6458efa5 2020-11-24 stsp return err;
8138 6458efa5 2020-11-24 stsp static const struct got_error *
8139 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8140 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8142 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8143 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8144 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8146 c42c9805 2020-11-24 stsp *new_view = NULL;
8148 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8149 c42c9805 2020-11-24 stsp if (err) {
8150 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8151 c42c9805 2020-11-24 stsp return err;
8153 c42c9805 2020-11-24 stsp return NULL;
8157 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8158 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8159 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8160 c42c9805 2020-11-24 stsp goto done;
8163 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8164 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8166 c42c9805 2020-11-24 stsp goto done;
8168 c42c9805 2020-11-24 stsp *new_view = tree_view;
8170 c42c9805 2020-11-24 stsp free(commit_id);
8171 c42c9805 2020-11-24 stsp return err;
8174 94b80cfa 2022-08-01 mark static const struct got_error *
8175 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8177 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8178 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8179 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8181 94b80cfa 2022-08-01 mark g = view->gline;
8182 94b80cfa 2022-08-01 mark view->gline = 0;
8184 94b80cfa 2022-08-01 mark if (g == 0)
8186 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8187 94b80cfa 2022-08-01 mark g = s->nrefs;
8189 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8190 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8191 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8192 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8193 94b80cfa 2022-08-01 mark return NULL;
8196 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8197 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8199 94b80cfa 2022-08-01 mark return err;
8200 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8201 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8202 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8203 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8204 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8205 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8207 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8208 94b80cfa 2022-08-01 mark s->selected = g - 1;
8210 94b80cfa 2022-08-01 mark return NULL;
8214 c42c9805 2020-11-24 stsp static const struct got_error *
8215 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8217 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8218 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8219 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8220 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8222 94b80cfa 2022-08-01 mark if (view->gline)
8223 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8225 6458efa5 2020-11-24 stsp switch (ch) {
8227 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8228 640cd7ff 2022-06-22 mark view->count = 0;
8231 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8232 640cd7ff 2022-06-22 mark view->count = 0;
8235 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8236 640cd7ff 2022-06-22 mark view->count = 0;
8237 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8238 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8239 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8242 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8243 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8244 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8247 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8248 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8250 6458efa5 2020-11-24 stsp case KEY_ENTER:
8251 6458efa5 2020-11-24 stsp case '\r':
8252 640cd7ff 2022-06-22 mark view->count = 0;
8253 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8255 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8258 640cd7ff 2022-06-22 mark view->count = 0;
8259 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8261 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8263 e4526bf5 2021-09-03 naddy case 'g':
8265 e4526bf5 2021-09-03 naddy case KEY_HOME:
8266 e4526bf5 2021-09-03 naddy s->selected = 0;
8267 640cd7ff 2022-06-22 mark view->count = 0;
8268 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8270 e4526bf5 2021-09-03 naddy case 'G':
8272 9b058f45 2022-06-30 mark case KEY_END: {
8273 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8275 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8276 9b058f45 2022-06-30 mark --eos; /* border */
8277 e4526bf5 2021-09-03 naddy s->selected = 0;
8278 640cd7ff 2022-06-22 mark view->count = 0;
8279 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8280 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8281 e4526bf5 2021-09-03 naddy if (re == NULL)
8283 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8284 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8286 e4526bf5 2021-09-03 naddy if (n > 0)
8287 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8291 6458efa5 2020-11-24 stsp case KEY_UP:
8292 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8293 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8294 6458efa5 2020-11-24 stsp s->selected--;
8297 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8298 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8299 640cd7ff 2022-06-22 mark view->count = 0;
8301 83cc4199 2022-06-13 stsp case CTRL('u'):
8303 83cc4199 2022-06-13 stsp nscroll /= 2;
8304 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8305 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8306 6458efa5 2020-11-24 stsp case CTRL('b'):
8308 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8309 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8310 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8311 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8312 640cd7ff 2022-06-22 mark view->count = 0;
8315 6458efa5 2020-11-24 stsp case KEY_DOWN:
8316 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8317 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8318 6458efa5 2020-11-24 stsp s->selected++;
8321 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8322 6458efa5 2020-11-24 stsp /* can't scroll any further */
8323 640cd7ff 2022-06-22 mark view->count = 0;
8326 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8328 83cc4199 2022-06-13 stsp case CTRL('d'):
8330 83cc4199 2022-06-13 stsp nscroll /= 2;
8331 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8332 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8333 6458efa5 2020-11-24 stsp case CTRL('f'):
8335 48bb96f0 2022-06-20 naddy case ' ':
8336 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8337 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8338 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8339 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8340 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8341 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8342 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8343 640cd7ff 2022-06-22 mark view->count = 0;
8346 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8348 6458efa5 2020-11-24 stsp case CTRL('l'):
8349 640cd7ff 2022-06-22 mark view->count = 0;
8350 8924d611 2020-12-26 stsp tog_free_refs();
8351 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8354 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8355 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8357 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8358 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8359 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8362 640cd7ff 2022-06-22 mark view->count = 0;
8366 6458efa5 2020-11-24 stsp return err;
8369 6458efa5 2020-11-24 stsp __dead static void
8370 6458efa5 2020-11-24 stsp usage_ref(void)
8373 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8374 6458efa5 2020-11-24 stsp getprogname());
8378 6458efa5 2020-11-24 stsp static const struct got_error *
8379 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8381 6458efa5 2020-11-24 stsp const struct got_error *error;
8382 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8383 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8384 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8386 6458efa5 2020-11-24 stsp struct tog_view *view;
8387 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8389 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8390 6458efa5 2020-11-24 stsp switch (ch) {
8392 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8393 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8394 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8398 e99e2d15 2020-11-24 naddy usage_ref();
8399 6458efa5 2020-11-24 stsp /* NOTREACHED */
8403 6458efa5 2020-11-24 stsp argc -= optind;
8404 6458efa5 2020-11-24 stsp argv += optind;
8406 6458efa5 2020-11-24 stsp if (argc > 1)
8407 e99e2d15 2020-11-24 naddy usage_ref();
8409 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8410 0ae84acc 2022-06-15 tracey if (error != NULL)
8411 0ae84acc 2022-06-15 tracey goto done;
8413 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8414 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8415 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8416 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8417 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8418 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8419 c156c7a4 2020-12-18 stsp goto done;
8420 6458efa5 2020-11-24 stsp if (worktree)
8421 6458efa5 2020-11-24 stsp repo_path =
8422 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8424 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8425 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8426 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8427 c156c7a4 2020-12-18 stsp goto done;
8431 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8432 6458efa5 2020-11-24 stsp if (error != NULL)
8433 6458efa5 2020-11-24 stsp goto done;
8435 6458efa5 2020-11-24 stsp init_curses();
8437 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8438 51a10b52 2020-12-26 stsp if (error)
8439 51a10b52 2020-12-26 stsp goto done;
8441 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8442 6458efa5 2020-11-24 stsp if (error)
8443 6458efa5 2020-11-24 stsp goto done;
8445 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8446 6458efa5 2020-11-24 stsp if (view == NULL) {
8447 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8448 6458efa5 2020-11-24 stsp goto done;
8451 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8452 6458efa5 2020-11-24 stsp if (error)
8453 6458efa5 2020-11-24 stsp goto done;
8455 6458efa5 2020-11-24 stsp if (worktree) {
8456 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8457 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8458 6458efa5 2020-11-24 stsp worktree = NULL;
8460 6458efa5 2020-11-24 stsp error = view_loop(view);
8462 6458efa5 2020-11-24 stsp free(repo_path);
8463 6458efa5 2020-11-24 stsp free(cwd);
8464 1d0f4054 2021-06-17 stsp if (repo) {
8465 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8466 1d0f4054 2021-06-17 stsp if (close_err)
8467 1d0f4054 2021-06-17 stsp error = close_err;
8469 0ae84acc 2022-06-15 tracey if (pack_fds) {
8470 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8471 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8472 0ae84acc 2022-06-15 tracey if (error == NULL)
8473 0ae84acc 2022-06-15 tracey error = pack_err;
8475 51a10b52 2020-12-26 stsp tog_free_refs();
8476 6458efa5 2020-11-24 stsp return error;
8479 ec2a9698 2022-09-15 mark static const struct got_error*
8480 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8481 ec2a9698 2022-09-15 mark const char *str)
8483 ec2a9698 2022-09-15 mark size_t len;
8485 ec2a9698 2022-09-15 mark if (win == NULL)
8486 ec2a9698 2022-09-15 mark win = stdscr;
8488 ec2a9698 2022-09-15 mark len = strlen(str);
8489 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8491 ec2a9698 2022-09-15 mark if (focus)
8492 ec2a9698 2022-09-15 mark wstandout(win);
8493 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8494 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8495 ec2a9698 2022-09-15 mark if (focus)
8496 ec2a9698 2022-09-15 mark wstandend(win);
8498 ec2a9698 2022-09-15 mark return NULL;
8501 136e2bd4 2022-07-23 mark static const struct got_error *
8502 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8506 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8507 ec2a9698 2022-09-15 mark if (p == NULL) {
8508 ec2a9698 2022-09-15 mark free(*line_offsets);
8509 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8510 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8513 ec2a9698 2022-09-15 mark *line_offsets = p;
8514 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8515 ec2a9698 2022-09-15 mark ++(*nlines);
8516 ec2a9698 2022-09-15 mark return NULL;
8519 ec2a9698 2022-09-15 mark static const struct got_error *
8520 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8524 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8525 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8526 ec2a9698 2022-09-15 mark size_t len = 1;
8528 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8531 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8532 ec2a9698 2022-09-15 mark if (t0 == NULL)
8533 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8535 ec2a9698 2022-09-15 mark len += strlen(t);
8536 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8537 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8539 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8542 ec2a9698 2022-09-15 mark return NULL;
8546 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8547 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8548 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8550 ec2a9698 2022-09-15 mark static const struct got_error *
8551 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8553 ec2a9698 2022-09-15 mark int n, len = width;
8555 ec2a9698 2022-09-15 mark if (km->keys) {
8556 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8557 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8558 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8560 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8561 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8563 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8565 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8566 ec2a9698 2022-09-15 mark if (t0 == NULL)
8567 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8569 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8570 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8571 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8572 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8573 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8574 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8575 ec2a9698 2022-09-15 mark if (n < 0) {
8577 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8579 ec2a9698 2022-09-15 mark first = 0;
8580 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8581 ec2a9698 2022-09-15 mark *off += n;
8585 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8586 ec2a9698 2022-09-15 mark if (n < 0)
8587 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8588 ec2a9698 2022-09-15 mark *off += n;
8590 ec2a9698 2022-09-15 mark return NULL;
8593 ec2a9698 2022-09-15 mark static const struct got_error *
8594 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8596 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8597 ec2a9698 2022-09-15 mark off_t off = 0;
8598 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8599 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8600 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8601 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8602 ec2a9698 2022-09-15 mark GENERATE_HELP
8603 ec2a9698 2022-09-15 mark #undef KEYMAP_
8604 ec2a9698 2022-09-15 mark #undef KEY_
8607 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8609 ec2a9698 2022-09-15 mark return err;
8611 ec2a9698 2022-09-15 mark n = nitems(km);
8612 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8614 ec2a9698 2022-09-15 mark return err;
8616 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8617 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8618 ec2a9698 2022-09-15 mark show = s->all;
8619 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8620 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8623 ec2a9698 2022-09-15 mark if (show) {
8624 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8626 ec2a9698 2022-09-15 mark return err;
8627 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8629 ec2a9698 2022-09-15 mark return err;
8632 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8634 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8635 ec2a9698 2022-09-15 mark return err;
8638 ec2a9698 2022-09-15 mark static const struct got_error *
8639 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8642 ec2a9698 2022-09-15 mark const struct got_error *err;
8644 ec2a9698 2022-09-15 mark free(s->line_offsets);
8645 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8646 ec2a9698 2022-09-15 mark s->nlines = 0;
8648 ec2a9698 2022-09-15 mark f = got_opentemp();
8649 ec2a9698 2022-09-15 mark if (f == NULL)
8650 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8653 ec2a9698 2022-09-15 mark err = format_help(s);
8655 ec2a9698 2022-09-15 mark return err;
8657 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8658 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8660 ec2a9698 2022-09-15 mark return NULL;
8663 ec2a9698 2022-09-15 mark static const struct got_error *
8664 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8666 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8667 ec2a9698 2022-09-15 mark return NULL;
8670 eaf2c13e 2022-09-17 mark static void
8671 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8672 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8674 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8676 eaf2c13e 2022-09-17 mark *f = s->f;
8677 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8678 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8679 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8680 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8681 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8682 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8685 ec2a9698 2022-09-15 mark static const struct got_error *
8686 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8688 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8689 ec2a9698 2022-09-15 mark const struct got_error *err;
8690 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8691 ec2a9698 2022-09-15 mark wchar_t *wline;
8692 ec2a9698 2022-09-15 mark char *line;
8693 ec2a9698 2022-09-15 mark ssize_t linelen;
8694 ec2a9698 2022-09-15 mark size_t linesz = 0;
8695 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8696 ec2a9698 2022-09-15 mark int eos = view->nlines;
8698 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8699 ec2a9698 2022-09-15 mark --eos; /* account for border */
8701 ec2a9698 2022-09-15 mark s->lineno = 0;
8702 ec2a9698 2022-09-15 mark rewind(s->f);
8703 ec2a9698 2022-09-15 mark werase(view->window);
8705 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8706 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8708 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8709 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8710 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8712 ec2a9698 2022-09-15 mark return err;
8713 ec2a9698 2022-09-15 mark if (eos <= 1)
8714 ec2a9698 2022-09-15 mark return NULL;
8715 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8718 ec2a9698 2022-09-15 mark s->eof = 0;
8719 ec2a9698 2022-09-15 mark view->maxx = 0;
8720 ec2a9698 2022-09-15 mark line = NULL;
8721 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8722 ec2a9698 2022-09-15 mark attr_t attr = 0;
8724 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8725 ec2a9698 2022-09-15 mark if (linelen == -1) {
8726 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8727 ec2a9698 2022-09-15 mark free(line);
8728 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8730 ec2a9698 2022-09-15 mark s->eof = 1;
8733 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8735 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8737 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8738 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8740 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8741 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8742 ec2a9698 2022-09-15 mark if (err) {
8743 ec2a9698 2022-09-15 mark free(line);
8744 ec2a9698 2022-09-15 mark return err;
8746 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8747 ec2a9698 2022-09-15 mark free(wline);
8748 ec2a9698 2022-09-15 mark wline = NULL;
8751 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8752 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8753 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8754 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8755 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8756 ec2a9698 2022-09-15 mark if (err) {
8757 ec2a9698 2022-09-15 mark free(line);
8758 ec2a9698 2022-09-15 mark return err;
8763 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8764 ec2a9698 2022-09-15 mark view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8765 ec2a9698 2022-09-15 mark if (err) {
8766 ec2a9698 2022-09-15 mark free(line);
8767 ec2a9698 2022-09-15 mark return err;
8769 ec2a9698 2022-09-15 mark rc = waddwstr(view->window, &wline[skip]);
8770 ec2a9698 2022-09-15 mark free(wline);
8771 ec2a9698 2022-09-15 mark wline = NULL;
8772 ec2a9698 2022-09-15 mark if (rc == ERR)
8773 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "waddwstr");
8775 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8776 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8777 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8779 ec2a9698 2022-09-15 mark if (width <= view->ncols)
8780 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8783 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8784 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8785 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8787 ec2a9698 2022-09-15 mark free(line);
8788 ec2a9698 2022-09-15 mark if (nprinted > 0)
8789 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8791 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8793 ec2a9698 2022-09-15 mark view_border(view);
8795 ec2a9698 2022-09-15 mark if (s->eof) {
8796 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8797 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8798 ec2a9698 2022-09-15 mark view->ncols - 1);
8799 ec2a9698 2022-09-15 mark if (rc == ERR)
8800 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8802 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8803 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8804 ec2a9698 2022-09-15 mark wstandout(view->window);
8805 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8806 ec2a9698 2022-09-15 mark view->ncols - 1);
8807 ec2a9698 2022-09-15 mark if (rc == ERR)
8808 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8809 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8810 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8811 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8812 ec2a9698 2022-09-15 mark if (rc == ERR)
8813 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8815 ec2a9698 2022-09-15 mark wstandend(view->window);
8818 ec2a9698 2022-09-15 mark return NULL;
8821 ec2a9698 2022-09-15 mark static const struct got_error *
8822 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8824 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8825 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8826 ec2a9698 2022-09-15 mark char *line = NULL;
8827 ec2a9698 2022-09-15 mark ssize_t linelen;
8828 ec2a9698 2022-09-15 mark size_t linesz = 0;
8829 ec2a9698 2022-09-15 mark int eos, nscroll;
8831 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8832 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8833 ec2a9698 2022-09-15 mark --eos; /* border */
8835 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8837 ec2a9698 2022-09-15 mark switch (ch) {
8839 ec2a9698 2022-09-15 mark view->x = 0;
8842 ec2a9698 2022-09-15 mark view->x = MAX(view->maxx - view->ncols / 3, 0);
8843 ec2a9698 2022-09-15 mark view->count = 0;
8845 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8847 ec2a9698 2022-09-15 mark if (view->x + view->ncols / 3 < view->maxx)
8848 ec2a9698 2022-09-15 mark view->x += 2;
8850 ec2a9698 2022-09-15 mark view->count = 0;
8852 ec2a9698 2022-09-15 mark case KEY_LEFT:
8854 ec2a9698 2022-09-15 mark view->x -= MIN(view->x, 2);
8855 ec2a9698 2022-09-15 mark if (view->x <= 0)
8856 ec2a9698 2022-09-15 mark view->count = 0;
8859 ec2a9698 2022-09-15 mark case KEY_HOME:
8860 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8861 ec2a9698 2022-09-15 mark view->count = 0;
8864 ec2a9698 2022-09-15 mark case KEY_END:
8865 ec2a9698 2022-09-15 mark view->count = 0;
8866 ec2a9698 2022-09-15 mark if (s->eof)
8868 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8869 ec2a9698 2022-09-15 mark s->eof = 1;
8872 ec2a9698 2022-09-15 mark case KEY_UP:
8873 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8874 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8876 ec2a9698 2022-09-15 mark view->count = 0;
8878 ec2a9698 2022-09-15 mark case CTRL('u'):
8880 ec2a9698 2022-09-15 mark nscroll /= 2;
8881 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8882 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8883 ec2a9698 2022-09-15 mark case CTRL('b'):
8885 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8886 ec2a9698 2022-09-15 mark view->count = 0;
8889 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8890 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8893 ec2a9698 2022-09-15 mark case KEY_DOWN:
8894 ec2a9698 2022-09-15 mark case CTRL('n'):
8895 ec2a9698 2022-09-15 mark if (!s->eof)
8896 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8898 ec2a9698 2022-09-15 mark view->count = 0;
8900 ec2a9698 2022-09-15 mark case CTRL('d'):
8902 ec2a9698 2022-09-15 mark nscroll /= 2;
8903 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8904 ec2a9698 2022-09-15 mark case KEY_NPAGE:
8905 ec2a9698 2022-09-15 mark case CTRL('f'):
8908 ec2a9698 2022-09-15 mark if (s->eof) {
8909 ec2a9698 2022-09-15 mark view->count = 0;
8912 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
8913 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8914 ec2a9698 2022-09-15 mark s->first_displayed_line++;
8915 ec2a9698 2022-09-15 mark if (linelen == -1) {
8916 ec2a9698 2022-09-15 mark if (feof(s->f))
8917 ec2a9698 2022-09-15 mark s->eof = 1;
8919 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
8923 ec2a9698 2022-09-15 mark free(line);
8926 ec2a9698 2022-09-15 mark view->count = 0;
8930 ec2a9698 2022-09-15 mark return err;
8933 ec2a9698 2022-09-15 mark static const struct got_error *
8934 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
8936 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8938 ec2a9698 2022-09-15 mark free(s->line_offsets);
8939 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8940 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
8941 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8943 ec2a9698 2022-09-15 mark return NULL;
8946 ec2a9698 2022-09-15 mark static const struct got_error *
8947 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
8949 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8952 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
8953 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8955 ec2a9698 2022-09-15 mark wclear(view->window);
8956 ec2a9698 2022-09-15 mark view->count = 0;
8957 ec2a9698 2022-09-15 mark view->x = 0;
8958 ec2a9698 2022-09-15 mark s->all = !s->all;
8959 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8960 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8961 ec2a9698 2022-09-15 mark s->matched_line = 0;
8963 ec2a9698 2022-09-15 mark return create_help(s);
8966 ec2a9698 2022-09-15 mark static const struct got_error *
8967 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
8969 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8970 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8972 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
8973 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8974 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8975 ec2a9698 2022-09-15 mark s->selected_line = 1;
8977 ec2a9698 2022-09-15 mark view->show = show_help_view;
8978 ec2a9698 2022-09-15 mark view->input = input_help_view;
8979 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
8980 ec2a9698 2022-09-15 mark view->close = close_help_view;
8981 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
8982 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
8983 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
8985 ec2a9698 2022-09-15 mark err = create_help(s);
8986 ec2a9698 2022-09-15 mark return err;
8989 ec2a9698 2022-09-15 mark static const struct got_error *
8990 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8991 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
8993 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
8995 136e2bd4 2022-07-23 mark *new_view = NULL;
8997 136e2bd4 2022-07-23 mark switch (request) {
8998 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
8999 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9000 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9002 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9003 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9004 136e2bd4 2022-07-23 mark view, s->repo);
9006 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9007 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9009 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9010 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9011 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9013 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9014 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9017 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9018 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9020 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9021 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9022 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9023 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9024 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9025 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9026 136e2bd4 2022-07-23 mark &view->state.tree);
9027 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9028 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9029 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9030 136e2bd4 2022-07-23 mark view->state.ref.repo);
9032 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9033 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9035 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9036 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9037 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9038 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9039 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9040 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9041 136e2bd4 2022-07-23 mark view->state.log.repo);
9042 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9043 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9044 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9045 136e2bd4 2022-07-23 mark view->state.ref.repo);
9047 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9048 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9050 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9051 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9052 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9053 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9054 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9055 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9056 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9057 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9059 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9060 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9062 136e2bd4 2022-07-23 mark view_close(*new_view);
9064 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9065 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9066 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9067 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9068 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9070 ec2a9698 2022-09-15 mark view_close(*new_view);
9073 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9076 136e2bd4 2022-07-23 mark return err;
9080 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9081 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9083 6458efa5 2020-11-24 stsp static void
9084 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9086 9b058f45 2022-06-30 mark switch (view->type) {
9087 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9088 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9089 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9090 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9094 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9095 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9097 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9098 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9101 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9102 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9103 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9105 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9106 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9107 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9109 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9110 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9111 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9117 9b058f45 2022-06-30 mark view->offset = 0;
9121 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9122 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9124 9b058f45 2022-06-30 mark static const struct got_error *
9125 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9127 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9128 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9129 9b058f45 2022-06-30 mark int *selected = NULL;
9130 9b058f45 2022-06-30 mark int header, offset;
9132 9b058f45 2022-06-30 mark switch (view->type) {
9133 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9134 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9135 9b058f45 2022-06-30 mark header = 3;
9136 9b058f45 2022-06-30 mark scrolld = NULL;
9137 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9138 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9139 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9140 9b058f45 2022-06-30 mark s->selected_line -= offset;
9141 9b058f45 2022-06-30 mark view->offset = offset;
9145 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9146 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9147 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9148 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9149 9b058f45 2022-06-30 mark selected = &s->selected;
9152 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9153 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9154 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9155 9b058f45 2022-06-30 mark header = 3;
9156 9b058f45 2022-06-30 mark selected = &s->selected;
9159 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9160 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9161 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9162 9b058f45 2022-06-30 mark header = 5;
9163 9b058f45 2022-06-30 mark selected = &s->selected;
9167 9b058f45 2022-06-30 mark selected = NULL;
9168 9b058f45 2022-06-30 mark scrolld = NULL;
9169 9b058f45 2022-06-30 mark header = 0;
9173 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9174 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9175 9b058f45 2022-06-30 mark view->offset = offset;
9176 9b058f45 2022-06-30 mark if (scrolld && offset) {
9177 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9178 9b058f45 2022-06-30 mark *selected -= offset;
9182 9b058f45 2022-06-30 mark return err;
9185 9b058f45 2022-06-30 mark static void
9186 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9190 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9191 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9192 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9193 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9195 6879ba42 2020-10-01 naddy fputc('\n', fp);
9198 4ed7e80c 2018-05-20 stsp __dead static void
9199 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9201 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9203 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9204 6879ba42 2020-10-01 naddy getprogname());
9205 6879ba42 2020-10-01 naddy if (hflag) {
9206 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9207 6879ba42 2020-10-01 naddy list_commands(fp);
9209 6879ba42 2020-10-01 naddy exit(status);
9212 c2301be8 2018-04-30 stsp static char **
9213 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9215 ee85c5e8 2020-02-29 stsp va_list ap;
9216 c2301be8 2018-04-30 stsp char **argv;
9219 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9221 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9222 c2301be8 2018-04-30 stsp if (argv == NULL)
9223 c2301be8 2018-04-30 stsp err(1, "calloc");
9224 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9225 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9226 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9227 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9230 ee85c5e8 2020-02-29 stsp va_end(ap);
9231 c2301be8 2018-04-30 stsp return argv;
9235 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9236 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9237 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9238 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9239 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9241 ee85c5e8 2020-02-29 stsp static const struct got_error *
9242 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9244 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9245 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9246 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9247 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9248 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9249 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9250 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9251 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9252 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9254 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9255 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9256 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9258 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9259 0ae84acc 2022-06-15 tracey if (error != NULL)
9260 0ae84acc 2022-06-15 tracey goto done;
9262 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9263 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9264 ee85c5e8 2020-02-29 stsp goto done;
9266 ee85c5e8 2020-02-29 stsp if (worktree)
9267 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9269 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9270 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9271 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9272 ee85c5e8 2020-02-29 stsp goto done;
9275 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9276 ee85c5e8 2020-02-29 stsp if (error != NULL)
9277 ee85c5e8 2020-02-29 stsp goto done;
9279 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9280 ee85c5e8 2020-02-29 stsp repo, worktree);
9281 ee85c5e8 2020-02-29 stsp if (error)
9282 ee85c5e8 2020-02-29 stsp goto done;
9284 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9285 84de9106 2020-12-26 stsp if (error)
9286 84de9106 2020-12-26 stsp goto done;
9287 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9288 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9289 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9290 ee85c5e8 2020-02-29 stsp if (error)
9291 ee85c5e8 2020-02-29 stsp goto done;
9293 ee85c5e8 2020-02-29 stsp if (worktree) {
9294 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9295 ee85c5e8 2020-02-29 stsp worktree = NULL;
9298 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9299 a44927cc 2022-04-07 stsp if (error)
9300 a44927cc 2022-04-07 stsp goto done;
9302 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9303 ee85c5e8 2020-02-29 stsp if (error) {
9304 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9305 ee85c5e8 2020-02-29 stsp goto done;
9306 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9307 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9308 6879ba42 2020-10-01 naddy usage(1, 1);
9309 ee85c5e8 2020-02-29 stsp /* not reached */
9312 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9313 ee85c5e8 2020-02-29 stsp if (error)
9314 ee85c5e8 2020-02-29 stsp goto done;
9316 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9318 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9319 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9321 1d0f4054 2021-06-17 stsp if (repo) {
9322 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9323 1d0f4054 2021-06-17 stsp if (error == NULL)
9324 1d0f4054 2021-06-17 stsp error = close_err;
9326 a44927cc 2022-04-07 stsp if (commit)
9327 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9328 ee85c5e8 2020-02-29 stsp if (worktree)
9329 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9330 0ae84acc 2022-06-15 tracey if (pack_fds) {
9331 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9332 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9333 0ae84acc 2022-06-15 tracey if (error == NULL)
9334 0ae84acc 2022-06-15 tracey error = pack_err;
9337 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9338 ee85c5e8 2020-02-29 stsp free(commit_id);
9339 ee85c5e8 2020-02-29 stsp free(cwd);
9340 ee85c5e8 2020-02-29 stsp free(repo_path);
9341 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9342 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9344 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9345 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9346 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9348 87670572 2020-12-26 naddy tog_free_refs();
9349 ee85c5e8 2020-02-29 stsp return error;
9353 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9355 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9356 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9357 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9358 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9359 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9360 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9361 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9363 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9365 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9366 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9368 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9370 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9371 9f7d7167 2018-04-29 stsp switch (ch) {
9373 9f7d7167 2018-04-29 stsp hflag = 1;
9376 53ccebc2 2019-07-30 stsp Vflag = 1;
9379 6879ba42 2020-10-01 naddy usage(hflag, 1);
9380 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9384 9f7d7167 2018-04-29 stsp argc -= optind;
9385 9f7d7167 2018-04-29 stsp argv += optind;
9386 9814e6a3 2020-09-27 naddy optind = 1;
9387 c2301be8 2018-04-30 stsp optreset = 1;
9389 53ccebc2 2019-07-30 stsp if (Vflag) {
9390 53ccebc2 2019-07-30 stsp got_version_print_str();
9391 6879ba42 2020-10-01 naddy return 0;
9394 4010e238 2020-12-04 stsp #ifndef PROFILE
9395 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9396 4010e238 2020-12-04 stsp NULL) == -1)
9397 4010e238 2020-12-04 stsp err(1, "pledge");
9400 c2301be8 2018-04-30 stsp if (argc == 0) {
9401 f29d3e89 2018-06-23 stsp if (hflag)
9402 6879ba42 2020-10-01 naddy usage(hflag, 0);
9403 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9404 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9406 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9410 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9411 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9412 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9413 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9414 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9420 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9421 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9422 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9423 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9424 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9425 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9428 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9429 ee85c5e8 2020-02-29 stsp if (argc != 1)
9430 6879ba42 2020-10-01 naddy usage(0, 1);
9431 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9432 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9434 ee85c5e8 2020-02-29 stsp if (hflag)
9435 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9437 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9441 b46c1e04 2020-09-20 naddy putchar('\n');
9442 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9444 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9445 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9446 a2f4a359 2020-02-28 stsp free(cmd_argv);
9449 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9450 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9451 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9452 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9453 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9454 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);