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;
693 3f6c6614 2023-01-23 mark const char *action;
696 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
697 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
698 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
699 78756c87 2020-11-24 stsp struct got_repository *);
700 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
701 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
702 e78dc838 2020-12-04 stsp struct tog_view *, int);
703 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
704 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
705 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
706 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
707 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
708 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
710 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
711 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
712 78756c87 2020-11-24 stsp const char *, const char *, int);
713 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
714 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
715 e78dc838 2020-12-04 stsp struct tog_view *, int);
716 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
717 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
718 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
719 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
721 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
722 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
723 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
724 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
725 e78dc838 2020-12-04 stsp struct tog_view *, int);
726 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
727 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
728 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
729 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
730 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
732 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
733 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
734 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
735 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
736 e78dc838 2020-12-04 stsp struct tog_view *, int);
737 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
738 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
739 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
741 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
742 6458efa5 2020-11-24 stsp struct got_repository *);
743 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
744 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
745 e78dc838 2020-12-04 stsp struct tog_view *, int);
746 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
747 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
748 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
750 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
751 eaf2c13e 2022-09-17 mark struct tog_view *);
752 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
753 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
754 eaf2c13e 2022-09-17 mark struct tog_view *, int);
755 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
756 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
757 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
758 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
759 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
761 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
762 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
763 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
764 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
765 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
767 25791caa 2018-10-24 stsp static void
768 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
770 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
773 83baff54 2019-08-12 stsp static void
774 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
776 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
779 61266923 2020-01-14 stsp static void
780 61266923 2020-01-14 stsp tog_sigcont(int signo)
782 61266923 2020-01-14 stsp tog_sigcont_received = 1;
785 2497f032 2022-05-31 stsp static void
786 2497f032 2022-05-31 stsp tog_sigint(int signo)
788 2497f032 2022-05-31 stsp tog_sigint_received = 1;
791 2497f032 2022-05-31 stsp static void
792 2497f032 2022-05-31 stsp tog_sigterm(int signo)
794 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
798 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
800 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
801 f044c841 2022-10-24 stsp tog_sigint_received || tog_sigterm_received);
804 e5a0f69f 2018-08-18 stsp static const struct got_error *
805 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
807 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
809 669b5ffa 2018-10-07 stsp if (view->child) {
810 5629093a 2022-07-11 stsp child_err = view_close(view->child);
811 669b5ffa 2018-10-07 stsp view->child = NULL;
813 e5a0f69f 2018-08-18 stsp if (view->close)
814 e5a0f69f 2018-08-18 stsp err = view->close(view);
815 ea5e7bb5 2018-08-01 stsp if (view->panel)
816 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
817 ea5e7bb5 2018-08-01 stsp if (view->window)
818 ea5e7bb5 2018-08-01 stsp delwin(view->window);
819 ea5e7bb5 2018-08-01 stsp free(view);
820 5629093a 2022-07-11 stsp return err ? err : child_err;
823 ea5e7bb5 2018-08-01 stsp static struct tog_view *
824 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
825 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
827 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
829 ea5e7bb5 2018-08-01 stsp if (view == NULL)
830 ea5e7bb5 2018-08-01 stsp return NULL;
832 d6b05b5b 2018-08-04 stsp view->type = type;
833 f7d12f7e 2018-08-01 stsp view->lines = LINES;
834 f7d12f7e 2018-08-01 stsp view->cols = COLS;
835 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
836 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
837 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
838 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
839 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
840 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
841 96a765a8 2018-08-04 stsp view_close(view);
842 ea5e7bb5 2018-08-01 stsp return NULL;
844 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
845 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
846 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
847 96a765a8 2018-08-04 stsp view_close(view);
848 ea5e7bb5 2018-08-01 stsp return NULL;
851 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
852 ea5e7bb5 2018-08-01 stsp return view;
856 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
858 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
860 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
863 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
865 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
867 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
870 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
872 5c60c32a 2018-10-18 stsp static const struct got_error *
873 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
875 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
877 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
878 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
879 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
881 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
882 9b058f45 2022-06-30 mark view->begin_x = 0;
883 571ccd73 2022-07-19 mark } else if (!view->resized) {
884 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
885 3c1dfe12 2022-07-08 mark view->cols > 119)
886 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
888 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
889 9b058f45 2022-06-30 mark view->begin_y = 0;
891 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
892 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
893 5c60c32a 2018-10-18 stsp view->lines = LINES;
894 5c60c32a 2018-10-18 stsp view->cols = COLS;
895 5c60c32a 2018-10-18 stsp err = view_resize(view);
897 5c60c32a 2018-10-18 stsp return err;
899 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
900 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
902 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
903 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
905 5c60c32a 2018-10-18 stsp return NULL;
908 5c60c32a 2018-10-18 stsp static const struct got_error *
909 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
911 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
913 5c60c32a 2018-10-18 stsp view->begin_x = 0;
914 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
915 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
916 5c60c32a 2018-10-18 stsp view->ncols = COLS;
917 5c60c32a 2018-10-18 stsp view->lines = LINES;
918 5c60c32a 2018-10-18 stsp view->cols = COLS;
919 5c60c32a 2018-10-18 stsp err = view_resize(view);
921 5c60c32a 2018-10-18 stsp return err;
923 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
924 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
926 5c60c32a 2018-10-18 stsp return NULL;
930 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
932 5c60c32a 2018-10-18 stsp return view->parent == NULL;
936 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
938 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
942 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
944 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
948 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
950 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
951 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
954 9b058f45 2022-06-30 mark static void
955 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
957 9b058f45 2022-06-30 mark PANEL *panel;
958 9b058f45 2022-06-30 mark const struct tog_view *view_above;
960 9b058f45 2022-06-30 mark if (view->parent)
961 9b058f45 2022-06-30 mark return view_border(view->parent);
963 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
964 9b058f45 2022-06-30 mark if (panel == NULL)
967 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
968 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
969 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
970 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
971 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
973 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
974 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
977 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
978 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
979 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
980 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
981 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
983 4d8c2215 2018-08-19 stsp static const struct got_error *
984 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
986 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
987 9b058f45 2022-06-30 mark int dif, nlines, ncols;
989 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
991 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
992 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
994 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
995 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
996 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
998 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1000 4dd27a72 2022-06-29 stsp if (view->child) {
1001 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
1003 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1004 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1005 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1006 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1007 0dbbbe90 2022-06-17 op ncols = COLS;
1009 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1010 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1011 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1013 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1015 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1017 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1018 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1021 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1022 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1023 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1026 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1028 9b058f45 2022-06-30 mark return err;
1029 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1030 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1032 9b058f45 2022-06-30 mark return err;
1034 9b058f45 2022-06-30 mark view_border(view);
1035 9b058f45 2022-06-30 mark update_panels();
1036 9b058f45 2022-06-30 mark doupdate();
1037 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1038 9b058f45 2022-06-30 mark nlines = view->nlines;
1040 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1041 0dbbbe90 2022-06-17 op ncols = COLS;
1043 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1044 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1046 571ccd73 2022-07-19 mark return err;
1049 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1050 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1051 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1052 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1053 0dbbbe90 2022-06-17 op wclear(view->window);
1055 0dbbbe90 2022-06-17 op view->nlines = nlines;
1056 0dbbbe90 2022-06-17 op view->ncols = ncols;
1057 0dbbbe90 2022-06-17 op view->lines = LINES;
1058 0dbbbe90 2022-06-17 op view->cols = COLS;
1060 5c60c32a 2018-10-18 stsp return NULL;
1063 571ccd73 2022-07-19 mark static const struct got_error *
1064 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1066 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1067 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1068 6fe51fee 2022-07-22 mark int n = 0;
1070 6fe51fee 2022-07-22 mark if (s->selected_entry)
1071 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1074 571ccd73 2022-07-19 mark * Request commits to account for the increased
1075 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1077 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1078 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1079 571ccd73 2022-07-19 mark err = request_log_commits(view);
1082 571ccd73 2022-07-19 mark return err;
1085 d9a7ab53 2022-07-11 mark static void
1086 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1088 d9a7ab53 2022-07-11 mark if (n == 0)
1091 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1092 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1093 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1095 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1096 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1097 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1098 d9a7ab53 2022-07-11 mark view->offset -= n;
1100 d9a7ab53 2022-07-11 mark view->offset = 0;
1104 3c1dfe12 2022-07-08 mark static const struct got_error *
1105 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1107 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1108 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1110 3c1dfe12 2022-07-08 mark if (view->parent)
1111 3c1dfe12 2022-07-08 mark v = view->parent;
1115 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1116 3c1dfe12 2022-07-08 mark return NULL;
1118 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1120 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1121 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1122 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1123 3c1dfe12 2022-07-08 mark if (view->parent)
1124 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1126 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1127 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1128 3c1dfe12 2022-07-08 mark view->count = 0;
1129 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1130 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1131 3c1dfe12 2022-07-08 mark view->count = 0;
1132 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1134 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1135 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1136 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1137 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1139 3c1dfe12 2022-07-08 mark return err;
1140 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1142 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1143 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1144 3c1dfe12 2022-07-08 mark if (view->parent)
1145 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1147 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1148 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1149 3c1dfe12 2022-07-08 mark view->count = 0;
1150 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1151 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1152 3c1dfe12 2022-07-08 mark view->count = 0;
1153 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1155 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1158 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1159 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1160 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1161 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1163 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1165 3c1dfe12 2022-07-08 mark return err;
1166 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1168 3c1dfe12 2022-07-08 mark return err;
1170 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1171 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1173 3c1dfe12 2022-07-08 mark return err;
1176 6fe51fee 2022-07-22 mark if (v->resize)
1177 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1178 6fe51fee 2022-07-22 mark else if (v->child->resize)
1179 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1181 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1183 3c1dfe12 2022-07-08 mark return err;
1186 3c1dfe12 2022-07-08 mark static void
1187 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1189 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1191 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1192 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1195 669b5ffa 2018-10-07 stsp static const struct got_error *
1196 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1198 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1200 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1201 669b5ffa 2018-10-07 stsp return NULL;
1203 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1204 669b5ffa 2018-10-07 stsp view->child = NULL;
1205 669b5ffa 2018-10-07 stsp return err;
1208 0dbbbe90 2022-06-17 op static const struct got_error *
1209 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1211 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1213 669b5ffa 2018-10-07 stsp view->child = child;
1214 669b5ffa 2018-10-07 stsp child->parent = view;
1216 3c1dfe12 2022-07-08 mark err = view_resize(view);
1218 3c1dfe12 2022-07-08 mark return err;
1220 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1221 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1223 3c1dfe12 2022-07-08 mark return err;
1226 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1227 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1229 136e2bd4 2022-07-23 mark static const struct got_error *
1230 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1231 136e2bd4 2022-07-23 mark enum tog_view_type request)
1233 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1234 136e2bd4 2022-07-23 mark const struct got_error *err;
1235 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1237 136e2bd4 2022-07-23 mark *requested = NULL;
1239 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1240 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1242 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1244 136e2bd4 2022-07-23 mark return err;
1246 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1247 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1248 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1250 136e2bd4 2022-07-23 mark return err;
1253 136e2bd4 2022-07-23 mark view->focussed = 0;
1254 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1255 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1256 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1257 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1259 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1260 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1261 136e2bd4 2022-07-23 mark err = view_close_child(view);
1263 136e2bd4 2022-07-23 mark return err;
1264 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1266 136e2bd4 2022-07-23 mark return err;
1267 136e2bd4 2022-07-23 mark view->focus_child = 1;
1269 136e2bd4 2022-07-23 mark *requested = new_view;
1271 136e2bd4 2022-07-23 mark return NULL;
1274 34bc9ec9 2019-02-22 stsp static void
1275 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1277 25791caa 2018-10-24 stsp int cols, lines;
1278 25791caa 2018-10-24 stsp struct winsize size;
1280 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1281 25791caa 2018-10-24 stsp cols = 80; /* Default */
1282 25791caa 2018-10-24 stsp lines = 24;
1284 25791caa 2018-10-24 stsp cols = size.ws_col;
1285 25791caa 2018-10-24 stsp lines = size.ws_row;
1287 25791caa 2018-10-24 stsp resize_term(lines, cols);
1290 2b49a8ae 2019-06-22 stsp static const struct got_error *
1291 602eda79 2023-02-09 mark view_search_start(struct tog_view *view, int fast_refresh)
1293 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1294 9b058f45 2022-06-30 mark struct tog_view *v = view;
1295 2b49a8ae 2019-06-22 stsp char pattern[1024];
1298 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1299 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1300 c0c4acc8 2021-01-24 stsp view->searching = 0;
1301 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1303 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1305 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1306 2b49a8ae 2019-06-22 stsp return NULL;
1308 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1309 9b058f45 2022-06-30 mark v = view->child;
1310 9d0feb8b 2022-12-27 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1311 9d0feb8b 2022-12-27 mark v = view->parent;
1313 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1314 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1316 092a9f9c 2022-12-27 mark nodelay(v->window, FALSE); /* block for search term input */
1317 2b49a8ae 2019-06-22 stsp nocbreak();
1319 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1320 9b058f45 2022-06-30 mark wrefresh(v->window);
1323 092a9f9c 2022-12-27 mark nodelay(v->window, TRUE);
1324 602eda79 2023-02-09 mark if (!fast_refresh)
1325 602eda79 2023-02-09 mark halfdelay(10);
1326 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1327 2b49a8ae 2019-06-22 stsp return NULL;
1329 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1330 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1331 7c32bd05 2019-06-22 stsp if (err) {
1332 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1333 7c32bd05 2019-06-22 stsp return err;
1335 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1336 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1337 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1338 2b49a8ae 2019-06-22 stsp view->search_next(view);
1341 2b49a8ae 2019-06-22 stsp return NULL;
1344 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1345 d2366e29 2022-07-07 mark static const struct got_error *
1346 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1348 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1349 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1351 d2366e29 2022-07-07 mark if (view->parent)
1352 d2366e29 2022-07-07 mark v = view->parent;
1356 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1357 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1359 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1361 7532ccda 2022-07-11 mark if (!v->child)
1362 7532ccda 2022-07-11 mark return NULL;
1363 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1364 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1366 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1367 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1368 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1369 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1370 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1373 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1374 d2366e29 2022-07-07 mark v->ncols = COLS;
1375 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1376 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1378 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1380 d2366e29 2022-07-07 mark return err;
1382 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1383 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1384 d2366e29 2022-07-07 mark v->focus_child = 1;
1386 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1388 d2366e29 2022-07-07 mark return err;
1389 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1391 d2366e29 2022-07-07 mark return err;
1393 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1394 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1395 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1396 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1398 279d2047 2022-07-29 stsp return err;
1399 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1401 279d2047 2022-07-29 stsp return err;
1403 7532ccda 2022-07-11 mark offset_selection_up(v);
1404 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1406 dff91825 2022-07-22 mark if (v->resize)
1407 dff91825 2022-07-22 mark err = v->resize(v, 0);
1408 dff91825 2022-07-22 mark else if (v->child->resize)
1409 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1411 d2366e29 2022-07-07 mark return err;
1415 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1416 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1418 640cd7ff 2022-06-22 mark static int
1419 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1421 9b058f45 2022-06-30 mark struct tog_view *v = view;
1422 9b058f45 2022-06-30 mark int x, n = 0;
1424 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1425 9b058f45 2022-06-30 mark v = view->child;
1426 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1427 9b058f45 2022-06-30 mark v = view->parent;
1429 640cd7ff 2022-06-22 mark view->count = 0;
1430 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1431 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1432 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1433 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1434 9b058f45 2022-06-30 mark waddch(v->window, ':');
1437 9b058f45 2022-06-30 mark x = getcurx(v->window);
1438 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1439 9b058f45 2022-06-30 mark waddch(v->window, c);
1440 9b058f45 2022-06-30 mark wrefresh(v->window);
1444 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1445 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1447 640cd7ff 2022-06-22 mark if (n >= 9999999)
1448 640cd7ff 2022-06-22 mark n = 9999999;
1450 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1451 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1453 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1454 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1459 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1460 640cd7ff 2022-06-22 mark view->count = n;
1465 3f6c6614 2023-01-23 mark static void
1466 3f6c6614 2023-01-23 mark action_report(struct tog_view *view)
1468 3f6c6614 2023-01-23 mark struct tog_view *v = view;
1470 3f6c6614 2023-01-23 mark if (view_is_hsplit_top(view))
1471 3f6c6614 2023-01-23 mark v = view->child;
1472 3f6c6614 2023-01-23 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1473 3f6c6614 2023-01-23 mark v = view->parent;
1475 3f6c6614 2023-01-23 mark wmove(v->window, v->nlines - 1, 0);
1476 3f6c6614 2023-01-23 mark wclrtoeol(v->window);
1477 3f6c6614 2023-01-23 mark wprintw(v->window, ":%s", view->action);
1478 3f6c6614 2023-01-23 mark wrefresh(v->window);
1481 3f6c6614 2023-01-23 mark * Clear action status report. Only clear in blame view
1482 3f6c6614 2023-01-23 mark * once annotating is complete, otherwise it's too fast.
1484 3f6c6614 2023-01-23 mark if (view->type == TOG_VIEW_BLAME) {
1485 3f6c6614 2023-01-23 mark if (view->state.blame.blame_complete)
1486 3f6c6614 2023-01-23 mark view->action = NULL;
1488 3f6c6614 2023-01-23 mark view->action = NULL;
1491 0cf4efb1 2018-09-29 stsp static const struct got_error *
1492 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1493 602eda79 2023-02-09 mark struct tog_view_list_head *views, int fast_refresh)
1495 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1496 669b5ffa 2018-10-07 stsp struct tog_view *v;
1497 1a76625f 2018-10-22 stsp int ch, errcode;
1499 e5a0f69f 2018-08-18 stsp *new = NULL;
1501 3f6c6614 2023-01-23 mark if (view->action)
1502 3f6c6614 2023-01-23 mark action_report(view);
1504 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1505 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1506 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1507 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1508 640cd7ff 2022-06-22 mark view->count = 0;
1511 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1512 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1513 82954512 2020-02-03 stsp if (errcode)
1514 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1515 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1516 3da8ef85 2021-09-21 stsp sched_yield();
1517 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1518 82954512 2020-02-03 stsp if (errcode)
1519 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1520 82954512 2020-02-03 stsp "pthread_mutex_lock");
1521 60493ae3 2019-06-20 stsp view->search_next(view);
1522 60493ae3 2019-06-20 stsp return NULL;
1525 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1526 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1527 1a76625f 2018-10-22 stsp if (errcode)
1528 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1529 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1530 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1532 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1533 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1534 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1535 a6d37fac 2022-07-03 mark view->count = 0;
1537 a6d37fac 2022-07-03 mark ch = view->ch;
1539 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1540 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1541 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1543 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1544 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1545 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1546 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1547 1a76625f 2018-10-22 stsp if (errcode)
1548 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1550 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1551 25791caa 2018-10-24 stsp tog_resizeterm();
1552 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1553 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1554 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1555 25791caa 2018-10-24 stsp err = view_resize(v);
1557 25791caa 2018-10-24 stsp return err;
1558 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1560 25791caa 2018-10-24 stsp return err;
1561 cdfcfb03 2020-12-06 stsp if (v->child) {
1562 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1564 cdfcfb03 2020-12-06 stsp return err;
1565 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1566 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1568 cdfcfb03 2020-12-06 stsp return err;
1569 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1570 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1572 3c1dfe12 2022-07-08 mark return err;
1578 e5a0f69f 2018-08-18 stsp switch (ch) {
1581 ec2a9698 2022-09-15 mark case KEY_F(1):
1582 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1583 ec2a9698 2022-09-15 mark err = view->reset(view);
1585 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1588 640cd7ff 2022-06-22 mark view->count = 0;
1589 1e37a5c2 2019-05-12 jcs if (view->child) {
1590 e78dc838 2020-12-04 stsp view->focussed = 0;
1591 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1592 e78dc838 2020-12-04 stsp view->focus_child = 1;
1593 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1594 e78dc838 2020-12-04 stsp view->focussed = 0;
1595 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1596 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1597 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1598 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1599 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1602 9b058f45 2022-06-30 mark return err;
1604 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1605 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1607 9b058f45 2022-06-30 mark return err;
1612 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1613 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1614 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1615 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1619 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1621 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1622 9970f7fc 2020-12-03 stsp view->dying = 1;
1628 640cd7ff 2022-06-22 mark view->count = 0;
1629 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1630 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1632 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1633 e78dc838 2020-12-04 stsp view->focussed = 0;
1634 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1635 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1637 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1639 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1643 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1644 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1646 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1647 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1648 e78dc838 2020-12-04 stsp view->focussed = 1;
1649 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1651 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1652 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1653 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1655 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1659 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1663 6fe51fee 2022-07-22 mark if (view->resize) {
1664 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1668 9b058f45 2022-06-30 mark if (view->parent)
1669 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1671 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1674 3c1dfe12 2022-07-08 mark view->count = 0;
1675 d2366e29 2022-07-07 mark err = switch_split(view);
1678 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1681 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1683 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1686 640cd7ff 2022-06-22 mark view->count = 0;
1687 60493ae3 2019-06-20 stsp if (view->search_start)
1688 602eda79 2023-02-09 mark view_search_start(view, fast_refresh);
1690 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1694 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1695 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1696 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1697 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1698 60493ae3 2019-06-20 stsp view->search_next(view);
1700 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1703 3f6c6614 2023-01-23 mark if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1704 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1705 3f6c6614 2023-01-23 mark view->action = "Patience diff algorithm";
1707 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1708 3f6c6614 2023-01-23 mark view->action = "Myers diff algorithm";
1710 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1711 917d79a7 2022-07-01 stsp if (v->reset) {
1712 917d79a7 2022-07-01 stsp err = v->reset(v);
1714 917d79a7 2022-07-01 stsp return err;
1716 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1717 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1719 917d79a7 2022-07-01 stsp return err;
1724 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1728 e5a0f69f 2018-08-18 stsp return err;
1732 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1734 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1735 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1737 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1739 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1742 669b5ffa 2018-10-07 stsp return view->focussed;
1745 bcbd79e2 2018-08-19 stsp static const struct got_error *
1746 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1748 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1749 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1750 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1751 d2366e29 2022-07-07 mark char *mode;
1752 fd823528 2018-10-22 stsp int fast_refresh = 10;
1753 1a76625f 2018-10-22 stsp int done = 0, errcode;
1755 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1756 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1757 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1759 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1761 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1762 1a76625f 2018-10-22 stsp if (errcode)
1763 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1765 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1766 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1768 1004088d 2018-09-29 stsp view->focussed = 1;
1769 878940b7 2018-09-29 stsp err = view->show(view);
1771 0cf4efb1 2018-09-29 stsp return err;
1772 0cf4efb1 2018-09-29 stsp update_panels();
1773 0cf4efb1 2018-09-29 stsp doupdate();
1774 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1775 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1776 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1777 388b24d6 2023-01-23 mark if (fast_refresh && --fast_refresh == 0)
1778 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1780 602eda79 2023-02-09 mark err = view_input(&new_view, &done, view, &views, fast_refresh);
1784 dc2c3344 2023-01-23 mark if (view->dying && view == TAILQ_FIRST(&views) &&
1785 dc2c3344 2023-01-23 mark TAILQ_NEXT(view, entry) == NULL)
1787 dc2c3344 2023-01-23 mark if (done) {
1788 dc2c3344 2023-01-23 mark struct tog_view *v;
1791 dc2c3344 2023-01-23 mark * When we quit, scroll the screen up a single line
1792 dc2c3344 2023-01-23 mark * so we don't lose any information.
1794 dc2c3344 2023-01-23 mark TAILQ_FOREACH(v, &views, entry) {
1795 dc2c3344 2023-01-23 mark wmove(v->window, 0, 0);
1796 dc2c3344 2023-01-23 mark wdeleteln(v->window);
1797 dc2c3344 2023-01-23 mark wnoutrefresh(v->window);
1798 dc2c3344 2023-01-23 mark if (v->child && !view_is_fullscreen(v)) {
1799 dc2c3344 2023-01-23 mark wmove(v->child->window, 0, 0);
1800 dc2c3344 2023-01-23 mark wdeleteln(v->child->window);
1801 dc2c3344 2023-01-23 mark wnoutrefresh(v->child->window);
1804 dc2c3344 2023-01-23 mark doupdate();
1807 9970f7fc 2020-12-03 stsp if (view->dying) {
1808 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1810 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1811 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1813 e78dc838 2020-12-04 stsp else if (view->parent)
1814 669b5ffa 2018-10-07 stsp prev = view->parent;
1816 e78dc838 2020-12-04 stsp if (view->parent) {
1817 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1818 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1819 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1820 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1821 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1824 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1825 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1827 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1829 9970f7fc 2020-12-03 stsp err = view_close(view);
1831 e5a0f69f 2018-08-18 stsp goto done;
1833 e78dc838 2020-12-04 stsp view = NULL;
1834 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1835 e78dc838 2020-12-04 stsp if (v->focussed)
1838 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1839 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1841 e78dc838 2020-12-04 stsp view = prev;
1842 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1843 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1844 e78dc838 2020-12-04 stsp tog_view_list_head);
1846 e78dc838 2020-12-04 stsp if (view) {
1847 e78dc838 2020-12-04 stsp if (view->focus_child) {
1848 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1849 e78dc838 2020-12-04 stsp view = view->child;
1851 e78dc838 2020-12-04 stsp view->focussed = 1;
1855 bcbd79e2 2018-08-19 stsp if (new_view) {
1856 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1857 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1858 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1859 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1861 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1862 86c66b02 2018-10-18 stsp err = view_close(v);
1864 86c66b02 2018-10-18 stsp goto done;
1867 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1868 fed7eaa8 2018-10-24 stsp view = new_view;
1870 dc2c3344 2023-01-23 mark if (view && !done) {
1871 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1872 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1873 e78dc838 2020-12-04 stsp view = view->child;
1875 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1876 e78dc838 2020-12-04 stsp view = view->parent;
1878 e78dc838 2020-12-04 stsp show_panel(view->panel);
1879 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1880 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1881 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1882 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1884 1a76625f 2018-10-22 stsp goto done;
1886 669b5ffa 2018-10-07 stsp err = view->show(view);
1888 1a76625f 2018-10-22 stsp goto done;
1889 669b5ffa 2018-10-07 stsp if (view->child) {
1890 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1892 1a76625f 2018-10-22 stsp goto done;
1894 1a76625f 2018-10-22 stsp update_panels();
1895 1a76625f 2018-10-22 stsp doupdate();
1899 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1900 5629093a 2022-07-11 stsp const struct got_error *close_err;
1901 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1902 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1903 5629093a 2022-07-11 stsp close_err = view_close(view);
1904 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1905 5629093a 2022-07-11 stsp err = close_err;
1908 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1909 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1910 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1912 e5a0f69f 2018-08-18 stsp return err;
1915 4ed7e80c 2018-05-20 stsp __dead static void
1916 9f7d7167 2018-04-29 stsp usage_log(void)
1919 c70c5802 2018-08-01 stsp fprintf(stderr,
1920 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1921 9f7d7167 2018-04-29 stsp getprogname());
1925 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1926 80ddbec8 2018-04-29 stsp static const struct got_error *
1927 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1929 00dfcb92 2018-06-11 stsp char *vis = NULL;
1930 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1932 963b370f 2018-05-20 stsp *ws = NULL;
1933 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1934 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1935 00dfcb92 2018-06-11 stsp int vislen;
1936 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1937 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1939 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1940 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1942 00dfcb92 2018-06-11 stsp return err;
1943 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1944 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1945 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1946 a7f50699 2018-06-11 stsp goto done;
1950 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1951 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1952 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1953 a7f50699 2018-06-11 stsp goto done;
1956 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1957 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1959 00dfcb92 2018-06-11 stsp free(vis);
1960 963b370f 2018-05-20 stsp if (err) {
1961 963b370f 2018-05-20 stsp free(*ws);
1962 963b370f 2018-05-20 stsp *ws = NULL;
1963 963b370f 2018-05-20 stsp *wlen = 0;
1965 963b370f 2018-05-20 stsp return err;
1968 145b6838 2022-06-16 stsp static const struct got_error *
1969 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1971 145b6838 2022-06-16 stsp char *dst;
1972 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1974 145b6838 2022-06-16 stsp *ptr = NULL;
1975 145b6838 2022-06-16 stsp n = len = strlen(src);
1976 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1977 145b6838 2022-06-16 stsp if (dst == NULL)
1978 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1980 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1981 145b6838 2022-06-16 stsp const char c = src[idx];
1983 145b6838 2022-06-16 stsp if (c == '\t') {
1984 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1987 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1988 6e1c41ad 2022-06-16 mark if (p == NULL) {
1989 6e1c41ad 2022-06-16 mark free(dst);
1990 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1995 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1998 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
2002 145b6838 2022-06-16 stsp dst[sz] = '\0';
2003 145b6838 2022-06-16 stsp *ptr = dst;
2004 145b6838 2022-06-16 stsp return NULL;
2008 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
2009 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
2010 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
2014 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2016 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
2018 4e4a9ac8 2022-06-17 op if (n == 0) {
2019 4e4a9ac8 2022-06-17 op *rcol = cols;
2023 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
2024 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
2025 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2027 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
2029 4e4a9ac8 2022-06-17 op if (width == -1) {
2031 4e4a9ac8 2022-06-17 op wline[i] = L'.';
2034 4e4a9ac8 2022-06-17 op if (cols + width > n)
2036 4e4a9ac8 2022-06-17 op cols += width;
2039 4e4a9ac8 2022-06-17 op *rcol = cols;
2044 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
2045 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
2046 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2048 ccda2f4d 2022-06-16 stsp static const struct got_error *
2049 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2050 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2052 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2054 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2055 145b6838 2022-06-16 stsp char *exstr = NULL;
2056 963b370f 2018-05-20 stsp size_t wlen;
2057 4e4a9ac8 2022-06-17 op int i, scrollx;
2059 963b370f 2018-05-20 stsp *wlinep = NULL;
2060 b700b5d6 2018-07-10 stsp *widthp = 0;
2062 145b6838 2022-06-16 stsp if (expand) {
2063 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2065 145b6838 2022-06-16 stsp return err;
2068 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2069 145b6838 2022-06-16 stsp free(exstr);
2071 963b370f 2018-05-20 stsp return err;
2073 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2075 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2076 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2079 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2080 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2084 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2085 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2087 b700b5d6 2018-07-10 stsp if (widthp)
2088 b700b5d6 2018-07-10 stsp *widthp = cols;
2089 ccda2f4d 2022-06-16 stsp if (scrollxp)
2090 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2092 963b370f 2018-05-20 stsp free(wline);
2094 963b370f 2018-05-20 stsp *wlinep = wline;
2095 963b370f 2018-05-20 stsp return err;
2098 8b473291 2019-02-21 stsp static const struct got_error*
2099 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2100 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2102 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2103 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2105 8b473291 2019-02-21 stsp const char *name;
2107 8b473291 2019-02-21 stsp *refs_str = NULL;
2109 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2110 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2111 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2114 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2115 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2117 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2118 8b473291 2019-02-21 stsp name += 5;
2119 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2120 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2122 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2123 8b473291 2019-02-21 stsp name += 6;
2124 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2125 8b473291 2019-02-21 stsp name += 8;
2126 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2127 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2130 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2133 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2134 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2135 5d844a1e 2019-08-13 stsp if (err) {
2136 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2137 48cae60d 2020-09-22 stsp free(ref_id);
2140 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2141 5d844a1e 2019-08-13 stsp err = NULL;
2142 5d844a1e 2019-08-13 stsp tag = NULL;
2145 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2146 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2147 48cae60d 2020-09-22 stsp free(ref_id);
2149 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2150 52b5abe1 2019-08-13 stsp if (cmp != 0)
2152 8b473291 2019-02-21 stsp s = *refs_str;
2153 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2154 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2155 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2157 8b473291 2019-02-21 stsp *refs_str = NULL;
2163 8b473291 2019-02-21 stsp return err;
2166 963b370f 2018-05-20 stsp static const struct got_error *
2167 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2168 27a741e5 2019-09-11 stsp int col_tab_align)
2170 e6b8b890 2020-12-29 naddy char *smallerthan;
2172 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2173 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2174 5813d178 2019-03-09 stsp author = smallerthan + 1;
2175 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2176 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2177 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2180 5813d178 2019-03-09 stsp static const struct got_error *
2181 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2182 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2183 8fdc79fe 2020-12-01 naddy int author_display_cols)
2185 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2186 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2187 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2188 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2189 5813d178 2019-03-09 stsp char *author = NULL;
2190 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2191 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2192 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2193 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2194 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2195 ccb26ccd 2018-11-05 stsp struct tm tm;
2196 45d799e2 2018-12-23 stsp time_t committer_time;
2197 11b20872 2019-11-08 stsp struct tog_color *tc;
2199 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2200 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2201 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2202 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2203 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2205 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2206 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2208 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2209 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2211 11b20872 2019-11-08 stsp wattr_on(view->window,
2212 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2213 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2215 11b20872 2019-11-08 stsp wattr_off(view->window,
2216 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2217 27a741e5 2019-09-11 stsp col = limit;
2218 b39d25c7 2018-07-10 stsp if (col > avail)
2219 b39d25c7 2018-07-10 stsp goto done;
2221 6570a66d 2019-11-08 stsp if (avail >= 120) {
2222 6570a66d 2019-11-08 stsp char *id_str;
2223 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2225 6570a66d 2019-11-08 stsp goto done;
2226 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2228 11b20872 2019-11-08 stsp wattr_on(view->window,
2229 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2230 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2232 11b20872 2019-11-08 stsp wattr_off(view->window,
2233 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2234 6570a66d 2019-11-08 stsp free(id_str);
2236 6570a66d 2019-11-08 stsp if (col > avail)
2237 6570a66d 2019-11-08 stsp goto done;
2240 10aab77f 2022-07-19 op if (s->use_committer)
2241 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2243 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2244 5813d178 2019-03-09 stsp if (author == NULL) {
2245 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2246 80ddbec8 2018-04-29 stsp goto done;
2248 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2250 bb737323 2018-05-20 stsp goto done;
2251 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2253 11b20872 2019-11-08 stsp wattr_on(view->window,
2254 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2255 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2256 bb737323 2018-05-20 stsp col += author_width;
2257 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2258 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2260 bb737323 2018-05-20 stsp author_width++;
2263 f0f62654 2022-09-08 mark wattr_off(view->window,
2264 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2265 9c2eaf34 2018-05-20 stsp if (col > avail)
2266 9c2eaf34 2018-05-20 stsp goto done;
2268 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2270 6d9fbc00 2018-04-29 stsp goto done;
2271 bb737323 2018-05-20 stsp logmsg = logmsg0;
2272 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2274 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2275 bb737323 2018-05-20 stsp if (newline)
2276 bb737323 2018-05-20 stsp *newline = '\0';
2277 ccda2f4d 2022-06-16 stsp limit = avail - col;
2278 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2279 4d1f6af3 2022-06-17 op limit--; /* for the border */
2280 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2281 ccda2f4d 2022-06-16 stsp limit, col, 1);
2283 29688b02 2022-06-16 stsp goto done;
2284 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2285 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2286 27a741e5 2019-09-11 stsp while (col < avail) {
2287 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2291 80ddbec8 2018-04-29 stsp free(logmsg0);
2292 bb737323 2018-05-20 stsp free(wlogmsg);
2293 5813d178 2019-03-09 stsp free(author);
2294 bb737323 2018-05-20 stsp free(wauthor);
2295 80ddbec8 2018-04-29 stsp free(line);
2296 80ddbec8 2018-04-29 stsp return err;
2299 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2300 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2301 899d86c2 2018-05-10 stsp struct got_object_id *id)
2303 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2304 e15c42de 2022-09-05 op struct got_object_id *dup;
2306 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2307 80ddbec8 2018-04-29 stsp if (entry == NULL)
2308 899d86c2 2018-05-10 stsp return NULL;
2310 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2311 e15c42de 2022-09-05 op if (dup == NULL) {
2312 e15c42de 2022-09-05 op free(entry);
2313 e15c42de 2022-09-05 op return NULL;
2316 e15c42de 2022-09-05 op entry->id = dup;
2317 99db9666 2018-05-07 stsp entry->commit = commit;
2318 899d86c2 2018-05-10 stsp return entry;
2321 99db9666 2018-05-07 stsp static void
2322 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2324 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2326 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2327 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2328 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2329 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2330 e15c42de 2022-09-05 op free(entry->id);
2331 99db9666 2018-05-07 stsp free(entry);
2334 99db9666 2018-05-07 stsp static void
2335 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2337 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2338 99db9666 2018-05-07 stsp pop_commit(commits);
2341 c4972b91 2018-05-07 stsp static const struct got_error *
2342 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2343 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2345 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2346 13add988 2019-10-15 stsp regmatch_t regmatch;
2347 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2349 13add988 2019-10-15 stsp *have_match = 0;
2351 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2353 13add988 2019-10-15 stsp return err;
2355 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2357 13add988 2019-10-15 stsp goto done;
2359 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2360 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2361 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2362 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2363 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2364 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2365 13add988 2019-10-15 stsp *have_match = 1;
2367 13add988 2019-10-15 stsp free(id_str);
2368 13add988 2019-10-15 stsp free(logmsg);
2369 13add988 2019-10-15 stsp return err;
2372 13add988 2019-10-15 stsp static const struct got_error *
2373 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2375 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2378 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2379 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2380 1a76625f 2018-10-22 stsp * while updating the display.
2383 d9787ed8 2022-09-10 op struct got_object_id id;
2384 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2385 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2386 568eae95 2022-09-11 mark int limit_match = 0;
2387 1a76625f 2018-10-22 stsp int errcode;
2389 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2390 4e0d2870 2020-12-07 naddy NULL, NULL);
2394 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2397 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2398 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2399 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2403 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2404 1a76625f 2018-10-22 stsp if (errcode) {
2405 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2406 13add988 2019-10-15 stsp "pthread_mutex_lock");
2410 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2411 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2412 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2414 568eae95 2022-09-11 mark if (*a->limiting) {
2415 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2416 568eae95 2022-09-11 mark a->limit_regex);
2420 568eae95 2022-09-11 mark if (limit_match) {
2421 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2423 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2424 568eae95 2022-09-11 mark entry->commit, entry->id);
2425 568eae95 2022-09-11 mark if (matched == NULL) {
2426 568eae95 2022-09-11 mark err = got_error_from_errno(
2427 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2430 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2431 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2433 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2434 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2435 568eae95 2022-09-11 mark matched, entry);
2436 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2440 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2441 568eae95 2022-09-11 mark * have found a match, and that it should be
2442 568eae95 2022-09-11 mark * counted as a new entry for the view.
2444 568eae95 2022-09-11 mark a->limit_match = limit_match;
2447 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2448 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2449 7c1452c1 2020-03-26 stsp int have_match;
2450 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2454 568eae95 2022-09-11 mark if (*a->limiting) {
2455 568eae95 2022-09-11 mark if (limit_match && have_match)
2456 568eae95 2022-09-11 mark *a->search_next_done =
2457 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2458 568eae95 2022-09-11 mark } else if (have_match)
2459 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2462 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2463 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2464 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2465 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2468 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2470 9ba79e04 2018-06-11 stsp return err;
2473 2b779855 2020-12-05 naddy static void
2474 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2476 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2477 2b779855 2020-12-05 naddy int ncommits = 0;
2479 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2480 2b779855 2020-12-05 naddy while (entry) {
2481 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2482 2b779855 2020-12-05 naddy s->selected_entry = entry;
2485 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2486 2b779855 2020-12-05 naddy ncommits++;
2490 0553a4e3 2018-04-30 stsp static const struct got_error *
2491 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2493 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2494 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2495 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2496 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2497 60493ae3 2019-06-20 stsp int width;
2498 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2499 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2500 8b473291 2019-02-21 stsp char *refs_str = NULL;
2501 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2502 11b20872 2019-11-08 stsp struct tog_color *tc;
2503 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2505 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2506 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2508 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2509 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2510 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2511 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2513 ecb28ae0 2018-07-16 stsp return err;
2514 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2515 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2516 d2075bf3 2020-12-25 stsp if (refs) {
2517 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2518 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2520 d2075bf3 2020-12-25 stsp goto done;
2524 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2525 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2527 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2528 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2529 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2530 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2531 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2532 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2533 8f4ed634 2020-03-26 stsp goto done;
2536 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2537 568eae95 2022-09-11 mark const char *limit_str = NULL;
2539 f9686aa5 2020-03-27 stsp if (view->searching) {
2540 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2541 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2542 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2543 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2544 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2545 f9686aa5 2020-03-27 stsp search_str = "searching...";
2548 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2549 568eae95 2022-09-11 mark limit_str = "no matches found";
2551 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2552 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2553 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2554 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2555 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2556 8f4ed634 2020-03-26 stsp goto done;
2560 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2561 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2562 87411fa9 2022-06-16 stsp "........................................",
2563 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2564 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2565 1a76625f 2018-10-22 stsp header = NULL;
2566 1a76625f 2018-10-22 stsp goto done;
2568 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2569 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2570 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2571 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2572 1a76625f 2018-10-22 stsp header = NULL;
2573 1a76625f 2018-10-22 stsp goto done;
2575 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2577 1a76625f 2018-10-22 stsp goto done;
2579 2814baeb 2018-08-01 stsp werase(view->window);
2581 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2582 a3404814 2018-09-02 stsp wstandout(view->window);
2583 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2585 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2586 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2587 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2588 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2592 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2593 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2594 a3404814 2018-09-02 stsp wstandend(view->window);
2595 ecb28ae0 2018-07-16 stsp free(wline);
2596 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2597 1a76625f 2018-10-22 stsp goto done;
2599 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2600 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2601 5813d178 2019-03-09 stsp ncommits = 0;
2602 145b6838 2022-06-16 stsp view->maxx = 0;
2603 5813d178 2019-03-09 stsp while (entry) {
2604 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2605 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2606 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2607 5813d178 2019-03-09 stsp int width;
2608 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2610 10aab77f 2022-07-19 op if (s->use_committer)
2611 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2613 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2614 5813d178 2019-03-09 stsp if (author == NULL) {
2615 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2616 5813d178 2019-03-09 stsp goto done;
2618 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2619 27a741e5 2019-09-11 stsp date_display_cols);
2620 5813d178 2019-03-09 stsp if (author_cols < width)
2621 5813d178 2019-03-09 stsp author_cols = width;
2622 5813d178 2019-03-09 stsp free(wauthor);
2623 5813d178 2019-03-09 stsp free(author);
2624 a310d9c3 2022-07-21 florian if (err)
2625 a310d9c3 2022-07-21 florian goto done;
2626 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2628 145b6838 2022-06-16 stsp goto done;
2629 145b6838 2022-06-16 stsp msg = msg0;
2630 145b6838 2022-06-16 stsp while (*msg == '\n')
2632 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2633 29688b02 2022-06-16 stsp *eol = '\0';
2634 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2635 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2637 29688b02 2022-06-16 stsp goto done;
2638 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2639 145b6838 2022-06-16 stsp free(msg0);
2640 29688b02 2022-06-16 stsp free(wmsg);
2641 7ca04879 2019-10-19 stsp ncommits++;
2642 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2645 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2646 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2647 867c6645 2018-07-10 stsp ncommits = 0;
2648 899d86c2 2018-05-10 stsp while (entry) {
2649 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2651 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2652 2814baeb 2018-08-01 stsp wstandout(view->window);
2653 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2654 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2655 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2656 2814baeb 2018-08-01 stsp wstandend(view->window);
2658 60493ae3 2019-06-20 stsp goto done;
2659 0553a4e3 2018-04-30 stsp ncommits++;
2660 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2661 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2664 9b058f45 2022-06-30 mark view_border(view);
2666 1a76625f 2018-10-22 stsp free(id_str);
2667 8b473291 2019-02-21 stsp free(refs_str);
2668 1a76625f 2018-10-22 stsp free(ncommits_str);
2669 1a76625f 2018-10-22 stsp free(header);
2670 80ddbec8 2018-04-29 stsp return err;
2673 07b55e75 2018-05-10 stsp static void
2674 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2676 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2677 07b55e75 2018-05-10 stsp int nscrolled = 0;
2679 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2680 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2683 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2684 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2685 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2686 07b55e75 2018-05-10 stsp if (entry) {
2687 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2688 07b55e75 2018-05-10 stsp nscrolled++;
2693 aa075928 2018-05-10 stsp static const struct got_error *
2694 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2696 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2697 5e224a3e 2019-02-22 stsp int errcode;
2699 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2701 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2702 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2703 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2704 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2705 7aafa0d1 2019-02-22 stsp if (errcode)
2706 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2707 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2710 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2711 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2713 7c1452c1 2020-03-26 stsp if (!wait)
2716 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2717 ffe38506 2020-12-01 naddy show_log_view(view);
2718 7c1452c1 2020-03-26 stsp update_panels();
2719 7c1452c1 2020-03-26 stsp doupdate();
2721 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2722 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2723 82954512 2020-02-03 stsp if (errcode)
2724 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2725 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2727 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2728 ffe38506 2020-12-01 naddy show_log_view(view);
2729 7c1452c1 2020-03-26 stsp update_panels();
2730 7c1452c1 2020-03-26 stsp doupdate();
2733 5e224a3e 2019-02-22 stsp return NULL;
2736 5e224a3e 2019-02-22 stsp static const struct got_error *
2737 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2739 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2740 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2742 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2743 2525dccb 2022-07-13 mark return NULL;
2745 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2746 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2747 9b058f45 2022-06-30 mark view->nscrolled = 0;
2749 9b058f45 2022-06-30 mark return err;
2752 9b058f45 2022-06-30 mark static const struct got_error *
2753 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2755 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2756 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2757 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2758 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2760 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2761 5e224a3e 2019-02-22 stsp return NULL;
2763 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2764 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2765 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2767 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2769 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2770 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2771 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2773 5e224a3e 2019-02-22 stsp return err;
2777 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2778 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2781 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2782 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
2784 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2785 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2787 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2788 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2790 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2791 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2793 9b058f45 2022-06-30 mark view->nscrolled = 0;
2795 dd0a52c1 2018-05-20 stsp return err;
2798 cd0acaa7 2018-05-20 stsp static const struct got_error *
2799 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2800 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2801 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2803 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2804 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2805 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2807 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2808 15a94983 2018-12-23 stsp if (diff_view == NULL)
2809 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2811 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2812 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2813 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2814 e5a0f69f 2018-08-18 stsp if (err == NULL)
2815 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2816 cd0acaa7 2018-05-20 stsp return err;
2819 80ddbec8 2018-04-29 stsp static const struct got_error *
2820 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2821 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2823 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2825 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2826 941e9f74 2019-05-21 stsp if (parent == NULL)
2827 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2829 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2830 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2831 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2832 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2833 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2834 941e9f74 2019-05-21 stsp s->tree = subtree;
2835 941e9f74 2019-05-21 stsp s->selected = 0;
2836 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2837 941e9f74 2019-05-21 stsp return NULL;
2840 941e9f74 2019-05-21 stsp static const struct got_error *
2841 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2842 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2844 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2845 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2846 941e9f74 2019-05-21 stsp const char *p;
2847 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2849 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2851 941e9f74 2019-05-21 stsp while (*p) {
2852 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2853 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2854 56e0773d 2019-11-28 stsp char *te_name;
2856 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2859 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2860 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2861 941e9f74 2019-05-21 stsp if (slash == NULL)
2862 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2864 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2865 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2866 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2869 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2870 56e0773d 2019-11-28 stsp if (te == NULL) {
2871 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2872 56e0773d 2019-11-28 stsp free(te_name);
2875 56e0773d 2019-11-28 stsp free(te_name);
2876 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2878 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2879 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2881 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2882 941e9f74 2019-05-21 stsp if (slash)
2883 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2885 941e9f74 2019-05-21 stsp subpath = strdup(path);
2886 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2887 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2891 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2896 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2897 941e9f74 2019-05-21 stsp free(tree_id);
2901 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2902 941e9f74 2019-05-21 stsp if (err) {
2903 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2906 941e9f74 2019-05-21 stsp if (slash == NULL)
2908 941e9f74 2019-05-21 stsp free(subpath);
2909 941e9f74 2019-05-21 stsp subpath = NULL;
2910 941e9f74 2019-05-21 stsp p = slash;
2913 941e9f74 2019-05-21 stsp free(subpath);
2914 1a76625f 2018-10-22 stsp return err;
2917 61266923 2020-01-14 stsp static const struct got_error *
2918 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2919 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2920 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2922 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2923 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2924 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2926 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2927 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2928 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2930 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2932 55cccc34 2020-02-20 stsp return err;
2933 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2935 55cccc34 2020-02-20 stsp *new_view = tree_view;
2937 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2938 55cccc34 2020-02-20 stsp return NULL;
2940 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2943 55cccc34 2020-02-20 stsp static const struct got_error *
2944 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2946 61266923 2020-01-14 stsp sigset_t sigset;
2947 61266923 2020-01-14 stsp int errcode;
2949 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2950 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2952 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2953 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2954 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2955 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2956 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2957 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2958 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2959 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2960 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2962 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2963 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2964 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2966 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2967 61266923 2020-01-14 stsp if (errcode)
2968 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2970 61266923 2020-01-14 stsp return NULL;
2973 1a76625f 2018-10-22 stsp static void *
2974 1a76625f 2018-10-22 stsp log_thread(void *arg)
2976 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2977 1a76625f 2018-10-22 stsp int errcode = 0;
2978 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2979 1a76625f 2018-10-22 stsp int done = 0;
2982 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2983 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2985 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2986 5629093a 2022-07-11 stsp if (errcode) {
2987 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2988 61266923 2020-01-14 stsp return (void *)err;
2991 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2992 5629093a 2022-07-11 stsp if (err) {
2993 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2994 5629093a 2022-07-11 stsp goto done;
2997 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2998 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2999 5629093a 2022-07-11 stsp if (errcode) {
3000 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
3001 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
3002 5629093a 2022-07-11 stsp goto done;
3004 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3005 1a76625f 2018-10-22 stsp if (err) {
3006 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3007 5629093a 2022-07-11 stsp goto done;
3008 1a76625f 2018-10-22 stsp err = NULL;
3010 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
3011 568eae95 2022-09-11 mark if (*a->limiting) {
3012 568eae95 2022-09-11 mark if (a->limit_match)
3013 568eae95 2022-09-11 mark a->commits_needed--;
3015 568eae95 2022-09-11 mark a->commits_needed--;
3018 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3019 3abe8080 2019-04-10 stsp if (errcode) {
3020 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3021 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3022 5629093a 2022-07-11 stsp goto done;
3023 3abe8080 2019-04-10 stsp } else if (*a->quit)
3025 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
3026 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3027 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
3028 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3029 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
3030 568eae95 2022-09-11 mark *a->first_displayed_entry =
3031 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
3032 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
3035 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3036 7c1452c1 2020-03-26 stsp if (errcode) {
3037 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3038 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3039 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3040 5629093a 2022-07-11 stsp goto done;
3044 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3046 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3047 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3048 7c1452c1 2020-03-26 stsp &tog_mutex);
3049 5629093a 2022-07-11 stsp if (errcode) {
3050 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3051 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3052 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3053 5629093a 2022-07-11 stsp goto done;
3055 21355643 2020-12-06 stsp if (*a->quit)
3060 3abe8080 2019-04-10 stsp a->log_complete = 1;
3061 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3062 5629093a 2022-07-11 stsp if (errcode)
3063 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3065 5629093a 2022-07-11 stsp if (err) {
3066 5629093a 2022-07-11 stsp tog_thread_error = 1;
3067 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3069 1a76625f 2018-10-22 stsp return (void *)err;
3072 1a76625f 2018-10-22 stsp static const struct got_error *
3073 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3075 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3076 1a76625f 2018-10-22 stsp int errcode;
3078 1a76625f 2018-10-22 stsp if (s->thread) {
3079 1a76625f 2018-10-22 stsp s->quit = 1;
3080 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3081 1a76625f 2018-10-22 stsp if (errcode)
3082 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3083 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3084 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3085 1a76625f 2018-10-22 stsp if (errcode)
3086 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3087 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3088 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3089 1a76625f 2018-10-22 stsp if (errcode)
3090 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3091 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3092 1a76625f 2018-10-22 stsp if (errcode)
3093 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3094 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3095 1a76625f 2018-10-22 stsp s->thread = NULL;
3098 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3099 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3100 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3103 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3104 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3105 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3106 74467cc8 2022-06-15 stsp if (err == NULL)
3107 74467cc8 2022-06-15 stsp err = pack_err;
3108 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3111 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3112 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3113 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3116 5629093a 2022-07-11 stsp return err ? err : thread_err;
3119 9343a5fb 2018-06-23 stsp static const struct got_error *
3120 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3122 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3123 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3124 276b94a1 2020-11-13 naddy int errcode;
3126 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3128 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3129 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3130 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3132 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3133 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3134 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3136 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3137 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3138 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3139 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3140 1a76625f 2018-10-22 stsp free(s->start_id);
3141 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3142 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3143 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3144 1a76625f 2018-10-22 stsp return err;
3148 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3149 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3150 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3151 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3152 568eae95 2022-09-11 mark * works with very slight change.
3154 1a76625f 2018-10-22 stsp static const struct got_error *
3155 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3157 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3158 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3159 568eae95 2022-09-11 mark struct tog_view *v = view;
3160 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3161 568eae95 2022-09-11 mark char pattern[1024];
3164 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3165 568eae95 2022-09-11 mark v = view->child;
3166 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3167 568eae95 2022-09-11 mark v = view->parent;
3169 568eae95 2022-09-11 mark /* Get the pattern */
3170 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3171 568eae95 2022-09-11 mark wclrtoeol(v->window);
3172 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3173 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3174 568eae95 2022-09-11 mark nocbreak();
3176 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3179 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3180 568eae95 2022-09-11 mark if (ret == ERR)
3181 568eae95 2022-09-11 mark return NULL;
3183 568eae95 2022-09-11 mark if (*pattern == '\0') {
3185 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3186 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3188 568eae95 2022-09-11 mark if (!s->limit_view)
3189 568eae95 2022-09-11 mark return NULL;
3192 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3193 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3194 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3195 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3196 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3197 568eae95 2022-09-11 mark * the queue.
3199 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3200 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3201 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3202 568eae95 2022-09-11 mark s->selected = 0;
3203 568eae95 2022-09-11 mark s->limit_view = 0;
3205 568eae95 2022-09-11 mark return NULL;
3208 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3209 568eae95 2022-09-11 mark return NULL;
3211 568eae95 2022-09-11 mark s->limit_view = 1;
3213 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3214 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3215 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3216 568eae95 2022-09-11 mark s->selected_entry = NULL;
3217 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3219 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3220 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3221 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3223 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3224 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3225 568eae95 2022-09-11 mark int have_match = 0;
3227 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3228 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3230 568eae95 2022-09-11 mark return err;
3232 568eae95 2022-09-11 mark if (have_match) {
3233 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3235 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3236 568eae95 2022-09-11 mark entry->id);
3237 568eae95 2022-09-11 mark if (matched == NULL) {
3238 568eae95 2022-09-11 mark err = got_error_from_errno(
3239 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3242 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3243 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3245 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3246 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3247 568eae95 2022-09-11 mark matched, entry);
3248 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3252 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3253 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3254 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3255 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3256 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3257 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3259 568eae95 2022-09-11 mark return err;
3262 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3263 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3264 568eae95 2022-09-11 mark s->selected = 0;
3266 568eae95 2022-09-11 mark return NULL;
3269 568eae95 2022-09-11 mark static const struct got_error *
3270 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3272 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3274 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3275 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3276 60493ae3 2019-06-20 stsp return NULL;
3279 60493ae3 2019-06-20 stsp static const struct got_error *
3280 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3282 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3283 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3284 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3286 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3287 f9686aa5 2020-03-27 stsp show_log_view(view);
3288 f9686aa5 2020-03-27 stsp update_panels();
3289 f9686aa5 2020-03-27 stsp doupdate();
3291 96e2b566 2019-07-08 stsp if (s->search_entry) {
3292 13add988 2019-10-15 stsp int errcode, ch;
3293 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3294 13add988 2019-10-15 stsp if (errcode)
3295 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3296 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3297 13add988 2019-10-15 stsp ch = wgetch(view->window);
3298 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3299 13add988 2019-10-15 stsp if (errcode)
3300 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3301 13add988 2019-10-15 stsp "pthread_mutex_lock");
3302 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3303 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3304 678cbce5 2019-07-28 stsp return NULL;
3306 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3307 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3309 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3310 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3311 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3313 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3314 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3315 f704b35c 2022-06-18 stsp * might have changed.
3317 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3318 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3320 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3323 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3326 60493ae3 2019-06-20 stsp while (1) {
3327 13add988 2019-10-15 stsp int have_match = 0;
3329 60493ae3 2019-06-20 stsp if (entry == NULL) {
3330 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3331 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3332 f9967bca 2020-03-27 stsp view->search_next_done =
3333 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3334 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3335 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3336 f801134a 2019-06-25 stsp return NULL;
3339 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3340 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3341 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3343 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3344 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3347 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3348 13add988 2019-10-15 stsp &view->regex);
3351 13add988 2019-10-15 stsp if (have_match) {
3352 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3353 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3357 96e2b566 2019-07-08 stsp s->search_entry = entry;
3358 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3359 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3361 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3364 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3365 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3366 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3367 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3369 60493ae3 2019-06-20 stsp return err;
3372 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3373 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3375 60493ae3 2019-06-20 stsp return err;
3380 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3382 60493ae3 2019-06-20 stsp return NULL;
3385 60493ae3 2019-06-20 stsp static const struct got_error *
3386 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3387 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3388 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3390 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3391 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3392 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3393 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3394 1a76625f 2018-10-22 stsp int errcode;
3396 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3397 f135c941 2020-02-20 stsp free(s->in_repo_path);
3398 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3399 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3400 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3403 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3404 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3405 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3406 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3408 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3409 568eae95 2022-09-11 mark s->limit_view = 0;
3410 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3412 fb2756b9 2018-08-04 stsp s->repo = repo;
3413 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3414 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3415 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3416 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3417 9cd7cbd1 2020-12-07 stsp goto done;
3420 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3421 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3422 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3423 5036bf37 2018-09-24 stsp goto done;
3425 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3426 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3428 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3429 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3430 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3431 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3433 11b20872 2019-11-08 stsp goto done;
3434 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3435 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3436 11b20872 2019-11-08 stsp if (err) {
3437 11b20872 2019-11-08 stsp free_colors(&s->colors);
3438 11b20872 2019-11-08 stsp goto done;
3440 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3441 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3442 11b20872 2019-11-08 stsp if (err) {
3443 11b20872 2019-11-08 stsp free_colors(&s->colors);
3444 11b20872 2019-11-08 stsp goto done;
3448 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3449 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3450 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3451 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3452 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3453 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3455 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3456 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3458 74467cc8 2022-06-15 stsp goto done;
3460 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3461 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3462 0ae84acc 2022-06-15 tracey if (err)
3463 0ae84acc 2022-06-15 tracey goto done;
3464 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3465 b672a97a 2020-01-27 stsp !s->log_branches);
3467 1a76625f 2018-10-22 stsp goto done;
3468 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3469 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3471 c5b78334 2020-01-12 stsp goto done;
3473 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3474 1a76625f 2018-10-22 stsp if (errcode) {
3475 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3476 1a76625f 2018-10-22 stsp goto done;
3478 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3479 7c1452c1 2020-03-26 stsp if (errcode) {
3480 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3481 7c1452c1 2020-03-26 stsp goto done;
3484 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3485 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3486 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3487 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3488 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3489 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3490 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3491 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3492 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3493 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3494 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3495 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3496 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3497 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3498 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3499 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3500 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3503 1a76625f 2018-10-22 stsp close_log_view(view);
3504 ba4f502b 2018-08-04 stsp return err;
3507 e5a0f69f 2018-08-18 stsp static const struct got_error *
3508 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3510 f2f6d207 2020-11-24 stsp const struct got_error *err;
3511 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3513 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3514 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3515 2b380cc8 2018-10-24 stsp &s->thread_args);
3516 2b380cc8 2018-10-24 stsp if (errcode)
3517 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3518 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3519 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3521 f2f6d207 2020-11-24 stsp return err;
3525 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3528 b880cc75 2022-06-30 mark static void
3529 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3531 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3533 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3535 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3536 568eae95 2022-09-11 mark view->count = 0;
3538 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3540 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3542 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3543 b880cc75 2022-06-30 mark --s->selected;
3545 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3547 b880cc75 2022-06-30 mark select_commit(s);
3551 b880cc75 2022-06-30 mark static const struct got_error *
3552 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3554 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3555 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3556 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3558 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3559 568eae95 2022-09-11 mark return NULL;
3561 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3562 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3563 b880cc75 2022-06-30 mark return NULL;
3565 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3566 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3568 631e7531 2022-08-12 mark if (!page) {
3569 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3570 b880cc75 2022-06-30 mark ++s->selected;
3572 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3573 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3574 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3577 631e7531 2022-08-12 mark s->selected = 0;
3578 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3579 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3580 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3581 631e7531 2022-08-12 mark if (entry == NULL)
3583 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3584 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3586 631e7531 2022-08-12 mark if (n > 0)
3587 631e7531 2022-08-12 mark s->selected = n - 1;
3589 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3590 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3591 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3592 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3594 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3597 b880cc75 2022-06-30 mark return err;
3600 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3601 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3603 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3604 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3605 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3606 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3609 b880cc75 2022-06-30 mark select_commit(s);
3611 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3612 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3613 b880cc75 2022-06-30 mark view->count = 0;
3615 b880cc75 2022-06-30 mark return NULL;
3618 9b058f45 2022-06-30 mark static void
3619 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3624 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3625 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3626 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3627 7532ccda 2022-07-11 mark else if (view->resized_y)
3628 7532ccda 2022-07-11 mark *y = view->resized_y;
3630 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3631 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3632 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3633 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3634 7532ccda 2022-07-11 mark else if (view->resized_x)
3635 7532ccda 2022-07-11 mark *x = view->resized_x;
3637 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3641 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3642 e5a0f69f 2018-08-18 stsp static const struct got_error *
3643 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3645 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3647 9b058f45 2022-06-30 mark view->nlines = y;
3648 d2366e29 2022-07-07 mark view->ncols = COLS;
3649 9b058f45 2022-06-30 mark err = view_resize(view);
3651 9b058f45 2022-06-30 mark return err;
3653 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3655 9b058f45 2022-06-30 mark return err;
3658 94b80cfa 2022-08-01 mark static const struct got_error *
3659 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3661 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3662 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3663 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3665 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3666 374f69dd 2022-08-13 stsp return NULL;
3668 94b80cfa 2022-08-01 mark g = view->gline;
3669 94b80cfa 2022-08-01 mark view->gline = 0;
3671 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3672 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3673 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3674 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3675 94b80cfa 2022-08-01 mark select_commit(s);
3676 94b80cfa 2022-08-01 mark return NULL;
3679 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3680 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3681 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3682 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3683 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3685 94b80cfa 2022-08-01 mark return err;
3686 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3687 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3689 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3690 94b80cfa 2022-08-01 mark s->selected = g - 1;
3692 94b80cfa 2022-08-01 mark select_commit(s);
3693 94b80cfa 2022-08-01 mark return NULL;
3697 d627976f 2023-02-01 mark static void
3698 d627976f 2023-02-01 mark horizontal_scroll_input(struct tog_view *view, int ch)
3701 d627976f 2023-02-01 mark switch (ch) {
3702 d627976f 2023-02-01 mark case KEY_LEFT:
3704 d627976f 2023-02-01 mark view->x -= MIN(view->x, 2);
3705 d627976f 2023-02-01 mark if (view->x <= 0)
3706 d627976f 2023-02-01 mark view->count = 0;
3708 d627976f 2023-02-01 mark case KEY_RIGHT:
3710 d627976f 2023-02-01 mark if (view->x + view->ncols / 2 < view->maxx)
3711 d627976f 2023-02-01 mark view->x += 2;
3713 d627976f 2023-02-01 mark view->count = 0;
3716 d627976f 2023-02-01 mark view->x = 0;
3719 d627976f 2023-02-01 mark view->x = MAX(view->maxx - view->ncols / 2, 0);
3720 d627976f 2023-02-01 mark view->count = 0;
3727 9b058f45 2022-06-30 mark static const struct got_error *
3728 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3730 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3731 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3732 631e7531 2022-08-12 mark int eos, nscroll;
3734 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3735 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3736 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3737 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3738 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3739 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3742 11edf34c 2022-08-12 mark return err;
3745 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3746 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3747 0dca135e 2022-06-30 mark --eos; /* border */
3749 94b80cfa 2022-08-01 mark if (view->gline)
3750 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3752 528dedf3 2021-08-30 stsp switch (ch) {
3754 568eae95 2022-09-11 mark err = limit_log_view(view);
3757 1e37a5c2 2019-05-12 jcs s->quit = 1;
3761 145b6838 2022-06-16 stsp case KEY_RIGHT:
3763 145b6838 2022-06-16 stsp case KEY_LEFT:
3765 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
3768 1e37a5c2 2019-05-12 jcs case KEY_UP:
3771 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3772 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3776 912a3f79 2021-08-30 j case KEY_HOME:
3777 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3778 640cd7ff 2022-06-22 mark view->count = 0;
3780 83cc4199 2022-06-13 stsp case CTRL('u'):
3782 83cc4199 2022-06-13 stsp nscroll /= 2;
3783 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3784 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3785 a4292ac5 2019-05-12 jcs case CTRL('b'):
3787 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3790 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3793 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3794 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3797 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3798 3f6c6614 2023-01-23 mark view->action = s->use_committer ?
3799 3f6c6614 2023-01-23 mark "show committer" : "show commit author";
3803 912a3f79 2021-08-30 j case KEY_END: {
3804 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3805 912a3f79 2021-08-30 j * traverse them all. */
3806 640cd7ff 2022-06-22 mark view->count = 0;
3807 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3808 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3809 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3810 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3811 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3814 80b7e8da 2022-06-11 stsp case CTRL('d'):
3816 83cc4199 2022-06-13 stsp nscroll /= 2;
3817 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3818 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3819 61417565 2022-06-20 mark case CTRL('f'):
3820 48bb96f0 2022-06-20 naddy case 'f':
3822 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3824 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3825 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3826 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3827 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3828 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3829 2b779855 2020-12-05 naddy select_commit(s);
3830 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3831 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3832 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3833 568eae95 2022-09-11 mark s->commits->ncommits;
3834 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3837 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3838 49b24ee5 2022-07-03 mark case '\r':
3839 640cd7ff 2022-06-22 mark view->count = 0;
3840 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3842 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3845 640cd7ff 2022-06-22 mark view->count = 0;
3846 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3848 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3850 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3851 21355643 2020-12-06 stsp case CTRL('l'):
3853 640cd7ff 2022-06-22 mark view->count = 0;
3854 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3855 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3857 21355643 2020-12-06 stsp err = stop_log_thread(s);
3859 74cfe85e 2020-10-20 stsp return err;
3860 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3861 21355643 2020-12-06 stsp char *parent_path;
3862 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3864 21355643 2020-12-06 stsp return err;
3865 21355643 2020-12-06 stsp free(s->in_repo_path);
3866 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3867 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3868 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3869 21355643 2020-12-06 stsp struct got_object_id *start_id;
3870 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3871 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3872 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3873 ea371198 2022-11-17 stsp if (err) {
3874 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
3875 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
3876 ea371198 2022-11-17 stsp return err;
3877 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
3878 ea371198 2022-11-17 stsp free(s->head_ref_name);
3879 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
3880 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
3881 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3882 ea371198 2022-11-17 stsp &tog_refs, s->repo);
3884 ea371198 2022-11-17 stsp return err;
3886 21355643 2020-12-06 stsp free(s->start_id);
3887 21355643 2020-12-06 stsp s->start_id = start_id;
3888 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3889 21355643 2020-12-06 stsp } else /* 'B' */
3890 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3892 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3893 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3895 b0dd8d27 2022-06-16 stsp return err;
3897 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3898 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3899 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3901 21355643 2020-12-06 stsp return err;
3902 51a10b52 2020-12-26 stsp tog_free_refs();
3903 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3905 ca51c541 2020-12-07 stsp return err;
3906 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3907 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3909 d01904d4 2019-06-25 stsp return err;
3910 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3911 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3913 b672a97a 2020-01-27 stsp return err;
3914 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3915 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3916 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3917 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3918 21355643 2020-12-06 stsp s->selected_entry = NULL;
3919 21355643 2020-12-06 stsp s->selected = 0;
3920 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3921 21355643 2020-12-06 stsp s->quit = 0;
3922 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3923 dfee752e 2022-06-18 op s->matched_entry = NULL;
3924 dfee752e 2022-06-18 op s->search_entry = NULL;
3925 01a7bcaf 2022-07-22 mark view->offset = 0;
3928 640cd7ff 2022-06-22 mark view->count = 0;
3929 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3932 640cd7ff 2022-06-22 mark view->count = 0;
3936 80ddbec8 2018-04-29 stsp return err;
3939 4ed7e80c 2018-05-20 stsp static const struct got_error *
3940 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3942 c2db6724 2019-01-04 stsp const struct got_error *error;
3944 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3945 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3946 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3948 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3949 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3951 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3952 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3954 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3955 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3957 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3958 c2db6724 2019-01-04 stsp if (error != NULL)
3959 c2db6724 2019-01-04 stsp return error;
3961 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3962 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3964 c2db6724 2019-01-04 stsp return NULL;
3967 a915003a 2019-02-05 stsp static void
3968 a915003a 2019-02-05 stsp init_curses(void)
3971 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3972 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3973 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3975 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3976 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3977 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3978 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3979 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3981 a915003a 2019-02-05 stsp initscr();
3983 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3986 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3987 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3988 a915003a 2019-02-05 stsp curs_set(0);
3989 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3990 6d17833f 2019-11-08 stsp start_color();
3991 6d17833f 2019-11-08 stsp use_default_colors();
3995 c2db6724 2019-01-04 stsp static const struct got_error *
3996 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3997 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3999 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4001 f135c941 2020-02-20 stsp if (argc == 0) {
4002 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4003 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4004 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4005 f135c941 2020-02-20 stsp return NULL;
4008 f135c941 2020-02-20 stsp if (worktree) {
4009 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4012 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4014 f135c941 2020-02-20 stsp return err;
4015 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4016 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4017 bfd61697 2020-11-14 stsp p) == -1) {
4018 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4019 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4023 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4025 f135c941 2020-02-20 stsp return err;
4028 f135c941 2020-02-20 stsp static const struct got_error *
4029 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4031 80ddbec8 2018-04-29 stsp const struct got_error *error;
4032 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4033 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4034 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4035 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4036 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4037 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4038 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4039 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4040 04cc582a 2018-08-01 stsp struct tog_view *view;
4041 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4043 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4044 80ddbec8 2018-04-29 stsp switch (ch) {
4046 b672a97a 2020-01-27 stsp log_branches = 1;
4049 80ddbec8 2018-04-29 stsp start_commit = optarg;
4052 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4053 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4054 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4058 17020d27 2019-03-07 stsp usage_log();
4059 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4063 80ddbec8 2018-04-29 stsp argc -= optind;
4064 80ddbec8 2018-04-29 stsp argv += optind;
4066 f135c941 2020-02-20 stsp if (argc > 1)
4067 f135c941 2020-02-20 stsp usage_log();
4069 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4070 0ae84acc 2022-06-15 tracey if (error != NULL)
4071 0ae84acc 2022-06-15 tracey goto done;
4073 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4074 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4075 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4076 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4077 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4078 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4079 c156c7a4 2020-12-18 stsp goto done;
4080 a1fbf39a 2019-08-11 stsp if (worktree)
4081 6962eb72 2020-02-20 stsp repo_path =
4082 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4084 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4085 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4086 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4087 c156c7a4 2020-12-18 stsp goto done;
4091 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4092 80ddbec8 2018-04-29 stsp if (error != NULL)
4093 ecb28ae0 2018-07-16 stsp goto done;
4095 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4096 f135c941 2020-02-20 stsp repo, worktree);
4097 f135c941 2020-02-20 stsp if (error)
4098 f135c941 2020-02-20 stsp goto done;
4100 f135c941 2020-02-20 stsp init_curses();
4102 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4103 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4104 c02c541e 2019-03-29 stsp if (error)
4105 c02c541e 2019-03-29 stsp goto done;
4107 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4108 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4109 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4110 87670572 2020-12-26 naddy if (error)
4111 87670572 2020-12-26 naddy goto done;
4114 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4115 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4116 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4117 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4118 d8f38dc4 2020-12-05 stsp if (error)
4119 d8f38dc4 2020-12-05 stsp goto done;
4120 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4122 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4123 d8f38dc4 2020-12-05 stsp if (error == NULL)
4124 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4125 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4126 d8f38dc4 2020-12-05 stsp goto done;
4127 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4128 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4129 d8f38dc4 2020-12-05 stsp if (error)
4130 d8f38dc4 2020-12-05 stsp goto done;
4133 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4134 04cc582a 2018-08-01 stsp if (view == NULL) {
4135 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4136 04cc582a 2018-08-01 stsp goto done;
4138 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4139 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4140 ba4f502b 2018-08-04 stsp if (error)
4141 ba4f502b 2018-08-04 stsp goto done;
4142 2fc00ff4 2019-08-31 stsp if (worktree) {
4143 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4144 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4145 2fc00ff4 2019-08-31 stsp worktree = NULL;
4147 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4149 f135c941 2020-02-20 stsp free(in_repo_path);
4150 ecb28ae0 2018-07-16 stsp free(repo_path);
4151 ecb28ae0 2018-07-16 stsp free(cwd);
4152 899d86c2 2018-05-10 stsp free(start_id);
4153 d8f38dc4 2020-12-05 stsp free(label);
4155 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4156 1d0f4054 2021-06-17 stsp if (repo) {
4157 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4158 1d0f4054 2021-06-17 stsp if (error == NULL)
4159 1d0f4054 2021-06-17 stsp error = close_err;
4161 ec142235 2019-03-07 stsp if (worktree)
4162 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4163 0ae84acc 2022-06-15 tracey if (pack_fds) {
4164 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4165 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4166 0ae84acc 2022-06-15 tracey if (error == NULL)
4167 0ae84acc 2022-06-15 tracey error = pack_err;
4169 51a10b52 2020-12-26 stsp tog_free_refs();
4170 80ddbec8 2018-04-29 stsp return error;
4173 4ed7e80c 2018-05-20 stsp __dead static void
4174 9f7d7167 2018-04-29 stsp usage_diff(void)
4177 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4178 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4182 6d17833f 2019-11-08 stsp static int
4183 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4184 41605754 2020-11-12 stsp regmatch_t *regmatch)
4186 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4189 336075a4 2022-06-25 op static struct tog_color *
4190 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4192 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4194 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4195 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4196 6d17833f 2019-11-08 stsp return tc;
4199 6d17833f 2019-11-08 stsp return NULL;
4202 4ed7e80c 2018-05-20 stsp static const struct got_error *
4203 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4204 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4206 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4207 44a87665 2022-06-16 stsp char *exstr = NULL;
4208 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4209 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4210 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4211 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4213 41605754 2020-11-12 stsp *wtotal = 0;
4215 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4216 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4218 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4220 44a87665 2022-06-16 stsp return err;
4222 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4223 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4224 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4225 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4226 44a87665 2022-06-16 stsp goto done;
4228 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4229 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4230 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4231 1853e0f4 2022-06-16 stsp goto done;
4233 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4234 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4235 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4236 1853e0f4 2022-06-16 stsp goto done;
4239 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4240 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4241 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4243 1853e0f4 2022-06-16 stsp goto done;
4244 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4246 1853e0f4 2022-06-16 stsp free(wline);
4247 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4248 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4250 1853e0f4 2022-06-16 stsp goto done;
4251 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4252 1853e0f4 2022-06-16 stsp wlimit -= width;
4253 1853e0f4 2022-06-16 stsp *wtotal += width;
4256 41605754 2020-11-12 stsp if (wlimit > 0) {
4257 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4258 1853e0f4 2022-06-16 stsp size_t wlen;
4260 1853e0f4 2022-06-16 stsp free(wline);
4261 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4262 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4264 1853e0f4 2022-06-16 stsp goto done;
4265 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4266 1853e0f4 2022-06-16 stsp while (i < wlen) {
4267 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4268 1853e0f4 2022-06-16 stsp if (width == -1) {
4269 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4270 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4271 1853e0f4 2022-06-16 stsp goto done;
4273 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4275 61417565 2022-06-20 mark w += width;
4278 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4279 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4280 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4281 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4282 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4283 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4284 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4288 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4289 1853e0f4 2022-06-16 stsp free(wline);
4290 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4291 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4292 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4293 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4295 1853e0f4 2022-06-16 stsp goto done;
4296 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4298 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4299 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4301 1853e0f4 2022-06-16 stsp goto done;
4302 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4304 1853e0f4 2022-06-16 stsp *wtotal += width2;
4307 145b6838 2022-06-16 stsp free(wline);
4308 44a87665 2022-06-16 stsp free(exstr);
4309 1853e0f4 2022-06-16 stsp free(seg0);
4310 1853e0f4 2022-06-16 stsp free(seg1);
4311 1853e0f4 2022-06-16 stsp free(seg2);
4312 1853e0f4 2022-06-16 stsp return err;
4315 94b80cfa 2022-08-01 mark static int
4316 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4318 94b80cfa 2022-08-01 mark FILE *f = NULL;
4319 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4321 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4322 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4324 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4325 ec2a9698 2022-09-15 mark selected = first;
4326 ec2a9698 2022-09-15 mark eof = &s->eof;
4328 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4329 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4331 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4332 94b80cfa 2022-08-01 mark selected = first;
4333 94b80cfa 2022-08-01 mark eof = &s->eof;
4335 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4336 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4338 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4339 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4340 94b80cfa 2022-08-01 mark eof = &s->eof;
4341 94b80cfa 2022-08-01 mark f = s->blame.f;
4345 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4346 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4348 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4349 94b80cfa 2022-08-01 mark rewind(f);
4351 94b80cfa 2022-08-01 mark *first = 1;
4352 94b80cfa 2022-08-01 mark *lineno = 0;
4353 94b80cfa 2022-08-01 mark *nprinted = 0;
4357 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4358 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4359 94b80cfa 2022-08-01 mark view->gline = 0;
4364 41605754 2020-11-12 stsp static const struct got_error *
4365 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4367 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4368 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4369 61e69b96 2018-05-20 stsp const struct got_error *err;
4370 c7d5c43c 2022-08-04 mark int nprinted = 0;
4371 b304db33 2018-05-20 stsp char *line;
4372 826082fe 2020-12-10 stsp size_t linesize = 0;
4373 826082fe 2020-12-10 stsp ssize_t linelen;
4374 61e69b96 2018-05-20 stsp wchar_t *wline;
4375 e0b650dd 2018-05-20 stsp int width;
4376 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4377 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4378 fe621944 2020-11-10 stsp off_t line_offset;
4380 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4381 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4382 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4383 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4385 f7d12f7e 2018-08-01 stsp werase(view->window);
4387 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4388 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4390 a3404814 2018-09-02 stsp if (header) {
4391 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4392 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4393 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4395 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4396 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4397 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4399 135a2da0 2020-11-11 stsp free(line);
4401 a3404814 2018-09-02 stsp return err;
4403 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4404 a3404814 2018-09-02 stsp wstandout(view->window);
4405 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4406 e54cc94a 2020-11-11 stsp free(wline);
4407 e54cc94a 2020-11-11 stsp wline = NULL;
4408 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4409 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4410 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4411 a3404814 2018-09-02 stsp wstandend(view->window);
4413 a3404814 2018-09-02 stsp if (max_lines <= 1)
4414 a3404814 2018-09-02 stsp return NULL;
4415 a3404814 2018-09-02 stsp max_lines--;
4418 89f1a395 2020-12-01 naddy s->eof = 0;
4419 145b6838 2022-06-16 stsp view->maxx = 0;
4420 826082fe 2020-12-10 stsp line = NULL;
4421 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4422 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4423 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4425 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4426 826082fe 2020-12-10 stsp if (linelen == -1) {
4427 826082fe 2020-12-10 stsp if (feof(s->f)) {
4428 826082fe 2020-12-10 stsp s->eof = 1;
4431 826082fe 2020-12-10 stsp free(line);
4432 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4435 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4437 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4439 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4440 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4442 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4443 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4444 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4445 1853e0f4 2022-06-16 stsp if (err) {
4446 1853e0f4 2022-06-16 stsp free(line);
4447 1853e0f4 2022-06-16 stsp return err;
4449 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4450 1853e0f4 2022-06-16 stsp free(wline);
4451 1853e0f4 2022-06-16 stsp wline = NULL;
4453 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4454 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4455 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4456 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4458 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4459 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4460 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4461 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4462 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4463 41605754 2020-11-12 stsp if (err) {
4464 41605754 2020-11-12 stsp free(line);
4465 41605754 2020-11-12 stsp return err;
4469 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4470 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4471 969c159c 2022-06-16 stsp if (err) {
4472 969c159c 2022-06-16 stsp free(line);
4473 969c159c 2022-06-16 stsp return err;
4475 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4476 969c159c 2022-06-16 stsp free(wline);
4477 41605754 2020-11-12 stsp wline = NULL;
4479 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4480 94b80cfa 2022-08-01 mark /* highlight full gline length */
4481 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4482 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4484 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4485 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4488 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4489 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4490 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4492 826082fe 2020-12-10 stsp free(line);
4493 fe621944 2020-11-10 stsp if (nprinted >= 1)
4494 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4495 89f1a395 2020-12-01 naddy (nprinted - 1);
4497 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4499 9b058f45 2022-06-30 mark view_border(view);
4501 89f1a395 2020-12-01 naddy if (s->eof) {
4502 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4503 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4504 c3e9aa98 2019-05-13 jcs nprinted++;
4507 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4508 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4510 c3e9aa98 2019-05-13 jcs return err;
4513 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4514 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4515 e54cc94a 2020-11-11 stsp free(wline);
4516 e54cc94a 2020-11-11 stsp wline = NULL;
4517 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4520 26ed57b2 2018-05-19 stsp return NULL;
4523 abd2672a 2018-12-23 stsp static char *
4524 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4526 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4527 09867e48 2019-08-13 stsp char *p, *s;
4529 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4530 09867e48 2019-08-13 stsp if (tm == NULL)
4531 09867e48 2019-08-13 stsp return NULL;
4532 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4533 09867e48 2019-08-13 stsp if (s == NULL)
4534 09867e48 2019-08-13 stsp return NULL;
4535 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4537 abd2672a 2018-12-23 stsp *p = '\0';
4541 4ed7e80c 2018-05-20 stsp static const struct got_error *
4542 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4543 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4545 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4547 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4548 fe621944 2020-11-10 stsp if (p == NULL)
4549 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4550 c7d5c43c 2022-08-04 mark *lines = p;
4551 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4552 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4553 fe621944 2020-11-10 stsp (*nlines)++;
4555 fe621944 2020-11-10 stsp return NULL;
4558 fe621944 2020-11-10 stsp static const struct got_error *
4559 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4560 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4562 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4563 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4564 1f3405c9 2023-01-17 mark size_t i, r;
4566 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4567 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4569 1f3405c9 2023-01-17 mark for (;;) {
4570 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4571 1f3405c9 2023-01-17 mark if (r == 0) {
4572 1f3405c9 2023-01-17 mark if (ferror(src))
4573 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4574 1f3405c9 2023-01-17 mark if (feof(src))
4577 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4578 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4582 1f3405c9 2023-01-17 mark * The diff driver initialises the first line at offset zero when the
4583 1f3405c9 2023-01-17 mark * array isn't prepopulated, skip it; we already have it in *d_lines.
4585 1f3405c9 2023-01-17 mark for (i = 1; i < s_nlines; ++i)
4586 1f3405c9 2023-01-17 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4588 1f3405c9 2023-01-17 mark --s_nlines;
4590 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4591 1f3405c9 2023-01-17 mark if (p == NULL) {
4592 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4593 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4596 1f3405c9 2023-01-17 mark *d_lines = p;
4598 1f3405c9 2023-01-17 mark memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4599 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4601 1f3405c9 2023-01-17 mark return NULL;
4604 1f3405c9 2023-01-17 mark static const struct got_error *
4605 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4606 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4607 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4608 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4610 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4611 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4612 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4613 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4614 45d799e2 2018-12-23 stsp time_t committer_time;
4615 45d799e2 2018-12-23 stsp const char *author, *committer;
4616 8b473291 2019-02-21 stsp char *refs_str = NULL;
4617 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4618 fe621944 2020-11-10 stsp off_t outoff = 0;
4621 8b473291 2019-02-21 stsp if (refs) {
4622 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4624 8b473291 2019-02-21 stsp return err;
4627 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4629 abd2672a 2018-12-23 stsp return err;
4631 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4632 15a94983 2018-12-23 stsp if (err) {
4633 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4634 15a94983 2018-12-23 stsp goto done;
4637 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4639 fe621944 2020-11-10 stsp goto done;
4641 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4642 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4643 fe621944 2020-11-10 stsp if (n < 0) {
4644 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4645 abd2672a 2018-12-23 stsp goto done;
4647 fe621944 2020-11-10 stsp outoff += n;
4648 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4650 fe621944 2020-11-10 stsp goto done;
4652 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4653 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4654 fe621944 2020-11-10 stsp if (n < 0) {
4655 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4656 abd2672a 2018-12-23 stsp goto done;
4658 fe621944 2020-11-10 stsp outoff += n;
4659 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4661 fe621944 2020-11-10 stsp goto done;
4663 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4664 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4665 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4666 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4667 fe621944 2020-11-10 stsp if (n < 0) {
4668 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4669 fe621944 2020-11-10 stsp goto done;
4671 fe621944 2020-11-10 stsp outoff += n;
4672 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4673 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4675 fe621944 2020-11-10 stsp goto done;
4677 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4678 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4679 1a99e0b4 2023-01-07 stsp if (datestr) {
4680 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4681 fe621944 2020-11-10 stsp if (n < 0) {
4682 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4683 fe621944 2020-11-10 stsp goto done;
4685 fe621944 2020-11-10 stsp outoff += n;
4686 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4687 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4689 fe621944 2020-11-10 stsp goto done;
4691 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4692 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4693 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4694 9f98ca05 2021-09-24 stsp int pn = 1;
4695 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4696 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4697 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4699 9f98ca05 2021-09-24 stsp goto done;
4700 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4701 9f98ca05 2021-09-24 stsp if (n < 0) {
4702 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4703 9f98ca05 2021-09-24 stsp goto done;
4705 9f98ca05 2021-09-24 stsp outoff += n;
4706 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4707 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4709 9f98ca05 2021-09-24 stsp goto done;
4710 9f98ca05 2021-09-24 stsp free(id_str);
4711 9f98ca05 2021-09-24 stsp id_str = NULL;
4715 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4717 5943eee2 2019-08-13 stsp goto done;
4718 fe621944 2020-11-10 stsp s = logmsg;
4719 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4720 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4721 fe621944 2020-11-10 stsp if (n < 0) {
4722 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4723 fe621944 2020-11-10 stsp goto done;
4725 fe621944 2020-11-10 stsp outoff += n;
4726 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4727 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4729 fe621944 2020-11-10 stsp goto done;
4732 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
4733 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4734 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
4736 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4737 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4738 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
4739 fe621944 2020-11-10 stsp if (n < 0) {
4740 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4741 fe621944 2020-11-10 stsp goto done;
4743 fe621944 2020-11-10 stsp outoff += n;
4744 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4745 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4747 fe621944 2020-11-10 stsp goto done;
4750 0208f208 2020-05-05 stsp fputc('\n', outfile);
4752 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4754 5191b70b 2023-01-07 mark goto done;
4756 5191b70b 2023-01-07 mark n = fprintf(outfile,
4757 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4758 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4759 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4760 5191b70b 2023-01-07 mark if (n < 0) {
4761 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
4762 5191b70b 2023-01-07 mark goto done;
4764 5191b70b 2023-01-07 mark outoff += n;
4765 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4767 5191b70b 2023-01-07 mark goto done;
4769 5191b70b 2023-01-07 mark fputc('\n', outfile);
4771 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4773 abd2672a 2018-12-23 stsp free(id_str);
4774 5943eee2 2019-08-13 stsp free(logmsg);
4775 8b473291 2019-02-21 stsp free(refs_str);
4776 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4777 7510f233 2020-08-09 stsp if (err) {
4778 c7d5c43c 2022-08-04 mark free(*lines);
4779 c7d5c43c 2022-08-04 mark *lines = NULL;
4780 ae6a6978 2020-08-09 stsp *nlines = 0;
4782 fe621944 2020-11-10 stsp return err;
4785 abd2672a 2018-12-23 stsp static const struct got_error *
4786 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4788 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4789 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
4790 15a94983 2018-12-23 stsp int obj_type;
4791 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
4792 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
4794 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
4796 c7d5c43c 2022-08-04 mark free(s->lines);
4797 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4798 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4799 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4800 fe621944 2020-11-10 stsp s->nlines = 0;
4802 511a516b 2018-05-19 stsp f = got_opentemp();
4803 48ae06ee 2018-10-18 stsp if (f == NULL) {
4804 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4805 48ae06ee 2018-10-18 stsp goto done;
4807 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
4808 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
4809 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
4810 1f3405c9 2023-01-17 mark goto done;
4812 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4813 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4814 fb43ecf1 2019-02-11 stsp goto done;
4818 15a94983 2018-12-23 stsp if (s->id1)
4819 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4821 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4823 15a94983 2018-12-23 stsp goto done;
4825 15a94983 2018-12-23 stsp switch (obj_type) {
4826 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4827 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4828 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4829 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4830 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4833 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4834 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4835 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4836 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4837 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
4839 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4840 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4841 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4842 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4843 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4844 1f3405c9 2023-01-17 mark size_t nlines = 0;
4845 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
4846 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
4847 1f3405c9 2023-01-17 mark &changed_paths,
4848 1f3405c9 2023-01-17 mark s->ignore_whitespace,
4849 1f3405c9 2023-01-17 mark s->force_text_diff,
4850 1f3405c9 2023-01-17 mark tog_diff_algo
4853 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
4854 1f3405c9 2023-01-17 mark if (lines == NULL) {
4855 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
4856 1f3405c9 2023-01-17 mark goto done;
4859 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
4860 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
4861 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4862 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
4863 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4867 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4869 3ffacbe1 2020-02-02 tracey goto done;
4870 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4871 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4872 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4873 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4874 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
4875 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4876 f44b1f58 2020-02-02 tracey if (err)
4877 f44b1f58 2020-02-02 tracey goto done;
4878 f44b1f58 2020-02-02 tracey } else {
4879 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4880 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4881 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4882 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4883 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4884 5191b70b 2023-01-07 mark s->ignore_whitespace,
4885 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4886 f44b1f58 2020-02-02 tracey if (err)
4887 f44b1f58 2020-02-02 tracey goto done;
4892 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4894 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4895 1f3405c9 2023-01-17 mark lines, nlines);
4899 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4903 1f3405c9 2023-01-17 mark free(lines);
4904 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4905 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4906 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4907 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4908 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
4909 48ae06ee 2018-10-18 stsp return err;
4912 f5215bb9 2019-02-22 stsp static void
4913 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4915 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4916 f5215bb9 2019-02-22 stsp update_panels();
4917 f5215bb9 2019-02-22 stsp doupdate();
4920 f44b1f58 2020-02-02 tracey static const struct got_error *
4921 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4923 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4925 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4926 f44b1f58 2020-02-02 tracey return NULL;
4929 eaf2c13e 2022-09-17 mark static void
4930 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4931 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4933 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4935 eaf2c13e 2022-09-17 mark *f = s->f;
4936 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4937 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4938 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4939 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4940 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4941 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4944 ec2a9698 2022-09-15 mark static const struct got_error *
4945 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4947 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4949 f44b1f58 2020-02-02 tracey int lineno;
4950 cb713507 2022-06-16 stsp char *line = NULL;
4951 826082fe 2020-12-10 stsp size_t linesize = 0;
4952 826082fe 2020-12-10 stsp ssize_t linelen;
4953 ec2a9698 2022-09-15 mark off_t *line_offsets;
4954 ec2a9698 2022-09-15 mark size_t nlines = 0;
4955 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4957 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4958 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4959 eaf2c13e 2022-09-17 mark "view search not supported");
4960 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4961 ec2a9698 2022-09-15 mark &match, &selected);
4963 f44b1f58 2020-02-02 tracey if (!view->searching) {
4964 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4965 f44b1f58 2020-02-02 tracey return NULL;
4968 ec2a9698 2022-09-15 mark if (*match) {
4969 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4970 ec2a9698 2022-09-15 mark lineno = *match + 1;
4972 ec2a9698 2022-09-15 mark lineno = *match - 1;
4974 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4976 f44b1f58 2020-02-02 tracey while (1) {
4977 f44b1f58 2020-02-02 tracey off_t offset;
4979 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4980 ec2a9698 2022-09-15 mark if (*match == 0) {
4981 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4985 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4986 f44b1f58 2020-02-02 tracey lineno = 1;
4988 ec2a9698 2022-09-15 mark lineno = nlines;
4991 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
4992 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
4993 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
4994 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
4995 f44b1f58 2020-02-02 tracey free(line);
4996 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4998 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
4999 cb713507 2022-06-16 stsp if (linelen != -1) {
5000 cb713507 2022-06-16 stsp char *exstr;
5001 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5004 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5005 cb713507 2022-06-16 stsp &view->regmatch)) {
5006 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5007 ec2a9698 2022-09-15 mark *match = lineno;
5008 cb713507 2022-06-16 stsp free(exstr);
5011 cb713507 2022-06-16 stsp free(exstr);
5013 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5014 f44b1f58 2020-02-02 tracey lineno++;
5016 f44b1f58 2020-02-02 tracey lineno--;
5018 826082fe 2020-12-10 stsp free(line);
5020 ec2a9698 2022-09-15 mark if (*match) {
5021 ec2a9698 2022-09-15 mark *first = *match;
5022 ec2a9698 2022-09-15 mark *selected = 1;
5025 145b6838 2022-06-16 stsp return err;
5028 48ae06ee 2018-10-18 stsp static const struct got_error *
5029 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
5031 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
5032 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5034 b72706c3 2022-06-01 stsp free(s->id1);
5035 b72706c3 2022-06-01 stsp s->id1 = NULL;
5036 b72706c3 2022-06-01 stsp free(s->id2);
5037 b72706c3 2022-06-01 stsp s->id2 = NULL;
5038 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
5039 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5040 b72706c3 2022-06-01 stsp s->f = NULL;
5041 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5042 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5043 b72706c3 2022-06-01 stsp s->f1 = NULL;
5044 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5045 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5046 b72706c3 2022-06-01 stsp s->f2 = NULL;
5047 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5048 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5049 f9d37699 2022-06-28 stsp s->fd1 = -1;
5050 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5051 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5052 f9d37699 2022-06-28 stsp s->fd2 = -1;
5053 c7d5c43c 2022-08-04 mark free(s->lines);
5054 c7d5c43c 2022-08-04 mark s->lines = NULL;
5055 b72706c3 2022-06-01 stsp s->nlines = 0;
5056 b72706c3 2022-06-01 stsp return err;
5059 b72706c3 2022-06-01 stsp static const struct got_error *
5060 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5061 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5062 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5063 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
5065 48ae06ee 2018-10-18 stsp const struct got_error *err;
5066 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5068 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
5069 f9d37699 2022-06-28 stsp s->fd1 = -1;
5070 f9d37699 2022-06-28 stsp s->fd2 = -1;
5072 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5073 15a94983 2018-12-23 stsp int type1, type2;
5074 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5076 15a94983 2018-12-23 stsp return err;
5077 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5079 15a94983 2018-12-23 stsp return err;
5081 15a94983 2018-12-23 stsp if (type1 != type2)
5082 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5084 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5085 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5086 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5087 f44b1f58 2020-02-02 tracey s->repo = repo;
5088 f44b1f58 2020-02-02 tracey s->id1 = id1;
5089 f44b1f58 2020-02-02 tracey s->id2 = id2;
5090 3dbaef42 2020-11-24 stsp s->label1 = label1;
5091 3dbaef42 2020-11-24 stsp s->label2 = label2;
5093 15a94983 2018-12-23 stsp if (id1) {
5094 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5095 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5096 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5098 5465d566 2020-02-01 tracey s->id1 = NULL;
5100 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5101 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5102 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5103 b72706c3 2022-06-01 stsp goto done;
5106 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5107 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5108 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5109 a00719e9 2022-06-17 stsp goto done;
5112 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5113 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5114 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5115 b72706c3 2022-06-01 stsp goto done;
5118 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5119 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5120 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5121 f9d37699 2022-06-28 stsp goto done;
5124 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5125 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5126 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5127 f9d37699 2022-06-28 stsp goto done;
5130 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5131 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5132 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5133 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5134 5465d566 2020-02-01 tracey s->repo = repo;
5136 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5139 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5140 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5141 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5142 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5143 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5144 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5145 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5146 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5147 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5148 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5149 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5150 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5151 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5152 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5153 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5154 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5155 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5156 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5157 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5158 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5159 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5160 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5161 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5162 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5163 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5164 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5165 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5166 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5167 b72706c3 2022-06-01 stsp goto done;
5171 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5172 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5173 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5174 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5176 5465d566 2020-02-01 tracey err = create_diff(s);
5178 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5179 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5180 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5181 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5182 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5183 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5184 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5187 b72706c3 2022-06-01 stsp close_diff_view(view);
5188 e5a0f69f 2018-08-18 stsp return err;
5191 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5192 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5194 a3404814 2018-09-02 stsp const struct got_error *err;
5195 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5196 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5197 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5199 a3404814 2018-09-02 stsp if (s->id1) {
5200 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5202 a3404814 2018-09-02 stsp return err;
5203 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5205 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5207 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5209 a3404814 2018-09-02 stsp return err;
5210 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5212 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5213 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5214 a3404814 2018-09-02 stsp free(id_str1);
5215 a3404814 2018-09-02 stsp free(id_str2);
5216 a3404814 2018-09-02 stsp return err;
5218 a3404814 2018-09-02 stsp free(id_str1);
5219 a3404814 2018-09-02 stsp free(id_str2);
5221 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5222 267bb3b8 2021-08-01 stsp free(header);
5223 267bb3b8 2021-08-01 stsp return err;
5226 15a087fe 2019-02-21 stsp static const struct got_error *
5227 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5228 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5230 d7a04538 2019-02-21 stsp const struct got_error *err;
5231 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5232 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5233 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5235 15a087fe 2019-02-21 stsp free(s->id2);
5236 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5237 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5238 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5240 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5242 d7a04538 2019-02-21 stsp return err;
5243 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5244 15a087fe 2019-02-21 stsp free(s->id1);
5245 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5246 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5247 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5248 15a087fe 2019-02-21 stsp return NULL;
5251 0cf4efb1 2018-09-29 stsp static const struct got_error *
5252 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5254 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5256 917d79a7 2022-07-01 stsp view->count = 0;
5257 917d79a7 2022-07-01 stsp wclear(view->window);
5258 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5259 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5260 917d79a7 2022-07-01 stsp s->matched_line = 0;
5261 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5262 917d79a7 2022-07-01 stsp return create_diff(s);
5265 c7d5c43c 2022-08-04 mark static void
5266 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5268 c7d5c43c 2022-08-04 mark int start, i;
5270 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5272 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5273 c7d5c43c 2022-08-04 mark if (i == 0)
5274 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5275 c7d5c43c 2022-08-04 mark if (--i == start)
5276 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5279 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5280 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5283 c7d5c43c 2022-08-04 mark static void
5284 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5286 c7d5c43c 2022-08-04 mark int start, i;
5288 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5290 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5291 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5293 c7d5c43c 2022-08-04 mark if (++i == start)
5294 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5297 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5298 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5301 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5302 c0f61fa4 2022-07-11 mark int, int, int);
5303 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5304 c0f61fa4 2022-07-11 mark int, int);
5306 917d79a7 2022-07-01 stsp static const struct got_error *
5307 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5309 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5310 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5311 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5312 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5313 826082fe 2020-12-10 stsp char *line = NULL;
5314 826082fe 2020-12-10 stsp size_t linesize = 0;
5315 826082fe 2020-12-10 stsp ssize_t linelen;
5316 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5318 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5320 e5a0f69f 2018-08-18 stsp switch (ch) {
5323 145b6838 2022-06-16 stsp case KEY_RIGHT:
5325 145b6838 2022-06-16 stsp case KEY_LEFT:
5327 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
5331 3f6c6614 2023-01-23 mark if (ch == 'a') {
5332 3f6c6614 2023-01-23 mark s->force_text_diff = !s->force_text_diff;
5333 3f6c6614 2023-01-23 mark view->action = s->force_text_diff ?
5334 3f6c6614 2023-01-23 mark "force ASCII text enabled" :
5335 3f6c6614 2023-01-23 mark "force ASCII text disabled";
5337 3f6c6614 2023-01-23 mark else if (ch == 'w') {
5338 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5339 3f6c6614 2023-01-23 mark view->action = s->ignore_whitespace ?
5340 3f6c6614 2023-01-23 mark "ignore whitespace enabled" :
5341 3f6c6614 2023-01-23 mark "ignore whitespace disabled";
5343 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5346 912a3f79 2021-08-30 j case KEY_HOME:
5347 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5348 640cd7ff 2022-06-22 mark view->count = 0;
5351 912a3f79 2021-08-30 j case KEY_END:
5352 640cd7ff 2022-06-22 mark view->count = 0;
5356 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5360 1e37a5c2 2019-05-12 jcs case KEY_UP:
5361 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5362 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5363 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5365 640cd7ff 2022-06-22 mark view->count = 0;
5367 83cc4199 2022-06-13 stsp case CTRL('u'):
5369 83cc4199 2022-06-13 stsp nscroll /= 2;
5370 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5371 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5372 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5374 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5375 640cd7ff 2022-06-22 mark view->count = 0;
5379 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5380 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5383 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5384 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5385 1e37a5c2 2019-05-12 jcs if (!s->eof)
5386 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5388 640cd7ff 2022-06-22 mark view->count = 0;
5390 83cc4199 2022-06-13 stsp case CTRL('d'):
5392 83cc4199 2022-06-13 stsp nscroll /= 2;
5393 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5394 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5395 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5398 640cd7ff 2022-06-22 mark if (s->eof) {
5399 640cd7ff 2022-06-22 mark view->count = 0;
5403 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5404 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5405 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5406 826082fe 2020-12-10 stsp if (linelen == -1) {
5407 826082fe 2020-12-10 stsp if (feof(s->f)) {
5408 826082fe 2020-12-10 stsp s->eof = 1;
5410 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5414 826082fe 2020-12-10 stsp free(line);
5417 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5420 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5423 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5426 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5429 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5430 1e37a5c2 2019-05-12 jcs s->diff_context--;
5431 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5432 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5433 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5434 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5435 27829c9e 2020-11-21 stsp s->nlines) {
5436 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5437 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5440 640cd7ff 2022-06-22 mark view->count = 0;
5443 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5444 1e37a5c2 2019-05-12 jcs s->diff_context++;
5445 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5446 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5447 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5449 640cd7ff 2022-06-22 mark view->count = 0;
5455 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5459 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5460 640cd7ff 2022-06-22 mark view->count = 0;
5463 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5465 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5466 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5467 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5469 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5470 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5473 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5475 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5478 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5481 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5482 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5483 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5485 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5486 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5487 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5489 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5490 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5493 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5495 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5497 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5498 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5499 c0f61fa4 2022-07-11 mark bs->selected_line);
5500 c0f61fa4 2022-07-11 mark if (id == NULL)
5503 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5506 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5510 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5511 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5512 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5513 145b6838 2022-06-16 stsp view->x = 0;
5515 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5516 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5519 640cd7ff 2022-06-22 mark view->count = 0;
5523 bcbd79e2 2018-08-19 stsp return err;
5526 4ed7e80c 2018-05-20 stsp static const struct got_error *
5527 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5529 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5530 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5531 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5532 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5533 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5534 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5535 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5536 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5537 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5538 3dbaef42 2020-11-24 stsp const char *errstr;
5539 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5540 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5542 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5543 26ed57b2 2018-05-19 stsp switch (ch) {
5545 64453f7e 2020-11-21 stsp force_text_diff = 1;
5548 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5550 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5551 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5552 5a20d08d 2022-02-09 op errstr, errstr);
5554 09b5bff8 2020-02-23 naddy case 'r':
5555 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5556 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5557 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5559 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5562 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5565 17020d27 2019-03-07 stsp usage_diff();
5566 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5570 26ed57b2 2018-05-19 stsp argc -= optind;
5571 26ed57b2 2018-05-19 stsp argv += optind;
5573 26ed57b2 2018-05-19 stsp if (argc == 0) {
5574 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5575 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5576 15a94983 2018-12-23 stsp id_str1 = argv[0];
5577 15a94983 2018-12-23 stsp id_str2 = argv[1];
5579 26ed57b2 2018-05-19 stsp usage_diff();
5581 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5582 0ae84acc 2022-06-15 tracey if (error)
5583 0ae84acc 2022-06-15 tracey goto done;
5585 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5586 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5587 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5588 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5589 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5590 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5591 c156c7a4 2020-12-18 stsp goto done;
5592 a273ac94 2020-02-23 naddy if (worktree)
5593 a273ac94 2020-02-23 naddy repo_path =
5594 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5596 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5597 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5598 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5599 c156c7a4 2020-12-18 stsp goto done;
5603 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5604 eb6600df 2019-01-04 stsp if (error)
5605 eb6600df 2019-01-04 stsp goto done;
5607 a273ac94 2020-02-23 naddy init_curses();
5609 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5610 51a10b52 2020-12-26 stsp if (error)
5611 51a10b52 2020-12-26 stsp goto done;
5613 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5614 26ed57b2 2018-05-19 stsp if (error)
5615 26ed57b2 2018-05-19 stsp goto done;
5617 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5618 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5619 26ed57b2 2018-05-19 stsp if (error)
5620 26ed57b2 2018-05-19 stsp goto done;
5622 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5623 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5624 26ed57b2 2018-05-19 stsp if (error)
5625 26ed57b2 2018-05-19 stsp goto done;
5627 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5628 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5629 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5630 ea5e7bb5 2018-08-01 stsp goto done;
5632 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5633 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5634 5dc9f4bc 2018-08-04 stsp if (error)
5635 5dc9f4bc 2018-08-04 stsp goto done;
5636 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5638 3dbaef42 2020-11-24 stsp free(label1);
5639 3dbaef42 2020-11-24 stsp free(label2);
5640 c02c541e 2019-03-29 stsp free(repo_path);
5641 a273ac94 2020-02-23 naddy free(cwd);
5642 1d0f4054 2021-06-17 stsp if (repo) {
5643 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5644 1d0f4054 2021-06-17 stsp if (error == NULL)
5645 1d0f4054 2021-06-17 stsp error = close_err;
5647 a273ac94 2020-02-23 naddy if (worktree)
5648 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5649 0ae84acc 2022-06-15 tracey if (pack_fds) {
5650 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5651 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5652 0ae84acc 2022-06-15 tracey if (error == NULL)
5653 0ae84acc 2022-06-15 tracey error = pack_err;
5655 51a10b52 2020-12-26 stsp tog_free_refs();
5656 26ed57b2 2018-05-19 stsp return error;
5659 4ed7e80c 2018-05-20 stsp __dead static void
5660 9f7d7167 2018-04-29 stsp usage_blame(void)
5663 87411fa9 2022-06-16 stsp fprintf(stderr,
5664 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5665 9f7d7167 2018-04-29 stsp getprogname());
5669 84451b3e 2018-07-10 stsp struct tog_blame_line {
5670 84451b3e 2018-07-10 stsp int annotated;
5671 84451b3e 2018-07-10 stsp struct got_object_id *id;
5674 4ed7e80c 2018-05-20 stsp static const struct got_error *
5675 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5677 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5678 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5679 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5680 84451b3e 2018-07-10 stsp const struct got_error *err;
5681 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5682 826082fe 2020-12-10 stsp char *line = NULL;
5683 826082fe 2020-12-10 stsp size_t linesize = 0;
5684 826082fe 2020-12-10 stsp ssize_t linelen;
5685 84451b3e 2018-07-10 stsp wchar_t *wline;
5686 27a741e5 2019-09-11 stsp int width;
5687 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5688 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5689 ab089a2a 2018-07-12 stsp char *id_str;
5690 11b20872 2019-11-08 stsp struct tog_color *tc;
5692 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5694 ab089a2a 2018-07-12 stsp return err;
5696 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5697 f7d12f7e 2018-08-01 stsp werase(view->window);
5699 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5700 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5701 ab089a2a 2018-07-12 stsp free(id_str);
5702 ab089a2a 2018-07-12 stsp return err;
5705 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5706 ab089a2a 2018-07-12 stsp free(line);
5707 2550e4c3 2018-07-13 stsp line = NULL;
5709 1cae65b4 2019-09-22 stsp return err;
5710 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5711 a3404814 2018-09-02 stsp wstandout(view->window);
5712 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5714 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5715 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5716 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5717 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5719 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5720 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5721 a3404814 2018-09-02 stsp wstandend(view->window);
5722 2550e4c3 2018-07-13 stsp free(wline);
5723 2550e4c3 2018-07-13 stsp wline = NULL;
5725 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5726 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5728 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5729 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5730 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5731 ab089a2a 2018-07-12 stsp free(id_str);
5732 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5734 ab089a2a 2018-07-12 stsp free(id_str);
5735 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5736 3f60a8ef 2018-07-10 stsp free(line);
5737 2550e4c3 2018-07-13 stsp line = NULL;
5739 3f60a8ef 2018-07-10 stsp return err;
5740 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5741 2550e4c3 2018-07-13 stsp free(wline);
5742 2550e4c3 2018-07-13 stsp wline = NULL;
5743 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5744 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5746 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5747 145b6838 2022-06-16 stsp view->maxx = 0;
5748 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5749 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5750 826082fe 2020-12-10 stsp if (linelen == -1) {
5751 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5752 826082fe 2020-12-10 stsp s->eof = 1;
5755 84451b3e 2018-07-10 stsp free(line);
5756 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5758 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5760 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5763 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5764 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5765 1853e0f4 2022-06-16 stsp if (err) {
5766 1853e0f4 2022-06-16 stsp free(line);
5767 1853e0f4 2022-06-16 stsp return err;
5769 1853e0f4 2022-06-16 stsp free(wline);
5770 1853e0f4 2022-06-16 stsp wline = NULL;
5771 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5773 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5774 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5776 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5777 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5778 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5779 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5780 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5781 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5782 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5783 8d0fe45a 2019-08-12 stsp char *id_str;
5784 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5785 87411fa9 2022-06-16 stsp blame_line->id);
5786 8d0fe45a 2019-08-12 stsp if (err) {
5787 8d0fe45a 2019-08-12 stsp free(line);
5788 8d0fe45a 2019-08-12 stsp return err;
5790 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5792 11b20872 2019-11-08 stsp wattr_on(view->window,
5793 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5794 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5796 11b20872 2019-11-08 stsp wattr_off(view->window,
5797 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5798 8d0fe45a 2019-08-12 stsp free(id_str);
5799 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5801 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5802 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5805 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5806 ee41ec32 2018-07-10 stsp prev_id = NULL;
5809 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5810 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5811 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5813 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5814 41605754 2020-11-12 stsp width = 9;
5815 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5816 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5817 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5818 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5819 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5820 41605754 2020-11-12 stsp if (err) {
5821 41605754 2020-11-12 stsp free(line);
5822 41605754 2020-11-12 stsp return err;
5824 41605754 2020-11-12 stsp width += 9;
5827 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5828 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5829 4cb9d869 2022-06-16 stsp if (err) {
5830 4cb9d869 2022-06-16 stsp free(line);
5831 4cb9d869 2022-06-16 stsp return err;
5833 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5834 a75b3e2d 2022-06-16 stsp width += 9;
5835 41605754 2020-11-12 stsp free(wline);
5836 41605754 2020-11-12 stsp wline = NULL;
5839 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5840 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5841 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5842 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5844 826082fe 2020-12-10 stsp free(line);
5845 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5847 9b058f45 2022-06-30 mark view_border(view);
5849 84451b3e 2018-07-10 stsp return NULL;
5852 84451b3e 2018-07-10 stsp static const struct got_error *
5853 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5854 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5856 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5857 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5858 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5859 1a76625f 2018-10-22 stsp int errcode;
5861 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5862 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5863 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5865 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5866 1a76625f 2018-10-22 stsp if (errcode)
5867 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5869 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5870 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5871 d68a0a7d 2018-07-10 stsp goto done;
5874 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5875 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5877 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5878 d68a0a7d 2018-07-10 stsp if (line->annotated)
5879 d68a0a7d 2018-07-10 stsp goto done;
5881 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5882 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5883 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5884 84451b3e 2018-07-10 stsp goto done;
5886 84451b3e 2018-07-10 stsp line->annotated = 1;
5888 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5889 1a76625f 2018-10-22 stsp if (errcode)
5890 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5891 84451b3e 2018-07-10 stsp return err;
5894 84451b3e 2018-07-10 stsp static void *
5895 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5897 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5898 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5899 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5900 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5901 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5903 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5904 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5905 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5907 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5908 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5909 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5910 e6e73e55 2022-06-30 tracey goto done;
5913 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5914 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5915 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5916 e6e73e55 2022-06-30 tracey goto done;
5918 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5919 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5920 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5921 e6e73e55 2022-06-30 tracey goto done;
5924 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5926 e6e73e55 2022-06-30 tracey goto done;
5928 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5929 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5930 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5931 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5932 fc06ba56 2019-08-22 stsp err = NULL;
5934 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5935 e6e73e55 2022-06-30 tracey if (errcode) {
5936 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5937 e6e73e55 2022-06-30 tracey goto done;
5940 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5941 1d0f4054 2021-06-17 stsp if (err == NULL)
5942 1d0f4054 2021-06-17 stsp err = close_err;
5943 c9beca56 2018-07-22 stsp ta->repo = NULL;
5944 c9beca56 2018-07-22 stsp *ta->complete = 1;
5946 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5947 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5948 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5951 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5952 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5953 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5954 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5955 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5956 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5957 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5958 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5960 18430de3 2018-07-10 stsp return (void *)err;
5963 245d91c1 2018-07-12 stsp static struct got_object_id *
5964 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5965 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5967 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5969 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5970 8d0fe45a 2019-08-12 stsp return NULL;
5972 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5973 c0f61fa4 2022-07-11 mark if (!line->annotated)
5974 c0f61fa4 2022-07-11 mark return NULL;
5976 c0f61fa4 2022-07-11 mark return line->id;
5979 c0f61fa4 2022-07-11 mark static struct got_object_id *
5980 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5981 c0f61fa4 2022-07-11 mark int lineno)
5983 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5985 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5986 c0f61fa4 2022-07-11 mark return NULL;
5988 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5989 245d91c1 2018-07-12 stsp if (!line->annotated)
5990 245d91c1 2018-07-12 stsp return NULL;
5992 245d91c1 2018-07-12 stsp return line->id;
5995 b880a918 2018-07-10 stsp static const struct got_error *
5996 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5998 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6001 245d91c1 2018-07-12 stsp if (blame->thread) {
6002 1a76625f 2018-10-22 stsp int errcode;
6003 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6004 1a76625f 2018-10-22 stsp if (errcode)
6005 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6006 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6007 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6008 1a76625f 2018-10-22 stsp if (errcode)
6009 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6010 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6011 1a76625f 2018-10-22 stsp if (errcode)
6012 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6013 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6014 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6015 245d91c1 2018-07-12 stsp err = NULL;
6016 245d91c1 2018-07-12 stsp blame->thread = NULL;
6018 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6019 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6020 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6021 1d0f4054 2021-06-17 stsp if (err == NULL)
6022 1d0f4054 2021-06-17 stsp err = close_err;
6023 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6025 245d91c1 2018-07-12 stsp if (blame->f) {
6026 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6027 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6028 245d91c1 2018-07-12 stsp blame->f = NULL;
6030 57670559 2018-12-23 stsp if (blame->lines) {
6031 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6032 57670559 2018-12-23 stsp free(blame->lines[i].id);
6033 57670559 2018-12-23 stsp free(blame->lines);
6034 57670559 2018-12-23 stsp blame->lines = NULL;
6036 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6037 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6038 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
6039 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6040 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
6041 0ae84acc 2022-06-15 tracey if (err == NULL)
6042 0ae84acc 2022-06-15 tracey err = pack_err;
6043 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
6045 245d91c1 2018-07-12 stsp return err;
6048 245d91c1 2018-07-12 stsp static const struct got_error *
6049 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6051 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6052 fc06ba56 2019-08-22 stsp int *done = arg;
6053 fc06ba56 2019-08-22 stsp int errcode;
6055 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6056 fc06ba56 2019-08-22 stsp if (errcode)
6057 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6058 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6060 fc06ba56 2019-08-22 stsp if (*done)
6061 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6063 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6064 fc06ba56 2019-08-22 stsp if (errcode)
6065 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6066 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6068 fc06ba56 2019-08-22 stsp return err;
6071 fc06ba56 2019-08-22 stsp static const struct got_error *
6072 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6074 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6075 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6076 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6077 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6078 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6079 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6080 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6081 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6082 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6084 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6085 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6087 15a94983 2018-12-23 stsp return err;
6089 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6090 eb81bc23 2022-06-28 tracey if (fd == -1) {
6091 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6092 eb81bc23 2022-06-28 tracey goto done;
6095 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6097 a44927cc 2022-04-07 stsp goto done;
6099 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6101 84451b3e 2018-07-10 stsp goto done;
6103 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6104 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6105 84451b3e 2018-07-10 stsp goto done;
6108 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6110 a70480e0 2018-06-23 stsp goto done;
6111 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6112 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6113 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6114 84451b3e 2018-07-10 stsp goto done;
6116 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6117 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6119 1fddf795 2021-01-20 stsp goto done;
6120 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6121 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6122 84451b3e 2018-07-10 stsp goto done;
6125 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6126 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6127 b02560ec 2019-08-19 stsp blame->nlines--;
6129 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6130 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6131 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6132 84451b3e 2018-07-10 stsp goto done;
6135 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6137 bd24772e 2018-07-11 stsp goto done;
6138 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6139 0ae84acc 2022-06-15 tracey pack_fds);
6140 0ae84acc 2022-06-15 tracey if (err)
6141 0ae84acc 2022-06-15 tracey goto done;
6143 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6144 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6145 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6146 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6147 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6148 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6149 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6150 245d91c1 2018-07-12 stsp goto done;
6152 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6154 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6155 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6156 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6157 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6158 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6159 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6160 a5388363 2020-12-01 naddy s->blame_complete = 0;
6162 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6163 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6164 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6165 f5a09613 2020-12-13 naddy s->selected_line = 1;
6167 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6170 a44927cc 2022-04-07 stsp if (commit)
6171 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6172 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6173 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6175 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6176 27d434c2 2018-09-15 stsp free(obj_id);
6178 245d91c1 2018-07-12 stsp stop_blame(blame);
6179 245d91c1 2018-07-12 stsp return err;
6182 245d91c1 2018-07-12 stsp static const struct got_error *
6183 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6184 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6186 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6187 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6189 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6191 c4843652 2019-08-12 stsp s->path = strdup(path);
6192 c4843652 2019-08-12 stsp if (s->path == NULL)
6193 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6195 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6196 c4843652 2019-08-12 stsp if (err) {
6197 c4843652 2019-08-12 stsp free(s->path);
6198 7cbe629d 2018-08-04 stsp return err;
6201 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6202 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6203 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6204 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6205 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6206 fb2756b9 2018-08-04 stsp s->repo = repo;
6207 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6208 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6210 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6211 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6212 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6213 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6215 11b20872 2019-11-08 stsp return err;
6218 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6219 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6220 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6221 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6222 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6223 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6224 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6226 a5388363 2020-12-01 naddy return run_blame(view);
6229 e5a0f69f 2018-08-18 stsp static const struct got_error *
6230 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6232 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6233 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6235 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6236 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6238 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6239 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6240 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6241 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6242 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6245 e5a0f69f 2018-08-18 stsp free(s->path);
6246 11b20872 2019-11-08 stsp free_colors(&s->colors);
6247 e5a0f69f 2018-08-18 stsp return err;
6250 7cbe629d 2018-08-04 stsp static const struct got_error *
6251 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6253 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6255 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6256 6c4c42e0 2019-06-24 stsp return NULL;
6259 eaf2c13e 2022-09-17 mark static void
6260 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6261 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6263 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6265 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6266 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6267 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6268 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6269 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6270 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6271 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6274 6c4c42e0 2019-06-24 stsp static const struct got_error *
6275 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6277 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6278 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6279 2b380cc8 2018-10-24 stsp int errcode;
6281 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6282 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6283 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6284 2b380cc8 2018-10-24 stsp if (errcode)
6285 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6287 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6290 51fe7530 2019-08-19 stsp if (s->blame_complete)
6291 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6293 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6295 9b058f45 2022-06-30 mark view_border(view);
6296 e5a0f69f 2018-08-18 stsp return err;
6299 e5a0f69f 2018-08-18 stsp static const struct got_error *
6300 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6301 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6303 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6304 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6306 05f04cdf 2022-07-20 mark *new_view = NULL;
6308 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6309 05f04cdf 2022-07-20 mark if (log_view == NULL)
6310 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6312 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6314 05f04cdf 2022-07-20 mark view_close(log_view);
6316 05f04cdf 2022-07-20 mark *new_view = log_view;
6318 05f04cdf 2022-07-20 mark return err;
6321 05f04cdf 2022-07-20 mark static const struct got_error *
6322 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6324 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6325 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6326 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6327 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6329 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6330 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6331 9b058f45 2022-06-30 mark --eos; /* border */
6333 e5a0f69f 2018-08-18 stsp switch (ch) {
6336 145b6838 2022-06-16 stsp case KEY_RIGHT:
6338 145b6838 2022-06-16 stsp case KEY_LEFT:
6340 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
6343 1e37a5c2 2019-05-12 jcs s->done = 1;
6345 4deef56f 2021-09-02 naddy case 'g':
6346 4deef56f 2021-09-02 naddy case KEY_HOME:
6347 4deef56f 2021-09-02 naddy s->selected_line = 1;
6348 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6349 640cd7ff 2022-06-22 mark view->count = 0;
6351 4deef56f 2021-09-02 naddy case 'G':
6352 4deef56f 2021-09-02 naddy case KEY_END:
6353 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6354 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6355 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6357 9b058f45 2022-06-30 mark s->selected_line = eos;
6358 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6360 640cd7ff 2022-06-22 mark view->count = 0;
6363 1e37a5c2 2019-05-12 jcs case KEY_UP:
6364 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6365 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6366 1e37a5c2 2019-05-12 jcs s->selected_line--;
6367 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6368 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6369 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6371 640cd7ff 2022-06-22 mark view->count = 0;
6373 83cc4199 2022-06-13 stsp case CTRL('u'):
6375 83cc4199 2022-06-13 stsp nscroll /= 2;
6376 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6377 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6378 ea025d1d 2020-02-22 naddy case CTRL('b'):
6380 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6381 640cd7ff 2022-06-22 mark if (view->count > 1)
6382 640cd7ff 2022-06-22 mark nscroll += nscroll;
6383 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6384 640cd7ff 2022-06-22 mark view->count = 0;
6387 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6388 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6390 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6393 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6394 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6395 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6396 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6397 1e37a5c2 2019-05-12 jcs s->selected_line++;
6398 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6399 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6401 640cd7ff 2022-06-22 mark view->count = 0;
6404 1e37a5c2 2019-05-12 jcs case 'p': {
6405 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6407 640cd7ff 2022-06-22 mark view->count = 0;
6408 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6409 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6410 1e37a5c2 2019-05-12 jcs if (id == NULL)
6412 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6413 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6414 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6415 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6416 1e37a5c2 2019-05-12 jcs int obj_type;
6417 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6418 1e37a5c2 2019-05-12 jcs s->repo, id);
6421 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6422 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6423 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6424 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6427 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6428 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6429 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6432 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6433 a44927cc 2022-04-07 stsp pcommit, s->path);
6434 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6435 e5a0f69f 2018-08-18 stsp if (err) {
6436 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6437 1e37a5c2 2019-05-12 jcs err = NULL;
6438 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6441 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6443 1e37a5c2 2019-05-12 jcs free(blob_id);
6444 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6445 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6446 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6449 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6450 d7b5a0e8 2022-04-20 stsp &pid->id);
6451 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6453 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6454 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6456 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6461 1e37a5c2 2019-05-12 jcs s->done = 1;
6462 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6463 1e37a5c2 2019-05-12 jcs s->done = 0;
6464 1e37a5c2 2019-05-12 jcs if (thread_err)
6466 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6467 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6468 a5388363 2020-12-01 naddy err = run_blame(view);
6473 61417565 2022-06-20 mark case 'C': {
6474 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6476 640cd7ff 2022-06-22 mark view->count = 0;
6477 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6478 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6480 1e37a5c2 2019-05-12 jcs s->done = 1;
6481 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6482 1e37a5c2 2019-05-12 jcs s->done = 0;
6483 1e37a5c2 2019-05-12 jcs if (thread_err)
6485 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6486 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6487 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6488 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6489 a5388363 2020-12-01 naddy err = run_blame(view);
6495 05f04cdf 2022-07-20 mark view->count = 0;
6496 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6497 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6498 136e2bd4 2022-07-23 mark if (s->id_to_log)
6499 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6501 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6502 1e37a5c2 2019-05-12 jcs case '\r': {
6503 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6504 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6505 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6507 640cd7ff 2022-06-22 mark view->count = 0;
6508 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6509 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6510 1e37a5c2 2019-05-12 jcs if (id == NULL)
6512 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6515 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6516 c0f61fa4 2022-07-11 mark if (*new_view) {
6517 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6518 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6521 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6523 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6524 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6526 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6527 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6528 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6529 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6530 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6534 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6535 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6536 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6538 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6541 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6542 c0f61fa4 2022-07-11 mark s->selected_line;
6543 c0f61fa4 2022-07-11 mark if (*new_view)
6544 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6545 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6546 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6547 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6552 e78dc838 2020-12-04 stsp view->focussed = 0;
6553 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6554 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6555 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6556 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6557 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6558 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6561 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6564 e78dc838 2020-12-04 stsp view->focus_child = 1;
6566 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6571 83cc4199 2022-06-13 stsp case CTRL('d'):
6573 83cc4199 2022-06-13 stsp nscroll /= 2;
6574 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6575 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6576 ea025d1d 2020-02-22 naddy case CTRL('f'):
6579 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6580 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6581 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6582 640cd7ff 2022-06-22 mark view->count = 0;
6585 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6586 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6587 83cc4199 2022-06-13 stsp s->selected_line +=
6588 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6589 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6591 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6592 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6594 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6595 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6597 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6598 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6599 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6600 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6604 640cd7ff 2022-06-22 mark view->count = 0;
6607 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6610 917d79a7 2022-07-01 stsp static const struct got_error *
6611 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6613 917d79a7 2022-07-01 stsp const struct got_error *err;
6614 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6616 917d79a7 2022-07-01 stsp view->count = 0;
6617 917d79a7 2022-07-01 stsp s->done = 1;
6618 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6619 917d79a7 2022-07-01 stsp s->done = 0;
6621 917d79a7 2022-07-01 stsp return err;
6622 917d79a7 2022-07-01 stsp return run_blame(view);
6625 a70480e0 2018-06-23 stsp static const struct got_error *
6626 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6628 a70480e0 2018-06-23 stsp const struct got_error *error;
6629 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6630 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6631 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6632 0587e10c 2020-07-23 stsp char *link_target = NULL;
6633 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6634 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6635 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6637 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6638 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6640 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6641 a70480e0 2018-06-23 stsp switch (ch) {
6643 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6646 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6647 69069811 2018-08-02 stsp if (repo_path == NULL)
6648 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6652 17020d27 2019-03-07 stsp usage_blame();
6653 a70480e0 2018-06-23 stsp /* NOTREACHED */
6657 a70480e0 2018-06-23 stsp argc -= optind;
6658 a70480e0 2018-06-23 stsp argv += optind;
6660 f135c941 2020-02-20 stsp if (argc != 1)
6661 a70480e0 2018-06-23 stsp usage_blame();
6663 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6664 0ae84acc 2022-06-15 tracey if (error != NULL)
6665 0ae84acc 2022-06-15 tracey goto done;
6667 69069811 2018-08-02 stsp if (repo_path == NULL) {
6668 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6669 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6670 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6671 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6672 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6673 c156c7a4 2020-12-18 stsp goto done;
6674 f135c941 2020-02-20 stsp if (worktree)
6675 eb41ed75 2019-02-05 stsp repo_path =
6676 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6678 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6679 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6680 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6681 c156c7a4 2020-12-18 stsp goto done;
6685 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6686 c02c541e 2019-03-29 stsp if (error != NULL)
6687 8e94dd5b 2019-01-04 stsp goto done;
6689 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6690 f135c941 2020-02-20 stsp worktree);
6691 c02c541e 2019-03-29 stsp if (error)
6692 92205607 2019-01-04 stsp goto done;
6694 f135c941 2020-02-20 stsp init_curses();
6696 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6697 51a10b52 2020-12-26 stsp if (error)
6698 51a10b52 2020-12-26 stsp goto done;
6700 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6701 eb41ed75 2019-02-05 stsp if (error)
6702 69069811 2018-08-02 stsp goto done;
6704 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6705 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6706 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6707 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6708 a70480e0 2018-06-23 stsp if (error != NULL)
6709 66b4983c 2018-06-23 stsp goto done;
6710 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6711 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6713 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6714 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6716 a19e88aa 2018-06-23 stsp if (error != NULL)
6717 8b473291 2019-02-21 stsp goto done;
6719 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6720 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6721 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6722 e1cd8fed 2018-08-01 stsp goto done;
6725 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6726 a44927cc 2022-04-07 stsp if (error)
6727 a44927cc 2022-04-07 stsp goto done;
6729 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6730 a44927cc 2022-04-07 stsp commit, repo);
6731 7cbe629d 2018-08-04 stsp if (error)
6732 7cbe629d 2018-08-04 stsp goto done;
6734 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6735 78756c87 2020-11-24 stsp commit_id, repo);
6736 0587e10c 2020-07-23 stsp if (error)
6737 0587e10c 2020-07-23 stsp goto done;
6738 12314ad4 2019-08-31 stsp if (worktree) {
6739 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6740 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6741 12314ad4 2019-08-31 stsp worktree = NULL;
6743 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6745 69069811 2018-08-02 stsp free(repo_path);
6746 f135c941 2020-02-20 stsp free(in_repo_path);
6747 0587e10c 2020-07-23 stsp free(link_target);
6748 69069811 2018-08-02 stsp free(cwd);
6749 a70480e0 2018-06-23 stsp free(commit_id);
6750 a44927cc 2022-04-07 stsp if (commit)
6751 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6752 eb41ed75 2019-02-05 stsp if (worktree)
6753 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6754 1d0f4054 2021-06-17 stsp if (repo) {
6755 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6756 1d0f4054 2021-06-17 stsp if (error == NULL)
6757 1d0f4054 2021-06-17 stsp error = close_err;
6759 0ae84acc 2022-06-15 tracey if (pack_fds) {
6760 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6761 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6762 0ae84acc 2022-06-15 tracey if (error == NULL)
6763 0ae84acc 2022-06-15 tracey error = pack_err;
6765 51a10b52 2020-12-26 stsp tog_free_refs();
6766 a70480e0 2018-06-23 stsp return error;
6769 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6770 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6772 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6773 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6774 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6775 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6776 0c6ad1bc 2022-09-09 mark char *index = NULL;
6777 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6778 d627976f 2023-02-01 mark int width, n, nentries, scrollx, i = 1;
6779 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6781 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6782 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6783 9b058f45 2022-06-30 mark --limit; /* border */
6785 f7d12f7e 2018-08-01 stsp werase(view->window);
6787 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6788 ffd1d5e5 2018-06-23 stsp return NULL;
6790 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6793 ffd1d5e5 2018-06-23 stsp return err;
6794 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6795 a3404814 2018-09-02 stsp wstandout(view->window);
6796 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6798 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6799 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6800 0c6ad1bc 2022-09-09 mark free(wline);
6801 0c6ad1bc 2022-09-09 mark wline = NULL;
6802 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6803 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6805 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6806 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6807 a3404814 2018-09-02 stsp wstandend(view->window);
6808 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6809 0c6ad1bc 2022-09-09 mark return NULL;
6811 df68a56b 2022-08-12 mark i += s->selected;
6812 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6813 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6814 df68a56b 2022-08-12 mark if (s->tree != s->root)
6815 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6817 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6818 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6819 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6820 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6821 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6822 0c6ad1bc 2022-09-09 mark free(index);
6824 ce52c690 2018-06-23 stsp return err;
6825 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6826 2550e4c3 2018-07-13 stsp free(wline);
6827 2550e4c3 2018-07-13 stsp wline = NULL;
6828 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6829 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6830 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6831 ffd1d5e5 2018-06-23 stsp return NULL;
6832 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6833 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6834 a1eca9bb 2018-06-23 stsp return NULL;
6836 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6837 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6838 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6839 0cf4efb1 2018-09-29 stsp if (view->focussed)
6840 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6841 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6843 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6844 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6845 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6846 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6847 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6848 ffd1d5e5 2018-06-23 stsp return NULL;
6852 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6855 d627976f 2023-02-01 mark view->maxx = 0;
6856 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6857 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6858 848d6979 2019-08-12 stsp const char *modestr = "";
6859 56e0773d 2019-11-28 stsp mode_t mode;
6861 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6862 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6864 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6865 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6866 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6868 638f9024 2019-05-13 stsp return got_error_from_errno(
6869 230a42bd 2019-05-11 jcs "got_object_id_str");
6871 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6872 63c5ca5d 2019-08-24 stsp modestr = "$";
6873 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6876 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6877 d86d3b18 2020-12-01 naddy te, s->repo);
6878 0d6c6ee3 2020-05-20 stsp if (err) {
6879 0d6c6ee3 2020-05-20 stsp free(id_str);
6880 0d6c6ee3 2020-05-20 stsp return err;
6882 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6883 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6884 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6886 848d6979 2019-08-12 stsp modestr = "@";
6888 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6889 848d6979 2019-08-12 stsp modestr = "/";
6890 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6891 848d6979 2019-08-12 stsp modestr = "*";
6892 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6893 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6894 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6895 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6896 1d13200f 2018-07-12 stsp free(id_str);
6897 0d6c6ee3 2020-05-20 stsp free(link_target);
6898 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6900 1d13200f 2018-07-12 stsp free(id_str);
6901 0d6c6ee3 2020-05-20 stsp free(link_target);
6903 d627976f 2023-02-01 mark /* use full line width to determine view->maxx */
6904 d627976f 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6905 ffd1d5e5 2018-06-23 stsp if (err) {
6906 ffd1d5e5 2018-06-23 stsp free(line);
6909 d627976f 2023-02-01 mark view->maxx = MAX(view->maxx, width);
6910 d627976f 2023-02-01 mark free(wline);
6911 d627976f 2023-02-01 mark wline = NULL;
6913 d627976f 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
6914 d627976f 2023-02-01 mark view->ncols, 0, 0);
6915 d627976f 2023-02-01 mark if (err) {
6916 d627976f 2023-02-01 mark free(line);
6919 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6920 0cf4efb1 2018-09-29 stsp if (view->focussed)
6921 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6922 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6924 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6926 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6927 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6928 d627976f 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
6930 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6931 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6932 b637f33f 2023-02-01 mark if (width < view->ncols)
6933 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6934 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6935 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6936 ffd1d5e5 2018-06-23 stsp free(line);
6937 2550e4c3 2018-07-13 stsp free(wline);
6938 2550e4c3 2018-07-13 stsp wline = NULL;
6940 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6941 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6942 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6946 ffd1d5e5 2018-06-23 stsp return err;
6949 ffd1d5e5 2018-06-23 stsp static void
6950 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6952 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6953 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6954 fa86c4bf 2020-11-29 stsp int i = 0;
6956 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6959 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6960 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6961 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6962 fa86c4bf 2020-11-29 stsp if (!isroot)
6963 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6966 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6967 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6971 9b058f45 2022-06-30 mark static const struct got_error *
6972 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6974 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6975 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6976 ffd1d5e5 2018-06-23 stsp int n = 0;
6978 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6979 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6980 694d3271 2020-12-01 naddy s->first_displayed_entry);
6982 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6984 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6985 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6986 94b80cfa 2022-08-01 mark if (last) {
6987 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6988 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6990 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6991 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6992 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6996 9b058f45 2022-06-30 mark return NULL;
6999 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7000 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7001 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7003 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7004 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7005 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7007 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7008 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7009 56e0773d 2019-11-28 stsp + 1 /* slash */;
7011 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7013 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7014 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7015 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7017 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7018 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7019 d9765a41 2018-06-23 stsp while (pt) {
7020 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7021 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7022 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7023 cb2ebc8a 2018-06-23 stsp goto done;
7025 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7026 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7027 cb2ebc8a 2018-06-23 stsp goto done;
7029 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7032 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7033 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7034 ce52c690 2018-06-23 stsp goto done;
7038 ce52c690 2018-06-23 stsp if (err) {
7039 ce52c690 2018-06-23 stsp free(*path);
7040 ce52c690 2018-06-23 stsp *path = NULL;
7042 ce52c690 2018-06-23 stsp return err;
7045 ce52c690 2018-06-23 stsp static const struct got_error *
7046 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7047 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7048 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7050 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7051 ce52c690 2018-06-23 stsp char *path;
7052 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7054 a0de39f3 2019-08-09 stsp *new_view = NULL;
7056 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7058 ce52c690 2018-06-23 stsp return err;
7060 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7061 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7062 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7063 83ce39e3 2019-08-12 stsp goto done;
7066 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7067 e5a0f69f 2018-08-18 stsp if (err) {
7068 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7069 fc06ba56 2019-08-22 stsp err = NULL;
7070 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7072 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7074 83ce39e3 2019-08-12 stsp free(path);
7075 69efd4c4 2018-07-18 stsp return err;
7078 69efd4c4 2018-07-18 stsp static const struct got_error *
7079 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7080 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7082 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7083 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7084 69efd4c4 2018-07-18 stsp char *path;
7086 a0de39f3 2019-08-09 stsp *new_view = NULL;
7088 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7089 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7090 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7092 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7094 69efd4c4 2018-07-18 stsp return err;
7096 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7099 e5a0f69f 2018-08-18 stsp view_close(log_view);
7101 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7102 cb2ebc8a 2018-06-23 stsp free(path);
7103 cb2ebc8a 2018-06-23 stsp return err;
7106 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7107 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7108 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7110 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7111 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7112 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7113 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7115 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7116 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7118 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7119 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7120 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7122 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7124 bc573f3b 2021-07-10 stsp goto done;
7127 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7128 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7129 bc573f3b 2021-07-10 stsp * closed on demand.
7131 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7132 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7134 bc573f3b 2021-07-10 stsp goto done;
7135 bc573f3b 2021-07-10 stsp s->tree = s->root;
7137 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7138 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7139 ffd1d5e5 2018-06-23 stsp goto done;
7141 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7142 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7143 ffd1d5e5 2018-06-23 stsp goto done;
7146 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7147 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7148 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7149 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7150 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7151 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7152 9cd7cbd1 2020-12-07 stsp goto done;
7155 fb2756b9 2018-08-04 stsp s->repo = repo;
7157 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7158 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7159 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7160 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7162 c0b01bdb 2019-11-08 stsp goto done;
7163 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7164 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7166 c0b01bdb 2019-11-08 stsp goto done;
7167 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7168 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7169 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7171 c0b01bdb 2019-11-08 stsp goto done;
7173 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7174 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7175 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7177 11b20872 2019-11-08 stsp goto done;
7179 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7180 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7182 c0b01bdb 2019-11-08 stsp goto done;
7185 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7186 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7187 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7188 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7189 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7191 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7192 bc573f3b 2021-07-10 stsp if (commit)
7193 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7195 bc573f3b 2021-07-10 stsp close_tree_view(view);
7196 ad80ab7b 2018-08-04 stsp return err;
7199 e5a0f69f 2018-08-18 stsp static const struct got_error *
7200 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7202 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7204 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7205 fb2756b9 2018-08-04 stsp free(s->tree_label);
7206 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7207 6484ec90 2018-09-29 stsp free(s->commit_id);
7208 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7209 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7210 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7211 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7212 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7213 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7214 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7215 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7216 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7217 ad80ab7b 2018-08-04 stsp free(parent);
7220 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7221 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7222 bc573f3b 2021-07-10 stsp if (s->root)
7223 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7224 7c32bd05 2019-06-22 stsp return NULL;
7227 7c32bd05 2019-06-22 stsp static const struct got_error *
7228 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7230 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7232 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7233 7c32bd05 2019-06-22 stsp return NULL;
7236 7c32bd05 2019-06-22 stsp static int
7237 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7239 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7241 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7245 7c32bd05 2019-06-22 stsp static const struct got_error *
7246 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7248 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7249 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7251 7c32bd05 2019-06-22 stsp if (!view->searching) {
7252 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7253 7c32bd05 2019-06-22 stsp return NULL;
7256 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7257 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7258 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7259 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7260 56e0773d 2019-11-28 stsp s->selected_entry);
7262 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7264 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7265 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7267 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7268 56e0773d 2019-11-28 stsp s->selected_entry);
7271 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7272 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7273 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7274 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7276 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7279 7c32bd05 2019-06-22 stsp while (1) {
7280 56e0773d 2019-11-28 stsp if (te == NULL) {
7281 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7282 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7283 ac66afb8 2019-06-24 stsp return NULL;
7285 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7286 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7288 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7291 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7292 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7293 56e0773d 2019-11-28 stsp s->matched_entry = te;
7297 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7298 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7300 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7303 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7304 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7305 7c32bd05 2019-06-22 stsp s->selected = 0;
7308 e5a0f69f 2018-08-18 stsp return NULL;
7311 ad80ab7b 2018-08-04 stsp static const struct got_error *
7312 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7314 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7315 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7316 e5a0f69f 2018-08-18 stsp char *parent_path;
7318 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7320 e5a0f69f 2018-08-18 stsp return err;
7322 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7323 e5a0f69f 2018-08-18 stsp free(parent_path);
7325 9b058f45 2022-06-30 mark view_border(view);
7326 e5a0f69f 2018-08-18 stsp return err;
7329 94b80cfa 2022-08-01 mark static const struct got_error *
7330 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7332 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7333 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7334 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7335 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7336 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7337 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7339 94b80cfa 2022-08-01 mark g = view->gline;
7340 94b80cfa 2022-08-01 mark view->gline = 0;
7342 94b80cfa 2022-08-01 mark if (g == 0)
7344 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7345 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7347 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7348 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7349 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7351 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7352 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7353 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7355 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7356 94b80cfa 2022-08-01 mark last += off;
7358 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7359 94b80cfa 2022-08-01 mark s->selected = g - first;
7360 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7363 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7364 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7368 94b80cfa 2022-08-01 mark if (i < g) {
7369 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7371 94b80cfa 2022-08-01 mark return err;
7372 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7373 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7374 94b80cfa 2022-08-01 mark first + s->selected < g &&
7375 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7376 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7377 94b80cfa 2022-08-01 mark first += off;
7378 94b80cfa 2022-08-01 mark s->selected = g - first;
7380 94b80cfa 2022-08-01 mark } else if (i > g)
7381 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7383 94b80cfa 2022-08-01 mark if (g < nlines &&
7384 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7385 94b80cfa 2022-08-01 mark s->selected = g - 1;
7387 94b80cfa 2022-08-01 mark return NULL;
7390 e5a0f69f 2018-08-18 stsp static const struct got_error *
7391 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7393 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7394 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7395 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7396 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7398 94b80cfa 2022-08-01 mark if (view->gline)
7399 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7401 e5a0f69f 2018-08-18 stsp switch (ch) {
7404 d627976f 2023-02-01 mark case KEY_RIGHT:
7406 d627976f 2023-02-01 mark case KEY_LEFT:
7408 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
7411 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7412 640cd7ff 2022-06-22 mark view->count = 0;
7415 640cd7ff 2022-06-22 mark view->count = 0;
7416 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7418 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7421 640cd7ff 2022-06-22 mark view->count = 0;
7422 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7424 e4526bf5 2021-09-03 naddy case 'g':
7426 e4526bf5 2021-09-03 naddy case KEY_HOME:
7427 e4526bf5 2021-09-03 naddy s->selected = 0;
7428 640cd7ff 2022-06-22 mark view->count = 0;
7429 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7430 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7431 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7433 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7435 e4526bf5 2021-09-03 naddy case 'G':
7437 9b058f45 2022-06-30 mark case KEY_END: {
7438 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7440 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7441 9b058f45 2022-06-30 mark --eos; /* border */
7442 e4526bf5 2021-09-03 naddy s->selected = 0;
7443 640cd7ff 2022-06-22 mark view->count = 0;
7444 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7445 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7446 e4526bf5 2021-09-03 naddy if (te == NULL) {
7447 ad055527 2022-07-16 mark if (s->tree != s->root) {
7448 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7453 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7454 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7456 e4526bf5 2021-09-03 naddy if (n > 0)
7457 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7461 1e37a5c2 2019-05-12 jcs case KEY_UP:
7462 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7463 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7464 1e37a5c2 2019-05-12 jcs s->selected--;
7467 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7468 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7469 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7470 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7471 640cd7ff 2022-06-22 mark view->count = 0;
7473 83cc4199 2022-06-13 stsp case CTRL('u'):
7475 83cc4199 2022-06-13 stsp nscroll /= 2;
7476 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7477 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7478 ea025d1d 2020-02-22 naddy case CTRL('b'):
7480 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7481 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7482 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7483 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7485 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7486 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7488 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7489 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7490 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7491 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7492 640cd7ff 2022-06-22 mark view->count = 0;
7495 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7496 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7497 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7498 1e37a5c2 2019-05-12 jcs s->selected++;
7501 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7502 640cd7ff 2022-06-22 mark == NULL) {
7503 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7504 640cd7ff 2022-06-22 mark view->count = 0;
7507 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7509 83cc4199 2022-06-13 stsp case CTRL('d'):
7511 83cc4199 2022-06-13 stsp nscroll /= 2;
7512 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7513 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7514 ea025d1d 2020-02-22 naddy case CTRL('f'):
7516 48bb96f0 2022-06-20 naddy case ' ':
7517 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7519 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7520 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7521 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7522 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7524 640cd7ff 2022-06-22 mark view->count = 0;
7527 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7529 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7531 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7532 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7533 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7534 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7535 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7536 640cd7ff 2022-06-22 mark view->count = 0;
7539 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7540 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7542 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7543 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7544 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7545 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7546 1e37a5c2 2019-05-12 jcs s->selected_entry =
7547 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7548 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7549 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7550 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7554 1e37a5c2 2019-05-12 jcs free(parent);
7555 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7556 56e0773d 2019-11-28 stsp s->selected_entry))) {
7557 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7558 640cd7ff 2022-06-22 mark view->count = 0;
7559 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7560 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7563 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7564 941e9f74 2019-05-21 stsp if (err) {
7565 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7568 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7569 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7571 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7572 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7573 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7574 640cd7ff 2022-06-22 mark view->count = 0;
7577 640cd7ff 2022-06-22 mark view->count = 0;
7581 ffd1d5e5 2018-06-23 stsp return err;
7584 ffd1d5e5 2018-06-23 stsp __dead static void
7585 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7588 87411fa9 2022-06-16 stsp fprintf(stderr,
7589 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7590 ffd1d5e5 2018-06-23 stsp getprogname());
7594 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7595 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7597 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7598 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7599 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7600 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7601 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7602 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7603 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7604 4e97c21c 2020-12-06 stsp char *label = NULL;
7605 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7606 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7608 5221c383 2018-08-01 stsp struct tog_view *view;
7609 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7611 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7612 ffd1d5e5 2018-06-23 stsp switch (ch) {
7614 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7617 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7618 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7619 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7623 e99e2d15 2020-11-24 naddy usage_tree();
7624 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7628 ffd1d5e5 2018-06-23 stsp argc -= optind;
7629 ffd1d5e5 2018-06-23 stsp argv += optind;
7631 55cccc34 2020-02-20 stsp if (argc > 1)
7632 e99e2d15 2020-11-24 naddy usage_tree();
7634 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7635 0ae84acc 2022-06-15 tracey if (error != NULL)
7636 0ae84acc 2022-06-15 tracey goto done;
7638 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7639 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7640 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7641 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7642 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7643 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7644 c156c7a4 2020-12-18 stsp goto done;
7645 55cccc34 2020-02-20 stsp if (worktree)
7646 52185f70 2019-02-05 stsp repo_path =
7647 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7649 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7650 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7651 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7652 c156c7a4 2020-12-18 stsp goto done;
7656 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7657 c02c541e 2019-03-29 stsp if (error != NULL)
7658 52185f70 2019-02-05 stsp goto done;
7660 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7661 55cccc34 2020-02-20 stsp repo, worktree);
7662 55cccc34 2020-02-20 stsp if (error)
7663 55cccc34 2020-02-20 stsp goto done;
7665 55cccc34 2020-02-20 stsp init_curses();
7667 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7668 c02c541e 2019-03-29 stsp if (error)
7669 52185f70 2019-02-05 stsp goto done;
7671 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7672 51a10b52 2020-12-26 stsp if (error)
7673 51a10b52 2020-12-26 stsp goto done;
7675 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7676 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7677 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7678 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7679 4e97c21c 2020-12-06 stsp if (error)
7680 4e97c21c 2020-12-06 stsp goto done;
7681 4e97c21c 2020-12-06 stsp head_ref_name = label;
7683 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7684 4e97c21c 2020-12-06 stsp if (error == NULL)
7685 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7686 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7687 4e97c21c 2020-12-06 stsp goto done;
7688 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7689 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7690 4e97c21c 2020-12-06 stsp if (error)
7691 4e97c21c 2020-12-06 stsp goto done;
7694 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7695 a44927cc 2022-04-07 stsp if (error)
7696 a44927cc 2022-04-07 stsp goto done;
7698 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7699 5221c383 2018-08-01 stsp if (view == NULL) {
7700 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7701 5221c383 2018-08-01 stsp goto done;
7703 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7704 ad80ab7b 2018-08-04 stsp if (error)
7705 ad80ab7b 2018-08-04 stsp goto done;
7706 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7707 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7708 d91faf3b 2020-12-01 naddy in_repo_path);
7709 55cccc34 2020-02-20 stsp if (error)
7710 55cccc34 2020-02-20 stsp goto done;
7713 55cccc34 2020-02-20 stsp if (worktree) {
7714 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7715 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7716 55cccc34 2020-02-20 stsp worktree = NULL;
7718 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7720 52185f70 2019-02-05 stsp free(repo_path);
7721 e4a0e26d 2020-02-20 stsp free(cwd);
7722 ffd1d5e5 2018-06-23 stsp free(commit_id);
7723 4e97c21c 2020-12-06 stsp free(label);
7725 486cd271 2020-12-06 stsp got_ref_close(ref);
7726 1d0f4054 2021-06-17 stsp if (repo) {
7727 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7728 1d0f4054 2021-06-17 stsp if (error == NULL)
7729 1d0f4054 2021-06-17 stsp error = close_err;
7731 0ae84acc 2022-06-15 tracey if (pack_fds) {
7732 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7733 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7734 0ae84acc 2022-06-15 tracey if (error == NULL)
7735 0ae84acc 2022-06-15 tracey error = pack_err;
7737 51a10b52 2020-12-26 stsp tog_free_refs();
7738 ffd1d5e5 2018-06-23 stsp return error;
7741 6458efa5 2020-11-24 stsp static const struct got_error *
7742 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7744 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7745 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7747 6458efa5 2020-11-24 stsp s->nrefs = 0;
7748 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7749 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7750 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7751 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7752 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7755 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7756 6458efa5 2020-11-24 stsp if (re == NULL)
7757 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7759 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7760 8924d611 2020-12-26 stsp if (re->ref == NULL)
7761 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7762 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7763 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7766 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7767 6458efa5 2020-11-24 stsp return NULL;
7771 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7773 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7775 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7776 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7777 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7778 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7783 6458efa5 2020-11-24 stsp static const struct got_error *
7784 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7786 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7787 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7789 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7790 6458efa5 2020-11-24 stsp s->repo = repo;
7792 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7793 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7795 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7797 6458efa5 2020-11-24 stsp return err;
7799 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7800 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7801 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7802 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7804 6458efa5 2020-11-24 stsp goto done;
7806 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7807 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7808 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7810 6458efa5 2020-11-24 stsp goto done;
7812 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7813 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7814 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7816 cc488aa7 2022-01-23 stsp goto done;
7818 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7819 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7820 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7822 6458efa5 2020-11-24 stsp goto done;
7825 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7826 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7827 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7828 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7829 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7832 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7833 6458efa5 2020-11-24 stsp return err;
7836 6458efa5 2020-11-24 stsp static const struct got_error *
7837 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7839 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7841 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7842 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7844 6458efa5 2020-11-24 stsp return NULL;
7847 6458efa5 2020-11-24 stsp static const struct got_error *
7848 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7849 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7851 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7852 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7853 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7854 6458efa5 2020-11-24 stsp int obj_type;
7856 c42c9805 2020-11-24 stsp *commit_id = NULL;
7858 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7860 6458efa5 2020-11-24 stsp return err;
7862 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7864 6458efa5 2020-11-24 stsp goto done;
7866 6458efa5 2020-11-24 stsp switch (obj_type) {
7867 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7868 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7870 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7871 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7873 6458efa5 2020-11-24 stsp goto done;
7874 c42c9805 2020-11-24 stsp free(obj_id);
7875 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7876 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7878 6458efa5 2020-11-24 stsp goto done;
7879 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7880 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7881 c42c9805 2020-11-24 stsp goto done;
7883 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7884 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7885 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7886 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7887 c42c9805 2020-11-24 stsp goto done;
7891 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7897 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7898 c42c9805 2020-11-24 stsp if (err) {
7899 c42c9805 2020-11-24 stsp free(*commit_id);
7900 c42c9805 2020-11-24 stsp *commit_id = NULL;
7902 c42c9805 2020-11-24 stsp return err;
7905 c42c9805 2020-11-24 stsp static const struct got_error *
7906 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7907 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7909 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7910 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7911 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7913 c42c9805 2020-11-24 stsp *new_view = NULL;
7915 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7916 c42c9805 2020-11-24 stsp if (err) {
7917 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7918 c42c9805 2020-11-24 stsp return err;
7920 c42c9805 2020-11-24 stsp return NULL;
7923 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7924 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7925 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7926 6458efa5 2020-11-24 stsp goto done;
7929 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7930 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7933 6458efa5 2020-11-24 stsp view_close(log_view);
7935 6458efa5 2020-11-24 stsp *new_view = log_view;
7936 c42c9805 2020-11-24 stsp free(commit_id);
7937 6458efa5 2020-11-24 stsp return err;
7940 ce5b7c56 2019-07-09 stsp static void
7941 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7943 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7944 34ba6917 2020-11-30 stsp int i = 0;
7946 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7949 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7950 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7951 34ba6917 2020-11-30 stsp if (re == NULL)
7953 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7954 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7958 9b058f45 2022-06-30 mark static const struct got_error *
7959 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7961 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7962 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7963 6458efa5 2020-11-24 stsp int n = 0;
7965 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7966 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7968 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7970 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7971 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7972 94b80cfa 2022-08-01 mark if (last) {
7973 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7974 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7976 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7977 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7978 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7982 9b058f45 2022-06-30 mark return NULL;
7985 6458efa5 2020-11-24 stsp static const struct got_error *
7986 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7988 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7990 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7991 6458efa5 2020-11-24 stsp return NULL;
7994 6458efa5 2020-11-24 stsp static int
7995 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7997 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7999 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8003 6458efa5 2020-11-24 stsp static const struct got_error *
8004 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8006 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8007 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8009 6458efa5 2020-11-24 stsp if (!view->searching) {
8010 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8011 6458efa5 2020-11-24 stsp return NULL;
8014 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8015 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8016 6458efa5 2020-11-24 stsp if (s->selected_entry)
8017 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8019 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8020 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8022 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8023 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8025 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8026 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8029 487cd7d2 2021-12-17 stsp if (s->selected_entry)
8030 487cd7d2 2021-12-17 stsp re = s->selected_entry;
8031 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
8032 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8034 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8037 6458efa5 2020-11-24 stsp while (1) {
8038 6458efa5 2020-11-24 stsp if (re == NULL) {
8039 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8040 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8041 6458efa5 2020-11-24 stsp return NULL;
8043 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8044 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8046 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8049 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8050 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8051 6458efa5 2020-11-24 stsp s->matched_entry = re;
8055 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8056 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8058 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8061 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8062 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8063 6458efa5 2020-11-24 stsp s->selected = 0;
8066 6458efa5 2020-11-24 stsp return NULL;
8069 6458efa5 2020-11-24 stsp static const struct got_error *
8070 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8072 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8073 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8074 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8075 6458efa5 2020-11-24 stsp char *line = NULL;
8076 6458efa5 2020-11-24 stsp wchar_t *wline;
8077 6458efa5 2020-11-24 stsp struct tog_color *tc;
8078 0ed6bc90 2023-02-01 mark int width, n, scrollx;
8079 6458efa5 2020-11-24 stsp int limit = view->nlines;
8081 6458efa5 2020-11-24 stsp werase(view->window);
8083 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8084 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8085 9b058f45 2022-06-30 mark --limit; /* border */
8087 6458efa5 2020-11-24 stsp if (limit == 0)
8088 6458efa5 2020-11-24 stsp return NULL;
8090 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8092 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8093 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8094 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8096 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8097 6458efa5 2020-11-24 stsp if (err) {
8098 6458efa5 2020-11-24 stsp free(line);
8099 6458efa5 2020-11-24 stsp return err;
8101 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8102 6458efa5 2020-11-24 stsp wstandout(view->window);
8103 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8104 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8105 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8106 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8107 6458efa5 2020-11-24 stsp wstandend(view->window);
8108 6458efa5 2020-11-24 stsp free(wline);
8109 6458efa5 2020-11-24 stsp wline = NULL;
8110 6458efa5 2020-11-24 stsp free(line);
8111 6458efa5 2020-11-24 stsp line = NULL;
8112 6458efa5 2020-11-24 stsp if (--limit <= 0)
8113 6458efa5 2020-11-24 stsp return NULL;
8116 0ed6bc90 2023-02-01 mark view->maxx = 0;
8117 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8118 6458efa5 2020-11-24 stsp char *line = NULL;
8119 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8121 b4996bee 2022-06-16 stsp if (s->show_date) {
8122 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8123 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8124 b4996bee 2022-06-16 stsp struct got_object_id *id;
8125 b4996bee 2022-06-16 stsp struct tm tm;
8128 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8130 b4996bee 2022-06-16 stsp return err;
8131 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8132 b4996bee 2022-06-16 stsp if (err) {
8133 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8135 b4996bee 2022-06-16 stsp return err;
8137 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8139 b4996bee 2022-06-16 stsp if (err) {
8141 b4996bee 2022-06-16 stsp return err;
8143 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8144 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8146 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8147 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8150 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8151 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8152 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8153 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8155 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8156 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8157 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8158 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8159 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8160 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8161 6458efa5 2020-11-24 stsp struct got_object_id *id;
8162 6458efa5 2020-11-24 stsp char *id_str;
8163 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8165 6458efa5 2020-11-24 stsp return err;
8166 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8167 6458efa5 2020-11-24 stsp if (err) {
8169 6458efa5 2020-11-24 stsp return err;
8171 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8172 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8173 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8175 6458efa5 2020-11-24 stsp free(id_str);
8176 6458efa5 2020-11-24 stsp return err;
8179 6458efa5 2020-11-24 stsp free(id_str);
8180 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8181 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8182 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8184 0ed6bc90 2023-02-01 mark /* use full line width to determine view->maxx */
8185 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8186 6458efa5 2020-11-24 stsp if (err) {
8187 6458efa5 2020-11-24 stsp free(line);
8188 6458efa5 2020-11-24 stsp return err;
8190 0ed6bc90 2023-02-01 mark view->maxx = MAX(view->maxx, width);
8191 0ed6bc90 2023-02-01 mark free(wline);
8192 0ed6bc90 2023-02-01 mark wline = NULL;
8194 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
8195 0ed6bc90 2023-02-01 mark view->ncols, 0, 0);
8196 0ed6bc90 2023-02-01 mark if (err) {
8197 0ed6bc90 2023-02-01 mark free(line);
8198 0ed6bc90 2023-02-01 mark return err;
8200 6458efa5 2020-11-24 stsp if (n == s->selected) {
8201 6458efa5 2020-11-24 stsp if (view->focussed)
8202 6458efa5 2020-11-24 stsp wstandout(view->window);
8203 6458efa5 2020-11-24 stsp s->selected_entry = re;
8205 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8207 6458efa5 2020-11-24 stsp wattr_on(view->window,
8208 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8209 0ed6bc90 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
8211 6458efa5 2020-11-24 stsp wattr_off(view->window,
8212 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8213 b637f33f 2023-02-01 mark if (width < view->ncols)
8214 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8215 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8216 6458efa5 2020-11-24 stsp wstandend(view->window);
8217 6458efa5 2020-11-24 stsp free(line);
8218 6458efa5 2020-11-24 stsp free(wline);
8219 6458efa5 2020-11-24 stsp wline = NULL;
8221 6458efa5 2020-11-24 stsp s->ndisplayed++;
8222 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8225 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8228 9b058f45 2022-06-30 mark view_border(view);
8229 6458efa5 2020-11-24 stsp return err;
8232 6458efa5 2020-11-24 stsp static const struct got_error *
8233 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8234 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8236 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8237 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8238 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8240 c42c9805 2020-11-24 stsp *new_view = NULL;
8242 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8243 c42c9805 2020-11-24 stsp if (err) {
8244 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8245 c42c9805 2020-11-24 stsp return err;
8247 c42c9805 2020-11-24 stsp return NULL;
8251 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8252 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8253 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8254 c42c9805 2020-11-24 stsp goto done;
8257 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8258 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8260 c42c9805 2020-11-24 stsp goto done;
8262 c42c9805 2020-11-24 stsp *new_view = tree_view;
8264 c42c9805 2020-11-24 stsp free(commit_id);
8265 c42c9805 2020-11-24 stsp return err;
8268 94b80cfa 2022-08-01 mark static const struct got_error *
8269 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8271 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8272 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8273 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8275 94b80cfa 2022-08-01 mark g = view->gline;
8276 94b80cfa 2022-08-01 mark view->gline = 0;
8278 94b80cfa 2022-08-01 mark if (g == 0)
8280 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8281 94b80cfa 2022-08-01 mark g = s->nrefs;
8283 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8284 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8285 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8286 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8287 94b80cfa 2022-08-01 mark return NULL;
8290 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8291 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8293 94b80cfa 2022-08-01 mark return err;
8294 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8295 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8296 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8297 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8298 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8299 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8301 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8302 94b80cfa 2022-08-01 mark s->selected = g - 1;
8304 94b80cfa 2022-08-01 mark return NULL;
8308 c42c9805 2020-11-24 stsp static const struct got_error *
8309 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8311 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8312 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8313 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8314 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8316 94b80cfa 2022-08-01 mark if (view->gline)
8317 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8319 6458efa5 2020-11-24 stsp switch (ch) {
8322 0ed6bc90 2023-02-01 mark case KEY_RIGHT:
8324 0ed6bc90 2023-02-01 mark case KEY_LEFT:
8326 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8329 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8330 640cd7ff 2022-06-22 mark view->count = 0;
8333 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8334 640cd7ff 2022-06-22 mark view->count = 0;
8337 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8338 3f6c6614 2023-01-23 mark view->action = s->sort_by_date ? "sort by date" : "sort by name";
8339 640cd7ff 2022-06-22 mark view->count = 0;
8340 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8341 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8342 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8345 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8346 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8347 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8350 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8351 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8353 6458efa5 2020-11-24 stsp case KEY_ENTER:
8354 6458efa5 2020-11-24 stsp case '\r':
8355 640cd7ff 2022-06-22 mark view->count = 0;
8356 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8358 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8361 640cd7ff 2022-06-22 mark view->count = 0;
8362 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8364 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8366 e4526bf5 2021-09-03 naddy case 'g':
8368 e4526bf5 2021-09-03 naddy case KEY_HOME:
8369 e4526bf5 2021-09-03 naddy s->selected = 0;
8370 640cd7ff 2022-06-22 mark view->count = 0;
8371 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8373 e4526bf5 2021-09-03 naddy case 'G':
8375 9b058f45 2022-06-30 mark case KEY_END: {
8376 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8378 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8379 9b058f45 2022-06-30 mark --eos; /* border */
8380 e4526bf5 2021-09-03 naddy s->selected = 0;
8381 640cd7ff 2022-06-22 mark view->count = 0;
8382 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8383 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8384 e4526bf5 2021-09-03 naddy if (re == NULL)
8386 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8387 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8389 e4526bf5 2021-09-03 naddy if (n > 0)
8390 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8394 6458efa5 2020-11-24 stsp case KEY_UP:
8395 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8396 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8397 6458efa5 2020-11-24 stsp s->selected--;
8400 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8401 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8402 640cd7ff 2022-06-22 mark view->count = 0;
8404 83cc4199 2022-06-13 stsp case CTRL('u'):
8406 83cc4199 2022-06-13 stsp nscroll /= 2;
8407 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8408 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8409 6458efa5 2020-11-24 stsp case CTRL('b'):
8411 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8412 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8413 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8414 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8415 640cd7ff 2022-06-22 mark view->count = 0;
8418 6458efa5 2020-11-24 stsp case KEY_DOWN:
8419 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8420 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8421 6458efa5 2020-11-24 stsp s->selected++;
8424 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8425 6458efa5 2020-11-24 stsp /* can't scroll any further */
8426 640cd7ff 2022-06-22 mark view->count = 0;
8429 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8431 83cc4199 2022-06-13 stsp case CTRL('d'):
8433 83cc4199 2022-06-13 stsp nscroll /= 2;
8434 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8435 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8436 6458efa5 2020-11-24 stsp case CTRL('f'):
8438 48bb96f0 2022-06-20 naddy case ' ':
8439 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8440 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8441 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8442 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8443 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8444 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8445 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8446 640cd7ff 2022-06-22 mark view->count = 0;
8449 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8451 6458efa5 2020-11-24 stsp case CTRL('l'):
8452 640cd7ff 2022-06-22 mark view->count = 0;
8453 8924d611 2020-12-26 stsp tog_free_refs();
8454 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8457 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8458 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8460 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8461 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8462 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8465 640cd7ff 2022-06-22 mark view->count = 0;
8469 6458efa5 2020-11-24 stsp return err;
8472 6458efa5 2020-11-24 stsp __dead static void
8473 6458efa5 2020-11-24 stsp usage_ref(void)
8476 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8477 6458efa5 2020-11-24 stsp getprogname());
8481 6458efa5 2020-11-24 stsp static const struct got_error *
8482 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8484 6458efa5 2020-11-24 stsp const struct got_error *error;
8485 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8486 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8487 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8489 6458efa5 2020-11-24 stsp struct tog_view *view;
8490 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8492 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8493 6458efa5 2020-11-24 stsp switch (ch) {
8495 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8496 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8497 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8501 e99e2d15 2020-11-24 naddy usage_ref();
8502 6458efa5 2020-11-24 stsp /* NOTREACHED */
8506 6458efa5 2020-11-24 stsp argc -= optind;
8507 6458efa5 2020-11-24 stsp argv += optind;
8509 6458efa5 2020-11-24 stsp if (argc > 1)
8510 e99e2d15 2020-11-24 naddy usage_ref();
8512 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8513 0ae84acc 2022-06-15 tracey if (error != NULL)
8514 0ae84acc 2022-06-15 tracey goto done;
8516 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8517 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8518 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8519 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8520 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8521 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8522 c156c7a4 2020-12-18 stsp goto done;
8523 6458efa5 2020-11-24 stsp if (worktree)
8524 6458efa5 2020-11-24 stsp repo_path =
8525 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8527 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8528 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8529 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8530 c156c7a4 2020-12-18 stsp goto done;
8534 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8535 6458efa5 2020-11-24 stsp if (error != NULL)
8536 6458efa5 2020-11-24 stsp goto done;
8538 6458efa5 2020-11-24 stsp init_curses();
8540 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8541 51a10b52 2020-12-26 stsp if (error)
8542 51a10b52 2020-12-26 stsp goto done;
8544 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8545 6458efa5 2020-11-24 stsp if (error)
8546 6458efa5 2020-11-24 stsp goto done;
8548 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8549 6458efa5 2020-11-24 stsp if (view == NULL) {
8550 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8551 6458efa5 2020-11-24 stsp goto done;
8554 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8555 6458efa5 2020-11-24 stsp if (error)
8556 6458efa5 2020-11-24 stsp goto done;
8558 6458efa5 2020-11-24 stsp if (worktree) {
8559 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8560 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8561 6458efa5 2020-11-24 stsp worktree = NULL;
8563 6458efa5 2020-11-24 stsp error = view_loop(view);
8565 6458efa5 2020-11-24 stsp free(repo_path);
8566 6458efa5 2020-11-24 stsp free(cwd);
8567 1d0f4054 2021-06-17 stsp if (repo) {
8568 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8569 1d0f4054 2021-06-17 stsp if (close_err)
8570 1d0f4054 2021-06-17 stsp error = close_err;
8572 0ae84acc 2022-06-15 tracey if (pack_fds) {
8573 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8574 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8575 0ae84acc 2022-06-15 tracey if (error == NULL)
8576 0ae84acc 2022-06-15 tracey error = pack_err;
8578 51a10b52 2020-12-26 stsp tog_free_refs();
8579 6458efa5 2020-11-24 stsp return error;
8582 ec2a9698 2022-09-15 mark static const struct got_error*
8583 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8584 ec2a9698 2022-09-15 mark const char *str)
8586 ec2a9698 2022-09-15 mark size_t len;
8588 ec2a9698 2022-09-15 mark if (win == NULL)
8589 ec2a9698 2022-09-15 mark win = stdscr;
8591 ec2a9698 2022-09-15 mark len = strlen(str);
8592 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8594 ec2a9698 2022-09-15 mark if (focus)
8595 ec2a9698 2022-09-15 mark wstandout(win);
8596 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8597 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8598 ec2a9698 2022-09-15 mark if (focus)
8599 ec2a9698 2022-09-15 mark wstandend(win);
8601 ec2a9698 2022-09-15 mark return NULL;
8604 136e2bd4 2022-07-23 mark static const struct got_error *
8605 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8609 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8610 ec2a9698 2022-09-15 mark if (p == NULL) {
8611 ec2a9698 2022-09-15 mark free(*line_offsets);
8612 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8613 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8616 ec2a9698 2022-09-15 mark *line_offsets = p;
8617 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8618 ec2a9698 2022-09-15 mark ++(*nlines);
8619 ec2a9698 2022-09-15 mark return NULL;
8622 ec2a9698 2022-09-15 mark static const struct got_error *
8623 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8627 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8628 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8629 ec2a9698 2022-09-15 mark size_t len = 1;
8631 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8634 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8635 ec2a9698 2022-09-15 mark if (t0 == NULL)
8636 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8638 ec2a9698 2022-09-15 mark len += strlen(t);
8639 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8640 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8642 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8645 ec2a9698 2022-09-15 mark return NULL;
8649 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8650 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8651 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8653 ec2a9698 2022-09-15 mark static const struct got_error *
8654 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8656 ec2a9698 2022-09-15 mark int n, len = width;
8658 ec2a9698 2022-09-15 mark if (km->keys) {
8659 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8660 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8661 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8663 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8664 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8666 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8668 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8669 ec2a9698 2022-09-15 mark if (t0 == NULL)
8670 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8672 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8673 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8674 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8675 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8676 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8677 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8678 ec2a9698 2022-09-15 mark if (n < 0) {
8680 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8682 ec2a9698 2022-09-15 mark first = 0;
8683 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8684 ec2a9698 2022-09-15 mark *off += n;
8688 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8689 ec2a9698 2022-09-15 mark if (n < 0)
8690 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8691 ec2a9698 2022-09-15 mark *off += n;
8693 ec2a9698 2022-09-15 mark return NULL;
8696 ec2a9698 2022-09-15 mark static const struct got_error *
8697 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8699 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8700 ec2a9698 2022-09-15 mark off_t off = 0;
8701 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8702 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8703 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8704 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8705 ec2a9698 2022-09-15 mark GENERATE_HELP
8706 ec2a9698 2022-09-15 mark #undef KEYMAP_
8707 ec2a9698 2022-09-15 mark #undef KEY_
8710 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8712 ec2a9698 2022-09-15 mark return err;
8714 ec2a9698 2022-09-15 mark n = nitems(km);
8715 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8717 ec2a9698 2022-09-15 mark return err;
8719 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8720 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8721 ec2a9698 2022-09-15 mark show = s->all;
8722 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8723 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8726 ec2a9698 2022-09-15 mark if (show) {
8727 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8729 ec2a9698 2022-09-15 mark return err;
8730 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8732 ec2a9698 2022-09-15 mark return err;
8735 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8737 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8738 ec2a9698 2022-09-15 mark return err;
8741 ec2a9698 2022-09-15 mark static const struct got_error *
8742 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8745 ec2a9698 2022-09-15 mark const struct got_error *err;
8747 ec2a9698 2022-09-15 mark free(s->line_offsets);
8748 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8749 ec2a9698 2022-09-15 mark s->nlines = 0;
8751 ec2a9698 2022-09-15 mark f = got_opentemp();
8752 ec2a9698 2022-09-15 mark if (f == NULL)
8753 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8756 ec2a9698 2022-09-15 mark err = format_help(s);
8758 ec2a9698 2022-09-15 mark return err;
8760 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8761 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8763 ec2a9698 2022-09-15 mark return NULL;
8766 ec2a9698 2022-09-15 mark static const struct got_error *
8767 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8769 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8770 ec2a9698 2022-09-15 mark return NULL;
8773 eaf2c13e 2022-09-17 mark static void
8774 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8775 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8777 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8779 eaf2c13e 2022-09-17 mark *f = s->f;
8780 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8781 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8782 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8783 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8784 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8785 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8788 ec2a9698 2022-09-15 mark static const struct got_error *
8789 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8791 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8792 ec2a9698 2022-09-15 mark const struct got_error *err;
8793 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8794 ec2a9698 2022-09-15 mark wchar_t *wline;
8795 ec2a9698 2022-09-15 mark char *line;
8796 ec2a9698 2022-09-15 mark ssize_t linelen;
8797 ec2a9698 2022-09-15 mark size_t linesz = 0;
8798 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8799 ec2a9698 2022-09-15 mark int eos = view->nlines;
8801 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8802 ec2a9698 2022-09-15 mark --eos; /* account for border */
8804 ec2a9698 2022-09-15 mark s->lineno = 0;
8805 ec2a9698 2022-09-15 mark rewind(s->f);
8806 ec2a9698 2022-09-15 mark werase(view->window);
8808 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8809 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8811 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8812 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8813 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8815 ec2a9698 2022-09-15 mark return err;
8816 ec2a9698 2022-09-15 mark if (eos <= 1)
8817 ec2a9698 2022-09-15 mark return NULL;
8818 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8821 ec2a9698 2022-09-15 mark s->eof = 0;
8822 ec2a9698 2022-09-15 mark view->maxx = 0;
8823 ec2a9698 2022-09-15 mark line = NULL;
8824 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8825 ec2a9698 2022-09-15 mark attr_t attr = 0;
8827 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8828 ec2a9698 2022-09-15 mark if (linelen == -1) {
8829 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8830 ec2a9698 2022-09-15 mark free(line);
8831 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8833 ec2a9698 2022-09-15 mark s->eof = 1;
8836 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8838 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8840 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8841 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8843 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8844 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8845 ec2a9698 2022-09-15 mark if (err) {
8846 ec2a9698 2022-09-15 mark free(line);
8847 ec2a9698 2022-09-15 mark return err;
8849 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8850 ec2a9698 2022-09-15 mark free(wline);
8851 ec2a9698 2022-09-15 mark wline = NULL;
8854 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8855 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8856 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8857 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8858 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8859 ec2a9698 2022-09-15 mark if (err) {
8860 ec2a9698 2022-09-15 mark free(line);
8861 ec2a9698 2022-09-15 mark return err;
8866 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8867 b637f33f 2023-02-01 mark view->x, view->ncols, 0, view->x ? 1 : 0);
8868 ec2a9698 2022-09-15 mark if (err) {
8869 ec2a9698 2022-09-15 mark free(line);
8870 ec2a9698 2022-09-15 mark return err;
8872 b637f33f 2023-02-01 mark waddwstr(view->window, &wline[skip]);
8873 ec2a9698 2022-09-15 mark free(wline);
8874 ec2a9698 2022-09-15 mark wline = NULL;
8876 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8877 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8878 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8880 b637f33f 2023-02-01 mark if (width < view->ncols)
8881 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8884 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8885 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8886 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8888 ec2a9698 2022-09-15 mark free(line);
8889 ec2a9698 2022-09-15 mark if (nprinted > 0)
8890 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8892 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8894 ec2a9698 2022-09-15 mark view_border(view);
8896 ec2a9698 2022-09-15 mark if (s->eof) {
8897 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8898 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8899 ec2a9698 2022-09-15 mark view->ncols - 1);
8900 ec2a9698 2022-09-15 mark if (rc == ERR)
8901 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8903 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8904 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8905 ec2a9698 2022-09-15 mark wstandout(view->window);
8906 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8907 ec2a9698 2022-09-15 mark view->ncols - 1);
8908 ec2a9698 2022-09-15 mark if (rc == ERR)
8909 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8910 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8911 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8912 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8913 ec2a9698 2022-09-15 mark if (rc == ERR)
8914 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8916 ec2a9698 2022-09-15 mark wstandend(view->window);
8919 ec2a9698 2022-09-15 mark return NULL;
8922 ec2a9698 2022-09-15 mark static const struct got_error *
8923 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8925 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8926 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8927 ec2a9698 2022-09-15 mark char *line = NULL;
8928 ec2a9698 2022-09-15 mark ssize_t linelen;
8929 ec2a9698 2022-09-15 mark size_t linesz = 0;
8930 ec2a9698 2022-09-15 mark int eos, nscroll;
8932 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8933 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8934 ec2a9698 2022-09-15 mark --eos; /* border */
8936 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8938 ec2a9698 2022-09-15 mark switch (ch) {
8941 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8943 ec2a9698 2022-09-15 mark case KEY_LEFT:
8945 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8948 ec2a9698 2022-09-15 mark case KEY_HOME:
8949 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8950 ec2a9698 2022-09-15 mark view->count = 0;
8953 ec2a9698 2022-09-15 mark case KEY_END:
8954 ec2a9698 2022-09-15 mark view->count = 0;
8955 ec2a9698 2022-09-15 mark if (s->eof)
8957 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8958 ec2a9698 2022-09-15 mark s->eof = 1;
8961 ec2a9698 2022-09-15 mark case KEY_UP:
8962 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8963 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8965 ec2a9698 2022-09-15 mark view->count = 0;
8967 ec2a9698 2022-09-15 mark case CTRL('u'):
8969 ec2a9698 2022-09-15 mark nscroll /= 2;
8970 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8971 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8972 ec2a9698 2022-09-15 mark case CTRL('b'):
8974 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8975 ec2a9698 2022-09-15 mark view->count = 0;
8978 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8979 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8982 ec2a9698 2022-09-15 mark case KEY_DOWN:
8983 ec2a9698 2022-09-15 mark case CTRL('n'):
8984 ec2a9698 2022-09-15 mark if (!s->eof)
8985 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8987 ec2a9698 2022-09-15 mark view->count = 0;
8989 ec2a9698 2022-09-15 mark case CTRL('d'):
8991 ec2a9698 2022-09-15 mark nscroll /= 2;
8992 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8993 ec2a9698 2022-09-15 mark case KEY_NPAGE:
8994 ec2a9698 2022-09-15 mark case CTRL('f'):
8997 ec2a9698 2022-09-15 mark if (s->eof) {
8998 ec2a9698 2022-09-15 mark view->count = 0;
9001 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
9002 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9003 ec2a9698 2022-09-15 mark s->first_displayed_line++;
9004 ec2a9698 2022-09-15 mark if (linelen == -1) {
9005 ec2a9698 2022-09-15 mark if (feof(s->f))
9006 ec2a9698 2022-09-15 mark s->eof = 1;
9008 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
9012 ec2a9698 2022-09-15 mark free(line);
9015 ec2a9698 2022-09-15 mark view->count = 0;
9019 ec2a9698 2022-09-15 mark return err;
9022 ec2a9698 2022-09-15 mark static const struct got_error *
9023 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
9025 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9027 ec2a9698 2022-09-15 mark free(s->line_offsets);
9028 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9029 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
9030 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9032 ec2a9698 2022-09-15 mark return NULL;
9035 ec2a9698 2022-09-15 mark static const struct got_error *
9036 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
9038 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9041 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
9042 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9044 ec2a9698 2022-09-15 mark wclear(view->window);
9045 ec2a9698 2022-09-15 mark view->count = 0;
9046 ec2a9698 2022-09-15 mark view->x = 0;
9047 ec2a9698 2022-09-15 mark s->all = !s->all;
9048 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9049 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9050 ec2a9698 2022-09-15 mark s->matched_line = 0;
9052 ec2a9698 2022-09-15 mark return create_help(s);
9055 ec2a9698 2022-09-15 mark static const struct got_error *
9056 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
9058 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9059 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9061 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
9062 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9063 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9064 ec2a9698 2022-09-15 mark s->selected_line = 1;
9066 ec2a9698 2022-09-15 mark view->show = show_help_view;
9067 ec2a9698 2022-09-15 mark view->input = input_help_view;
9068 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
9069 ec2a9698 2022-09-15 mark view->close = close_help_view;
9070 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
9071 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
9072 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
9074 ec2a9698 2022-09-15 mark err = create_help(s);
9075 ec2a9698 2022-09-15 mark return err;
9078 ec2a9698 2022-09-15 mark static const struct got_error *
9079 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9080 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
9082 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
9084 136e2bd4 2022-07-23 mark *new_view = NULL;
9086 136e2bd4 2022-07-23 mark switch (request) {
9087 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
9088 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9089 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9091 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9092 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9093 136e2bd4 2022-07-23 mark view, s->repo);
9095 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9096 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9098 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9099 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9100 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9102 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9103 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9106 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9107 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9109 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9110 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9111 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9112 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9113 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9114 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9115 136e2bd4 2022-07-23 mark &view->state.tree);
9116 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9117 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9118 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9119 136e2bd4 2022-07-23 mark view->state.ref.repo);
9121 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9122 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9124 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9125 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9126 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9127 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9128 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9129 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9130 136e2bd4 2022-07-23 mark view->state.log.repo);
9131 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9132 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9133 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9134 136e2bd4 2022-07-23 mark view->state.ref.repo);
9136 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9137 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9139 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9140 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9141 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9142 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9143 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9144 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9145 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9146 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9148 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9149 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9151 136e2bd4 2022-07-23 mark view_close(*new_view);
9153 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9154 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9155 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9156 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9157 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9159 ec2a9698 2022-09-15 mark view_close(*new_view);
9162 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9165 136e2bd4 2022-07-23 mark return err;
9169 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9170 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9172 6458efa5 2020-11-24 stsp static void
9173 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9175 9b058f45 2022-06-30 mark switch (view->type) {
9176 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9177 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9178 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9179 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9183 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9184 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9186 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9187 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9190 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9191 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9192 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9194 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9195 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9196 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9198 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9199 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9200 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9206 9b058f45 2022-06-30 mark view->offset = 0;
9210 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9211 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9213 9b058f45 2022-06-30 mark static const struct got_error *
9214 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9216 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9217 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9218 9b058f45 2022-06-30 mark int *selected = NULL;
9219 9b058f45 2022-06-30 mark int header, offset;
9221 9b058f45 2022-06-30 mark switch (view->type) {
9222 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9223 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9224 9b058f45 2022-06-30 mark header = 3;
9225 9b058f45 2022-06-30 mark scrolld = NULL;
9226 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9227 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9228 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9229 9b058f45 2022-06-30 mark s->selected_line -= offset;
9230 9b058f45 2022-06-30 mark view->offset = offset;
9234 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9235 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9236 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9237 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9238 9b058f45 2022-06-30 mark selected = &s->selected;
9241 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9242 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9243 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9244 9b058f45 2022-06-30 mark header = 3;
9245 9b058f45 2022-06-30 mark selected = &s->selected;
9248 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9249 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9250 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9251 9b058f45 2022-06-30 mark header = 5;
9252 9b058f45 2022-06-30 mark selected = &s->selected;
9256 9b058f45 2022-06-30 mark selected = NULL;
9257 9b058f45 2022-06-30 mark scrolld = NULL;
9258 9b058f45 2022-06-30 mark header = 0;
9262 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9263 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9264 9b058f45 2022-06-30 mark view->offset = offset;
9265 9b058f45 2022-06-30 mark if (scrolld && offset) {
9266 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9267 9b058f45 2022-06-30 mark *selected -= offset;
9271 9b058f45 2022-06-30 mark return err;
9274 9b058f45 2022-06-30 mark static void
9275 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9279 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9280 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9281 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9282 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9284 6879ba42 2020-10-01 naddy fputc('\n', fp);
9287 4ed7e80c 2018-05-20 stsp __dead static void
9288 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9290 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9292 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9293 6879ba42 2020-10-01 naddy getprogname());
9294 6879ba42 2020-10-01 naddy if (hflag) {
9295 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9296 6879ba42 2020-10-01 naddy list_commands(fp);
9298 6879ba42 2020-10-01 naddy exit(status);
9301 c2301be8 2018-04-30 stsp static char **
9302 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9304 ee85c5e8 2020-02-29 stsp va_list ap;
9305 c2301be8 2018-04-30 stsp char **argv;
9308 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9310 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9311 c2301be8 2018-04-30 stsp if (argv == NULL)
9312 c2301be8 2018-04-30 stsp err(1, "calloc");
9313 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9314 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9315 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9316 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9319 ee85c5e8 2020-02-29 stsp va_end(ap);
9320 c2301be8 2018-04-30 stsp return argv;
9324 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9325 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9326 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9327 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9328 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9330 ee85c5e8 2020-02-29 stsp static const struct got_error *
9331 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9333 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9334 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9335 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9336 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9337 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9338 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9339 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9340 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9341 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9343 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9344 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9345 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9347 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9348 0ae84acc 2022-06-15 tracey if (error != NULL)
9349 0ae84acc 2022-06-15 tracey goto done;
9351 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9352 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9353 ee85c5e8 2020-02-29 stsp goto done;
9355 ee85c5e8 2020-02-29 stsp if (worktree)
9356 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9358 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9359 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9360 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9361 ee85c5e8 2020-02-29 stsp goto done;
9364 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9365 ee85c5e8 2020-02-29 stsp if (error != NULL)
9366 ee85c5e8 2020-02-29 stsp goto done;
9368 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9369 ee85c5e8 2020-02-29 stsp repo, worktree);
9370 ee85c5e8 2020-02-29 stsp if (error)
9371 ee85c5e8 2020-02-29 stsp goto done;
9373 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9374 84de9106 2020-12-26 stsp if (error)
9375 84de9106 2020-12-26 stsp goto done;
9376 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9377 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9378 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9379 ee85c5e8 2020-02-29 stsp if (error)
9380 ee85c5e8 2020-02-29 stsp goto done;
9382 ee85c5e8 2020-02-29 stsp if (worktree) {
9383 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9384 ee85c5e8 2020-02-29 stsp worktree = NULL;
9387 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9388 a44927cc 2022-04-07 stsp if (error)
9389 a44927cc 2022-04-07 stsp goto done;
9391 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9392 ee85c5e8 2020-02-29 stsp if (error) {
9393 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9394 ee85c5e8 2020-02-29 stsp goto done;
9395 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9396 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9397 6879ba42 2020-10-01 naddy usage(1, 1);
9398 ee85c5e8 2020-02-29 stsp /* not reached */
9401 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9402 ee85c5e8 2020-02-29 stsp if (error)
9403 ee85c5e8 2020-02-29 stsp goto done;
9405 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9407 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9408 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9410 1d0f4054 2021-06-17 stsp if (repo) {
9411 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9412 1d0f4054 2021-06-17 stsp if (error == NULL)
9413 1d0f4054 2021-06-17 stsp error = close_err;
9415 a44927cc 2022-04-07 stsp if (commit)
9416 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9417 ee85c5e8 2020-02-29 stsp if (worktree)
9418 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9419 0ae84acc 2022-06-15 tracey if (pack_fds) {
9420 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9421 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9422 0ae84acc 2022-06-15 tracey if (error == NULL)
9423 0ae84acc 2022-06-15 tracey error = pack_err;
9426 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9427 ee85c5e8 2020-02-29 stsp free(commit_id);
9428 ee85c5e8 2020-02-29 stsp free(cwd);
9429 ee85c5e8 2020-02-29 stsp free(repo_path);
9430 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9431 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9433 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9434 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9435 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9437 87670572 2020-12-26 naddy tog_free_refs();
9438 ee85c5e8 2020-02-29 stsp return error;
9442 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9444 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9445 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9446 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9447 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9448 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9449 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9450 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9452 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9454 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9455 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9457 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9459 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9460 9f7d7167 2018-04-29 stsp switch (ch) {
9462 9f7d7167 2018-04-29 stsp hflag = 1;
9465 53ccebc2 2019-07-30 stsp Vflag = 1;
9468 6879ba42 2020-10-01 naddy usage(hflag, 1);
9469 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9473 9f7d7167 2018-04-29 stsp argc -= optind;
9474 9f7d7167 2018-04-29 stsp argv += optind;
9475 9814e6a3 2020-09-27 naddy optind = 1;
9476 c2301be8 2018-04-30 stsp optreset = 1;
9478 53ccebc2 2019-07-30 stsp if (Vflag) {
9479 53ccebc2 2019-07-30 stsp got_version_print_str();
9480 6879ba42 2020-10-01 naddy return 0;
9483 4010e238 2020-12-04 stsp #ifndef PROFILE
9484 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9485 4010e238 2020-12-04 stsp NULL) == -1)
9486 4010e238 2020-12-04 stsp err(1, "pledge");
9489 c2301be8 2018-04-30 stsp if (argc == 0) {
9490 f29d3e89 2018-06-23 stsp if (hflag)
9491 6879ba42 2020-10-01 naddy usage(hflag, 0);
9492 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9493 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9495 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9499 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9500 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9501 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9502 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9503 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9509 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9510 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9511 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9512 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9513 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9514 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9517 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9518 ee85c5e8 2020-02-29 stsp if (argc != 1)
9519 6879ba42 2020-10-01 naddy usage(0, 1);
9520 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9521 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9523 ee85c5e8 2020-02-29 stsp if (hflag)
9524 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9526 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9530 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9532 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9533 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9534 a2f4a359 2020-02-28 stsp free(cmd_argv);
9537 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9538 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9539 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9540 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9541 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9542 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);