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 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
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 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1325 2b49a8ae 2019-06-22 stsp return NULL;
1327 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1328 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1329 7c32bd05 2019-06-22 stsp if (err) {
1330 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1331 7c32bd05 2019-06-22 stsp return err;
1333 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1334 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1335 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1336 2b49a8ae 2019-06-22 stsp view->search_next(view);
1339 2b49a8ae 2019-06-22 stsp return NULL;
1342 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1343 d2366e29 2022-07-07 mark static const struct got_error *
1344 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1346 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1347 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1349 d2366e29 2022-07-07 mark if (view->parent)
1350 d2366e29 2022-07-07 mark v = view->parent;
1354 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1355 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1357 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1359 7532ccda 2022-07-11 mark if (!v->child)
1360 7532ccda 2022-07-11 mark return NULL;
1361 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1362 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1364 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1365 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1366 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1367 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1368 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1371 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1372 d2366e29 2022-07-07 mark v->ncols = COLS;
1373 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1374 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1376 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1378 d2366e29 2022-07-07 mark return err;
1380 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1381 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1382 d2366e29 2022-07-07 mark v->focus_child = 1;
1384 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1386 d2366e29 2022-07-07 mark return err;
1387 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1389 d2366e29 2022-07-07 mark return err;
1391 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1392 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1393 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1394 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1396 279d2047 2022-07-29 stsp return err;
1397 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1399 279d2047 2022-07-29 stsp return err;
1401 7532ccda 2022-07-11 mark offset_selection_up(v);
1402 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1404 dff91825 2022-07-22 mark if (v->resize)
1405 dff91825 2022-07-22 mark err = v->resize(v, 0);
1406 dff91825 2022-07-22 mark else if (v->child->resize)
1407 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1409 d2366e29 2022-07-07 mark return err;
1413 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1414 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1416 640cd7ff 2022-06-22 mark static int
1417 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1419 9b058f45 2022-06-30 mark struct tog_view *v = view;
1420 9b058f45 2022-06-30 mark int x, n = 0;
1422 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1423 9b058f45 2022-06-30 mark v = view->child;
1424 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1425 9b058f45 2022-06-30 mark v = view->parent;
1427 640cd7ff 2022-06-22 mark view->count = 0;
1428 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1429 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1430 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1431 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1432 9b058f45 2022-06-30 mark waddch(v->window, ':');
1435 9b058f45 2022-06-30 mark x = getcurx(v->window);
1436 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1437 9b058f45 2022-06-30 mark waddch(v->window, c);
1438 9b058f45 2022-06-30 mark wrefresh(v->window);
1442 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1443 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1445 640cd7ff 2022-06-22 mark if (n >= 9999999)
1446 640cd7ff 2022-06-22 mark n = 9999999;
1448 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1449 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1451 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1452 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1457 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1458 640cd7ff 2022-06-22 mark view->count = n;
1463 3f6c6614 2023-01-23 mark static void
1464 3f6c6614 2023-01-23 mark action_report(struct tog_view *view)
1466 3f6c6614 2023-01-23 mark struct tog_view *v = view;
1468 3f6c6614 2023-01-23 mark if (view_is_hsplit_top(view))
1469 3f6c6614 2023-01-23 mark v = view->child;
1470 3f6c6614 2023-01-23 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1471 3f6c6614 2023-01-23 mark v = view->parent;
1473 3f6c6614 2023-01-23 mark wmove(v->window, v->nlines - 1, 0);
1474 3f6c6614 2023-01-23 mark wclrtoeol(v->window);
1475 3f6c6614 2023-01-23 mark wprintw(v->window, ":%s", view->action);
1476 3f6c6614 2023-01-23 mark wrefresh(v->window);
1479 3f6c6614 2023-01-23 mark * Clear action status report. Only clear in blame view
1480 3f6c6614 2023-01-23 mark * once annotating is complete, otherwise it's too fast.
1482 3f6c6614 2023-01-23 mark if (view->type == TOG_VIEW_BLAME) {
1483 3f6c6614 2023-01-23 mark if (view->state.blame.blame_complete)
1484 3f6c6614 2023-01-23 mark view->action = NULL;
1486 3f6c6614 2023-01-23 mark view->action = NULL;
1489 0cf4efb1 2018-09-29 stsp static const struct got_error *
1490 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1491 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1493 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1494 669b5ffa 2018-10-07 stsp struct tog_view *v;
1495 1a76625f 2018-10-22 stsp int ch, errcode;
1497 e5a0f69f 2018-08-18 stsp *new = NULL;
1499 3f6c6614 2023-01-23 mark if (view->action)
1500 3f6c6614 2023-01-23 mark action_report(view);
1502 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1503 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1504 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1505 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1506 640cd7ff 2022-06-22 mark view->count = 0;
1509 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1510 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1511 82954512 2020-02-03 stsp if (errcode)
1512 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1513 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1514 3da8ef85 2021-09-21 stsp sched_yield();
1515 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1516 82954512 2020-02-03 stsp if (errcode)
1517 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1518 82954512 2020-02-03 stsp "pthread_mutex_lock");
1519 60493ae3 2019-06-20 stsp view->search_next(view);
1520 60493ae3 2019-06-20 stsp return NULL;
1523 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1524 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1525 1a76625f 2018-10-22 stsp if (errcode)
1526 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1527 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1528 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1530 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1531 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1532 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1533 a6d37fac 2022-07-03 mark view->count = 0;
1535 a6d37fac 2022-07-03 mark ch = view->ch;
1537 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1538 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1539 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1541 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1542 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1543 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1544 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1545 1a76625f 2018-10-22 stsp if (errcode)
1546 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1548 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1549 25791caa 2018-10-24 stsp tog_resizeterm();
1550 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1551 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1552 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1553 25791caa 2018-10-24 stsp err = view_resize(v);
1555 25791caa 2018-10-24 stsp return err;
1556 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1558 25791caa 2018-10-24 stsp return err;
1559 cdfcfb03 2020-12-06 stsp if (v->child) {
1560 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1562 cdfcfb03 2020-12-06 stsp return err;
1563 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1564 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1566 cdfcfb03 2020-12-06 stsp return err;
1567 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1568 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1570 3c1dfe12 2022-07-08 mark return err;
1576 e5a0f69f 2018-08-18 stsp switch (ch) {
1579 ec2a9698 2022-09-15 mark case KEY_F(1):
1580 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1581 ec2a9698 2022-09-15 mark err = view->reset(view);
1583 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1586 640cd7ff 2022-06-22 mark view->count = 0;
1587 1e37a5c2 2019-05-12 jcs if (view->child) {
1588 e78dc838 2020-12-04 stsp view->focussed = 0;
1589 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1590 e78dc838 2020-12-04 stsp view->focus_child = 1;
1591 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1592 e78dc838 2020-12-04 stsp view->focussed = 0;
1593 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1594 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1595 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1596 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1597 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1600 9b058f45 2022-06-30 mark return err;
1602 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1603 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1605 9b058f45 2022-06-30 mark return err;
1610 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1611 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1612 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1613 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1617 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1619 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1620 9970f7fc 2020-12-03 stsp view->dying = 1;
1626 640cd7ff 2022-06-22 mark view->count = 0;
1627 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1628 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1630 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1631 e78dc838 2020-12-04 stsp view->focussed = 0;
1632 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1633 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1635 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1637 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1641 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1642 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1644 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1645 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1646 e78dc838 2020-12-04 stsp view->focussed = 1;
1647 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1649 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1650 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1651 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1653 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1657 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1661 6fe51fee 2022-07-22 mark if (view->resize) {
1662 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1666 9b058f45 2022-06-30 mark if (view->parent)
1667 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1669 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1672 3c1dfe12 2022-07-08 mark view->count = 0;
1673 d2366e29 2022-07-07 mark err = switch_split(view);
1676 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1679 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1681 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1684 640cd7ff 2022-06-22 mark view->count = 0;
1685 60493ae3 2019-06-20 stsp if (view->search_start)
1686 2b49a8ae 2019-06-22 stsp view_search_start(view);
1688 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1692 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1693 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1694 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1695 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1696 60493ae3 2019-06-20 stsp view->search_next(view);
1698 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1701 3f6c6614 2023-01-23 mark if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1702 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1703 3f6c6614 2023-01-23 mark view->action = "Patience diff algorithm";
1705 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1706 3f6c6614 2023-01-23 mark view->action = "Myers diff algorithm";
1708 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1709 917d79a7 2022-07-01 stsp if (v->reset) {
1710 917d79a7 2022-07-01 stsp err = v->reset(v);
1712 917d79a7 2022-07-01 stsp return err;
1714 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1715 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1717 917d79a7 2022-07-01 stsp return err;
1722 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1726 e5a0f69f 2018-08-18 stsp return err;
1730 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1732 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1733 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1735 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1737 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1740 669b5ffa 2018-10-07 stsp return view->focussed;
1743 bcbd79e2 2018-08-19 stsp static const struct got_error *
1744 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1746 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1747 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1748 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1749 d2366e29 2022-07-07 mark char *mode;
1750 fd823528 2018-10-22 stsp int fast_refresh = 10;
1751 1a76625f 2018-10-22 stsp int done = 0, errcode;
1753 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1754 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1755 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1757 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1759 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1760 1a76625f 2018-10-22 stsp if (errcode)
1761 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1763 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1764 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1766 1004088d 2018-09-29 stsp view->focussed = 1;
1767 878940b7 2018-09-29 stsp err = view->show(view);
1769 0cf4efb1 2018-09-29 stsp return err;
1770 0cf4efb1 2018-09-29 stsp update_panels();
1771 0cf4efb1 2018-09-29 stsp doupdate();
1772 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1773 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1774 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1775 388b24d6 2023-01-23 mark if (fast_refresh && --fast_refresh == 0)
1776 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1778 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1781 9970f7fc 2020-12-03 stsp if (view->dying) {
1782 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1784 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1785 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1787 e78dc838 2020-12-04 stsp else if (view->parent)
1788 669b5ffa 2018-10-07 stsp prev = view->parent;
1790 e78dc838 2020-12-04 stsp if (view->parent) {
1791 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1792 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1793 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1794 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1795 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1798 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1799 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1801 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1803 9970f7fc 2020-12-03 stsp err = view_close(view);
1805 e5a0f69f 2018-08-18 stsp goto done;
1807 e78dc838 2020-12-04 stsp view = NULL;
1808 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1809 e78dc838 2020-12-04 stsp if (v->focussed)
1812 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1813 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1815 e78dc838 2020-12-04 stsp view = prev;
1816 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1817 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1818 e78dc838 2020-12-04 stsp tog_view_list_head);
1820 e78dc838 2020-12-04 stsp if (view) {
1821 e78dc838 2020-12-04 stsp if (view->focus_child) {
1822 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1823 e78dc838 2020-12-04 stsp view = view->child;
1825 e78dc838 2020-12-04 stsp view->focussed = 1;
1829 bcbd79e2 2018-08-19 stsp if (new_view) {
1830 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1831 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1832 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1833 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1835 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1836 86c66b02 2018-10-18 stsp err = view_close(v);
1838 86c66b02 2018-10-18 stsp goto done;
1841 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1842 fed7eaa8 2018-10-24 stsp view = new_view;
1844 669b5ffa 2018-10-07 stsp if (view) {
1845 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1846 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1847 e78dc838 2020-12-04 stsp view = view->child;
1849 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1850 e78dc838 2020-12-04 stsp view = view->parent;
1852 e78dc838 2020-12-04 stsp show_panel(view->panel);
1853 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1854 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1855 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1856 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1858 1a76625f 2018-10-22 stsp goto done;
1860 669b5ffa 2018-10-07 stsp err = view->show(view);
1862 1a76625f 2018-10-22 stsp goto done;
1863 669b5ffa 2018-10-07 stsp if (view->child) {
1864 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1866 1a76625f 2018-10-22 stsp goto done;
1868 1a76625f 2018-10-22 stsp update_panels();
1869 1a76625f 2018-10-22 stsp doupdate();
1873 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1874 5629093a 2022-07-11 stsp const struct got_error *close_err;
1875 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1876 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1877 5629093a 2022-07-11 stsp close_err = view_close(view);
1878 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1879 5629093a 2022-07-11 stsp err = close_err;
1882 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1883 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1884 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1886 e5a0f69f 2018-08-18 stsp return err;
1889 4ed7e80c 2018-05-20 stsp __dead static void
1890 9f7d7167 2018-04-29 stsp usage_log(void)
1893 c70c5802 2018-08-01 stsp fprintf(stderr,
1894 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1895 9f7d7167 2018-04-29 stsp getprogname());
1899 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1900 80ddbec8 2018-04-29 stsp static const struct got_error *
1901 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1903 00dfcb92 2018-06-11 stsp char *vis = NULL;
1904 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1906 963b370f 2018-05-20 stsp *ws = NULL;
1907 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1908 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1909 00dfcb92 2018-06-11 stsp int vislen;
1910 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1911 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1913 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1914 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1916 00dfcb92 2018-06-11 stsp return err;
1917 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1918 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1919 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1920 a7f50699 2018-06-11 stsp goto done;
1924 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1925 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1926 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1927 a7f50699 2018-06-11 stsp goto done;
1930 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1931 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1933 00dfcb92 2018-06-11 stsp free(vis);
1934 963b370f 2018-05-20 stsp if (err) {
1935 963b370f 2018-05-20 stsp free(*ws);
1936 963b370f 2018-05-20 stsp *ws = NULL;
1937 963b370f 2018-05-20 stsp *wlen = 0;
1939 963b370f 2018-05-20 stsp return err;
1942 145b6838 2022-06-16 stsp static const struct got_error *
1943 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1945 145b6838 2022-06-16 stsp char *dst;
1946 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1948 145b6838 2022-06-16 stsp *ptr = NULL;
1949 145b6838 2022-06-16 stsp n = len = strlen(src);
1950 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1951 145b6838 2022-06-16 stsp if (dst == NULL)
1952 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1954 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1955 145b6838 2022-06-16 stsp const char c = src[idx];
1957 145b6838 2022-06-16 stsp if (c == '\t') {
1958 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1961 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1962 6e1c41ad 2022-06-16 mark if (p == NULL) {
1963 6e1c41ad 2022-06-16 mark free(dst);
1964 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1969 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1972 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1976 145b6838 2022-06-16 stsp dst[sz] = '\0';
1977 145b6838 2022-06-16 stsp *ptr = dst;
1978 145b6838 2022-06-16 stsp return NULL;
1982 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1983 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1984 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1988 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1990 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1992 4e4a9ac8 2022-06-17 op if (n == 0) {
1993 4e4a9ac8 2022-06-17 op *rcol = cols;
1997 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1998 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1999 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2001 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
2003 4e4a9ac8 2022-06-17 op if (width == -1) {
2005 4e4a9ac8 2022-06-17 op wline[i] = L'.';
2008 4e4a9ac8 2022-06-17 op if (cols + width > n)
2010 4e4a9ac8 2022-06-17 op cols += width;
2013 4e4a9ac8 2022-06-17 op *rcol = cols;
2018 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
2019 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
2020 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2022 ccda2f4d 2022-06-16 stsp static const struct got_error *
2023 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2024 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2026 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2028 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2029 145b6838 2022-06-16 stsp char *exstr = NULL;
2030 963b370f 2018-05-20 stsp size_t wlen;
2031 4e4a9ac8 2022-06-17 op int i, scrollx;
2033 963b370f 2018-05-20 stsp *wlinep = NULL;
2034 b700b5d6 2018-07-10 stsp *widthp = 0;
2036 145b6838 2022-06-16 stsp if (expand) {
2037 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2039 145b6838 2022-06-16 stsp return err;
2042 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2043 145b6838 2022-06-16 stsp free(exstr);
2045 963b370f 2018-05-20 stsp return err;
2047 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2049 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2050 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2053 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2054 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2058 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2059 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2061 b700b5d6 2018-07-10 stsp if (widthp)
2062 b700b5d6 2018-07-10 stsp *widthp = cols;
2063 ccda2f4d 2022-06-16 stsp if (scrollxp)
2064 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2066 963b370f 2018-05-20 stsp free(wline);
2068 963b370f 2018-05-20 stsp *wlinep = wline;
2069 963b370f 2018-05-20 stsp return err;
2072 8b473291 2019-02-21 stsp static const struct got_error*
2073 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2074 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2076 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2077 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2079 8b473291 2019-02-21 stsp const char *name;
2081 8b473291 2019-02-21 stsp *refs_str = NULL;
2083 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2084 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2085 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2088 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2089 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2091 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2092 8b473291 2019-02-21 stsp name += 5;
2093 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2094 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2096 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2097 8b473291 2019-02-21 stsp name += 6;
2098 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2099 8b473291 2019-02-21 stsp name += 8;
2100 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2101 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2104 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2107 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2108 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2109 5d844a1e 2019-08-13 stsp if (err) {
2110 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2111 48cae60d 2020-09-22 stsp free(ref_id);
2114 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2115 5d844a1e 2019-08-13 stsp err = NULL;
2116 5d844a1e 2019-08-13 stsp tag = NULL;
2119 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2120 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2121 48cae60d 2020-09-22 stsp free(ref_id);
2123 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2124 52b5abe1 2019-08-13 stsp if (cmp != 0)
2126 8b473291 2019-02-21 stsp s = *refs_str;
2127 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2128 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2129 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2131 8b473291 2019-02-21 stsp *refs_str = NULL;
2137 8b473291 2019-02-21 stsp return err;
2140 963b370f 2018-05-20 stsp static const struct got_error *
2141 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2142 27a741e5 2019-09-11 stsp int col_tab_align)
2144 e6b8b890 2020-12-29 naddy char *smallerthan;
2146 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2147 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2148 5813d178 2019-03-09 stsp author = smallerthan + 1;
2149 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2150 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2151 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2154 5813d178 2019-03-09 stsp static const struct got_error *
2155 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2156 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2157 8fdc79fe 2020-12-01 naddy int author_display_cols)
2159 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2160 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2161 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2162 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2163 5813d178 2019-03-09 stsp char *author = NULL;
2164 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2165 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2166 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2167 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2168 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2169 ccb26ccd 2018-11-05 stsp struct tm tm;
2170 45d799e2 2018-12-23 stsp time_t committer_time;
2171 11b20872 2019-11-08 stsp struct tog_color *tc;
2173 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2174 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2175 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2176 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2177 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2179 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2180 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2182 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2183 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2185 11b20872 2019-11-08 stsp wattr_on(view->window,
2186 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2187 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2189 11b20872 2019-11-08 stsp wattr_off(view->window,
2190 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2191 27a741e5 2019-09-11 stsp col = limit;
2192 b39d25c7 2018-07-10 stsp if (col > avail)
2193 b39d25c7 2018-07-10 stsp goto done;
2195 6570a66d 2019-11-08 stsp if (avail >= 120) {
2196 6570a66d 2019-11-08 stsp char *id_str;
2197 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2199 6570a66d 2019-11-08 stsp goto done;
2200 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2202 11b20872 2019-11-08 stsp wattr_on(view->window,
2203 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2204 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2206 11b20872 2019-11-08 stsp wattr_off(view->window,
2207 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2208 6570a66d 2019-11-08 stsp free(id_str);
2210 6570a66d 2019-11-08 stsp if (col > avail)
2211 6570a66d 2019-11-08 stsp goto done;
2214 10aab77f 2022-07-19 op if (s->use_committer)
2215 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2217 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2218 5813d178 2019-03-09 stsp if (author == NULL) {
2219 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2220 80ddbec8 2018-04-29 stsp goto done;
2222 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2224 bb737323 2018-05-20 stsp goto done;
2225 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2227 11b20872 2019-11-08 stsp wattr_on(view->window,
2228 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2229 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2230 bb737323 2018-05-20 stsp col += author_width;
2231 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2232 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2234 bb737323 2018-05-20 stsp author_width++;
2237 f0f62654 2022-09-08 mark wattr_off(view->window,
2238 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2239 9c2eaf34 2018-05-20 stsp if (col > avail)
2240 9c2eaf34 2018-05-20 stsp goto done;
2242 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2244 6d9fbc00 2018-04-29 stsp goto done;
2245 bb737323 2018-05-20 stsp logmsg = logmsg0;
2246 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2248 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2249 bb737323 2018-05-20 stsp if (newline)
2250 bb737323 2018-05-20 stsp *newline = '\0';
2251 ccda2f4d 2022-06-16 stsp limit = avail - col;
2252 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2253 4d1f6af3 2022-06-17 op limit--; /* for the border */
2254 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2255 ccda2f4d 2022-06-16 stsp limit, col, 1);
2257 29688b02 2022-06-16 stsp goto done;
2258 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2259 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2260 27a741e5 2019-09-11 stsp while (col < avail) {
2261 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2265 80ddbec8 2018-04-29 stsp free(logmsg0);
2266 bb737323 2018-05-20 stsp free(wlogmsg);
2267 5813d178 2019-03-09 stsp free(author);
2268 bb737323 2018-05-20 stsp free(wauthor);
2269 80ddbec8 2018-04-29 stsp free(line);
2270 80ddbec8 2018-04-29 stsp return err;
2273 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2274 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2275 899d86c2 2018-05-10 stsp struct got_object_id *id)
2277 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2278 e15c42de 2022-09-05 op struct got_object_id *dup;
2280 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2281 80ddbec8 2018-04-29 stsp if (entry == NULL)
2282 899d86c2 2018-05-10 stsp return NULL;
2284 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2285 e15c42de 2022-09-05 op if (dup == NULL) {
2286 e15c42de 2022-09-05 op free(entry);
2287 e15c42de 2022-09-05 op return NULL;
2290 e15c42de 2022-09-05 op entry->id = dup;
2291 99db9666 2018-05-07 stsp entry->commit = commit;
2292 899d86c2 2018-05-10 stsp return entry;
2295 99db9666 2018-05-07 stsp static void
2296 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2298 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2300 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2301 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2302 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2303 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2304 e15c42de 2022-09-05 op free(entry->id);
2305 99db9666 2018-05-07 stsp free(entry);
2308 99db9666 2018-05-07 stsp static void
2309 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2311 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2312 99db9666 2018-05-07 stsp pop_commit(commits);
2315 c4972b91 2018-05-07 stsp static const struct got_error *
2316 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2317 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2319 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2320 13add988 2019-10-15 stsp regmatch_t regmatch;
2321 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2323 13add988 2019-10-15 stsp *have_match = 0;
2325 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2327 13add988 2019-10-15 stsp return err;
2329 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2331 13add988 2019-10-15 stsp goto done;
2333 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2334 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2335 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2336 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2337 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2338 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2339 13add988 2019-10-15 stsp *have_match = 1;
2341 13add988 2019-10-15 stsp free(id_str);
2342 13add988 2019-10-15 stsp free(logmsg);
2343 13add988 2019-10-15 stsp return err;
2346 13add988 2019-10-15 stsp static const struct got_error *
2347 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2349 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2352 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2353 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2354 1a76625f 2018-10-22 stsp * while updating the display.
2357 d9787ed8 2022-09-10 op struct got_object_id id;
2358 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2359 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2360 568eae95 2022-09-11 mark int limit_match = 0;
2361 1a76625f 2018-10-22 stsp int errcode;
2363 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2364 4e0d2870 2020-12-07 naddy NULL, NULL);
2368 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2371 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2372 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2373 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2377 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2378 1a76625f 2018-10-22 stsp if (errcode) {
2379 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2380 13add988 2019-10-15 stsp "pthread_mutex_lock");
2384 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2385 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2386 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2388 568eae95 2022-09-11 mark if (*a->limiting) {
2389 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2390 568eae95 2022-09-11 mark a->limit_regex);
2394 568eae95 2022-09-11 mark if (limit_match) {
2395 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2397 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2398 568eae95 2022-09-11 mark entry->commit, entry->id);
2399 568eae95 2022-09-11 mark if (matched == NULL) {
2400 568eae95 2022-09-11 mark err = got_error_from_errno(
2401 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2404 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2405 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2407 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2408 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2409 568eae95 2022-09-11 mark matched, entry);
2410 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2414 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2415 568eae95 2022-09-11 mark * have found a match, and that it should be
2416 568eae95 2022-09-11 mark * counted as a new entry for the view.
2418 568eae95 2022-09-11 mark a->limit_match = limit_match;
2421 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2422 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2423 7c1452c1 2020-03-26 stsp int have_match;
2424 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2428 568eae95 2022-09-11 mark if (*a->limiting) {
2429 568eae95 2022-09-11 mark if (limit_match && have_match)
2430 568eae95 2022-09-11 mark *a->search_next_done =
2431 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2432 568eae95 2022-09-11 mark } else if (have_match)
2433 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2436 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2437 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2438 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2439 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2442 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2444 9ba79e04 2018-06-11 stsp return err;
2447 2b779855 2020-12-05 naddy static void
2448 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2450 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2451 2b779855 2020-12-05 naddy int ncommits = 0;
2453 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2454 2b779855 2020-12-05 naddy while (entry) {
2455 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2456 2b779855 2020-12-05 naddy s->selected_entry = entry;
2459 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2460 2b779855 2020-12-05 naddy ncommits++;
2464 0553a4e3 2018-04-30 stsp static const struct got_error *
2465 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2467 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2468 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2469 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2470 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2471 60493ae3 2019-06-20 stsp int width;
2472 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2473 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2474 8b473291 2019-02-21 stsp char *refs_str = NULL;
2475 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2476 11b20872 2019-11-08 stsp struct tog_color *tc;
2477 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2479 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2480 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2482 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2483 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2484 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2485 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2487 ecb28ae0 2018-07-16 stsp return err;
2488 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2489 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2490 d2075bf3 2020-12-25 stsp if (refs) {
2491 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2492 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2494 d2075bf3 2020-12-25 stsp goto done;
2498 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2499 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2501 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2502 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2503 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2504 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2505 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2506 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2507 8f4ed634 2020-03-26 stsp goto done;
2510 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2511 568eae95 2022-09-11 mark const char *limit_str = NULL;
2513 f9686aa5 2020-03-27 stsp if (view->searching) {
2514 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2515 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2516 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2517 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2518 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2519 f9686aa5 2020-03-27 stsp search_str = "searching...";
2522 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2523 568eae95 2022-09-11 mark limit_str = "no matches found";
2525 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2526 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2527 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2528 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2529 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2530 8f4ed634 2020-03-26 stsp goto done;
2534 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2535 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2536 87411fa9 2022-06-16 stsp "........................................",
2537 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2538 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2539 1a76625f 2018-10-22 stsp header = NULL;
2540 1a76625f 2018-10-22 stsp goto done;
2542 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2543 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2544 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2545 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2546 1a76625f 2018-10-22 stsp header = NULL;
2547 1a76625f 2018-10-22 stsp goto done;
2549 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2551 1a76625f 2018-10-22 stsp goto done;
2553 2814baeb 2018-08-01 stsp werase(view->window);
2555 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2556 a3404814 2018-09-02 stsp wstandout(view->window);
2557 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2559 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2560 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2561 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2562 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2566 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2567 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2568 a3404814 2018-09-02 stsp wstandend(view->window);
2569 ecb28ae0 2018-07-16 stsp free(wline);
2570 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2571 1a76625f 2018-10-22 stsp goto done;
2573 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2574 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2575 5813d178 2019-03-09 stsp ncommits = 0;
2576 145b6838 2022-06-16 stsp view->maxx = 0;
2577 5813d178 2019-03-09 stsp while (entry) {
2578 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2579 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2580 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2581 5813d178 2019-03-09 stsp int width;
2582 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2584 10aab77f 2022-07-19 op if (s->use_committer)
2585 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2587 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2588 5813d178 2019-03-09 stsp if (author == NULL) {
2589 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2590 5813d178 2019-03-09 stsp goto done;
2592 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2593 27a741e5 2019-09-11 stsp date_display_cols);
2594 5813d178 2019-03-09 stsp if (author_cols < width)
2595 5813d178 2019-03-09 stsp author_cols = width;
2596 5813d178 2019-03-09 stsp free(wauthor);
2597 5813d178 2019-03-09 stsp free(author);
2598 a310d9c3 2022-07-21 florian if (err)
2599 a310d9c3 2022-07-21 florian goto done;
2600 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2602 145b6838 2022-06-16 stsp goto done;
2603 145b6838 2022-06-16 stsp msg = msg0;
2604 145b6838 2022-06-16 stsp while (*msg == '\n')
2606 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2607 29688b02 2022-06-16 stsp *eol = '\0';
2608 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2609 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2611 29688b02 2022-06-16 stsp goto done;
2612 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2613 145b6838 2022-06-16 stsp free(msg0);
2614 29688b02 2022-06-16 stsp free(wmsg);
2615 7ca04879 2019-10-19 stsp ncommits++;
2616 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2619 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2620 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2621 867c6645 2018-07-10 stsp ncommits = 0;
2622 899d86c2 2018-05-10 stsp while (entry) {
2623 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2625 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2626 2814baeb 2018-08-01 stsp wstandout(view->window);
2627 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2628 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2629 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2630 2814baeb 2018-08-01 stsp wstandend(view->window);
2632 60493ae3 2019-06-20 stsp goto done;
2633 0553a4e3 2018-04-30 stsp ncommits++;
2634 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2635 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2638 9b058f45 2022-06-30 mark view_border(view);
2640 1a76625f 2018-10-22 stsp free(id_str);
2641 8b473291 2019-02-21 stsp free(refs_str);
2642 1a76625f 2018-10-22 stsp free(ncommits_str);
2643 1a76625f 2018-10-22 stsp free(header);
2644 80ddbec8 2018-04-29 stsp return err;
2647 07b55e75 2018-05-10 stsp static void
2648 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2650 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2651 07b55e75 2018-05-10 stsp int nscrolled = 0;
2653 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2654 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2657 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2658 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2659 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2660 07b55e75 2018-05-10 stsp if (entry) {
2661 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2662 07b55e75 2018-05-10 stsp nscrolled++;
2667 aa075928 2018-05-10 stsp static const struct got_error *
2668 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2670 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2671 5e224a3e 2019-02-22 stsp int errcode;
2673 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2675 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2676 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2677 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2678 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2679 7aafa0d1 2019-02-22 stsp if (errcode)
2680 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2681 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2684 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2685 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2687 7c1452c1 2020-03-26 stsp if (!wait)
2690 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2691 ffe38506 2020-12-01 naddy show_log_view(view);
2692 7c1452c1 2020-03-26 stsp update_panels();
2693 7c1452c1 2020-03-26 stsp doupdate();
2695 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2696 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2697 82954512 2020-02-03 stsp if (errcode)
2698 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2699 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2701 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2702 ffe38506 2020-12-01 naddy show_log_view(view);
2703 7c1452c1 2020-03-26 stsp update_panels();
2704 7c1452c1 2020-03-26 stsp doupdate();
2707 5e224a3e 2019-02-22 stsp return NULL;
2710 5e224a3e 2019-02-22 stsp static const struct got_error *
2711 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2713 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2714 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2716 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2717 2525dccb 2022-07-13 mark return NULL;
2719 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2720 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2721 9b058f45 2022-06-30 mark view->nscrolled = 0;
2723 9b058f45 2022-06-30 mark return err;
2726 9b058f45 2022-06-30 mark static const struct got_error *
2727 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2729 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2730 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2731 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2732 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2734 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2735 5e224a3e 2019-02-22 stsp return NULL;
2737 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2738 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2739 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2741 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2743 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2744 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2745 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2747 5e224a3e 2019-02-22 stsp return err;
2751 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2752 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2755 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2756 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
2758 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2759 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2761 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2762 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2764 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2765 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2767 9b058f45 2022-06-30 mark view->nscrolled = 0;
2769 dd0a52c1 2018-05-20 stsp return err;
2772 cd0acaa7 2018-05-20 stsp static const struct got_error *
2773 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2774 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2775 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2777 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2778 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2779 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2781 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2782 15a94983 2018-12-23 stsp if (diff_view == NULL)
2783 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2785 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2786 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2787 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2788 e5a0f69f 2018-08-18 stsp if (err == NULL)
2789 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2790 cd0acaa7 2018-05-20 stsp return err;
2793 80ddbec8 2018-04-29 stsp static const struct got_error *
2794 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2795 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2797 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2799 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2800 941e9f74 2019-05-21 stsp if (parent == NULL)
2801 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2803 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2804 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2805 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2806 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2807 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2808 941e9f74 2019-05-21 stsp s->tree = subtree;
2809 941e9f74 2019-05-21 stsp s->selected = 0;
2810 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2811 941e9f74 2019-05-21 stsp return NULL;
2814 941e9f74 2019-05-21 stsp static const struct got_error *
2815 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2816 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2818 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2819 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2820 941e9f74 2019-05-21 stsp const char *p;
2821 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2823 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2825 941e9f74 2019-05-21 stsp while (*p) {
2826 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2827 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2828 56e0773d 2019-11-28 stsp char *te_name;
2830 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2833 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2834 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2835 941e9f74 2019-05-21 stsp if (slash == NULL)
2836 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2838 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2839 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2840 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2843 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2844 56e0773d 2019-11-28 stsp if (te == NULL) {
2845 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2846 56e0773d 2019-11-28 stsp free(te_name);
2849 56e0773d 2019-11-28 stsp free(te_name);
2850 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2852 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2853 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2855 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2856 941e9f74 2019-05-21 stsp if (slash)
2857 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2859 941e9f74 2019-05-21 stsp subpath = strdup(path);
2860 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2861 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2865 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2870 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2871 941e9f74 2019-05-21 stsp free(tree_id);
2875 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2876 941e9f74 2019-05-21 stsp if (err) {
2877 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2880 941e9f74 2019-05-21 stsp if (slash == NULL)
2882 941e9f74 2019-05-21 stsp free(subpath);
2883 941e9f74 2019-05-21 stsp subpath = NULL;
2884 941e9f74 2019-05-21 stsp p = slash;
2887 941e9f74 2019-05-21 stsp free(subpath);
2888 1a76625f 2018-10-22 stsp return err;
2891 61266923 2020-01-14 stsp static const struct got_error *
2892 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2893 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2894 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2896 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2897 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2898 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2900 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2901 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2902 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2904 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2906 55cccc34 2020-02-20 stsp return err;
2907 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2909 55cccc34 2020-02-20 stsp *new_view = tree_view;
2911 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2912 55cccc34 2020-02-20 stsp return NULL;
2914 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2917 55cccc34 2020-02-20 stsp static const struct got_error *
2918 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2920 61266923 2020-01-14 stsp sigset_t sigset;
2921 61266923 2020-01-14 stsp int errcode;
2923 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2924 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2926 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2927 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2928 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2929 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2930 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2931 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2932 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2933 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2934 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2936 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2937 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2938 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2940 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2941 61266923 2020-01-14 stsp if (errcode)
2942 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2944 61266923 2020-01-14 stsp return NULL;
2947 1a76625f 2018-10-22 stsp static void *
2948 1a76625f 2018-10-22 stsp log_thread(void *arg)
2950 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2951 1a76625f 2018-10-22 stsp int errcode = 0;
2952 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2953 1a76625f 2018-10-22 stsp int done = 0;
2956 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2957 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2959 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2960 5629093a 2022-07-11 stsp if (errcode) {
2961 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2962 61266923 2020-01-14 stsp return (void *)err;
2965 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2966 5629093a 2022-07-11 stsp if (err) {
2967 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2968 5629093a 2022-07-11 stsp goto done;
2971 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2972 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2973 5629093a 2022-07-11 stsp if (errcode) {
2974 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2975 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2976 5629093a 2022-07-11 stsp goto done;
2978 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2979 1a76625f 2018-10-22 stsp if (err) {
2980 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2981 5629093a 2022-07-11 stsp goto done;
2982 1a76625f 2018-10-22 stsp err = NULL;
2984 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
2985 568eae95 2022-09-11 mark if (*a->limiting) {
2986 568eae95 2022-09-11 mark if (a->limit_match)
2987 568eae95 2022-09-11 mark a->commits_needed--;
2989 568eae95 2022-09-11 mark a->commits_needed--;
2992 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2993 3abe8080 2019-04-10 stsp if (errcode) {
2994 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2995 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2996 5629093a 2022-07-11 stsp goto done;
2997 3abe8080 2019-04-10 stsp } else if (*a->quit)
2999 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
3000 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3001 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
3002 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3003 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
3004 568eae95 2022-09-11 mark *a->first_displayed_entry =
3005 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
3006 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
3009 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3010 7c1452c1 2020-03-26 stsp if (errcode) {
3011 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3012 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3013 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3014 5629093a 2022-07-11 stsp goto done;
3018 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3020 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3021 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3022 7c1452c1 2020-03-26 stsp &tog_mutex);
3023 5629093a 2022-07-11 stsp if (errcode) {
3024 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3025 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3026 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3027 5629093a 2022-07-11 stsp goto done;
3029 21355643 2020-12-06 stsp if (*a->quit)
3034 3abe8080 2019-04-10 stsp a->log_complete = 1;
3035 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3036 5629093a 2022-07-11 stsp if (errcode)
3037 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3039 5629093a 2022-07-11 stsp if (err) {
3040 5629093a 2022-07-11 stsp tog_thread_error = 1;
3041 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3043 1a76625f 2018-10-22 stsp return (void *)err;
3046 1a76625f 2018-10-22 stsp static const struct got_error *
3047 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3049 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3050 1a76625f 2018-10-22 stsp int errcode;
3052 1a76625f 2018-10-22 stsp if (s->thread) {
3053 1a76625f 2018-10-22 stsp s->quit = 1;
3054 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3055 1a76625f 2018-10-22 stsp if (errcode)
3056 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3057 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3058 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3059 1a76625f 2018-10-22 stsp if (errcode)
3060 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3061 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3062 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3063 1a76625f 2018-10-22 stsp if (errcode)
3064 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3065 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3066 1a76625f 2018-10-22 stsp if (errcode)
3067 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3068 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3069 1a76625f 2018-10-22 stsp s->thread = NULL;
3072 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3073 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3074 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3077 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3078 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3079 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3080 74467cc8 2022-06-15 stsp if (err == NULL)
3081 74467cc8 2022-06-15 stsp err = pack_err;
3082 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3085 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3086 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3087 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3090 5629093a 2022-07-11 stsp return err ? err : thread_err;
3093 9343a5fb 2018-06-23 stsp static const struct got_error *
3094 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3096 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3097 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3098 276b94a1 2020-11-13 naddy int errcode;
3100 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3102 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3103 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3104 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3106 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3107 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3108 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3110 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3111 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3112 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3113 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3114 1a76625f 2018-10-22 stsp free(s->start_id);
3115 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3116 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3117 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3118 1a76625f 2018-10-22 stsp return err;
3122 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3123 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3124 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3125 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3126 568eae95 2022-09-11 mark * works with very slight change.
3128 1a76625f 2018-10-22 stsp static const struct got_error *
3129 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3131 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3132 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3133 568eae95 2022-09-11 mark struct tog_view *v = view;
3134 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3135 568eae95 2022-09-11 mark char pattern[1024];
3138 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3139 568eae95 2022-09-11 mark v = view->child;
3140 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3141 568eae95 2022-09-11 mark v = view->parent;
3143 568eae95 2022-09-11 mark /* Get the pattern */
3144 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3145 568eae95 2022-09-11 mark wclrtoeol(v->window);
3146 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3147 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3148 568eae95 2022-09-11 mark nocbreak();
3150 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3153 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3154 568eae95 2022-09-11 mark if (ret == ERR)
3155 568eae95 2022-09-11 mark return NULL;
3157 568eae95 2022-09-11 mark if (*pattern == '\0') {
3159 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3160 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3162 568eae95 2022-09-11 mark if (!s->limit_view)
3163 568eae95 2022-09-11 mark return NULL;
3166 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3167 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3168 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3169 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3170 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3171 568eae95 2022-09-11 mark * the queue.
3173 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3174 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3175 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3176 568eae95 2022-09-11 mark s->selected = 0;
3177 568eae95 2022-09-11 mark s->limit_view = 0;
3179 568eae95 2022-09-11 mark return NULL;
3182 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3183 568eae95 2022-09-11 mark return NULL;
3185 568eae95 2022-09-11 mark s->limit_view = 1;
3187 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3188 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3189 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3190 568eae95 2022-09-11 mark s->selected_entry = NULL;
3191 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3193 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3194 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3195 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3197 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3198 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3199 568eae95 2022-09-11 mark int have_match = 0;
3201 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3202 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3204 568eae95 2022-09-11 mark return err;
3206 568eae95 2022-09-11 mark if (have_match) {
3207 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3209 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3210 568eae95 2022-09-11 mark entry->id);
3211 568eae95 2022-09-11 mark if (matched == NULL) {
3212 568eae95 2022-09-11 mark err = got_error_from_errno(
3213 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3216 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3217 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3219 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3220 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3221 568eae95 2022-09-11 mark matched, entry);
3222 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3226 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3227 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3228 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3229 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3230 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3231 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3233 568eae95 2022-09-11 mark return err;
3236 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3237 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3238 568eae95 2022-09-11 mark s->selected = 0;
3240 568eae95 2022-09-11 mark return NULL;
3243 568eae95 2022-09-11 mark static const struct got_error *
3244 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3246 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3248 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3249 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3250 60493ae3 2019-06-20 stsp return NULL;
3253 60493ae3 2019-06-20 stsp static const struct got_error *
3254 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3256 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3257 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3258 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3260 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3261 f9686aa5 2020-03-27 stsp show_log_view(view);
3262 f9686aa5 2020-03-27 stsp update_panels();
3263 f9686aa5 2020-03-27 stsp doupdate();
3265 96e2b566 2019-07-08 stsp if (s->search_entry) {
3266 13add988 2019-10-15 stsp int errcode, ch;
3267 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3268 13add988 2019-10-15 stsp if (errcode)
3269 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3270 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3271 13add988 2019-10-15 stsp ch = wgetch(view->window);
3272 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3273 13add988 2019-10-15 stsp if (errcode)
3274 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3275 13add988 2019-10-15 stsp "pthread_mutex_lock");
3276 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3277 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3278 678cbce5 2019-07-28 stsp return NULL;
3280 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3281 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3283 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3284 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3285 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3287 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3288 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3289 f704b35c 2022-06-18 stsp * might have changed.
3291 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3292 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3294 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3297 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3300 60493ae3 2019-06-20 stsp while (1) {
3301 13add988 2019-10-15 stsp int have_match = 0;
3303 60493ae3 2019-06-20 stsp if (entry == NULL) {
3304 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3305 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3306 f9967bca 2020-03-27 stsp view->search_next_done =
3307 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3308 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3309 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3310 f801134a 2019-06-25 stsp return NULL;
3313 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3314 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3315 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3317 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3318 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3321 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3322 13add988 2019-10-15 stsp &view->regex);
3325 13add988 2019-10-15 stsp if (have_match) {
3326 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3327 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3331 96e2b566 2019-07-08 stsp s->search_entry = entry;
3332 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3333 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3335 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3338 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3339 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3340 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3341 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3343 60493ae3 2019-06-20 stsp return err;
3346 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3347 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3349 60493ae3 2019-06-20 stsp return err;
3354 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3356 60493ae3 2019-06-20 stsp return NULL;
3359 60493ae3 2019-06-20 stsp static const struct got_error *
3360 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3361 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3362 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3364 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3365 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3366 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3367 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3368 1a76625f 2018-10-22 stsp int errcode;
3370 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3371 f135c941 2020-02-20 stsp free(s->in_repo_path);
3372 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3373 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3374 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3377 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3378 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3379 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3380 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3382 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3383 568eae95 2022-09-11 mark s->limit_view = 0;
3384 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3386 fb2756b9 2018-08-04 stsp s->repo = repo;
3387 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3388 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3389 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3390 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3391 9cd7cbd1 2020-12-07 stsp goto done;
3394 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3395 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3396 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3397 5036bf37 2018-09-24 stsp goto done;
3399 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3400 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3402 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3403 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3404 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3405 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3407 11b20872 2019-11-08 stsp goto done;
3408 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3409 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3410 11b20872 2019-11-08 stsp if (err) {
3411 11b20872 2019-11-08 stsp free_colors(&s->colors);
3412 11b20872 2019-11-08 stsp goto done;
3414 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3415 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3416 11b20872 2019-11-08 stsp if (err) {
3417 11b20872 2019-11-08 stsp free_colors(&s->colors);
3418 11b20872 2019-11-08 stsp goto done;
3422 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3423 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3424 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3425 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3426 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3427 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3429 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3430 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3432 74467cc8 2022-06-15 stsp goto done;
3434 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3435 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3436 0ae84acc 2022-06-15 tracey if (err)
3437 0ae84acc 2022-06-15 tracey goto done;
3438 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3439 b672a97a 2020-01-27 stsp !s->log_branches);
3441 1a76625f 2018-10-22 stsp goto done;
3442 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3443 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3445 c5b78334 2020-01-12 stsp goto done;
3447 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3448 1a76625f 2018-10-22 stsp if (errcode) {
3449 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3450 1a76625f 2018-10-22 stsp goto done;
3452 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3453 7c1452c1 2020-03-26 stsp if (errcode) {
3454 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3455 7c1452c1 2020-03-26 stsp goto done;
3458 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3459 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3460 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3461 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3462 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3463 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3464 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3465 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3466 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3467 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3468 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3469 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3470 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3471 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3472 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3473 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3474 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3477 1a76625f 2018-10-22 stsp close_log_view(view);
3478 ba4f502b 2018-08-04 stsp return err;
3481 e5a0f69f 2018-08-18 stsp static const struct got_error *
3482 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3484 f2f6d207 2020-11-24 stsp const struct got_error *err;
3485 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3487 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3488 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3489 2b380cc8 2018-10-24 stsp &s->thread_args);
3490 2b380cc8 2018-10-24 stsp if (errcode)
3491 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3492 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3493 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3495 f2f6d207 2020-11-24 stsp return err;
3499 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3502 b880cc75 2022-06-30 mark static void
3503 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3505 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3507 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3509 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3510 568eae95 2022-09-11 mark view->count = 0;
3512 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3514 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3516 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3517 b880cc75 2022-06-30 mark --s->selected;
3519 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3521 b880cc75 2022-06-30 mark select_commit(s);
3525 b880cc75 2022-06-30 mark static const struct got_error *
3526 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3528 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3529 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3530 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3532 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3533 568eae95 2022-09-11 mark return NULL;
3535 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3536 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3537 b880cc75 2022-06-30 mark return NULL;
3539 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3540 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3542 631e7531 2022-08-12 mark if (!page) {
3543 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3544 b880cc75 2022-06-30 mark ++s->selected;
3546 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3547 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3548 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3551 631e7531 2022-08-12 mark s->selected = 0;
3552 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3553 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3554 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3555 631e7531 2022-08-12 mark if (entry == NULL)
3557 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3558 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3560 631e7531 2022-08-12 mark if (n > 0)
3561 631e7531 2022-08-12 mark s->selected = n - 1;
3563 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3564 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3565 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3566 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3568 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3571 b880cc75 2022-06-30 mark return err;
3574 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3575 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3577 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3578 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3579 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3580 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3583 b880cc75 2022-06-30 mark select_commit(s);
3585 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3586 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3587 b880cc75 2022-06-30 mark view->count = 0;
3589 b880cc75 2022-06-30 mark return NULL;
3592 9b058f45 2022-06-30 mark static void
3593 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3598 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3599 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3600 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3601 7532ccda 2022-07-11 mark else if (view->resized_y)
3602 7532ccda 2022-07-11 mark *y = view->resized_y;
3604 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3605 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3606 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3607 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3608 7532ccda 2022-07-11 mark else if (view->resized_x)
3609 7532ccda 2022-07-11 mark *x = view->resized_x;
3611 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3615 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3616 e5a0f69f 2018-08-18 stsp static const struct got_error *
3617 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3619 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3621 9b058f45 2022-06-30 mark view->nlines = y;
3622 d2366e29 2022-07-07 mark view->ncols = COLS;
3623 9b058f45 2022-06-30 mark err = view_resize(view);
3625 9b058f45 2022-06-30 mark return err;
3627 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3629 9b058f45 2022-06-30 mark return err;
3632 94b80cfa 2022-08-01 mark static const struct got_error *
3633 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3635 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3636 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3637 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3639 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3640 374f69dd 2022-08-13 stsp return NULL;
3642 94b80cfa 2022-08-01 mark g = view->gline;
3643 94b80cfa 2022-08-01 mark view->gline = 0;
3645 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3646 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3647 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3648 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3649 94b80cfa 2022-08-01 mark select_commit(s);
3650 94b80cfa 2022-08-01 mark return NULL;
3653 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3654 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3655 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3656 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3657 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3659 94b80cfa 2022-08-01 mark return err;
3660 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3661 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3663 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3664 94b80cfa 2022-08-01 mark s->selected = g - 1;
3666 94b80cfa 2022-08-01 mark select_commit(s);
3667 94b80cfa 2022-08-01 mark return NULL;
3671 9b058f45 2022-06-30 mark static const struct got_error *
3672 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3674 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3675 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3676 631e7531 2022-08-12 mark int eos, nscroll;
3678 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3679 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3680 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3681 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3682 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3683 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3686 11edf34c 2022-08-12 mark return err;
3689 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3690 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3691 0dca135e 2022-06-30 mark --eos; /* border */
3693 94b80cfa 2022-08-01 mark if (view->gline)
3694 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3696 528dedf3 2021-08-30 stsp switch (ch) {
3698 568eae95 2022-09-11 mark err = limit_log_view(view);
3701 1e37a5c2 2019-05-12 jcs s->quit = 1;
3704 145b6838 2022-06-16 stsp view->x = 0;
3707 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3708 640cd7ff 2022-06-22 mark view->count = 0;
3710 145b6838 2022-06-16 stsp case KEY_RIGHT:
3712 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3713 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3715 640cd7ff 2022-06-22 mark view->count = 0;
3717 145b6838 2022-06-16 stsp case KEY_LEFT:
3719 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3720 640cd7ff 2022-06-22 mark if (view->x <= 0)
3721 640cd7ff 2022-06-22 mark view->count = 0;
3724 1e37a5c2 2019-05-12 jcs case KEY_UP:
3727 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3728 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3732 912a3f79 2021-08-30 j case KEY_HOME:
3733 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3734 640cd7ff 2022-06-22 mark view->count = 0;
3736 83cc4199 2022-06-13 stsp case CTRL('u'):
3738 83cc4199 2022-06-13 stsp nscroll /= 2;
3739 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3740 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3741 a4292ac5 2019-05-12 jcs case CTRL('b'):
3743 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3746 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3749 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3750 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3753 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3754 3f6c6614 2023-01-23 mark view->action = s->use_committer ?
3755 3f6c6614 2023-01-23 mark "show committer" : "show commit author";
3759 912a3f79 2021-08-30 j case KEY_END: {
3760 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3761 912a3f79 2021-08-30 j * traverse them all. */
3762 640cd7ff 2022-06-22 mark view->count = 0;
3763 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3764 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3765 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3766 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3767 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3770 80b7e8da 2022-06-11 stsp case CTRL('d'):
3772 83cc4199 2022-06-13 stsp nscroll /= 2;
3773 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3774 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3775 61417565 2022-06-20 mark case CTRL('f'):
3776 48bb96f0 2022-06-20 naddy case 'f':
3778 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3780 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3781 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3782 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3783 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3784 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3785 2b779855 2020-12-05 naddy select_commit(s);
3786 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3787 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3788 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3789 568eae95 2022-09-11 mark s->commits->ncommits;
3790 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3793 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3794 49b24ee5 2022-07-03 mark case '\r':
3795 640cd7ff 2022-06-22 mark view->count = 0;
3796 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3798 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3801 640cd7ff 2022-06-22 mark view->count = 0;
3802 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3804 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3806 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3807 21355643 2020-12-06 stsp case CTRL('l'):
3809 640cd7ff 2022-06-22 mark view->count = 0;
3810 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3811 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3813 21355643 2020-12-06 stsp err = stop_log_thread(s);
3815 74cfe85e 2020-10-20 stsp return err;
3816 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3817 21355643 2020-12-06 stsp char *parent_path;
3818 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3820 21355643 2020-12-06 stsp return err;
3821 21355643 2020-12-06 stsp free(s->in_repo_path);
3822 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3823 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3824 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3825 21355643 2020-12-06 stsp struct got_object_id *start_id;
3826 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3827 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3828 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3829 ea371198 2022-11-17 stsp if (err) {
3830 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
3831 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
3832 ea371198 2022-11-17 stsp return err;
3833 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
3834 ea371198 2022-11-17 stsp free(s->head_ref_name);
3835 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
3836 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
3837 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3838 ea371198 2022-11-17 stsp &tog_refs, s->repo);
3840 ea371198 2022-11-17 stsp return err;
3842 21355643 2020-12-06 stsp free(s->start_id);
3843 21355643 2020-12-06 stsp s->start_id = start_id;
3844 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3845 21355643 2020-12-06 stsp } else /* 'B' */
3846 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3848 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3849 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3851 b0dd8d27 2022-06-16 stsp return err;
3853 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3854 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3855 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3857 21355643 2020-12-06 stsp return err;
3858 51a10b52 2020-12-26 stsp tog_free_refs();
3859 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3861 ca51c541 2020-12-07 stsp return err;
3862 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3863 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3865 d01904d4 2019-06-25 stsp return err;
3866 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3867 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3869 b672a97a 2020-01-27 stsp return err;
3870 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3871 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3872 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3873 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3874 21355643 2020-12-06 stsp s->selected_entry = NULL;
3875 21355643 2020-12-06 stsp s->selected = 0;
3876 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3877 21355643 2020-12-06 stsp s->quit = 0;
3878 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3879 dfee752e 2022-06-18 op s->matched_entry = NULL;
3880 dfee752e 2022-06-18 op s->search_entry = NULL;
3881 01a7bcaf 2022-07-22 mark view->offset = 0;
3884 640cd7ff 2022-06-22 mark view->count = 0;
3885 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3888 640cd7ff 2022-06-22 mark view->count = 0;
3892 80ddbec8 2018-04-29 stsp return err;
3895 4ed7e80c 2018-05-20 stsp static const struct got_error *
3896 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3898 c2db6724 2019-01-04 stsp const struct got_error *error;
3900 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3901 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3902 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3904 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3905 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3907 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3908 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3910 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3911 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3913 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3914 c2db6724 2019-01-04 stsp if (error != NULL)
3915 c2db6724 2019-01-04 stsp return error;
3917 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3918 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3920 c2db6724 2019-01-04 stsp return NULL;
3923 a915003a 2019-02-05 stsp static void
3924 a915003a 2019-02-05 stsp init_curses(void)
3927 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3928 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3929 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3931 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3932 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3933 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3934 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3935 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3937 a915003a 2019-02-05 stsp initscr();
3939 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3942 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3943 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3944 a915003a 2019-02-05 stsp curs_set(0);
3945 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3946 6d17833f 2019-11-08 stsp start_color();
3947 6d17833f 2019-11-08 stsp use_default_colors();
3951 c2db6724 2019-01-04 stsp static const struct got_error *
3952 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3953 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3955 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3957 f135c941 2020-02-20 stsp if (argc == 0) {
3958 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3959 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3960 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3961 f135c941 2020-02-20 stsp return NULL;
3964 f135c941 2020-02-20 stsp if (worktree) {
3965 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3968 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3970 f135c941 2020-02-20 stsp return err;
3971 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3972 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3973 bfd61697 2020-11-14 stsp p) == -1) {
3974 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3975 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3979 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3981 f135c941 2020-02-20 stsp return err;
3984 f135c941 2020-02-20 stsp static const struct got_error *
3985 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3987 80ddbec8 2018-04-29 stsp const struct got_error *error;
3988 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3989 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3990 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3991 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3992 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3993 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3994 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3995 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3996 04cc582a 2018-08-01 stsp struct tog_view *view;
3997 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3999 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4000 80ddbec8 2018-04-29 stsp switch (ch) {
4002 b672a97a 2020-01-27 stsp log_branches = 1;
4005 80ddbec8 2018-04-29 stsp start_commit = optarg;
4008 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4009 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4010 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4014 17020d27 2019-03-07 stsp usage_log();
4015 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4019 80ddbec8 2018-04-29 stsp argc -= optind;
4020 80ddbec8 2018-04-29 stsp argv += optind;
4022 f135c941 2020-02-20 stsp if (argc > 1)
4023 f135c941 2020-02-20 stsp usage_log();
4025 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4026 0ae84acc 2022-06-15 tracey if (error != NULL)
4027 0ae84acc 2022-06-15 tracey goto done;
4029 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4030 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4031 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4032 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4033 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4034 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4035 c156c7a4 2020-12-18 stsp goto done;
4036 a1fbf39a 2019-08-11 stsp if (worktree)
4037 6962eb72 2020-02-20 stsp repo_path =
4038 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4040 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4041 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4042 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4043 c156c7a4 2020-12-18 stsp goto done;
4047 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4048 80ddbec8 2018-04-29 stsp if (error != NULL)
4049 ecb28ae0 2018-07-16 stsp goto done;
4051 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4052 f135c941 2020-02-20 stsp repo, worktree);
4053 f135c941 2020-02-20 stsp if (error)
4054 f135c941 2020-02-20 stsp goto done;
4056 f135c941 2020-02-20 stsp init_curses();
4058 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4059 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4060 c02c541e 2019-03-29 stsp if (error)
4061 c02c541e 2019-03-29 stsp goto done;
4063 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4064 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4065 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4066 87670572 2020-12-26 naddy if (error)
4067 87670572 2020-12-26 naddy goto done;
4070 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4071 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4072 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4073 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4074 d8f38dc4 2020-12-05 stsp if (error)
4075 d8f38dc4 2020-12-05 stsp goto done;
4076 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4078 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4079 d8f38dc4 2020-12-05 stsp if (error == NULL)
4080 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4081 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4082 d8f38dc4 2020-12-05 stsp goto done;
4083 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4084 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4085 d8f38dc4 2020-12-05 stsp if (error)
4086 d8f38dc4 2020-12-05 stsp goto done;
4089 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4090 04cc582a 2018-08-01 stsp if (view == NULL) {
4091 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4092 04cc582a 2018-08-01 stsp goto done;
4094 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4095 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4096 ba4f502b 2018-08-04 stsp if (error)
4097 ba4f502b 2018-08-04 stsp goto done;
4098 2fc00ff4 2019-08-31 stsp if (worktree) {
4099 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4100 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4101 2fc00ff4 2019-08-31 stsp worktree = NULL;
4103 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4105 f135c941 2020-02-20 stsp free(in_repo_path);
4106 ecb28ae0 2018-07-16 stsp free(repo_path);
4107 ecb28ae0 2018-07-16 stsp free(cwd);
4108 899d86c2 2018-05-10 stsp free(start_id);
4109 d8f38dc4 2020-12-05 stsp free(label);
4111 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4112 1d0f4054 2021-06-17 stsp if (repo) {
4113 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4114 1d0f4054 2021-06-17 stsp if (error == NULL)
4115 1d0f4054 2021-06-17 stsp error = close_err;
4117 ec142235 2019-03-07 stsp if (worktree)
4118 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4119 0ae84acc 2022-06-15 tracey if (pack_fds) {
4120 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4121 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4122 0ae84acc 2022-06-15 tracey if (error == NULL)
4123 0ae84acc 2022-06-15 tracey error = pack_err;
4125 51a10b52 2020-12-26 stsp tog_free_refs();
4126 80ddbec8 2018-04-29 stsp return error;
4129 4ed7e80c 2018-05-20 stsp __dead static void
4130 9f7d7167 2018-04-29 stsp usage_diff(void)
4133 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4134 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4138 6d17833f 2019-11-08 stsp static int
4139 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4140 41605754 2020-11-12 stsp regmatch_t *regmatch)
4142 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4145 336075a4 2022-06-25 op static struct tog_color *
4146 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4148 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4150 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4151 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4152 6d17833f 2019-11-08 stsp return tc;
4155 6d17833f 2019-11-08 stsp return NULL;
4158 4ed7e80c 2018-05-20 stsp static const struct got_error *
4159 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4160 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4162 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4163 44a87665 2022-06-16 stsp char *exstr = NULL;
4164 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4165 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4166 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4167 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4169 41605754 2020-11-12 stsp *wtotal = 0;
4171 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4172 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4174 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4176 44a87665 2022-06-16 stsp return err;
4178 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4179 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4180 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4181 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4182 44a87665 2022-06-16 stsp goto done;
4184 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4185 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4186 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4187 1853e0f4 2022-06-16 stsp goto done;
4189 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4190 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4191 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4192 1853e0f4 2022-06-16 stsp goto done;
4195 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4196 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4197 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4199 1853e0f4 2022-06-16 stsp goto done;
4200 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4202 1853e0f4 2022-06-16 stsp free(wline);
4203 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4204 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4206 1853e0f4 2022-06-16 stsp goto done;
4207 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4208 1853e0f4 2022-06-16 stsp wlimit -= width;
4209 1853e0f4 2022-06-16 stsp *wtotal += width;
4212 41605754 2020-11-12 stsp if (wlimit > 0) {
4213 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4214 1853e0f4 2022-06-16 stsp size_t wlen;
4216 1853e0f4 2022-06-16 stsp free(wline);
4217 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4218 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4220 1853e0f4 2022-06-16 stsp goto done;
4221 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4222 1853e0f4 2022-06-16 stsp while (i < wlen) {
4223 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4224 1853e0f4 2022-06-16 stsp if (width == -1) {
4225 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4226 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4227 1853e0f4 2022-06-16 stsp goto done;
4229 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4231 61417565 2022-06-20 mark w += width;
4234 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4235 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4236 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4237 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4238 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4239 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4240 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4244 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4245 1853e0f4 2022-06-16 stsp free(wline);
4246 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4247 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4248 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4249 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4251 1853e0f4 2022-06-16 stsp goto done;
4252 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4254 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4255 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4257 1853e0f4 2022-06-16 stsp goto done;
4258 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4260 1853e0f4 2022-06-16 stsp *wtotal += width2;
4263 145b6838 2022-06-16 stsp free(wline);
4264 44a87665 2022-06-16 stsp free(exstr);
4265 1853e0f4 2022-06-16 stsp free(seg0);
4266 1853e0f4 2022-06-16 stsp free(seg1);
4267 1853e0f4 2022-06-16 stsp free(seg2);
4268 1853e0f4 2022-06-16 stsp return err;
4271 94b80cfa 2022-08-01 mark static int
4272 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4274 94b80cfa 2022-08-01 mark FILE *f = NULL;
4275 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4277 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4278 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4280 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4281 ec2a9698 2022-09-15 mark selected = first;
4282 ec2a9698 2022-09-15 mark eof = &s->eof;
4284 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4285 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4287 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4288 94b80cfa 2022-08-01 mark selected = first;
4289 94b80cfa 2022-08-01 mark eof = &s->eof;
4291 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4292 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4294 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4295 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4296 94b80cfa 2022-08-01 mark eof = &s->eof;
4297 94b80cfa 2022-08-01 mark f = s->blame.f;
4301 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4302 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4304 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4305 94b80cfa 2022-08-01 mark rewind(f);
4307 94b80cfa 2022-08-01 mark *first = 1;
4308 94b80cfa 2022-08-01 mark *lineno = 0;
4309 94b80cfa 2022-08-01 mark *nprinted = 0;
4313 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4314 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4315 94b80cfa 2022-08-01 mark view->gline = 0;
4320 41605754 2020-11-12 stsp static const struct got_error *
4321 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4323 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4324 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4325 61e69b96 2018-05-20 stsp const struct got_error *err;
4326 c7d5c43c 2022-08-04 mark int nprinted = 0;
4327 b304db33 2018-05-20 stsp char *line;
4328 826082fe 2020-12-10 stsp size_t linesize = 0;
4329 826082fe 2020-12-10 stsp ssize_t linelen;
4330 61e69b96 2018-05-20 stsp wchar_t *wline;
4331 e0b650dd 2018-05-20 stsp int width;
4332 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4333 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4334 fe621944 2020-11-10 stsp off_t line_offset;
4336 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4337 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4338 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4339 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4341 f7d12f7e 2018-08-01 stsp werase(view->window);
4343 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4344 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4346 a3404814 2018-09-02 stsp if (header) {
4347 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4348 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4349 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4351 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4352 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4353 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4355 135a2da0 2020-11-11 stsp free(line);
4357 a3404814 2018-09-02 stsp return err;
4359 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4360 a3404814 2018-09-02 stsp wstandout(view->window);
4361 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4362 e54cc94a 2020-11-11 stsp free(wline);
4363 e54cc94a 2020-11-11 stsp wline = NULL;
4364 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4365 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4366 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4367 a3404814 2018-09-02 stsp wstandend(view->window);
4369 a3404814 2018-09-02 stsp if (max_lines <= 1)
4370 a3404814 2018-09-02 stsp return NULL;
4371 a3404814 2018-09-02 stsp max_lines--;
4374 89f1a395 2020-12-01 naddy s->eof = 0;
4375 145b6838 2022-06-16 stsp view->maxx = 0;
4376 826082fe 2020-12-10 stsp line = NULL;
4377 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4378 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4379 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4381 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4382 826082fe 2020-12-10 stsp if (linelen == -1) {
4383 826082fe 2020-12-10 stsp if (feof(s->f)) {
4384 826082fe 2020-12-10 stsp s->eof = 1;
4387 826082fe 2020-12-10 stsp free(line);
4388 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4391 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4393 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4395 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4396 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4398 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4399 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4400 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4401 1853e0f4 2022-06-16 stsp if (err) {
4402 1853e0f4 2022-06-16 stsp free(line);
4403 1853e0f4 2022-06-16 stsp return err;
4405 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4406 1853e0f4 2022-06-16 stsp free(wline);
4407 1853e0f4 2022-06-16 stsp wline = NULL;
4409 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4410 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4411 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4412 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4414 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4415 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4416 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4417 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4418 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4419 41605754 2020-11-12 stsp if (err) {
4420 41605754 2020-11-12 stsp free(line);
4421 41605754 2020-11-12 stsp return err;
4425 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4426 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4427 969c159c 2022-06-16 stsp if (err) {
4428 969c159c 2022-06-16 stsp free(line);
4429 969c159c 2022-06-16 stsp return err;
4431 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4432 969c159c 2022-06-16 stsp free(wline);
4433 41605754 2020-11-12 stsp wline = NULL;
4435 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4436 94b80cfa 2022-08-01 mark /* highlight full gline length */
4437 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4438 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4440 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4441 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4444 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4445 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4446 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4448 826082fe 2020-12-10 stsp free(line);
4449 fe621944 2020-11-10 stsp if (nprinted >= 1)
4450 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4451 89f1a395 2020-12-01 naddy (nprinted - 1);
4453 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4455 9b058f45 2022-06-30 mark view_border(view);
4457 89f1a395 2020-12-01 naddy if (s->eof) {
4458 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4459 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4460 c3e9aa98 2019-05-13 jcs nprinted++;
4463 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4464 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4466 c3e9aa98 2019-05-13 jcs return err;
4469 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4470 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4471 e54cc94a 2020-11-11 stsp free(wline);
4472 e54cc94a 2020-11-11 stsp wline = NULL;
4473 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4476 26ed57b2 2018-05-19 stsp return NULL;
4479 abd2672a 2018-12-23 stsp static char *
4480 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4482 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4483 09867e48 2019-08-13 stsp char *p, *s;
4485 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4486 09867e48 2019-08-13 stsp if (tm == NULL)
4487 09867e48 2019-08-13 stsp return NULL;
4488 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4489 09867e48 2019-08-13 stsp if (s == NULL)
4490 09867e48 2019-08-13 stsp return NULL;
4491 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4493 abd2672a 2018-12-23 stsp *p = '\0';
4497 4ed7e80c 2018-05-20 stsp static const struct got_error *
4498 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4499 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4501 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4503 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4504 fe621944 2020-11-10 stsp if (p == NULL)
4505 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4506 c7d5c43c 2022-08-04 mark *lines = p;
4507 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4508 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4509 fe621944 2020-11-10 stsp (*nlines)++;
4511 fe621944 2020-11-10 stsp return NULL;
4514 fe621944 2020-11-10 stsp static const struct got_error *
4515 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4516 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4518 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4519 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4520 1f3405c9 2023-01-17 mark size_t i, r;
4522 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4523 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4525 1f3405c9 2023-01-17 mark for (;;) {
4526 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4527 1f3405c9 2023-01-17 mark if (r == 0) {
4528 1f3405c9 2023-01-17 mark if (ferror(src))
4529 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4530 1f3405c9 2023-01-17 mark if (feof(src))
4533 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4534 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4538 1f3405c9 2023-01-17 mark * The diff driver initialises the first line at offset zero when the
4539 1f3405c9 2023-01-17 mark * array isn't prepopulated, skip it; we already have it in *d_lines.
4541 1f3405c9 2023-01-17 mark for (i = 1; i < s_nlines; ++i)
4542 1f3405c9 2023-01-17 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4544 1f3405c9 2023-01-17 mark --s_nlines;
4546 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4547 1f3405c9 2023-01-17 mark if (p == NULL) {
4548 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4549 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4552 1f3405c9 2023-01-17 mark *d_lines = p;
4554 1f3405c9 2023-01-17 mark memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4555 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4557 1f3405c9 2023-01-17 mark return NULL;
4560 1f3405c9 2023-01-17 mark static const struct got_error *
4561 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4562 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4563 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4564 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4566 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4567 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4568 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4569 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4570 45d799e2 2018-12-23 stsp time_t committer_time;
4571 45d799e2 2018-12-23 stsp const char *author, *committer;
4572 8b473291 2019-02-21 stsp char *refs_str = NULL;
4573 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4574 fe621944 2020-11-10 stsp off_t outoff = 0;
4577 8b473291 2019-02-21 stsp if (refs) {
4578 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4580 8b473291 2019-02-21 stsp return err;
4583 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4585 abd2672a 2018-12-23 stsp return err;
4587 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4588 15a94983 2018-12-23 stsp if (err) {
4589 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4590 15a94983 2018-12-23 stsp goto done;
4593 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4595 fe621944 2020-11-10 stsp goto done;
4597 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4598 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4599 fe621944 2020-11-10 stsp if (n < 0) {
4600 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4601 abd2672a 2018-12-23 stsp goto done;
4603 fe621944 2020-11-10 stsp outoff += n;
4604 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4606 fe621944 2020-11-10 stsp goto done;
4608 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4609 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4610 fe621944 2020-11-10 stsp if (n < 0) {
4611 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4612 abd2672a 2018-12-23 stsp goto done;
4614 fe621944 2020-11-10 stsp outoff += n;
4615 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4617 fe621944 2020-11-10 stsp goto done;
4619 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4620 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4621 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4622 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4623 fe621944 2020-11-10 stsp if (n < 0) {
4624 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4625 fe621944 2020-11-10 stsp goto done;
4627 fe621944 2020-11-10 stsp outoff += n;
4628 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4629 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4631 fe621944 2020-11-10 stsp goto done;
4633 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4634 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4635 1a99e0b4 2023-01-07 stsp if (datestr) {
4636 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4637 fe621944 2020-11-10 stsp if (n < 0) {
4638 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4639 fe621944 2020-11-10 stsp goto done;
4641 fe621944 2020-11-10 stsp outoff += n;
4642 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4643 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4645 fe621944 2020-11-10 stsp goto done;
4647 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4648 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4649 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4650 9f98ca05 2021-09-24 stsp int pn = 1;
4651 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4652 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4653 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4655 9f98ca05 2021-09-24 stsp goto done;
4656 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4657 9f98ca05 2021-09-24 stsp if (n < 0) {
4658 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4659 9f98ca05 2021-09-24 stsp goto done;
4661 9f98ca05 2021-09-24 stsp outoff += n;
4662 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4663 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4665 9f98ca05 2021-09-24 stsp goto done;
4666 9f98ca05 2021-09-24 stsp free(id_str);
4667 9f98ca05 2021-09-24 stsp id_str = NULL;
4671 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4673 5943eee2 2019-08-13 stsp goto done;
4674 fe621944 2020-11-10 stsp s = logmsg;
4675 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4676 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4677 fe621944 2020-11-10 stsp if (n < 0) {
4678 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4679 fe621944 2020-11-10 stsp goto done;
4681 fe621944 2020-11-10 stsp outoff += n;
4682 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4683 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4685 fe621944 2020-11-10 stsp goto done;
4688 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
4689 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4690 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
4692 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4693 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4694 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
4695 fe621944 2020-11-10 stsp if (n < 0) {
4696 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4697 fe621944 2020-11-10 stsp goto done;
4699 fe621944 2020-11-10 stsp outoff += n;
4700 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4701 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4703 fe621944 2020-11-10 stsp goto done;
4706 0208f208 2020-05-05 stsp fputc('\n', outfile);
4708 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4710 5191b70b 2023-01-07 mark goto done;
4712 5191b70b 2023-01-07 mark n = fprintf(outfile,
4713 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4714 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4715 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4716 5191b70b 2023-01-07 mark if (n < 0) {
4717 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
4718 5191b70b 2023-01-07 mark goto done;
4720 5191b70b 2023-01-07 mark outoff += n;
4721 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4723 5191b70b 2023-01-07 mark goto done;
4725 5191b70b 2023-01-07 mark fputc('\n', outfile);
4727 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4729 abd2672a 2018-12-23 stsp free(id_str);
4730 5943eee2 2019-08-13 stsp free(logmsg);
4731 8b473291 2019-02-21 stsp free(refs_str);
4732 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4733 7510f233 2020-08-09 stsp if (err) {
4734 c7d5c43c 2022-08-04 mark free(*lines);
4735 c7d5c43c 2022-08-04 mark *lines = NULL;
4736 ae6a6978 2020-08-09 stsp *nlines = 0;
4738 fe621944 2020-11-10 stsp return err;
4741 abd2672a 2018-12-23 stsp static const struct got_error *
4742 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4744 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4745 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
4746 15a94983 2018-12-23 stsp int obj_type;
4747 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
4748 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
4750 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
4752 c7d5c43c 2022-08-04 mark free(s->lines);
4753 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4754 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4755 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4756 fe621944 2020-11-10 stsp s->nlines = 0;
4758 511a516b 2018-05-19 stsp f = got_opentemp();
4759 48ae06ee 2018-10-18 stsp if (f == NULL) {
4760 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4761 48ae06ee 2018-10-18 stsp goto done;
4763 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
4764 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
4765 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
4766 1f3405c9 2023-01-17 mark goto done;
4768 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4769 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4770 fb43ecf1 2019-02-11 stsp goto done;
4774 15a94983 2018-12-23 stsp if (s->id1)
4775 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4777 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4779 15a94983 2018-12-23 stsp goto done;
4781 15a94983 2018-12-23 stsp switch (obj_type) {
4782 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4783 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4784 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4785 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4786 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4789 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4790 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4791 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4792 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4793 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
4795 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4796 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4797 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4798 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4799 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4800 1f3405c9 2023-01-17 mark size_t nlines = 0;
4801 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
4802 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
4803 1f3405c9 2023-01-17 mark &changed_paths,
4804 1f3405c9 2023-01-17 mark s->ignore_whitespace,
4805 1f3405c9 2023-01-17 mark s->force_text_diff,
4806 1f3405c9 2023-01-17 mark tog_diff_algo
4809 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
4810 1f3405c9 2023-01-17 mark if (lines == NULL) {
4811 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
4812 1f3405c9 2023-01-17 mark goto done;
4815 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
4816 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
4817 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4818 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
4819 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4823 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4825 3ffacbe1 2020-02-02 tracey goto done;
4826 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4827 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4828 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4829 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4830 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
4831 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4832 f44b1f58 2020-02-02 tracey if (err)
4833 f44b1f58 2020-02-02 tracey goto done;
4834 f44b1f58 2020-02-02 tracey } else {
4835 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4836 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4837 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4838 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4839 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4840 5191b70b 2023-01-07 mark s->ignore_whitespace,
4841 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4842 f44b1f58 2020-02-02 tracey if (err)
4843 f44b1f58 2020-02-02 tracey goto done;
4848 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4850 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4851 1f3405c9 2023-01-17 mark lines, nlines);
4855 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4859 1f3405c9 2023-01-17 mark free(lines);
4860 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4861 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4862 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4863 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4864 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
4865 48ae06ee 2018-10-18 stsp return err;
4868 f5215bb9 2019-02-22 stsp static void
4869 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4871 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4872 f5215bb9 2019-02-22 stsp update_panels();
4873 f5215bb9 2019-02-22 stsp doupdate();
4876 f44b1f58 2020-02-02 tracey static const struct got_error *
4877 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4879 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4881 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4882 f44b1f58 2020-02-02 tracey return NULL;
4885 eaf2c13e 2022-09-17 mark static void
4886 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4887 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4889 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4891 eaf2c13e 2022-09-17 mark *f = s->f;
4892 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4893 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4894 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4895 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4896 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4897 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4900 ec2a9698 2022-09-15 mark static const struct got_error *
4901 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4903 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4905 f44b1f58 2020-02-02 tracey int lineno;
4906 cb713507 2022-06-16 stsp char *line = NULL;
4907 826082fe 2020-12-10 stsp size_t linesize = 0;
4908 826082fe 2020-12-10 stsp ssize_t linelen;
4909 ec2a9698 2022-09-15 mark off_t *line_offsets;
4910 ec2a9698 2022-09-15 mark size_t nlines = 0;
4911 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4913 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4914 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4915 eaf2c13e 2022-09-17 mark "view search not supported");
4916 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4917 ec2a9698 2022-09-15 mark &match, &selected);
4919 f44b1f58 2020-02-02 tracey if (!view->searching) {
4920 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4921 f44b1f58 2020-02-02 tracey return NULL;
4924 ec2a9698 2022-09-15 mark if (*match) {
4925 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4926 ec2a9698 2022-09-15 mark lineno = *match + 1;
4928 ec2a9698 2022-09-15 mark lineno = *match - 1;
4930 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4932 f44b1f58 2020-02-02 tracey while (1) {
4933 f44b1f58 2020-02-02 tracey off_t offset;
4935 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4936 ec2a9698 2022-09-15 mark if (*match == 0) {
4937 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4941 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4942 f44b1f58 2020-02-02 tracey lineno = 1;
4944 ec2a9698 2022-09-15 mark lineno = nlines;
4947 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
4948 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
4949 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
4950 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
4951 f44b1f58 2020-02-02 tracey free(line);
4952 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4954 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
4955 cb713507 2022-06-16 stsp if (linelen != -1) {
4956 cb713507 2022-06-16 stsp char *exstr;
4957 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4960 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4961 cb713507 2022-06-16 stsp &view->regmatch)) {
4962 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4963 ec2a9698 2022-09-15 mark *match = lineno;
4964 cb713507 2022-06-16 stsp free(exstr);
4967 cb713507 2022-06-16 stsp free(exstr);
4969 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4970 f44b1f58 2020-02-02 tracey lineno++;
4972 f44b1f58 2020-02-02 tracey lineno--;
4974 826082fe 2020-12-10 stsp free(line);
4976 ec2a9698 2022-09-15 mark if (*match) {
4977 ec2a9698 2022-09-15 mark *first = *match;
4978 ec2a9698 2022-09-15 mark *selected = 1;
4981 145b6838 2022-06-16 stsp return err;
4984 48ae06ee 2018-10-18 stsp static const struct got_error *
4985 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4987 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4988 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4990 b72706c3 2022-06-01 stsp free(s->id1);
4991 b72706c3 2022-06-01 stsp s->id1 = NULL;
4992 b72706c3 2022-06-01 stsp free(s->id2);
4993 b72706c3 2022-06-01 stsp s->id2 = NULL;
4994 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4995 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4996 b72706c3 2022-06-01 stsp s->f = NULL;
4997 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4998 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4999 b72706c3 2022-06-01 stsp s->f1 = NULL;
5000 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5001 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5002 b72706c3 2022-06-01 stsp s->f2 = NULL;
5003 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5004 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5005 f9d37699 2022-06-28 stsp s->fd1 = -1;
5006 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5007 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5008 f9d37699 2022-06-28 stsp s->fd2 = -1;
5009 c7d5c43c 2022-08-04 mark free(s->lines);
5010 c7d5c43c 2022-08-04 mark s->lines = NULL;
5011 b72706c3 2022-06-01 stsp s->nlines = 0;
5012 b72706c3 2022-06-01 stsp return err;
5015 b72706c3 2022-06-01 stsp static const struct got_error *
5016 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5017 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5018 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5019 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
5021 48ae06ee 2018-10-18 stsp const struct got_error *err;
5022 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5024 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
5025 f9d37699 2022-06-28 stsp s->fd1 = -1;
5026 f9d37699 2022-06-28 stsp s->fd2 = -1;
5028 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5029 15a94983 2018-12-23 stsp int type1, type2;
5030 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5032 15a94983 2018-12-23 stsp return err;
5033 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5035 15a94983 2018-12-23 stsp return err;
5037 15a94983 2018-12-23 stsp if (type1 != type2)
5038 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5040 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5041 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5042 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5043 f44b1f58 2020-02-02 tracey s->repo = repo;
5044 f44b1f58 2020-02-02 tracey s->id1 = id1;
5045 f44b1f58 2020-02-02 tracey s->id2 = id2;
5046 3dbaef42 2020-11-24 stsp s->label1 = label1;
5047 3dbaef42 2020-11-24 stsp s->label2 = label2;
5049 15a94983 2018-12-23 stsp if (id1) {
5050 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5051 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5052 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5054 5465d566 2020-02-01 tracey s->id1 = NULL;
5056 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5057 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5058 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5059 b72706c3 2022-06-01 stsp goto done;
5062 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5063 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5064 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5065 a00719e9 2022-06-17 stsp goto done;
5068 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5069 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5070 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5071 b72706c3 2022-06-01 stsp goto done;
5074 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5075 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5076 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5077 f9d37699 2022-06-28 stsp goto done;
5080 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5081 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5082 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5083 f9d37699 2022-06-28 stsp goto done;
5086 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5087 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5088 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5089 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5090 5465d566 2020-02-01 tracey s->repo = repo;
5092 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5095 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5096 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5097 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5098 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5099 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5100 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5101 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5102 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5103 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5104 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5105 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5106 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5107 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5108 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5109 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5110 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5111 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5112 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5113 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5114 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5115 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5116 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5117 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5118 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5119 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5120 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5121 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5122 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5123 b72706c3 2022-06-01 stsp goto done;
5127 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5128 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5129 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5130 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5132 5465d566 2020-02-01 tracey err = create_diff(s);
5134 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5135 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5136 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5137 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5138 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5139 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5140 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5143 b72706c3 2022-06-01 stsp close_diff_view(view);
5144 e5a0f69f 2018-08-18 stsp return err;
5147 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5148 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5150 a3404814 2018-09-02 stsp const struct got_error *err;
5151 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5152 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5153 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5155 a3404814 2018-09-02 stsp if (s->id1) {
5156 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5158 a3404814 2018-09-02 stsp return err;
5159 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5161 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5163 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5165 a3404814 2018-09-02 stsp return err;
5166 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5168 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5169 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5170 a3404814 2018-09-02 stsp free(id_str1);
5171 a3404814 2018-09-02 stsp free(id_str2);
5172 a3404814 2018-09-02 stsp return err;
5174 a3404814 2018-09-02 stsp free(id_str1);
5175 a3404814 2018-09-02 stsp free(id_str2);
5177 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5178 267bb3b8 2021-08-01 stsp free(header);
5179 267bb3b8 2021-08-01 stsp return err;
5182 15a087fe 2019-02-21 stsp static const struct got_error *
5183 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5184 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5186 d7a04538 2019-02-21 stsp const struct got_error *err;
5187 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5188 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5189 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5191 15a087fe 2019-02-21 stsp free(s->id2);
5192 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5193 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5194 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5196 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5198 d7a04538 2019-02-21 stsp return err;
5199 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5200 15a087fe 2019-02-21 stsp free(s->id1);
5201 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5202 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5203 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5204 15a087fe 2019-02-21 stsp return NULL;
5207 0cf4efb1 2018-09-29 stsp static const struct got_error *
5208 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5210 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5212 917d79a7 2022-07-01 stsp view->count = 0;
5213 917d79a7 2022-07-01 stsp wclear(view->window);
5214 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5215 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5216 917d79a7 2022-07-01 stsp s->matched_line = 0;
5217 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5218 917d79a7 2022-07-01 stsp return create_diff(s);
5221 c7d5c43c 2022-08-04 mark static void
5222 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5224 c7d5c43c 2022-08-04 mark int start, i;
5226 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5228 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5229 c7d5c43c 2022-08-04 mark if (i == 0)
5230 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5231 c7d5c43c 2022-08-04 mark if (--i == start)
5232 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5235 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5236 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5239 c7d5c43c 2022-08-04 mark static void
5240 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5242 c7d5c43c 2022-08-04 mark int start, i;
5244 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5246 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5247 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5249 c7d5c43c 2022-08-04 mark if (++i == start)
5250 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5253 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5254 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5257 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5258 c0f61fa4 2022-07-11 mark int, int, int);
5259 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5260 c0f61fa4 2022-07-11 mark int, int);
5262 917d79a7 2022-07-01 stsp static const struct got_error *
5263 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5265 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5266 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5267 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5268 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5269 826082fe 2020-12-10 stsp char *line = NULL;
5270 826082fe 2020-12-10 stsp size_t linesize = 0;
5271 826082fe 2020-12-10 stsp ssize_t linelen;
5272 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5274 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5276 e5a0f69f 2018-08-18 stsp switch (ch) {
5278 145b6838 2022-06-16 stsp view->x = 0;
5281 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5282 640cd7ff 2022-06-22 mark view->count = 0;
5284 145b6838 2022-06-16 stsp case KEY_RIGHT:
5286 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5287 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5289 640cd7ff 2022-06-22 mark view->count = 0;
5291 145b6838 2022-06-16 stsp case KEY_LEFT:
5293 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5294 640cd7ff 2022-06-22 mark if (view->x <= 0)
5295 640cd7ff 2022-06-22 mark view->count = 0;
5299 3f6c6614 2023-01-23 mark if (ch == 'a') {
5300 3f6c6614 2023-01-23 mark s->force_text_diff = !s->force_text_diff;
5301 3f6c6614 2023-01-23 mark view->action = s->force_text_diff ?
5302 3f6c6614 2023-01-23 mark "force ASCII text enabled" :
5303 3f6c6614 2023-01-23 mark "force ASCII text disabled";
5305 3f6c6614 2023-01-23 mark else if (ch == 'w') {
5306 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5307 3f6c6614 2023-01-23 mark view->action = s->ignore_whitespace ?
5308 3f6c6614 2023-01-23 mark "ignore whitespace enabled" :
5309 3f6c6614 2023-01-23 mark "ignore whitespace disabled";
5311 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5314 912a3f79 2021-08-30 j case KEY_HOME:
5315 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5316 640cd7ff 2022-06-22 mark view->count = 0;
5319 912a3f79 2021-08-30 j case KEY_END:
5320 640cd7ff 2022-06-22 mark view->count = 0;
5324 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5328 1e37a5c2 2019-05-12 jcs case KEY_UP:
5329 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5330 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5331 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5333 640cd7ff 2022-06-22 mark view->count = 0;
5335 83cc4199 2022-06-13 stsp case CTRL('u'):
5337 83cc4199 2022-06-13 stsp nscroll /= 2;
5338 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5339 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5340 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5342 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5343 640cd7ff 2022-06-22 mark view->count = 0;
5347 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5348 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5351 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5352 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5353 1e37a5c2 2019-05-12 jcs if (!s->eof)
5354 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5356 640cd7ff 2022-06-22 mark view->count = 0;
5358 83cc4199 2022-06-13 stsp case CTRL('d'):
5360 83cc4199 2022-06-13 stsp nscroll /= 2;
5361 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5362 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5363 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5366 640cd7ff 2022-06-22 mark if (s->eof) {
5367 640cd7ff 2022-06-22 mark view->count = 0;
5371 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5372 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5373 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5374 826082fe 2020-12-10 stsp if (linelen == -1) {
5375 826082fe 2020-12-10 stsp if (feof(s->f)) {
5376 826082fe 2020-12-10 stsp s->eof = 1;
5378 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5382 826082fe 2020-12-10 stsp free(line);
5385 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5388 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5391 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5394 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5397 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5398 1e37a5c2 2019-05-12 jcs s->diff_context--;
5399 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5400 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5401 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5402 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5403 27829c9e 2020-11-21 stsp s->nlines) {
5404 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5405 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5408 640cd7ff 2022-06-22 mark view->count = 0;
5411 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5412 1e37a5c2 2019-05-12 jcs s->diff_context++;
5413 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5414 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5415 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5417 640cd7ff 2022-06-22 mark view->count = 0;
5423 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5427 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5428 640cd7ff 2022-06-22 mark view->count = 0;
5431 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5433 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5434 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5435 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5437 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5438 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5441 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5443 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5446 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5449 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5450 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5451 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5453 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5454 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5455 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5457 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5458 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5461 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5463 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5465 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5466 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5467 c0f61fa4 2022-07-11 mark bs->selected_line);
5468 c0f61fa4 2022-07-11 mark if (id == NULL)
5471 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5474 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5478 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5479 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5480 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5481 145b6838 2022-06-16 stsp view->x = 0;
5483 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5484 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5487 640cd7ff 2022-06-22 mark view->count = 0;
5491 bcbd79e2 2018-08-19 stsp return err;
5494 4ed7e80c 2018-05-20 stsp static const struct got_error *
5495 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5497 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5498 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5499 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5500 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5501 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5502 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5503 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5504 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5505 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5506 3dbaef42 2020-11-24 stsp const char *errstr;
5507 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5508 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5510 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5511 26ed57b2 2018-05-19 stsp switch (ch) {
5513 64453f7e 2020-11-21 stsp force_text_diff = 1;
5516 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5518 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5519 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5520 5a20d08d 2022-02-09 op errstr, errstr);
5522 09b5bff8 2020-02-23 naddy case 'r':
5523 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5524 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5525 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5527 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5530 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5533 17020d27 2019-03-07 stsp usage_diff();
5534 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5538 26ed57b2 2018-05-19 stsp argc -= optind;
5539 26ed57b2 2018-05-19 stsp argv += optind;
5541 26ed57b2 2018-05-19 stsp if (argc == 0) {
5542 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5543 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5544 15a94983 2018-12-23 stsp id_str1 = argv[0];
5545 15a94983 2018-12-23 stsp id_str2 = argv[1];
5547 26ed57b2 2018-05-19 stsp usage_diff();
5549 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5550 0ae84acc 2022-06-15 tracey if (error)
5551 0ae84acc 2022-06-15 tracey goto done;
5553 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5554 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5555 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5556 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5557 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5558 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5559 c156c7a4 2020-12-18 stsp goto done;
5560 a273ac94 2020-02-23 naddy if (worktree)
5561 a273ac94 2020-02-23 naddy repo_path =
5562 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5564 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5565 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5566 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5567 c156c7a4 2020-12-18 stsp goto done;
5571 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5572 eb6600df 2019-01-04 stsp if (error)
5573 eb6600df 2019-01-04 stsp goto done;
5575 a273ac94 2020-02-23 naddy init_curses();
5577 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5578 51a10b52 2020-12-26 stsp if (error)
5579 51a10b52 2020-12-26 stsp goto done;
5581 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5582 26ed57b2 2018-05-19 stsp if (error)
5583 26ed57b2 2018-05-19 stsp goto done;
5585 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5586 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5587 26ed57b2 2018-05-19 stsp if (error)
5588 26ed57b2 2018-05-19 stsp goto done;
5590 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5591 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5592 26ed57b2 2018-05-19 stsp if (error)
5593 26ed57b2 2018-05-19 stsp goto done;
5595 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5596 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5597 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5598 ea5e7bb5 2018-08-01 stsp goto done;
5600 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5601 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5602 5dc9f4bc 2018-08-04 stsp if (error)
5603 5dc9f4bc 2018-08-04 stsp goto done;
5604 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5606 3dbaef42 2020-11-24 stsp free(label1);
5607 3dbaef42 2020-11-24 stsp free(label2);
5608 c02c541e 2019-03-29 stsp free(repo_path);
5609 a273ac94 2020-02-23 naddy free(cwd);
5610 1d0f4054 2021-06-17 stsp if (repo) {
5611 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5612 1d0f4054 2021-06-17 stsp if (error == NULL)
5613 1d0f4054 2021-06-17 stsp error = close_err;
5615 a273ac94 2020-02-23 naddy if (worktree)
5616 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5617 0ae84acc 2022-06-15 tracey if (pack_fds) {
5618 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5619 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5620 0ae84acc 2022-06-15 tracey if (error == NULL)
5621 0ae84acc 2022-06-15 tracey error = pack_err;
5623 51a10b52 2020-12-26 stsp tog_free_refs();
5624 26ed57b2 2018-05-19 stsp return error;
5627 4ed7e80c 2018-05-20 stsp __dead static void
5628 9f7d7167 2018-04-29 stsp usage_blame(void)
5631 87411fa9 2022-06-16 stsp fprintf(stderr,
5632 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5633 9f7d7167 2018-04-29 stsp getprogname());
5637 84451b3e 2018-07-10 stsp struct tog_blame_line {
5638 84451b3e 2018-07-10 stsp int annotated;
5639 84451b3e 2018-07-10 stsp struct got_object_id *id;
5642 4ed7e80c 2018-05-20 stsp static const struct got_error *
5643 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5645 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5646 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5647 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5648 84451b3e 2018-07-10 stsp const struct got_error *err;
5649 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5650 826082fe 2020-12-10 stsp char *line = NULL;
5651 826082fe 2020-12-10 stsp size_t linesize = 0;
5652 826082fe 2020-12-10 stsp ssize_t linelen;
5653 84451b3e 2018-07-10 stsp wchar_t *wline;
5654 27a741e5 2019-09-11 stsp int width;
5655 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5656 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5657 ab089a2a 2018-07-12 stsp char *id_str;
5658 11b20872 2019-11-08 stsp struct tog_color *tc;
5660 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5662 ab089a2a 2018-07-12 stsp return err;
5664 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5665 f7d12f7e 2018-08-01 stsp werase(view->window);
5667 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5668 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5669 ab089a2a 2018-07-12 stsp free(id_str);
5670 ab089a2a 2018-07-12 stsp return err;
5673 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5674 ab089a2a 2018-07-12 stsp free(line);
5675 2550e4c3 2018-07-13 stsp line = NULL;
5677 1cae65b4 2019-09-22 stsp return err;
5678 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5679 a3404814 2018-09-02 stsp wstandout(view->window);
5680 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5682 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5683 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5684 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5685 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5687 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5688 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5689 a3404814 2018-09-02 stsp wstandend(view->window);
5690 2550e4c3 2018-07-13 stsp free(wline);
5691 2550e4c3 2018-07-13 stsp wline = NULL;
5693 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5694 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5696 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5697 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5698 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5699 ab089a2a 2018-07-12 stsp free(id_str);
5700 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5702 ab089a2a 2018-07-12 stsp free(id_str);
5703 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5704 3f60a8ef 2018-07-10 stsp free(line);
5705 2550e4c3 2018-07-13 stsp line = NULL;
5707 3f60a8ef 2018-07-10 stsp return err;
5708 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5709 2550e4c3 2018-07-13 stsp free(wline);
5710 2550e4c3 2018-07-13 stsp wline = NULL;
5711 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5712 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5714 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5715 145b6838 2022-06-16 stsp view->maxx = 0;
5716 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5717 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5718 826082fe 2020-12-10 stsp if (linelen == -1) {
5719 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5720 826082fe 2020-12-10 stsp s->eof = 1;
5723 84451b3e 2018-07-10 stsp free(line);
5724 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5726 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5728 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5731 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5732 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5733 1853e0f4 2022-06-16 stsp if (err) {
5734 1853e0f4 2022-06-16 stsp free(line);
5735 1853e0f4 2022-06-16 stsp return err;
5737 1853e0f4 2022-06-16 stsp free(wline);
5738 1853e0f4 2022-06-16 stsp wline = NULL;
5739 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5741 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5742 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5744 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5745 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5746 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5747 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5748 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5749 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5750 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5751 8d0fe45a 2019-08-12 stsp char *id_str;
5752 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5753 87411fa9 2022-06-16 stsp blame_line->id);
5754 8d0fe45a 2019-08-12 stsp if (err) {
5755 8d0fe45a 2019-08-12 stsp free(line);
5756 8d0fe45a 2019-08-12 stsp return err;
5758 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5760 11b20872 2019-11-08 stsp wattr_on(view->window,
5761 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5762 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5764 11b20872 2019-11-08 stsp wattr_off(view->window,
5765 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5766 8d0fe45a 2019-08-12 stsp free(id_str);
5767 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5769 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5770 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5773 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5774 ee41ec32 2018-07-10 stsp prev_id = NULL;
5777 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5778 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5779 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5781 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5782 41605754 2020-11-12 stsp width = 9;
5783 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5784 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5785 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5786 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5787 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5788 41605754 2020-11-12 stsp if (err) {
5789 41605754 2020-11-12 stsp free(line);
5790 41605754 2020-11-12 stsp return err;
5792 41605754 2020-11-12 stsp width += 9;
5795 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5796 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5797 4cb9d869 2022-06-16 stsp if (err) {
5798 4cb9d869 2022-06-16 stsp free(line);
5799 4cb9d869 2022-06-16 stsp return err;
5801 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5802 a75b3e2d 2022-06-16 stsp width += 9;
5803 41605754 2020-11-12 stsp free(wline);
5804 41605754 2020-11-12 stsp wline = NULL;
5807 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5808 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5809 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5810 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5812 826082fe 2020-12-10 stsp free(line);
5813 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5815 9b058f45 2022-06-30 mark view_border(view);
5817 84451b3e 2018-07-10 stsp return NULL;
5820 84451b3e 2018-07-10 stsp static const struct got_error *
5821 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5822 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5824 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5825 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5826 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5827 1a76625f 2018-10-22 stsp int errcode;
5829 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5830 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5831 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5833 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5834 1a76625f 2018-10-22 stsp if (errcode)
5835 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5837 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5838 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5839 d68a0a7d 2018-07-10 stsp goto done;
5842 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5843 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5845 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5846 d68a0a7d 2018-07-10 stsp if (line->annotated)
5847 d68a0a7d 2018-07-10 stsp goto done;
5849 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5850 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5851 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5852 84451b3e 2018-07-10 stsp goto done;
5854 84451b3e 2018-07-10 stsp line->annotated = 1;
5856 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5857 1a76625f 2018-10-22 stsp if (errcode)
5858 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5859 84451b3e 2018-07-10 stsp return err;
5862 84451b3e 2018-07-10 stsp static void *
5863 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5865 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5866 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5867 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5868 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5869 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5871 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5872 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5873 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5875 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5876 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5877 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5878 e6e73e55 2022-06-30 tracey goto done;
5881 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5882 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5883 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5884 e6e73e55 2022-06-30 tracey goto done;
5886 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5887 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5888 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5889 e6e73e55 2022-06-30 tracey goto done;
5892 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5894 e6e73e55 2022-06-30 tracey goto done;
5896 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5897 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5898 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5899 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5900 fc06ba56 2019-08-22 stsp err = NULL;
5902 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5903 e6e73e55 2022-06-30 tracey if (errcode) {
5904 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5905 e6e73e55 2022-06-30 tracey goto done;
5908 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5909 1d0f4054 2021-06-17 stsp if (err == NULL)
5910 1d0f4054 2021-06-17 stsp err = close_err;
5911 c9beca56 2018-07-22 stsp ta->repo = NULL;
5912 c9beca56 2018-07-22 stsp *ta->complete = 1;
5914 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5915 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5916 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5919 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5920 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5921 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5922 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5923 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5924 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5925 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5926 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5928 18430de3 2018-07-10 stsp return (void *)err;
5931 245d91c1 2018-07-12 stsp static struct got_object_id *
5932 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5933 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5935 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5937 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5938 8d0fe45a 2019-08-12 stsp return NULL;
5940 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5941 c0f61fa4 2022-07-11 mark if (!line->annotated)
5942 c0f61fa4 2022-07-11 mark return NULL;
5944 c0f61fa4 2022-07-11 mark return line->id;
5947 c0f61fa4 2022-07-11 mark static struct got_object_id *
5948 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5949 c0f61fa4 2022-07-11 mark int lineno)
5951 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5953 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5954 c0f61fa4 2022-07-11 mark return NULL;
5956 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5957 245d91c1 2018-07-12 stsp if (!line->annotated)
5958 245d91c1 2018-07-12 stsp return NULL;
5960 245d91c1 2018-07-12 stsp return line->id;
5963 b880a918 2018-07-10 stsp static const struct got_error *
5964 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5966 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5969 245d91c1 2018-07-12 stsp if (blame->thread) {
5970 1a76625f 2018-10-22 stsp int errcode;
5971 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5972 1a76625f 2018-10-22 stsp if (errcode)
5973 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5974 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5975 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5976 1a76625f 2018-10-22 stsp if (errcode)
5977 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5978 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5979 1a76625f 2018-10-22 stsp if (errcode)
5980 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5981 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5982 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5983 245d91c1 2018-07-12 stsp err = NULL;
5984 245d91c1 2018-07-12 stsp blame->thread = NULL;
5986 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5987 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5988 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5989 1d0f4054 2021-06-17 stsp if (err == NULL)
5990 1d0f4054 2021-06-17 stsp err = close_err;
5991 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5993 245d91c1 2018-07-12 stsp if (blame->f) {
5994 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5995 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5996 245d91c1 2018-07-12 stsp blame->f = NULL;
5998 57670559 2018-12-23 stsp if (blame->lines) {
5999 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6000 57670559 2018-12-23 stsp free(blame->lines[i].id);
6001 57670559 2018-12-23 stsp free(blame->lines);
6002 57670559 2018-12-23 stsp blame->lines = NULL;
6004 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6005 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6006 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
6007 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6008 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
6009 0ae84acc 2022-06-15 tracey if (err == NULL)
6010 0ae84acc 2022-06-15 tracey err = pack_err;
6011 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
6013 245d91c1 2018-07-12 stsp return err;
6016 245d91c1 2018-07-12 stsp static const struct got_error *
6017 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6019 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6020 fc06ba56 2019-08-22 stsp int *done = arg;
6021 fc06ba56 2019-08-22 stsp int errcode;
6023 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6024 fc06ba56 2019-08-22 stsp if (errcode)
6025 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6026 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6028 fc06ba56 2019-08-22 stsp if (*done)
6029 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6031 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6032 fc06ba56 2019-08-22 stsp if (errcode)
6033 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6034 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6036 fc06ba56 2019-08-22 stsp return err;
6039 fc06ba56 2019-08-22 stsp static const struct got_error *
6040 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6042 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6043 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6044 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6045 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6046 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6047 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6048 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6049 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6050 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6052 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6053 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6055 15a94983 2018-12-23 stsp return err;
6057 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6058 eb81bc23 2022-06-28 tracey if (fd == -1) {
6059 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6060 eb81bc23 2022-06-28 tracey goto done;
6063 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6065 a44927cc 2022-04-07 stsp goto done;
6067 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6069 84451b3e 2018-07-10 stsp goto done;
6071 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6072 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6073 84451b3e 2018-07-10 stsp goto done;
6076 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6078 a70480e0 2018-06-23 stsp goto done;
6079 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6080 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6081 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6082 84451b3e 2018-07-10 stsp goto done;
6084 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6085 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6087 1fddf795 2021-01-20 stsp goto done;
6088 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6089 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6090 84451b3e 2018-07-10 stsp goto done;
6093 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6094 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6095 b02560ec 2019-08-19 stsp blame->nlines--;
6097 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6098 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6099 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6100 84451b3e 2018-07-10 stsp goto done;
6103 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6105 bd24772e 2018-07-11 stsp goto done;
6106 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6107 0ae84acc 2022-06-15 tracey pack_fds);
6108 0ae84acc 2022-06-15 tracey if (err)
6109 0ae84acc 2022-06-15 tracey goto done;
6111 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6112 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6113 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6114 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6115 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6116 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6117 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6118 245d91c1 2018-07-12 stsp goto done;
6120 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6122 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6123 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6124 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6125 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6126 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6127 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6128 a5388363 2020-12-01 naddy s->blame_complete = 0;
6130 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6131 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6132 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6133 f5a09613 2020-12-13 naddy s->selected_line = 1;
6135 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6138 a44927cc 2022-04-07 stsp if (commit)
6139 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6140 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6141 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6143 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6144 27d434c2 2018-09-15 stsp free(obj_id);
6146 245d91c1 2018-07-12 stsp stop_blame(blame);
6147 245d91c1 2018-07-12 stsp return err;
6150 245d91c1 2018-07-12 stsp static const struct got_error *
6151 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6152 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6154 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6155 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6157 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6159 c4843652 2019-08-12 stsp s->path = strdup(path);
6160 c4843652 2019-08-12 stsp if (s->path == NULL)
6161 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6163 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6164 c4843652 2019-08-12 stsp if (err) {
6165 c4843652 2019-08-12 stsp free(s->path);
6166 7cbe629d 2018-08-04 stsp return err;
6169 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6170 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6171 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6172 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6173 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6174 fb2756b9 2018-08-04 stsp s->repo = repo;
6175 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6176 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6178 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6179 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6180 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6181 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6183 11b20872 2019-11-08 stsp return err;
6186 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6187 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6188 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6189 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6190 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6191 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6192 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6194 a5388363 2020-12-01 naddy return run_blame(view);
6197 e5a0f69f 2018-08-18 stsp static const struct got_error *
6198 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6200 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6201 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6203 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6204 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6206 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6207 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6208 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6209 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6210 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6213 e5a0f69f 2018-08-18 stsp free(s->path);
6214 11b20872 2019-11-08 stsp free_colors(&s->colors);
6215 e5a0f69f 2018-08-18 stsp return err;
6218 7cbe629d 2018-08-04 stsp static const struct got_error *
6219 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6221 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6223 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6224 6c4c42e0 2019-06-24 stsp return NULL;
6227 eaf2c13e 2022-09-17 mark static void
6228 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6229 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6231 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6233 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6234 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6235 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6236 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6237 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6238 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6239 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6242 6c4c42e0 2019-06-24 stsp static const struct got_error *
6243 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6245 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6246 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6247 2b380cc8 2018-10-24 stsp int errcode;
6249 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6250 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6251 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6252 2b380cc8 2018-10-24 stsp if (errcode)
6253 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6255 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6258 51fe7530 2019-08-19 stsp if (s->blame_complete)
6259 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6261 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6263 9b058f45 2022-06-30 mark view_border(view);
6264 e5a0f69f 2018-08-18 stsp return err;
6267 e5a0f69f 2018-08-18 stsp static const struct got_error *
6268 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6269 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6271 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6272 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6274 05f04cdf 2022-07-20 mark *new_view = NULL;
6276 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6277 05f04cdf 2022-07-20 mark if (log_view == NULL)
6278 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6280 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6282 05f04cdf 2022-07-20 mark view_close(log_view);
6284 05f04cdf 2022-07-20 mark *new_view = log_view;
6286 05f04cdf 2022-07-20 mark return err;
6289 05f04cdf 2022-07-20 mark static const struct got_error *
6290 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6292 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6293 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6294 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6295 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6297 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6298 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6299 9b058f45 2022-06-30 mark --eos; /* border */
6301 e5a0f69f 2018-08-18 stsp switch (ch) {
6303 145b6838 2022-06-16 stsp view->x = 0;
6306 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
6307 640cd7ff 2022-06-22 mark view->count = 0;
6309 145b6838 2022-06-16 stsp case KEY_RIGHT:
6311 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
6312 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
6314 640cd7ff 2022-06-22 mark view->count = 0;
6316 145b6838 2022-06-16 stsp case KEY_LEFT:
6318 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
6319 640cd7ff 2022-06-22 mark if (view->x <= 0)
6320 640cd7ff 2022-06-22 mark view->count = 0;
6323 1e37a5c2 2019-05-12 jcs s->done = 1;
6325 4deef56f 2021-09-02 naddy case 'g':
6326 4deef56f 2021-09-02 naddy case KEY_HOME:
6327 4deef56f 2021-09-02 naddy s->selected_line = 1;
6328 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6329 640cd7ff 2022-06-22 mark view->count = 0;
6331 4deef56f 2021-09-02 naddy case 'G':
6332 4deef56f 2021-09-02 naddy case KEY_END:
6333 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6334 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6335 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6337 9b058f45 2022-06-30 mark s->selected_line = eos;
6338 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6340 640cd7ff 2022-06-22 mark view->count = 0;
6343 1e37a5c2 2019-05-12 jcs case KEY_UP:
6344 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6345 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6346 1e37a5c2 2019-05-12 jcs s->selected_line--;
6347 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6348 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6349 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6351 640cd7ff 2022-06-22 mark view->count = 0;
6353 83cc4199 2022-06-13 stsp case CTRL('u'):
6355 83cc4199 2022-06-13 stsp nscroll /= 2;
6356 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6357 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6358 ea025d1d 2020-02-22 naddy case CTRL('b'):
6360 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6361 640cd7ff 2022-06-22 mark if (view->count > 1)
6362 640cd7ff 2022-06-22 mark nscroll += nscroll;
6363 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6364 640cd7ff 2022-06-22 mark view->count = 0;
6367 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6368 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6370 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6373 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6374 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6375 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6376 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6377 1e37a5c2 2019-05-12 jcs s->selected_line++;
6378 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6379 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6381 640cd7ff 2022-06-22 mark view->count = 0;
6384 1e37a5c2 2019-05-12 jcs case 'p': {
6385 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6387 640cd7ff 2022-06-22 mark view->count = 0;
6388 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6389 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6390 1e37a5c2 2019-05-12 jcs if (id == NULL)
6392 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6393 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6394 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6395 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6396 1e37a5c2 2019-05-12 jcs int obj_type;
6397 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6398 1e37a5c2 2019-05-12 jcs s->repo, id);
6401 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6402 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6403 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6404 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6407 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6408 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6409 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6412 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6413 a44927cc 2022-04-07 stsp pcommit, s->path);
6414 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6415 e5a0f69f 2018-08-18 stsp if (err) {
6416 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6417 1e37a5c2 2019-05-12 jcs err = NULL;
6418 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6421 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6423 1e37a5c2 2019-05-12 jcs free(blob_id);
6424 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6425 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6426 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6429 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6430 d7b5a0e8 2022-04-20 stsp &pid->id);
6431 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6433 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6434 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6436 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6441 1e37a5c2 2019-05-12 jcs s->done = 1;
6442 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6443 1e37a5c2 2019-05-12 jcs s->done = 0;
6444 1e37a5c2 2019-05-12 jcs if (thread_err)
6446 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6447 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6448 a5388363 2020-12-01 naddy err = run_blame(view);
6453 61417565 2022-06-20 mark case 'C': {
6454 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6456 640cd7ff 2022-06-22 mark view->count = 0;
6457 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6458 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6460 1e37a5c2 2019-05-12 jcs s->done = 1;
6461 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6462 1e37a5c2 2019-05-12 jcs s->done = 0;
6463 1e37a5c2 2019-05-12 jcs if (thread_err)
6465 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6466 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6467 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6468 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6469 a5388363 2020-12-01 naddy err = run_blame(view);
6475 05f04cdf 2022-07-20 mark view->count = 0;
6476 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6477 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6478 136e2bd4 2022-07-23 mark if (s->id_to_log)
6479 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6481 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6482 1e37a5c2 2019-05-12 jcs case '\r': {
6483 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6484 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6485 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6487 640cd7ff 2022-06-22 mark view->count = 0;
6488 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6489 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6490 1e37a5c2 2019-05-12 jcs if (id == NULL)
6492 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6495 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6496 c0f61fa4 2022-07-11 mark if (*new_view) {
6497 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6498 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6501 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6503 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6504 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6506 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6507 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6508 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6509 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6510 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6514 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6515 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6516 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6518 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6521 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6522 c0f61fa4 2022-07-11 mark s->selected_line;
6523 c0f61fa4 2022-07-11 mark if (*new_view)
6524 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6525 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6526 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6527 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6532 e78dc838 2020-12-04 stsp view->focussed = 0;
6533 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6534 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6535 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6536 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6537 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6538 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6541 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6544 e78dc838 2020-12-04 stsp view->focus_child = 1;
6546 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6551 83cc4199 2022-06-13 stsp case CTRL('d'):
6553 83cc4199 2022-06-13 stsp nscroll /= 2;
6554 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6555 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6556 ea025d1d 2020-02-22 naddy case CTRL('f'):
6559 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6560 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6561 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6562 640cd7ff 2022-06-22 mark view->count = 0;
6565 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6566 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6567 83cc4199 2022-06-13 stsp s->selected_line +=
6568 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6569 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6571 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6572 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6574 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6575 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6577 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6578 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6579 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6580 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6584 640cd7ff 2022-06-22 mark view->count = 0;
6587 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6590 917d79a7 2022-07-01 stsp static const struct got_error *
6591 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6593 917d79a7 2022-07-01 stsp const struct got_error *err;
6594 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6596 917d79a7 2022-07-01 stsp view->count = 0;
6597 917d79a7 2022-07-01 stsp s->done = 1;
6598 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6599 917d79a7 2022-07-01 stsp s->done = 0;
6601 917d79a7 2022-07-01 stsp return err;
6602 917d79a7 2022-07-01 stsp return run_blame(view);
6605 a70480e0 2018-06-23 stsp static const struct got_error *
6606 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6608 a70480e0 2018-06-23 stsp const struct got_error *error;
6609 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6610 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6611 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6612 0587e10c 2020-07-23 stsp char *link_target = NULL;
6613 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6614 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6615 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6617 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6618 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6620 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6621 a70480e0 2018-06-23 stsp switch (ch) {
6623 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6626 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6627 69069811 2018-08-02 stsp if (repo_path == NULL)
6628 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6632 17020d27 2019-03-07 stsp usage_blame();
6633 a70480e0 2018-06-23 stsp /* NOTREACHED */
6637 a70480e0 2018-06-23 stsp argc -= optind;
6638 a70480e0 2018-06-23 stsp argv += optind;
6640 f135c941 2020-02-20 stsp if (argc != 1)
6641 a70480e0 2018-06-23 stsp usage_blame();
6643 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6644 0ae84acc 2022-06-15 tracey if (error != NULL)
6645 0ae84acc 2022-06-15 tracey goto done;
6647 69069811 2018-08-02 stsp if (repo_path == NULL) {
6648 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6649 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6650 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6651 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6652 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6653 c156c7a4 2020-12-18 stsp goto done;
6654 f135c941 2020-02-20 stsp if (worktree)
6655 eb41ed75 2019-02-05 stsp repo_path =
6656 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6658 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6659 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6660 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6661 c156c7a4 2020-12-18 stsp goto done;
6665 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6666 c02c541e 2019-03-29 stsp if (error != NULL)
6667 8e94dd5b 2019-01-04 stsp goto done;
6669 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6670 f135c941 2020-02-20 stsp worktree);
6671 c02c541e 2019-03-29 stsp if (error)
6672 92205607 2019-01-04 stsp goto done;
6674 f135c941 2020-02-20 stsp init_curses();
6676 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6677 51a10b52 2020-12-26 stsp if (error)
6678 51a10b52 2020-12-26 stsp goto done;
6680 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6681 eb41ed75 2019-02-05 stsp if (error)
6682 69069811 2018-08-02 stsp goto done;
6684 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6685 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6686 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6687 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6688 a70480e0 2018-06-23 stsp if (error != NULL)
6689 66b4983c 2018-06-23 stsp goto done;
6690 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6691 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6693 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6694 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6696 a19e88aa 2018-06-23 stsp if (error != NULL)
6697 8b473291 2019-02-21 stsp goto done;
6699 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6700 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6701 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6702 e1cd8fed 2018-08-01 stsp goto done;
6705 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6706 a44927cc 2022-04-07 stsp if (error)
6707 a44927cc 2022-04-07 stsp goto done;
6709 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6710 a44927cc 2022-04-07 stsp commit, repo);
6711 7cbe629d 2018-08-04 stsp if (error)
6712 7cbe629d 2018-08-04 stsp goto done;
6714 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6715 78756c87 2020-11-24 stsp commit_id, repo);
6716 0587e10c 2020-07-23 stsp if (error)
6717 0587e10c 2020-07-23 stsp goto done;
6718 12314ad4 2019-08-31 stsp if (worktree) {
6719 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6720 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6721 12314ad4 2019-08-31 stsp worktree = NULL;
6723 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6725 69069811 2018-08-02 stsp free(repo_path);
6726 f135c941 2020-02-20 stsp free(in_repo_path);
6727 0587e10c 2020-07-23 stsp free(link_target);
6728 69069811 2018-08-02 stsp free(cwd);
6729 a70480e0 2018-06-23 stsp free(commit_id);
6730 a44927cc 2022-04-07 stsp if (commit)
6731 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6732 eb41ed75 2019-02-05 stsp if (worktree)
6733 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6734 1d0f4054 2021-06-17 stsp if (repo) {
6735 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6736 1d0f4054 2021-06-17 stsp if (error == NULL)
6737 1d0f4054 2021-06-17 stsp error = close_err;
6739 0ae84acc 2022-06-15 tracey if (pack_fds) {
6740 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6741 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6742 0ae84acc 2022-06-15 tracey if (error == NULL)
6743 0ae84acc 2022-06-15 tracey error = pack_err;
6745 51a10b52 2020-12-26 stsp tog_free_refs();
6746 a70480e0 2018-06-23 stsp return error;
6749 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6750 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6752 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6753 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6754 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6755 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6756 0c6ad1bc 2022-09-09 mark char *index = NULL;
6757 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6758 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6759 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6761 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6762 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6763 9b058f45 2022-06-30 mark --limit; /* border */
6765 f7d12f7e 2018-08-01 stsp werase(view->window);
6767 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6768 ffd1d5e5 2018-06-23 stsp return NULL;
6770 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6773 ffd1d5e5 2018-06-23 stsp return err;
6774 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6775 a3404814 2018-09-02 stsp wstandout(view->window);
6776 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6778 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6779 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6780 0c6ad1bc 2022-09-09 mark free(wline);
6781 0c6ad1bc 2022-09-09 mark wline = NULL;
6782 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6783 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6785 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6786 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6787 a3404814 2018-09-02 stsp wstandend(view->window);
6788 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6789 0c6ad1bc 2022-09-09 mark return NULL;
6791 df68a56b 2022-08-12 mark i += s->selected;
6792 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6793 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6794 df68a56b 2022-08-12 mark if (s->tree != s->root)
6795 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6797 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6798 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6799 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6800 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6801 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6802 0c6ad1bc 2022-09-09 mark free(index);
6804 ce52c690 2018-06-23 stsp return err;
6805 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6806 2550e4c3 2018-07-13 stsp free(wline);
6807 2550e4c3 2018-07-13 stsp wline = NULL;
6808 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6809 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6810 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6811 ffd1d5e5 2018-06-23 stsp return NULL;
6812 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6813 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6814 a1eca9bb 2018-06-23 stsp return NULL;
6816 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6817 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6818 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6819 0cf4efb1 2018-09-29 stsp if (view->focussed)
6820 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6821 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6823 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6824 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6825 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6826 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6827 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6828 ffd1d5e5 2018-06-23 stsp return NULL;
6832 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6835 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6836 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6837 848d6979 2019-08-12 stsp const char *modestr = "";
6838 56e0773d 2019-11-28 stsp mode_t mode;
6840 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6841 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6843 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6844 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6845 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6847 638f9024 2019-05-13 stsp return got_error_from_errno(
6848 230a42bd 2019-05-11 jcs "got_object_id_str");
6850 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6851 63c5ca5d 2019-08-24 stsp modestr = "$";
6852 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6855 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6856 d86d3b18 2020-12-01 naddy te, s->repo);
6857 0d6c6ee3 2020-05-20 stsp if (err) {
6858 0d6c6ee3 2020-05-20 stsp free(id_str);
6859 0d6c6ee3 2020-05-20 stsp return err;
6861 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6862 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6863 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6865 848d6979 2019-08-12 stsp modestr = "@";
6867 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6868 848d6979 2019-08-12 stsp modestr = "/";
6869 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6870 848d6979 2019-08-12 stsp modestr = "*";
6871 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6872 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6873 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6874 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6875 1d13200f 2018-07-12 stsp free(id_str);
6876 0d6c6ee3 2020-05-20 stsp free(link_target);
6877 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6879 1d13200f 2018-07-12 stsp free(id_str);
6880 0d6c6ee3 2020-05-20 stsp free(link_target);
6881 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6883 ffd1d5e5 2018-06-23 stsp if (err) {
6884 ffd1d5e5 2018-06-23 stsp free(line);
6887 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6888 0cf4efb1 2018-09-29 stsp if (view->focussed)
6889 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6890 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6892 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6894 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6895 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6896 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6898 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6899 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6900 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6901 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6902 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6903 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6904 ffd1d5e5 2018-06-23 stsp free(line);
6905 2550e4c3 2018-07-13 stsp free(wline);
6906 2550e4c3 2018-07-13 stsp wline = NULL;
6908 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6909 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6910 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6914 ffd1d5e5 2018-06-23 stsp return err;
6917 ffd1d5e5 2018-06-23 stsp static void
6918 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6920 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6921 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6922 fa86c4bf 2020-11-29 stsp int i = 0;
6924 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6927 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6928 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6929 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6930 fa86c4bf 2020-11-29 stsp if (!isroot)
6931 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6934 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6935 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6939 9b058f45 2022-06-30 mark static const struct got_error *
6940 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6942 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6943 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6944 ffd1d5e5 2018-06-23 stsp int n = 0;
6946 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6947 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6948 694d3271 2020-12-01 naddy s->first_displayed_entry);
6950 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6952 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6953 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6954 94b80cfa 2022-08-01 mark if (last) {
6955 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6956 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6958 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6959 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6960 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6964 9b058f45 2022-06-30 mark return NULL;
6967 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6968 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6969 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6971 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6972 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6973 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6975 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6976 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6977 56e0773d 2019-11-28 stsp + 1 /* slash */;
6979 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6981 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6982 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6983 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6985 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6986 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6987 d9765a41 2018-06-23 stsp while (pt) {
6988 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6989 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6990 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6991 cb2ebc8a 2018-06-23 stsp goto done;
6993 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6994 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6995 cb2ebc8a 2018-06-23 stsp goto done;
6997 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7000 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7001 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7002 ce52c690 2018-06-23 stsp goto done;
7006 ce52c690 2018-06-23 stsp if (err) {
7007 ce52c690 2018-06-23 stsp free(*path);
7008 ce52c690 2018-06-23 stsp *path = NULL;
7010 ce52c690 2018-06-23 stsp return err;
7013 ce52c690 2018-06-23 stsp static const struct got_error *
7014 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7015 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7016 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7018 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7019 ce52c690 2018-06-23 stsp char *path;
7020 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7022 a0de39f3 2019-08-09 stsp *new_view = NULL;
7024 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7026 ce52c690 2018-06-23 stsp return err;
7028 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7029 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7030 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7031 83ce39e3 2019-08-12 stsp goto done;
7034 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7035 e5a0f69f 2018-08-18 stsp if (err) {
7036 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7037 fc06ba56 2019-08-22 stsp err = NULL;
7038 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7040 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7042 83ce39e3 2019-08-12 stsp free(path);
7043 69efd4c4 2018-07-18 stsp return err;
7046 69efd4c4 2018-07-18 stsp static const struct got_error *
7047 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7048 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7050 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7051 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7052 69efd4c4 2018-07-18 stsp char *path;
7054 a0de39f3 2019-08-09 stsp *new_view = NULL;
7056 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7057 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7058 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7060 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7062 69efd4c4 2018-07-18 stsp return err;
7064 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7067 e5a0f69f 2018-08-18 stsp view_close(log_view);
7069 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7070 cb2ebc8a 2018-06-23 stsp free(path);
7071 cb2ebc8a 2018-06-23 stsp return err;
7074 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7075 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7076 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7078 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7079 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7080 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7081 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7083 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7084 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7086 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7087 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7088 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7090 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7092 bc573f3b 2021-07-10 stsp goto done;
7095 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7096 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7097 bc573f3b 2021-07-10 stsp * closed on demand.
7099 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7100 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7102 bc573f3b 2021-07-10 stsp goto done;
7103 bc573f3b 2021-07-10 stsp s->tree = s->root;
7105 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7106 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7107 ffd1d5e5 2018-06-23 stsp goto done;
7109 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7110 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7111 ffd1d5e5 2018-06-23 stsp goto done;
7114 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7115 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7116 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7117 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7118 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7119 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7120 9cd7cbd1 2020-12-07 stsp goto done;
7123 fb2756b9 2018-08-04 stsp s->repo = repo;
7125 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7126 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7127 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7128 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7130 c0b01bdb 2019-11-08 stsp goto done;
7131 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7132 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7134 c0b01bdb 2019-11-08 stsp goto done;
7135 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7136 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7137 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7139 c0b01bdb 2019-11-08 stsp goto done;
7141 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7142 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7143 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7145 11b20872 2019-11-08 stsp goto done;
7147 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7148 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7150 c0b01bdb 2019-11-08 stsp goto done;
7153 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7154 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7155 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7156 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7157 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7159 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7160 bc573f3b 2021-07-10 stsp if (commit)
7161 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7163 bc573f3b 2021-07-10 stsp close_tree_view(view);
7164 ad80ab7b 2018-08-04 stsp return err;
7167 e5a0f69f 2018-08-18 stsp static const struct got_error *
7168 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7170 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7172 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7173 fb2756b9 2018-08-04 stsp free(s->tree_label);
7174 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7175 6484ec90 2018-09-29 stsp free(s->commit_id);
7176 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7177 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7178 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7179 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7180 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7181 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7182 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7183 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7184 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7185 ad80ab7b 2018-08-04 stsp free(parent);
7188 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7189 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7190 bc573f3b 2021-07-10 stsp if (s->root)
7191 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7192 7c32bd05 2019-06-22 stsp return NULL;
7195 7c32bd05 2019-06-22 stsp static const struct got_error *
7196 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7198 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7200 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7201 7c32bd05 2019-06-22 stsp return NULL;
7204 7c32bd05 2019-06-22 stsp static int
7205 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7207 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7209 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7213 7c32bd05 2019-06-22 stsp static const struct got_error *
7214 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7216 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7217 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7219 7c32bd05 2019-06-22 stsp if (!view->searching) {
7220 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7221 7c32bd05 2019-06-22 stsp return NULL;
7224 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7225 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7226 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7227 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7228 56e0773d 2019-11-28 stsp s->selected_entry);
7230 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7232 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7233 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7235 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7236 56e0773d 2019-11-28 stsp s->selected_entry);
7239 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7240 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7241 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7242 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7244 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7247 7c32bd05 2019-06-22 stsp while (1) {
7248 56e0773d 2019-11-28 stsp if (te == NULL) {
7249 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7250 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7251 ac66afb8 2019-06-24 stsp return NULL;
7253 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7254 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7256 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7259 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7260 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7261 56e0773d 2019-11-28 stsp s->matched_entry = te;
7265 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7266 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7268 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7271 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7272 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7273 7c32bd05 2019-06-22 stsp s->selected = 0;
7276 e5a0f69f 2018-08-18 stsp return NULL;
7279 ad80ab7b 2018-08-04 stsp static const struct got_error *
7280 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7282 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7283 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7284 e5a0f69f 2018-08-18 stsp char *parent_path;
7286 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7288 e5a0f69f 2018-08-18 stsp return err;
7290 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7291 e5a0f69f 2018-08-18 stsp free(parent_path);
7293 9b058f45 2022-06-30 mark view_border(view);
7294 e5a0f69f 2018-08-18 stsp return err;
7297 94b80cfa 2022-08-01 mark static const struct got_error *
7298 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7300 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7301 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7302 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7303 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7304 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7305 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7307 94b80cfa 2022-08-01 mark g = view->gline;
7308 94b80cfa 2022-08-01 mark view->gline = 0;
7310 94b80cfa 2022-08-01 mark if (g == 0)
7312 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7313 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7315 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7316 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7317 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7319 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7320 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7321 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7323 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7324 94b80cfa 2022-08-01 mark last += off;
7326 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7327 94b80cfa 2022-08-01 mark s->selected = g - first;
7328 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7331 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7332 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7336 94b80cfa 2022-08-01 mark if (i < g) {
7337 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7339 94b80cfa 2022-08-01 mark return err;
7340 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7341 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7342 94b80cfa 2022-08-01 mark first + s->selected < g &&
7343 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7344 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7345 94b80cfa 2022-08-01 mark first += off;
7346 94b80cfa 2022-08-01 mark s->selected = g - first;
7348 94b80cfa 2022-08-01 mark } else if (i > g)
7349 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7351 94b80cfa 2022-08-01 mark if (g < nlines &&
7352 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7353 94b80cfa 2022-08-01 mark s->selected = g - 1;
7355 94b80cfa 2022-08-01 mark return NULL;
7358 e5a0f69f 2018-08-18 stsp static const struct got_error *
7359 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7361 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7362 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7363 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7364 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7366 94b80cfa 2022-08-01 mark if (view->gline)
7367 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7369 e5a0f69f 2018-08-18 stsp switch (ch) {
7371 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7372 640cd7ff 2022-06-22 mark view->count = 0;
7375 640cd7ff 2022-06-22 mark view->count = 0;
7376 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7378 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7381 640cd7ff 2022-06-22 mark view->count = 0;
7382 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7384 e4526bf5 2021-09-03 naddy case 'g':
7386 e4526bf5 2021-09-03 naddy case KEY_HOME:
7387 e4526bf5 2021-09-03 naddy s->selected = 0;
7388 640cd7ff 2022-06-22 mark view->count = 0;
7389 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7390 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7391 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7393 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7395 e4526bf5 2021-09-03 naddy case 'G':
7397 9b058f45 2022-06-30 mark case KEY_END: {
7398 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7400 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7401 9b058f45 2022-06-30 mark --eos; /* border */
7402 e4526bf5 2021-09-03 naddy s->selected = 0;
7403 640cd7ff 2022-06-22 mark view->count = 0;
7404 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7405 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7406 e4526bf5 2021-09-03 naddy if (te == NULL) {
7407 ad055527 2022-07-16 mark if (s->tree != s->root) {
7408 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7413 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7414 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7416 e4526bf5 2021-09-03 naddy if (n > 0)
7417 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7421 1e37a5c2 2019-05-12 jcs case KEY_UP:
7422 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7423 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7424 1e37a5c2 2019-05-12 jcs s->selected--;
7427 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7428 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7429 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7430 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7431 640cd7ff 2022-06-22 mark view->count = 0;
7433 83cc4199 2022-06-13 stsp case CTRL('u'):
7435 83cc4199 2022-06-13 stsp nscroll /= 2;
7436 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7437 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7438 ea025d1d 2020-02-22 naddy case CTRL('b'):
7440 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7441 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7442 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7443 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7445 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7446 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7448 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7449 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7450 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7451 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7452 640cd7ff 2022-06-22 mark view->count = 0;
7455 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7456 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7457 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7458 1e37a5c2 2019-05-12 jcs s->selected++;
7461 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7462 640cd7ff 2022-06-22 mark == NULL) {
7463 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7464 640cd7ff 2022-06-22 mark view->count = 0;
7467 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7469 83cc4199 2022-06-13 stsp case CTRL('d'):
7471 83cc4199 2022-06-13 stsp nscroll /= 2;
7472 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7473 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7474 ea025d1d 2020-02-22 naddy case CTRL('f'):
7476 48bb96f0 2022-06-20 naddy case ' ':
7477 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7479 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7480 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7481 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7482 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7484 640cd7ff 2022-06-22 mark view->count = 0;
7487 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7489 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7491 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7492 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7493 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7494 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7495 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7496 640cd7ff 2022-06-22 mark view->count = 0;
7499 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7500 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7502 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7503 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7504 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7505 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7506 1e37a5c2 2019-05-12 jcs s->selected_entry =
7507 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7508 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7509 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7510 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7514 1e37a5c2 2019-05-12 jcs free(parent);
7515 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7516 56e0773d 2019-11-28 stsp s->selected_entry))) {
7517 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7518 640cd7ff 2022-06-22 mark view->count = 0;
7519 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7520 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7523 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7524 941e9f74 2019-05-21 stsp if (err) {
7525 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7528 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7529 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7531 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7532 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7533 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7534 640cd7ff 2022-06-22 mark view->count = 0;
7537 640cd7ff 2022-06-22 mark view->count = 0;
7541 ffd1d5e5 2018-06-23 stsp return err;
7544 ffd1d5e5 2018-06-23 stsp __dead static void
7545 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7548 87411fa9 2022-06-16 stsp fprintf(stderr,
7549 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7550 ffd1d5e5 2018-06-23 stsp getprogname());
7554 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7555 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7557 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7558 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7559 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7560 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7561 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7562 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7563 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7564 4e97c21c 2020-12-06 stsp char *label = NULL;
7565 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7566 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7568 5221c383 2018-08-01 stsp struct tog_view *view;
7569 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7571 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7572 ffd1d5e5 2018-06-23 stsp switch (ch) {
7574 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7577 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7578 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7579 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7583 e99e2d15 2020-11-24 naddy usage_tree();
7584 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7588 ffd1d5e5 2018-06-23 stsp argc -= optind;
7589 ffd1d5e5 2018-06-23 stsp argv += optind;
7591 55cccc34 2020-02-20 stsp if (argc > 1)
7592 e99e2d15 2020-11-24 naddy usage_tree();
7594 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7595 0ae84acc 2022-06-15 tracey if (error != NULL)
7596 0ae84acc 2022-06-15 tracey goto done;
7598 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7599 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7600 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7601 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7602 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7603 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7604 c156c7a4 2020-12-18 stsp goto done;
7605 55cccc34 2020-02-20 stsp if (worktree)
7606 52185f70 2019-02-05 stsp repo_path =
7607 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7609 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7610 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7611 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7612 c156c7a4 2020-12-18 stsp goto done;
7616 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7617 c02c541e 2019-03-29 stsp if (error != NULL)
7618 52185f70 2019-02-05 stsp goto done;
7620 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7621 55cccc34 2020-02-20 stsp repo, worktree);
7622 55cccc34 2020-02-20 stsp if (error)
7623 55cccc34 2020-02-20 stsp goto done;
7625 55cccc34 2020-02-20 stsp init_curses();
7627 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7628 c02c541e 2019-03-29 stsp if (error)
7629 52185f70 2019-02-05 stsp goto done;
7631 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7632 51a10b52 2020-12-26 stsp if (error)
7633 51a10b52 2020-12-26 stsp goto done;
7635 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7636 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7637 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7638 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7639 4e97c21c 2020-12-06 stsp if (error)
7640 4e97c21c 2020-12-06 stsp goto done;
7641 4e97c21c 2020-12-06 stsp head_ref_name = label;
7643 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7644 4e97c21c 2020-12-06 stsp if (error == NULL)
7645 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7646 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7647 4e97c21c 2020-12-06 stsp goto done;
7648 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7649 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7650 4e97c21c 2020-12-06 stsp if (error)
7651 4e97c21c 2020-12-06 stsp goto done;
7654 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7655 a44927cc 2022-04-07 stsp if (error)
7656 a44927cc 2022-04-07 stsp goto done;
7658 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7659 5221c383 2018-08-01 stsp if (view == NULL) {
7660 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7661 5221c383 2018-08-01 stsp goto done;
7663 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7664 ad80ab7b 2018-08-04 stsp if (error)
7665 ad80ab7b 2018-08-04 stsp goto done;
7666 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7667 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7668 d91faf3b 2020-12-01 naddy in_repo_path);
7669 55cccc34 2020-02-20 stsp if (error)
7670 55cccc34 2020-02-20 stsp goto done;
7673 55cccc34 2020-02-20 stsp if (worktree) {
7674 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7675 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7676 55cccc34 2020-02-20 stsp worktree = NULL;
7678 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7680 52185f70 2019-02-05 stsp free(repo_path);
7681 e4a0e26d 2020-02-20 stsp free(cwd);
7682 ffd1d5e5 2018-06-23 stsp free(commit_id);
7683 4e97c21c 2020-12-06 stsp free(label);
7685 486cd271 2020-12-06 stsp got_ref_close(ref);
7686 1d0f4054 2021-06-17 stsp if (repo) {
7687 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7688 1d0f4054 2021-06-17 stsp if (error == NULL)
7689 1d0f4054 2021-06-17 stsp error = close_err;
7691 0ae84acc 2022-06-15 tracey if (pack_fds) {
7692 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7693 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7694 0ae84acc 2022-06-15 tracey if (error == NULL)
7695 0ae84acc 2022-06-15 tracey error = pack_err;
7697 51a10b52 2020-12-26 stsp tog_free_refs();
7698 ffd1d5e5 2018-06-23 stsp return error;
7701 6458efa5 2020-11-24 stsp static const struct got_error *
7702 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7704 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7705 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7707 6458efa5 2020-11-24 stsp s->nrefs = 0;
7708 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7709 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7710 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7711 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7712 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7715 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7716 6458efa5 2020-11-24 stsp if (re == NULL)
7717 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7719 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7720 8924d611 2020-12-26 stsp if (re->ref == NULL)
7721 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7722 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7723 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7726 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7727 6458efa5 2020-11-24 stsp return NULL;
7731 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7733 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7735 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7736 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7737 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7738 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7743 6458efa5 2020-11-24 stsp static const struct got_error *
7744 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7746 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7747 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7749 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7750 6458efa5 2020-11-24 stsp s->repo = repo;
7752 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7753 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7755 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7757 6458efa5 2020-11-24 stsp return err;
7759 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7760 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7761 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7762 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7764 6458efa5 2020-11-24 stsp goto done;
7766 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7767 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7768 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7770 6458efa5 2020-11-24 stsp goto done;
7772 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7773 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7774 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7776 cc488aa7 2022-01-23 stsp goto done;
7778 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7779 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7780 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7782 6458efa5 2020-11-24 stsp goto done;
7785 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7786 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7787 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7788 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7789 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7792 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7793 6458efa5 2020-11-24 stsp return err;
7796 6458efa5 2020-11-24 stsp static const struct got_error *
7797 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7799 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7801 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7802 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7804 6458efa5 2020-11-24 stsp return NULL;
7807 6458efa5 2020-11-24 stsp static const struct got_error *
7808 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7809 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7811 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7812 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7813 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7814 6458efa5 2020-11-24 stsp int obj_type;
7816 c42c9805 2020-11-24 stsp *commit_id = NULL;
7818 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7820 6458efa5 2020-11-24 stsp return err;
7822 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7824 6458efa5 2020-11-24 stsp goto done;
7826 6458efa5 2020-11-24 stsp switch (obj_type) {
7827 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7828 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7830 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7831 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7833 6458efa5 2020-11-24 stsp goto done;
7834 c42c9805 2020-11-24 stsp free(obj_id);
7835 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7836 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7838 6458efa5 2020-11-24 stsp goto done;
7839 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7840 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7841 c42c9805 2020-11-24 stsp goto done;
7843 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7844 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7845 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7846 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7847 c42c9805 2020-11-24 stsp goto done;
7851 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7857 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7858 c42c9805 2020-11-24 stsp if (err) {
7859 c42c9805 2020-11-24 stsp free(*commit_id);
7860 c42c9805 2020-11-24 stsp *commit_id = NULL;
7862 c42c9805 2020-11-24 stsp return err;
7865 c42c9805 2020-11-24 stsp static const struct got_error *
7866 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7867 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7869 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7870 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7871 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7873 c42c9805 2020-11-24 stsp *new_view = NULL;
7875 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7876 c42c9805 2020-11-24 stsp if (err) {
7877 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7878 c42c9805 2020-11-24 stsp return err;
7880 c42c9805 2020-11-24 stsp return NULL;
7883 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7884 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7885 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7886 6458efa5 2020-11-24 stsp goto done;
7889 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7890 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7893 6458efa5 2020-11-24 stsp view_close(log_view);
7895 6458efa5 2020-11-24 stsp *new_view = log_view;
7896 c42c9805 2020-11-24 stsp free(commit_id);
7897 6458efa5 2020-11-24 stsp return err;
7900 ce5b7c56 2019-07-09 stsp static void
7901 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7903 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7904 34ba6917 2020-11-30 stsp int i = 0;
7906 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7909 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7910 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7911 34ba6917 2020-11-30 stsp if (re == NULL)
7913 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7914 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7918 9b058f45 2022-06-30 mark static const struct got_error *
7919 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7921 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7922 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7923 6458efa5 2020-11-24 stsp int n = 0;
7925 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7926 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7928 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7930 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7931 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7932 94b80cfa 2022-08-01 mark if (last) {
7933 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7934 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7936 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7937 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7938 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7942 9b058f45 2022-06-30 mark return NULL;
7945 6458efa5 2020-11-24 stsp static const struct got_error *
7946 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7948 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7950 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7951 6458efa5 2020-11-24 stsp return NULL;
7954 6458efa5 2020-11-24 stsp static int
7955 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7957 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7959 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7963 6458efa5 2020-11-24 stsp static const struct got_error *
7964 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7966 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7967 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7969 6458efa5 2020-11-24 stsp if (!view->searching) {
7970 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7971 6458efa5 2020-11-24 stsp return NULL;
7974 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7975 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7976 6458efa5 2020-11-24 stsp if (s->selected_entry)
7977 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7979 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7980 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7982 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7983 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7985 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7986 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7989 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7990 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7991 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7992 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7994 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7997 6458efa5 2020-11-24 stsp while (1) {
7998 6458efa5 2020-11-24 stsp if (re == NULL) {
7999 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8000 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8001 6458efa5 2020-11-24 stsp return NULL;
8003 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8004 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8006 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8009 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8010 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8011 6458efa5 2020-11-24 stsp s->matched_entry = re;
8015 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8016 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8018 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8021 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8022 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8023 6458efa5 2020-11-24 stsp s->selected = 0;
8026 6458efa5 2020-11-24 stsp return NULL;
8029 6458efa5 2020-11-24 stsp static const struct got_error *
8030 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8032 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8033 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8034 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8035 6458efa5 2020-11-24 stsp char *line = NULL;
8036 6458efa5 2020-11-24 stsp wchar_t *wline;
8037 6458efa5 2020-11-24 stsp struct tog_color *tc;
8038 6458efa5 2020-11-24 stsp int width, n;
8039 6458efa5 2020-11-24 stsp int limit = view->nlines;
8041 6458efa5 2020-11-24 stsp werase(view->window);
8043 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8044 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8045 9b058f45 2022-06-30 mark --limit; /* border */
8047 6458efa5 2020-11-24 stsp if (limit == 0)
8048 6458efa5 2020-11-24 stsp return NULL;
8050 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8052 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8053 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8054 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8056 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8057 6458efa5 2020-11-24 stsp if (err) {
8058 6458efa5 2020-11-24 stsp free(line);
8059 6458efa5 2020-11-24 stsp return err;
8061 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8062 6458efa5 2020-11-24 stsp wstandout(view->window);
8063 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8064 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8065 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8066 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8067 6458efa5 2020-11-24 stsp wstandend(view->window);
8068 6458efa5 2020-11-24 stsp free(wline);
8069 6458efa5 2020-11-24 stsp wline = NULL;
8070 6458efa5 2020-11-24 stsp free(line);
8071 6458efa5 2020-11-24 stsp line = NULL;
8072 6458efa5 2020-11-24 stsp if (--limit <= 0)
8073 6458efa5 2020-11-24 stsp return NULL;
8076 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8077 6458efa5 2020-11-24 stsp char *line = NULL;
8078 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8080 b4996bee 2022-06-16 stsp if (s->show_date) {
8081 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8082 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8083 b4996bee 2022-06-16 stsp struct got_object_id *id;
8084 b4996bee 2022-06-16 stsp struct tm tm;
8087 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8089 b4996bee 2022-06-16 stsp return err;
8090 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8091 b4996bee 2022-06-16 stsp if (err) {
8092 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8094 b4996bee 2022-06-16 stsp return err;
8096 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8098 b4996bee 2022-06-16 stsp if (err) {
8100 b4996bee 2022-06-16 stsp return err;
8102 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8103 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8105 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8106 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8109 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8110 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8111 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8112 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8114 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8115 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8116 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8117 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8118 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8119 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8120 6458efa5 2020-11-24 stsp struct got_object_id *id;
8121 6458efa5 2020-11-24 stsp char *id_str;
8122 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8124 6458efa5 2020-11-24 stsp return err;
8125 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8126 6458efa5 2020-11-24 stsp if (err) {
8128 6458efa5 2020-11-24 stsp return err;
8130 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8131 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8132 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8134 6458efa5 2020-11-24 stsp free(id_str);
8135 6458efa5 2020-11-24 stsp return err;
8138 6458efa5 2020-11-24 stsp free(id_str);
8139 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8140 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8141 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8143 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8145 6458efa5 2020-11-24 stsp if (err) {
8146 6458efa5 2020-11-24 stsp free(line);
8147 6458efa5 2020-11-24 stsp return err;
8149 6458efa5 2020-11-24 stsp if (n == s->selected) {
8150 6458efa5 2020-11-24 stsp if (view->focussed)
8151 6458efa5 2020-11-24 stsp wstandout(view->window);
8152 6458efa5 2020-11-24 stsp s->selected_entry = re;
8154 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8156 6458efa5 2020-11-24 stsp wattr_on(view->window,
8157 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8158 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8160 6458efa5 2020-11-24 stsp wattr_off(view->window,
8161 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8162 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8163 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8164 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8165 6458efa5 2020-11-24 stsp wstandend(view->window);
8166 6458efa5 2020-11-24 stsp free(line);
8167 6458efa5 2020-11-24 stsp free(wline);
8168 6458efa5 2020-11-24 stsp wline = NULL;
8170 6458efa5 2020-11-24 stsp s->ndisplayed++;
8171 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8174 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8177 9b058f45 2022-06-30 mark view_border(view);
8178 6458efa5 2020-11-24 stsp return err;
8181 6458efa5 2020-11-24 stsp static const struct got_error *
8182 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8183 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8185 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8186 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8187 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8189 c42c9805 2020-11-24 stsp *new_view = NULL;
8191 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8192 c42c9805 2020-11-24 stsp if (err) {
8193 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8194 c42c9805 2020-11-24 stsp return err;
8196 c42c9805 2020-11-24 stsp return NULL;
8200 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8201 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8202 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8203 c42c9805 2020-11-24 stsp goto done;
8206 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8207 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8209 c42c9805 2020-11-24 stsp goto done;
8211 c42c9805 2020-11-24 stsp *new_view = tree_view;
8213 c42c9805 2020-11-24 stsp free(commit_id);
8214 c42c9805 2020-11-24 stsp return err;
8217 94b80cfa 2022-08-01 mark static const struct got_error *
8218 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8220 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8221 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8222 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8224 94b80cfa 2022-08-01 mark g = view->gline;
8225 94b80cfa 2022-08-01 mark view->gline = 0;
8227 94b80cfa 2022-08-01 mark if (g == 0)
8229 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8230 94b80cfa 2022-08-01 mark g = s->nrefs;
8232 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8233 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8234 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8235 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8236 94b80cfa 2022-08-01 mark return NULL;
8239 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8240 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8242 94b80cfa 2022-08-01 mark return err;
8243 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8244 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8245 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8246 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8247 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8248 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8250 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8251 94b80cfa 2022-08-01 mark s->selected = g - 1;
8253 94b80cfa 2022-08-01 mark return NULL;
8257 c42c9805 2020-11-24 stsp static const struct got_error *
8258 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8260 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8261 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8262 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8263 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8265 94b80cfa 2022-08-01 mark if (view->gline)
8266 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8268 6458efa5 2020-11-24 stsp switch (ch) {
8270 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8271 640cd7ff 2022-06-22 mark view->count = 0;
8274 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8275 640cd7ff 2022-06-22 mark view->count = 0;
8278 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8279 3f6c6614 2023-01-23 mark view->action = s->sort_by_date ? "sort by date" : "sort by name";
8280 640cd7ff 2022-06-22 mark view->count = 0;
8281 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8282 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8283 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8286 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8287 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8288 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8291 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8292 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8294 6458efa5 2020-11-24 stsp case KEY_ENTER:
8295 6458efa5 2020-11-24 stsp case '\r':
8296 640cd7ff 2022-06-22 mark view->count = 0;
8297 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8299 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8302 640cd7ff 2022-06-22 mark view->count = 0;
8303 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8305 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8307 e4526bf5 2021-09-03 naddy case 'g':
8309 e4526bf5 2021-09-03 naddy case KEY_HOME:
8310 e4526bf5 2021-09-03 naddy s->selected = 0;
8311 640cd7ff 2022-06-22 mark view->count = 0;
8312 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8314 e4526bf5 2021-09-03 naddy case 'G':
8316 9b058f45 2022-06-30 mark case KEY_END: {
8317 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8319 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8320 9b058f45 2022-06-30 mark --eos; /* border */
8321 e4526bf5 2021-09-03 naddy s->selected = 0;
8322 640cd7ff 2022-06-22 mark view->count = 0;
8323 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8324 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8325 e4526bf5 2021-09-03 naddy if (re == NULL)
8327 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8328 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8330 e4526bf5 2021-09-03 naddy if (n > 0)
8331 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8335 6458efa5 2020-11-24 stsp case KEY_UP:
8336 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8337 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8338 6458efa5 2020-11-24 stsp s->selected--;
8341 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8342 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8343 640cd7ff 2022-06-22 mark view->count = 0;
8345 83cc4199 2022-06-13 stsp case CTRL('u'):
8347 83cc4199 2022-06-13 stsp nscroll /= 2;
8348 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8349 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8350 6458efa5 2020-11-24 stsp case CTRL('b'):
8352 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8353 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8354 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8355 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8356 640cd7ff 2022-06-22 mark view->count = 0;
8359 6458efa5 2020-11-24 stsp case KEY_DOWN:
8360 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8361 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8362 6458efa5 2020-11-24 stsp s->selected++;
8365 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8366 6458efa5 2020-11-24 stsp /* can't scroll any further */
8367 640cd7ff 2022-06-22 mark view->count = 0;
8370 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8372 83cc4199 2022-06-13 stsp case CTRL('d'):
8374 83cc4199 2022-06-13 stsp nscroll /= 2;
8375 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8376 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8377 6458efa5 2020-11-24 stsp case CTRL('f'):
8379 48bb96f0 2022-06-20 naddy case ' ':
8380 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8381 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8382 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8383 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8384 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8385 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8386 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8387 640cd7ff 2022-06-22 mark view->count = 0;
8390 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8392 6458efa5 2020-11-24 stsp case CTRL('l'):
8393 640cd7ff 2022-06-22 mark view->count = 0;
8394 8924d611 2020-12-26 stsp tog_free_refs();
8395 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8398 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8399 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8401 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8402 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8403 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8406 640cd7ff 2022-06-22 mark view->count = 0;
8410 6458efa5 2020-11-24 stsp return err;
8413 6458efa5 2020-11-24 stsp __dead static void
8414 6458efa5 2020-11-24 stsp usage_ref(void)
8417 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8418 6458efa5 2020-11-24 stsp getprogname());
8422 6458efa5 2020-11-24 stsp static const struct got_error *
8423 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8425 6458efa5 2020-11-24 stsp const struct got_error *error;
8426 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8427 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8428 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8430 6458efa5 2020-11-24 stsp struct tog_view *view;
8431 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8433 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8434 6458efa5 2020-11-24 stsp switch (ch) {
8436 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8437 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8438 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8442 e99e2d15 2020-11-24 naddy usage_ref();
8443 6458efa5 2020-11-24 stsp /* NOTREACHED */
8447 6458efa5 2020-11-24 stsp argc -= optind;
8448 6458efa5 2020-11-24 stsp argv += optind;
8450 6458efa5 2020-11-24 stsp if (argc > 1)
8451 e99e2d15 2020-11-24 naddy usage_ref();
8453 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8454 0ae84acc 2022-06-15 tracey if (error != NULL)
8455 0ae84acc 2022-06-15 tracey goto done;
8457 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8458 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8459 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8460 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8461 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8462 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8463 c156c7a4 2020-12-18 stsp goto done;
8464 6458efa5 2020-11-24 stsp if (worktree)
8465 6458efa5 2020-11-24 stsp repo_path =
8466 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8468 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8469 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8470 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8471 c156c7a4 2020-12-18 stsp goto done;
8475 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8476 6458efa5 2020-11-24 stsp if (error != NULL)
8477 6458efa5 2020-11-24 stsp goto done;
8479 6458efa5 2020-11-24 stsp init_curses();
8481 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8482 51a10b52 2020-12-26 stsp if (error)
8483 51a10b52 2020-12-26 stsp goto done;
8485 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8486 6458efa5 2020-11-24 stsp if (error)
8487 6458efa5 2020-11-24 stsp goto done;
8489 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8490 6458efa5 2020-11-24 stsp if (view == NULL) {
8491 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8492 6458efa5 2020-11-24 stsp goto done;
8495 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8496 6458efa5 2020-11-24 stsp if (error)
8497 6458efa5 2020-11-24 stsp goto done;
8499 6458efa5 2020-11-24 stsp if (worktree) {
8500 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8501 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8502 6458efa5 2020-11-24 stsp worktree = NULL;
8504 6458efa5 2020-11-24 stsp error = view_loop(view);
8506 6458efa5 2020-11-24 stsp free(repo_path);
8507 6458efa5 2020-11-24 stsp free(cwd);
8508 1d0f4054 2021-06-17 stsp if (repo) {
8509 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8510 1d0f4054 2021-06-17 stsp if (close_err)
8511 1d0f4054 2021-06-17 stsp error = close_err;
8513 0ae84acc 2022-06-15 tracey if (pack_fds) {
8514 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8515 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8516 0ae84acc 2022-06-15 tracey if (error == NULL)
8517 0ae84acc 2022-06-15 tracey error = pack_err;
8519 51a10b52 2020-12-26 stsp tog_free_refs();
8520 6458efa5 2020-11-24 stsp return error;
8523 ec2a9698 2022-09-15 mark static const struct got_error*
8524 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8525 ec2a9698 2022-09-15 mark const char *str)
8527 ec2a9698 2022-09-15 mark size_t len;
8529 ec2a9698 2022-09-15 mark if (win == NULL)
8530 ec2a9698 2022-09-15 mark win = stdscr;
8532 ec2a9698 2022-09-15 mark len = strlen(str);
8533 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8535 ec2a9698 2022-09-15 mark if (focus)
8536 ec2a9698 2022-09-15 mark wstandout(win);
8537 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8538 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8539 ec2a9698 2022-09-15 mark if (focus)
8540 ec2a9698 2022-09-15 mark wstandend(win);
8542 ec2a9698 2022-09-15 mark return NULL;
8545 136e2bd4 2022-07-23 mark static const struct got_error *
8546 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8550 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8551 ec2a9698 2022-09-15 mark if (p == NULL) {
8552 ec2a9698 2022-09-15 mark free(*line_offsets);
8553 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8554 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8557 ec2a9698 2022-09-15 mark *line_offsets = p;
8558 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8559 ec2a9698 2022-09-15 mark ++(*nlines);
8560 ec2a9698 2022-09-15 mark return NULL;
8563 ec2a9698 2022-09-15 mark static const struct got_error *
8564 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8568 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8569 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8570 ec2a9698 2022-09-15 mark size_t len = 1;
8572 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8575 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8576 ec2a9698 2022-09-15 mark if (t0 == NULL)
8577 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8579 ec2a9698 2022-09-15 mark len += strlen(t);
8580 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8581 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8583 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8586 ec2a9698 2022-09-15 mark return NULL;
8590 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8591 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8592 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8594 ec2a9698 2022-09-15 mark static const struct got_error *
8595 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8597 ec2a9698 2022-09-15 mark int n, len = width;
8599 ec2a9698 2022-09-15 mark if (km->keys) {
8600 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8601 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8602 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8604 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8605 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8607 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8609 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8610 ec2a9698 2022-09-15 mark if (t0 == NULL)
8611 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8613 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8614 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8615 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8616 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8617 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8618 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8619 ec2a9698 2022-09-15 mark if (n < 0) {
8621 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8623 ec2a9698 2022-09-15 mark first = 0;
8624 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8625 ec2a9698 2022-09-15 mark *off += n;
8629 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8630 ec2a9698 2022-09-15 mark if (n < 0)
8631 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8632 ec2a9698 2022-09-15 mark *off += n;
8634 ec2a9698 2022-09-15 mark return NULL;
8637 ec2a9698 2022-09-15 mark static const struct got_error *
8638 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8640 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8641 ec2a9698 2022-09-15 mark off_t off = 0;
8642 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8643 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8644 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8645 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8646 ec2a9698 2022-09-15 mark GENERATE_HELP
8647 ec2a9698 2022-09-15 mark #undef KEYMAP_
8648 ec2a9698 2022-09-15 mark #undef KEY_
8651 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8653 ec2a9698 2022-09-15 mark return err;
8655 ec2a9698 2022-09-15 mark n = nitems(km);
8656 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8658 ec2a9698 2022-09-15 mark return err;
8660 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8661 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8662 ec2a9698 2022-09-15 mark show = s->all;
8663 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8664 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8667 ec2a9698 2022-09-15 mark if (show) {
8668 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8670 ec2a9698 2022-09-15 mark return err;
8671 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8673 ec2a9698 2022-09-15 mark return err;
8676 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8678 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8679 ec2a9698 2022-09-15 mark return err;
8682 ec2a9698 2022-09-15 mark static const struct got_error *
8683 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8686 ec2a9698 2022-09-15 mark const struct got_error *err;
8688 ec2a9698 2022-09-15 mark free(s->line_offsets);
8689 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8690 ec2a9698 2022-09-15 mark s->nlines = 0;
8692 ec2a9698 2022-09-15 mark f = got_opentemp();
8693 ec2a9698 2022-09-15 mark if (f == NULL)
8694 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8697 ec2a9698 2022-09-15 mark err = format_help(s);
8699 ec2a9698 2022-09-15 mark return err;
8701 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8702 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8704 ec2a9698 2022-09-15 mark return NULL;
8707 ec2a9698 2022-09-15 mark static const struct got_error *
8708 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8710 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8711 ec2a9698 2022-09-15 mark return NULL;
8714 eaf2c13e 2022-09-17 mark static void
8715 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8716 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8718 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8720 eaf2c13e 2022-09-17 mark *f = s->f;
8721 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8722 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8723 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8724 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8725 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8726 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8729 ec2a9698 2022-09-15 mark static const struct got_error *
8730 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8732 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8733 ec2a9698 2022-09-15 mark const struct got_error *err;
8734 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8735 ec2a9698 2022-09-15 mark wchar_t *wline;
8736 ec2a9698 2022-09-15 mark char *line;
8737 ec2a9698 2022-09-15 mark ssize_t linelen;
8738 ec2a9698 2022-09-15 mark size_t linesz = 0;
8739 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8740 ec2a9698 2022-09-15 mark int eos = view->nlines;
8742 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8743 ec2a9698 2022-09-15 mark --eos; /* account for border */
8745 ec2a9698 2022-09-15 mark s->lineno = 0;
8746 ec2a9698 2022-09-15 mark rewind(s->f);
8747 ec2a9698 2022-09-15 mark werase(view->window);
8749 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8750 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8752 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8753 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8754 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8756 ec2a9698 2022-09-15 mark return err;
8757 ec2a9698 2022-09-15 mark if (eos <= 1)
8758 ec2a9698 2022-09-15 mark return NULL;
8759 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8762 ec2a9698 2022-09-15 mark s->eof = 0;
8763 ec2a9698 2022-09-15 mark view->maxx = 0;
8764 ec2a9698 2022-09-15 mark line = NULL;
8765 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8766 ec2a9698 2022-09-15 mark attr_t attr = 0;
8768 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8769 ec2a9698 2022-09-15 mark if (linelen == -1) {
8770 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8771 ec2a9698 2022-09-15 mark free(line);
8772 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8774 ec2a9698 2022-09-15 mark s->eof = 1;
8777 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8779 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8781 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8782 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8784 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8785 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8786 ec2a9698 2022-09-15 mark if (err) {
8787 ec2a9698 2022-09-15 mark free(line);
8788 ec2a9698 2022-09-15 mark return err;
8790 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8791 ec2a9698 2022-09-15 mark free(wline);
8792 ec2a9698 2022-09-15 mark wline = NULL;
8795 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8796 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8797 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8798 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8799 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8800 ec2a9698 2022-09-15 mark if (err) {
8801 ec2a9698 2022-09-15 mark free(line);
8802 ec2a9698 2022-09-15 mark return err;
8807 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8808 ec2a9698 2022-09-15 mark view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8809 ec2a9698 2022-09-15 mark if (err) {
8810 ec2a9698 2022-09-15 mark free(line);
8811 ec2a9698 2022-09-15 mark return err;
8813 ec2a9698 2022-09-15 mark rc = waddwstr(view->window, &wline[skip]);
8814 ec2a9698 2022-09-15 mark free(wline);
8815 ec2a9698 2022-09-15 mark wline = NULL;
8816 ec2a9698 2022-09-15 mark if (rc == ERR)
8817 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "waddwstr");
8819 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8820 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8821 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8823 ec2a9698 2022-09-15 mark if (width <= view->ncols)
8824 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8827 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8828 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8829 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8831 ec2a9698 2022-09-15 mark free(line);
8832 ec2a9698 2022-09-15 mark if (nprinted > 0)
8833 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8835 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8837 ec2a9698 2022-09-15 mark view_border(view);
8839 ec2a9698 2022-09-15 mark if (s->eof) {
8840 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8841 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8842 ec2a9698 2022-09-15 mark view->ncols - 1);
8843 ec2a9698 2022-09-15 mark if (rc == ERR)
8844 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8846 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8847 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8848 ec2a9698 2022-09-15 mark wstandout(view->window);
8849 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8850 ec2a9698 2022-09-15 mark view->ncols - 1);
8851 ec2a9698 2022-09-15 mark if (rc == ERR)
8852 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8853 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8854 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8855 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8856 ec2a9698 2022-09-15 mark if (rc == ERR)
8857 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8859 ec2a9698 2022-09-15 mark wstandend(view->window);
8862 ec2a9698 2022-09-15 mark return NULL;
8865 ec2a9698 2022-09-15 mark static const struct got_error *
8866 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8868 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8869 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8870 ec2a9698 2022-09-15 mark char *line = NULL;
8871 ec2a9698 2022-09-15 mark ssize_t linelen;
8872 ec2a9698 2022-09-15 mark size_t linesz = 0;
8873 ec2a9698 2022-09-15 mark int eos, nscroll;
8875 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8876 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8877 ec2a9698 2022-09-15 mark --eos; /* border */
8879 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8881 ec2a9698 2022-09-15 mark switch (ch) {
8883 ec2a9698 2022-09-15 mark view->x = 0;
8886 ec2a9698 2022-09-15 mark view->x = MAX(view->maxx - view->ncols / 3, 0);
8887 ec2a9698 2022-09-15 mark view->count = 0;
8889 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8891 ec2a9698 2022-09-15 mark if (view->x + view->ncols / 3 < view->maxx)
8892 ec2a9698 2022-09-15 mark view->x += 2;
8894 ec2a9698 2022-09-15 mark view->count = 0;
8896 ec2a9698 2022-09-15 mark case KEY_LEFT:
8898 ec2a9698 2022-09-15 mark view->x -= MIN(view->x, 2);
8899 ec2a9698 2022-09-15 mark if (view->x <= 0)
8900 ec2a9698 2022-09-15 mark view->count = 0;
8903 ec2a9698 2022-09-15 mark case KEY_HOME:
8904 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8905 ec2a9698 2022-09-15 mark view->count = 0;
8908 ec2a9698 2022-09-15 mark case KEY_END:
8909 ec2a9698 2022-09-15 mark view->count = 0;
8910 ec2a9698 2022-09-15 mark if (s->eof)
8912 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8913 ec2a9698 2022-09-15 mark s->eof = 1;
8916 ec2a9698 2022-09-15 mark case KEY_UP:
8917 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8918 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8920 ec2a9698 2022-09-15 mark view->count = 0;
8922 ec2a9698 2022-09-15 mark case CTRL('u'):
8924 ec2a9698 2022-09-15 mark nscroll /= 2;
8925 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8926 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8927 ec2a9698 2022-09-15 mark case CTRL('b'):
8929 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8930 ec2a9698 2022-09-15 mark view->count = 0;
8933 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8934 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8937 ec2a9698 2022-09-15 mark case KEY_DOWN:
8938 ec2a9698 2022-09-15 mark case CTRL('n'):
8939 ec2a9698 2022-09-15 mark if (!s->eof)
8940 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8942 ec2a9698 2022-09-15 mark view->count = 0;
8944 ec2a9698 2022-09-15 mark case CTRL('d'):
8946 ec2a9698 2022-09-15 mark nscroll /= 2;
8947 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8948 ec2a9698 2022-09-15 mark case KEY_NPAGE:
8949 ec2a9698 2022-09-15 mark case CTRL('f'):
8952 ec2a9698 2022-09-15 mark if (s->eof) {
8953 ec2a9698 2022-09-15 mark view->count = 0;
8956 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
8957 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8958 ec2a9698 2022-09-15 mark s->first_displayed_line++;
8959 ec2a9698 2022-09-15 mark if (linelen == -1) {
8960 ec2a9698 2022-09-15 mark if (feof(s->f))
8961 ec2a9698 2022-09-15 mark s->eof = 1;
8963 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
8967 ec2a9698 2022-09-15 mark free(line);
8970 ec2a9698 2022-09-15 mark view->count = 0;
8974 ec2a9698 2022-09-15 mark return err;
8977 ec2a9698 2022-09-15 mark static const struct got_error *
8978 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
8980 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8982 ec2a9698 2022-09-15 mark free(s->line_offsets);
8983 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8984 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
8985 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8987 ec2a9698 2022-09-15 mark return NULL;
8990 ec2a9698 2022-09-15 mark static const struct got_error *
8991 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
8993 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8996 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
8997 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8999 ec2a9698 2022-09-15 mark wclear(view->window);
9000 ec2a9698 2022-09-15 mark view->count = 0;
9001 ec2a9698 2022-09-15 mark view->x = 0;
9002 ec2a9698 2022-09-15 mark s->all = !s->all;
9003 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9004 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9005 ec2a9698 2022-09-15 mark s->matched_line = 0;
9007 ec2a9698 2022-09-15 mark return create_help(s);
9010 ec2a9698 2022-09-15 mark static const struct got_error *
9011 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
9013 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9014 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9016 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
9017 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9018 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9019 ec2a9698 2022-09-15 mark s->selected_line = 1;
9021 ec2a9698 2022-09-15 mark view->show = show_help_view;
9022 ec2a9698 2022-09-15 mark view->input = input_help_view;
9023 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
9024 ec2a9698 2022-09-15 mark view->close = close_help_view;
9025 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
9026 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
9027 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
9029 ec2a9698 2022-09-15 mark err = create_help(s);
9030 ec2a9698 2022-09-15 mark return err;
9033 ec2a9698 2022-09-15 mark static const struct got_error *
9034 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9035 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
9037 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
9039 136e2bd4 2022-07-23 mark *new_view = NULL;
9041 136e2bd4 2022-07-23 mark switch (request) {
9042 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
9043 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9044 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9046 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9047 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9048 136e2bd4 2022-07-23 mark view, s->repo);
9050 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9051 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9053 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9054 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9055 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9057 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9058 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9061 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9062 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9064 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9065 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9066 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9067 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9068 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9069 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9070 136e2bd4 2022-07-23 mark &view->state.tree);
9071 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9072 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9073 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9074 136e2bd4 2022-07-23 mark view->state.ref.repo);
9076 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9077 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9079 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9080 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9081 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9082 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9083 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9084 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9085 136e2bd4 2022-07-23 mark view->state.log.repo);
9086 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9087 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9088 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9089 136e2bd4 2022-07-23 mark view->state.ref.repo);
9091 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9092 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9094 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9095 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9096 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9097 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9098 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9099 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9100 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9101 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9103 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9104 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9106 136e2bd4 2022-07-23 mark view_close(*new_view);
9108 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9109 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9110 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9111 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9112 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9114 ec2a9698 2022-09-15 mark view_close(*new_view);
9117 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9120 136e2bd4 2022-07-23 mark return err;
9124 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9125 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9127 6458efa5 2020-11-24 stsp static void
9128 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9130 9b058f45 2022-06-30 mark switch (view->type) {
9131 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9132 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9133 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9134 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9138 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9139 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9141 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9142 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9145 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9146 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9147 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9149 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9150 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9151 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9153 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9154 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9155 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9161 9b058f45 2022-06-30 mark view->offset = 0;
9165 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9166 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9168 9b058f45 2022-06-30 mark static const struct got_error *
9169 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9171 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9172 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9173 9b058f45 2022-06-30 mark int *selected = NULL;
9174 9b058f45 2022-06-30 mark int header, offset;
9176 9b058f45 2022-06-30 mark switch (view->type) {
9177 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9178 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9179 9b058f45 2022-06-30 mark header = 3;
9180 9b058f45 2022-06-30 mark scrolld = NULL;
9181 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9182 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9183 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9184 9b058f45 2022-06-30 mark s->selected_line -= offset;
9185 9b058f45 2022-06-30 mark view->offset = offset;
9189 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9190 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9191 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9192 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9193 9b058f45 2022-06-30 mark selected = &s->selected;
9196 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9197 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9198 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9199 9b058f45 2022-06-30 mark header = 3;
9200 9b058f45 2022-06-30 mark selected = &s->selected;
9203 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9204 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9205 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9206 9b058f45 2022-06-30 mark header = 5;
9207 9b058f45 2022-06-30 mark selected = &s->selected;
9211 9b058f45 2022-06-30 mark selected = NULL;
9212 9b058f45 2022-06-30 mark scrolld = NULL;
9213 9b058f45 2022-06-30 mark header = 0;
9217 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9218 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9219 9b058f45 2022-06-30 mark view->offset = offset;
9220 9b058f45 2022-06-30 mark if (scrolld && offset) {
9221 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9222 9b058f45 2022-06-30 mark *selected -= offset;
9226 9b058f45 2022-06-30 mark return err;
9229 9b058f45 2022-06-30 mark static void
9230 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9234 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9235 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9236 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9237 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9239 6879ba42 2020-10-01 naddy fputc('\n', fp);
9242 4ed7e80c 2018-05-20 stsp __dead static void
9243 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9245 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9247 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9248 6879ba42 2020-10-01 naddy getprogname());
9249 6879ba42 2020-10-01 naddy if (hflag) {
9250 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9251 6879ba42 2020-10-01 naddy list_commands(fp);
9253 6879ba42 2020-10-01 naddy exit(status);
9256 c2301be8 2018-04-30 stsp static char **
9257 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9259 ee85c5e8 2020-02-29 stsp va_list ap;
9260 c2301be8 2018-04-30 stsp char **argv;
9263 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9265 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9266 c2301be8 2018-04-30 stsp if (argv == NULL)
9267 c2301be8 2018-04-30 stsp err(1, "calloc");
9268 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9269 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9270 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9271 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9274 ee85c5e8 2020-02-29 stsp va_end(ap);
9275 c2301be8 2018-04-30 stsp return argv;
9279 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9280 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9281 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9282 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9283 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9285 ee85c5e8 2020-02-29 stsp static const struct got_error *
9286 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9288 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9289 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9290 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9291 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9292 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9293 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9294 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9295 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9296 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9298 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9299 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9300 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9302 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9303 0ae84acc 2022-06-15 tracey if (error != NULL)
9304 0ae84acc 2022-06-15 tracey goto done;
9306 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9307 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9308 ee85c5e8 2020-02-29 stsp goto done;
9310 ee85c5e8 2020-02-29 stsp if (worktree)
9311 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9313 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9314 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9315 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9316 ee85c5e8 2020-02-29 stsp goto done;
9319 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9320 ee85c5e8 2020-02-29 stsp if (error != NULL)
9321 ee85c5e8 2020-02-29 stsp goto done;
9323 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9324 ee85c5e8 2020-02-29 stsp repo, worktree);
9325 ee85c5e8 2020-02-29 stsp if (error)
9326 ee85c5e8 2020-02-29 stsp goto done;
9328 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9329 84de9106 2020-12-26 stsp if (error)
9330 84de9106 2020-12-26 stsp goto done;
9331 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9332 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9333 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9334 ee85c5e8 2020-02-29 stsp if (error)
9335 ee85c5e8 2020-02-29 stsp goto done;
9337 ee85c5e8 2020-02-29 stsp if (worktree) {
9338 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9339 ee85c5e8 2020-02-29 stsp worktree = NULL;
9342 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9343 a44927cc 2022-04-07 stsp if (error)
9344 a44927cc 2022-04-07 stsp goto done;
9346 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9347 ee85c5e8 2020-02-29 stsp if (error) {
9348 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9349 ee85c5e8 2020-02-29 stsp goto done;
9350 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9351 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9352 6879ba42 2020-10-01 naddy usage(1, 1);
9353 ee85c5e8 2020-02-29 stsp /* not reached */
9356 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9357 ee85c5e8 2020-02-29 stsp if (error)
9358 ee85c5e8 2020-02-29 stsp goto done;
9360 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9362 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9363 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9365 1d0f4054 2021-06-17 stsp if (repo) {
9366 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9367 1d0f4054 2021-06-17 stsp if (error == NULL)
9368 1d0f4054 2021-06-17 stsp error = close_err;
9370 a44927cc 2022-04-07 stsp if (commit)
9371 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9372 ee85c5e8 2020-02-29 stsp if (worktree)
9373 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9374 0ae84acc 2022-06-15 tracey if (pack_fds) {
9375 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9376 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9377 0ae84acc 2022-06-15 tracey if (error == NULL)
9378 0ae84acc 2022-06-15 tracey error = pack_err;
9381 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9382 ee85c5e8 2020-02-29 stsp free(commit_id);
9383 ee85c5e8 2020-02-29 stsp free(cwd);
9384 ee85c5e8 2020-02-29 stsp free(repo_path);
9385 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9386 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9388 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9389 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9390 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9392 87670572 2020-12-26 naddy tog_free_refs();
9393 ee85c5e8 2020-02-29 stsp return error;
9397 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9399 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9400 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9401 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9402 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9403 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9404 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9405 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9407 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9409 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9410 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9412 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9414 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9415 9f7d7167 2018-04-29 stsp switch (ch) {
9417 9f7d7167 2018-04-29 stsp hflag = 1;
9420 53ccebc2 2019-07-30 stsp Vflag = 1;
9423 6879ba42 2020-10-01 naddy usage(hflag, 1);
9424 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9428 9f7d7167 2018-04-29 stsp argc -= optind;
9429 9f7d7167 2018-04-29 stsp argv += optind;
9430 9814e6a3 2020-09-27 naddy optind = 1;
9431 c2301be8 2018-04-30 stsp optreset = 1;
9433 53ccebc2 2019-07-30 stsp if (Vflag) {
9434 53ccebc2 2019-07-30 stsp got_version_print_str();
9435 6879ba42 2020-10-01 naddy return 0;
9438 4010e238 2020-12-04 stsp #ifndef PROFILE
9439 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9440 4010e238 2020-12-04 stsp NULL) == -1)
9441 4010e238 2020-12-04 stsp err(1, "pledge");
9444 c2301be8 2018-04-30 stsp if (argc == 0) {
9445 f29d3e89 2018-06-23 stsp if (hflag)
9446 6879ba42 2020-10-01 naddy usage(hflag, 0);
9447 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9448 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9450 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9454 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9455 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9456 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9457 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9458 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9464 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9465 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9466 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9467 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9468 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9469 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9472 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9473 ee85c5e8 2020-02-29 stsp if (argc != 1)
9474 6879ba42 2020-10-01 naddy usage(0, 1);
9475 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9476 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9478 ee85c5e8 2020-02-29 stsp if (hflag)
9479 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9481 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9485 b46c1e04 2020-09-20 naddy putchar('\n');
9486 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9488 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9489 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9490 a2f4a359 2020-02-28 stsp free(cmd_argv);
9493 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9494 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9495 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9496 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9497 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9498 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);