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);
1782 dc2c3344 2023-01-23 mark if (view->dying && view == TAILQ_FIRST(&views) &&
1783 dc2c3344 2023-01-23 mark TAILQ_NEXT(view, entry) == NULL)
1785 dc2c3344 2023-01-23 mark if (done) {
1786 dc2c3344 2023-01-23 mark struct tog_view *v;
1789 dc2c3344 2023-01-23 mark * When we quit, scroll the screen up a single line
1790 dc2c3344 2023-01-23 mark * so we don't lose any information.
1792 dc2c3344 2023-01-23 mark TAILQ_FOREACH(v, &views, entry) {
1793 dc2c3344 2023-01-23 mark wmove(v->window, 0, 0);
1794 dc2c3344 2023-01-23 mark wdeleteln(v->window);
1795 dc2c3344 2023-01-23 mark wnoutrefresh(v->window);
1796 dc2c3344 2023-01-23 mark if (v->child && !view_is_fullscreen(v)) {
1797 dc2c3344 2023-01-23 mark wmove(v->child->window, 0, 0);
1798 dc2c3344 2023-01-23 mark wdeleteln(v->child->window);
1799 dc2c3344 2023-01-23 mark wnoutrefresh(v->child->window);
1802 dc2c3344 2023-01-23 mark doupdate();
1805 9970f7fc 2020-12-03 stsp if (view->dying) {
1806 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1808 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1809 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1811 e78dc838 2020-12-04 stsp else if (view->parent)
1812 669b5ffa 2018-10-07 stsp prev = view->parent;
1814 e78dc838 2020-12-04 stsp if (view->parent) {
1815 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1816 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1817 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1818 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1819 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1822 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1823 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1825 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1827 9970f7fc 2020-12-03 stsp err = view_close(view);
1829 e5a0f69f 2018-08-18 stsp goto done;
1831 e78dc838 2020-12-04 stsp view = NULL;
1832 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1833 e78dc838 2020-12-04 stsp if (v->focussed)
1836 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1837 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1839 e78dc838 2020-12-04 stsp view = prev;
1840 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1841 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1842 e78dc838 2020-12-04 stsp tog_view_list_head);
1844 e78dc838 2020-12-04 stsp if (view) {
1845 e78dc838 2020-12-04 stsp if (view->focus_child) {
1846 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1847 e78dc838 2020-12-04 stsp view = view->child;
1849 e78dc838 2020-12-04 stsp view->focussed = 1;
1853 bcbd79e2 2018-08-19 stsp if (new_view) {
1854 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1855 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1856 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1857 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1859 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1860 86c66b02 2018-10-18 stsp err = view_close(v);
1862 86c66b02 2018-10-18 stsp goto done;
1865 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1866 fed7eaa8 2018-10-24 stsp view = new_view;
1868 dc2c3344 2023-01-23 mark if (view && !done) {
1869 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1870 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1871 e78dc838 2020-12-04 stsp view = view->child;
1873 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1874 e78dc838 2020-12-04 stsp view = view->parent;
1876 e78dc838 2020-12-04 stsp show_panel(view->panel);
1877 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1878 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1879 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1880 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1882 1a76625f 2018-10-22 stsp goto done;
1884 669b5ffa 2018-10-07 stsp err = view->show(view);
1886 1a76625f 2018-10-22 stsp goto done;
1887 669b5ffa 2018-10-07 stsp if (view->child) {
1888 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1890 1a76625f 2018-10-22 stsp goto done;
1892 1a76625f 2018-10-22 stsp update_panels();
1893 1a76625f 2018-10-22 stsp doupdate();
1897 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1898 5629093a 2022-07-11 stsp const struct got_error *close_err;
1899 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1900 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1901 5629093a 2022-07-11 stsp close_err = view_close(view);
1902 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1903 5629093a 2022-07-11 stsp err = close_err;
1906 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1907 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1908 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1910 e5a0f69f 2018-08-18 stsp return err;
1913 4ed7e80c 2018-05-20 stsp __dead static void
1914 9f7d7167 2018-04-29 stsp usage_log(void)
1917 c70c5802 2018-08-01 stsp fprintf(stderr,
1918 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1919 9f7d7167 2018-04-29 stsp getprogname());
1923 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1924 80ddbec8 2018-04-29 stsp static const struct got_error *
1925 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1927 00dfcb92 2018-06-11 stsp char *vis = NULL;
1928 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1930 963b370f 2018-05-20 stsp *ws = NULL;
1931 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1932 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1933 00dfcb92 2018-06-11 stsp int vislen;
1934 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1935 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1937 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1938 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1940 00dfcb92 2018-06-11 stsp return err;
1941 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1942 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1943 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1944 a7f50699 2018-06-11 stsp goto done;
1948 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1949 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1950 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1951 a7f50699 2018-06-11 stsp goto done;
1954 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1955 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1957 00dfcb92 2018-06-11 stsp free(vis);
1958 963b370f 2018-05-20 stsp if (err) {
1959 963b370f 2018-05-20 stsp free(*ws);
1960 963b370f 2018-05-20 stsp *ws = NULL;
1961 963b370f 2018-05-20 stsp *wlen = 0;
1963 963b370f 2018-05-20 stsp return err;
1966 145b6838 2022-06-16 stsp static const struct got_error *
1967 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1969 145b6838 2022-06-16 stsp char *dst;
1970 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1972 145b6838 2022-06-16 stsp *ptr = NULL;
1973 145b6838 2022-06-16 stsp n = len = strlen(src);
1974 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1975 145b6838 2022-06-16 stsp if (dst == NULL)
1976 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1978 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1979 145b6838 2022-06-16 stsp const char c = src[idx];
1981 145b6838 2022-06-16 stsp if (c == '\t') {
1982 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1985 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1986 6e1c41ad 2022-06-16 mark if (p == NULL) {
1987 6e1c41ad 2022-06-16 mark free(dst);
1988 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1993 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1996 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
2000 145b6838 2022-06-16 stsp dst[sz] = '\0';
2001 145b6838 2022-06-16 stsp *ptr = dst;
2002 145b6838 2022-06-16 stsp return NULL;
2006 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
2007 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
2008 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
2012 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2014 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
2016 4e4a9ac8 2022-06-17 op if (n == 0) {
2017 4e4a9ac8 2022-06-17 op *rcol = cols;
2021 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
2022 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
2023 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2025 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
2027 4e4a9ac8 2022-06-17 op if (width == -1) {
2029 4e4a9ac8 2022-06-17 op wline[i] = L'.';
2032 4e4a9ac8 2022-06-17 op if (cols + width > n)
2034 4e4a9ac8 2022-06-17 op cols += width;
2037 4e4a9ac8 2022-06-17 op *rcol = cols;
2042 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
2043 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
2044 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2046 ccda2f4d 2022-06-16 stsp static const struct got_error *
2047 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2048 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2050 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2052 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2053 145b6838 2022-06-16 stsp char *exstr = NULL;
2054 963b370f 2018-05-20 stsp size_t wlen;
2055 4e4a9ac8 2022-06-17 op int i, scrollx;
2057 963b370f 2018-05-20 stsp *wlinep = NULL;
2058 b700b5d6 2018-07-10 stsp *widthp = 0;
2060 145b6838 2022-06-16 stsp if (expand) {
2061 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2063 145b6838 2022-06-16 stsp return err;
2066 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2067 145b6838 2022-06-16 stsp free(exstr);
2069 963b370f 2018-05-20 stsp return err;
2071 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2073 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2074 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2077 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2078 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2082 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2083 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2085 b700b5d6 2018-07-10 stsp if (widthp)
2086 b700b5d6 2018-07-10 stsp *widthp = cols;
2087 ccda2f4d 2022-06-16 stsp if (scrollxp)
2088 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2090 963b370f 2018-05-20 stsp free(wline);
2092 963b370f 2018-05-20 stsp *wlinep = wline;
2093 963b370f 2018-05-20 stsp return err;
2096 8b473291 2019-02-21 stsp static const struct got_error*
2097 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2098 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2100 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2101 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2103 8b473291 2019-02-21 stsp const char *name;
2105 8b473291 2019-02-21 stsp *refs_str = NULL;
2107 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2108 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2109 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2112 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2113 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2115 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2116 8b473291 2019-02-21 stsp name += 5;
2117 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2118 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2120 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2121 8b473291 2019-02-21 stsp name += 6;
2122 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2123 8b473291 2019-02-21 stsp name += 8;
2124 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2125 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2128 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2131 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2132 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2133 5d844a1e 2019-08-13 stsp if (err) {
2134 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2135 48cae60d 2020-09-22 stsp free(ref_id);
2138 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2139 5d844a1e 2019-08-13 stsp err = NULL;
2140 5d844a1e 2019-08-13 stsp tag = NULL;
2143 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2144 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2145 48cae60d 2020-09-22 stsp free(ref_id);
2147 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2148 52b5abe1 2019-08-13 stsp if (cmp != 0)
2150 8b473291 2019-02-21 stsp s = *refs_str;
2151 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2152 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2153 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2155 8b473291 2019-02-21 stsp *refs_str = NULL;
2161 8b473291 2019-02-21 stsp return err;
2164 963b370f 2018-05-20 stsp static const struct got_error *
2165 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2166 27a741e5 2019-09-11 stsp int col_tab_align)
2168 e6b8b890 2020-12-29 naddy char *smallerthan;
2170 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2171 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2172 5813d178 2019-03-09 stsp author = smallerthan + 1;
2173 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2174 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2175 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2178 5813d178 2019-03-09 stsp static const struct got_error *
2179 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2180 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2181 8fdc79fe 2020-12-01 naddy int author_display_cols)
2183 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2184 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2185 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2186 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2187 5813d178 2019-03-09 stsp char *author = NULL;
2188 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2189 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2190 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2191 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2192 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2193 ccb26ccd 2018-11-05 stsp struct tm tm;
2194 45d799e2 2018-12-23 stsp time_t committer_time;
2195 11b20872 2019-11-08 stsp struct tog_color *tc;
2197 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2198 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2199 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2200 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2201 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2203 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2204 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2206 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2207 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2209 11b20872 2019-11-08 stsp wattr_on(view->window,
2210 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2211 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2213 11b20872 2019-11-08 stsp wattr_off(view->window,
2214 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2215 27a741e5 2019-09-11 stsp col = limit;
2216 b39d25c7 2018-07-10 stsp if (col > avail)
2217 b39d25c7 2018-07-10 stsp goto done;
2219 6570a66d 2019-11-08 stsp if (avail >= 120) {
2220 6570a66d 2019-11-08 stsp char *id_str;
2221 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2223 6570a66d 2019-11-08 stsp goto done;
2224 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2226 11b20872 2019-11-08 stsp wattr_on(view->window,
2227 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2228 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2230 11b20872 2019-11-08 stsp wattr_off(view->window,
2231 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2232 6570a66d 2019-11-08 stsp free(id_str);
2234 6570a66d 2019-11-08 stsp if (col > avail)
2235 6570a66d 2019-11-08 stsp goto done;
2238 10aab77f 2022-07-19 op if (s->use_committer)
2239 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2241 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2242 5813d178 2019-03-09 stsp if (author == NULL) {
2243 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2244 80ddbec8 2018-04-29 stsp goto done;
2246 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2248 bb737323 2018-05-20 stsp goto done;
2249 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2251 11b20872 2019-11-08 stsp wattr_on(view->window,
2252 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2253 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2254 bb737323 2018-05-20 stsp col += author_width;
2255 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2256 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2258 bb737323 2018-05-20 stsp author_width++;
2261 f0f62654 2022-09-08 mark wattr_off(view->window,
2262 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2263 9c2eaf34 2018-05-20 stsp if (col > avail)
2264 9c2eaf34 2018-05-20 stsp goto done;
2266 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2268 6d9fbc00 2018-04-29 stsp goto done;
2269 bb737323 2018-05-20 stsp logmsg = logmsg0;
2270 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2272 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2273 bb737323 2018-05-20 stsp if (newline)
2274 bb737323 2018-05-20 stsp *newline = '\0';
2275 ccda2f4d 2022-06-16 stsp limit = avail - col;
2276 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2277 4d1f6af3 2022-06-17 op limit--; /* for the border */
2278 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2279 ccda2f4d 2022-06-16 stsp limit, col, 1);
2281 29688b02 2022-06-16 stsp goto done;
2282 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2283 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2284 27a741e5 2019-09-11 stsp while (col < avail) {
2285 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2289 80ddbec8 2018-04-29 stsp free(logmsg0);
2290 bb737323 2018-05-20 stsp free(wlogmsg);
2291 5813d178 2019-03-09 stsp free(author);
2292 bb737323 2018-05-20 stsp free(wauthor);
2293 80ddbec8 2018-04-29 stsp free(line);
2294 80ddbec8 2018-04-29 stsp return err;
2297 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2298 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2299 899d86c2 2018-05-10 stsp struct got_object_id *id)
2301 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2302 e15c42de 2022-09-05 op struct got_object_id *dup;
2304 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2305 80ddbec8 2018-04-29 stsp if (entry == NULL)
2306 899d86c2 2018-05-10 stsp return NULL;
2308 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2309 e15c42de 2022-09-05 op if (dup == NULL) {
2310 e15c42de 2022-09-05 op free(entry);
2311 e15c42de 2022-09-05 op return NULL;
2314 e15c42de 2022-09-05 op entry->id = dup;
2315 99db9666 2018-05-07 stsp entry->commit = commit;
2316 899d86c2 2018-05-10 stsp return entry;
2319 99db9666 2018-05-07 stsp static void
2320 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2322 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2324 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2325 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2326 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2327 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2328 e15c42de 2022-09-05 op free(entry->id);
2329 99db9666 2018-05-07 stsp free(entry);
2332 99db9666 2018-05-07 stsp static void
2333 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2335 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2336 99db9666 2018-05-07 stsp pop_commit(commits);
2339 c4972b91 2018-05-07 stsp static const struct got_error *
2340 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2341 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2343 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2344 13add988 2019-10-15 stsp regmatch_t regmatch;
2345 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2347 13add988 2019-10-15 stsp *have_match = 0;
2349 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2351 13add988 2019-10-15 stsp return err;
2353 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2355 13add988 2019-10-15 stsp goto done;
2357 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2358 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2359 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2360 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2361 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2362 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2363 13add988 2019-10-15 stsp *have_match = 1;
2365 13add988 2019-10-15 stsp free(id_str);
2366 13add988 2019-10-15 stsp free(logmsg);
2367 13add988 2019-10-15 stsp return err;
2370 13add988 2019-10-15 stsp static const struct got_error *
2371 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2373 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2376 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2377 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2378 1a76625f 2018-10-22 stsp * while updating the display.
2381 d9787ed8 2022-09-10 op struct got_object_id id;
2382 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2383 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2384 568eae95 2022-09-11 mark int limit_match = 0;
2385 1a76625f 2018-10-22 stsp int errcode;
2387 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2388 4e0d2870 2020-12-07 naddy NULL, NULL);
2392 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2395 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2396 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2397 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2401 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2402 1a76625f 2018-10-22 stsp if (errcode) {
2403 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2404 13add988 2019-10-15 stsp "pthread_mutex_lock");
2408 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2409 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2410 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2412 568eae95 2022-09-11 mark if (*a->limiting) {
2413 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2414 568eae95 2022-09-11 mark a->limit_regex);
2418 568eae95 2022-09-11 mark if (limit_match) {
2419 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2421 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2422 568eae95 2022-09-11 mark entry->commit, entry->id);
2423 568eae95 2022-09-11 mark if (matched == NULL) {
2424 568eae95 2022-09-11 mark err = got_error_from_errno(
2425 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2428 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2429 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2431 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2432 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2433 568eae95 2022-09-11 mark matched, entry);
2434 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2438 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2439 568eae95 2022-09-11 mark * have found a match, and that it should be
2440 568eae95 2022-09-11 mark * counted as a new entry for the view.
2442 568eae95 2022-09-11 mark a->limit_match = limit_match;
2445 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2446 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2447 7c1452c1 2020-03-26 stsp int have_match;
2448 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2452 568eae95 2022-09-11 mark if (*a->limiting) {
2453 568eae95 2022-09-11 mark if (limit_match && have_match)
2454 568eae95 2022-09-11 mark *a->search_next_done =
2455 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2456 568eae95 2022-09-11 mark } else if (have_match)
2457 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2460 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2461 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2462 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2463 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2466 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2468 9ba79e04 2018-06-11 stsp return err;
2471 2b779855 2020-12-05 naddy static void
2472 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2474 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2475 2b779855 2020-12-05 naddy int ncommits = 0;
2477 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2478 2b779855 2020-12-05 naddy while (entry) {
2479 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2480 2b779855 2020-12-05 naddy s->selected_entry = entry;
2483 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2484 2b779855 2020-12-05 naddy ncommits++;
2488 0553a4e3 2018-04-30 stsp static const struct got_error *
2489 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2491 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2492 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2493 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2494 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2495 60493ae3 2019-06-20 stsp int width;
2496 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2497 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2498 8b473291 2019-02-21 stsp char *refs_str = NULL;
2499 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2500 11b20872 2019-11-08 stsp struct tog_color *tc;
2501 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2503 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2504 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2506 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2507 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2508 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2509 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2511 ecb28ae0 2018-07-16 stsp return err;
2512 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2513 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2514 d2075bf3 2020-12-25 stsp if (refs) {
2515 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2516 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2518 d2075bf3 2020-12-25 stsp goto done;
2522 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2523 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2525 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2526 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2527 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2528 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2529 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2530 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2531 8f4ed634 2020-03-26 stsp goto done;
2534 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2535 568eae95 2022-09-11 mark const char *limit_str = NULL;
2537 f9686aa5 2020-03-27 stsp if (view->searching) {
2538 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2539 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2540 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2541 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2542 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2543 f9686aa5 2020-03-27 stsp search_str = "searching...";
2546 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2547 568eae95 2022-09-11 mark limit_str = "no matches found";
2549 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2550 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2551 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2552 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2553 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2554 8f4ed634 2020-03-26 stsp goto done;
2558 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2559 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2560 87411fa9 2022-06-16 stsp "........................................",
2561 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2562 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2563 1a76625f 2018-10-22 stsp header = NULL;
2564 1a76625f 2018-10-22 stsp goto done;
2566 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2567 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2568 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2569 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2570 1a76625f 2018-10-22 stsp header = NULL;
2571 1a76625f 2018-10-22 stsp goto done;
2573 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2575 1a76625f 2018-10-22 stsp goto done;
2577 2814baeb 2018-08-01 stsp werase(view->window);
2579 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2580 a3404814 2018-09-02 stsp wstandout(view->window);
2581 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2583 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2584 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2585 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2586 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2590 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2591 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2592 a3404814 2018-09-02 stsp wstandend(view->window);
2593 ecb28ae0 2018-07-16 stsp free(wline);
2594 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2595 1a76625f 2018-10-22 stsp goto done;
2597 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2598 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2599 5813d178 2019-03-09 stsp ncommits = 0;
2600 145b6838 2022-06-16 stsp view->maxx = 0;
2601 5813d178 2019-03-09 stsp while (entry) {
2602 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2603 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2604 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2605 5813d178 2019-03-09 stsp int width;
2606 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2608 10aab77f 2022-07-19 op if (s->use_committer)
2609 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2611 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2612 5813d178 2019-03-09 stsp if (author == NULL) {
2613 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2614 5813d178 2019-03-09 stsp goto done;
2616 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2617 27a741e5 2019-09-11 stsp date_display_cols);
2618 5813d178 2019-03-09 stsp if (author_cols < width)
2619 5813d178 2019-03-09 stsp author_cols = width;
2620 5813d178 2019-03-09 stsp free(wauthor);
2621 5813d178 2019-03-09 stsp free(author);
2622 a310d9c3 2022-07-21 florian if (err)
2623 a310d9c3 2022-07-21 florian goto done;
2624 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2626 145b6838 2022-06-16 stsp goto done;
2627 145b6838 2022-06-16 stsp msg = msg0;
2628 145b6838 2022-06-16 stsp while (*msg == '\n')
2630 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2631 29688b02 2022-06-16 stsp *eol = '\0';
2632 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2633 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2635 29688b02 2022-06-16 stsp goto done;
2636 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2637 145b6838 2022-06-16 stsp free(msg0);
2638 29688b02 2022-06-16 stsp free(wmsg);
2639 7ca04879 2019-10-19 stsp ncommits++;
2640 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2643 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2644 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2645 867c6645 2018-07-10 stsp ncommits = 0;
2646 899d86c2 2018-05-10 stsp while (entry) {
2647 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2649 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2650 2814baeb 2018-08-01 stsp wstandout(view->window);
2651 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2652 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2653 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2654 2814baeb 2018-08-01 stsp wstandend(view->window);
2656 60493ae3 2019-06-20 stsp goto done;
2657 0553a4e3 2018-04-30 stsp ncommits++;
2658 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2659 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2662 9b058f45 2022-06-30 mark view_border(view);
2664 1a76625f 2018-10-22 stsp free(id_str);
2665 8b473291 2019-02-21 stsp free(refs_str);
2666 1a76625f 2018-10-22 stsp free(ncommits_str);
2667 1a76625f 2018-10-22 stsp free(header);
2668 80ddbec8 2018-04-29 stsp return err;
2671 07b55e75 2018-05-10 stsp static void
2672 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2674 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2675 07b55e75 2018-05-10 stsp int nscrolled = 0;
2677 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2678 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2681 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2682 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2683 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2684 07b55e75 2018-05-10 stsp if (entry) {
2685 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2686 07b55e75 2018-05-10 stsp nscrolled++;
2691 aa075928 2018-05-10 stsp static const struct got_error *
2692 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2694 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2695 5e224a3e 2019-02-22 stsp int errcode;
2697 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2699 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2700 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2701 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2702 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2703 7aafa0d1 2019-02-22 stsp if (errcode)
2704 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2705 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2708 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2709 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2711 7c1452c1 2020-03-26 stsp if (!wait)
2714 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2715 ffe38506 2020-12-01 naddy show_log_view(view);
2716 7c1452c1 2020-03-26 stsp update_panels();
2717 7c1452c1 2020-03-26 stsp doupdate();
2719 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2720 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2721 82954512 2020-02-03 stsp if (errcode)
2722 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2723 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2725 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2726 ffe38506 2020-12-01 naddy show_log_view(view);
2727 7c1452c1 2020-03-26 stsp update_panels();
2728 7c1452c1 2020-03-26 stsp doupdate();
2731 5e224a3e 2019-02-22 stsp return NULL;
2734 5e224a3e 2019-02-22 stsp static const struct got_error *
2735 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2737 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2738 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2740 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2741 2525dccb 2022-07-13 mark return NULL;
2743 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2744 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2745 9b058f45 2022-06-30 mark view->nscrolled = 0;
2747 9b058f45 2022-06-30 mark return err;
2750 9b058f45 2022-06-30 mark static const struct got_error *
2751 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2753 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2754 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2755 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2756 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2758 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2759 5e224a3e 2019-02-22 stsp return NULL;
2761 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2762 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2763 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2765 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2767 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2768 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2769 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2771 5e224a3e 2019-02-22 stsp return err;
2775 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2776 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2779 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2780 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
2782 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2783 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2785 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2786 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2788 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2789 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2791 9b058f45 2022-06-30 mark view->nscrolled = 0;
2793 dd0a52c1 2018-05-20 stsp return err;
2796 cd0acaa7 2018-05-20 stsp static const struct got_error *
2797 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2798 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2799 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2801 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2802 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2803 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2805 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2806 15a94983 2018-12-23 stsp if (diff_view == NULL)
2807 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2809 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2810 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2811 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2812 e5a0f69f 2018-08-18 stsp if (err == NULL)
2813 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2814 cd0acaa7 2018-05-20 stsp return err;
2817 80ddbec8 2018-04-29 stsp static const struct got_error *
2818 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2819 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2821 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2823 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2824 941e9f74 2019-05-21 stsp if (parent == NULL)
2825 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2827 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2828 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2829 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2830 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2831 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2832 941e9f74 2019-05-21 stsp s->tree = subtree;
2833 941e9f74 2019-05-21 stsp s->selected = 0;
2834 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2835 941e9f74 2019-05-21 stsp return NULL;
2838 941e9f74 2019-05-21 stsp static const struct got_error *
2839 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2840 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2842 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2843 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2844 941e9f74 2019-05-21 stsp const char *p;
2845 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2847 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2849 941e9f74 2019-05-21 stsp while (*p) {
2850 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2851 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2852 56e0773d 2019-11-28 stsp char *te_name;
2854 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2857 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2858 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2859 941e9f74 2019-05-21 stsp if (slash == NULL)
2860 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2862 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2863 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2864 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2867 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2868 56e0773d 2019-11-28 stsp if (te == NULL) {
2869 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2870 56e0773d 2019-11-28 stsp free(te_name);
2873 56e0773d 2019-11-28 stsp free(te_name);
2874 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2876 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2877 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2879 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2880 941e9f74 2019-05-21 stsp if (slash)
2881 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2883 941e9f74 2019-05-21 stsp subpath = strdup(path);
2884 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2885 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2889 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2894 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2895 941e9f74 2019-05-21 stsp free(tree_id);
2899 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2900 941e9f74 2019-05-21 stsp if (err) {
2901 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2904 941e9f74 2019-05-21 stsp if (slash == NULL)
2906 941e9f74 2019-05-21 stsp free(subpath);
2907 941e9f74 2019-05-21 stsp subpath = NULL;
2908 941e9f74 2019-05-21 stsp p = slash;
2911 941e9f74 2019-05-21 stsp free(subpath);
2912 1a76625f 2018-10-22 stsp return err;
2915 61266923 2020-01-14 stsp static const struct got_error *
2916 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2917 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2918 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2920 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2921 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2922 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2924 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2925 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2926 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2928 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2930 55cccc34 2020-02-20 stsp return err;
2931 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2933 55cccc34 2020-02-20 stsp *new_view = tree_view;
2935 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2936 55cccc34 2020-02-20 stsp return NULL;
2938 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2941 55cccc34 2020-02-20 stsp static const struct got_error *
2942 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2944 61266923 2020-01-14 stsp sigset_t sigset;
2945 61266923 2020-01-14 stsp int errcode;
2947 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2948 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2950 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2951 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2952 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2953 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2954 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2955 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2956 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2957 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2958 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2960 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2961 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2962 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2964 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2965 61266923 2020-01-14 stsp if (errcode)
2966 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2968 61266923 2020-01-14 stsp return NULL;
2971 1a76625f 2018-10-22 stsp static void *
2972 1a76625f 2018-10-22 stsp log_thread(void *arg)
2974 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2975 1a76625f 2018-10-22 stsp int errcode = 0;
2976 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2977 1a76625f 2018-10-22 stsp int done = 0;
2980 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2981 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2983 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2984 5629093a 2022-07-11 stsp if (errcode) {
2985 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2986 61266923 2020-01-14 stsp return (void *)err;
2989 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2990 5629093a 2022-07-11 stsp if (err) {
2991 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2992 5629093a 2022-07-11 stsp goto done;
2995 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2996 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2997 5629093a 2022-07-11 stsp if (errcode) {
2998 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2999 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
3000 5629093a 2022-07-11 stsp goto done;
3002 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3003 1a76625f 2018-10-22 stsp if (err) {
3004 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3005 5629093a 2022-07-11 stsp goto done;
3006 1a76625f 2018-10-22 stsp err = NULL;
3008 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
3009 568eae95 2022-09-11 mark if (*a->limiting) {
3010 568eae95 2022-09-11 mark if (a->limit_match)
3011 568eae95 2022-09-11 mark a->commits_needed--;
3013 568eae95 2022-09-11 mark a->commits_needed--;
3016 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3017 3abe8080 2019-04-10 stsp if (errcode) {
3018 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3019 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3020 5629093a 2022-07-11 stsp goto done;
3021 3abe8080 2019-04-10 stsp } else if (*a->quit)
3023 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
3024 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3025 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
3026 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3027 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
3028 568eae95 2022-09-11 mark *a->first_displayed_entry =
3029 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
3030 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
3033 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3034 7c1452c1 2020-03-26 stsp if (errcode) {
3035 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3036 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3037 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3038 5629093a 2022-07-11 stsp goto done;
3042 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3044 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3045 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3046 7c1452c1 2020-03-26 stsp &tog_mutex);
3047 5629093a 2022-07-11 stsp if (errcode) {
3048 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3049 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3050 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3051 5629093a 2022-07-11 stsp goto done;
3053 21355643 2020-12-06 stsp if (*a->quit)
3058 3abe8080 2019-04-10 stsp a->log_complete = 1;
3059 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3060 5629093a 2022-07-11 stsp if (errcode)
3061 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3063 5629093a 2022-07-11 stsp if (err) {
3064 5629093a 2022-07-11 stsp tog_thread_error = 1;
3065 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3067 1a76625f 2018-10-22 stsp return (void *)err;
3070 1a76625f 2018-10-22 stsp static const struct got_error *
3071 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3073 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3074 1a76625f 2018-10-22 stsp int errcode;
3076 1a76625f 2018-10-22 stsp if (s->thread) {
3077 1a76625f 2018-10-22 stsp s->quit = 1;
3078 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3079 1a76625f 2018-10-22 stsp if (errcode)
3080 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3081 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3082 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3083 1a76625f 2018-10-22 stsp if (errcode)
3084 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3085 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3086 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3087 1a76625f 2018-10-22 stsp if (errcode)
3088 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3090 1a76625f 2018-10-22 stsp if (errcode)
3091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3092 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3093 1a76625f 2018-10-22 stsp s->thread = NULL;
3096 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3097 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3098 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3101 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3102 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3103 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3104 74467cc8 2022-06-15 stsp if (err == NULL)
3105 74467cc8 2022-06-15 stsp err = pack_err;
3106 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3109 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3110 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3111 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3114 5629093a 2022-07-11 stsp return err ? err : thread_err;
3117 9343a5fb 2018-06-23 stsp static const struct got_error *
3118 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3120 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3121 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3122 276b94a1 2020-11-13 naddy int errcode;
3124 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3126 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3127 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3128 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3130 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3131 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3132 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3134 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3135 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3136 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3137 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3138 1a76625f 2018-10-22 stsp free(s->start_id);
3139 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3140 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3141 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3142 1a76625f 2018-10-22 stsp return err;
3146 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3147 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3148 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3149 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3150 568eae95 2022-09-11 mark * works with very slight change.
3152 1a76625f 2018-10-22 stsp static const struct got_error *
3153 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3155 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3156 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3157 568eae95 2022-09-11 mark struct tog_view *v = view;
3158 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3159 568eae95 2022-09-11 mark char pattern[1024];
3162 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3163 568eae95 2022-09-11 mark v = view->child;
3164 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3165 568eae95 2022-09-11 mark v = view->parent;
3167 568eae95 2022-09-11 mark /* Get the pattern */
3168 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3169 568eae95 2022-09-11 mark wclrtoeol(v->window);
3170 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3171 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3172 568eae95 2022-09-11 mark nocbreak();
3174 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3177 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3178 568eae95 2022-09-11 mark if (ret == ERR)
3179 568eae95 2022-09-11 mark return NULL;
3181 568eae95 2022-09-11 mark if (*pattern == '\0') {
3183 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3184 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3186 568eae95 2022-09-11 mark if (!s->limit_view)
3187 568eae95 2022-09-11 mark return NULL;
3190 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3191 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3192 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3193 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3194 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3195 568eae95 2022-09-11 mark * the queue.
3197 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3198 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3199 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3200 568eae95 2022-09-11 mark s->selected = 0;
3201 568eae95 2022-09-11 mark s->limit_view = 0;
3203 568eae95 2022-09-11 mark return NULL;
3206 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3207 568eae95 2022-09-11 mark return NULL;
3209 568eae95 2022-09-11 mark s->limit_view = 1;
3211 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3212 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3213 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3214 568eae95 2022-09-11 mark s->selected_entry = NULL;
3215 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3217 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3218 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3219 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3221 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3222 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3223 568eae95 2022-09-11 mark int have_match = 0;
3225 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3226 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3228 568eae95 2022-09-11 mark return err;
3230 568eae95 2022-09-11 mark if (have_match) {
3231 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3233 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3234 568eae95 2022-09-11 mark entry->id);
3235 568eae95 2022-09-11 mark if (matched == NULL) {
3236 568eae95 2022-09-11 mark err = got_error_from_errno(
3237 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3240 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3241 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3243 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3244 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3245 568eae95 2022-09-11 mark matched, entry);
3246 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3250 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3251 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3252 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3253 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3254 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3255 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3257 568eae95 2022-09-11 mark return err;
3260 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3261 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3262 568eae95 2022-09-11 mark s->selected = 0;
3264 568eae95 2022-09-11 mark return NULL;
3267 568eae95 2022-09-11 mark static const struct got_error *
3268 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3270 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3272 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3273 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3274 60493ae3 2019-06-20 stsp return NULL;
3277 60493ae3 2019-06-20 stsp static const struct got_error *
3278 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3280 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3281 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3282 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3284 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3285 f9686aa5 2020-03-27 stsp show_log_view(view);
3286 f9686aa5 2020-03-27 stsp update_panels();
3287 f9686aa5 2020-03-27 stsp doupdate();
3289 96e2b566 2019-07-08 stsp if (s->search_entry) {
3290 13add988 2019-10-15 stsp int errcode, ch;
3291 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3292 13add988 2019-10-15 stsp if (errcode)
3293 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3294 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3295 13add988 2019-10-15 stsp ch = wgetch(view->window);
3296 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3297 13add988 2019-10-15 stsp if (errcode)
3298 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3299 13add988 2019-10-15 stsp "pthread_mutex_lock");
3300 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3301 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3302 678cbce5 2019-07-28 stsp return NULL;
3304 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3305 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3307 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3308 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3309 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3311 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3312 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3313 f704b35c 2022-06-18 stsp * might have changed.
3315 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3316 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3318 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3321 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3324 60493ae3 2019-06-20 stsp while (1) {
3325 13add988 2019-10-15 stsp int have_match = 0;
3327 60493ae3 2019-06-20 stsp if (entry == NULL) {
3328 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3329 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3330 f9967bca 2020-03-27 stsp view->search_next_done =
3331 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3332 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3333 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3334 f801134a 2019-06-25 stsp return NULL;
3337 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3338 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3339 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3341 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3342 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3345 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3346 13add988 2019-10-15 stsp &view->regex);
3349 13add988 2019-10-15 stsp if (have_match) {
3350 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3351 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3355 96e2b566 2019-07-08 stsp s->search_entry = entry;
3356 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3357 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3359 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3362 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3363 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3364 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3365 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3367 60493ae3 2019-06-20 stsp return err;
3370 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3371 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3373 60493ae3 2019-06-20 stsp return err;
3378 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3380 60493ae3 2019-06-20 stsp return NULL;
3383 60493ae3 2019-06-20 stsp static const struct got_error *
3384 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3385 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3386 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3388 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3389 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3390 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3391 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3392 1a76625f 2018-10-22 stsp int errcode;
3394 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3395 f135c941 2020-02-20 stsp free(s->in_repo_path);
3396 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3397 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3398 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3401 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3402 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3403 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3404 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3406 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3407 568eae95 2022-09-11 mark s->limit_view = 0;
3408 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3410 fb2756b9 2018-08-04 stsp s->repo = repo;
3411 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3412 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3413 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3414 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3415 9cd7cbd1 2020-12-07 stsp goto done;
3418 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3419 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3420 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3421 5036bf37 2018-09-24 stsp goto done;
3423 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3424 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3426 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3427 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3428 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3429 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3431 11b20872 2019-11-08 stsp goto done;
3432 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3433 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3434 11b20872 2019-11-08 stsp if (err) {
3435 11b20872 2019-11-08 stsp free_colors(&s->colors);
3436 11b20872 2019-11-08 stsp goto done;
3438 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3439 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3440 11b20872 2019-11-08 stsp if (err) {
3441 11b20872 2019-11-08 stsp free_colors(&s->colors);
3442 11b20872 2019-11-08 stsp goto done;
3446 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3447 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3448 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3449 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3450 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3451 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3453 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3454 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3456 74467cc8 2022-06-15 stsp goto done;
3458 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3459 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3460 0ae84acc 2022-06-15 tracey if (err)
3461 0ae84acc 2022-06-15 tracey goto done;
3462 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3463 b672a97a 2020-01-27 stsp !s->log_branches);
3465 1a76625f 2018-10-22 stsp goto done;
3466 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3467 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3469 c5b78334 2020-01-12 stsp goto done;
3471 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3472 1a76625f 2018-10-22 stsp if (errcode) {
3473 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3474 1a76625f 2018-10-22 stsp goto done;
3476 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3477 7c1452c1 2020-03-26 stsp if (errcode) {
3478 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3479 7c1452c1 2020-03-26 stsp goto done;
3482 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3483 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3484 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3485 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3486 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3487 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3488 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3489 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3490 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3491 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3492 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3493 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3494 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3495 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3496 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3497 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3498 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3501 1a76625f 2018-10-22 stsp close_log_view(view);
3502 ba4f502b 2018-08-04 stsp return err;
3505 e5a0f69f 2018-08-18 stsp static const struct got_error *
3506 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3508 f2f6d207 2020-11-24 stsp const struct got_error *err;
3509 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3511 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3512 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3513 2b380cc8 2018-10-24 stsp &s->thread_args);
3514 2b380cc8 2018-10-24 stsp if (errcode)
3515 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3516 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3517 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3519 f2f6d207 2020-11-24 stsp return err;
3523 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3526 b880cc75 2022-06-30 mark static void
3527 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3529 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3531 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3533 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3534 568eae95 2022-09-11 mark view->count = 0;
3536 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3538 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3540 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3541 b880cc75 2022-06-30 mark --s->selected;
3543 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3545 b880cc75 2022-06-30 mark select_commit(s);
3549 b880cc75 2022-06-30 mark static const struct got_error *
3550 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3552 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3553 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3554 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3556 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3557 568eae95 2022-09-11 mark return NULL;
3559 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3560 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3561 b880cc75 2022-06-30 mark return NULL;
3563 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3564 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3566 631e7531 2022-08-12 mark if (!page) {
3567 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3568 b880cc75 2022-06-30 mark ++s->selected;
3570 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3571 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3572 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3575 631e7531 2022-08-12 mark s->selected = 0;
3576 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3577 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3578 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3579 631e7531 2022-08-12 mark if (entry == NULL)
3581 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3582 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3584 631e7531 2022-08-12 mark if (n > 0)
3585 631e7531 2022-08-12 mark s->selected = n - 1;
3587 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3588 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3589 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3590 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3592 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3595 b880cc75 2022-06-30 mark return err;
3598 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3599 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3601 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3602 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3603 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3604 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3607 b880cc75 2022-06-30 mark select_commit(s);
3609 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3610 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3611 b880cc75 2022-06-30 mark view->count = 0;
3613 b880cc75 2022-06-30 mark return NULL;
3616 9b058f45 2022-06-30 mark static void
3617 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3622 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3623 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3624 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3625 7532ccda 2022-07-11 mark else if (view->resized_y)
3626 7532ccda 2022-07-11 mark *y = view->resized_y;
3628 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3629 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3630 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3631 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3632 7532ccda 2022-07-11 mark else if (view->resized_x)
3633 7532ccda 2022-07-11 mark *x = view->resized_x;
3635 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3639 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3640 e5a0f69f 2018-08-18 stsp static const struct got_error *
3641 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3643 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3645 9b058f45 2022-06-30 mark view->nlines = y;
3646 d2366e29 2022-07-07 mark view->ncols = COLS;
3647 9b058f45 2022-06-30 mark err = view_resize(view);
3649 9b058f45 2022-06-30 mark return err;
3651 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3653 9b058f45 2022-06-30 mark return err;
3656 94b80cfa 2022-08-01 mark static const struct got_error *
3657 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3659 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3660 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3661 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3663 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3664 374f69dd 2022-08-13 stsp return NULL;
3666 94b80cfa 2022-08-01 mark g = view->gline;
3667 94b80cfa 2022-08-01 mark view->gline = 0;
3669 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3670 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3671 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3672 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3673 94b80cfa 2022-08-01 mark select_commit(s);
3674 94b80cfa 2022-08-01 mark return NULL;
3677 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3678 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3679 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3680 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3681 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3683 94b80cfa 2022-08-01 mark return err;
3684 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3685 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3687 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3688 94b80cfa 2022-08-01 mark s->selected = g - 1;
3690 94b80cfa 2022-08-01 mark select_commit(s);
3691 94b80cfa 2022-08-01 mark return NULL;
3695 d627976f 2023-02-01 mark static void
3696 d627976f 2023-02-01 mark horizontal_scroll_input(struct tog_view *view, int ch)
3699 d627976f 2023-02-01 mark switch (ch) {
3700 d627976f 2023-02-01 mark case KEY_LEFT:
3702 d627976f 2023-02-01 mark view->x -= MIN(view->x, 2);
3703 d627976f 2023-02-01 mark if (view->x <= 0)
3704 d627976f 2023-02-01 mark view->count = 0;
3706 d627976f 2023-02-01 mark case KEY_RIGHT:
3708 d627976f 2023-02-01 mark if (view->x + view->ncols / 2 < view->maxx)
3709 d627976f 2023-02-01 mark view->x += 2;
3711 d627976f 2023-02-01 mark view->count = 0;
3714 d627976f 2023-02-01 mark view->x = 0;
3717 d627976f 2023-02-01 mark view->x = MAX(view->maxx - view->ncols / 2, 0);
3718 d627976f 2023-02-01 mark view->count = 0;
3725 9b058f45 2022-06-30 mark static const struct got_error *
3726 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3728 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3729 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3730 631e7531 2022-08-12 mark int eos, nscroll;
3732 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3733 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3734 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3735 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3736 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3737 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3740 11edf34c 2022-08-12 mark return err;
3743 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3744 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3745 0dca135e 2022-06-30 mark --eos; /* border */
3747 94b80cfa 2022-08-01 mark if (view->gline)
3748 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3750 528dedf3 2021-08-30 stsp switch (ch) {
3752 568eae95 2022-09-11 mark err = limit_log_view(view);
3755 1e37a5c2 2019-05-12 jcs s->quit = 1;
3759 145b6838 2022-06-16 stsp case KEY_RIGHT:
3761 145b6838 2022-06-16 stsp case KEY_LEFT:
3763 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
3766 1e37a5c2 2019-05-12 jcs case KEY_UP:
3769 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3770 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3774 912a3f79 2021-08-30 j case KEY_HOME:
3775 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3776 640cd7ff 2022-06-22 mark view->count = 0;
3778 83cc4199 2022-06-13 stsp case CTRL('u'):
3780 83cc4199 2022-06-13 stsp nscroll /= 2;
3781 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3782 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3783 a4292ac5 2019-05-12 jcs case CTRL('b'):
3785 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3788 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3791 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3792 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3795 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3796 3f6c6614 2023-01-23 mark view->action = s->use_committer ?
3797 3f6c6614 2023-01-23 mark "show committer" : "show commit author";
3801 912a3f79 2021-08-30 j case KEY_END: {
3802 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3803 912a3f79 2021-08-30 j * traverse them all. */
3804 640cd7ff 2022-06-22 mark view->count = 0;
3805 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3806 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3807 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3808 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3809 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3812 80b7e8da 2022-06-11 stsp case CTRL('d'):
3814 83cc4199 2022-06-13 stsp nscroll /= 2;
3815 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3816 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3817 61417565 2022-06-20 mark case CTRL('f'):
3818 48bb96f0 2022-06-20 naddy case 'f':
3820 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3822 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3823 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3824 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3825 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3826 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3827 2b779855 2020-12-05 naddy select_commit(s);
3828 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3829 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3830 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3831 568eae95 2022-09-11 mark s->commits->ncommits;
3832 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3835 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3836 49b24ee5 2022-07-03 mark case '\r':
3837 640cd7ff 2022-06-22 mark view->count = 0;
3838 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3840 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3843 640cd7ff 2022-06-22 mark view->count = 0;
3844 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3846 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3848 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3849 21355643 2020-12-06 stsp case CTRL('l'):
3851 640cd7ff 2022-06-22 mark view->count = 0;
3852 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3853 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3855 21355643 2020-12-06 stsp err = stop_log_thread(s);
3857 74cfe85e 2020-10-20 stsp return err;
3858 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3859 21355643 2020-12-06 stsp char *parent_path;
3860 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3862 21355643 2020-12-06 stsp return err;
3863 21355643 2020-12-06 stsp free(s->in_repo_path);
3864 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3865 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3866 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3867 21355643 2020-12-06 stsp struct got_object_id *start_id;
3868 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3869 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3870 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3871 ea371198 2022-11-17 stsp if (err) {
3872 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
3873 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
3874 ea371198 2022-11-17 stsp return err;
3875 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
3876 ea371198 2022-11-17 stsp free(s->head_ref_name);
3877 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
3878 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
3879 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3880 ea371198 2022-11-17 stsp &tog_refs, s->repo);
3882 ea371198 2022-11-17 stsp return err;
3884 21355643 2020-12-06 stsp free(s->start_id);
3885 21355643 2020-12-06 stsp s->start_id = start_id;
3886 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3887 21355643 2020-12-06 stsp } else /* 'B' */
3888 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3890 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3891 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3893 b0dd8d27 2022-06-16 stsp return err;
3895 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3896 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3897 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3899 21355643 2020-12-06 stsp return err;
3900 51a10b52 2020-12-26 stsp tog_free_refs();
3901 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3903 ca51c541 2020-12-07 stsp return err;
3904 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3905 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3907 d01904d4 2019-06-25 stsp return err;
3908 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3909 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3911 b672a97a 2020-01-27 stsp return err;
3912 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3913 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3914 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3915 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3916 21355643 2020-12-06 stsp s->selected_entry = NULL;
3917 21355643 2020-12-06 stsp s->selected = 0;
3918 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3919 21355643 2020-12-06 stsp s->quit = 0;
3920 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3921 dfee752e 2022-06-18 op s->matched_entry = NULL;
3922 dfee752e 2022-06-18 op s->search_entry = NULL;
3923 01a7bcaf 2022-07-22 mark view->offset = 0;
3926 640cd7ff 2022-06-22 mark view->count = 0;
3927 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3930 640cd7ff 2022-06-22 mark view->count = 0;
3934 80ddbec8 2018-04-29 stsp return err;
3937 4ed7e80c 2018-05-20 stsp static const struct got_error *
3938 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3940 c2db6724 2019-01-04 stsp const struct got_error *error;
3942 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3943 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3944 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3946 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3947 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3949 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3950 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3952 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3953 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3955 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3956 c2db6724 2019-01-04 stsp if (error != NULL)
3957 c2db6724 2019-01-04 stsp return error;
3959 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3960 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3962 c2db6724 2019-01-04 stsp return NULL;
3965 a915003a 2019-02-05 stsp static void
3966 a915003a 2019-02-05 stsp init_curses(void)
3969 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3970 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3971 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3973 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3974 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3975 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3976 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3977 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3979 a915003a 2019-02-05 stsp initscr();
3981 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3984 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3985 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3986 a915003a 2019-02-05 stsp curs_set(0);
3987 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3988 6d17833f 2019-11-08 stsp start_color();
3989 6d17833f 2019-11-08 stsp use_default_colors();
3993 c2db6724 2019-01-04 stsp static const struct got_error *
3994 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3995 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3997 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3999 f135c941 2020-02-20 stsp if (argc == 0) {
4000 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4001 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4002 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4003 f135c941 2020-02-20 stsp return NULL;
4006 f135c941 2020-02-20 stsp if (worktree) {
4007 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4010 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4012 f135c941 2020-02-20 stsp return err;
4013 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4014 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4015 bfd61697 2020-11-14 stsp p) == -1) {
4016 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4017 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4021 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4023 f135c941 2020-02-20 stsp return err;
4026 f135c941 2020-02-20 stsp static const struct got_error *
4027 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4029 80ddbec8 2018-04-29 stsp const struct got_error *error;
4030 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4031 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4032 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4033 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4034 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4035 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4036 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4037 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4038 04cc582a 2018-08-01 stsp struct tog_view *view;
4039 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4041 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4042 80ddbec8 2018-04-29 stsp switch (ch) {
4044 b672a97a 2020-01-27 stsp log_branches = 1;
4047 80ddbec8 2018-04-29 stsp start_commit = optarg;
4050 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4051 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4052 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4056 17020d27 2019-03-07 stsp usage_log();
4057 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4061 80ddbec8 2018-04-29 stsp argc -= optind;
4062 80ddbec8 2018-04-29 stsp argv += optind;
4064 f135c941 2020-02-20 stsp if (argc > 1)
4065 f135c941 2020-02-20 stsp usage_log();
4067 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4068 0ae84acc 2022-06-15 tracey if (error != NULL)
4069 0ae84acc 2022-06-15 tracey goto done;
4071 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4072 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4073 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4074 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4075 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4076 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4077 c156c7a4 2020-12-18 stsp goto done;
4078 a1fbf39a 2019-08-11 stsp if (worktree)
4079 6962eb72 2020-02-20 stsp repo_path =
4080 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4082 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4083 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4084 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4085 c156c7a4 2020-12-18 stsp goto done;
4089 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4090 80ddbec8 2018-04-29 stsp if (error != NULL)
4091 ecb28ae0 2018-07-16 stsp goto done;
4093 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4094 f135c941 2020-02-20 stsp repo, worktree);
4095 f135c941 2020-02-20 stsp if (error)
4096 f135c941 2020-02-20 stsp goto done;
4098 f135c941 2020-02-20 stsp init_curses();
4100 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4101 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4102 c02c541e 2019-03-29 stsp if (error)
4103 c02c541e 2019-03-29 stsp goto done;
4105 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4106 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4107 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4108 87670572 2020-12-26 naddy if (error)
4109 87670572 2020-12-26 naddy goto done;
4112 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4113 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4114 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4115 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4116 d8f38dc4 2020-12-05 stsp if (error)
4117 d8f38dc4 2020-12-05 stsp goto done;
4118 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4120 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4121 d8f38dc4 2020-12-05 stsp if (error == NULL)
4122 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4123 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4124 d8f38dc4 2020-12-05 stsp goto done;
4125 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4126 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4127 d8f38dc4 2020-12-05 stsp if (error)
4128 d8f38dc4 2020-12-05 stsp goto done;
4131 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4132 04cc582a 2018-08-01 stsp if (view == NULL) {
4133 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4134 04cc582a 2018-08-01 stsp goto done;
4136 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4137 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4138 ba4f502b 2018-08-04 stsp if (error)
4139 ba4f502b 2018-08-04 stsp goto done;
4140 2fc00ff4 2019-08-31 stsp if (worktree) {
4141 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4142 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4143 2fc00ff4 2019-08-31 stsp worktree = NULL;
4145 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4147 f135c941 2020-02-20 stsp free(in_repo_path);
4148 ecb28ae0 2018-07-16 stsp free(repo_path);
4149 ecb28ae0 2018-07-16 stsp free(cwd);
4150 899d86c2 2018-05-10 stsp free(start_id);
4151 d8f38dc4 2020-12-05 stsp free(label);
4153 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4154 1d0f4054 2021-06-17 stsp if (repo) {
4155 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4156 1d0f4054 2021-06-17 stsp if (error == NULL)
4157 1d0f4054 2021-06-17 stsp error = close_err;
4159 ec142235 2019-03-07 stsp if (worktree)
4160 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4161 0ae84acc 2022-06-15 tracey if (pack_fds) {
4162 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4163 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4164 0ae84acc 2022-06-15 tracey if (error == NULL)
4165 0ae84acc 2022-06-15 tracey error = pack_err;
4167 51a10b52 2020-12-26 stsp tog_free_refs();
4168 80ddbec8 2018-04-29 stsp return error;
4171 4ed7e80c 2018-05-20 stsp __dead static void
4172 9f7d7167 2018-04-29 stsp usage_diff(void)
4175 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4176 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4180 6d17833f 2019-11-08 stsp static int
4181 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4182 41605754 2020-11-12 stsp regmatch_t *regmatch)
4184 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4187 336075a4 2022-06-25 op static struct tog_color *
4188 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4190 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4192 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4193 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4194 6d17833f 2019-11-08 stsp return tc;
4197 6d17833f 2019-11-08 stsp return NULL;
4200 4ed7e80c 2018-05-20 stsp static const struct got_error *
4201 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4202 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4204 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4205 44a87665 2022-06-16 stsp char *exstr = NULL;
4206 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4207 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4208 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4209 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4211 41605754 2020-11-12 stsp *wtotal = 0;
4213 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4214 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4216 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4218 44a87665 2022-06-16 stsp return err;
4220 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4221 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4222 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4223 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4224 44a87665 2022-06-16 stsp goto done;
4226 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4227 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4228 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4229 1853e0f4 2022-06-16 stsp goto done;
4231 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4232 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4233 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4234 1853e0f4 2022-06-16 stsp goto done;
4237 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4238 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4239 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4241 1853e0f4 2022-06-16 stsp goto done;
4242 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4244 1853e0f4 2022-06-16 stsp free(wline);
4245 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4246 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4248 1853e0f4 2022-06-16 stsp goto done;
4249 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4250 1853e0f4 2022-06-16 stsp wlimit -= width;
4251 1853e0f4 2022-06-16 stsp *wtotal += width;
4254 41605754 2020-11-12 stsp if (wlimit > 0) {
4255 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4256 1853e0f4 2022-06-16 stsp size_t wlen;
4258 1853e0f4 2022-06-16 stsp free(wline);
4259 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4260 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4262 1853e0f4 2022-06-16 stsp goto done;
4263 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4264 1853e0f4 2022-06-16 stsp while (i < wlen) {
4265 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4266 1853e0f4 2022-06-16 stsp if (width == -1) {
4267 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4268 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4269 1853e0f4 2022-06-16 stsp goto done;
4271 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4273 61417565 2022-06-20 mark w += width;
4276 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4277 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4278 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4279 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4280 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4281 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4282 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4286 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4287 1853e0f4 2022-06-16 stsp free(wline);
4288 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4289 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4290 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4291 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4293 1853e0f4 2022-06-16 stsp goto done;
4294 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4296 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4297 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4299 1853e0f4 2022-06-16 stsp goto done;
4300 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4302 1853e0f4 2022-06-16 stsp *wtotal += width2;
4305 145b6838 2022-06-16 stsp free(wline);
4306 44a87665 2022-06-16 stsp free(exstr);
4307 1853e0f4 2022-06-16 stsp free(seg0);
4308 1853e0f4 2022-06-16 stsp free(seg1);
4309 1853e0f4 2022-06-16 stsp free(seg2);
4310 1853e0f4 2022-06-16 stsp return err;
4313 94b80cfa 2022-08-01 mark static int
4314 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4316 94b80cfa 2022-08-01 mark FILE *f = NULL;
4317 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4319 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4320 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4322 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4323 ec2a9698 2022-09-15 mark selected = first;
4324 ec2a9698 2022-09-15 mark eof = &s->eof;
4326 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4327 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4329 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4330 94b80cfa 2022-08-01 mark selected = first;
4331 94b80cfa 2022-08-01 mark eof = &s->eof;
4333 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4334 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4336 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4337 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4338 94b80cfa 2022-08-01 mark eof = &s->eof;
4339 94b80cfa 2022-08-01 mark f = s->blame.f;
4343 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4344 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4346 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4347 94b80cfa 2022-08-01 mark rewind(f);
4349 94b80cfa 2022-08-01 mark *first = 1;
4350 94b80cfa 2022-08-01 mark *lineno = 0;
4351 94b80cfa 2022-08-01 mark *nprinted = 0;
4355 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4356 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4357 94b80cfa 2022-08-01 mark view->gline = 0;
4362 41605754 2020-11-12 stsp static const struct got_error *
4363 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4365 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4366 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4367 61e69b96 2018-05-20 stsp const struct got_error *err;
4368 c7d5c43c 2022-08-04 mark int nprinted = 0;
4369 b304db33 2018-05-20 stsp char *line;
4370 826082fe 2020-12-10 stsp size_t linesize = 0;
4371 826082fe 2020-12-10 stsp ssize_t linelen;
4372 61e69b96 2018-05-20 stsp wchar_t *wline;
4373 e0b650dd 2018-05-20 stsp int width;
4374 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4375 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4376 fe621944 2020-11-10 stsp off_t line_offset;
4378 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4379 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4380 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4381 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4383 f7d12f7e 2018-08-01 stsp werase(view->window);
4385 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4386 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4388 a3404814 2018-09-02 stsp if (header) {
4389 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4390 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4391 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4393 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4394 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4395 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4397 135a2da0 2020-11-11 stsp free(line);
4399 a3404814 2018-09-02 stsp return err;
4401 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4402 a3404814 2018-09-02 stsp wstandout(view->window);
4403 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4404 e54cc94a 2020-11-11 stsp free(wline);
4405 e54cc94a 2020-11-11 stsp wline = NULL;
4406 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4407 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4408 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4409 a3404814 2018-09-02 stsp wstandend(view->window);
4411 a3404814 2018-09-02 stsp if (max_lines <= 1)
4412 a3404814 2018-09-02 stsp return NULL;
4413 a3404814 2018-09-02 stsp max_lines--;
4416 89f1a395 2020-12-01 naddy s->eof = 0;
4417 145b6838 2022-06-16 stsp view->maxx = 0;
4418 826082fe 2020-12-10 stsp line = NULL;
4419 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4420 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4421 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4423 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4424 826082fe 2020-12-10 stsp if (linelen == -1) {
4425 826082fe 2020-12-10 stsp if (feof(s->f)) {
4426 826082fe 2020-12-10 stsp s->eof = 1;
4429 826082fe 2020-12-10 stsp free(line);
4430 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4433 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4435 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4437 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4438 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4440 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4441 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4442 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4443 1853e0f4 2022-06-16 stsp if (err) {
4444 1853e0f4 2022-06-16 stsp free(line);
4445 1853e0f4 2022-06-16 stsp return err;
4447 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4448 1853e0f4 2022-06-16 stsp free(wline);
4449 1853e0f4 2022-06-16 stsp wline = NULL;
4451 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4452 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4453 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4454 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4456 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4457 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4458 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4459 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4460 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4461 41605754 2020-11-12 stsp if (err) {
4462 41605754 2020-11-12 stsp free(line);
4463 41605754 2020-11-12 stsp return err;
4467 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4468 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4469 969c159c 2022-06-16 stsp if (err) {
4470 969c159c 2022-06-16 stsp free(line);
4471 969c159c 2022-06-16 stsp return err;
4473 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4474 969c159c 2022-06-16 stsp free(wline);
4475 41605754 2020-11-12 stsp wline = NULL;
4477 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4478 94b80cfa 2022-08-01 mark /* highlight full gline length */
4479 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4480 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4482 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4483 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4486 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4487 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4488 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4490 826082fe 2020-12-10 stsp free(line);
4491 fe621944 2020-11-10 stsp if (nprinted >= 1)
4492 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4493 89f1a395 2020-12-01 naddy (nprinted - 1);
4495 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4497 9b058f45 2022-06-30 mark view_border(view);
4499 89f1a395 2020-12-01 naddy if (s->eof) {
4500 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4501 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4502 c3e9aa98 2019-05-13 jcs nprinted++;
4505 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4506 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4508 c3e9aa98 2019-05-13 jcs return err;
4511 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4512 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4513 e54cc94a 2020-11-11 stsp free(wline);
4514 e54cc94a 2020-11-11 stsp wline = NULL;
4515 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4518 26ed57b2 2018-05-19 stsp return NULL;
4521 abd2672a 2018-12-23 stsp static char *
4522 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4524 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4525 09867e48 2019-08-13 stsp char *p, *s;
4527 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4528 09867e48 2019-08-13 stsp if (tm == NULL)
4529 09867e48 2019-08-13 stsp return NULL;
4530 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4531 09867e48 2019-08-13 stsp if (s == NULL)
4532 09867e48 2019-08-13 stsp return NULL;
4533 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4535 abd2672a 2018-12-23 stsp *p = '\0';
4539 4ed7e80c 2018-05-20 stsp static const struct got_error *
4540 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4541 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4543 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4545 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4546 fe621944 2020-11-10 stsp if (p == NULL)
4547 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4548 c7d5c43c 2022-08-04 mark *lines = p;
4549 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4550 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4551 fe621944 2020-11-10 stsp (*nlines)++;
4553 fe621944 2020-11-10 stsp return NULL;
4556 fe621944 2020-11-10 stsp static const struct got_error *
4557 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4558 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4560 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4561 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4562 1f3405c9 2023-01-17 mark size_t i, r;
4564 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4565 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4567 1f3405c9 2023-01-17 mark for (;;) {
4568 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4569 1f3405c9 2023-01-17 mark if (r == 0) {
4570 1f3405c9 2023-01-17 mark if (ferror(src))
4571 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4572 1f3405c9 2023-01-17 mark if (feof(src))
4575 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4576 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4580 1f3405c9 2023-01-17 mark * The diff driver initialises the first line at offset zero when the
4581 1f3405c9 2023-01-17 mark * array isn't prepopulated, skip it; we already have it in *d_lines.
4583 1f3405c9 2023-01-17 mark for (i = 1; i < s_nlines; ++i)
4584 1f3405c9 2023-01-17 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4586 1f3405c9 2023-01-17 mark --s_nlines;
4588 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4589 1f3405c9 2023-01-17 mark if (p == NULL) {
4590 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4591 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4594 1f3405c9 2023-01-17 mark *d_lines = p;
4596 1f3405c9 2023-01-17 mark memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4597 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4599 1f3405c9 2023-01-17 mark return NULL;
4602 1f3405c9 2023-01-17 mark static const struct got_error *
4603 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4604 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4605 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4606 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4608 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4609 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4610 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4611 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4612 45d799e2 2018-12-23 stsp time_t committer_time;
4613 45d799e2 2018-12-23 stsp const char *author, *committer;
4614 8b473291 2019-02-21 stsp char *refs_str = NULL;
4615 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4616 fe621944 2020-11-10 stsp off_t outoff = 0;
4619 8b473291 2019-02-21 stsp if (refs) {
4620 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4622 8b473291 2019-02-21 stsp return err;
4625 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4627 abd2672a 2018-12-23 stsp return err;
4629 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4630 15a94983 2018-12-23 stsp if (err) {
4631 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4632 15a94983 2018-12-23 stsp goto done;
4635 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4637 fe621944 2020-11-10 stsp goto done;
4639 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4640 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4641 fe621944 2020-11-10 stsp if (n < 0) {
4642 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4643 abd2672a 2018-12-23 stsp goto done;
4645 fe621944 2020-11-10 stsp outoff += n;
4646 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4648 fe621944 2020-11-10 stsp goto done;
4650 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4651 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4652 fe621944 2020-11-10 stsp if (n < 0) {
4653 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4654 abd2672a 2018-12-23 stsp goto done;
4656 fe621944 2020-11-10 stsp outoff += n;
4657 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4659 fe621944 2020-11-10 stsp goto done;
4661 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4662 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4663 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4664 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4665 fe621944 2020-11-10 stsp if (n < 0) {
4666 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4667 fe621944 2020-11-10 stsp goto done;
4669 fe621944 2020-11-10 stsp outoff += n;
4670 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4671 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4673 fe621944 2020-11-10 stsp goto done;
4675 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4676 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4677 1a99e0b4 2023-01-07 stsp if (datestr) {
4678 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4679 fe621944 2020-11-10 stsp if (n < 0) {
4680 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4681 fe621944 2020-11-10 stsp goto done;
4683 fe621944 2020-11-10 stsp outoff += n;
4684 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4685 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4687 fe621944 2020-11-10 stsp goto done;
4689 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4690 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4691 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4692 9f98ca05 2021-09-24 stsp int pn = 1;
4693 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4694 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4695 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4697 9f98ca05 2021-09-24 stsp goto done;
4698 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4699 9f98ca05 2021-09-24 stsp if (n < 0) {
4700 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4701 9f98ca05 2021-09-24 stsp goto done;
4703 9f98ca05 2021-09-24 stsp outoff += n;
4704 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4705 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4707 9f98ca05 2021-09-24 stsp goto done;
4708 9f98ca05 2021-09-24 stsp free(id_str);
4709 9f98ca05 2021-09-24 stsp id_str = NULL;
4713 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4715 5943eee2 2019-08-13 stsp goto done;
4716 fe621944 2020-11-10 stsp s = logmsg;
4717 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4718 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4719 fe621944 2020-11-10 stsp if (n < 0) {
4720 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4721 fe621944 2020-11-10 stsp goto done;
4723 fe621944 2020-11-10 stsp outoff += n;
4724 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4725 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4727 fe621944 2020-11-10 stsp goto done;
4730 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
4731 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4732 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
4734 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4735 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4736 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
4737 fe621944 2020-11-10 stsp if (n < 0) {
4738 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4739 fe621944 2020-11-10 stsp goto done;
4741 fe621944 2020-11-10 stsp outoff += n;
4742 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4743 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4745 fe621944 2020-11-10 stsp goto done;
4748 0208f208 2020-05-05 stsp fputc('\n', outfile);
4750 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4752 5191b70b 2023-01-07 mark goto done;
4754 5191b70b 2023-01-07 mark n = fprintf(outfile,
4755 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4756 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4757 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4758 5191b70b 2023-01-07 mark if (n < 0) {
4759 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
4760 5191b70b 2023-01-07 mark goto done;
4762 5191b70b 2023-01-07 mark outoff += n;
4763 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4765 5191b70b 2023-01-07 mark goto done;
4767 5191b70b 2023-01-07 mark fputc('\n', outfile);
4769 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4771 abd2672a 2018-12-23 stsp free(id_str);
4772 5943eee2 2019-08-13 stsp free(logmsg);
4773 8b473291 2019-02-21 stsp free(refs_str);
4774 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4775 7510f233 2020-08-09 stsp if (err) {
4776 c7d5c43c 2022-08-04 mark free(*lines);
4777 c7d5c43c 2022-08-04 mark *lines = NULL;
4778 ae6a6978 2020-08-09 stsp *nlines = 0;
4780 fe621944 2020-11-10 stsp return err;
4783 abd2672a 2018-12-23 stsp static const struct got_error *
4784 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4786 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4787 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
4788 15a94983 2018-12-23 stsp int obj_type;
4789 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
4790 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
4792 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
4794 c7d5c43c 2022-08-04 mark free(s->lines);
4795 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4796 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4797 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4798 fe621944 2020-11-10 stsp s->nlines = 0;
4800 511a516b 2018-05-19 stsp f = got_opentemp();
4801 48ae06ee 2018-10-18 stsp if (f == NULL) {
4802 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4803 48ae06ee 2018-10-18 stsp goto done;
4805 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
4806 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
4807 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
4808 1f3405c9 2023-01-17 mark goto done;
4810 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4811 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4812 fb43ecf1 2019-02-11 stsp goto done;
4816 15a94983 2018-12-23 stsp if (s->id1)
4817 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4819 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4821 15a94983 2018-12-23 stsp goto done;
4823 15a94983 2018-12-23 stsp switch (obj_type) {
4824 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4825 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4826 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4827 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4828 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4831 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4832 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4833 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4834 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4835 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
4837 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4838 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4839 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4840 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4841 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4842 1f3405c9 2023-01-17 mark size_t nlines = 0;
4843 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
4844 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
4845 1f3405c9 2023-01-17 mark &changed_paths,
4846 1f3405c9 2023-01-17 mark s->ignore_whitespace,
4847 1f3405c9 2023-01-17 mark s->force_text_diff,
4848 1f3405c9 2023-01-17 mark tog_diff_algo
4851 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
4852 1f3405c9 2023-01-17 mark if (lines == NULL) {
4853 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
4854 1f3405c9 2023-01-17 mark goto done;
4857 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
4858 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
4859 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4860 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
4861 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4865 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4867 3ffacbe1 2020-02-02 tracey goto done;
4868 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4869 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4870 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4871 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4872 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
4873 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4874 f44b1f58 2020-02-02 tracey if (err)
4875 f44b1f58 2020-02-02 tracey goto done;
4876 f44b1f58 2020-02-02 tracey } else {
4877 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4878 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4879 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4880 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4881 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4882 5191b70b 2023-01-07 mark s->ignore_whitespace,
4883 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4884 f44b1f58 2020-02-02 tracey if (err)
4885 f44b1f58 2020-02-02 tracey goto done;
4890 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4892 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4893 1f3405c9 2023-01-17 mark lines, nlines);
4897 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4901 1f3405c9 2023-01-17 mark free(lines);
4902 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4903 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4904 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4905 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4906 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
4907 48ae06ee 2018-10-18 stsp return err;
4910 f5215bb9 2019-02-22 stsp static void
4911 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4913 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4914 f5215bb9 2019-02-22 stsp update_panels();
4915 f5215bb9 2019-02-22 stsp doupdate();
4918 f44b1f58 2020-02-02 tracey static const struct got_error *
4919 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4921 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4923 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4924 f44b1f58 2020-02-02 tracey return NULL;
4927 eaf2c13e 2022-09-17 mark static void
4928 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4929 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4931 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4933 eaf2c13e 2022-09-17 mark *f = s->f;
4934 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4935 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4936 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4937 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4938 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4939 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4942 ec2a9698 2022-09-15 mark static const struct got_error *
4943 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4945 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4947 f44b1f58 2020-02-02 tracey int lineno;
4948 cb713507 2022-06-16 stsp char *line = NULL;
4949 826082fe 2020-12-10 stsp size_t linesize = 0;
4950 826082fe 2020-12-10 stsp ssize_t linelen;
4951 ec2a9698 2022-09-15 mark off_t *line_offsets;
4952 ec2a9698 2022-09-15 mark size_t nlines = 0;
4953 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4955 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4956 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4957 eaf2c13e 2022-09-17 mark "view search not supported");
4958 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4959 ec2a9698 2022-09-15 mark &match, &selected);
4961 f44b1f58 2020-02-02 tracey if (!view->searching) {
4962 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4963 f44b1f58 2020-02-02 tracey return NULL;
4966 ec2a9698 2022-09-15 mark if (*match) {
4967 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4968 ec2a9698 2022-09-15 mark lineno = *match + 1;
4970 ec2a9698 2022-09-15 mark lineno = *match - 1;
4972 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4974 f44b1f58 2020-02-02 tracey while (1) {
4975 f44b1f58 2020-02-02 tracey off_t offset;
4977 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4978 ec2a9698 2022-09-15 mark if (*match == 0) {
4979 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4983 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4984 f44b1f58 2020-02-02 tracey lineno = 1;
4986 ec2a9698 2022-09-15 mark lineno = nlines;
4989 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
4990 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
4991 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
4992 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
4993 f44b1f58 2020-02-02 tracey free(line);
4994 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4996 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
4997 cb713507 2022-06-16 stsp if (linelen != -1) {
4998 cb713507 2022-06-16 stsp char *exstr;
4999 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5002 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5003 cb713507 2022-06-16 stsp &view->regmatch)) {
5004 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5005 ec2a9698 2022-09-15 mark *match = lineno;
5006 cb713507 2022-06-16 stsp free(exstr);
5009 cb713507 2022-06-16 stsp free(exstr);
5011 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5012 f44b1f58 2020-02-02 tracey lineno++;
5014 f44b1f58 2020-02-02 tracey lineno--;
5016 826082fe 2020-12-10 stsp free(line);
5018 ec2a9698 2022-09-15 mark if (*match) {
5019 ec2a9698 2022-09-15 mark *first = *match;
5020 ec2a9698 2022-09-15 mark *selected = 1;
5023 145b6838 2022-06-16 stsp return err;
5026 48ae06ee 2018-10-18 stsp static const struct got_error *
5027 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
5029 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
5030 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5032 b72706c3 2022-06-01 stsp free(s->id1);
5033 b72706c3 2022-06-01 stsp s->id1 = NULL;
5034 b72706c3 2022-06-01 stsp free(s->id2);
5035 b72706c3 2022-06-01 stsp s->id2 = NULL;
5036 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
5037 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5038 b72706c3 2022-06-01 stsp s->f = NULL;
5039 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5040 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5041 b72706c3 2022-06-01 stsp s->f1 = NULL;
5042 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5043 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5044 b72706c3 2022-06-01 stsp s->f2 = NULL;
5045 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5046 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5047 f9d37699 2022-06-28 stsp s->fd1 = -1;
5048 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5049 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5050 f9d37699 2022-06-28 stsp s->fd2 = -1;
5051 c7d5c43c 2022-08-04 mark free(s->lines);
5052 c7d5c43c 2022-08-04 mark s->lines = NULL;
5053 b72706c3 2022-06-01 stsp s->nlines = 0;
5054 b72706c3 2022-06-01 stsp return err;
5057 b72706c3 2022-06-01 stsp static const struct got_error *
5058 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5059 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5060 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5061 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
5063 48ae06ee 2018-10-18 stsp const struct got_error *err;
5064 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5066 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
5067 f9d37699 2022-06-28 stsp s->fd1 = -1;
5068 f9d37699 2022-06-28 stsp s->fd2 = -1;
5070 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5071 15a94983 2018-12-23 stsp int type1, type2;
5072 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5074 15a94983 2018-12-23 stsp return err;
5075 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5077 15a94983 2018-12-23 stsp return err;
5079 15a94983 2018-12-23 stsp if (type1 != type2)
5080 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5082 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5083 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5084 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5085 f44b1f58 2020-02-02 tracey s->repo = repo;
5086 f44b1f58 2020-02-02 tracey s->id1 = id1;
5087 f44b1f58 2020-02-02 tracey s->id2 = id2;
5088 3dbaef42 2020-11-24 stsp s->label1 = label1;
5089 3dbaef42 2020-11-24 stsp s->label2 = label2;
5091 15a94983 2018-12-23 stsp if (id1) {
5092 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5093 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5094 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5096 5465d566 2020-02-01 tracey s->id1 = NULL;
5098 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5099 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5100 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5101 b72706c3 2022-06-01 stsp goto done;
5104 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5105 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5106 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5107 a00719e9 2022-06-17 stsp goto done;
5110 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5111 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5112 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5113 b72706c3 2022-06-01 stsp goto done;
5116 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5117 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5118 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5119 f9d37699 2022-06-28 stsp goto done;
5122 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5123 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5124 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5125 f9d37699 2022-06-28 stsp goto done;
5128 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5129 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5130 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5131 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5132 5465d566 2020-02-01 tracey s->repo = repo;
5134 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5137 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5138 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5139 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5140 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5141 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5142 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5143 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5144 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5145 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5146 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5147 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5148 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5149 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5150 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5151 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5152 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5153 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5154 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5155 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5156 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5157 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5158 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5159 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5160 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5161 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5162 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5163 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5164 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5165 b72706c3 2022-06-01 stsp goto done;
5169 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5170 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5171 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5172 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5174 5465d566 2020-02-01 tracey err = create_diff(s);
5176 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5177 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5178 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5179 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5180 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5181 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5182 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5185 b72706c3 2022-06-01 stsp close_diff_view(view);
5186 e5a0f69f 2018-08-18 stsp return err;
5189 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5190 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5192 a3404814 2018-09-02 stsp const struct got_error *err;
5193 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5194 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5195 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5197 a3404814 2018-09-02 stsp if (s->id1) {
5198 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5200 a3404814 2018-09-02 stsp return err;
5201 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5203 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5205 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5207 a3404814 2018-09-02 stsp return err;
5208 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5210 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5211 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5212 a3404814 2018-09-02 stsp free(id_str1);
5213 a3404814 2018-09-02 stsp free(id_str2);
5214 a3404814 2018-09-02 stsp return err;
5216 a3404814 2018-09-02 stsp free(id_str1);
5217 a3404814 2018-09-02 stsp free(id_str2);
5219 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5220 267bb3b8 2021-08-01 stsp free(header);
5221 267bb3b8 2021-08-01 stsp return err;
5224 15a087fe 2019-02-21 stsp static const struct got_error *
5225 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5226 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5228 d7a04538 2019-02-21 stsp const struct got_error *err;
5229 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5230 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5231 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5233 15a087fe 2019-02-21 stsp free(s->id2);
5234 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5235 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5236 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5238 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5240 d7a04538 2019-02-21 stsp return err;
5241 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5242 15a087fe 2019-02-21 stsp free(s->id1);
5243 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5244 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5245 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5246 15a087fe 2019-02-21 stsp return NULL;
5249 0cf4efb1 2018-09-29 stsp static const struct got_error *
5250 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5252 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5254 917d79a7 2022-07-01 stsp view->count = 0;
5255 917d79a7 2022-07-01 stsp wclear(view->window);
5256 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5257 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5258 917d79a7 2022-07-01 stsp s->matched_line = 0;
5259 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5260 917d79a7 2022-07-01 stsp return create_diff(s);
5263 c7d5c43c 2022-08-04 mark static void
5264 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5266 c7d5c43c 2022-08-04 mark int start, i;
5268 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5270 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5271 c7d5c43c 2022-08-04 mark if (i == 0)
5272 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5273 c7d5c43c 2022-08-04 mark if (--i == start)
5274 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5277 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5278 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5281 c7d5c43c 2022-08-04 mark static void
5282 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5284 c7d5c43c 2022-08-04 mark int start, i;
5286 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5288 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5289 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5291 c7d5c43c 2022-08-04 mark if (++i == start)
5292 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5295 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5296 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5299 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5300 c0f61fa4 2022-07-11 mark int, int, int);
5301 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5302 c0f61fa4 2022-07-11 mark int, int);
5304 917d79a7 2022-07-01 stsp static const struct got_error *
5305 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5307 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5308 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5309 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5310 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5311 826082fe 2020-12-10 stsp char *line = NULL;
5312 826082fe 2020-12-10 stsp size_t linesize = 0;
5313 826082fe 2020-12-10 stsp ssize_t linelen;
5314 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5316 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5318 e5a0f69f 2018-08-18 stsp switch (ch) {
5321 145b6838 2022-06-16 stsp case KEY_RIGHT:
5323 145b6838 2022-06-16 stsp case KEY_LEFT:
5325 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
5329 3f6c6614 2023-01-23 mark if (ch == 'a') {
5330 3f6c6614 2023-01-23 mark s->force_text_diff = !s->force_text_diff;
5331 3f6c6614 2023-01-23 mark view->action = s->force_text_diff ?
5332 3f6c6614 2023-01-23 mark "force ASCII text enabled" :
5333 3f6c6614 2023-01-23 mark "force ASCII text disabled";
5335 3f6c6614 2023-01-23 mark else if (ch == 'w') {
5336 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5337 3f6c6614 2023-01-23 mark view->action = s->ignore_whitespace ?
5338 3f6c6614 2023-01-23 mark "ignore whitespace enabled" :
5339 3f6c6614 2023-01-23 mark "ignore whitespace disabled";
5341 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5344 912a3f79 2021-08-30 j case KEY_HOME:
5345 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5346 640cd7ff 2022-06-22 mark view->count = 0;
5349 912a3f79 2021-08-30 j case KEY_END:
5350 640cd7ff 2022-06-22 mark view->count = 0;
5354 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5358 1e37a5c2 2019-05-12 jcs case KEY_UP:
5359 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5360 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5361 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5363 640cd7ff 2022-06-22 mark view->count = 0;
5365 83cc4199 2022-06-13 stsp case CTRL('u'):
5367 83cc4199 2022-06-13 stsp nscroll /= 2;
5368 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5369 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5370 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5372 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5373 640cd7ff 2022-06-22 mark view->count = 0;
5377 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5378 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5381 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5382 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5383 1e37a5c2 2019-05-12 jcs if (!s->eof)
5384 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5386 640cd7ff 2022-06-22 mark view->count = 0;
5388 83cc4199 2022-06-13 stsp case CTRL('d'):
5390 83cc4199 2022-06-13 stsp nscroll /= 2;
5391 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5392 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5393 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5396 640cd7ff 2022-06-22 mark if (s->eof) {
5397 640cd7ff 2022-06-22 mark view->count = 0;
5401 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5402 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5403 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5404 826082fe 2020-12-10 stsp if (linelen == -1) {
5405 826082fe 2020-12-10 stsp if (feof(s->f)) {
5406 826082fe 2020-12-10 stsp s->eof = 1;
5408 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5412 826082fe 2020-12-10 stsp free(line);
5415 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5418 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5421 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5424 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5427 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5428 1e37a5c2 2019-05-12 jcs s->diff_context--;
5429 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5430 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5431 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5432 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5433 27829c9e 2020-11-21 stsp s->nlines) {
5434 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5435 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5438 640cd7ff 2022-06-22 mark view->count = 0;
5441 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5442 1e37a5c2 2019-05-12 jcs s->diff_context++;
5443 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5444 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5445 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5447 640cd7ff 2022-06-22 mark view->count = 0;
5453 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5457 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5458 640cd7ff 2022-06-22 mark view->count = 0;
5461 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5463 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5464 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5465 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5467 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5468 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5471 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5473 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5476 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5479 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5480 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5481 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5483 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5484 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5485 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5487 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5488 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5491 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5493 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5495 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5496 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5497 c0f61fa4 2022-07-11 mark bs->selected_line);
5498 c0f61fa4 2022-07-11 mark if (id == NULL)
5501 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5504 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5508 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5509 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5510 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5511 145b6838 2022-06-16 stsp view->x = 0;
5513 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5514 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5517 640cd7ff 2022-06-22 mark view->count = 0;
5521 bcbd79e2 2018-08-19 stsp return err;
5524 4ed7e80c 2018-05-20 stsp static const struct got_error *
5525 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5527 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5528 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5529 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5530 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5531 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5532 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5533 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5534 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5535 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5536 3dbaef42 2020-11-24 stsp const char *errstr;
5537 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5538 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5540 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5541 26ed57b2 2018-05-19 stsp switch (ch) {
5543 64453f7e 2020-11-21 stsp force_text_diff = 1;
5546 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5548 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5549 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5550 5a20d08d 2022-02-09 op errstr, errstr);
5552 09b5bff8 2020-02-23 naddy case 'r':
5553 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5554 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5555 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5557 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5560 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5563 17020d27 2019-03-07 stsp usage_diff();
5564 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5568 26ed57b2 2018-05-19 stsp argc -= optind;
5569 26ed57b2 2018-05-19 stsp argv += optind;
5571 26ed57b2 2018-05-19 stsp if (argc == 0) {
5572 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5573 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5574 15a94983 2018-12-23 stsp id_str1 = argv[0];
5575 15a94983 2018-12-23 stsp id_str2 = argv[1];
5577 26ed57b2 2018-05-19 stsp usage_diff();
5579 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5580 0ae84acc 2022-06-15 tracey if (error)
5581 0ae84acc 2022-06-15 tracey goto done;
5583 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5584 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5585 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5586 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5587 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5588 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5589 c156c7a4 2020-12-18 stsp goto done;
5590 a273ac94 2020-02-23 naddy if (worktree)
5591 a273ac94 2020-02-23 naddy repo_path =
5592 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5594 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5595 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5596 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5597 c156c7a4 2020-12-18 stsp goto done;
5601 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5602 eb6600df 2019-01-04 stsp if (error)
5603 eb6600df 2019-01-04 stsp goto done;
5605 a273ac94 2020-02-23 naddy init_curses();
5607 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5608 51a10b52 2020-12-26 stsp if (error)
5609 51a10b52 2020-12-26 stsp goto done;
5611 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5612 26ed57b2 2018-05-19 stsp if (error)
5613 26ed57b2 2018-05-19 stsp goto done;
5615 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5616 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5617 26ed57b2 2018-05-19 stsp if (error)
5618 26ed57b2 2018-05-19 stsp goto done;
5620 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5621 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5622 26ed57b2 2018-05-19 stsp if (error)
5623 26ed57b2 2018-05-19 stsp goto done;
5625 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5626 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5627 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5628 ea5e7bb5 2018-08-01 stsp goto done;
5630 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5631 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5632 5dc9f4bc 2018-08-04 stsp if (error)
5633 5dc9f4bc 2018-08-04 stsp goto done;
5634 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5636 3dbaef42 2020-11-24 stsp free(label1);
5637 3dbaef42 2020-11-24 stsp free(label2);
5638 c02c541e 2019-03-29 stsp free(repo_path);
5639 a273ac94 2020-02-23 naddy free(cwd);
5640 1d0f4054 2021-06-17 stsp if (repo) {
5641 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5642 1d0f4054 2021-06-17 stsp if (error == NULL)
5643 1d0f4054 2021-06-17 stsp error = close_err;
5645 a273ac94 2020-02-23 naddy if (worktree)
5646 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5647 0ae84acc 2022-06-15 tracey if (pack_fds) {
5648 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5649 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5650 0ae84acc 2022-06-15 tracey if (error == NULL)
5651 0ae84acc 2022-06-15 tracey error = pack_err;
5653 51a10b52 2020-12-26 stsp tog_free_refs();
5654 26ed57b2 2018-05-19 stsp return error;
5657 4ed7e80c 2018-05-20 stsp __dead static void
5658 9f7d7167 2018-04-29 stsp usage_blame(void)
5661 87411fa9 2022-06-16 stsp fprintf(stderr,
5662 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5663 9f7d7167 2018-04-29 stsp getprogname());
5667 84451b3e 2018-07-10 stsp struct tog_blame_line {
5668 84451b3e 2018-07-10 stsp int annotated;
5669 84451b3e 2018-07-10 stsp struct got_object_id *id;
5672 4ed7e80c 2018-05-20 stsp static const struct got_error *
5673 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5675 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5676 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5677 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5678 84451b3e 2018-07-10 stsp const struct got_error *err;
5679 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5680 826082fe 2020-12-10 stsp char *line = NULL;
5681 826082fe 2020-12-10 stsp size_t linesize = 0;
5682 826082fe 2020-12-10 stsp ssize_t linelen;
5683 84451b3e 2018-07-10 stsp wchar_t *wline;
5684 27a741e5 2019-09-11 stsp int width;
5685 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5686 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5687 ab089a2a 2018-07-12 stsp char *id_str;
5688 11b20872 2019-11-08 stsp struct tog_color *tc;
5690 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5692 ab089a2a 2018-07-12 stsp return err;
5694 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5695 f7d12f7e 2018-08-01 stsp werase(view->window);
5697 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5698 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5699 ab089a2a 2018-07-12 stsp free(id_str);
5700 ab089a2a 2018-07-12 stsp return err;
5703 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5704 ab089a2a 2018-07-12 stsp free(line);
5705 2550e4c3 2018-07-13 stsp line = NULL;
5707 1cae65b4 2019-09-22 stsp return err;
5708 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5709 a3404814 2018-09-02 stsp wstandout(view->window);
5710 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5712 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5713 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5714 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5715 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5717 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5718 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5719 a3404814 2018-09-02 stsp wstandend(view->window);
5720 2550e4c3 2018-07-13 stsp free(wline);
5721 2550e4c3 2018-07-13 stsp wline = NULL;
5723 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5724 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5726 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5727 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5728 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5729 ab089a2a 2018-07-12 stsp free(id_str);
5730 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5732 ab089a2a 2018-07-12 stsp free(id_str);
5733 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5734 3f60a8ef 2018-07-10 stsp free(line);
5735 2550e4c3 2018-07-13 stsp line = NULL;
5737 3f60a8ef 2018-07-10 stsp return err;
5738 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5739 2550e4c3 2018-07-13 stsp free(wline);
5740 2550e4c3 2018-07-13 stsp wline = NULL;
5741 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5742 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5744 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5745 145b6838 2022-06-16 stsp view->maxx = 0;
5746 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5747 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5748 826082fe 2020-12-10 stsp if (linelen == -1) {
5749 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5750 826082fe 2020-12-10 stsp s->eof = 1;
5753 84451b3e 2018-07-10 stsp free(line);
5754 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5756 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5758 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5761 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5762 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5763 1853e0f4 2022-06-16 stsp if (err) {
5764 1853e0f4 2022-06-16 stsp free(line);
5765 1853e0f4 2022-06-16 stsp return err;
5767 1853e0f4 2022-06-16 stsp free(wline);
5768 1853e0f4 2022-06-16 stsp wline = NULL;
5769 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5771 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5772 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5774 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5775 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5776 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5777 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5778 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5779 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5780 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5781 8d0fe45a 2019-08-12 stsp char *id_str;
5782 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5783 87411fa9 2022-06-16 stsp blame_line->id);
5784 8d0fe45a 2019-08-12 stsp if (err) {
5785 8d0fe45a 2019-08-12 stsp free(line);
5786 8d0fe45a 2019-08-12 stsp return err;
5788 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5790 11b20872 2019-11-08 stsp wattr_on(view->window,
5791 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5792 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5794 11b20872 2019-11-08 stsp wattr_off(view->window,
5795 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5796 8d0fe45a 2019-08-12 stsp free(id_str);
5797 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5799 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5800 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5803 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5804 ee41ec32 2018-07-10 stsp prev_id = NULL;
5807 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5808 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5809 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5811 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5812 41605754 2020-11-12 stsp width = 9;
5813 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5814 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5815 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5816 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5817 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5818 41605754 2020-11-12 stsp if (err) {
5819 41605754 2020-11-12 stsp free(line);
5820 41605754 2020-11-12 stsp return err;
5822 41605754 2020-11-12 stsp width += 9;
5825 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5826 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5827 4cb9d869 2022-06-16 stsp if (err) {
5828 4cb9d869 2022-06-16 stsp free(line);
5829 4cb9d869 2022-06-16 stsp return err;
5831 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5832 a75b3e2d 2022-06-16 stsp width += 9;
5833 41605754 2020-11-12 stsp free(wline);
5834 41605754 2020-11-12 stsp wline = NULL;
5837 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5838 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5839 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5840 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5842 826082fe 2020-12-10 stsp free(line);
5843 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5845 9b058f45 2022-06-30 mark view_border(view);
5847 84451b3e 2018-07-10 stsp return NULL;
5850 84451b3e 2018-07-10 stsp static const struct got_error *
5851 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5852 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5854 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5855 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5856 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5857 1a76625f 2018-10-22 stsp int errcode;
5859 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5860 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5861 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5863 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5864 1a76625f 2018-10-22 stsp if (errcode)
5865 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5867 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5868 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5869 d68a0a7d 2018-07-10 stsp goto done;
5872 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5873 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5875 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5876 d68a0a7d 2018-07-10 stsp if (line->annotated)
5877 d68a0a7d 2018-07-10 stsp goto done;
5879 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5880 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5881 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5882 84451b3e 2018-07-10 stsp goto done;
5884 84451b3e 2018-07-10 stsp line->annotated = 1;
5886 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5887 1a76625f 2018-10-22 stsp if (errcode)
5888 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5889 84451b3e 2018-07-10 stsp return err;
5892 84451b3e 2018-07-10 stsp static void *
5893 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5895 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5896 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5897 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5898 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5899 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5901 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5902 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5903 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5905 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5906 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5907 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5908 e6e73e55 2022-06-30 tracey goto done;
5911 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5912 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5913 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5914 e6e73e55 2022-06-30 tracey goto done;
5916 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5917 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5918 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5919 e6e73e55 2022-06-30 tracey goto done;
5922 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5924 e6e73e55 2022-06-30 tracey goto done;
5926 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5927 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5928 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5929 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5930 fc06ba56 2019-08-22 stsp err = NULL;
5932 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5933 e6e73e55 2022-06-30 tracey if (errcode) {
5934 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5935 e6e73e55 2022-06-30 tracey goto done;
5938 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5939 1d0f4054 2021-06-17 stsp if (err == NULL)
5940 1d0f4054 2021-06-17 stsp err = close_err;
5941 c9beca56 2018-07-22 stsp ta->repo = NULL;
5942 c9beca56 2018-07-22 stsp *ta->complete = 1;
5944 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5945 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5946 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5949 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5950 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5951 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5952 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5953 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5954 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5955 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5956 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5958 18430de3 2018-07-10 stsp return (void *)err;
5961 245d91c1 2018-07-12 stsp static struct got_object_id *
5962 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5963 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5965 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5967 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5968 8d0fe45a 2019-08-12 stsp return NULL;
5970 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5971 c0f61fa4 2022-07-11 mark if (!line->annotated)
5972 c0f61fa4 2022-07-11 mark return NULL;
5974 c0f61fa4 2022-07-11 mark return line->id;
5977 c0f61fa4 2022-07-11 mark static struct got_object_id *
5978 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5979 c0f61fa4 2022-07-11 mark int lineno)
5981 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5983 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5984 c0f61fa4 2022-07-11 mark return NULL;
5986 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5987 245d91c1 2018-07-12 stsp if (!line->annotated)
5988 245d91c1 2018-07-12 stsp return NULL;
5990 245d91c1 2018-07-12 stsp return line->id;
5993 b880a918 2018-07-10 stsp static const struct got_error *
5994 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5996 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5999 245d91c1 2018-07-12 stsp if (blame->thread) {
6000 1a76625f 2018-10-22 stsp int errcode;
6001 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6002 1a76625f 2018-10-22 stsp if (errcode)
6003 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6004 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6005 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6006 1a76625f 2018-10-22 stsp if (errcode)
6007 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6008 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6009 1a76625f 2018-10-22 stsp if (errcode)
6010 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6011 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6012 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6013 245d91c1 2018-07-12 stsp err = NULL;
6014 245d91c1 2018-07-12 stsp blame->thread = NULL;
6016 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6017 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6018 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6019 1d0f4054 2021-06-17 stsp if (err == NULL)
6020 1d0f4054 2021-06-17 stsp err = close_err;
6021 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6023 245d91c1 2018-07-12 stsp if (blame->f) {
6024 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6025 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6026 245d91c1 2018-07-12 stsp blame->f = NULL;
6028 57670559 2018-12-23 stsp if (blame->lines) {
6029 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6030 57670559 2018-12-23 stsp free(blame->lines[i].id);
6031 57670559 2018-12-23 stsp free(blame->lines);
6032 57670559 2018-12-23 stsp blame->lines = NULL;
6034 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6035 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6036 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
6037 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6038 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
6039 0ae84acc 2022-06-15 tracey if (err == NULL)
6040 0ae84acc 2022-06-15 tracey err = pack_err;
6041 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
6043 245d91c1 2018-07-12 stsp return err;
6046 245d91c1 2018-07-12 stsp static const struct got_error *
6047 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6049 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6050 fc06ba56 2019-08-22 stsp int *done = arg;
6051 fc06ba56 2019-08-22 stsp int errcode;
6053 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6054 fc06ba56 2019-08-22 stsp if (errcode)
6055 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6056 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6058 fc06ba56 2019-08-22 stsp if (*done)
6059 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6061 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6062 fc06ba56 2019-08-22 stsp if (errcode)
6063 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6064 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6066 fc06ba56 2019-08-22 stsp return err;
6069 fc06ba56 2019-08-22 stsp static const struct got_error *
6070 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6072 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6073 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6074 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6075 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6076 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6077 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6078 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6079 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6080 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6082 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6083 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6085 15a94983 2018-12-23 stsp return err;
6087 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6088 eb81bc23 2022-06-28 tracey if (fd == -1) {
6089 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6090 eb81bc23 2022-06-28 tracey goto done;
6093 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6095 a44927cc 2022-04-07 stsp goto done;
6097 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6099 84451b3e 2018-07-10 stsp goto done;
6101 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6102 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6103 84451b3e 2018-07-10 stsp goto done;
6106 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6108 a70480e0 2018-06-23 stsp goto done;
6109 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6110 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6111 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6112 84451b3e 2018-07-10 stsp goto done;
6114 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6115 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6117 1fddf795 2021-01-20 stsp goto done;
6118 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6119 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6120 84451b3e 2018-07-10 stsp goto done;
6123 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6124 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6125 b02560ec 2019-08-19 stsp blame->nlines--;
6127 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6128 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6129 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6130 84451b3e 2018-07-10 stsp goto done;
6133 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6135 bd24772e 2018-07-11 stsp goto done;
6136 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6137 0ae84acc 2022-06-15 tracey pack_fds);
6138 0ae84acc 2022-06-15 tracey if (err)
6139 0ae84acc 2022-06-15 tracey goto done;
6141 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6142 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6143 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6144 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6145 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6146 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6147 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6148 245d91c1 2018-07-12 stsp goto done;
6150 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6152 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6153 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6154 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6155 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6156 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6157 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6158 a5388363 2020-12-01 naddy s->blame_complete = 0;
6160 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6161 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6162 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6163 f5a09613 2020-12-13 naddy s->selected_line = 1;
6165 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6168 a44927cc 2022-04-07 stsp if (commit)
6169 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6170 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6171 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6173 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6174 27d434c2 2018-09-15 stsp free(obj_id);
6176 245d91c1 2018-07-12 stsp stop_blame(blame);
6177 245d91c1 2018-07-12 stsp return err;
6180 245d91c1 2018-07-12 stsp static const struct got_error *
6181 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6182 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6184 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6185 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6187 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6189 c4843652 2019-08-12 stsp s->path = strdup(path);
6190 c4843652 2019-08-12 stsp if (s->path == NULL)
6191 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6193 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6194 c4843652 2019-08-12 stsp if (err) {
6195 c4843652 2019-08-12 stsp free(s->path);
6196 7cbe629d 2018-08-04 stsp return err;
6199 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6200 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6201 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6202 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6203 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6204 fb2756b9 2018-08-04 stsp s->repo = repo;
6205 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6206 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6208 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6209 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6210 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6211 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6213 11b20872 2019-11-08 stsp return err;
6216 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6217 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6218 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6219 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6220 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6221 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6222 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6224 a5388363 2020-12-01 naddy return run_blame(view);
6227 e5a0f69f 2018-08-18 stsp static const struct got_error *
6228 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6230 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6231 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6233 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6234 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6236 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6237 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6238 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6239 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6240 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6243 e5a0f69f 2018-08-18 stsp free(s->path);
6244 11b20872 2019-11-08 stsp free_colors(&s->colors);
6245 e5a0f69f 2018-08-18 stsp return err;
6248 7cbe629d 2018-08-04 stsp static const struct got_error *
6249 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6251 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6253 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6254 6c4c42e0 2019-06-24 stsp return NULL;
6257 eaf2c13e 2022-09-17 mark static void
6258 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6259 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6261 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6263 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6264 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6265 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6266 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6267 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6268 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6269 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6272 6c4c42e0 2019-06-24 stsp static const struct got_error *
6273 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6275 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6276 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6277 2b380cc8 2018-10-24 stsp int errcode;
6279 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6280 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6281 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6282 2b380cc8 2018-10-24 stsp if (errcode)
6283 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6285 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6288 51fe7530 2019-08-19 stsp if (s->blame_complete)
6289 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6291 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6293 9b058f45 2022-06-30 mark view_border(view);
6294 e5a0f69f 2018-08-18 stsp return err;
6297 e5a0f69f 2018-08-18 stsp static const struct got_error *
6298 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6299 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6301 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6302 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6304 05f04cdf 2022-07-20 mark *new_view = NULL;
6306 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6307 05f04cdf 2022-07-20 mark if (log_view == NULL)
6308 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6310 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6312 05f04cdf 2022-07-20 mark view_close(log_view);
6314 05f04cdf 2022-07-20 mark *new_view = log_view;
6316 05f04cdf 2022-07-20 mark return err;
6319 05f04cdf 2022-07-20 mark static const struct got_error *
6320 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6322 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6323 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6324 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6325 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6327 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6328 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6329 9b058f45 2022-06-30 mark --eos; /* border */
6331 e5a0f69f 2018-08-18 stsp switch (ch) {
6334 145b6838 2022-06-16 stsp case KEY_RIGHT:
6336 145b6838 2022-06-16 stsp case KEY_LEFT:
6338 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
6341 1e37a5c2 2019-05-12 jcs s->done = 1;
6343 4deef56f 2021-09-02 naddy case 'g':
6344 4deef56f 2021-09-02 naddy case KEY_HOME:
6345 4deef56f 2021-09-02 naddy s->selected_line = 1;
6346 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6347 640cd7ff 2022-06-22 mark view->count = 0;
6349 4deef56f 2021-09-02 naddy case 'G':
6350 4deef56f 2021-09-02 naddy case KEY_END:
6351 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6352 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6353 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6355 9b058f45 2022-06-30 mark s->selected_line = eos;
6356 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6358 640cd7ff 2022-06-22 mark view->count = 0;
6361 1e37a5c2 2019-05-12 jcs case KEY_UP:
6362 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6363 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6364 1e37a5c2 2019-05-12 jcs s->selected_line--;
6365 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6366 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6367 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6369 640cd7ff 2022-06-22 mark view->count = 0;
6371 83cc4199 2022-06-13 stsp case CTRL('u'):
6373 83cc4199 2022-06-13 stsp nscroll /= 2;
6374 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6375 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6376 ea025d1d 2020-02-22 naddy case CTRL('b'):
6378 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6379 640cd7ff 2022-06-22 mark if (view->count > 1)
6380 640cd7ff 2022-06-22 mark nscroll += nscroll;
6381 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6382 640cd7ff 2022-06-22 mark view->count = 0;
6385 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6386 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6388 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6391 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6392 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6393 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6394 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6395 1e37a5c2 2019-05-12 jcs s->selected_line++;
6396 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6397 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6399 640cd7ff 2022-06-22 mark view->count = 0;
6402 1e37a5c2 2019-05-12 jcs case 'p': {
6403 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6405 640cd7ff 2022-06-22 mark view->count = 0;
6406 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6407 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6408 1e37a5c2 2019-05-12 jcs if (id == NULL)
6410 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6411 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6412 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6413 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6414 1e37a5c2 2019-05-12 jcs int obj_type;
6415 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6416 1e37a5c2 2019-05-12 jcs s->repo, id);
6419 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6420 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6421 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6422 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6425 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6426 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6427 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6430 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6431 a44927cc 2022-04-07 stsp pcommit, s->path);
6432 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6433 e5a0f69f 2018-08-18 stsp if (err) {
6434 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6435 1e37a5c2 2019-05-12 jcs err = NULL;
6436 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6439 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6441 1e37a5c2 2019-05-12 jcs free(blob_id);
6442 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6443 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6444 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6447 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6448 d7b5a0e8 2022-04-20 stsp &pid->id);
6449 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6451 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6452 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6454 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6459 1e37a5c2 2019-05-12 jcs s->done = 1;
6460 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6461 1e37a5c2 2019-05-12 jcs s->done = 0;
6462 1e37a5c2 2019-05-12 jcs if (thread_err)
6464 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6465 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6466 a5388363 2020-12-01 naddy err = run_blame(view);
6471 61417565 2022-06-20 mark case 'C': {
6472 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6474 640cd7ff 2022-06-22 mark view->count = 0;
6475 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6476 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6478 1e37a5c2 2019-05-12 jcs s->done = 1;
6479 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6480 1e37a5c2 2019-05-12 jcs s->done = 0;
6481 1e37a5c2 2019-05-12 jcs if (thread_err)
6483 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6484 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6485 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6486 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6487 a5388363 2020-12-01 naddy err = run_blame(view);
6493 05f04cdf 2022-07-20 mark view->count = 0;
6494 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6495 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6496 136e2bd4 2022-07-23 mark if (s->id_to_log)
6497 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6499 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6500 1e37a5c2 2019-05-12 jcs case '\r': {
6501 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6502 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6503 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6505 640cd7ff 2022-06-22 mark view->count = 0;
6506 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6507 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6508 1e37a5c2 2019-05-12 jcs if (id == NULL)
6510 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6513 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6514 c0f61fa4 2022-07-11 mark if (*new_view) {
6515 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6516 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6519 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6521 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6522 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6524 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6525 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6526 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6527 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6528 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6532 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6533 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6534 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6536 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6539 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6540 c0f61fa4 2022-07-11 mark s->selected_line;
6541 c0f61fa4 2022-07-11 mark if (*new_view)
6542 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6543 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6544 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6545 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6550 e78dc838 2020-12-04 stsp view->focussed = 0;
6551 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6552 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6553 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6554 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6555 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6556 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6559 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6562 e78dc838 2020-12-04 stsp view->focus_child = 1;
6564 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6569 83cc4199 2022-06-13 stsp case CTRL('d'):
6571 83cc4199 2022-06-13 stsp nscroll /= 2;
6572 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6573 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6574 ea025d1d 2020-02-22 naddy case CTRL('f'):
6577 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6578 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6579 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6580 640cd7ff 2022-06-22 mark view->count = 0;
6583 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6584 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6585 83cc4199 2022-06-13 stsp s->selected_line +=
6586 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6587 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6589 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6590 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6592 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6593 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6595 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6596 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6597 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6598 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6602 640cd7ff 2022-06-22 mark view->count = 0;
6605 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6608 917d79a7 2022-07-01 stsp static const struct got_error *
6609 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6611 917d79a7 2022-07-01 stsp const struct got_error *err;
6612 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6614 917d79a7 2022-07-01 stsp view->count = 0;
6615 917d79a7 2022-07-01 stsp s->done = 1;
6616 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6617 917d79a7 2022-07-01 stsp s->done = 0;
6619 917d79a7 2022-07-01 stsp return err;
6620 917d79a7 2022-07-01 stsp return run_blame(view);
6623 a70480e0 2018-06-23 stsp static const struct got_error *
6624 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6626 a70480e0 2018-06-23 stsp const struct got_error *error;
6627 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6628 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6629 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6630 0587e10c 2020-07-23 stsp char *link_target = NULL;
6631 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6632 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6633 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6635 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6636 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6638 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6639 a70480e0 2018-06-23 stsp switch (ch) {
6641 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6644 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6645 69069811 2018-08-02 stsp if (repo_path == NULL)
6646 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6650 17020d27 2019-03-07 stsp usage_blame();
6651 a70480e0 2018-06-23 stsp /* NOTREACHED */
6655 a70480e0 2018-06-23 stsp argc -= optind;
6656 a70480e0 2018-06-23 stsp argv += optind;
6658 f135c941 2020-02-20 stsp if (argc != 1)
6659 a70480e0 2018-06-23 stsp usage_blame();
6661 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6662 0ae84acc 2022-06-15 tracey if (error != NULL)
6663 0ae84acc 2022-06-15 tracey goto done;
6665 69069811 2018-08-02 stsp if (repo_path == NULL) {
6666 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6667 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6668 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6669 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6670 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6671 c156c7a4 2020-12-18 stsp goto done;
6672 f135c941 2020-02-20 stsp if (worktree)
6673 eb41ed75 2019-02-05 stsp repo_path =
6674 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6676 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6677 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6678 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6679 c156c7a4 2020-12-18 stsp goto done;
6683 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6684 c02c541e 2019-03-29 stsp if (error != NULL)
6685 8e94dd5b 2019-01-04 stsp goto done;
6687 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6688 f135c941 2020-02-20 stsp worktree);
6689 c02c541e 2019-03-29 stsp if (error)
6690 92205607 2019-01-04 stsp goto done;
6692 f135c941 2020-02-20 stsp init_curses();
6694 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6695 51a10b52 2020-12-26 stsp if (error)
6696 51a10b52 2020-12-26 stsp goto done;
6698 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6699 eb41ed75 2019-02-05 stsp if (error)
6700 69069811 2018-08-02 stsp goto done;
6702 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6703 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6704 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6705 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6706 a70480e0 2018-06-23 stsp if (error != NULL)
6707 66b4983c 2018-06-23 stsp goto done;
6708 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6709 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6711 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6712 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6714 a19e88aa 2018-06-23 stsp if (error != NULL)
6715 8b473291 2019-02-21 stsp goto done;
6717 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6718 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6719 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6720 e1cd8fed 2018-08-01 stsp goto done;
6723 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6724 a44927cc 2022-04-07 stsp if (error)
6725 a44927cc 2022-04-07 stsp goto done;
6727 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6728 a44927cc 2022-04-07 stsp commit, repo);
6729 7cbe629d 2018-08-04 stsp if (error)
6730 7cbe629d 2018-08-04 stsp goto done;
6732 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6733 78756c87 2020-11-24 stsp commit_id, repo);
6734 0587e10c 2020-07-23 stsp if (error)
6735 0587e10c 2020-07-23 stsp goto done;
6736 12314ad4 2019-08-31 stsp if (worktree) {
6737 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6738 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6739 12314ad4 2019-08-31 stsp worktree = NULL;
6741 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6743 69069811 2018-08-02 stsp free(repo_path);
6744 f135c941 2020-02-20 stsp free(in_repo_path);
6745 0587e10c 2020-07-23 stsp free(link_target);
6746 69069811 2018-08-02 stsp free(cwd);
6747 a70480e0 2018-06-23 stsp free(commit_id);
6748 a44927cc 2022-04-07 stsp if (commit)
6749 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6750 eb41ed75 2019-02-05 stsp if (worktree)
6751 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6752 1d0f4054 2021-06-17 stsp if (repo) {
6753 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6754 1d0f4054 2021-06-17 stsp if (error == NULL)
6755 1d0f4054 2021-06-17 stsp error = close_err;
6757 0ae84acc 2022-06-15 tracey if (pack_fds) {
6758 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6759 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6760 0ae84acc 2022-06-15 tracey if (error == NULL)
6761 0ae84acc 2022-06-15 tracey error = pack_err;
6763 51a10b52 2020-12-26 stsp tog_free_refs();
6764 a70480e0 2018-06-23 stsp return error;
6767 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6768 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6770 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6771 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6772 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6773 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6774 0c6ad1bc 2022-09-09 mark char *index = NULL;
6775 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6776 d627976f 2023-02-01 mark int width, n, nentries, scrollx, i = 1;
6777 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6779 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6780 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6781 9b058f45 2022-06-30 mark --limit; /* border */
6783 f7d12f7e 2018-08-01 stsp werase(view->window);
6785 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6786 ffd1d5e5 2018-06-23 stsp return NULL;
6788 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6791 ffd1d5e5 2018-06-23 stsp return err;
6792 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6793 a3404814 2018-09-02 stsp wstandout(view->window);
6794 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6796 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6797 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6798 0c6ad1bc 2022-09-09 mark free(wline);
6799 0c6ad1bc 2022-09-09 mark wline = NULL;
6800 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6801 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6803 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6804 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6805 a3404814 2018-09-02 stsp wstandend(view->window);
6806 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6807 0c6ad1bc 2022-09-09 mark return NULL;
6809 df68a56b 2022-08-12 mark i += s->selected;
6810 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6811 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6812 df68a56b 2022-08-12 mark if (s->tree != s->root)
6813 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6815 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6816 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6817 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6818 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6819 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6820 0c6ad1bc 2022-09-09 mark free(index);
6822 ce52c690 2018-06-23 stsp return err;
6823 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6824 2550e4c3 2018-07-13 stsp free(wline);
6825 2550e4c3 2018-07-13 stsp wline = NULL;
6826 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6827 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6828 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6829 ffd1d5e5 2018-06-23 stsp return NULL;
6830 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6831 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6832 a1eca9bb 2018-06-23 stsp return NULL;
6834 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6835 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6836 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6837 0cf4efb1 2018-09-29 stsp if (view->focussed)
6838 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6839 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6841 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6842 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6843 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6844 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6845 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6846 ffd1d5e5 2018-06-23 stsp return NULL;
6850 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6853 d627976f 2023-02-01 mark view->maxx = 0;
6854 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6855 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6856 848d6979 2019-08-12 stsp const char *modestr = "";
6857 56e0773d 2019-11-28 stsp mode_t mode;
6859 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6860 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6862 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6863 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6864 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6866 638f9024 2019-05-13 stsp return got_error_from_errno(
6867 230a42bd 2019-05-11 jcs "got_object_id_str");
6869 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6870 63c5ca5d 2019-08-24 stsp modestr = "$";
6871 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6874 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6875 d86d3b18 2020-12-01 naddy te, s->repo);
6876 0d6c6ee3 2020-05-20 stsp if (err) {
6877 0d6c6ee3 2020-05-20 stsp free(id_str);
6878 0d6c6ee3 2020-05-20 stsp return err;
6880 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6881 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6882 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6884 848d6979 2019-08-12 stsp modestr = "@";
6886 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6887 848d6979 2019-08-12 stsp modestr = "/";
6888 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6889 848d6979 2019-08-12 stsp modestr = "*";
6890 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6891 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6892 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6893 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6894 1d13200f 2018-07-12 stsp free(id_str);
6895 0d6c6ee3 2020-05-20 stsp free(link_target);
6896 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6898 1d13200f 2018-07-12 stsp free(id_str);
6899 0d6c6ee3 2020-05-20 stsp free(link_target);
6901 d627976f 2023-02-01 mark /* use full line width to determine view->maxx */
6902 d627976f 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6903 ffd1d5e5 2018-06-23 stsp if (err) {
6904 ffd1d5e5 2018-06-23 stsp free(line);
6907 d627976f 2023-02-01 mark view->maxx = MAX(view->maxx, width);
6908 d627976f 2023-02-01 mark free(wline);
6909 d627976f 2023-02-01 mark wline = NULL;
6911 d627976f 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
6912 d627976f 2023-02-01 mark view->ncols, 0, 0);
6913 d627976f 2023-02-01 mark if (err) {
6914 d627976f 2023-02-01 mark free(line);
6917 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6918 0cf4efb1 2018-09-29 stsp if (view->focussed)
6919 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6920 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6922 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6924 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6925 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6926 d627976f 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
6928 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6929 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6930 b637f33f 2023-02-01 mark if (width < view->ncols)
6931 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6932 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6933 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6934 ffd1d5e5 2018-06-23 stsp free(line);
6935 2550e4c3 2018-07-13 stsp free(wline);
6936 2550e4c3 2018-07-13 stsp wline = NULL;
6938 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6939 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6940 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6944 ffd1d5e5 2018-06-23 stsp return err;
6947 ffd1d5e5 2018-06-23 stsp static void
6948 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6950 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6951 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6952 fa86c4bf 2020-11-29 stsp int i = 0;
6954 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6957 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6958 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6959 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6960 fa86c4bf 2020-11-29 stsp if (!isroot)
6961 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6964 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6965 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6969 9b058f45 2022-06-30 mark static const struct got_error *
6970 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6972 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6973 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6974 ffd1d5e5 2018-06-23 stsp int n = 0;
6976 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6977 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6978 694d3271 2020-12-01 naddy s->first_displayed_entry);
6980 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6982 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6983 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6984 94b80cfa 2022-08-01 mark if (last) {
6985 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6986 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6988 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6989 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6990 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6994 9b058f45 2022-06-30 mark return NULL;
6997 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6998 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6999 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7001 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7002 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7003 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7005 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7006 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7007 56e0773d 2019-11-28 stsp + 1 /* slash */;
7009 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7011 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7012 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7013 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7015 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7016 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7017 d9765a41 2018-06-23 stsp while (pt) {
7018 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7019 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7020 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7021 cb2ebc8a 2018-06-23 stsp goto done;
7023 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7024 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7025 cb2ebc8a 2018-06-23 stsp goto done;
7027 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7030 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7031 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7032 ce52c690 2018-06-23 stsp goto done;
7036 ce52c690 2018-06-23 stsp if (err) {
7037 ce52c690 2018-06-23 stsp free(*path);
7038 ce52c690 2018-06-23 stsp *path = NULL;
7040 ce52c690 2018-06-23 stsp return err;
7043 ce52c690 2018-06-23 stsp static const struct got_error *
7044 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7045 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7046 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7048 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7049 ce52c690 2018-06-23 stsp char *path;
7050 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7052 a0de39f3 2019-08-09 stsp *new_view = NULL;
7054 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7056 ce52c690 2018-06-23 stsp return err;
7058 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7059 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7060 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7061 83ce39e3 2019-08-12 stsp goto done;
7064 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7065 e5a0f69f 2018-08-18 stsp if (err) {
7066 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7067 fc06ba56 2019-08-22 stsp err = NULL;
7068 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7070 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7072 83ce39e3 2019-08-12 stsp free(path);
7073 69efd4c4 2018-07-18 stsp return err;
7076 69efd4c4 2018-07-18 stsp static const struct got_error *
7077 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7078 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7080 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7081 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7082 69efd4c4 2018-07-18 stsp char *path;
7084 a0de39f3 2019-08-09 stsp *new_view = NULL;
7086 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7087 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7088 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7090 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7092 69efd4c4 2018-07-18 stsp return err;
7094 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7097 e5a0f69f 2018-08-18 stsp view_close(log_view);
7099 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7100 cb2ebc8a 2018-06-23 stsp free(path);
7101 cb2ebc8a 2018-06-23 stsp return err;
7104 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7105 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7106 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7108 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7109 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7110 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7111 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7113 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7114 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7116 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7117 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7118 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7120 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7122 bc573f3b 2021-07-10 stsp goto done;
7125 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7126 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7127 bc573f3b 2021-07-10 stsp * closed on demand.
7129 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7130 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7132 bc573f3b 2021-07-10 stsp goto done;
7133 bc573f3b 2021-07-10 stsp s->tree = s->root;
7135 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7136 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7137 ffd1d5e5 2018-06-23 stsp goto done;
7139 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7140 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7141 ffd1d5e5 2018-06-23 stsp goto done;
7144 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7145 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7146 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7147 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7148 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7149 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7150 9cd7cbd1 2020-12-07 stsp goto done;
7153 fb2756b9 2018-08-04 stsp s->repo = repo;
7155 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7156 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7157 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7158 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7160 c0b01bdb 2019-11-08 stsp goto done;
7161 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7162 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7164 c0b01bdb 2019-11-08 stsp goto done;
7165 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7166 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7167 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7169 c0b01bdb 2019-11-08 stsp goto done;
7171 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7172 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7173 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7175 11b20872 2019-11-08 stsp goto done;
7177 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7178 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7180 c0b01bdb 2019-11-08 stsp goto done;
7183 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7184 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7185 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7186 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7187 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7189 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7190 bc573f3b 2021-07-10 stsp if (commit)
7191 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7193 bc573f3b 2021-07-10 stsp close_tree_view(view);
7194 ad80ab7b 2018-08-04 stsp return err;
7197 e5a0f69f 2018-08-18 stsp static const struct got_error *
7198 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7200 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7202 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7203 fb2756b9 2018-08-04 stsp free(s->tree_label);
7204 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7205 6484ec90 2018-09-29 stsp free(s->commit_id);
7206 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7207 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7208 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7209 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7210 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7211 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7212 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7213 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7214 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7215 ad80ab7b 2018-08-04 stsp free(parent);
7218 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7219 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7220 bc573f3b 2021-07-10 stsp if (s->root)
7221 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7222 7c32bd05 2019-06-22 stsp return NULL;
7225 7c32bd05 2019-06-22 stsp static const struct got_error *
7226 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7228 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7230 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7231 7c32bd05 2019-06-22 stsp return NULL;
7234 7c32bd05 2019-06-22 stsp static int
7235 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7237 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7239 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7243 7c32bd05 2019-06-22 stsp static const struct got_error *
7244 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7246 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7247 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7249 7c32bd05 2019-06-22 stsp if (!view->searching) {
7250 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7251 7c32bd05 2019-06-22 stsp return NULL;
7254 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7255 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7256 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7257 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7258 56e0773d 2019-11-28 stsp s->selected_entry);
7260 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7262 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7263 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7265 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7266 56e0773d 2019-11-28 stsp s->selected_entry);
7269 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7270 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7271 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7272 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7274 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7277 7c32bd05 2019-06-22 stsp while (1) {
7278 56e0773d 2019-11-28 stsp if (te == NULL) {
7279 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7280 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7281 ac66afb8 2019-06-24 stsp return NULL;
7283 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7284 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7286 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7289 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7290 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7291 56e0773d 2019-11-28 stsp s->matched_entry = te;
7295 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7296 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7298 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7301 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7302 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7303 7c32bd05 2019-06-22 stsp s->selected = 0;
7306 e5a0f69f 2018-08-18 stsp return NULL;
7309 ad80ab7b 2018-08-04 stsp static const struct got_error *
7310 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7312 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7313 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7314 e5a0f69f 2018-08-18 stsp char *parent_path;
7316 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7318 e5a0f69f 2018-08-18 stsp return err;
7320 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7321 e5a0f69f 2018-08-18 stsp free(parent_path);
7323 9b058f45 2022-06-30 mark view_border(view);
7324 e5a0f69f 2018-08-18 stsp return err;
7327 94b80cfa 2022-08-01 mark static const struct got_error *
7328 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7330 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7331 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7332 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7333 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7334 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7335 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7337 94b80cfa 2022-08-01 mark g = view->gline;
7338 94b80cfa 2022-08-01 mark view->gline = 0;
7340 94b80cfa 2022-08-01 mark if (g == 0)
7342 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7343 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7345 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7346 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7347 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7349 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7350 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7351 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7353 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7354 94b80cfa 2022-08-01 mark last += off;
7356 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7357 94b80cfa 2022-08-01 mark s->selected = g - first;
7358 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7361 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7362 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7366 94b80cfa 2022-08-01 mark if (i < g) {
7367 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7369 94b80cfa 2022-08-01 mark return err;
7370 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7371 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7372 94b80cfa 2022-08-01 mark first + s->selected < g &&
7373 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7374 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7375 94b80cfa 2022-08-01 mark first += off;
7376 94b80cfa 2022-08-01 mark s->selected = g - first;
7378 94b80cfa 2022-08-01 mark } else if (i > g)
7379 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7381 94b80cfa 2022-08-01 mark if (g < nlines &&
7382 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7383 94b80cfa 2022-08-01 mark s->selected = g - 1;
7385 94b80cfa 2022-08-01 mark return NULL;
7388 e5a0f69f 2018-08-18 stsp static const struct got_error *
7389 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7391 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7392 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7393 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7394 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7396 94b80cfa 2022-08-01 mark if (view->gline)
7397 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7399 e5a0f69f 2018-08-18 stsp switch (ch) {
7402 d627976f 2023-02-01 mark case KEY_RIGHT:
7404 d627976f 2023-02-01 mark case KEY_LEFT:
7406 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
7409 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7410 640cd7ff 2022-06-22 mark view->count = 0;
7413 640cd7ff 2022-06-22 mark view->count = 0;
7414 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7416 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7419 640cd7ff 2022-06-22 mark view->count = 0;
7420 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7422 e4526bf5 2021-09-03 naddy case 'g':
7424 e4526bf5 2021-09-03 naddy case KEY_HOME:
7425 e4526bf5 2021-09-03 naddy s->selected = 0;
7426 640cd7ff 2022-06-22 mark view->count = 0;
7427 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7428 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7429 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7431 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7433 e4526bf5 2021-09-03 naddy case 'G':
7435 9b058f45 2022-06-30 mark case KEY_END: {
7436 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7438 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7439 9b058f45 2022-06-30 mark --eos; /* border */
7440 e4526bf5 2021-09-03 naddy s->selected = 0;
7441 640cd7ff 2022-06-22 mark view->count = 0;
7442 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7443 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7444 e4526bf5 2021-09-03 naddy if (te == NULL) {
7445 ad055527 2022-07-16 mark if (s->tree != s->root) {
7446 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7451 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7452 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7454 e4526bf5 2021-09-03 naddy if (n > 0)
7455 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7459 1e37a5c2 2019-05-12 jcs case KEY_UP:
7460 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7461 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7462 1e37a5c2 2019-05-12 jcs s->selected--;
7465 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7466 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7467 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7468 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7469 640cd7ff 2022-06-22 mark view->count = 0;
7471 83cc4199 2022-06-13 stsp case CTRL('u'):
7473 83cc4199 2022-06-13 stsp nscroll /= 2;
7474 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7475 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7476 ea025d1d 2020-02-22 naddy case CTRL('b'):
7478 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7479 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7480 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7481 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7483 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7484 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7486 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7487 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7488 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7489 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7490 640cd7ff 2022-06-22 mark view->count = 0;
7493 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7494 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7495 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7496 1e37a5c2 2019-05-12 jcs s->selected++;
7499 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7500 640cd7ff 2022-06-22 mark == NULL) {
7501 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7502 640cd7ff 2022-06-22 mark view->count = 0;
7505 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7507 83cc4199 2022-06-13 stsp case CTRL('d'):
7509 83cc4199 2022-06-13 stsp nscroll /= 2;
7510 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7511 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7512 ea025d1d 2020-02-22 naddy case CTRL('f'):
7514 48bb96f0 2022-06-20 naddy case ' ':
7515 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7517 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7518 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7519 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7520 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7522 640cd7ff 2022-06-22 mark view->count = 0;
7525 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7527 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7529 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7530 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7531 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7532 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7533 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7534 640cd7ff 2022-06-22 mark view->count = 0;
7537 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7538 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7540 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7541 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7542 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7543 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7544 1e37a5c2 2019-05-12 jcs s->selected_entry =
7545 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7546 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7547 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7548 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7552 1e37a5c2 2019-05-12 jcs free(parent);
7553 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7554 56e0773d 2019-11-28 stsp s->selected_entry))) {
7555 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7556 640cd7ff 2022-06-22 mark view->count = 0;
7557 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7558 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7561 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7562 941e9f74 2019-05-21 stsp if (err) {
7563 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7566 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7567 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7569 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7570 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7571 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7572 640cd7ff 2022-06-22 mark view->count = 0;
7575 640cd7ff 2022-06-22 mark view->count = 0;
7579 ffd1d5e5 2018-06-23 stsp return err;
7582 ffd1d5e5 2018-06-23 stsp __dead static void
7583 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7586 87411fa9 2022-06-16 stsp fprintf(stderr,
7587 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7588 ffd1d5e5 2018-06-23 stsp getprogname());
7592 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7593 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7595 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7596 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7597 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7598 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7599 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7600 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7601 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7602 4e97c21c 2020-12-06 stsp char *label = NULL;
7603 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7604 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7606 5221c383 2018-08-01 stsp struct tog_view *view;
7607 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7609 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7610 ffd1d5e5 2018-06-23 stsp switch (ch) {
7612 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7615 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7616 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7617 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7621 e99e2d15 2020-11-24 naddy usage_tree();
7622 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7626 ffd1d5e5 2018-06-23 stsp argc -= optind;
7627 ffd1d5e5 2018-06-23 stsp argv += optind;
7629 55cccc34 2020-02-20 stsp if (argc > 1)
7630 e99e2d15 2020-11-24 naddy usage_tree();
7632 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7633 0ae84acc 2022-06-15 tracey if (error != NULL)
7634 0ae84acc 2022-06-15 tracey goto done;
7636 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7637 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7638 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7639 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7640 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7641 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7642 c156c7a4 2020-12-18 stsp goto done;
7643 55cccc34 2020-02-20 stsp if (worktree)
7644 52185f70 2019-02-05 stsp repo_path =
7645 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7647 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7648 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7649 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7650 c156c7a4 2020-12-18 stsp goto done;
7654 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7655 c02c541e 2019-03-29 stsp if (error != NULL)
7656 52185f70 2019-02-05 stsp goto done;
7658 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7659 55cccc34 2020-02-20 stsp repo, worktree);
7660 55cccc34 2020-02-20 stsp if (error)
7661 55cccc34 2020-02-20 stsp goto done;
7663 55cccc34 2020-02-20 stsp init_curses();
7665 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7666 c02c541e 2019-03-29 stsp if (error)
7667 52185f70 2019-02-05 stsp goto done;
7669 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7670 51a10b52 2020-12-26 stsp if (error)
7671 51a10b52 2020-12-26 stsp goto done;
7673 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7674 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7675 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7676 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7677 4e97c21c 2020-12-06 stsp if (error)
7678 4e97c21c 2020-12-06 stsp goto done;
7679 4e97c21c 2020-12-06 stsp head_ref_name = label;
7681 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7682 4e97c21c 2020-12-06 stsp if (error == NULL)
7683 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7684 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7685 4e97c21c 2020-12-06 stsp goto done;
7686 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7687 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7688 4e97c21c 2020-12-06 stsp if (error)
7689 4e97c21c 2020-12-06 stsp goto done;
7692 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7693 a44927cc 2022-04-07 stsp if (error)
7694 a44927cc 2022-04-07 stsp goto done;
7696 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7697 5221c383 2018-08-01 stsp if (view == NULL) {
7698 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7699 5221c383 2018-08-01 stsp goto done;
7701 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7702 ad80ab7b 2018-08-04 stsp if (error)
7703 ad80ab7b 2018-08-04 stsp goto done;
7704 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7705 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7706 d91faf3b 2020-12-01 naddy in_repo_path);
7707 55cccc34 2020-02-20 stsp if (error)
7708 55cccc34 2020-02-20 stsp goto done;
7711 55cccc34 2020-02-20 stsp if (worktree) {
7712 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7713 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7714 55cccc34 2020-02-20 stsp worktree = NULL;
7716 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7718 52185f70 2019-02-05 stsp free(repo_path);
7719 e4a0e26d 2020-02-20 stsp free(cwd);
7720 ffd1d5e5 2018-06-23 stsp free(commit_id);
7721 4e97c21c 2020-12-06 stsp free(label);
7723 486cd271 2020-12-06 stsp got_ref_close(ref);
7724 1d0f4054 2021-06-17 stsp if (repo) {
7725 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7726 1d0f4054 2021-06-17 stsp if (error == NULL)
7727 1d0f4054 2021-06-17 stsp error = close_err;
7729 0ae84acc 2022-06-15 tracey if (pack_fds) {
7730 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7731 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7732 0ae84acc 2022-06-15 tracey if (error == NULL)
7733 0ae84acc 2022-06-15 tracey error = pack_err;
7735 51a10b52 2020-12-26 stsp tog_free_refs();
7736 ffd1d5e5 2018-06-23 stsp return error;
7739 6458efa5 2020-11-24 stsp static const struct got_error *
7740 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7742 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7743 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7745 6458efa5 2020-11-24 stsp s->nrefs = 0;
7746 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7747 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7748 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7749 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7750 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7753 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7754 6458efa5 2020-11-24 stsp if (re == NULL)
7755 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7757 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7758 8924d611 2020-12-26 stsp if (re->ref == NULL)
7759 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7760 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7761 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7764 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7765 6458efa5 2020-11-24 stsp return NULL;
7769 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7771 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7773 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7774 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7775 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7776 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7781 6458efa5 2020-11-24 stsp static const struct got_error *
7782 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7784 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7785 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7787 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7788 6458efa5 2020-11-24 stsp s->repo = repo;
7790 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7791 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7793 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7795 6458efa5 2020-11-24 stsp return err;
7797 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7798 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7799 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7800 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7802 6458efa5 2020-11-24 stsp goto done;
7804 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7805 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7806 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7808 6458efa5 2020-11-24 stsp goto done;
7810 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7811 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7812 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7814 cc488aa7 2022-01-23 stsp goto done;
7816 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7817 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7818 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7820 6458efa5 2020-11-24 stsp goto done;
7823 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7824 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7825 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7826 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7827 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7830 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7831 6458efa5 2020-11-24 stsp return err;
7834 6458efa5 2020-11-24 stsp static const struct got_error *
7835 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7837 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7839 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7840 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7842 6458efa5 2020-11-24 stsp return NULL;
7845 6458efa5 2020-11-24 stsp static const struct got_error *
7846 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7847 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7849 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7850 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7851 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7852 6458efa5 2020-11-24 stsp int obj_type;
7854 c42c9805 2020-11-24 stsp *commit_id = NULL;
7856 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7858 6458efa5 2020-11-24 stsp return err;
7860 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7862 6458efa5 2020-11-24 stsp goto done;
7864 6458efa5 2020-11-24 stsp switch (obj_type) {
7865 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7866 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7868 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7869 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7871 6458efa5 2020-11-24 stsp goto done;
7872 c42c9805 2020-11-24 stsp free(obj_id);
7873 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7874 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7876 6458efa5 2020-11-24 stsp goto done;
7877 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7878 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7879 c42c9805 2020-11-24 stsp goto done;
7881 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7882 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7883 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7884 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7885 c42c9805 2020-11-24 stsp goto done;
7889 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7895 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7896 c42c9805 2020-11-24 stsp if (err) {
7897 c42c9805 2020-11-24 stsp free(*commit_id);
7898 c42c9805 2020-11-24 stsp *commit_id = NULL;
7900 c42c9805 2020-11-24 stsp return err;
7903 c42c9805 2020-11-24 stsp static const struct got_error *
7904 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7905 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7907 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7908 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7909 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7911 c42c9805 2020-11-24 stsp *new_view = NULL;
7913 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7914 c42c9805 2020-11-24 stsp if (err) {
7915 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7916 c42c9805 2020-11-24 stsp return err;
7918 c42c9805 2020-11-24 stsp return NULL;
7921 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7922 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7923 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7924 6458efa5 2020-11-24 stsp goto done;
7927 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7928 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7931 6458efa5 2020-11-24 stsp view_close(log_view);
7933 6458efa5 2020-11-24 stsp *new_view = log_view;
7934 c42c9805 2020-11-24 stsp free(commit_id);
7935 6458efa5 2020-11-24 stsp return err;
7938 ce5b7c56 2019-07-09 stsp static void
7939 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7941 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7942 34ba6917 2020-11-30 stsp int i = 0;
7944 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7947 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7948 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7949 34ba6917 2020-11-30 stsp if (re == NULL)
7951 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7952 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7956 9b058f45 2022-06-30 mark static const struct got_error *
7957 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7959 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7960 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7961 6458efa5 2020-11-24 stsp int n = 0;
7963 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7964 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7966 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7968 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7969 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7970 94b80cfa 2022-08-01 mark if (last) {
7971 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7972 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7974 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7975 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7976 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7980 9b058f45 2022-06-30 mark return NULL;
7983 6458efa5 2020-11-24 stsp static const struct got_error *
7984 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7986 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7988 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7989 6458efa5 2020-11-24 stsp return NULL;
7992 6458efa5 2020-11-24 stsp static int
7993 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7995 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7997 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8001 6458efa5 2020-11-24 stsp static const struct got_error *
8002 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8004 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8005 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8007 6458efa5 2020-11-24 stsp if (!view->searching) {
8008 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8009 6458efa5 2020-11-24 stsp return NULL;
8012 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8013 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8014 6458efa5 2020-11-24 stsp if (s->selected_entry)
8015 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8017 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8018 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8020 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8021 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8023 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8024 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8027 487cd7d2 2021-12-17 stsp if (s->selected_entry)
8028 487cd7d2 2021-12-17 stsp re = s->selected_entry;
8029 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
8030 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8032 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8035 6458efa5 2020-11-24 stsp while (1) {
8036 6458efa5 2020-11-24 stsp if (re == NULL) {
8037 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8038 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8039 6458efa5 2020-11-24 stsp return NULL;
8041 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8042 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8044 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8047 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8048 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8049 6458efa5 2020-11-24 stsp s->matched_entry = re;
8053 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8054 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8056 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8059 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8060 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8061 6458efa5 2020-11-24 stsp s->selected = 0;
8064 6458efa5 2020-11-24 stsp return NULL;
8067 6458efa5 2020-11-24 stsp static const struct got_error *
8068 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8070 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8071 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8072 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8073 6458efa5 2020-11-24 stsp char *line = NULL;
8074 6458efa5 2020-11-24 stsp wchar_t *wline;
8075 6458efa5 2020-11-24 stsp struct tog_color *tc;
8076 0ed6bc90 2023-02-01 mark int width, n, scrollx;
8077 6458efa5 2020-11-24 stsp int limit = view->nlines;
8079 6458efa5 2020-11-24 stsp werase(view->window);
8081 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8082 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8083 9b058f45 2022-06-30 mark --limit; /* border */
8085 6458efa5 2020-11-24 stsp if (limit == 0)
8086 6458efa5 2020-11-24 stsp return NULL;
8088 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8090 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8091 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8092 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8094 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8095 6458efa5 2020-11-24 stsp if (err) {
8096 6458efa5 2020-11-24 stsp free(line);
8097 6458efa5 2020-11-24 stsp return err;
8099 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8100 6458efa5 2020-11-24 stsp wstandout(view->window);
8101 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8102 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8103 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8104 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8105 6458efa5 2020-11-24 stsp wstandend(view->window);
8106 6458efa5 2020-11-24 stsp free(wline);
8107 6458efa5 2020-11-24 stsp wline = NULL;
8108 6458efa5 2020-11-24 stsp free(line);
8109 6458efa5 2020-11-24 stsp line = NULL;
8110 6458efa5 2020-11-24 stsp if (--limit <= 0)
8111 6458efa5 2020-11-24 stsp return NULL;
8114 0ed6bc90 2023-02-01 mark view->maxx = 0;
8115 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8116 6458efa5 2020-11-24 stsp char *line = NULL;
8117 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8119 b4996bee 2022-06-16 stsp if (s->show_date) {
8120 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8121 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8122 b4996bee 2022-06-16 stsp struct got_object_id *id;
8123 b4996bee 2022-06-16 stsp struct tm tm;
8126 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8128 b4996bee 2022-06-16 stsp return err;
8129 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8130 b4996bee 2022-06-16 stsp if (err) {
8131 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8133 b4996bee 2022-06-16 stsp return err;
8135 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8137 b4996bee 2022-06-16 stsp if (err) {
8139 b4996bee 2022-06-16 stsp return err;
8141 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8142 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8144 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8145 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8148 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8149 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8150 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8151 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8153 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8154 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8155 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8156 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8157 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8158 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8159 6458efa5 2020-11-24 stsp struct got_object_id *id;
8160 6458efa5 2020-11-24 stsp char *id_str;
8161 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8163 6458efa5 2020-11-24 stsp return err;
8164 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8165 6458efa5 2020-11-24 stsp if (err) {
8167 6458efa5 2020-11-24 stsp return err;
8169 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8170 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8171 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8173 6458efa5 2020-11-24 stsp free(id_str);
8174 6458efa5 2020-11-24 stsp return err;
8177 6458efa5 2020-11-24 stsp free(id_str);
8178 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8179 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8180 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8182 0ed6bc90 2023-02-01 mark /* use full line width to determine view->maxx */
8183 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8184 6458efa5 2020-11-24 stsp if (err) {
8185 6458efa5 2020-11-24 stsp free(line);
8186 6458efa5 2020-11-24 stsp return err;
8188 0ed6bc90 2023-02-01 mark view->maxx = MAX(view->maxx, width);
8189 0ed6bc90 2023-02-01 mark free(wline);
8190 0ed6bc90 2023-02-01 mark wline = NULL;
8192 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
8193 0ed6bc90 2023-02-01 mark view->ncols, 0, 0);
8194 0ed6bc90 2023-02-01 mark if (err) {
8195 0ed6bc90 2023-02-01 mark free(line);
8196 0ed6bc90 2023-02-01 mark return err;
8198 6458efa5 2020-11-24 stsp if (n == s->selected) {
8199 6458efa5 2020-11-24 stsp if (view->focussed)
8200 6458efa5 2020-11-24 stsp wstandout(view->window);
8201 6458efa5 2020-11-24 stsp s->selected_entry = re;
8203 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8205 6458efa5 2020-11-24 stsp wattr_on(view->window,
8206 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8207 0ed6bc90 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
8209 6458efa5 2020-11-24 stsp wattr_off(view->window,
8210 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8211 b637f33f 2023-02-01 mark if (width < view->ncols)
8212 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8213 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8214 6458efa5 2020-11-24 stsp wstandend(view->window);
8215 6458efa5 2020-11-24 stsp free(line);
8216 6458efa5 2020-11-24 stsp free(wline);
8217 6458efa5 2020-11-24 stsp wline = NULL;
8219 6458efa5 2020-11-24 stsp s->ndisplayed++;
8220 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8223 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8226 9b058f45 2022-06-30 mark view_border(view);
8227 6458efa5 2020-11-24 stsp return err;
8230 6458efa5 2020-11-24 stsp static const struct got_error *
8231 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8232 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8234 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8235 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8236 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8238 c42c9805 2020-11-24 stsp *new_view = NULL;
8240 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8241 c42c9805 2020-11-24 stsp if (err) {
8242 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8243 c42c9805 2020-11-24 stsp return err;
8245 c42c9805 2020-11-24 stsp return NULL;
8249 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8250 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8251 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8252 c42c9805 2020-11-24 stsp goto done;
8255 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8256 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8258 c42c9805 2020-11-24 stsp goto done;
8260 c42c9805 2020-11-24 stsp *new_view = tree_view;
8262 c42c9805 2020-11-24 stsp free(commit_id);
8263 c42c9805 2020-11-24 stsp return err;
8266 94b80cfa 2022-08-01 mark static const struct got_error *
8267 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8269 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8270 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8271 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8273 94b80cfa 2022-08-01 mark g = view->gline;
8274 94b80cfa 2022-08-01 mark view->gline = 0;
8276 94b80cfa 2022-08-01 mark if (g == 0)
8278 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8279 94b80cfa 2022-08-01 mark g = s->nrefs;
8281 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8282 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8283 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8284 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8285 94b80cfa 2022-08-01 mark return NULL;
8288 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8289 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8291 94b80cfa 2022-08-01 mark return err;
8292 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8293 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8294 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8295 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8296 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8297 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8299 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8300 94b80cfa 2022-08-01 mark s->selected = g - 1;
8302 94b80cfa 2022-08-01 mark return NULL;
8306 c42c9805 2020-11-24 stsp static const struct got_error *
8307 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8309 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8310 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8311 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8312 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8314 94b80cfa 2022-08-01 mark if (view->gline)
8315 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8317 6458efa5 2020-11-24 stsp switch (ch) {
8320 0ed6bc90 2023-02-01 mark case KEY_RIGHT:
8322 0ed6bc90 2023-02-01 mark case KEY_LEFT:
8324 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8327 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8328 640cd7ff 2022-06-22 mark view->count = 0;
8331 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8332 640cd7ff 2022-06-22 mark view->count = 0;
8335 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8336 3f6c6614 2023-01-23 mark view->action = s->sort_by_date ? "sort by date" : "sort by name";
8337 640cd7ff 2022-06-22 mark view->count = 0;
8338 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8339 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8340 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8343 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8344 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8345 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8348 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8349 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8351 6458efa5 2020-11-24 stsp case KEY_ENTER:
8352 6458efa5 2020-11-24 stsp case '\r':
8353 640cd7ff 2022-06-22 mark view->count = 0;
8354 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8356 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8359 640cd7ff 2022-06-22 mark view->count = 0;
8360 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8362 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8364 e4526bf5 2021-09-03 naddy case 'g':
8366 e4526bf5 2021-09-03 naddy case KEY_HOME:
8367 e4526bf5 2021-09-03 naddy s->selected = 0;
8368 640cd7ff 2022-06-22 mark view->count = 0;
8369 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8371 e4526bf5 2021-09-03 naddy case 'G':
8373 9b058f45 2022-06-30 mark case KEY_END: {
8374 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8376 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8377 9b058f45 2022-06-30 mark --eos; /* border */
8378 e4526bf5 2021-09-03 naddy s->selected = 0;
8379 640cd7ff 2022-06-22 mark view->count = 0;
8380 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8381 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8382 e4526bf5 2021-09-03 naddy if (re == NULL)
8384 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8385 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8387 e4526bf5 2021-09-03 naddy if (n > 0)
8388 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8392 6458efa5 2020-11-24 stsp case KEY_UP:
8393 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8394 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8395 6458efa5 2020-11-24 stsp s->selected--;
8398 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8399 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8400 640cd7ff 2022-06-22 mark view->count = 0;
8402 83cc4199 2022-06-13 stsp case CTRL('u'):
8404 83cc4199 2022-06-13 stsp nscroll /= 2;
8405 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8406 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8407 6458efa5 2020-11-24 stsp case CTRL('b'):
8409 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8410 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8411 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8412 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8413 640cd7ff 2022-06-22 mark view->count = 0;
8416 6458efa5 2020-11-24 stsp case KEY_DOWN:
8417 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8418 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8419 6458efa5 2020-11-24 stsp s->selected++;
8422 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8423 6458efa5 2020-11-24 stsp /* can't scroll any further */
8424 640cd7ff 2022-06-22 mark view->count = 0;
8427 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8429 83cc4199 2022-06-13 stsp case CTRL('d'):
8431 83cc4199 2022-06-13 stsp nscroll /= 2;
8432 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8433 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8434 6458efa5 2020-11-24 stsp case CTRL('f'):
8436 48bb96f0 2022-06-20 naddy case ' ':
8437 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8438 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8439 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8440 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8441 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8442 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8443 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8444 640cd7ff 2022-06-22 mark view->count = 0;
8447 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8449 6458efa5 2020-11-24 stsp case CTRL('l'):
8450 640cd7ff 2022-06-22 mark view->count = 0;
8451 8924d611 2020-12-26 stsp tog_free_refs();
8452 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8455 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8456 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8458 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8459 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8460 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8463 640cd7ff 2022-06-22 mark view->count = 0;
8467 6458efa5 2020-11-24 stsp return err;
8470 6458efa5 2020-11-24 stsp __dead static void
8471 6458efa5 2020-11-24 stsp usage_ref(void)
8474 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8475 6458efa5 2020-11-24 stsp getprogname());
8479 6458efa5 2020-11-24 stsp static const struct got_error *
8480 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8482 6458efa5 2020-11-24 stsp const struct got_error *error;
8483 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8484 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8485 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8487 6458efa5 2020-11-24 stsp struct tog_view *view;
8488 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8490 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8491 6458efa5 2020-11-24 stsp switch (ch) {
8493 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8494 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8495 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8499 e99e2d15 2020-11-24 naddy usage_ref();
8500 6458efa5 2020-11-24 stsp /* NOTREACHED */
8504 6458efa5 2020-11-24 stsp argc -= optind;
8505 6458efa5 2020-11-24 stsp argv += optind;
8507 6458efa5 2020-11-24 stsp if (argc > 1)
8508 e99e2d15 2020-11-24 naddy usage_ref();
8510 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8511 0ae84acc 2022-06-15 tracey if (error != NULL)
8512 0ae84acc 2022-06-15 tracey goto done;
8514 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8515 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8516 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8517 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8518 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8519 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8520 c156c7a4 2020-12-18 stsp goto done;
8521 6458efa5 2020-11-24 stsp if (worktree)
8522 6458efa5 2020-11-24 stsp repo_path =
8523 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8525 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8526 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8527 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8528 c156c7a4 2020-12-18 stsp goto done;
8532 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8533 6458efa5 2020-11-24 stsp if (error != NULL)
8534 6458efa5 2020-11-24 stsp goto done;
8536 6458efa5 2020-11-24 stsp init_curses();
8538 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8539 51a10b52 2020-12-26 stsp if (error)
8540 51a10b52 2020-12-26 stsp goto done;
8542 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8543 6458efa5 2020-11-24 stsp if (error)
8544 6458efa5 2020-11-24 stsp goto done;
8546 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8547 6458efa5 2020-11-24 stsp if (view == NULL) {
8548 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8549 6458efa5 2020-11-24 stsp goto done;
8552 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8553 6458efa5 2020-11-24 stsp if (error)
8554 6458efa5 2020-11-24 stsp goto done;
8556 6458efa5 2020-11-24 stsp if (worktree) {
8557 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8558 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8559 6458efa5 2020-11-24 stsp worktree = NULL;
8561 6458efa5 2020-11-24 stsp error = view_loop(view);
8563 6458efa5 2020-11-24 stsp free(repo_path);
8564 6458efa5 2020-11-24 stsp free(cwd);
8565 1d0f4054 2021-06-17 stsp if (repo) {
8566 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8567 1d0f4054 2021-06-17 stsp if (close_err)
8568 1d0f4054 2021-06-17 stsp error = close_err;
8570 0ae84acc 2022-06-15 tracey if (pack_fds) {
8571 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8572 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8573 0ae84acc 2022-06-15 tracey if (error == NULL)
8574 0ae84acc 2022-06-15 tracey error = pack_err;
8576 51a10b52 2020-12-26 stsp tog_free_refs();
8577 6458efa5 2020-11-24 stsp return error;
8580 ec2a9698 2022-09-15 mark static const struct got_error*
8581 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8582 ec2a9698 2022-09-15 mark const char *str)
8584 ec2a9698 2022-09-15 mark size_t len;
8586 ec2a9698 2022-09-15 mark if (win == NULL)
8587 ec2a9698 2022-09-15 mark win = stdscr;
8589 ec2a9698 2022-09-15 mark len = strlen(str);
8590 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8592 ec2a9698 2022-09-15 mark if (focus)
8593 ec2a9698 2022-09-15 mark wstandout(win);
8594 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8595 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8596 ec2a9698 2022-09-15 mark if (focus)
8597 ec2a9698 2022-09-15 mark wstandend(win);
8599 ec2a9698 2022-09-15 mark return NULL;
8602 136e2bd4 2022-07-23 mark static const struct got_error *
8603 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8607 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8608 ec2a9698 2022-09-15 mark if (p == NULL) {
8609 ec2a9698 2022-09-15 mark free(*line_offsets);
8610 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8611 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8614 ec2a9698 2022-09-15 mark *line_offsets = p;
8615 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8616 ec2a9698 2022-09-15 mark ++(*nlines);
8617 ec2a9698 2022-09-15 mark return NULL;
8620 ec2a9698 2022-09-15 mark static const struct got_error *
8621 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8625 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8626 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8627 ec2a9698 2022-09-15 mark size_t len = 1;
8629 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8632 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8633 ec2a9698 2022-09-15 mark if (t0 == NULL)
8634 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8636 ec2a9698 2022-09-15 mark len += strlen(t);
8637 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8638 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8640 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8643 ec2a9698 2022-09-15 mark return NULL;
8647 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8648 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8649 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8651 ec2a9698 2022-09-15 mark static const struct got_error *
8652 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8654 ec2a9698 2022-09-15 mark int n, len = width;
8656 ec2a9698 2022-09-15 mark if (km->keys) {
8657 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8658 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8659 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8661 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8662 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8664 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8666 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8667 ec2a9698 2022-09-15 mark if (t0 == NULL)
8668 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8670 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8671 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8672 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8673 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8674 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8675 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8676 ec2a9698 2022-09-15 mark if (n < 0) {
8678 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8680 ec2a9698 2022-09-15 mark first = 0;
8681 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8682 ec2a9698 2022-09-15 mark *off += n;
8686 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8687 ec2a9698 2022-09-15 mark if (n < 0)
8688 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8689 ec2a9698 2022-09-15 mark *off += n;
8691 ec2a9698 2022-09-15 mark return NULL;
8694 ec2a9698 2022-09-15 mark static const struct got_error *
8695 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8697 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8698 ec2a9698 2022-09-15 mark off_t off = 0;
8699 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8700 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8701 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8702 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8703 ec2a9698 2022-09-15 mark GENERATE_HELP
8704 ec2a9698 2022-09-15 mark #undef KEYMAP_
8705 ec2a9698 2022-09-15 mark #undef KEY_
8708 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8710 ec2a9698 2022-09-15 mark return err;
8712 ec2a9698 2022-09-15 mark n = nitems(km);
8713 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8715 ec2a9698 2022-09-15 mark return err;
8717 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8718 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8719 ec2a9698 2022-09-15 mark show = s->all;
8720 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8721 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8724 ec2a9698 2022-09-15 mark if (show) {
8725 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8727 ec2a9698 2022-09-15 mark return err;
8728 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8730 ec2a9698 2022-09-15 mark return err;
8733 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8735 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8736 ec2a9698 2022-09-15 mark return err;
8739 ec2a9698 2022-09-15 mark static const struct got_error *
8740 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8743 ec2a9698 2022-09-15 mark const struct got_error *err;
8745 ec2a9698 2022-09-15 mark free(s->line_offsets);
8746 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8747 ec2a9698 2022-09-15 mark s->nlines = 0;
8749 ec2a9698 2022-09-15 mark f = got_opentemp();
8750 ec2a9698 2022-09-15 mark if (f == NULL)
8751 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8754 ec2a9698 2022-09-15 mark err = format_help(s);
8756 ec2a9698 2022-09-15 mark return err;
8758 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8759 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8761 ec2a9698 2022-09-15 mark return NULL;
8764 ec2a9698 2022-09-15 mark static const struct got_error *
8765 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8767 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8768 ec2a9698 2022-09-15 mark return NULL;
8771 eaf2c13e 2022-09-17 mark static void
8772 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8773 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8775 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8777 eaf2c13e 2022-09-17 mark *f = s->f;
8778 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8779 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8780 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8781 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8782 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8783 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8786 ec2a9698 2022-09-15 mark static const struct got_error *
8787 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8789 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8790 ec2a9698 2022-09-15 mark const struct got_error *err;
8791 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8792 ec2a9698 2022-09-15 mark wchar_t *wline;
8793 ec2a9698 2022-09-15 mark char *line;
8794 ec2a9698 2022-09-15 mark ssize_t linelen;
8795 ec2a9698 2022-09-15 mark size_t linesz = 0;
8796 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8797 ec2a9698 2022-09-15 mark int eos = view->nlines;
8799 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8800 ec2a9698 2022-09-15 mark --eos; /* account for border */
8802 ec2a9698 2022-09-15 mark s->lineno = 0;
8803 ec2a9698 2022-09-15 mark rewind(s->f);
8804 ec2a9698 2022-09-15 mark werase(view->window);
8806 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8807 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8809 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8810 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8811 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8813 ec2a9698 2022-09-15 mark return err;
8814 ec2a9698 2022-09-15 mark if (eos <= 1)
8815 ec2a9698 2022-09-15 mark return NULL;
8816 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8819 ec2a9698 2022-09-15 mark s->eof = 0;
8820 ec2a9698 2022-09-15 mark view->maxx = 0;
8821 ec2a9698 2022-09-15 mark line = NULL;
8822 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8823 ec2a9698 2022-09-15 mark attr_t attr = 0;
8825 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8826 ec2a9698 2022-09-15 mark if (linelen == -1) {
8827 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8828 ec2a9698 2022-09-15 mark free(line);
8829 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8831 ec2a9698 2022-09-15 mark s->eof = 1;
8834 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8836 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8838 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8839 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8841 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8842 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8843 ec2a9698 2022-09-15 mark if (err) {
8844 ec2a9698 2022-09-15 mark free(line);
8845 ec2a9698 2022-09-15 mark return err;
8847 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8848 ec2a9698 2022-09-15 mark free(wline);
8849 ec2a9698 2022-09-15 mark wline = NULL;
8852 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8853 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8854 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8855 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8856 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8857 ec2a9698 2022-09-15 mark if (err) {
8858 ec2a9698 2022-09-15 mark free(line);
8859 ec2a9698 2022-09-15 mark return err;
8864 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8865 b637f33f 2023-02-01 mark view->x, view->ncols, 0, view->x ? 1 : 0);
8866 ec2a9698 2022-09-15 mark if (err) {
8867 ec2a9698 2022-09-15 mark free(line);
8868 ec2a9698 2022-09-15 mark return err;
8870 b637f33f 2023-02-01 mark waddwstr(view->window, &wline[skip]);
8871 ec2a9698 2022-09-15 mark free(wline);
8872 ec2a9698 2022-09-15 mark wline = NULL;
8874 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8875 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8876 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8878 b637f33f 2023-02-01 mark if (width < view->ncols)
8879 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8882 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8883 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8884 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8886 ec2a9698 2022-09-15 mark free(line);
8887 ec2a9698 2022-09-15 mark if (nprinted > 0)
8888 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8890 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8892 ec2a9698 2022-09-15 mark view_border(view);
8894 ec2a9698 2022-09-15 mark if (s->eof) {
8895 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8896 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8897 ec2a9698 2022-09-15 mark view->ncols - 1);
8898 ec2a9698 2022-09-15 mark if (rc == ERR)
8899 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8901 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8902 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8903 ec2a9698 2022-09-15 mark wstandout(view->window);
8904 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8905 ec2a9698 2022-09-15 mark view->ncols - 1);
8906 ec2a9698 2022-09-15 mark if (rc == ERR)
8907 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8908 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8909 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8910 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8911 ec2a9698 2022-09-15 mark if (rc == ERR)
8912 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8914 ec2a9698 2022-09-15 mark wstandend(view->window);
8917 ec2a9698 2022-09-15 mark return NULL;
8920 ec2a9698 2022-09-15 mark static const struct got_error *
8921 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8923 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8924 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8925 ec2a9698 2022-09-15 mark char *line = NULL;
8926 ec2a9698 2022-09-15 mark ssize_t linelen;
8927 ec2a9698 2022-09-15 mark size_t linesz = 0;
8928 ec2a9698 2022-09-15 mark int eos, nscroll;
8930 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8931 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8932 ec2a9698 2022-09-15 mark --eos; /* border */
8934 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8936 ec2a9698 2022-09-15 mark switch (ch) {
8939 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8941 ec2a9698 2022-09-15 mark case KEY_LEFT:
8943 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8946 ec2a9698 2022-09-15 mark case KEY_HOME:
8947 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8948 ec2a9698 2022-09-15 mark view->count = 0;
8951 ec2a9698 2022-09-15 mark case KEY_END:
8952 ec2a9698 2022-09-15 mark view->count = 0;
8953 ec2a9698 2022-09-15 mark if (s->eof)
8955 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8956 ec2a9698 2022-09-15 mark s->eof = 1;
8959 ec2a9698 2022-09-15 mark case KEY_UP:
8960 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8961 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8963 ec2a9698 2022-09-15 mark view->count = 0;
8965 ec2a9698 2022-09-15 mark case CTRL('u'):
8967 ec2a9698 2022-09-15 mark nscroll /= 2;
8968 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8969 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8970 ec2a9698 2022-09-15 mark case CTRL('b'):
8972 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8973 ec2a9698 2022-09-15 mark view->count = 0;
8976 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8977 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8980 ec2a9698 2022-09-15 mark case KEY_DOWN:
8981 ec2a9698 2022-09-15 mark case CTRL('n'):
8982 ec2a9698 2022-09-15 mark if (!s->eof)
8983 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8985 ec2a9698 2022-09-15 mark view->count = 0;
8987 ec2a9698 2022-09-15 mark case CTRL('d'):
8989 ec2a9698 2022-09-15 mark nscroll /= 2;
8990 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8991 ec2a9698 2022-09-15 mark case KEY_NPAGE:
8992 ec2a9698 2022-09-15 mark case CTRL('f'):
8995 ec2a9698 2022-09-15 mark if (s->eof) {
8996 ec2a9698 2022-09-15 mark view->count = 0;
8999 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
9000 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9001 ec2a9698 2022-09-15 mark s->first_displayed_line++;
9002 ec2a9698 2022-09-15 mark if (linelen == -1) {
9003 ec2a9698 2022-09-15 mark if (feof(s->f))
9004 ec2a9698 2022-09-15 mark s->eof = 1;
9006 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
9010 ec2a9698 2022-09-15 mark free(line);
9013 ec2a9698 2022-09-15 mark view->count = 0;
9017 ec2a9698 2022-09-15 mark return err;
9020 ec2a9698 2022-09-15 mark static const struct got_error *
9021 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
9023 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9025 ec2a9698 2022-09-15 mark free(s->line_offsets);
9026 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9027 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
9028 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9030 ec2a9698 2022-09-15 mark return NULL;
9033 ec2a9698 2022-09-15 mark static const struct got_error *
9034 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
9036 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9039 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
9040 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9042 ec2a9698 2022-09-15 mark wclear(view->window);
9043 ec2a9698 2022-09-15 mark view->count = 0;
9044 ec2a9698 2022-09-15 mark view->x = 0;
9045 ec2a9698 2022-09-15 mark s->all = !s->all;
9046 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9047 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9048 ec2a9698 2022-09-15 mark s->matched_line = 0;
9050 ec2a9698 2022-09-15 mark return create_help(s);
9053 ec2a9698 2022-09-15 mark static const struct got_error *
9054 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
9056 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9057 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9059 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
9060 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9061 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9062 ec2a9698 2022-09-15 mark s->selected_line = 1;
9064 ec2a9698 2022-09-15 mark view->show = show_help_view;
9065 ec2a9698 2022-09-15 mark view->input = input_help_view;
9066 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
9067 ec2a9698 2022-09-15 mark view->close = close_help_view;
9068 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
9069 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
9070 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
9072 ec2a9698 2022-09-15 mark err = create_help(s);
9073 ec2a9698 2022-09-15 mark return err;
9076 ec2a9698 2022-09-15 mark static const struct got_error *
9077 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9078 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
9080 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
9082 136e2bd4 2022-07-23 mark *new_view = NULL;
9084 136e2bd4 2022-07-23 mark switch (request) {
9085 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
9086 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9087 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9089 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9090 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9091 136e2bd4 2022-07-23 mark view, s->repo);
9093 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9094 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9096 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9097 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9098 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9100 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9101 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9104 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9105 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9107 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9108 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9109 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9110 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9111 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9112 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9113 136e2bd4 2022-07-23 mark &view->state.tree);
9114 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9115 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9116 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9117 136e2bd4 2022-07-23 mark view->state.ref.repo);
9119 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9120 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9122 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9123 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9124 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9125 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9126 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9127 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9128 136e2bd4 2022-07-23 mark view->state.log.repo);
9129 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9130 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9131 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9132 136e2bd4 2022-07-23 mark view->state.ref.repo);
9134 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9135 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9137 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9138 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9139 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9140 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9141 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9142 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9143 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9144 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9146 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9147 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9149 136e2bd4 2022-07-23 mark view_close(*new_view);
9151 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9152 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9153 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9154 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9155 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9157 ec2a9698 2022-09-15 mark view_close(*new_view);
9160 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9163 136e2bd4 2022-07-23 mark return err;
9167 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9168 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9170 6458efa5 2020-11-24 stsp static void
9171 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9173 9b058f45 2022-06-30 mark switch (view->type) {
9174 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9175 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9176 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9177 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9181 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9182 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9184 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9185 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9188 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9189 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9190 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9192 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9193 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9194 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9196 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9197 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9198 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9204 9b058f45 2022-06-30 mark view->offset = 0;
9208 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9209 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9211 9b058f45 2022-06-30 mark static const struct got_error *
9212 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9214 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9215 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9216 9b058f45 2022-06-30 mark int *selected = NULL;
9217 9b058f45 2022-06-30 mark int header, offset;
9219 9b058f45 2022-06-30 mark switch (view->type) {
9220 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9221 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9222 9b058f45 2022-06-30 mark header = 3;
9223 9b058f45 2022-06-30 mark scrolld = NULL;
9224 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9225 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9226 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9227 9b058f45 2022-06-30 mark s->selected_line -= offset;
9228 9b058f45 2022-06-30 mark view->offset = offset;
9232 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9233 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9234 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9235 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9236 9b058f45 2022-06-30 mark selected = &s->selected;
9239 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9240 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9241 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9242 9b058f45 2022-06-30 mark header = 3;
9243 9b058f45 2022-06-30 mark selected = &s->selected;
9246 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9247 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9248 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9249 9b058f45 2022-06-30 mark header = 5;
9250 9b058f45 2022-06-30 mark selected = &s->selected;
9254 9b058f45 2022-06-30 mark selected = NULL;
9255 9b058f45 2022-06-30 mark scrolld = NULL;
9256 9b058f45 2022-06-30 mark header = 0;
9260 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9261 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9262 9b058f45 2022-06-30 mark view->offset = offset;
9263 9b058f45 2022-06-30 mark if (scrolld && offset) {
9264 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9265 9b058f45 2022-06-30 mark *selected -= offset;
9269 9b058f45 2022-06-30 mark return err;
9272 9b058f45 2022-06-30 mark static void
9273 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9277 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9278 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9279 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9280 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9282 6879ba42 2020-10-01 naddy fputc('\n', fp);
9285 4ed7e80c 2018-05-20 stsp __dead static void
9286 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9288 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9290 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9291 6879ba42 2020-10-01 naddy getprogname());
9292 6879ba42 2020-10-01 naddy if (hflag) {
9293 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9294 6879ba42 2020-10-01 naddy list_commands(fp);
9296 6879ba42 2020-10-01 naddy exit(status);
9299 c2301be8 2018-04-30 stsp static char **
9300 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9302 ee85c5e8 2020-02-29 stsp va_list ap;
9303 c2301be8 2018-04-30 stsp char **argv;
9306 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9308 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9309 c2301be8 2018-04-30 stsp if (argv == NULL)
9310 c2301be8 2018-04-30 stsp err(1, "calloc");
9311 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9312 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9313 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9314 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9317 ee85c5e8 2020-02-29 stsp va_end(ap);
9318 c2301be8 2018-04-30 stsp return argv;
9322 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9323 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9324 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9325 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9326 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9328 ee85c5e8 2020-02-29 stsp static const struct got_error *
9329 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9331 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9332 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9333 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9334 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9335 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9336 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9337 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9338 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9339 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9341 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9342 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9343 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9345 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9346 0ae84acc 2022-06-15 tracey if (error != NULL)
9347 0ae84acc 2022-06-15 tracey goto done;
9349 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9350 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9351 ee85c5e8 2020-02-29 stsp goto done;
9353 ee85c5e8 2020-02-29 stsp if (worktree)
9354 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9356 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9357 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9358 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9359 ee85c5e8 2020-02-29 stsp goto done;
9362 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9363 ee85c5e8 2020-02-29 stsp if (error != NULL)
9364 ee85c5e8 2020-02-29 stsp goto done;
9366 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9367 ee85c5e8 2020-02-29 stsp repo, worktree);
9368 ee85c5e8 2020-02-29 stsp if (error)
9369 ee85c5e8 2020-02-29 stsp goto done;
9371 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9372 84de9106 2020-12-26 stsp if (error)
9373 84de9106 2020-12-26 stsp goto done;
9374 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9375 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9376 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9377 ee85c5e8 2020-02-29 stsp if (error)
9378 ee85c5e8 2020-02-29 stsp goto done;
9380 ee85c5e8 2020-02-29 stsp if (worktree) {
9381 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9382 ee85c5e8 2020-02-29 stsp worktree = NULL;
9385 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9386 a44927cc 2022-04-07 stsp if (error)
9387 a44927cc 2022-04-07 stsp goto done;
9389 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9390 ee85c5e8 2020-02-29 stsp if (error) {
9391 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9392 ee85c5e8 2020-02-29 stsp goto done;
9393 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9394 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9395 6879ba42 2020-10-01 naddy usage(1, 1);
9396 ee85c5e8 2020-02-29 stsp /* not reached */
9399 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9400 ee85c5e8 2020-02-29 stsp if (error)
9401 ee85c5e8 2020-02-29 stsp goto done;
9403 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9405 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9406 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9408 1d0f4054 2021-06-17 stsp if (repo) {
9409 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9410 1d0f4054 2021-06-17 stsp if (error == NULL)
9411 1d0f4054 2021-06-17 stsp error = close_err;
9413 a44927cc 2022-04-07 stsp if (commit)
9414 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9415 ee85c5e8 2020-02-29 stsp if (worktree)
9416 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9417 0ae84acc 2022-06-15 tracey if (pack_fds) {
9418 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9419 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9420 0ae84acc 2022-06-15 tracey if (error == NULL)
9421 0ae84acc 2022-06-15 tracey error = pack_err;
9424 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9425 ee85c5e8 2020-02-29 stsp free(commit_id);
9426 ee85c5e8 2020-02-29 stsp free(cwd);
9427 ee85c5e8 2020-02-29 stsp free(repo_path);
9428 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9429 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9431 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9432 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9433 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9435 87670572 2020-12-26 naddy tog_free_refs();
9436 ee85c5e8 2020-02-29 stsp return error;
9440 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9442 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9443 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9444 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9445 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9446 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9447 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9448 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9450 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9452 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9453 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9455 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9457 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9458 9f7d7167 2018-04-29 stsp switch (ch) {
9460 9f7d7167 2018-04-29 stsp hflag = 1;
9463 53ccebc2 2019-07-30 stsp Vflag = 1;
9466 6879ba42 2020-10-01 naddy usage(hflag, 1);
9467 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9471 9f7d7167 2018-04-29 stsp argc -= optind;
9472 9f7d7167 2018-04-29 stsp argv += optind;
9473 9814e6a3 2020-09-27 naddy optind = 1;
9474 c2301be8 2018-04-30 stsp optreset = 1;
9476 53ccebc2 2019-07-30 stsp if (Vflag) {
9477 53ccebc2 2019-07-30 stsp got_version_print_str();
9478 6879ba42 2020-10-01 naddy return 0;
9481 4010e238 2020-12-04 stsp #ifndef PROFILE
9482 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9483 4010e238 2020-12-04 stsp NULL) == -1)
9484 4010e238 2020-12-04 stsp err(1, "pledge");
9487 c2301be8 2018-04-30 stsp if (argc == 0) {
9488 f29d3e89 2018-06-23 stsp if (hflag)
9489 6879ba42 2020-10-01 naddy usage(hflag, 0);
9490 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9491 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9493 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9497 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9498 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9499 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9500 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9501 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9507 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9508 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9509 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9510 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9511 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9512 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9515 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9516 ee85c5e8 2020-02-29 stsp if (argc != 1)
9517 6879ba42 2020-10-01 naddy usage(0, 1);
9518 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9519 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9521 ee85c5e8 2020-02-29 stsp if (hflag)
9522 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9524 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9528 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9530 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9531 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9532 a2f4a359 2020-02-28 stsp free(cmd_argv);
9535 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9536 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9537 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9538 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9539 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9540 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);