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 5822e79e 2023-02-23 op #include <sha2.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
45 53ccebc2 2019-07-30 stsp #include "got_version.h"
46 9f7d7167 2018-04-29 stsp #include "got_error.h"
47 80ddbec8 2018-04-29 stsp #include "got_object.h"
48 80ddbec8 2018-04-29 stsp #include "got_reference.h"
49 80ddbec8 2018-04-29 stsp #include "got_repository.h"
50 80ddbec8 2018-04-29 stsp #include "got_diff.h"
51 511a516b 2018-05-19 stsp #include "got_opentemp.h"
52 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
53 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
54 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
55 a70480e0 2018-06-23 stsp #include "got_blame.h"
56 c2db6724 2019-01-04 stsp #include "got_privsep.h"
57 1dd54920 2019-05-11 stsp #include "got_path.h"
58 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
70 9f7d7167 2018-04-29 stsp #ifndef nitems
71 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 9f7d7167 2018-04-29 stsp struct tog_cmd {
75 c2301be8 2018-04-30 stsp const char *name;
76 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
77 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
80 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
84 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
85 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
90 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
91 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
93 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
94 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
95 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
96 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
97 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
98 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
101 d6b05b5b 2018-08-04 stsp enum tog_view_type {
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
103 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
104 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
105 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
106 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
107 ec2a9698 2022-09-15 mark TOG_VIEW_HELP
110 ec2a9698 2022-09-15 mark /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
111 ec2a9698 2022-09-15 mark enum tog_keymap_type {
112 ec2a9698 2022-09-15 mark TOG_KEYMAP_KEYS = -2,
113 ec2a9698 2022-09-15 mark TOG_KEYMAP_GLOBAL,
114 ec2a9698 2022-09-15 mark TOG_KEYMAP_DIFF,
115 ec2a9698 2022-09-15 mark TOG_KEYMAP_LOG,
116 ec2a9698 2022-09-15 mark TOG_KEYMAP_BLAME,
117 ec2a9698 2022-09-15 mark TOG_KEYMAP_TREE,
118 ec2a9698 2022-09-15 mark TOG_KEYMAP_REF,
119 ec2a9698 2022-09-15 mark TOG_KEYMAP_HELP
122 9b058f45 2022-06-30 mark enum tog_view_mode {
123 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
124 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
125 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
128 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
130 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
132 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
133 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
134 ba4f502b 2018-08-04 stsp struct got_object_id *id;
135 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
138 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
139 ba4f502b 2018-08-04 stsp struct commit_queue {
140 ba4f502b 2018-08-04 stsp int ncommits;
141 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
144 f26dddb7 2019-11-08 stsp struct tog_color {
145 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
146 6d17833f 2019-11-08 stsp regex_t regex;
147 6d17833f 2019-11-08 stsp short colorpair;
149 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
151 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
152 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
153 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
155 cc488aa7 2022-01-23 stsp static const struct got_error *
156 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
157 cc488aa7 2022-01-23 stsp struct got_reference* re2)
159 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
160 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
161 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
163 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
164 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
165 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
166 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
168 cc488aa7 2022-01-23 stsp return NULL;
169 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
171 cc488aa7 2022-01-23 stsp return NULL;
174 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
175 cc488aa7 2022-01-23 stsp return NULL;
178 11b20872 2019-11-08 stsp static const struct got_error *
179 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
181 51a10b52 2020-12-26 stsp const struct got_error *err;
183 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
184 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
187 51a10b52 2020-12-26 stsp return err;
189 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
193 51a10b52 2020-12-26 stsp static void
194 51a10b52 2020-12-26 stsp tog_free_refs(void)
196 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
197 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
198 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
200 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
203 51a10b52 2020-12-26 stsp static const struct got_error *
204 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
205 11b20872 2019-11-08 stsp int idx, short color)
207 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
208 11b20872 2019-11-08 stsp struct tog_color *tc;
209 11b20872 2019-11-08 stsp int regerr = 0;
211 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
212 11b20872 2019-11-08 stsp return NULL;
214 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
216 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
217 11b20872 2019-11-08 stsp if (tc == NULL)
218 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
219 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
220 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
221 11b20872 2019-11-08 stsp if (regerr) {
222 11b20872 2019-11-08 stsp static char regerr_msg[512];
223 11b20872 2019-11-08 stsp static char err_msg[512];
224 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
225 11b20872 2019-11-08 stsp sizeof(regerr_msg));
226 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
227 11b20872 2019-11-08 stsp regerr_msg);
228 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
230 11b20872 2019-11-08 stsp return err;
232 11b20872 2019-11-08 stsp tc->colorpair = idx;
233 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
234 11b20872 2019-11-08 stsp return NULL;
237 11b20872 2019-11-08 stsp static void
238 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
240 11b20872 2019-11-08 stsp struct tog_color *tc;
242 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
243 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
244 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
245 11b20872 2019-11-08 stsp regfree(&tc->regex);
250 336075a4 2022-06-25 op static struct tog_color *
251 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
253 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
255 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
256 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
260 11b20872 2019-11-08 stsp return NULL;
264 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
267 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
269 11b20872 2019-11-08 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
271 11b20872 2019-11-08 stsp return COLOR_YELLOW;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
273 11b20872 2019-11-08 stsp return COLOR_GREEN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
275 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
277 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
279 91b8c405 2020-01-25 stsp return COLOR_CYAN;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
281 11b20872 2019-11-08 stsp return COLOR_GREEN;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
283 11b20872 2019-11-08 stsp return COLOR_GREEN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
285 11b20872 2019-11-08 stsp return COLOR_CYAN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
287 11b20872 2019-11-08 stsp return COLOR_YELLOW;
288 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
289 6458efa5 2020-11-24 stsp return COLOR_GREEN;
290 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
291 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
294 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
295 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
301 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
303 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
305 11b20872 2019-11-08 stsp if (val == NULL)
306 11b20872 2019-11-08 stsp return default_color_value(envvar);
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
309 11b20872 2019-11-08 stsp return COLOR_BLACK;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
311 11b20872 2019-11-08 stsp return COLOR_RED;
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
313 11b20872 2019-11-08 stsp return COLOR_GREEN;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
315 11b20872 2019-11-08 stsp return COLOR_YELLOW;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
317 11b20872 2019-11-08 stsp return COLOR_BLUE;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
319 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
321 11b20872 2019-11-08 stsp return COLOR_CYAN;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
323 11b20872 2019-11-08 stsp return COLOR_WHITE;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
327 11b20872 2019-11-08 stsp return default_color_value(envvar);
330 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
331 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
332 3dbaef42 2020-11-24 stsp const char *label1, *label2;
333 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
334 f9d37699 2022-06-28 stsp int fd1, fd2;
335 c7d5c43c 2022-08-04 mark int lineno;
336 15a087fe 2019-02-21 stsp int first_displayed_line;
337 15a087fe 2019-02-21 stsp int last_displayed_line;
339 15a087fe 2019-02-21 stsp int diff_context;
340 3dbaef42 2020-11-24 stsp int ignore_whitespace;
341 64453f7e 2020-11-21 stsp int force_text_diff;
342 15a087fe 2019-02-21 stsp struct got_repository *repo;
343 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
344 fe621944 2020-11-10 stsp size_t nlines;
345 f44b1f58 2020-02-02 tracey int matched_line;
346 f44b1f58 2020-02-02 tracey int selected_line;
348 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
349 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
352 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
353 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
355 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
356 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
357 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
358 1a76625f 2018-10-22 stsp int commits_needed;
359 fb280deb 2021-08-30 stsp int load_all;
360 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
361 568eae95 2022-09-11 mark struct commit_queue *real_commits;
362 1a76625f 2018-10-22 stsp const char *in_repo_path;
363 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
364 1a76625f 2018-10-22 stsp struct got_repository *repo;
365 74467cc8 2022-06-15 stsp int *pack_fds;
366 1a76625f 2018-10-22 stsp int log_complete;
367 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
368 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
369 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
370 13add988 2019-10-15 stsp int *searching;
371 13add988 2019-10-15 stsp int *search_next_done;
372 13add988 2019-10-15 stsp regex_t *regex;
373 568eae95 2022-09-11 mark int *limiting;
374 568eae95 2022-09-11 mark int limit_match;
375 568eae95 2022-09-11 mark regex_t *limit_regex;
376 568eae95 2022-09-11 mark struct commit_queue *limit_commits;
379 1a76625f 2018-10-22 stsp struct tog_log_view_state {
380 568eae95 2022-09-11 mark struct commit_queue *commits;
381 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
382 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
383 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
384 568eae95 2022-09-11 mark struct commit_queue real_commits;
385 b01e7d3b 2018-08-04 stsp int selected;
386 b01e7d3b 2018-08-04 stsp char *in_repo_path;
387 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
388 b672a97a 2020-01-27 stsp int log_branches;
389 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
390 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
391 1a76625f 2018-10-22 stsp sig_atomic_t quit;
392 1a76625f 2018-10-22 stsp pthread_t thread;
393 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
394 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
395 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
396 11b20872 2019-11-08 stsp struct tog_colors colors;
397 10aab77f 2022-07-19 op int use_committer;
398 568eae95 2022-09-11 mark int limit_view;
399 568eae95 2022-09-11 mark regex_t limit_regex;
400 568eae95 2022-09-11 mark struct commit_queue limit_commits;
403 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
404 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
405 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
406 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
407 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
408 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
409 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
410 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
411 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
412 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
413 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
414 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
415 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
416 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
417 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
419 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
420 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
421 e9424729 2018-08-04 stsp int nlines;
423 e9424729 2018-08-04 stsp struct tog_view *view;
424 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
429 e9424729 2018-08-04 stsp const char *path;
430 e9424729 2018-08-04 stsp struct got_repository *repo;
431 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
432 e9424729 2018-08-04 stsp int *complete;
433 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
434 fc06ba56 2019-08-22 stsp void *cancel_arg;
437 e9424729 2018-08-04 stsp struct tog_blame {
439 be659d10 2020-11-18 stsp off_t filesize;
440 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
441 6fcac457 2018-11-19 stsp int nlines;
442 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
443 e9424729 2018-08-04 stsp pthread_t thread;
444 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
445 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
446 e9424729 2018-08-04 stsp const char *path;
447 0ae84acc 2022-06-15 tracey int *pack_fds;
450 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
451 7cbe629d 2018-08-04 stsp int first_displayed_line;
452 7cbe629d 2018-08-04 stsp int last_displayed_line;
453 7cbe629d 2018-08-04 stsp int selected_line;
454 c0f61fa4 2022-07-11 mark int last_diffed_line;
455 7cbe629d 2018-08-04 stsp int blame_complete;
458 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
459 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
460 e5a0f69f 2018-08-18 stsp char *path;
461 7cbe629d 2018-08-04 stsp struct got_repository *repo;
462 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
463 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
464 e9424729 2018-08-04 stsp struct tog_blame blame;
465 6c4c42e0 2019-06-24 stsp int matched_line;
466 11b20872 2019-11-08 stsp struct tog_colors colors;
469 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
470 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
471 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
472 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
473 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
474 ad80ab7b 2018-08-04 stsp int selected;
477 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
479 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
480 ad80ab7b 2018-08-04 stsp char *tree_label;
481 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
482 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
483 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
484 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
486 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
487 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
488 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
489 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
490 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
491 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
492 6458efa5 2020-11-24 stsp struct tog_colors colors;
495 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
496 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
497 6458efa5 2020-11-24 stsp struct got_reference *ref;
501 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
503 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
504 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
506 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
507 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
508 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
509 6458efa5 2020-11-24 stsp struct got_repository *repo;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
511 bddb1296 2019-11-08 stsp struct tog_colors colors;
514 ec2a9698 2022-09-15 mark struct tog_help_view_state {
516 ec2a9698 2022-09-15 mark off_t *line_offsets;
517 ec2a9698 2022-09-15 mark size_t nlines;
518 ec2a9698 2022-09-15 mark int lineno;
519 ec2a9698 2022-09-15 mark int first_displayed_line;
520 ec2a9698 2022-09-15 mark int last_displayed_line;
522 ec2a9698 2022-09-15 mark int matched_line;
523 ec2a9698 2022-09-15 mark int selected_line;
525 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
528 ec2a9698 2022-09-15 mark #define GENERATE_HELP \
529 ec2a9698 2022-09-15 mark KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
530 ec2a9698 2022-09-15 mark KEY_("H F1", "Open view-specific help (double tap for all help)"), \
531 ec2a9698 2022-09-15 mark KEY_("k C-p Up", "Move cursor or page up one line"), \
532 ec2a9698 2022-09-15 mark KEY_("j C-n Down", "Move cursor or page down one line"), \
533 ec2a9698 2022-09-15 mark KEY_("C-b b PgUp", "Scroll the view up one page"), \
534 ec2a9698 2022-09-15 mark KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
535 ec2a9698 2022-09-15 mark KEY_("C-u u", "Scroll the view up one half page"), \
536 ec2a9698 2022-09-15 mark KEY_("C-d d", "Scroll the view down one half page"), \
537 0b3f028d 2023-01-09 mark KEY_("g", "Go to line N (default: first line)"), \
538 0b3f028d 2023-01-09 mark KEY_("Home =", "Go to the first line"), \
539 0b3f028d 2023-01-09 mark KEY_("G", "Go to line N (default: last line)"), \
540 0b3f028d 2023-01-09 mark KEY_("End *", "Go to the last line"), \
541 ec2a9698 2022-09-15 mark KEY_("l Right", "Scroll the view right"), \
542 ec2a9698 2022-09-15 mark KEY_("h Left", "Scroll the view left"), \
543 ec2a9698 2022-09-15 mark KEY_("$", "Scroll view to the rightmost position"), \
544 ec2a9698 2022-09-15 mark KEY_("0", "Scroll view to the leftmost position"), \
545 ec2a9698 2022-09-15 mark KEY_("-", "Decrease size of the focussed split"), \
546 ec2a9698 2022-09-15 mark KEY_("+", "Increase size of the focussed split"), \
547 ec2a9698 2022-09-15 mark KEY_("Tab", "Switch focus between views"), \
548 ec2a9698 2022-09-15 mark KEY_("F", "Toggle fullscreen mode"), \
549 ec2a9698 2022-09-15 mark KEY_("/", "Open prompt to enter search term"), \
550 ec2a9698 2022-09-15 mark KEY_("n", "Find next line/token matching the current search term"), \
551 ec2a9698 2022-09-15 mark KEY_("N", "Find previous line/token matching the current search term"),\
552 16a2d4f0 2022-09-19 stsp KEY_("q", "Quit the focussed view; Quit help screen"), \
553 ec2a9698 2022-09-15 mark KEY_("Q", "Quit tog"), \
555 fd6badaa 2022-09-23 stsp KEYMAP_("Log view", TOG_KEYMAP_LOG), \
556 ec2a9698 2022-09-15 mark KEY_("< ,", "Move cursor up one commit"), \
557 ec2a9698 2022-09-15 mark KEY_("> .", "Move cursor down one commit"), \
558 ec2a9698 2022-09-15 mark KEY_("Enter", "Open diff view of the selected commit"), \
559 ec2a9698 2022-09-15 mark KEY_("B", "Reload the log view and toggle display of merged commits"), \
560 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
561 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the repository from the selected" \
562 ec2a9698 2022-09-15 mark " commit"), \
563 ec2a9698 2022-09-15 mark KEY_("@", "Toggle between displaying author and committer name"), \
564 ec2a9698 2022-09-15 mark KEY_("&", "Open prompt to enter term to limit commits displayed"), \
565 ec2a9698 2022-09-15 mark KEY_("C-g Backspace", "Cancel current search or log operation"), \
566 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload the log view with new commits in the repository"), \
568 fd6badaa 2022-09-23 stsp KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
569 ec2a9698 2022-09-15 mark KEY_("K < ,", "Display diff of next line in the file/log entry"), \
570 ec2a9698 2022-09-15 mark KEY_("J > .", "Display diff of previous line in the file/log entry"), \
571 ec2a9698 2022-09-15 mark KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
572 ec2a9698 2022-09-15 mark KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
573 ec2a9698 2022-09-15 mark " data"), \
574 ec2a9698 2022-09-15 mark KEY_("(", "Go to the previous file in the diff"), \
575 ec2a9698 2022-09-15 mark KEY_(")", "Go to the next file in the diff"), \
576 ec2a9698 2022-09-15 mark KEY_("{", "Go to the previous hunk in the diff"), \
577 ec2a9698 2022-09-15 mark KEY_("}", "Go to the next hunk in the diff"), \
578 ec2a9698 2022-09-15 mark KEY_("[", "Decrease the number of context lines"), \
579 ec2a9698 2022-09-15 mark KEY_("]", "Increase the number of context lines"), \
580 ec2a9698 2022-09-15 mark KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
582 fd6badaa 2022-09-23 stsp KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
583 ec2a9698 2022-09-15 mark KEY_("Enter", "Display diff view of the selected line's commit"), \
584 ec2a9698 2022-09-15 mark KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
585 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the currently selected annotated line"), \
586 ec2a9698 2022-09-15 mark KEY_("C", "Reload view with the previously blamed commit"), \
587 ec2a9698 2022-09-15 mark KEY_("c", "Reload view with the version of the file found in the" \
588 ec2a9698 2022-09-15 mark " selected line's commit"), \
589 ec2a9698 2022-09-15 mark KEY_("p", "Reload view with the version of the file found in the" \
590 ec2a9698 2022-09-15 mark " selected line's parent commit"), \
592 fd6badaa 2022-09-23 stsp KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
593 ec2a9698 2022-09-15 mark KEY_("Enter", "Enter selected directory or open blame view of the" \
594 ec2a9698 2022-09-15 mark " selected file"), \
595 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the selected entry"), \
596 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
597 ec2a9698 2022-09-15 mark KEY_("i", "Show object IDs for all tree entries"), \
598 ec2a9698 2022-09-15 mark KEY_("Backspace", "Return to the parent directory"), \
600 fd6badaa 2022-09-23 stsp KEYMAP_("Ref view", TOG_KEYMAP_REF), \
601 ec2a9698 2022-09-15 mark KEY_("Enter", "Display log view of the selected reference"), \
602 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the selected reference"), \
603 ec2a9698 2022-09-15 mark KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
604 ec2a9698 2022-09-15 mark KEY_("m", "Toggle display of last modified date for each reference"), \
605 ec2a9698 2022-09-15 mark KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
606 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload view with all repository references")
608 ec2a9698 2022-09-15 mark struct tog_key_map {
609 ec2a9698 2022-09-15 mark const char *keys;
610 ec2a9698 2022-09-15 mark const char *info;
611 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
614 af21bb7e 2023-04-12 mark /* curses io for tog regress */
615 af21bb7e 2023-04-12 mark struct tog_io {
617 af21bb7e 2023-04-12 mark FILE *cout;
621 af21bb7e 2023-04-12 mark #define TOG_SCREEN_DUMP "SCREENDUMP"
622 af21bb7e 2023-04-12 mark #define TOG_SCREEN_DUMP_LEN (sizeof(TOG_SCREEN_DUMP) - 1)
623 af21bb7e 2023-04-12 mark #define TOG_KEY_SCRDUMP SHRT_MIN
626 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
628 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
629 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
630 669b5ffa 2018-10-07 stsp * there is enough screen estate.
632 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
633 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
635 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
636 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
637 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
639 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
640 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
642 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
644 cc3c9aac 2018-08-01 stsp struct tog_view {
645 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
646 26ed57b2 2018-05-19 stsp WINDOW *window;
647 26ed57b2 2018-05-19 stsp PANEL *panel;
648 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
649 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
650 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
651 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
652 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
653 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
654 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
655 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
656 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
658 669b5ffa 2018-10-07 stsp struct tog_view *parent;
659 669b5ffa 2018-10-07 stsp struct tog_view *child;
662 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
663 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
664 e78dc838 2020-12-04 stsp * between parent and child.
665 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
666 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
667 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
668 e78dc838 2020-12-04 stsp * situations.
670 e78dc838 2020-12-04 stsp int focus_child;
672 9b058f45 2022-06-30 mark enum tog_view_mode mode;
673 5dc9f4bc 2018-08-04 stsp /* type-specific state */
674 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
676 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
677 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
678 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
679 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
680 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
681 ec2a9698 2022-09-15 mark struct tog_help_view_state help;
684 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
685 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
686 e78dc838 2020-12-04 stsp struct tog_view *, int);
687 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
688 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
689 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
691 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
692 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
693 eaf2c13e 2022-09-17 mark void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
694 eaf2c13e 2022-09-17 mark int **, int **, int **, int **);
695 c0c4acc8 2021-01-24 stsp int search_started;
696 60493ae3 2019-06-20 stsp int searching;
697 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
698 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
699 60493ae3 2019-06-20 stsp int search_next_done;
700 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
701 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
702 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
703 1803e47f 2019-06-22 stsp regex_t regex;
704 41605754 2020-11-12 stsp regmatch_t regmatch;
705 3f6c6614 2023-01-23 mark const char *action;
708 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
709 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
710 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
711 78756c87 2020-11-24 stsp struct got_repository *);
712 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
713 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
714 e78dc838 2020-12-04 stsp struct tog_view *, int);
715 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
716 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
717 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
718 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
719 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
720 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
722 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
723 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
724 78756c87 2020-11-24 stsp const char *, const char *, int);
725 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
726 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
727 e78dc838 2020-12-04 stsp struct tog_view *, int);
728 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
729 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
730 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
731 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
733 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
734 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
735 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
736 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
737 e78dc838 2020-12-04 stsp struct tog_view *, int);
738 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
739 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
740 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
741 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
742 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
744 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
745 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
746 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
747 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
748 e78dc838 2020-12-04 stsp struct tog_view *, int);
749 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
750 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
751 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
753 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
754 6458efa5 2020-11-24 stsp struct got_repository *);
755 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
756 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
757 e78dc838 2020-12-04 stsp struct tog_view *, int);
758 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
759 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
760 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
762 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
763 eaf2c13e 2022-09-17 mark struct tog_view *);
764 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
765 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
766 eaf2c13e 2022-09-17 mark struct tog_view *, int);
767 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
768 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
769 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
770 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
771 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
773 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
774 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
775 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
776 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
777 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
779 25791caa 2018-10-24 stsp static void
780 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
782 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
785 83baff54 2019-08-12 stsp static void
786 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
788 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
791 61266923 2020-01-14 stsp static void
792 61266923 2020-01-14 stsp tog_sigcont(int signo)
794 61266923 2020-01-14 stsp tog_sigcont_received = 1;
797 2497f032 2022-05-31 stsp static void
798 2497f032 2022-05-31 stsp tog_sigint(int signo)
800 2497f032 2022-05-31 stsp tog_sigint_received = 1;
803 2497f032 2022-05-31 stsp static void
804 2497f032 2022-05-31 stsp tog_sigterm(int signo)
806 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
810 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
812 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
813 f044c841 2022-10-24 stsp tog_sigint_received || tog_sigterm_received);
816 e5a0f69f 2018-08-18 stsp static const struct got_error *
817 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
819 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
821 669b5ffa 2018-10-07 stsp if (view->child) {
822 5629093a 2022-07-11 stsp child_err = view_close(view->child);
823 669b5ffa 2018-10-07 stsp view->child = NULL;
825 e5a0f69f 2018-08-18 stsp if (view->close)
826 e5a0f69f 2018-08-18 stsp err = view->close(view);
827 ea5e7bb5 2018-08-01 stsp if (view->panel)
828 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
829 ea5e7bb5 2018-08-01 stsp if (view->window)
830 ea5e7bb5 2018-08-01 stsp delwin(view->window);
831 ea5e7bb5 2018-08-01 stsp free(view);
832 5629093a 2022-07-11 stsp return err ? err : child_err;
835 ea5e7bb5 2018-08-01 stsp static struct tog_view *
836 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
837 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
839 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
841 ea5e7bb5 2018-08-01 stsp if (view == NULL)
842 ea5e7bb5 2018-08-01 stsp return NULL;
844 d6b05b5b 2018-08-04 stsp view->type = type;
845 f7d12f7e 2018-08-01 stsp view->lines = LINES;
846 f7d12f7e 2018-08-01 stsp view->cols = COLS;
847 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
848 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
849 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
850 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
851 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
852 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
853 96a765a8 2018-08-04 stsp view_close(view);
854 ea5e7bb5 2018-08-01 stsp return NULL;
856 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
857 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
858 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
859 96a765a8 2018-08-04 stsp view_close(view);
860 ea5e7bb5 2018-08-01 stsp return NULL;
863 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
864 ea5e7bb5 2018-08-01 stsp return view;
868 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
870 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
872 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
875 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
877 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
879 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
882 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
884 5c60c32a 2018-10-18 stsp static const struct got_error *
885 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
887 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
889 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
890 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
891 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
893 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
894 9b058f45 2022-06-30 mark view->begin_x = 0;
895 571ccd73 2022-07-19 mark } else if (!view->resized) {
896 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
897 3c1dfe12 2022-07-08 mark view->cols > 119)
898 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
900 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
901 9b058f45 2022-06-30 mark view->begin_y = 0;
903 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
904 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
905 5c60c32a 2018-10-18 stsp view->lines = LINES;
906 5c60c32a 2018-10-18 stsp view->cols = COLS;
907 5c60c32a 2018-10-18 stsp err = view_resize(view);
909 5c60c32a 2018-10-18 stsp return err;
911 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
912 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
914 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
915 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
917 5c60c32a 2018-10-18 stsp return NULL;
920 5c60c32a 2018-10-18 stsp static const struct got_error *
921 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
923 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
925 5c60c32a 2018-10-18 stsp view->begin_x = 0;
926 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
927 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
928 5c60c32a 2018-10-18 stsp view->ncols = COLS;
929 5c60c32a 2018-10-18 stsp view->lines = LINES;
930 5c60c32a 2018-10-18 stsp view->cols = COLS;
931 5c60c32a 2018-10-18 stsp err = view_resize(view);
933 5c60c32a 2018-10-18 stsp return err;
935 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
936 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
938 5c60c32a 2018-10-18 stsp return NULL;
942 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
944 5c60c32a 2018-10-18 stsp return view->parent == NULL;
948 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
950 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
954 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
956 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
960 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
962 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
963 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
966 9b058f45 2022-06-30 mark static void
967 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
969 9b058f45 2022-06-30 mark PANEL *panel;
970 9b058f45 2022-06-30 mark const struct tog_view *view_above;
972 9b058f45 2022-06-30 mark if (view->parent)
973 9b058f45 2022-06-30 mark return view_border(view->parent);
975 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
976 9b058f45 2022-06-30 mark if (panel == NULL)
979 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
980 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
981 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
982 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
983 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
985 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
986 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
989 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
990 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
991 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
992 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
993 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
995 4d8c2215 2018-08-19 stsp static const struct got_error *
996 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
998 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
999 9b058f45 2022-06-30 mark int dif, nlines, ncols;
1001 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
1003 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1004 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1006 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1007 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1008 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1010 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1012 4dd27a72 2022-06-29 stsp if (view->child) {
1013 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
1015 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1016 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1017 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1018 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1019 0dbbbe90 2022-06-17 op ncols = COLS;
1021 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1022 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1023 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1025 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1027 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1029 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1030 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1033 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1034 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1035 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1038 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1040 9b058f45 2022-06-30 mark return err;
1041 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1042 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1044 9b058f45 2022-06-30 mark return err;
1046 9b058f45 2022-06-30 mark view_border(view);
1047 9b058f45 2022-06-30 mark update_panels();
1048 9b058f45 2022-06-30 mark doupdate();
1049 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1050 9b058f45 2022-06-30 mark nlines = view->nlines;
1052 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1053 0dbbbe90 2022-06-17 op ncols = COLS;
1055 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1056 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1058 571ccd73 2022-07-19 mark return err;
1061 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1062 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1063 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1064 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1065 0dbbbe90 2022-06-17 op wclear(view->window);
1067 0dbbbe90 2022-06-17 op view->nlines = nlines;
1068 0dbbbe90 2022-06-17 op view->ncols = ncols;
1069 0dbbbe90 2022-06-17 op view->lines = LINES;
1070 0dbbbe90 2022-06-17 op view->cols = COLS;
1072 5c60c32a 2018-10-18 stsp return NULL;
1075 571ccd73 2022-07-19 mark static const struct got_error *
1076 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1078 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1079 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1080 6fe51fee 2022-07-22 mark int n = 0;
1082 6fe51fee 2022-07-22 mark if (s->selected_entry)
1083 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1086 571ccd73 2022-07-19 mark * Request commits to account for the increased
1087 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1089 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1090 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1091 571ccd73 2022-07-19 mark err = request_log_commits(view);
1094 571ccd73 2022-07-19 mark return err;
1097 d9a7ab53 2022-07-11 mark static void
1098 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1100 d9a7ab53 2022-07-11 mark if (n == 0)
1103 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1104 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1105 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1107 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1108 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1109 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1110 d9a7ab53 2022-07-11 mark view->offset -= n;
1112 d9a7ab53 2022-07-11 mark view->offset = 0;
1116 3c1dfe12 2022-07-08 mark static const struct got_error *
1117 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1119 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1120 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1122 3c1dfe12 2022-07-08 mark if (view->parent)
1123 3c1dfe12 2022-07-08 mark v = view->parent;
1127 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1128 3c1dfe12 2022-07-08 mark return NULL;
1130 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1132 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1133 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1134 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1135 3c1dfe12 2022-07-08 mark if (view->parent)
1136 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1138 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1139 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1140 3c1dfe12 2022-07-08 mark view->count = 0;
1141 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1142 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1143 3c1dfe12 2022-07-08 mark view->count = 0;
1144 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1146 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1147 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1148 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1149 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1151 3c1dfe12 2022-07-08 mark return err;
1152 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1154 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1155 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1156 3c1dfe12 2022-07-08 mark if (view->parent)
1157 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1159 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1160 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1161 3c1dfe12 2022-07-08 mark view->count = 0;
1162 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1163 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1164 3c1dfe12 2022-07-08 mark view->count = 0;
1165 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1167 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1170 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1171 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1172 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1173 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1175 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1177 3c1dfe12 2022-07-08 mark return err;
1178 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1180 3c1dfe12 2022-07-08 mark return err;
1182 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1183 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1185 3c1dfe12 2022-07-08 mark return err;
1188 6fe51fee 2022-07-22 mark if (v->resize)
1189 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1190 6fe51fee 2022-07-22 mark else if (v->child->resize)
1191 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1193 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1195 3c1dfe12 2022-07-08 mark return err;
1198 3c1dfe12 2022-07-08 mark static void
1199 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1201 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1203 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1204 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1207 669b5ffa 2018-10-07 stsp static const struct got_error *
1208 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1210 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1212 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1213 669b5ffa 2018-10-07 stsp return NULL;
1215 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1216 669b5ffa 2018-10-07 stsp view->child = NULL;
1217 669b5ffa 2018-10-07 stsp return err;
1220 0dbbbe90 2022-06-17 op static const struct got_error *
1221 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1223 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1225 669b5ffa 2018-10-07 stsp view->child = child;
1226 669b5ffa 2018-10-07 stsp child->parent = view;
1228 3c1dfe12 2022-07-08 mark err = view_resize(view);
1230 3c1dfe12 2022-07-08 mark return err;
1232 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1233 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1235 3c1dfe12 2022-07-08 mark return err;
1238 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1239 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1241 136e2bd4 2022-07-23 mark static const struct got_error *
1242 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1243 136e2bd4 2022-07-23 mark enum tog_view_type request)
1245 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1246 136e2bd4 2022-07-23 mark const struct got_error *err;
1247 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1249 136e2bd4 2022-07-23 mark *requested = NULL;
1251 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1252 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1254 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1256 136e2bd4 2022-07-23 mark return err;
1258 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1259 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1260 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1262 136e2bd4 2022-07-23 mark return err;
1265 136e2bd4 2022-07-23 mark view->focussed = 0;
1266 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1267 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1268 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1269 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1271 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1272 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1273 136e2bd4 2022-07-23 mark err = view_close_child(view);
1275 136e2bd4 2022-07-23 mark return err;
1276 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1278 136e2bd4 2022-07-23 mark return err;
1279 136e2bd4 2022-07-23 mark view->focus_child = 1;
1281 136e2bd4 2022-07-23 mark *requested = new_view;
1283 136e2bd4 2022-07-23 mark return NULL;
1286 34bc9ec9 2019-02-22 stsp static void
1287 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1289 25791caa 2018-10-24 stsp int cols, lines;
1290 25791caa 2018-10-24 stsp struct winsize size;
1292 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1293 25791caa 2018-10-24 stsp cols = 80; /* Default */
1294 25791caa 2018-10-24 stsp lines = 24;
1296 25791caa 2018-10-24 stsp cols = size.ws_col;
1297 25791caa 2018-10-24 stsp lines = size.ws_row;
1299 25791caa 2018-10-24 stsp resize_term(lines, cols);
1302 2b49a8ae 2019-06-22 stsp static const struct got_error *
1303 602eda79 2023-02-09 mark view_search_start(struct tog_view *view, int fast_refresh)
1305 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1306 9b058f45 2022-06-30 mark struct tog_view *v = view;
1307 2b49a8ae 2019-06-22 stsp char pattern[1024];
1310 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1311 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1312 c0c4acc8 2021-01-24 stsp view->searching = 0;
1313 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1315 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1317 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1318 2b49a8ae 2019-06-22 stsp return NULL;
1320 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1321 9b058f45 2022-06-30 mark v = view->child;
1322 9d0feb8b 2022-12-27 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1323 9d0feb8b 2022-12-27 mark v = view->parent;
1325 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1326 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1328 092a9f9c 2022-12-27 mark nodelay(v->window, FALSE); /* block for search term input */
1329 2b49a8ae 2019-06-22 stsp nocbreak();
1331 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1332 9b058f45 2022-06-30 mark wrefresh(v->window);
1335 092a9f9c 2022-12-27 mark nodelay(v->window, TRUE);
1336 602eda79 2023-02-09 mark if (!fast_refresh)
1337 602eda79 2023-02-09 mark halfdelay(10);
1338 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1339 2b49a8ae 2019-06-22 stsp return NULL;
1341 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1342 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1343 7c32bd05 2019-06-22 stsp if (err) {
1344 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1345 7c32bd05 2019-06-22 stsp return err;
1347 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1348 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1349 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1350 2b49a8ae 2019-06-22 stsp view->search_next(view);
1353 2b49a8ae 2019-06-22 stsp return NULL;
1356 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1357 d2366e29 2022-07-07 mark static const struct got_error *
1358 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1360 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1361 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1363 d2366e29 2022-07-07 mark if (view->parent)
1364 d2366e29 2022-07-07 mark v = view->parent;
1368 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1369 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1371 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1373 7532ccda 2022-07-11 mark if (!v->child)
1374 7532ccda 2022-07-11 mark return NULL;
1375 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1376 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1378 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1379 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1380 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1381 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1382 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1385 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1386 d2366e29 2022-07-07 mark v->ncols = COLS;
1387 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1388 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1390 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1392 d2366e29 2022-07-07 mark return err;
1394 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1395 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1396 d2366e29 2022-07-07 mark v->focus_child = 1;
1398 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1400 d2366e29 2022-07-07 mark return err;
1401 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1403 d2366e29 2022-07-07 mark return err;
1405 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1406 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1407 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1408 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1410 279d2047 2022-07-29 stsp return err;
1411 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1413 279d2047 2022-07-29 stsp return err;
1415 7532ccda 2022-07-11 mark offset_selection_up(v);
1416 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1418 dff91825 2022-07-22 mark if (v->resize)
1419 dff91825 2022-07-22 mark err = v->resize(v, 0);
1420 dff91825 2022-07-22 mark else if (v->child->resize)
1421 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1423 d2366e29 2022-07-07 mark return err;
1427 af21bb7e 2023-04-12 mark * Strip trailing whitespace from str starting at byte *n;
1428 af21bb7e 2023-04-12 mark * if *n < 0, use strlen(str). Return new str length in *n.
1430 af21bb7e 2023-04-12 mark static void
1431 af21bb7e 2023-04-12 mark strip_trailing_ws(char *str, int *n)
1433 af21bb7e 2023-04-12 mark size_t x = *n;
1435 af21bb7e 2023-04-12 mark if (str == NULL || *str == '\0')
1438 af21bb7e 2023-04-12 mark if (x < 0)
1439 af21bb7e 2023-04-12 mark x = strlen(str);
1441 af21bb7e 2023-04-12 mark while (x-- > 0 && isspace((unsigned char)str[x]))
1442 af21bb7e 2023-04-12 mark str[x] = '\0';
1444 af21bb7e 2023-04-12 mark *n = x + 1;
1448 af21bb7e 2023-04-12 mark * Extract visible substring of line y from the curses screen
1449 af21bb7e 2023-04-12 mark * and strip trailing whitespace. If vline is set and locale is
1450 af21bb7e 2023-04-12 mark * UTF-8, overwrite line[vline] with '|' because the ACS_VLINE
1451 af21bb7e 2023-04-12 mark * character is written out as 'x'. Write the line to file f.
1453 af21bb7e 2023-04-12 mark static const struct got_error *
1454 af21bb7e 2023-04-12 mark view_write_line(FILE *f, int y, int vline)
1456 af21bb7e 2023-04-12 mark char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1459 af21bb7e 2023-04-12 mark r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1460 af21bb7e 2023-04-12 mark if (r == ERR)
1461 af21bb7e 2023-04-12 mark return got_error_fmt(GOT_ERR_RANGE,
1462 af21bb7e 2023-04-12 mark "failed to extract line %d", y);
1465 af21bb7e 2023-04-12 mark * In some views, lines are padded with blanks to COLS width.
1466 af21bb7e 2023-04-12 mark * Strip them so we can diff without the -b flag when testing.
1468 af21bb7e 2023-04-12 mark strip_trailing_ws(line, &r);
1470 af21bb7e 2023-04-12 mark if (vline > 0 && got_locale_is_utf8())
1471 af21bb7e 2023-04-12 mark line[vline] = '|';
1473 af21bb7e 2023-04-12 mark w = fprintf(f, "%s\n", line);
1474 af21bb7e 2023-04-12 mark if (w != r + 1) /* \n */
1475 af21bb7e 2023-04-12 mark return got_ferror(f, GOT_ERR_IO);
1477 af21bb7e 2023-04-12 mark return NULL;
1481 af21bb7e 2023-04-12 mark * Capture the visible curses screen by writing each line to the
1482 af21bb7e 2023-04-12 mark * file at the path set via the TOG_SCR_DUMP environment variable.
1484 af21bb7e 2023-04-12 mark static const struct got_error *
1485 af21bb7e 2023-04-12 mark screendump(struct tog_view *view)
1487 af21bb7e 2023-04-12 mark const struct got_error *err;
1488 af21bb7e 2023-04-12 mark FILE *f = NULL;
1489 af21bb7e 2023-04-12 mark const char *path;
1492 af21bb7e 2023-04-12 mark path = getenv("TOG_SCR_DUMP");
1493 af21bb7e 2023-04-12 mark if (path == NULL || *path == '\0')
1494 af21bb7e 2023-04-12 mark return got_error_msg(GOT_ERR_BAD_PATH,
1495 af21bb7e 2023-04-12 mark "TOG_SCR_DUMP path not set to capture screen dump");
1496 af21bb7e 2023-04-12 mark f = fopen(path, "wex");
1497 af21bb7e 2023-04-12 mark if (f == NULL)
1498 af21bb7e 2023-04-12 mark return got_error_from_errno_fmt("fopen: %s", path);
1500 af21bb7e 2023-04-12 mark if ((view->child && view->child->begin_x) ||
1501 af21bb7e 2023-04-12 mark (view->parent && view->begin_x)) {
1502 af21bb7e 2023-04-12 mark int ncols = view->child ? view->ncols : view->parent->ncols;
1504 af21bb7e 2023-04-12 mark /* vertical splitscreen */
1505 af21bb7e 2023-04-12 mark for (i = 0; i < view->nlines; ++i) {
1506 af21bb7e 2023-04-12 mark err = view_write_line(f, i, ncols - 1);
1508 af21bb7e 2023-04-12 mark goto done;
1511 af21bb7e 2023-04-12 mark int hline = 0;
1513 af21bb7e 2023-04-12 mark /* fullscreen or horizontal splitscreen */
1514 af21bb7e 2023-04-12 mark if ((view->child && view->child->begin_y) ||
1515 af21bb7e 2023-04-12 mark (view->parent && view->begin_y)) /* hsplit */
1516 af21bb7e 2023-04-12 mark hline = view->child ?
1517 af21bb7e 2023-04-12 mark view->child->begin_y : view->begin_y;
1519 af21bb7e 2023-04-12 mark for (i = 0; i < view->lines; i++) {
1520 af21bb7e 2023-04-12 mark if (hline && got_locale_is_utf8() && i == hline - 1) {
1523 af21bb7e 2023-04-12 mark /* ACS_HLINE writes out as 'q', overwrite it */
1524 af21bb7e 2023-04-12 mark for (c = 0; c < view->cols; ++c)
1525 af21bb7e 2023-04-12 mark fputc('-', f);
1526 af21bb7e 2023-04-12 mark fputc('\n', f);
1530 af21bb7e 2023-04-12 mark err = view_write_line(f, i, 0);
1532 af21bb7e 2023-04-12 mark goto done;
1537 af21bb7e 2023-04-12 mark if (f && fclose(f) == EOF && err == NULL)
1538 af21bb7e 2023-04-12 mark err = got_ferror(f, GOT_ERR_IO);
1539 af21bb7e 2023-04-12 mark return err;
1543 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1544 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1546 640cd7ff 2022-06-22 mark static int
1547 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1549 9b058f45 2022-06-30 mark struct tog_view *v = view;
1550 9b058f45 2022-06-30 mark int x, n = 0;
1552 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1553 9b058f45 2022-06-30 mark v = view->child;
1554 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1555 9b058f45 2022-06-30 mark v = view->parent;
1557 640cd7ff 2022-06-22 mark view->count = 0;
1558 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1559 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1560 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1561 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1562 9b058f45 2022-06-30 mark waddch(v->window, ':');
1565 9b058f45 2022-06-30 mark x = getcurx(v->window);
1566 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1567 9b058f45 2022-06-30 mark waddch(v->window, c);
1568 9b058f45 2022-06-30 mark wrefresh(v->window);
1572 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1573 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1575 640cd7ff 2022-06-22 mark if (n >= 9999999)
1576 640cd7ff 2022-06-22 mark n = 9999999;
1578 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1579 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1581 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1582 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1587 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1588 640cd7ff 2022-06-22 mark view->count = n;
1593 3f6c6614 2023-01-23 mark static void
1594 3f6c6614 2023-01-23 mark action_report(struct tog_view *view)
1596 3f6c6614 2023-01-23 mark struct tog_view *v = view;
1598 3f6c6614 2023-01-23 mark if (view_is_hsplit_top(view))
1599 3f6c6614 2023-01-23 mark v = view->child;
1600 3f6c6614 2023-01-23 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1601 3f6c6614 2023-01-23 mark v = view->parent;
1603 3f6c6614 2023-01-23 mark wmove(v->window, v->nlines - 1, 0);
1604 3f6c6614 2023-01-23 mark wclrtoeol(v->window);
1605 3f6c6614 2023-01-23 mark wprintw(v->window, ":%s", view->action);
1606 3f6c6614 2023-01-23 mark wrefresh(v->window);
1609 3f6c6614 2023-01-23 mark * Clear action status report. Only clear in blame view
1610 3f6c6614 2023-01-23 mark * once annotating is complete, otherwise it's too fast.
1612 3f6c6614 2023-01-23 mark if (view->type == TOG_VIEW_BLAME) {
1613 3f6c6614 2023-01-23 mark if (view->state.blame.blame_complete)
1614 3f6c6614 2023-01-23 mark view->action = NULL;
1616 3f6c6614 2023-01-23 mark view->action = NULL;
1620 af21bb7e 2023-04-12 mark * Read the next line from the test script and assign
1621 af21bb7e 2023-04-12 mark * key instruction to *ch. If at EOF, set the *done flag.
1623 0cf4efb1 2018-09-29 stsp static const struct got_error *
1624 af21bb7e 2023-04-12 mark tog_read_script_key(FILE *script, int *ch, int *done)
1626 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
1627 af21bb7e 2023-04-12 mark char *line = NULL;
1628 af21bb7e 2023-04-12 mark size_t linesz = 0;
1630 af21bb7e 2023-04-12 mark if (getline(&line, &linesz, script) == -1) {
1631 af21bb7e 2023-04-12 mark if (feof(script)) {
1632 af21bb7e 2023-04-12 mark *done = 1;
1633 af21bb7e 2023-04-12 mark goto done;
1635 af21bb7e 2023-04-12 mark err = got_ferror(script, GOT_ERR_IO);
1636 af21bb7e 2023-04-12 mark goto done;
1638 af21bb7e 2023-04-12 mark } else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1639 af21bb7e 2023-04-12 mark *ch = KEY_ENTER;
1640 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1641 af21bb7e 2023-04-12 mark *ch = KEY_RIGHT;
1642 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1643 af21bb7e 2023-04-12 mark *ch = KEY_LEFT;
1644 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1645 af21bb7e 2023-04-12 mark *ch = KEY_DOWN;
1646 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_UP", 6) == 0)
1647 af21bb7e 2023-04-12 mark *ch = KEY_UP;
1648 af21bb7e 2023-04-12 mark else if (strncasecmp(line, TOG_SCREEN_DUMP, TOG_SCREEN_DUMP_LEN) == 0)
1649 af21bb7e 2023-04-12 mark *ch = TOG_KEY_SCRDUMP;
1651 af21bb7e 2023-04-12 mark *ch = *line;
1654 af21bb7e 2023-04-12 mark free(line);
1655 af21bb7e 2023-04-12 mark return err;
1658 af21bb7e 2023-04-12 mark static const struct got_error *
1659 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1660 af21bb7e 2023-04-12 mark struct tog_view_list_head *views, struct tog_io *tog_io, int fast_refresh)
1662 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1663 669b5ffa 2018-10-07 stsp struct tog_view *v;
1664 1a76625f 2018-10-22 stsp int ch, errcode;
1666 e5a0f69f 2018-08-18 stsp *new = NULL;
1668 3f6c6614 2023-01-23 mark if (view->action)
1669 3f6c6614 2023-01-23 mark action_report(view);
1671 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1672 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1673 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1674 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1675 640cd7ff 2022-06-22 mark view->count = 0;
1678 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1679 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1680 82954512 2020-02-03 stsp if (errcode)
1681 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1682 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1683 3da8ef85 2021-09-21 stsp sched_yield();
1684 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1685 82954512 2020-02-03 stsp if (errcode)
1686 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1687 82954512 2020-02-03 stsp "pthread_mutex_lock");
1688 60493ae3 2019-06-20 stsp view->search_next(view);
1689 60493ae3 2019-06-20 stsp return NULL;
1692 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1693 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1694 1a76625f 2018-10-22 stsp if (errcode)
1695 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1697 af21bb7e 2023-04-12 mark if (tog_io && tog_io->f) {
1698 af21bb7e 2023-04-12 mark err = tog_read_script_key(tog_io->f, &ch, done);
1700 af21bb7e 2023-04-12 mark return err;
1701 af21bb7e 2023-04-12 mark } else if (view->count && --view->count) {
1703 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1704 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1705 af21bb7e 2023-04-12 mark /* let C-g or backspace abort unfinished count */
1706 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1707 a6d37fac 2022-07-03 mark view->count = 0;
1709 a6d37fac 2022-07-03 mark ch = view->ch;
1711 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1712 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1713 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1715 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1716 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1717 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1718 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1719 1a76625f 2018-10-22 stsp if (errcode)
1720 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1722 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1723 25791caa 2018-10-24 stsp tog_resizeterm();
1724 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1725 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1726 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1727 25791caa 2018-10-24 stsp err = view_resize(v);
1729 25791caa 2018-10-24 stsp return err;
1730 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1732 25791caa 2018-10-24 stsp return err;
1733 cdfcfb03 2020-12-06 stsp if (v->child) {
1734 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1736 cdfcfb03 2020-12-06 stsp return err;
1737 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1738 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1740 cdfcfb03 2020-12-06 stsp return err;
1741 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1742 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1744 3c1dfe12 2022-07-08 mark return err;
1750 e5a0f69f 2018-08-18 stsp switch (ch) {
1753 ec2a9698 2022-09-15 mark case KEY_F(1):
1754 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1755 ec2a9698 2022-09-15 mark err = view->reset(view);
1757 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1760 640cd7ff 2022-06-22 mark view->count = 0;
1761 1e37a5c2 2019-05-12 jcs if (view->child) {
1762 e78dc838 2020-12-04 stsp view->focussed = 0;
1763 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1764 e78dc838 2020-12-04 stsp view->focus_child = 1;
1765 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1766 e78dc838 2020-12-04 stsp view->focussed = 0;
1767 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1768 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1769 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1770 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1771 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1774 9b058f45 2022-06-30 mark return err;
1776 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1777 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1779 9b058f45 2022-06-30 mark return err;
1784 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1785 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1786 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1787 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1791 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1793 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1794 9970f7fc 2020-12-03 stsp view->dying = 1;
1800 640cd7ff 2022-06-22 mark view->count = 0;
1801 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1802 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1804 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1805 e78dc838 2020-12-04 stsp view->focussed = 0;
1806 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1807 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1809 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1811 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1815 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1816 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1818 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1819 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1820 e78dc838 2020-12-04 stsp view->focussed = 1;
1821 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1823 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1824 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1825 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1827 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1831 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1835 6fe51fee 2022-07-22 mark if (view->resize) {
1836 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1840 9b058f45 2022-06-30 mark if (view->parent)
1841 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1843 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1846 3c1dfe12 2022-07-08 mark view->count = 0;
1847 d2366e29 2022-07-07 mark err = switch_split(view);
1850 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1853 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1855 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1858 640cd7ff 2022-06-22 mark view->count = 0;
1859 60493ae3 2019-06-20 stsp if (view->search_start)
1860 602eda79 2023-02-09 mark view_search_start(view, fast_refresh);
1862 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1866 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1867 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1868 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1869 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1870 60493ae3 2019-06-20 stsp view->search_next(view);
1872 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1875 3f6c6614 2023-01-23 mark if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1876 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1877 3f6c6614 2023-01-23 mark view->action = "Patience diff algorithm";
1879 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1880 3f6c6614 2023-01-23 mark view->action = "Myers diff algorithm";
1882 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1883 917d79a7 2022-07-01 stsp if (v->reset) {
1884 917d79a7 2022-07-01 stsp err = v->reset(v);
1886 917d79a7 2022-07-01 stsp return err;
1888 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1889 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1891 917d79a7 2022-07-01 stsp return err;
1895 af21bb7e 2023-04-12 mark case TOG_KEY_SCRDUMP:
1896 af21bb7e 2023-04-12 mark err = screendump(view);
1899 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1903 e5a0f69f 2018-08-18 stsp return err;
1907 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1909 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1910 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1912 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1914 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1917 669b5ffa 2018-10-07 stsp return view->focussed;
1920 bcbd79e2 2018-08-19 stsp static const struct got_error *
1921 af21bb7e 2023-04-12 mark tog_io_close(struct tog_io *tog_io)
1923 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1925 af21bb7e 2023-04-12 mark if (tog_io->cin && fclose(tog_io->cin) == EOF)
1926 af21bb7e 2023-04-12 mark err = got_ferror(tog_io->cin, GOT_ERR_IO);
1927 af21bb7e 2023-04-12 mark if (tog_io->cout && fclose(tog_io->cout) == EOF && err == NULL)
1928 af21bb7e 2023-04-12 mark err = got_ferror(tog_io->cout, GOT_ERR_IO);
1929 af21bb7e 2023-04-12 mark if (tog_io->f && fclose(tog_io->f) == EOF && err == NULL)
1930 af21bb7e 2023-04-12 mark err = got_ferror(tog_io->f, GOT_ERR_IO);
1931 af21bb7e 2023-04-12 mark free(tog_io);
1932 af21bb7e 2023-04-12 mark tog_io = NULL;
1934 af21bb7e 2023-04-12 mark return err;
1937 af21bb7e 2023-04-12 mark static const struct got_error *
1938 af21bb7e 2023-04-12 mark view_loop(struct tog_view *view, struct tog_io *tog_io)
1940 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
1941 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1942 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1943 d2366e29 2022-07-07 mark char *mode;
1944 fd823528 2018-10-22 stsp int fast_refresh = 10;
1945 1a76625f 2018-10-22 stsp int done = 0, errcode;
1947 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1948 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1949 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1951 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1953 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1954 1a76625f 2018-10-22 stsp if (errcode)
1955 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1957 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1958 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1960 1004088d 2018-09-29 stsp view->focussed = 1;
1961 878940b7 2018-09-29 stsp err = view->show(view);
1963 0cf4efb1 2018-09-29 stsp return err;
1964 0cf4efb1 2018-09-29 stsp update_panels();
1965 0cf4efb1 2018-09-29 stsp doupdate();
1966 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1967 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1968 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1969 388b24d6 2023-01-23 mark if (fast_refresh && --fast_refresh == 0)
1970 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1972 af21bb7e 2023-04-12 mark err = view_input(&new_view, &done, view, &views, tog_io,
1973 af21bb7e 2023-04-12 mark fast_refresh);
1977 dc2c3344 2023-01-23 mark if (view->dying && view == TAILQ_FIRST(&views) &&
1978 dc2c3344 2023-01-23 mark TAILQ_NEXT(view, entry) == NULL)
1980 dc2c3344 2023-01-23 mark if (done) {
1981 dc2c3344 2023-01-23 mark struct tog_view *v;
1984 dc2c3344 2023-01-23 mark * When we quit, scroll the screen up a single line
1985 dc2c3344 2023-01-23 mark * so we don't lose any information.
1987 dc2c3344 2023-01-23 mark TAILQ_FOREACH(v, &views, entry) {
1988 dc2c3344 2023-01-23 mark wmove(v->window, 0, 0);
1989 dc2c3344 2023-01-23 mark wdeleteln(v->window);
1990 dc2c3344 2023-01-23 mark wnoutrefresh(v->window);
1991 dc2c3344 2023-01-23 mark if (v->child && !view_is_fullscreen(v)) {
1992 dc2c3344 2023-01-23 mark wmove(v->child->window, 0, 0);
1993 dc2c3344 2023-01-23 mark wdeleteln(v->child->window);
1994 dc2c3344 2023-01-23 mark wnoutrefresh(v->child->window);
1997 dc2c3344 2023-01-23 mark doupdate();
2000 9970f7fc 2020-12-03 stsp if (view->dying) {
2001 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2003 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2004 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2006 e78dc838 2020-12-04 stsp else if (view->parent)
2007 669b5ffa 2018-10-07 stsp prev = view->parent;
2009 e78dc838 2020-12-04 stsp if (view->parent) {
2010 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2011 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2012 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
2013 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
2014 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
2017 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
2018 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
2020 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2022 9970f7fc 2020-12-03 stsp err = view_close(view);
2024 e5a0f69f 2018-08-18 stsp goto done;
2026 e78dc838 2020-12-04 stsp view = NULL;
2027 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2028 e78dc838 2020-12-04 stsp if (v->focussed)
2031 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2032 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2034 e78dc838 2020-12-04 stsp view = prev;
2035 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2036 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2037 e78dc838 2020-12-04 stsp tog_view_list_head);
2039 e78dc838 2020-12-04 stsp if (view) {
2040 e78dc838 2020-12-04 stsp if (view->focus_child) {
2041 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2042 e78dc838 2020-12-04 stsp view = view->child;
2044 e78dc838 2020-12-04 stsp view->focussed = 1;
2048 bcbd79e2 2018-08-19 stsp if (new_view) {
2049 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2050 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2051 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2052 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2054 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2055 86c66b02 2018-10-18 stsp err = view_close(v);
2057 86c66b02 2018-10-18 stsp goto done;
2060 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2061 fed7eaa8 2018-10-24 stsp view = new_view;
2063 dc2c3344 2023-01-23 mark if (view && !done) {
2064 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2065 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2066 e78dc838 2020-12-04 stsp view = view->child;
2068 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2069 e78dc838 2020-12-04 stsp view = view->parent;
2071 e78dc838 2020-12-04 stsp show_panel(view->panel);
2072 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2073 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2074 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2075 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2077 1a76625f 2018-10-22 stsp goto done;
2079 669b5ffa 2018-10-07 stsp err = view->show(view);
2081 1a76625f 2018-10-22 stsp goto done;
2082 669b5ffa 2018-10-07 stsp if (view->child) {
2083 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2085 1a76625f 2018-10-22 stsp goto done;
2087 1a76625f 2018-10-22 stsp update_panels();
2088 1a76625f 2018-10-22 stsp doupdate();
2092 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2093 5629093a 2022-07-11 stsp const struct got_error *close_err;
2094 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2095 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2096 5629093a 2022-07-11 stsp close_err = view_close(view);
2097 5629093a 2022-07-11 stsp if (close_err && err == NULL)
2098 5629093a 2022-07-11 stsp err = close_err;
2101 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2102 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2103 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2105 e5a0f69f 2018-08-18 stsp return err;
2108 4ed7e80c 2018-05-20 stsp __dead static void
2109 9f7d7167 2018-04-29 stsp usage_log(void)
2112 c70c5802 2018-08-01 stsp fprintf(stderr,
2113 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2114 9f7d7167 2018-04-29 stsp getprogname());
2118 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2119 80ddbec8 2018-04-29 stsp static const struct got_error *
2120 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2122 00dfcb92 2018-06-11 stsp char *vis = NULL;
2123 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2125 963b370f 2018-05-20 stsp *ws = NULL;
2126 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2127 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2128 00dfcb92 2018-06-11 stsp int vislen;
2129 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2130 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2132 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2133 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2135 00dfcb92 2018-06-11 stsp return err;
2136 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2137 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2138 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2139 a7f50699 2018-06-11 stsp goto done;
2143 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2144 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2145 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2146 a7f50699 2018-06-11 stsp goto done;
2149 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2150 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2152 00dfcb92 2018-06-11 stsp free(vis);
2153 963b370f 2018-05-20 stsp if (err) {
2154 963b370f 2018-05-20 stsp free(*ws);
2155 963b370f 2018-05-20 stsp *ws = NULL;
2156 963b370f 2018-05-20 stsp *wlen = 0;
2158 963b370f 2018-05-20 stsp return err;
2161 145b6838 2022-06-16 stsp static const struct got_error *
2162 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
2164 145b6838 2022-06-16 stsp char *dst;
2165 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
2167 145b6838 2022-06-16 stsp *ptr = NULL;
2168 145b6838 2022-06-16 stsp n = len = strlen(src);
2169 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
2170 145b6838 2022-06-16 stsp if (dst == NULL)
2171 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
2173 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
2174 145b6838 2022-06-16 stsp const char c = src[idx];
2176 145b6838 2022-06-16 stsp if (c == '\t') {
2177 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
2180 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
2181 6e1c41ad 2022-06-16 mark if (p == NULL) {
2182 6e1c41ad 2022-06-16 mark free(dst);
2183 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
2188 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
2191 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
2195 145b6838 2022-06-16 stsp dst[sz] = '\0';
2196 145b6838 2022-06-16 stsp *ptr = dst;
2197 145b6838 2022-06-16 stsp return NULL;
2201 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
2202 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
2203 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
2207 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2209 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
2211 4e4a9ac8 2022-06-17 op if (n == 0) {
2212 4e4a9ac8 2022-06-17 op *rcol = cols;
2216 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
2217 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
2218 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2220 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
2222 4e4a9ac8 2022-06-17 op if (width == -1) {
2224 4e4a9ac8 2022-06-17 op wline[i] = L'.';
2227 4e4a9ac8 2022-06-17 op if (cols + width > n)
2229 4e4a9ac8 2022-06-17 op cols += width;
2232 4e4a9ac8 2022-06-17 op *rcol = cols;
2237 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
2238 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
2239 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2241 ccda2f4d 2022-06-16 stsp static const struct got_error *
2242 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2243 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2245 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2247 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2248 145b6838 2022-06-16 stsp char *exstr = NULL;
2249 963b370f 2018-05-20 stsp size_t wlen;
2250 4e4a9ac8 2022-06-17 op int i, scrollx;
2252 963b370f 2018-05-20 stsp *wlinep = NULL;
2253 b700b5d6 2018-07-10 stsp *widthp = 0;
2255 145b6838 2022-06-16 stsp if (expand) {
2256 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2258 145b6838 2022-06-16 stsp return err;
2261 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2262 145b6838 2022-06-16 stsp free(exstr);
2264 963b370f 2018-05-20 stsp return err;
2266 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2268 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2269 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2272 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2273 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2277 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2278 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2280 b700b5d6 2018-07-10 stsp if (widthp)
2281 b700b5d6 2018-07-10 stsp *widthp = cols;
2282 ccda2f4d 2022-06-16 stsp if (scrollxp)
2283 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2285 963b370f 2018-05-20 stsp free(wline);
2287 963b370f 2018-05-20 stsp *wlinep = wline;
2288 963b370f 2018-05-20 stsp return err;
2291 8b473291 2019-02-21 stsp static const struct got_error*
2292 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2293 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2295 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2296 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2298 8b473291 2019-02-21 stsp const char *name;
2300 8b473291 2019-02-21 stsp *refs_str = NULL;
2302 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2303 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2304 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2307 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2308 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2310 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2311 8b473291 2019-02-21 stsp name += 5;
2312 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2313 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2315 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2316 8b473291 2019-02-21 stsp name += 6;
2317 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2318 8b473291 2019-02-21 stsp name += 8;
2319 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2320 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2323 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2326 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2327 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2328 5d844a1e 2019-08-13 stsp if (err) {
2329 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2330 48cae60d 2020-09-22 stsp free(ref_id);
2333 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2334 5d844a1e 2019-08-13 stsp err = NULL;
2335 5d844a1e 2019-08-13 stsp tag = NULL;
2338 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2339 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2340 48cae60d 2020-09-22 stsp free(ref_id);
2342 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2343 52b5abe1 2019-08-13 stsp if (cmp != 0)
2345 8b473291 2019-02-21 stsp s = *refs_str;
2346 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2347 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2348 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2350 8b473291 2019-02-21 stsp *refs_str = NULL;
2356 8b473291 2019-02-21 stsp return err;
2359 963b370f 2018-05-20 stsp static const struct got_error *
2360 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2361 27a741e5 2019-09-11 stsp int col_tab_align)
2363 e6b8b890 2020-12-29 naddy char *smallerthan;
2365 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2366 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2367 5813d178 2019-03-09 stsp author = smallerthan + 1;
2368 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2369 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2370 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2373 5813d178 2019-03-09 stsp static const struct got_error *
2374 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2375 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2376 8fdc79fe 2020-12-01 naddy int author_display_cols)
2378 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2379 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2380 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2381 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2382 5813d178 2019-03-09 stsp char *author = NULL;
2383 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2384 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2385 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2386 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2387 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2388 ccb26ccd 2018-11-05 stsp struct tm tm;
2389 45d799e2 2018-12-23 stsp time_t committer_time;
2390 11b20872 2019-11-08 stsp struct tog_color *tc;
2392 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2393 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2394 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2395 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2396 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2398 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2399 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2401 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2402 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2404 11b20872 2019-11-08 stsp wattr_on(view->window,
2405 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2406 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2408 11b20872 2019-11-08 stsp wattr_off(view->window,
2409 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2410 27a741e5 2019-09-11 stsp col = limit;
2411 b39d25c7 2018-07-10 stsp if (col > avail)
2412 b39d25c7 2018-07-10 stsp goto done;
2414 6570a66d 2019-11-08 stsp if (avail >= 120) {
2415 6570a66d 2019-11-08 stsp char *id_str;
2416 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2418 6570a66d 2019-11-08 stsp goto done;
2419 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2421 11b20872 2019-11-08 stsp wattr_on(view->window,
2422 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2423 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2425 11b20872 2019-11-08 stsp wattr_off(view->window,
2426 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2427 6570a66d 2019-11-08 stsp free(id_str);
2429 6570a66d 2019-11-08 stsp if (col > avail)
2430 6570a66d 2019-11-08 stsp goto done;
2433 10aab77f 2022-07-19 op if (s->use_committer)
2434 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2436 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2437 5813d178 2019-03-09 stsp if (author == NULL) {
2438 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2439 80ddbec8 2018-04-29 stsp goto done;
2441 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2443 bb737323 2018-05-20 stsp goto done;
2444 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2446 11b20872 2019-11-08 stsp wattr_on(view->window,
2447 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2448 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2449 bb737323 2018-05-20 stsp col += author_width;
2450 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2451 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2453 bb737323 2018-05-20 stsp author_width++;
2456 f0f62654 2022-09-08 mark wattr_off(view->window,
2457 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2458 9c2eaf34 2018-05-20 stsp if (col > avail)
2459 9c2eaf34 2018-05-20 stsp goto done;
2461 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2463 6d9fbc00 2018-04-29 stsp goto done;
2464 bb737323 2018-05-20 stsp logmsg = logmsg0;
2465 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2467 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2468 bb737323 2018-05-20 stsp if (newline)
2469 bb737323 2018-05-20 stsp *newline = '\0';
2470 ccda2f4d 2022-06-16 stsp limit = avail - col;
2471 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2472 4d1f6af3 2022-06-17 op limit--; /* for the border */
2473 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2474 ccda2f4d 2022-06-16 stsp limit, col, 1);
2476 29688b02 2022-06-16 stsp goto done;
2477 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2478 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2479 27a741e5 2019-09-11 stsp while (col < avail) {
2480 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2484 80ddbec8 2018-04-29 stsp free(logmsg0);
2485 bb737323 2018-05-20 stsp free(wlogmsg);
2486 5813d178 2019-03-09 stsp free(author);
2487 bb737323 2018-05-20 stsp free(wauthor);
2488 80ddbec8 2018-04-29 stsp free(line);
2489 80ddbec8 2018-04-29 stsp return err;
2492 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2493 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2494 899d86c2 2018-05-10 stsp struct got_object_id *id)
2496 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2497 e15c42de 2022-09-05 op struct got_object_id *dup;
2499 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2500 80ddbec8 2018-04-29 stsp if (entry == NULL)
2501 899d86c2 2018-05-10 stsp return NULL;
2503 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2504 e15c42de 2022-09-05 op if (dup == NULL) {
2505 e15c42de 2022-09-05 op free(entry);
2506 e15c42de 2022-09-05 op return NULL;
2509 e15c42de 2022-09-05 op entry->id = dup;
2510 99db9666 2018-05-07 stsp entry->commit = commit;
2511 899d86c2 2018-05-10 stsp return entry;
2514 99db9666 2018-05-07 stsp static void
2515 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2517 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2519 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2520 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2521 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2522 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2523 e15c42de 2022-09-05 op free(entry->id);
2524 99db9666 2018-05-07 stsp free(entry);
2527 99db9666 2018-05-07 stsp static void
2528 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2530 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2531 99db9666 2018-05-07 stsp pop_commit(commits);
2534 c4972b91 2018-05-07 stsp static const struct got_error *
2535 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2536 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2538 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2539 13add988 2019-10-15 stsp regmatch_t regmatch;
2540 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2542 13add988 2019-10-15 stsp *have_match = 0;
2544 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2546 13add988 2019-10-15 stsp return err;
2548 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2550 13add988 2019-10-15 stsp goto done;
2552 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2553 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2554 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2555 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2556 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2557 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2558 13add988 2019-10-15 stsp *have_match = 1;
2560 13add988 2019-10-15 stsp free(id_str);
2561 13add988 2019-10-15 stsp free(logmsg);
2562 13add988 2019-10-15 stsp return err;
2565 13add988 2019-10-15 stsp static const struct got_error *
2566 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2568 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2571 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2572 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2573 1a76625f 2018-10-22 stsp * while updating the display.
2576 d9787ed8 2022-09-10 op struct got_object_id id;
2577 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2578 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2579 568eae95 2022-09-11 mark int limit_match = 0;
2580 1a76625f 2018-10-22 stsp int errcode;
2582 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2583 4e0d2870 2020-12-07 naddy NULL, NULL);
2587 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2590 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2591 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2592 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2596 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2597 1a76625f 2018-10-22 stsp if (errcode) {
2598 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2599 13add988 2019-10-15 stsp "pthread_mutex_lock");
2603 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2604 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2605 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2607 568eae95 2022-09-11 mark if (*a->limiting) {
2608 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2609 568eae95 2022-09-11 mark a->limit_regex);
2613 568eae95 2022-09-11 mark if (limit_match) {
2614 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2616 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2617 568eae95 2022-09-11 mark entry->commit, entry->id);
2618 568eae95 2022-09-11 mark if (matched == NULL) {
2619 568eae95 2022-09-11 mark err = got_error_from_errno(
2620 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2623 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2624 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2626 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2627 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2628 568eae95 2022-09-11 mark matched, entry);
2629 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2633 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2634 568eae95 2022-09-11 mark * have found a match, and that it should be
2635 568eae95 2022-09-11 mark * counted as a new entry for the view.
2637 568eae95 2022-09-11 mark a->limit_match = limit_match;
2640 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2641 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2642 7c1452c1 2020-03-26 stsp int have_match;
2643 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2647 568eae95 2022-09-11 mark if (*a->limiting) {
2648 568eae95 2022-09-11 mark if (limit_match && have_match)
2649 568eae95 2022-09-11 mark *a->search_next_done =
2650 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2651 568eae95 2022-09-11 mark } else if (have_match)
2652 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2655 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2656 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2657 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2658 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2661 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2663 9ba79e04 2018-06-11 stsp return err;
2666 2b779855 2020-12-05 naddy static void
2667 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2669 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2670 2b779855 2020-12-05 naddy int ncommits = 0;
2672 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2673 2b779855 2020-12-05 naddy while (entry) {
2674 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2675 2b779855 2020-12-05 naddy s->selected_entry = entry;
2678 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2679 2b779855 2020-12-05 naddy ncommits++;
2683 0553a4e3 2018-04-30 stsp static const struct got_error *
2684 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2686 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2687 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2688 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2689 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2690 60493ae3 2019-06-20 stsp int width;
2691 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2692 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2693 8b473291 2019-02-21 stsp char *refs_str = NULL;
2694 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2695 11b20872 2019-11-08 stsp struct tog_color *tc;
2696 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2698 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2699 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2701 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2702 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2703 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2704 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2706 ecb28ae0 2018-07-16 stsp return err;
2707 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2708 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2709 d2075bf3 2020-12-25 stsp if (refs) {
2710 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2711 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2713 d2075bf3 2020-12-25 stsp goto done;
2717 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2718 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2720 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2721 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2722 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2723 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2724 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2725 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2726 8f4ed634 2020-03-26 stsp goto done;
2729 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2730 568eae95 2022-09-11 mark const char *limit_str = NULL;
2732 f9686aa5 2020-03-27 stsp if (view->searching) {
2733 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2734 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2735 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2736 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2737 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2738 f9686aa5 2020-03-27 stsp search_str = "searching...";
2741 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2742 568eae95 2022-09-11 mark limit_str = "no matches found";
2744 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2745 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2746 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2747 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2748 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2749 8f4ed634 2020-03-26 stsp goto done;
2753 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2754 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2755 87411fa9 2022-06-16 stsp "........................................",
2756 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2757 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2758 1a76625f 2018-10-22 stsp header = NULL;
2759 1a76625f 2018-10-22 stsp goto done;
2761 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2762 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2763 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2764 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2765 1a76625f 2018-10-22 stsp header = NULL;
2766 1a76625f 2018-10-22 stsp goto done;
2768 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2770 1a76625f 2018-10-22 stsp goto done;
2772 2814baeb 2018-08-01 stsp werase(view->window);
2774 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2775 a3404814 2018-09-02 stsp wstandout(view->window);
2776 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2778 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2779 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2780 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2781 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2785 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2786 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2787 a3404814 2018-09-02 stsp wstandend(view->window);
2788 ecb28ae0 2018-07-16 stsp free(wline);
2789 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2790 1a76625f 2018-10-22 stsp goto done;
2792 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2793 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2794 5813d178 2019-03-09 stsp ncommits = 0;
2795 145b6838 2022-06-16 stsp view->maxx = 0;
2796 5813d178 2019-03-09 stsp while (entry) {
2797 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2798 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2799 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2800 5813d178 2019-03-09 stsp int width;
2801 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2803 10aab77f 2022-07-19 op if (s->use_committer)
2804 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2806 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2807 5813d178 2019-03-09 stsp if (author == NULL) {
2808 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2809 5813d178 2019-03-09 stsp goto done;
2811 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2812 27a741e5 2019-09-11 stsp date_display_cols);
2813 5813d178 2019-03-09 stsp if (author_cols < width)
2814 5813d178 2019-03-09 stsp author_cols = width;
2815 5813d178 2019-03-09 stsp free(wauthor);
2816 5813d178 2019-03-09 stsp free(author);
2817 a310d9c3 2022-07-21 florian if (err)
2818 a310d9c3 2022-07-21 florian goto done;
2819 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2821 145b6838 2022-06-16 stsp goto done;
2822 145b6838 2022-06-16 stsp msg = msg0;
2823 145b6838 2022-06-16 stsp while (*msg == '\n')
2825 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2826 29688b02 2022-06-16 stsp *eol = '\0';
2827 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2828 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2830 29688b02 2022-06-16 stsp goto done;
2831 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2832 145b6838 2022-06-16 stsp free(msg0);
2833 29688b02 2022-06-16 stsp free(wmsg);
2834 7ca04879 2019-10-19 stsp ncommits++;
2835 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2838 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2839 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2840 867c6645 2018-07-10 stsp ncommits = 0;
2841 899d86c2 2018-05-10 stsp while (entry) {
2842 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2844 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2845 2814baeb 2018-08-01 stsp wstandout(view->window);
2846 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2847 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2848 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2849 2814baeb 2018-08-01 stsp wstandend(view->window);
2851 60493ae3 2019-06-20 stsp goto done;
2852 0553a4e3 2018-04-30 stsp ncommits++;
2853 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2854 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2857 9b058f45 2022-06-30 mark view_border(view);
2859 1a76625f 2018-10-22 stsp free(id_str);
2860 8b473291 2019-02-21 stsp free(refs_str);
2861 1a76625f 2018-10-22 stsp free(ncommits_str);
2862 1a76625f 2018-10-22 stsp free(header);
2863 80ddbec8 2018-04-29 stsp return err;
2866 07b55e75 2018-05-10 stsp static void
2867 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2869 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2870 07b55e75 2018-05-10 stsp int nscrolled = 0;
2872 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2873 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2876 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2877 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2878 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2879 07b55e75 2018-05-10 stsp if (entry) {
2880 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2881 07b55e75 2018-05-10 stsp nscrolled++;
2886 aa075928 2018-05-10 stsp static const struct got_error *
2887 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2889 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2890 5e224a3e 2019-02-22 stsp int errcode;
2892 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2894 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2895 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2896 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2897 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2898 7aafa0d1 2019-02-22 stsp if (errcode)
2899 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2900 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2903 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2904 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2906 7c1452c1 2020-03-26 stsp if (!wait)
2909 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2910 ffe38506 2020-12-01 naddy show_log_view(view);
2911 7c1452c1 2020-03-26 stsp update_panels();
2912 7c1452c1 2020-03-26 stsp doupdate();
2914 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2915 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2916 82954512 2020-02-03 stsp if (errcode)
2917 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2918 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2920 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2921 ffe38506 2020-12-01 naddy show_log_view(view);
2922 7c1452c1 2020-03-26 stsp update_panels();
2923 7c1452c1 2020-03-26 stsp doupdate();
2926 5e224a3e 2019-02-22 stsp return NULL;
2929 5e224a3e 2019-02-22 stsp static const struct got_error *
2930 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2932 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2933 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2935 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2936 2525dccb 2022-07-13 mark return NULL;
2938 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2939 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2940 9b058f45 2022-06-30 mark view->nscrolled = 0;
2942 9b058f45 2022-06-30 mark return err;
2945 9b058f45 2022-06-30 mark static const struct got_error *
2946 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2948 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2949 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2950 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2951 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2953 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2954 5e224a3e 2019-02-22 stsp return NULL;
2956 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2957 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2958 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2960 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2962 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2963 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2964 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2966 5e224a3e 2019-02-22 stsp return err;
2970 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2971 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2974 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2975 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
2977 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2978 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2980 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2981 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2983 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2984 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2986 9b058f45 2022-06-30 mark view->nscrolled = 0;
2988 dd0a52c1 2018-05-20 stsp return err;
2991 cd0acaa7 2018-05-20 stsp static const struct got_error *
2992 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2993 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2994 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2996 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2997 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2998 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3000 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3001 15a94983 2018-12-23 stsp if (diff_view == NULL)
3002 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3004 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3005 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3006 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3007 e5a0f69f 2018-08-18 stsp if (err == NULL)
3008 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3009 cd0acaa7 2018-05-20 stsp return err;
3012 80ddbec8 2018-04-29 stsp static const struct got_error *
3013 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3014 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3016 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3018 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3019 941e9f74 2019-05-21 stsp if (parent == NULL)
3020 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3022 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3023 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3024 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3025 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3026 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3027 941e9f74 2019-05-21 stsp s->tree = subtree;
3028 941e9f74 2019-05-21 stsp s->selected = 0;
3029 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3030 941e9f74 2019-05-21 stsp return NULL;
3033 941e9f74 2019-05-21 stsp static const struct got_error *
3034 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3035 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
3037 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3038 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3039 941e9f74 2019-05-21 stsp const char *p;
3040 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3042 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3044 941e9f74 2019-05-21 stsp while (*p) {
3045 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3046 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3047 56e0773d 2019-11-28 stsp char *te_name;
3049 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3052 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3053 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3054 941e9f74 2019-05-21 stsp if (slash == NULL)
3055 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3057 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3058 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3059 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3062 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3063 56e0773d 2019-11-28 stsp if (te == NULL) {
3064 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3065 56e0773d 2019-11-28 stsp free(te_name);
3068 56e0773d 2019-11-28 stsp free(te_name);
3069 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3071 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3072 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3074 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3075 941e9f74 2019-05-21 stsp if (slash)
3076 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3078 941e9f74 2019-05-21 stsp subpath = strdup(path);
3079 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3080 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3084 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
3089 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3090 941e9f74 2019-05-21 stsp free(tree_id);
3094 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3095 941e9f74 2019-05-21 stsp if (err) {
3096 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3099 941e9f74 2019-05-21 stsp if (slash == NULL)
3101 941e9f74 2019-05-21 stsp free(subpath);
3102 941e9f74 2019-05-21 stsp subpath = NULL;
3103 941e9f74 2019-05-21 stsp p = slash;
3106 941e9f74 2019-05-21 stsp free(subpath);
3107 1a76625f 2018-10-22 stsp return err;
3110 61266923 2020-01-14 stsp static const struct got_error *
3111 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3112 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3113 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3115 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3116 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3117 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3119 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3120 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3121 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3123 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3125 55cccc34 2020-02-20 stsp return err;
3126 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3128 55cccc34 2020-02-20 stsp *new_view = tree_view;
3130 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3131 55cccc34 2020-02-20 stsp return NULL;
3133 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
3136 55cccc34 2020-02-20 stsp static const struct got_error *
3137 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3139 61266923 2020-01-14 stsp sigset_t sigset;
3140 61266923 2020-01-14 stsp int errcode;
3142 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3143 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3145 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3146 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3147 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3148 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3149 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
3150 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
3151 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3152 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
3153 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
3155 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3156 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3157 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3159 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3160 61266923 2020-01-14 stsp if (errcode)
3161 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3163 61266923 2020-01-14 stsp return NULL;
3166 1a76625f 2018-10-22 stsp static void *
3167 1a76625f 2018-10-22 stsp log_thread(void *arg)
3169 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3170 1a76625f 2018-10-22 stsp int errcode = 0;
3171 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3172 1a76625f 2018-10-22 stsp int done = 0;
3175 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
3176 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
3178 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
3179 5629093a 2022-07-11 stsp if (errcode) {
3180 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
3181 61266923 2020-01-14 stsp return (void *)err;
3184 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
3185 5629093a 2022-07-11 stsp if (err) {
3186 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3187 5629093a 2022-07-11 stsp goto done;
3190 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
3191 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3192 5629093a 2022-07-11 stsp if (errcode) {
3193 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
3194 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
3195 5629093a 2022-07-11 stsp goto done;
3197 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3198 1a76625f 2018-10-22 stsp if (err) {
3199 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3200 5629093a 2022-07-11 stsp goto done;
3201 1a76625f 2018-10-22 stsp err = NULL;
3203 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
3204 568eae95 2022-09-11 mark if (*a->limiting) {
3205 568eae95 2022-09-11 mark if (a->limit_match)
3206 568eae95 2022-09-11 mark a->commits_needed--;
3208 568eae95 2022-09-11 mark a->commits_needed--;
3211 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3212 3abe8080 2019-04-10 stsp if (errcode) {
3213 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3214 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3215 5629093a 2022-07-11 stsp goto done;
3216 3abe8080 2019-04-10 stsp } else if (*a->quit)
3218 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
3219 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3220 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
3221 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3222 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
3223 568eae95 2022-09-11 mark *a->first_displayed_entry =
3224 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
3225 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
3228 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3229 7c1452c1 2020-03-26 stsp if (errcode) {
3230 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3231 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3232 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3233 5629093a 2022-07-11 stsp goto done;
3237 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3239 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3240 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3241 7c1452c1 2020-03-26 stsp &tog_mutex);
3242 5629093a 2022-07-11 stsp if (errcode) {
3243 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3244 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3245 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3246 5629093a 2022-07-11 stsp goto done;
3248 21355643 2020-12-06 stsp if (*a->quit)
3253 3abe8080 2019-04-10 stsp a->log_complete = 1;
3254 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3255 5629093a 2022-07-11 stsp if (errcode)
3256 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3258 5629093a 2022-07-11 stsp if (err) {
3259 5629093a 2022-07-11 stsp tog_thread_error = 1;
3260 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3262 1a76625f 2018-10-22 stsp return (void *)err;
3265 1a76625f 2018-10-22 stsp static const struct got_error *
3266 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3268 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3269 1a76625f 2018-10-22 stsp int errcode;
3271 1a76625f 2018-10-22 stsp if (s->thread) {
3272 1a76625f 2018-10-22 stsp s->quit = 1;
3273 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3274 1a76625f 2018-10-22 stsp if (errcode)
3275 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3276 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3277 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3278 1a76625f 2018-10-22 stsp if (errcode)
3279 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3280 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3281 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3282 1a76625f 2018-10-22 stsp if (errcode)
3283 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3284 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3285 1a76625f 2018-10-22 stsp if (errcode)
3286 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3287 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3288 1a76625f 2018-10-22 stsp s->thread = NULL;
3291 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3292 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3293 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3296 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3297 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3298 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3299 74467cc8 2022-06-15 stsp if (err == NULL)
3300 74467cc8 2022-06-15 stsp err = pack_err;
3301 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3304 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3305 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3306 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3309 5629093a 2022-07-11 stsp return err ? err : thread_err;
3312 9343a5fb 2018-06-23 stsp static const struct got_error *
3313 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3315 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3316 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3317 276b94a1 2020-11-13 naddy int errcode;
3319 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3321 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3322 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3323 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3325 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3326 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3327 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3329 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3330 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3331 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3332 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3333 1a76625f 2018-10-22 stsp free(s->start_id);
3334 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3335 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3336 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3337 1a76625f 2018-10-22 stsp return err;
3341 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3342 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3343 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3344 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3345 568eae95 2022-09-11 mark * works with very slight change.
3347 1a76625f 2018-10-22 stsp static const struct got_error *
3348 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3350 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3351 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3352 568eae95 2022-09-11 mark struct tog_view *v = view;
3353 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3354 568eae95 2022-09-11 mark char pattern[1024];
3357 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3358 568eae95 2022-09-11 mark v = view->child;
3359 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3360 568eae95 2022-09-11 mark v = view->parent;
3362 568eae95 2022-09-11 mark /* Get the pattern */
3363 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3364 568eae95 2022-09-11 mark wclrtoeol(v->window);
3365 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3366 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3367 568eae95 2022-09-11 mark nocbreak();
3369 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3372 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3373 568eae95 2022-09-11 mark if (ret == ERR)
3374 568eae95 2022-09-11 mark return NULL;
3376 568eae95 2022-09-11 mark if (*pattern == '\0') {
3378 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3379 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3381 568eae95 2022-09-11 mark if (!s->limit_view)
3382 568eae95 2022-09-11 mark return NULL;
3385 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3386 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3387 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3388 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3389 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3390 568eae95 2022-09-11 mark * the queue.
3392 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3393 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3394 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3395 568eae95 2022-09-11 mark s->selected = 0;
3396 568eae95 2022-09-11 mark s->limit_view = 0;
3398 568eae95 2022-09-11 mark return NULL;
3401 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3402 568eae95 2022-09-11 mark return NULL;
3404 568eae95 2022-09-11 mark s->limit_view = 1;
3406 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3407 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3408 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3409 568eae95 2022-09-11 mark s->selected_entry = NULL;
3410 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3412 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3413 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3414 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3416 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3417 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3418 568eae95 2022-09-11 mark int have_match = 0;
3420 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3421 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3423 568eae95 2022-09-11 mark return err;
3425 568eae95 2022-09-11 mark if (have_match) {
3426 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3428 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3429 568eae95 2022-09-11 mark entry->id);
3430 568eae95 2022-09-11 mark if (matched == NULL) {
3431 568eae95 2022-09-11 mark err = got_error_from_errno(
3432 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3435 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3436 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3438 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3439 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3440 568eae95 2022-09-11 mark matched, entry);
3441 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3445 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3446 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3447 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3448 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3449 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3450 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3452 568eae95 2022-09-11 mark return err;
3455 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3456 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3457 568eae95 2022-09-11 mark s->selected = 0;
3459 568eae95 2022-09-11 mark return NULL;
3462 568eae95 2022-09-11 mark static const struct got_error *
3463 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3465 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3467 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3468 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3469 60493ae3 2019-06-20 stsp return NULL;
3472 60493ae3 2019-06-20 stsp static const struct got_error *
3473 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3475 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3476 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3477 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3479 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3480 f9686aa5 2020-03-27 stsp show_log_view(view);
3481 f9686aa5 2020-03-27 stsp update_panels();
3482 f9686aa5 2020-03-27 stsp doupdate();
3484 96e2b566 2019-07-08 stsp if (s->search_entry) {
3485 13add988 2019-10-15 stsp int errcode, ch;
3486 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3487 13add988 2019-10-15 stsp if (errcode)
3488 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3489 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3490 13add988 2019-10-15 stsp ch = wgetch(view->window);
3491 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3492 13add988 2019-10-15 stsp if (errcode)
3493 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3494 13add988 2019-10-15 stsp "pthread_mutex_lock");
3495 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3496 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3497 678cbce5 2019-07-28 stsp return NULL;
3499 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3500 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3502 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3503 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3504 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3506 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3507 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3508 f704b35c 2022-06-18 stsp * might have changed.
3510 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3511 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3513 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3516 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3519 60493ae3 2019-06-20 stsp while (1) {
3520 13add988 2019-10-15 stsp int have_match = 0;
3522 60493ae3 2019-06-20 stsp if (entry == NULL) {
3523 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3524 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3525 f9967bca 2020-03-27 stsp view->search_next_done =
3526 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3527 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3528 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3529 f801134a 2019-06-25 stsp return NULL;
3532 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3533 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3534 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3536 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3537 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3540 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3541 13add988 2019-10-15 stsp &view->regex);
3544 13add988 2019-10-15 stsp if (have_match) {
3545 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3546 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3550 96e2b566 2019-07-08 stsp s->search_entry = entry;
3551 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3552 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3554 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3557 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3558 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3559 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3560 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3562 60493ae3 2019-06-20 stsp return err;
3565 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3566 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3568 60493ae3 2019-06-20 stsp return err;
3573 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3575 60493ae3 2019-06-20 stsp return NULL;
3578 60493ae3 2019-06-20 stsp static const struct got_error *
3579 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3580 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3581 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3583 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3584 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3585 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3586 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3587 1a76625f 2018-10-22 stsp int errcode;
3589 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3590 f135c941 2020-02-20 stsp free(s->in_repo_path);
3591 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3592 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3593 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3596 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3597 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3598 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3599 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3601 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3602 568eae95 2022-09-11 mark s->limit_view = 0;
3603 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3605 fb2756b9 2018-08-04 stsp s->repo = repo;
3606 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3607 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3608 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3609 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3610 9cd7cbd1 2020-12-07 stsp goto done;
3613 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3614 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3615 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3616 5036bf37 2018-09-24 stsp goto done;
3618 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3619 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3621 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3622 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3623 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3624 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3626 11b20872 2019-11-08 stsp goto done;
3627 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3628 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3629 11b20872 2019-11-08 stsp if (err) {
3630 11b20872 2019-11-08 stsp free_colors(&s->colors);
3631 11b20872 2019-11-08 stsp goto done;
3633 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3634 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3635 11b20872 2019-11-08 stsp if (err) {
3636 11b20872 2019-11-08 stsp free_colors(&s->colors);
3637 11b20872 2019-11-08 stsp goto done;
3641 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3642 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3643 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3644 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3645 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3646 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3648 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3649 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3651 74467cc8 2022-06-15 stsp goto done;
3653 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3654 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3655 0ae84acc 2022-06-15 tracey if (err)
3656 0ae84acc 2022-06-15 tracey goto done;
3657 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3658 b672a97a 2020-01-27 stsp !s->log_branches);
3660 1a76625f 2018-10-22 stsp goto done;
3661 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3662 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3664 c5b78334 2020-01-12 stsp goto done;
3666 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3667 1a76625f 2018-10-22 stsp if (errcode) {
3668 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3669 1a76625f 2018-10-22 stsp goto done;
3671 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3672 7c1452c1 2020-03-26 stsp if (errcode) {
3673 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3674 7c1452c1 2020-03-26 stsp goto done;
3677 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3678 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3679 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3680 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3681 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3682 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3683 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3684 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3685 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3686 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3687 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3688 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3689 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3690 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3691 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3692 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3693 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3696 1a76625f 2018-10-22 stsp close_log_view(view);
3697 ba4f502b 2018-08-04 stsp return err;
3700 e5a0f69f 2018-08-18 stsp static const struct got_error *
3701 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3703 f2f6d207 2020-11-24 stsp const struct got_error *err;
3704 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3706 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3707 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3708 2b380cc8 2018-10-24 stsp &s->thread_args);
3709 2b380cc8 2018-10-24 stsp if (errcode)
3710 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3711 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3712 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3714 f2f6d207 2020-11-24 stsp return err;
3718 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3721 b880cc75 2022-06-30 mark static void
3722 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3724 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3726 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3728 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3729 568eae95 2022-09-11 mark view->count = 0;
3731 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3733 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3735 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3736 b880cc75 2022-06-30 mark --s->selected;
3738 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3740 b880cc75 2022-06-30 mark select_commit(s);
3744 b880cc75 2022-06-30 mark static const struct got_error *
3745 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3747 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3748 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3749 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3751 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3752 568eae95 2022-09-11 mark return NULL;
3754 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3755 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3756 b880cc75 2022-06-30 mark return NULL;
3758 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3759 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3761 631e7531 2022-08-12 mark if (!page) {
3762 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3763 b880cc75 2022-06-30 mark ++s->selected;
3765 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3766 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3767 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3770 631e7531 2022-08-12 mark s->selected = 0;
3771 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3772 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3773 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3774 631e7531 2022-08-12 mark if (entry == NULL)
3776 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3777 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3779 631e7531 2022-08-12 mark if (n > 0)
3780 631e7531 2022-08-12 mark s->selected = n - 1;
3782 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3783 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3784 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3785 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3787 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3790 b880cc75 2022-06-30 mark return err;
3793 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3794 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3796 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3797 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3798 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3799 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3802 b880cc75 2022-06-30 mark select_commit(s);
3804 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3805 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3806 b880cc75 2022-06-30 mark view->count = 0;
3808 b880cc75 2022-06-30 mark return NULL;
3811 9b058f45 2022-06-30 mark static void
3812 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3817 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3818 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3819 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3820 7532ccda 2022-07-11 mark else if (view->resized_y)
3821 7532ccda 2022-07-11 mark *y = view->resized_y;
3823 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3824 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3825 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3826 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3827 7532ccda 2022-07-11 mark else if (view->resized_x)
3828 7532ccda 2022-07-11 mark *x = view->resized_x;
3830 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3834 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3835 e5a0f69f 2018-08-18 stsp static const struct got_error *
3836 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3838 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3840 9b058f45 2022-06-30 mark view->nlines = y;
3841 d2366e29 2022-07-07 mark view->ncols = COLS;
3842 9b058f45 2022-06-30 mark err = view_resize(view);
3844 9b058f45 2022-06-30 mark return err;
3846 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3848 9b058f45 2022-06-30 mark return err;
3851 94b80cfa 2022-08-01 mark static const struct got_error *
3852 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3854 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3855 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3856 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3858 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3859 374f69dd 2022-08-13 stsp return NULL;
3861 94b80cfa 2022-08-01 mark g = view->gline;
3862 94b80cfa 2022-08-01 mark view->gline = 0;
3864 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3865 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3866 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3867 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3868 94b80cfa 2022-08-01 mark select_commit(s);
3869 94b80cfa 2022-08-01 mark return NULL;
3872 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3873 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3874 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3875 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3876 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3878 94b80cfa 2022-08-01 mark return err;
3879 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3880 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3882 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3883 94b80cfa 2022-08-01 mark s->selected = g - 1;
3885 94b80cfa 2022-08-01 mark select_commit(s);
3886 94b80cfa 2022-08-01 mark return NULL;
3890 d627976f 2023-02-01 mark static void
3891 d627976f 2023-02-01 mark horizontal_scroll_input(struct tog_view *view, int ch)
3894 d627976f 2023-02-01 mark switch (ch) {
3895 d627976f 2023-02-01 mark case KEY_LEFT:
3897 d627976f 2023-02-01 mark view->x -= MIN(view->x, 2);
3898 d627976f 2023-02-01 mark if (view->x <= 0)
3899 d627976f 2023-02-01 mark view->count = 0;
3901 d627976f 2023-02-01 mark case KEY_RIGHT:
3903 d627976f 2023-02-01 mark if (view->x + view->ncols / 2 < view->maxx)
3904 d627976f 2023-02-01 mark view->x += 2;
3906 d627976f 2023-02-01 mark view->count = 0;
3909 d627976f 2023-02-01 mark view->x = 0;
3912 d627976f 2023-02-01 mark view->x = MAX(view->maxx - view->ncols / 2, 0);
3913 d627976f 2023-02-01 mark view->count = 0;
3920 9b058f45 2022-06-30 mark static const struct got_error *
3921 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3923 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3924 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3925 631e7531 2022-08-12 mark int eos, nscroll;
3927 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3928 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3929 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3930 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3931 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3932 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3935 11edf34c 2022-08-12 mark return err;
3938 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3939 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3940 0dca135e 2022-06-30 mark --eos; /* border */
3942 94b80cfa 2022-08-01 mark if (view->gline)
3943 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3945 528dedf3 2021-08-30 stsp switch (ch) {
3947 568eae95 2022-09-11 mark err = limit_log_view(view);
3950 1e37a5c2 2019-05-12 jcs s->quit = 1;
3954 145b6838 2022-06-16 stsp case KEY_RIGHT:
3956 145b6838 2022-06-16 stsp case KEY_LEFT:
3958 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
3961 1e37a5c2 2019-05-12 jcs case KEY_UP:
3964 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3965 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3969 912a3f79 2021-08-30 j case KEY_HOME:
3970 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3971 640cd7ff 2022-06-22 mark view->count = 0;
3973 83cc4199 2022-06-13 stsp case CTRL('u'):
3975 83cc4199 2022-06-13 stsp nscroll /= 2;
3976 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3977 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3978 a4292ac5 2019-05-12 jcs case CTRL('b'):
3980 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3983 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3986 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3987 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3990 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3991 3f6c6614 2023-01-23 mark view->action = s->use_committer ?
3992 3f6c6614 2023-01-23 mark "show committer" : "show commit author";
3996 912a3f79 2021-08-30 j case KEY_END: {
3997 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3998 912a3f79 2021-08-30 j * traverse them all. */
3999 640cd7ff 2022-06-22 mark view->count = 0;
4000 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
4001 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
4002 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4003 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
4004 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
4007 80b7e8da 2022-06-11 stsp case CTRL('d'):
4009 83cc4199 2022-06-13 stsp nscroll /= 2;
4010 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4011 83cc4199 2022-06-13 stsp case KEY_NPAGE:
4012 61417565 2022-06-20 mark case CTRL('f'):
4013 48bb96f0 2022-06-20 naddy case 'f':
4015 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
4017 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4018 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4019 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4020 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
4021 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
4022 2b779855 2020-12-05 naddy select_commit(s);
4023 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
4024 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4025 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4026 568eae95 2022-09-11 mark s->commits->ncommits;
4027 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4030 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4031 49b24ee5 2022-07-03 mark case '\r':
4032 640cd7ff 2022-06-22 mark view->count = 0;
4033 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4035 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4038 640cd7ff 2022-06-22 mark view->count = 0;
4039 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4041 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
4043 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4044 21355643 2020-12-06 stsp case CTRL('l'):
4046 640cd7ff 2022-06-22 mark view->count = 0;
4047 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4048 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4050 21355643 2020-12-06 stsp err = stop_log_thread(s);
4052 74cfe85e 2020-10-20 stsp return err;
4053 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4054 21355643 2020-12-06 stsp char *parent_path;
4055 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4057 21355643 2020-12-06 stsp return err;
4058 21355643 2020-12-06 stsp free(s->in_repo_path);
4059 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4060 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4061 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4062 21355643 2020-12-06 stsp struct got_object_id *start_id;
4063 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4064 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4065 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4066 ea371198 2022-11-17 stsp if (err) {
4067 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
4068 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
4069 ea371198 2022-11-17 stsp return err;
4070 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
4071 ea371198 2022-11-17 stsp free(s->head_ref_name);
4072 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
4073 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
4074 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4075 ea371198 2022-11-17 stsp &tog_refs, s->repo);
4077 ea371198 2022-11-17 stsp return err;
4079 21355643 2020-12-06 stsp free(s->start_id);
4080 21355643 2020-12-06 stsp s->start_id = start_id;
4081 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4082 21355643 2020-12-06 stsp } else /* 'B' */
4083 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4085 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
4086 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4088 b0dd8d27 2022-06-16 stsp return err;
4090 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
4091 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
4092 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
4094 21355643 2020-12-06 stsp return err;
4095 51a10b52 2020-12-26 stsp tog_free_refs();
4096 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
4098 ca51c541 2020-12-07 stsp return err;
4099 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4100 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4102 d01904d4 2019-06-25 stsp return err;
4103 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4104 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4106 b672a97a 2020-01-27 stsp return err;
4107 568eae95 2022-09-11 mark free_commits(&s->real_commits);
4108 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
4109 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4110 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4111 21355643 2020-12-06 stsp s->selected_entry = NULL;
4112 21355643 2020-12-06 stsp s->selected = 0;
4113 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4114 21355643 2020-12-06 stsp s->quit = 0;
4115 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
4116 dfee752e 2022-06-18 op s->matched_entry = NULL;
4117 dfee752e 2022-06-18 op s->search_entry = NULL;
4118 01a7bcaf 2022-07-22 mark view->offset = 0;
4121 640cd7ff 2022-06-22 mark view->count = 0;
4122 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
4125 640cd7ff 2022-06-22 mark view->count = 0;
4129 80ddbec8 2018-04-29 stsp return err;
4132 4ed7e80c 2018-05-20 stsp static const struct got_error *
4133 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4135 c2db6724 2019-01-04 stsp const struct got_error *error;
4137 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4138 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4139 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4141 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4142 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4144 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4145 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4147 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4148 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4150 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4151 c2db6724 2019-01-04 stsp if (error != NULL)
4152 c2db6724 2019-01-04 stsp return error;
4154 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4155 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4157 c2db6724 2019-01-04 stsp return NULL;
4160 af21bb7e 2023-04-12 mark static const struct got_error *
4161 af21bb7e 2023-04-12 mark init_mock_term(struct tog_io **tog_io, const char *test_script_path)
4163 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
4164 af21bb7e 2023-04-12 mark struct tog_io *io;
4166 af21bb7e 2023-04-12 mark if (*tog_io)
4167 af21bb7e 2023-04-12 mark *tog_io = NULL;
4169 af21bb7e 2023-04-12 mark if (test_script_path == NULL || *test_script_path == '\0')
4170 af21bb7e 2023-04-12 mark return got_error_msg(GOT_ERR_IO, "GOT_TOG_TEST not defined");
4172 af21bb7e 2023-04-12 mark io = calloc(1, sizeof(*io));
4173 af21bb7e 2023-04-12 mark if (io == NULL)
4174 af21bb7e 2023-04-12 mark return got_error_from_errno("calloc");
4176 af21bb7e 2023-04-12 mark io->f = fopen(test_script_path, "re");
4177 af21bb7e 2023-04-12 mark if (io->f == NULL) {
4178 af21bb7e 2023-04-12 mark err = got_error_from_errno_fmt("fopen: %s",
4179 af21bb7e 2023-04-12 mark test_script_path);
4180 af21bb7e 2023-04-12 mark goto done;
4183 af21bb7e 2023-04-12 mark /* test mode, we don't want any output */
4184 af21bb7e 2023-04-12 mark io->cout = fopen("/dev/null", "w+");
4185 af21bb7e 2023-04-12 mark if (io->cout == NULL) {
4186 af21bb7e 2023-04-12 mark err = got_error_from_errno("fopen: /dev/null");
4187 af21bb7e 2023-04-12 mark goto done;
4190 af21bb7e 2023-04-12 mark io->cin = fopen("/dev/tty", "r+");
4191 af21bb7e 2023-04-12 mark if (io->cin == NULL) {
4192 af21bb7e 2023-04-12 mark err = got_error_from_errno("fopen: /dev/tty");
4193 af21bb7e 2023-04-12 mark goto done;
4196 af21bb7e 2023-04-12 mark if (fseeko(io->f, 0L, SEEK_SET) == -1) {
4197 af21bb7e 2023-04-12 mark err = got_error_from_errno("fseeko");
4198 af21bb7e 2023-04-12 mark goto done;
4202 af21bb7e 2023-04-12 mark * XXX Perhaps we should define "xterm" as the terminal
4203 af21bb7e 2023-04-12 mark * type for standardised testing instead of using $TERM?
4205 af21bb7e 2023-04-12 mark if (newterm(NULL, io->cout, io->cin) == NULL)
4206 af21bb7e 2023-04-12 mark err = got_error_msg(GOT_ERR_IO,
4207 af21bb7e 2023-04-12 mark "newterm: failed to initialise curses");
4210 af21bb7e 2023-04-12 mark tog_io_close(io);
4212 af21bb7e 2023-04-12 mark *tog_io = io;
4213 af21bb7e 2023-04-12 mark return err;
4216 af21bb7e 2023-04-12 mark static const struct got_error *
4217 af21bb7e 2023-04-12 mark init_curses(struct tog_io **tog_io)
4219 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
4220 af21bb7e 2023-04-12 mark const char *test_script_path;
4223 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
4224 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
4225 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
4227 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
4228 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
4229 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
4230 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
4231 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
4233 af21bb7e 2023-04-12 mark test_script_path = getenv("GOT_TOG_TEST");
4234 af21bb7e 2023-04-12 mark if (test_script_path != NULL) {
4235 af21bb7e 2023-04-12 mark err = init_mock_term(tog_io, test_script_path);
4237 af21bb7e 2023-04-12 mark return err;
4239 af21bb7e 2023-04-12 mark initscr();
4242 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
4245 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4246 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4247 a915003a 2019-02-05 stsp curs_set(0);
4248 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4249 6d17833f 2019-11-08 stsp start_color();
4250 6d17833f 2019-11-08 stsp use_default_colors();
4253 af21bb7e 2023-04-12 mark return NULL;
4256 c2db6724 2019-01-04 stsp static const struct got_error *
4257 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4258 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4260 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4262 f135c941 2020-02-20 stsp if (argc == 0) {
4263 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4264 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4265 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4266 f135c941 2020-02-20 stsp return NULL;
4269 f135c941 2020-02-20 stsp if (worktree) {
4270 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4273 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4275 f135c941 2020-02-20 stsp return err;
4276 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4277 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4278 bfd61697 2020-11-14 stsp p) == -1) {
4279 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4280 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4284 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4286 f135c941 2020-02-20 stsp return err;
4289 f135c941 2020-02-20 stsp static const struct got_error *
4290 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4292 af21bb7e 2023-04-12 mark const struct got_error *io_err, *error;
4293 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4294 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4295 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4296 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4297 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4298 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4299 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4300 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4301 04cc582a 2018-08-01 stsp struct tog_view *view;
4302 af21bb7e 2023-04-12 mark struct tog_io *tog_io = NULL;
4303 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4305 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4306 80ddbec8 2018-04-29 stsp switch (ch) {
4308 b672a97a 2020-01-27 stsp log_branches = 1;
4311 80ddbec8 2018-04-29 stsp start_commit = optarg;
4314 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4315 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4316 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4320 17020d27 2019-03-07 stsp usage_log();
4321 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4325 80ddbec8 2018-04-29 stsp argc -= optind;
4326 80ddbec8 2018-04-29 stsp argv += optind;
4328 f135c941 2020-02-20 stsp if (argc > 1)
4329 f135c941 2020-02-20 stsp usage_log();
4331 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4332 0ae84acc 2022-06-15 tracey if (error != NULL)
4333 0ae84acc 2022-06-15 tracey goto done;
4335 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4336 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4337 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4338 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4339 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4340 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4341 c156c7a4 2020-12-18 stsp goto done;
4342 a1fbf39a 2019-08-11 stsp if (worktree)
4343 6962eb72 2020-02-20 stsp repo_path =
4344 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4346 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4347 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4348 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4349 c156c7a4 2020-12-18 stsp goto done;
4353 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4354 80ddbec8 2018-04-29 stsp if (error != NULL)
4355 ecb28ae0 2018-07-16 stsp goto done;
4357 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4358 f135c941 2020-02-20 stsp repo, worktree);
4359 f135c941 2020-02-20 stsp if (error)
4360 f135c941 2020-02-20 stsp goto done;
4362 af21bb7e 2023-04-12 mark error = init_curses(&tog_io);
4363 af21bb7e 2023-04-12 mark if (error)
4364 af21bb7e 2023-04-12 mark goto done;
4366 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4367 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4368 c02c541e 2019-03-29 stsp if (error)
4369 c02c541e 2019-03-29 stsp goto done;
4371 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4372 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4373 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4374 87670572 2020-12-26 naddy if (error)
4375 87670572 2020-12-26 naddy goto done;
4378 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4379 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4380 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4381 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4382 d8f38dc4 2020-12-05 stsp if (error)
4383 d8f38dc4 2020-12-05 stsp goto done;
4384 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4386 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4387 d8f38dc4 2020-12-05 stsp if (error == NULL)
4388 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4389 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4390 d8f38dc4 2020-12-05 stsp goto done;
4391 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4392 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4393 d8f38dc4 2020-12-05 stsp if (error)
4394 d8f38dc4 2020-12-05 stsp goto done;
4397 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4398 04cc582a 2018-08-01 stsp if (view == NULL) {
4399 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4400 04cc582a 2018-08-01 stsp goto done;
4402 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4403 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4404 ba4f502b 2018-08-04 stsp if (error)
4405 ba4f502b 2018-08-04 stsp goto done;
4406 2fc00ff4 2019-08-31 stsp if (worktree) {
4407 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4408 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4409 2fc00ff4 2019-08-31 stsp worktree = NULL;
4411 af21bb7e 2023-04-12 mark error = view_loop(view, tog_io);
4413 f135c941 2020-02-20 stsp free(in_repo_path);
4414 ecb28ae0 2018-07-16 stsp free(repo_path);
4415 ecb28ae0 2018-07-16 stsp free(cwd);
4416 899d86c2 2018-05-10 stsp free(start_id);
4417 d8f38dc4 2020-12-05 stsp free(label);
4419 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4420 1d0f4054 2021-06-17 stsp if (repo) {
4421 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4422 1d0f4054 2021-06-17 stsp if (error == NULL)
4423 1d0f4054 2021-06-17 stsp error = close_err;
4425 ec142235 2019-03-07 stsp if (worktree)
4426 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4427 0ae84acc 2022-06-15 tracey if (pack_fds) {
4428 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4429 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4430 0ae84acc 2022-06-15 tracey if (error == NULL)
4431 0ae84acc 2022-06-15 tracey error = pack_err;
4433 af21bb7e 2023-04-12 mark if (tog_io != NULL) {
4434 af21bb7e 2023-04-12 mark io_err = tog_io_close(tog_io);
4435 af21bb7e 2023-04-12 mark if (error == NULL)
4436 af21bb7e 2023-04-12 mark error = io_err;
4438 51a10b52 2020-12-26 stsp tog_free_refs();
4439 80ddbec8 2018-04-29 stsp return error;
4442 4ed7e80c 2018-05-20 stsp __dead static void
4443 9f7d7167 2018-04-29 stsp usage_diff(void)
4446 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4447 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4451 6d17833f 2019-11-08 stsp static int
4452 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4453 41605754 2020-11-12 stsp regmatch_t *regmatch)
4455 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4458 336075a4 2022-06-25 op static struct tog_color *
4459 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4461 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4463 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4464 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4465 6d17833f 2019-11-08 stsp return tc;
4468 6d17833f 2019-11-08 stsp return NULL;
4471 4ed7e80c 2018-05-20 stsp static const struct got_error *
4472 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4473 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4475 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4476 44a87665 2022-06-16 stsp char *exstr = NULL;
4477 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4478 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4479 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4480 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4482 41605754 2020-11-12 stsp *wtotal = 0;
4484 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4485 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4487 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4489 44a87665 2022-06-16 stsp return err;
4491 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4492 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4493 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4494 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4495 44a87665 2022-06-16 stsp goto done;
4497 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4498 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4499 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4500 1853e0f4 2022-06-16 stsp goto done;
4502 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4503 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4504 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4505 1853e0f4 2022-06-16 stsp goto done;
4508 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4509 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4510 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4512 1853e0f4 2022-06-16 stsp goto done;
4513 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4515 1853e0f4 2022-06-16 stsp free(wline);
4516 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4517 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4519 1853e0f4 2022-06-16 stsp goto done;
4520 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4521 1853e0f4 2022-06-16 stsp wlimit -= width;
4522 1853e0f4 2022-06-16 stsp *wtotal += width;
4525 41605754 2020-11-12 stsp if (wlimit > 0) {
4526 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4527 1853e0f4 2022-06-16 stsp size_t wlen;
4529 1853e0f4 2022-06-16 stsp free(wline);
4530 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4531 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4533 1853e0f4 2022-06-16 stsp goto done;
4534 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4535 1853e0f4 2022-06-16 stsp while (i < wlen) {
4536 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4537 1853e0f4 2022-06-16 stsp if (width == -1) {
4538 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4539 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4540 1853e0f4 2022-06-16 stsp goto done;
4542 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4544 61417565 2022-06-20 mark w += width;
4547 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4548 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4549 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4550 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4551 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4552 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4553 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4557 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4558 1853e0f4 2022-06-16 stsp free(wline);
4559 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4560 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4561 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4562 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4564 1853e0f4 2022-06-16 stsp goto done;
4565 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4567 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4568 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4570 1853e0f4 2022-06-16 stsp goto done;
4571 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4573 1853e0f4 2022-06-16 stsp *wtotal += width2;
4576 145b6838 2022-06-16 stsp free(wline);
4577 44a87665 2022-06-16 stsp free(exstr);
4578 1853e0f4 2022-06-16 stsp free(seg0);
4579 1853e0f4 2022-06-16 stsp free(seg1);
4580 1853e0f4 2022-06-16 stsp free(seg2);
4581 1853e0f4 2022-06-16 stsp return err;
4584 94b80cfa 2022-08-01 mark static int
4585 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4587 94b80cfa 2022-08-01 mark FILE *f = NULL;
4588 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4590 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4591 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4593 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4594 ec2a9698 2022-09-15 mark selected = first;
4595 ec2a9698 2022-09-15 mark eof = &s->eof;
4597 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4598 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4600 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4601 94b80cfa 2022-08-01 mark selected = first;
4602 94b80cfa 2022-08-01 mark eof = &s->eof;
4604 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4605 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4607 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4608 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4609 94b80cfa 2022-08-01 mark eof = &s->eof;
4610 94b80cfa 2022-08-01 mark f = s->blame.f;
4614 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4615 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4617 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4618 94b80cfa 2022-08-01 mark rewind(f);
4620 94b80cfa 2022-08-01 mark *first = 1;
4621 94b80cfa 2022-08-01 mark *lineno = 0;
4622 94b80cfa 2022-08-01 mark *nprinted = 0;
4626 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4627 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4628 94b80cfa 2022-08-01 mark view->gline = 0;
4633 41605754 2020-11-12 stsp static const struct got_error *
4634 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4636 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4637 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4638 61e69b96 2018-05-20 stsp const struct got_error *err;
4639 c7d5c43c 2022-08-04 mark int nprinted = 0;
4640 b304db33 2018-05-20 stsp char *line;
4641 826082fe 2020-12-10 stsp size_t linesize = 0;
4642 826082fe 2020-12-10 stsp ssize_t linelen;
4643 61e69b96 2018-05-20 stsp wchar_t *wline;
4644 e0b650dd 2018-05-20 stsp int width;
4645 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4646 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4647 fe621944 2020-11-10 stsp off_t line_offset;
4649 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4650 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4651 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4652 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4654 f7d12f7e 2018-08-01 stsp werase(view->window);
4656 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4657 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4659 a3404814 2018-09-02 stsp if (header) {
4660 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4661 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4662 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4664 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4665 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4666 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4668 135a2da0 2020-11-11 stsp free(line);
4670 a3404814 2018-09-02 stsp return err;
4672 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4673 a3404814 2018-09-02 stsp wstandout(view->window);
4674 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4675 e54cc94a 2020-11-11 stsp free(wline);
4676 e54cc94a 2020-11-11 stsp wline = NULL;
4677 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4678 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4679 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4680 a3404814 2018-09-02 stsp wstandend(view->window);
4682 a3404814 2018-09-02 stsp if (max_lines <= 1)
4683 a3404814 2018-09-02 stsp return NULL;
4684 a3404814 2018-09-02 stsp max_lines--;
4687 89f1a395 2020-12-01 naddy s->eof = 0;
4688 145b6838 2022-06-16 stsp view->maxx = 0;
4689 826082fe 2020-12-10 stsp line = NULL;
4690 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4691 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4692 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4694 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4695 826082fe 2020-12-10 stsp if (linelen == -1) {
4696 826082fe 2020-12-10 stsp if (feof(s->f)) {
4697 826082fe 2020-12-10 stsp s->eof = 1;
4700 826082fe 2020-12-10 stsp free(line);
4701 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4704 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4706 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4708 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4709 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4711 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4712 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4713 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4714 1853e0f4 2022-06-16 stsp if (err) {
4715 1853e0f4 2022-06-16 stsp free(line);
4716 1853e0f4 2022-06-16 stsp return err;
4718 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4719 1853e0f4 2022-06-16 stsp free(wline);
4720 1853e0f4 2022-06-16 stsp wline = NULL;
4722 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4723 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4724 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4725 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4727 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4728 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4729 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4730 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4731 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4732 41605754 2020-11-12 stsp if (err) {
4733 41605754 2020-11-12 stsp free(line);
4734 41605754 2020-11-12 stsp return err;
4738 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4739 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4740 969c159c 2022-06-16 stsp if (err) {
4741 969c159c 2022-06-16 stsp free(line);
4742 969c159c 2022-06-16 stsp return err;
4744 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4745 969c159c 2022-06-16 stsp free(wline);
4746 41605754 2020-11-12 stsp wline = NULL;
4748 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4749 94b80cfa 2022-08-01 mark /* highlight full gline length */
4750 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4751 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4753 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4754 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4757 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4758 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4759 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4761 826082fe 2020-12-10 stsp free(line);
4762 fe621944 2020-11-10 stsp if (nprinted >= 1)
4763 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4764 89f1a395 2020-12-01 naddy (nprinted - 1);
4766 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4768 9b058f45 2022-06-30 mark view_border(view);
4770 89f1a395 2020-12-01 naddy if (s->eof) {
4771 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4772 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4773 c3e9aa98 2019-05-13 jcs nprinted++;
4776 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4777 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4779 c3e9aa98 2019-05-13 jcs return err;
4782 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4783 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4784 e54cc94a 2020-11-11 stsp free(wline);
4785 e54cc94a 2020-11-11 stsp wline = NULL;
4786 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4789 26ed57b2 2018-05-19 stsp return NULL;
4792 abd2672a 2018-12-23 stsp static char *
4793 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4795 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4796 09867e48 2019-08-13 stsp char *p, *s;
4798 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4799 09867e48 2019-08-13 stsp if (tm == NULL)
4800 09867e48 2019-08-13 stsp return NULL;
4801 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4802 09867e48 2019-08-13 stsp if (s == NULL)
4803 09867e48 2019-08-13 stsp return NULL;
4804 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4806 abd2672a 2018-12-23 stsp *p = '\0';
4810 4ed7e80c 2018-05-20 stsp static const struct got_error *
4811 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4812 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4814 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4816 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4817 fe621944 2020-11-10 stsp if (p == NULL)
4818 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4819 c7d5c43c 2022-08-04 mark *lines = p;
4820 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4821 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4822 fe621944 2020-11-10 stsp (*nlines)++;
4824 fe621944 2020-11-10 stsp return NULL;
4827 fe621944 2020-11-10 stsp static const struct got_error *
4828 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4829 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4831 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4832 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4833 1f3405c9 2023-01-17 mark size_t i, r;
4835 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4836 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4838 1f3405c9 2023-01-17 mark for (;;) {
4839 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4840 1f3405c9 2023-01-17 mark if (r == 0) {
4841 1f3405c9 2023-01-17 mark if (ferror(src))
4842 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4843 1f3405c9 2023-01-17 mark if (feof(src))
4846 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4847 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4850 5afebbd1 2023-02-22 mark if (s_nlines == 0 && *d_nlines == 0)
4851 5afebbd1 2023-02-22 mark return NULL;
4854 5afebbd1 2023-02-22 mark * If commit info was in dst, increment line offsets
4855 5afebbd1 2023-02-22 mark * of the appended diff content, but skip s_lines[0]
4856 5afebbd1 2023-02-22 mark * because offset zero is already in *d_lines.
4858 5afebbd1 2023-02-22 mark if (*d_nlines > 0) {
4859 5afebbd1 2023-02-22 mark for (i = 1; i < s_nlines; ++i)
4860 5afebbd1 2023-02-22 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4862 5afebbd1 2023-02-22 mark if (s_nlines > 0) {
4863 5afebbd1 2023-02-22 mark --s_nlines;
4864 5afebbd1 2023-02-22 mark ++s_lines;
4868 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4869 1f3405c9 2023-01-17 mark if (p == NULL) {
4870 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4871 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4874 1f3405c9 2023-01-17 mark *d_lines = p;
4876 5afebbd1 2023-02-22 mark memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4877 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4879 1f3405c9 2023-01-17 mark return NULL;
4882 1f3405c9 2023-01-17 mark static const struct got_error *
4883 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4884 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4885 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4886 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4888 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4889 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4890 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4891 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4892 45d799e2 2018-12-23 stsp time_t committer_time;
4893 45d799e2 2018-12-23 stsp const char *author, *committer;
4894 8b473291 2019-02-21 stsp char *refs_str = NULL;
4895 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4896 fe621944 2020-11-10 stsp off_t outoff = 0;
4899 8b473291 2019-02-21 stsp if (refs) {
4900 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4902 8b473291 2019-02-21 stsp return err;
4905 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4907 abd2672a 2018-12-23 stsp return err;
4909 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4910 15a94983 2018-12-23 stsp if (err) {
4911 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4912 15a94983 2018-12-23 stsp goto done;
4915 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4917 fe621944 2020-11-10 stsp goto done;
4919 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4920 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4921 fe621944 2020-11-10 stsp if (n < 0) {
4922 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4923 abd2672a 2018-12-23 stsp goto done;
4925 fe621944 2020-11-10 stsp outoff += n;
4926 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4928 fe621944 2020-11-10 stsp goto done;
4930 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4931 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4932 fe621944 2020-11-10 stsp if (n < 0) {
4933 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4934 abd2672a 2018-12-23 stsp goto done;
4936 fe621944 2020-11-10 stsp outoff += n;
4937 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4939 fe621944 2020-11-10 stsp goto done;
4941 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4942 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4943 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4944 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4945 fe621944 2020-11-10 stsp if (n < 0) {
4946 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4947 fe621944 2020-11-10 stsp goto done;
4949 fe621944 2020-11-10 stsp outoff += n;
4950 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4951 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4953 fe621944 2020-11-10 stsp goto done;
4955 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4956 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4957 1a99e0b4 2023-01-07 stsp if (datestr) {
4958 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4959 fe621944 2020-11-10 stsp if (n < 0) {
4960 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4961 fe621944 2020-11-10 stsp goto done;
4963 fe621944 2020-11-10 stsp outoff += n;
4964 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4965 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4967 fe621944 2020-11-10 stsp goto done;
4969 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4970 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4971 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4972 9f98ca05 2021-09-24 stsp int pn = 1;
4973 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4974 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4975 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4977 9f98ca05 2021-09-24 stsp goto done;
4978 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4979 9f98ca05 2021-09-24 stsp if (n < 0) {
4980 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4981 9f98ca05 2021-09-24 stsp goto done;
4983 9f98ca05 2021-09-24 stsp outoff += n;
4984 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4985 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4987 9f98ca05 2021-09-24 stsp goto done;
4988 9f98ca05 2021-09-24 stsp free(id_str);
4989 9f98ca05 2021-09-24 stsp id_str = NULL;
4993 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4995 5943eee2 2019-08-13 stsp goto done;
4996 fe621944 2020-11-10 stsp s = logmsg;
4997 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4998 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4999 fe621944 2020-11-10 stsp if (n < 0) {
5000 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5001 fe621944 2020-11-10 stsp goto done;
5003 fe621944 2020-11-10 stsp outoff += n;
5004 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
5005 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
5007 fe621944 2020-11-10 stsp goto done;
5010 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
5011 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5012 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
5014 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5015 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5016 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
5017 fe621944 2020-11-10 stsp if (n < 0) {
5018 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5019 fe621944 2020-11-10 stsp goto done;
5021 fe621944 2020-11-10 stsp outoff += n;
5022 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
5023 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
5025 fe621944 2020-11-10 stsp goto done;
5028 0208f208 2020-05-05 stsp fputc('\n', outfile);
5030 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5032 5191b70b 2023-01-07 mark goto done;
5034 5191b70b 2023-01-07 mark n = fprintf(outfile,
5035 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5036 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5037 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5038 5191b70b 2023-01-07 mark if (n < 0) {
5039 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
5040 5191b70b 2023-01-07 mark goto done;
5042 5191b70b 2023-01-07 mark outoff += n;
5043 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5045 5191b70b 2023-01-07 mark goto done;
5047 5191b70b 2023-01-07 mark fputc('\n', outfile);
5049 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5051 abd2672a 2018-12-23 stsp free(id_str);
5052 5943eee2 2019-08-13 stsp free(logmsg);
5053 8b473291 2019-02-21 stsp free(refs_str);
5054 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5055 7510f233 2020-08-09 stsp if (err) {
5056 c7d5c43c 2022-08-04 mark free(*lines);
5057 c7d5c43c 2022-08-04 mark *lines = NULL;
5058 ae6a6978 2020-08-09 stsp *nlines = 0;
5060 fe621944 2020-11-10 stsp return err;
5063 abd2672a 2018-12-23 stsp static const struct got_error *
5064 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5066 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5067 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
5068 15a94983 2018-12-23 stsp int obj_type;
5069 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
5070 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
5072 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
5074 c7d5c43c 2022-08-04 mark free(s->lines);
5075 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
5076 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
5077 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5078 fe621944 2020-11-10 stsp s->nlines = 0;
5080 511a516b 2018-05-19 stsp f = got_opentemp();
5081 48ae06ee 2018-10-18 stsp if (f == NULL) {
5082 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5083 48ae06ee 2018-10-18 stsp goto done;
5085 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
5086 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
5087 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
5088 1f3405c9 2023-01-17 mark goto done;
5090 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5091 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5092 fb43ecf1 2019-02-11 stsp goto done;
5096 15a94983 2018-12-23 stsp if (s->id1)
5097 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5099 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5101 15a94983 2018-12-23 stsp goto done;
5103 15a94983 2018-12-23 stsp switch (obj_type) {
5104 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5105 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5106 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5107 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
5108 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5111 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5112 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5113 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5114 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
5115 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
5117 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5118 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5119 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5120 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5121 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5122 1f3405c9 2023-01-17 mark size_t nlines = 0;
5123 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
5124 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
5125 1f3405c9 2023-01-17 mark &changed_paths,
5126 1f3405c9 2023-01-17 mark s->ignore_whitespace,
5127 1f3405c9 2023-01-17 mark s->force_text_diff,
5128 1f3405c9 2023-01-17 mark tog_diff_algo
5131 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
5132 1f3405c9 2023-01-17 mark if (lines == NULL) {
5133 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
5134 1f3405c9 2023-01-17 mark goto done;
5137 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
5138 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
5139 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5140 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
5141 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5145 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5147 3ffacbe1 2020-02-02 tracey goto done;
5148 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5149 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5150 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5151 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
5152 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
5153 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
5154 f44b1f58 2020-02-02 tracey if (err)
5155 f44b1f58 2020-02-02 tracey goto done;
5156 f44b1f58 2020-02-02 tracey } else {
5157 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5158 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5159 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5160 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
5161 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
5162 5191b70b 2023-01-07 mark s->ignore_whitespace,
5163 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
5164 f44b1f58 2020-02-02 tracey if (err)
5165 f44b1f58 2020-02-02 tracey goto done;
5170 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5172 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5173 1f3405c9 2023-01-17 mark lines, nlines);
5177 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5181 1f3405c9 2023-01-17 mark free(lines);
5182 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5183 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5184 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5185 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5186 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
5187 48ae06ee 2018-10-18 stsp return err;
5190 f5215bb9 2019-02-22 stsp static void
5191 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5193 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5194 f5215bb9 2019-02-22 stsp update_panels();
5195 f5215bb9 2019-02-22 stsp doupdate();
5198 f44b1f58 2020-02-02 tracey static const struct got_error *
5199 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5201 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5203 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5204 f44b1f58 2020-02-02 tracey return NULL;
5207 eaf2c13e 2022-09-17 mark static void
5208 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5209 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
5211 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
5213 eaf2c13e 2022-09-17 mark *f = s->f;
5214 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
5215 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
5216 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
5217 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
5218 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
5219 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
5222 ec2a9698 2022-09-15 mark static const struct got_error *
5223 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
5225 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5227 f44b1f58 2020-02-02 tracey int lineno;
5228 cb713507 2022-06-16 stsp char *line = NULL;
5229 826082fe 2020-12-10 stsp size_t linesize = 0;
5230 826082fe 2020-12-10 stsp ssize_t linelen;
5231 ec2a9698 2022-09-15 mark off_t *line_offsets;
5232 ec2a9698 2022-09-15 mark size_t nlines = 0;
5233 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
5235 eaf2c13e 2022-09-17 mark if (!view->search_setup)
5236 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
5237 eaf2c13e 2022-09-17 mark "view search not supported");
5238 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5239 ec2a9698 2022-09-15 mark &match, &selected);
5241 f44b1f58 2020-02-02 tracey if (!view->searching) {
5242 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5243 f44b1f58 2020-02-02 tracey return NULL;
5246 ec2a9698 2022-09-15 mark if (*match) {
5247 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5248 8c28a2ab 2023-04-02 mark lineno = *first + 1;
5250 8c28a2ab 2023-04-02 mark lineno = *first - 1;
5252 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
5254 f44b1f58 2020-02-02 tracey while (1) {
5255 f44b1f58 2020-02-02 tracey off_t offset;
5257 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
5258 ec2a9698 2022-09-15 mark if (*match == 0) {
5259 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5263 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5264 f44b1f58 2020-02-02 tracey lineno = 1;
5266 ec2a9698 2022-09-15 mark lineno = nlines;
5269 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
5270 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
5271 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
5272 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
5273 f44b1f58 2020-02-02 tracey free(line);
5274 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5276 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
5277 cb713507 2022-06-16 stsp if (linelen != -1) {
5278 cb713507 2022-06-16 stsp char *exstr;
5279 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5282 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5283 cb713507 2022-06-16 stsp &view->regmatch)) {
5284 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5285 ec2a9698 2022-09-15 mark *match = lineno;
5286 cb713507 2022-06-16 stsp free(exstr);
5289 cb713507 2022-06-16 stsp free(exstr);
5291 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5292 f44b1f58 2020-02-02 tracey lineno++;
5294 f44b1f58 2020-02-02 tracey lineno--;
5296 826082fe 2020-12-10 stsp free(line);
5298 ec2a9698 2022-09-15 mark if (*match) {
5299 ec2a9698 2022-09-15 mark *first = *match;
5300 ec2a9698 2022-09-15 mark *selected = 1;
5303 145b6838 2022-06-16 stsp return err;
5306 48ae06ee 2018-10-18 stsp static const struct got_error *
5307 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
5309 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
5310 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5312 b72706c3 2022-06-01 stsp free(s->id1);
5313 b72706c3 2022-06-01 stsp s->id1 = NULL;
5314 b72706c3 2022-06-01 stsp free(s->id2);
5315 b72706c3 2022-06-01 stsp s->id2 = NULL;
5316 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
5317 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5318 b72706c3 2022-06-01 stsp s->f = NULL;
5319 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5320 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5321 b72706c3 2022-06-01 stsp s->f1 = NULL;
5322 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5323 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5324 b72706c3 2022-06-01 stsp s->f2 = NULL;
5325 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5326 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5327 f9d37699 2022-06-28 stsp s->fd1 = -1;
5328 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5329 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5330 f9d37699 2022-06-28 stsp s->fd2 = -1;
5331 c7d5c43c 2022-08-04 mark free(s->lines);
5332 c7d5c43c 2022-08-04 mark s->lines = NULL;
5333 b72706c3 2022-06-01 stsp s->nlines = 0;
5334 b72706c3 2022-06-01 stsp return err;
5337 b72706c3 2022-06-01 stsp static const struct got_error *
5338 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5339 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5340 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5341 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
5343 48ae06ee 2018-10-18 stsp const struct got_error *err;
5344 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5346 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
5347 f9d37699 2022-06-28 stsp s->fd1 = -1;
5348 f9d37699 2022-06-28 stsp s->fd2 = -1;
5350 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5351 15a94983 2018-12-23 stsp int type1, type2;
5352 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5354 15a94983 2018-12-23 stsp return err;
5355 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5357 15a94983 2018-12-23 stsp return err;
5359 15a94983 2018-12-23 stsp if (type1 != type2)
5360 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5362 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5363 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5364 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5365 f44b1f58 2020-02-02 tracey s->repo = repo;
5366 f44b1f58 2020-02-02 tracey s->id1 = id1;
5367 f44b1f58 2020-02-02 tracey s->id2 = id2;
5368 3dbaef42 2020-11-24 stsp s->label1 = label1;
5369 3dbaef42 2020-11-24 stsp s->label2 = label2;
5371 15a94983 2018-12-23 stsp if (id1) {
5372 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5373 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5374 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5376 5465d566 2020-02-01 tracey s->id1 = NULL;
5378 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5379 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5380 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5381 b72706c3 2022-06-01 stsp goto done;
5384 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5385 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5386 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5387 a00719e9 2022-06-17 stsp goto done;
5390 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5391 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5392 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5393 b72706c3 2022-06-01 stsp goto done;
5396 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5397 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5398 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5399 f9d37699 2022-06-28 stsp goto done;
5402 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5403 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5404 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5405 f9d37699 2022-06-28 stsp goto done;
5408 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5409 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5410 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5411 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5412 5465d566 2020-02-01 tracey s->repo = repo;
5414 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5417 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5418 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5419 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5420 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5421 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5422 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5423 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5424 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5425 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5426 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5427 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5428 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5429 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5430 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5431 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5432 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5433 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5434 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5435 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5436 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5437 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5438 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5439 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5440 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5441 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5442 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5443 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5444 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5445 b72706c3 2022-06-01 stsp goto done;
5449 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5450 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5451 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5452 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5454 5465d566 2020-02-01 tracey err = create_diff(s);
5456 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5457 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5458 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5459 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5460 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5461 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5462 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5465 b72706c3 2022-06-01 stsp close_diff_view(view);
5466 e5a0f69f 2018-08-18 stsp return err;
5469 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5470 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5472 a3404814 2018-09-02 stsp const struct got_error *err;
5473 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5474 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5475 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5477 a3404814 2018-09-02 stsp if (s->id1) {
5478 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5480 a3404814 2018-09-02 stsp return err;
5481 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5483 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5485 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5487 a3404814 2018-09-02 stsp return err;
5488 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5490 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5491 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5492 a3404814 2018-09-02 stsp free(id_str1);
5493 a3404814 2018-09-02 stsp free(id_str2);
5494 a3404814 2018-09-02 stsp return err;
5496 a3404814 2018-09-02 stsp free(id_str1);
5497 a3404814 2018-09-02 stsp free(id_str2);
5499 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5500 267bb3b8 2021-08-01 stsp free(header);
5501 267bb3b8 2021-08-01 stsp return err;
5504 15a087fe 2019-02-21 stsp static const struct got_error *
5505 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5506 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5508 d7a04538 2019-02-21 stsp const struct got_error *err;
5509 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5510 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5511 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5513 15a087fe 2019-02-21 stsp free(s->id2);
5514 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5515 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5516 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5518 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5520 d7a04538 2019-02-21 stsp return err;
5521 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5522 15a087fe 2019-02-21 stsp free(s->id1);
5523 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5524 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5525 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5526 15a087fe 2019-02-21 stsp return NULL;
5529 0cf4efb1 2018-09-29 stsp static const struct got_error *
5530 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5532 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5534 917d79a7 2022-07-01 stsp view->count = 0;
5535 917d79a7 2022-07-01 stsp wclear(view->window);
5536 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5537 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5538 917d79a7 2022-07-01 stsp s->matched_line = 0;
5539 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5540 917d79a7 2022-07-01 stsp return create_diff(s);
5543 c7d5c43c 2022-08-04 mark static void
5544 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5546 c7d5c43c 2022-08-04 mark int start, i;
5548 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5550 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5551 c7d5c43c 2022-08-04 mark if (i == 0)
5552 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5553 c7d5c43c 2022-08-04 mark if (--i == start)
5554 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5557 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5558 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5561 c7d5c43c 2022-08-04 mark static void
5562 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5564 c7d5c43c 2022-08-04 mark int start, i;
5566 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5568 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5569 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5571 c7d5c43c 2022-08-04 mark if (++i == start)
5572 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5575 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5576 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5579 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5580 c0f61fa4 2022-07-11 mark int, int, int);
5581 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5582 c0f61fa4 2022-07-11 mark int, int);
5584 917d79a7 2022-07-01 stsp static const struct got_error *
5585 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5587 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5588 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5589 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5590 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5591 826082fe 2020-12-10 stsp char *line = NULL;
5592 826082fe 2020-12-10 stsp size_t linesize = 0;
5593 826082fe 2020-12-10 stsp ssize_t linelen;
5594 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5596 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5598 e5a0f69f 2018-08-18 stsp switch (ch) {
5601 145b6838 2022-06-16 stsp case KEY_RIGHT:
5603 145b6838 2022-06-16 stsp case KEY_LEFT:
5605 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
5609 3f6c6614 2023-01-23 mark if (ch == 'a') {
5610 3f6c6614 2023-01-23 mark s->force_text_diff = !s->force_text_diff;
5611 3f6c6614 2023-01-23 mark view->action = s->force_text_diff ?
5612 3f6c6614 2023-01-23 mark "force ASCII text enabled" :
5613 3f6c6614 2023-01-23 mark "force ASCII text disabled";
5615 3f6c6614 2023-01-23 mark else if (ch == 'w') {
5616 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5617 3f6c6614 2023-01-23 mark view->action = s->ignore_whitespace ?
5618 3f6c6614 2023-01-23 mark "ignore whitespace enabled" :
5619 3f6c6614 2023-01-23 mark "ignore whitespace disabled";
5621 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5624 912a3f79 2021-08-30 j case KEY_HOME:
5625 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5626 640cd7ff 2022-06-22 mark view->count = 0;
5629 912a3f79 2021-08-30 j case KEY_END:
5630 640cd7ff 2022-06-22 mark view->count = 0;
5634 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5638 1e37a5c2 2019-05-12 jcs case KEY_UP:
5639 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5640 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5641 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5643 640cd7ff 2022-06-22 mark view->count = 0;
5645 83cc4199 2022-06-13 stsp case CTRL('u'):
5647 83cc4199 2022-06-13 stsp nscroll /= 2;
5648 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5649 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5650 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5652 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5653 640cd7ff 2022-06-22 mark view->count = 0;
5657 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5658 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5661 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5662 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5663 1e37a5c2 2019-05-12 jcs if (!s->eof)
5664 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5666 640cd7ff 2022-06-22 mark view->count = 0;
5668 83cc4199 2022-06-13 stsp case CTRL('d'):
5670 83cc4199 2022-06-13 stsp nscroll /= 2;
5671 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5672 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5673 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5676 640cd7ff 2022-06-22 mark if (s->eof) {
5677 640cd7ff 2022-06-22 mark view->count = 0;
5681 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5682 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5683 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5684 826082fe 2020-12-10 stsp if (linelen == -1) {
5685 826082fe 2020-12-10 stsp if (feof(s->f)) {
5686 826082fe 2020-12-10 stsp s->eof = 1;
5688 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5692 826082fe 2020-12-10 stsp free(line);
5695 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5698 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5701 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5704 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5707 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5708 1e37a5c2 2019-05-12 jcs s->diff_context--;
5709 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5710 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5711 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5712 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5713 27829c9e 2020-11-21 stsp s->nlines) {
5714 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5715 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5718 640cd7ff 2022-06-22 mark view->count = 0;
5721 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5722 1e37a5c2 2019-05-12 jcs s->diff_context++;
5723 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5724 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5725 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5727 640cd7ff 2022-06-22 mark view->count = 0;
5733 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5737 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5738 640cd7ff 2022-06-22 mark view->count = 0;
5741 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5743 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5744 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5745 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5747 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5748 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5751 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5753 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5756 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5759 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5760 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5761 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5763 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5764 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5765 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5767 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5768 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5771 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5773 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5775 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5776 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5777 c0f61fa4 2022-07-11 mark bs->selected_line);
5778 c0f61fa4 2022-07-11 mark if (id == NULL)
5781 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5784 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5788 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5789 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5790 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5791 145b6838 2022-06-16 stsp view->x = 0;
5793 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5794 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5797 640cd7ff 2022-06-22 mark view->count = 0;
5801 bcbd79e2 2018-08-19 stsp return err;
5804 4ed7e80c 2018-05-20 stsp static const struct got_error *
5805 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5807 af21bb7e 2023-04-12 mark const struct got_error *io_err, *error;
5808 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5809 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5810 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5811 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5812 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5813 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5814 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5815 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5816 3dbaef42 2020-11-24 stsp const char *errstr;
5817 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5818 af21bb7e 2023-04-12 mark struct tog_io *tog_io = NULL;
5819 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5821 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5822 26ed57b2 2018-05-19 stsp switch (ch) {
5824 64453f7e 2020-11-21 stsp force_text_diff = 1;
5827 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5829 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5830 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5831 5a20d08d 2022-02-09 op errstr, errstr);
5833 09b5bff8 2020-02-23 naddy case 'r':
5834 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5835 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5836 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5838 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5841 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5844 17020d27 2019-03-07 stsp usage_diff();
5845 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5849 26ed57b2 2018-05-19 stsp argc -= optind;
5850 26ed57b2 2018-05-19 stsp argv += optind;
5852 26ed57b2 2018-05-19 stsp if (argc == 0) {
5853 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5854 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5855 15a94983 2018-12-23 stsp id_str1 = argv[0];
5856 15a94983 2018-12-23 stsp id_str2 = argv[1];
5858 26ed57b2 2018-05-19 stsp usage_diff();
5860 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5861 0ae84acc 2022-06-15 tracey if (error)
5862 0ae84acc 2022-06-15 tracey goto done;
5864 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5865 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5866 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5867 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5868 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5869 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5870 c156c7a4 2020-12-18 stsp goto done;
5871 a273ac94 2020-02-23 naddy if (worktree)
5872 a273ac94 2020-02-23 naddy repo_path =
5873 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5875 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5876 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5877 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5878 c156c7a4 2020-12-18 stsp goto done;
5882 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5883 eb6600df 2019-01-04 stsp if (error)
5884 eb6600df 2019-01-04 stsp goto done;
5886 af21bb7e 2023-04-12 mark error = init_curses(&tog_io);
5887 af21bb7e 2023-04-12 mark if (error)
5888 af21bb7e 2023-04-12 mark goto done;
5890 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5891 51a10b52 2020-12-26 stsp if (error)
5892 51a10b52 2020-12-26 stsp goto done;
5894 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5895 26ed57b2 2018-05-19 stsp if (error)
5896 26ed57b2 2018-05-19 stsp goto done;
5898 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5899 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5900 26ed57b2 2018-05-19 stsp if (error)
5901 26ed57b2 2018-05-19 stsp goto done;
5903 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5904 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5905 26ed57b2 2018-05-19 stsp if (error)
5906 26ed57b2 2018-05-19 stsp goto done;
5908 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5909 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5910 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5911 ea5e7bb5 2018-08-01 stsp goto done;
5913 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5914 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5915 5dc9f4bc 2018-08-04 stsp if (error)
5916 5dc9f4bc 2018-08-04 stsp goto done;
5917 af21bb7e 2023-04-12 mark error = view_loop(view, tog_io);
5919 3dbaef42 2020-11-24 stsp free(label1);
5920 3dbaef42 2020-11-24 stsp free(label2);
5921 c02c541e 2019-03-29 stsp free(repo_path);
5922 a273ac94 2020-02-23 naddy free(cwd);
5923 1d0f4054 2021-06-17 stsp if (repo) {
5924 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5925 1d0f4054 2021-06-17 stsp if (error == NULL)
5926 1d0f4054 2021-06-17 stsp error = close_err;
5928 a273ac94 2020-02-23 naddy if (worktree)
5929 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5930 0ae84acc 2022-06-15 tracey if (pack_fds) {
5931 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5932 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5933 0ae84acc 2022-06-15 tracey if (error == NULL)
5934 0ae84acc 2022-06-15 tracey error = pack_err;
5936 af21bb7e 2023-04-12 mark if (tog_io != NULL) {
5937 af21bb7e 2023-04-12 mark io_err = tog_io_close(tog_io);
5938 af21bb7e 2023-04-12 mark if (error == NULL)
5939 af21bb7e 2023-04-12 mark error = io_err;
5941 51a10b52 2020-12-26 stsp tog_free_refs();
5942 26ed57b2 2018-05-19 stsp return error;
5945 4ed7e80c 2018-05-20 stsp __dead static void
5946 9f7d7167 2018-04-29 stsp usage_blame(void)
5949 87411fa9 2022-06-16 stsp fprintf(stderr,
5950 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5951 9f7d7167 2018-04-29 stsp getprogname());
5955 84451b3e 2018-07-10 stsp struct tog_blame_line {
5956 84451b3e 2018-07-10 stsp int annotated;
5957 84451b3e 2018-07-10 stsp struct got_object_id *id;
5960 4ed7e80c 2018-05-20 stsp static const struct got_error *
5961 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5963 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5964 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5965 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5966 84451b3e 2018-07-10 stsp const struct got_error *err;
5967 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5968 826082fe 2020-12-10 stsp char *line = NULL;
5969 826082fe 2020-12-10 stsp size_t linesize = 0;
5970 826082fe 2020-12-10 stsp ssize_t linelen;
5971 84451b3e 2018-07-10 stsp wchar_t *wline;
5972 27a741e5 2019-09-11 stsp int width;
5973 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5974 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5975 ab089a2a 2018-07-12 stsp char *id_str;
5976 11b20872 2019-11-08 stsp struct tog_color *tc;
5978 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5980 ab089a2a 2018-07-12 stsp return err;
5982 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5983 f7d12f7e 2018-08-01 stsp werase(view->window);
5985 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5986 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5987 ab089a2a 2018-07-12 stsp free(id_str);
5988 ab089a2a 2018-07-12 stsp return err;
5991 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5992 ab089a2a 2018-07-12 stsp free(line);
5993 2550e4c3 2018-07-13 stsp line = NULL;
5995 1cae65b4 2019-09-22 stsp return err;
5996 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5997 a3404814 2018-09-02 stsp wstandout(view->window);
5998 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6000 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6001 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6002 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6003 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6005 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6006 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6007 a3404814 2018-09-02 stsp wstandend(view->window);
6008 2550e4c3 2018-07-13 stsp free(wline);
6009 2550e4c3 2018-07-13 stsp wline = NULL;
6011 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
6012 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
6014 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6015 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6016 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6017 ab089a2a 2018-07-12 stsp free(id_str);
6018 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6020 ab089a2a 2018-07-12 stsp free(id_str);
6021 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6022 3f60a8ef 2018-07-10 stsp free(line);
6023 2550e4c3 2018-07-13 stsp line = NULL;
6025 3f60a8ef 2018-07-10 stsp return err;
6026 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6027 2550e4c3 2018-07-13 stsp free(wline);
6028 2550e4c3 2018-07-13 stsp wline = NULL;
6029 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6030 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6032 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6033 145b6838 2022-06-16 stsp view->maxx = 0;
6034 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6035 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6036 826082fe 2020-12-10 stsp if (linelen == -1) {
6037 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6038 826082fe 2020-12-10 stsp s->eof = 1;
6041 84451b3e 2018-07-10 stsp free(line);
6042 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6044 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6046 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
6049 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
6050 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6051 1853e0f4 2022-06-16 stsp if (err) {
6052 1853e0f4 2022-06-16 stsp free(line);
6053 1853e0f4 2022-06-16 stsp return err;
6055 1853e0f4 2022-06-16 stsp free(wline);
6056 1853e0f4 2022-06-16 stsp wline = NULL;
6057 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
6059 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
6060 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6062 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6063 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6064 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6065 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6066 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
6067 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6068 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6069 8d0fe45a 2019-08-12 stsp char *id_str;
6070 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
6071 87411fa9 2022-06-16 stsp blame_line->id);
6072 8d0fe45a 2019-08-12 stsp if (err) {
6073 8d0fe45a 2019-08-12 stsp free(line);
6074 8d0fe45a 2019-08-12 stsp return err;
6076 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6078 11b20872 2019-11-08 stsp wattr_on(view->window,
6079 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6080 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6082 11b20872 2019-11-08 stsp wattr_off(view->window,
6083 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6084 8d0fe45a 2019-08-12 stsp free(id_str);
6085 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6087 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6088 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6091 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6092 ee41ec32 2018-07-10 stsp prev_id = NULL;
6095 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
6096 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6097 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6099 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6100 41605754 2020-11-12 stsp width = 9;
6101 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6102 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6103 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6104 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6105 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
6106 41605754 2020-11-12 stsp if (err) {
6107 41605754 2020-11-12 stsp free(line);
6108 41605754 2020-11-12 stsp return err;
6110 41605754 2020-11-12 stsp width += 9;
6113 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
6114 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
6115 4cb9d869 2022-06-16 stsp if (err) {
6116 4cb9d869 2022-06-16 stsp free(line);
6117 4cb9d869 2022-06-16 stsp return err;
6119 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
6120 a75b3e2d 2022-06-16 stsp width += 9;
6121 41605754 2020-11-12 stsp free(wline);
6122 41605754 2020-11-12 stsp wline = NULL;
6125 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6126 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6127 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6128 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6130 826082fe 2020-12-10 stsp free(line);
6131 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6133 9b058f45 2022-06-30 mark view_border(view);
6135 84451b3e 2018-07-10 stsp return NULL;
6138 84451b3e 2018-07-10 stsp static const struct got_error *
6139 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
6140 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
6142 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6143 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6144 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6145 1a76625f 2018-10-22 stsp int errcode;
6147 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6148 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6149 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6151 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6152 1a76625f 2018-10-22 stsp if (errcode)
6153 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6155 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6156 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6157 d68a0a7d 2018-07-10 stsp goto done;
6160 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6161 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6163 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6164 d68a0a7d 2018-07-10 stsp if (line->annotated)
6165 d68a0a7d 2018-07-10 stsp goto done;
6167 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6168 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6169 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6170 84451b3e 2018-07-10 stsp goto done;
6172 84451b3e 2018-07-10 stsp line->annotated = 1;
6174 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6175 1a76625f 2018-10-22 stsp if (errcode)
6176 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6177 84451b3e 2018-07-10 stsp return err;
6180 84451b3e 2018-07-10 stsp static void *
6181 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6183 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6184 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6185 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6186 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
6187 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
6189 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
6190 e6e73e55 2022-06-30 tracey if (fd1 == -1)
6191 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
6193 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
6194 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
6195 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
6196 e6e73e55 2022-06-30 tracey goto done;
6199 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
6200 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
6201 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
6202 e6e73e55 2022-06-30 tracey goto done;
6204 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
6205 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
6206 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
6207 e6e73e55 2022-06-30 tracey goto done;
6210 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6212 e6e73e55 2022-06-30 tracey goto done;
6214 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6215 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
6216 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6217 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6218 fc06ba56 2019-08-22 stsp err = NULL;
6220 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6221 e6e73e55 2022-06-30 tracey if (errcode) {
6222 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
6223 e6e73e55 2022-06-30 tracey goto done;
6226 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6227 1d0f4054 2021-06-17 stsp if (err == NULL)
6228 1d0f4054 2021-06-17 stsp err = close_err;
6229 c9beca56 2018-07-22 stsp ta->repo = NULL;
6230 c9beca56 2018-07-22 stsp *ta->complete = 1;
6232 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6233 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6234 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6237 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6238 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
6239 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6240 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
6241 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
6242 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
6243 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
6244 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
6246 18430de3 2018-07-10 stsp return (void *)err;
6249 245d91c1 2018-07-12 stsp static struct got_object_id *
6250 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6251 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6253 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6255 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6256 8d0fe45a 2019-08-12 stsp return NULL;
6258 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6259 c0f61fa4 2022-07-11 mark if (!line->annotated)
6260 c0f61fa4 2022-07-11 mark return NULL;
6262 c0f61fa4 2022-07-11 mark return line->id;
6265 c0f61fa4 2022-07-11 mark static struct got_object_id *
6266 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6267 c0f61fa4 2022-07-11 mark int lineno)
6269 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
6271 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
6272 c0f61fa4 2022-07-11 mark return NULL;
6274 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
6275 245d91c1 2018-07-12 stsp if (!line->annotated)
6276 245d91c1 2018-07-12 stsp return NULL;
6278 245d91c1 2018-07-12 stsp return line->id;
6281 b880a918 2018-07-10 stsp static const struct got_error *
6282 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6284 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6287 245d91c1 2018-07-12 stsp if (blame->thread) {
6288 1a76625f 2018-10-22 stsp int errcode;
6289 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6290 1a76625f 2018-10-22 stsp if (errcode)
6291 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6292 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6293 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6294 1a76625f 2018-10-22 stsp if (errcode)
6295 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6296 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6297 1a76625f 2018-10-22 stsp if (errcode)
6298 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6299 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6300 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6301 245d91c1 2018-07-12 stsp err = NULL;
6302 245d91c1 2018-07-12 stsp blame->thread = NULL;
6304 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6305 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6306 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6307 1d0f4054 2021-06-17 stsp if (err == NULL)
6308 1d0f4054 2021-06-17 stsp err = close_err;
6309 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6311 245d91c1 2018-07-12 stsp if (blame->f) {
6312 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6313 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6314 245d91c1 2018-07-12 stsp blame->f = NULL;
6316 57670559 2018-12-23 stsp if (blame->lines) {
6317 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6318 57670559 2018-12-23 stsp free(blame->lines[i].id);
6319 57670559 2018-12-23 stsp free(blame->lines);
6320 57670559 2018-12-23 stsp blame->lines = NULL;
6322 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6323 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6324 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
6325 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6326 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
6327 0ae84acc 2022-06-15 tracey if (err == NULL)
6328 0ae84acc 2022-06-15 tracey err = pack_err;
6329 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
6331 245d91c1 2018-07-12 stsp return err;
6334 245d91c1 2018-07-12 stsp static const struct got_error *
6335 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6337 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6338 fc06ba56 2019-08-22 stsp int *done = arg;
6339 fc06ba56 2019-08-22 stsp int errcode;
6341 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6342 fc06ba56 2019-08-22 stsp if (errcode)
6343 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6344 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6346 fc06ba56 2019-08-22 stsp if (*done)
6347 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6349 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6350 fc06ba56 2019-08-22 stsp if (errcode)
6351 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6352 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6354 fc06ba56 2019-08-22 stsp return err;
6357 fc06ba56 2019-08-22 stsp static const struct got_error *
6358 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6360 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6361 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6362 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6363 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6364 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6365 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6366 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6367 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6368 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6370 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6371 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6373 15a94983 2018-12-23 stsp return err;
6375 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6376 eb81bc23 2022-06-28 tracey if (fd == -1) {
6377 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6378 eb81bc23 2022-06-28 tracey goto done;
6381 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6383 a44927cc 2022-04-07 stsp goto done;
6385 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6387 84451b3e 2018-07-10 stsp goto done;
6389 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6390 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6391 84451b3e 2018-07-10 stsp goto done;
6394 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6396 a70480e0 2018-06-23 stsp goto done;
6397 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6398 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6399 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6400 84451b3e 2018-07-10 stsp goto done;
6402 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6403 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6405 1fddf795 2021-01-20 stsp goto done;
6406 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6407 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6408 84451b3e 2018-07-10 stsp goto done;
6411 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6412 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6413 b02560ec 2019-08-19 stsp blame->nlines--;
6415 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6416 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6417 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6418 84451b3e 2018-07-10 stsp goto done;
6421 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6423 bd24772e 2018-07-11 stsp goto done;
6424 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6425 0ae84acc 2022-06-15 tracey pack_fds);
6426 0ae84acc 2022-06-15 tracey if (err)
6427 0ae84acc 2022-06-15 tracey goto done;
6429 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6430 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6431 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6432 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6433 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6434 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6435 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6436 245d91c1 2018-07-12 stsp goto done;
6438 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6440 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6441 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6442 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6443 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6444 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6445 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6446 a5388363 2020-12-01 naddy s->blame_complete = 0;
6448 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6449 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6450 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6451 f5a09613 2020-12-13 naddy s->selected_line = 1;
6453 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6456 a44927cc 2022-04-07 stsp if (commit)
6457 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6458 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6459 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6461 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6462 27d434c2 2018-09-15 stsp free(obj_id);
6464 245d91c1 2018-07-12 stsp stop_blame(blame);
6465 245d91c1 2018-07-12 stsp return err;
6468 245d91c1 2018-07-12 stsp static const struct got_error *
6469 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6470 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6472 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6473 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6475 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6477 c4843652 2019-08-12 stsp s->path = strdup(path);
6478 c4843652 2019-08-12 stsp if (s->path == NULL)
6479 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6481 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6482 c4843652 2019-08-12 stsp if (err) {
6483 c4843652 2019-08-12 stsp free(s->path);
6484 7cbe629d 2018-08-04 stsp return err;
6487 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6488 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6489 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6490 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6491 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6492 fb2756b9 2018-08-04 stsp s->repo = repo;
6493 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6494 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6496 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6497 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6498 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6499 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6501 11b20872 2019-11-08 stsp return err;
6504 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6505 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6506 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6507 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6508 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6509 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6510 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6512 a5388363 2020-12-01 naddy return run_blame(view);
6515 e5a0f69f 2018-08-18 stsp static const struct got_error *
6516 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6518 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6519 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6521 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6522 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6524 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6525 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6526 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6527 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6528 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6531 e5a0f69f 2018-08-18 stsp free(s->path);
6532 11b20872 2019-11-08 stsp free_colors(&s->colors);
6533 e5a0f69f 2018-08-18 stsp return err;
6536 7cbe629d 2018-08-04 stsp static const struct got_error *
6537 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6539 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6541 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6542 6c4c42e0 2019-06-24 stsp return NULL;
6545 eaf2c13e 2022-09-17 mark static void
6546 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6547 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6549 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6551 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6552 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6553 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6554 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6555 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6556 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6557 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6560 6c4c42e0 2019-06-24 stsp static const struct got_error *
6561 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6563 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6564 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6565 2b380cc8 2018-10-24 stsp int errcode;
6567 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6568 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6569 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6570 2b380cc8 2018-10-24 stsp if (errcode)
6571 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6573 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6576 51fe7530 2019-08-19 stsp if (s->blame_complete)
6577 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6579 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6581 9b058f45 2022-06-30 mark view_border(view);
6582 e5a0f69f 2018-08-18 stsp return err;
6585 e5a0f69f 2018-08-18 stsp static const struct got_error *
6586 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6587 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6589 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6590 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6592 05f04cdf 2022-07-20 mark *new_view = NULL;
6594 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6595 05f04cdf 2022-07-20 mark if (log_view == NULL)
6596 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6598 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6600 05f04cdf 2022-07-20 mark view_close(log_view);
6602 05f04cdf 2022-07-20 mark *new_view = log_view;
6604 05f04cdf 2022-07-20 mark return err;
6607 05f04cdf 2022-07-20 mark static const struct got_error *
6608 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6610 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6611 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6612 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6613 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6615 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6616 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6617 9b058f45 2022-06-30 mark --eos; /* border */
6619 e5a0f69f 2018-08-18 stsp switch (ch) {
6622 145b6838 2022-06-16 stsp case KEY_RIGHT:
6624 145b6838 2022-06-16 stsp case KEY_LEFT:
6626 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
6629 1e37a5c2 2019-05-12 jcs s->done = 1;
6631 4deef56f 2021-09-02 naddy case 'g':
6632 4deef56f 2021-09-02 naddy case KEY_HOME:
6633 4deef56f 2021-09-02 naddy s->selected_line = 1;
6634 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6635 640cd7ff 2022-06-22 mark view->count = 0;
6637 4deef56f 2021-09-02 naddy case 'G':
6638 4deef56f 2021-09-02 naddy case KEY_END:
6639 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6640 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6641 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6643 9b058f45 2022-06-30 mark s->selected_line = eos;
6644 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6646 640cd7ff 2022-06-22 mark view->count = 0;
6649 1e37a5c2 2019-05-12 jcs case KEY_UP:
6650 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6651 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6652 1e37a5c2 2019-05-12 jcs s->selected_line--;
6653 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6654 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6655 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6657 640cd7ff 2022-06-22 mark view->count = 0;
6659 83cc4199 2022-06-13 stsp case CTRL('u'):
6661 83cc4199 2022-06-13 stsp nscroll /= 2;
6662 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6663 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6664 ea025d1d 2020-02-22 naddy case CTRL('b'):
6666 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6667 640cd7ff 2022-06-22 mark if (view->count > 1)
6668 640cd7ff 2022-06-22 mark nscroll += nscroll;
6669 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6670 640cd7ff 2022-06-22 mark view->count = 0;
6673 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6674 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6676 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6679 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6680 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6681 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6682 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6683 1e37a5c2 2019-05-12 jcs s->selected_line++;
6684 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6685 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6687 640cd7ff 2022-06-22 mark view->count = 0;
6690 1e37a5c2 2019-05-12 jcs case 'p': {
6691 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6693 640cd7ff 2022-06-22 mark view->count = 0;
6694 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6695 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6696 1e37a5c2 2019-05-12 jcs if (id == NULL)
6698 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6699 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6700 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6701 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6702 1e37a5c2 2019-05-12 jcs int obj_type;
6703 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6704 1e37a5c2 2019-05-12 jcs s->repo, id);
6707 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6708 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6709 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6710 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6713 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6714 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6715 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6718 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6719 a44927cc 2022-04-07 stsp pcommit, s->path);
6720 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6721 e5a0f69f 2018-08-18 stsp if (err) {
6722 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6723 1e37a5c2 2019-05-12 jcs err = NULL;
6724 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6727 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6729 1e37a5c2 2019-05-12 jcs free(blob_id);
6730 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6731 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6732 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6735 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6736 d7b5a0e8 2022-04-20 stsp &pid->id);
6737 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6739 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6740 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6742 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6747 1e37a5c2 2019-05-12 jcs s->done = 1;
6748 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6749 1e37a5c2 2019-05-12 jcs s->done = 0;
6750 1e37a5c2 2019-05-12 jcs if (thread_err)
6752 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6753 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6754 a5388363 2020-12-01 naddy err = run_blame(view);
6759 61417565 2022-06-20 mark case 'C': {
6760 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6762 640cd7ff 2022-06-22 mark view->count = 0;
6763 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6764 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6766 1e37a5c2 2019-05-12 jcs s->done = 1;
6767 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6768 1e37a5c2 2019-05-12 jcs s->done = 0;
6769 1e37a5c2 2019-05-12 jcs if (thread_err)
6771 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6772 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6773 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6774 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6775 a5388363 2020-12-01 naddy err = run_blame(view);
6781 05f04cdf 2022-07-20 mark view->count = 0;
6782 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6783 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6784 136e2bd4 2022-07-23 mark if (s->id_to_log)
6785 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6787 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6788 1e37a5c2 2019-05-12 jcs case '\r': {
6789 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6790 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6791 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6793 640cd7ff 2022-06-22 mark view->count = 0;
6794 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6795 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6796 1e37a5c2 2019-05-12 jcs if (id == NULL)
6798 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6801 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6802 c0f61fa4 2022-07-11 mark if (*new_view) {
6803 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6804 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6807 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6809 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6810 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6812 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6813 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6814 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6815 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6816 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6820 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6821 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6822 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6824 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6827 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6828 c0f61fa4 2022-07-11 mark s->selected_line;
6829 c0f61fa4 2022-07-11 mark if (*new_view)
6830 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6831 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6832 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6833 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6838 e78dc838 2020-12-04 stsp view->focussed = 0;
6839 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6840 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6841 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6842 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6843 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6844 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6847 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6850 e78dc838 2020-12-04 stsp view->focus_child = 1;
6852 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6857 83cc4199 2022-06-13 stsp case CTRL('d'):
6859 83cc4199 2022-06-13 stsp nscroll /= 2;
6860 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6861 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6862 ea025d1d 2020-02-22 naddy case CTRL('f'):
6865 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6866 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6867 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6868 640cd7ff 2022-06-22 mark view->count = 0;
6871 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6872 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6873 83cc4199 2022-06-13 stsp s->selected_line +=
6874 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6875 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6877 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6878 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6880 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6881 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6883 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6884 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6885 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6886 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6890 640cd7ff 2022-06-22 mark view->count = 0;
6893 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6896 917d79a7 2022-07-01 stsp static const struct got_error *
6897 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6899 917d79a7 2022-07-01 stsp const struct got_error *err;
6900 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6902 917d79a7 2022-07-01 stsp view->count = 0;
6903 917d79a7 2022-07-01 stsp s->done = 1;
6904 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6905 917d79a7 2022-07-01 stsp s->done = 0;
6907 917d79a7 2022-07-01 stsp return err;
6908 917d79a7 2022-07-01 stsp return run_blame(view);
6911 a70480e0 2018-06-23 stsp static const struct got_error *
6912 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6914 af21bb7e 2023-04-12 mark const struct got_error *io_err, *error;
6915 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6916 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6917 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6918 0587e10c 2020-07-23 stsp char *link_target = NULL;
6919 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6920 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6921 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6923 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6924 af21bb7e 2023-04-12 mark struct tog_io *tog_io = NULL;
6925 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6927 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6928 a70480e0 2018-06-23 stsp switch (ch) {
6930 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6933 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6934 69069811 2018-08-02 stsp if (repo_path == NULL)
6935 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6939 17020d27 2019-03-07 stsp usage_blame();
6940 a70480e0 2018-06-23 stsp /* NOTREACHED */
6944 a70480e0 2018-06-23 stsp argc -= optind;
6945 a70480e0 2018-06-23 stsp argv += optind;
6947 f135c941 2020-02-20 stsp if (argc != 1)
6948 a70480e0 2018-06-23 stsp usage_blame();
6950 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6951 0ae84acc 2022-06-15 tracey if (error != NULL)
6952 0ae84acc 2022-06-15 tracey goto done;
6954 69069811 2018-08-02 stsp if (repo_path == NULL) {
6955 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6956 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6957 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6958 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6959 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6960 c156c7a4 2020-12-18 stsp goto done;
6961 f135c941 2020-02-20 stsp if (worktree)
6962 eb41ed75 2019-02-05 stsp repo_path =
6963 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6965 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6966 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6967 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6968 c156c7a4 2020-12-18 stsp goto done;
6972 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6973 c02c541e 2019-03-29 stsp if (error != NULL)
6974 8e94dd5b 2019-01-04 stsp goto done;
6976 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6977 f135c941 2020-02-20 stsp worktree);
6978 c02c541e 2019-03-29 stsp if (error)
6979 92205607 2019-01-04 stsp goto done;
6981 af21bb7e 2023-04-12 mark error = init_curses(&tog_io);
6982 af21bb7e 2023-04-12 mark if (error)
6983 af21bb7e 2023-04-12 mark goto done;
6985 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6986 51a10b52 2020-12-26 stsp if (error)
6987 51a10b52 2020-12-26 stsp goto done;
6989 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6990 eb41ed75 2019-02-05 stsp if (error)
6991 69069811 2018-08-02 stsp goto done;
6993 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6994 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6995 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6996 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6997 a70480e0 2018-06-23 stsp if (error != NULL)
6998 66b4983c 2018-06-23 stsp goto done;
6999 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7000 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7002 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7003 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7005 a19e88aa 2018-06-23 stsp if (error != NULL)
7006 8b473291 2019-02-21 stsp goto done;
7008 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7009 e1cd8fed 2018-08-01 stsp if (view == NULL) {
7010 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7011 e1cd8fed 2018-08-01 stsp goto done;
7014 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7015 a44927cc 2022-04-07 stsp if (error)
7016 a44927cc 2022-04-07 stsp goto done;
7018 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7019 a44927cc 2022-04-07 stsp commit, repo);
7020 7cbe629d 2018-08-04 stsp if (error)
7021 7cbe629d 2018-08-04 stsp goto done;
7023 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7024 78756c87 2020-11-24 stsp commit_id, repo);
7025 0587e10c 2020-07-23 stsp if (error)
7026 0587e10c 2020-07-23 stsp goto done;
7027 12314ad4 2019-08-31 stsp if (worktree) {
7028 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7029 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7030 12314ad4 2019-08-31 stsp worktree = NULL;
7032 af21bb7e 2023-04-12 mark error = view_loop(view, tog_io);
7034 69069811 2018-08-02 stsp free(repo_path);
7035 f135c941 2020-02-20 stsp free(in_repo_path);
7036 0587e10c 2020-07-23 stsp free(link_target);
7037 69069811 2018-08-02 stsp free(cwd);
7038 a70480e0 2018-06-23 stsp free(commit_id);
7039 a44927cc 2022-04-07 stsp if (commit)
7040 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7041 eb41ed75 2019-02-05 stsp if (worktree)
7042 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7043 1d0f4054 2021-06-17 stsp if (repo) {
7044 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7045 1d0f4054 2021-06-17 stsp if (error == NULL)
7046 1d0f4054 2021-06-17 stsp error = close_err;
7048 0ae84acc 2022-06-15 tracey if (pack_fds) {
7049 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7050 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7051 0ae84acc 2022-06-15 tracey if (error == NULL)
7052 0ae84acc 2022-06-15 tracey error = pack_err;
7054 af21bb7e 2023-04-12 mark if (tog_io != NULL) {
7055 af21bb7e 2023-04-12 mark io_err = tog_io_close(tog_io);
7056 af21bb7e 2023-04-12 mark if (error == NULL)
7057 af21bb7e 2023-04-12 mark error = io_err;
7059 51a10b52 2020-12-26 stsp tog_free_refs();
7060 a70480e0 2018-06-23 stsp return error;
7063 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7064 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7066 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7067 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7068 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7069 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7070 0c6ad1bc 2022-09-09 mark char *index = NULL;
7071 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7072 d627976f 2023-02-01 mark int width, n, nentries, scrollx, i = 1;
7073 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7075 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7076 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7077 9b058f45 2022-06-30 mark --limit; /* border */
7079 f7d12f7e 2018-08-01 stsp werase(view->window);
7081 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7082 ffd1d5e5 2018-06-23 stsp return NULL;
7084 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7087 ffd1d5e5 2018-06-23 stsp return err;
7088 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7089 a3404814 2018-09-02 stsp wstandout(view->window);
7090 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7092 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7093 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7094 0c6ad1bc 2022-09-09 mark free(wline);
7095 0c6ad1bc 2022-09-09 mark wline = NULL;
7096 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
7097 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
7099 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7100 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7101 a3404814 2018-09-02 stsp wstandend(view->window);
7102 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
7103 0c6ad1bc 2022-09-09 mark return NULL;
7105 df68a56b 2022-08-12 mark i += s->selected;
7106 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
7107 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
7108 df68a56b 2022-08-12 mark if (s->tree != s->root)
7109 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
7111 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
7112 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
7113 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7114 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
7115 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7116 0c6ad1bc 2022-09-09 mark free(index);
7118 ce52c690 2018-06-23 stsp return err;
7119 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7120 2550e4c3 2018-07-13 stsp free(wline);
7121 2550e4c3 2018-07-13 stsp wline = NULL;
7122 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7123 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7124 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7125 ffd1d5e5 2018-06-23 stsp return NULL;
7126 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7127 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7128 a1eca9bb 2018-06-23 stsp return NULL;
7130 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7131 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7132 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7133 0cf4efb1 2018-09-29 stsp if (view->focussed)
7134 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7135 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7137 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7138 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7139 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7140 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7141 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7142 ffd1d5e5 2018-06-23 stsp return NULL;
7146 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7149 d627976f 2023-02-01 mark view->maxx = 0;
7150 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7151 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7152 848d6979 2019-08-12 stsp const char *modestr = "";
7153 56e0773d 2019-11-28 stsp mode_t mode;
7155 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7156 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7158 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7159 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7160 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7162 638f9024 2019-05-13 stsp return got_error_from_errno(
7163 230a42bd 2019-05-11 jcs "got_object_id_str");
7165 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7166 63c5ca5d 2019-08-24 stsp modestr = "$";
7167 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7170 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7171 d86d3b18 2020-12-01 naddy te, s->repo);
7172 0d6c6ee3 2020-05-20 stsp if (err) {
7173 0d6c6ee3 2020-05-20 stsp free(id_str);
7174 0d6c6ee3 2020-05-20 stsp return err;
7176 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
7177 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7178 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7180 848d6979 2019-08-12 stsp modestr = "@";
7182 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7183 848d6979 2019-08-12 stsp modestr = "/";
7184 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7185 848d6979 2019-08-12 stsp modestr = "*";
7186 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7187 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7188 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7189 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7190 1d13200f 2018-07-12 stsp free(id_str);
7191 0d6c6ee3 2020-05-20 stsp free(link_target);
7192 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7194 1d13200f 2018-07-12 stsp free(id_str);
7195 0d6c6ee3 2020-05-20 stsp free(link_target);
7197 d627976f 2023-02-01 mark /* use full line width to determine view->maxx */
7198 d627976f 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7199 ffd1d5e5 2018-06-23 stsp if (err) {
7200 ffd1d5e5 2018-06-23 stsp free(line);
7203 d627976f 2023-02-01 mark view->maxx = MAX(view->maxx, width);
7204 d627976f 2023-02-01 mark free(wline);
7205 d627976f 2023-02-01 mark wline = NULL;
7207 d627976f 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
7208 d627976f 2023-02-01 mark view->ncols, 0, 0);
7209 d627976f 2023-02-01 mark if (err) {
7210 d627976f 2023-02-01 mark free(line);
7213 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7214 0cf4efb1 2018-09-29 stsp if (view->focussed)
7215 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7216 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7218 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7220 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7221 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7222 d627976f 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
7224 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7225 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7226 b637f33f 2023-02-01 mark if (width < view->ncols)
7227 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7228 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7229 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7230 ffd1d5e5 2018-06-23 stsp free(line);
7231 2550e4c3 2018-07-13 stsp free(wline);
7232 2550e4c3 2018-07-13 stsp wline = NULL;
7234 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7235 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7236 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7240 ffd1d5e5 2018-06-23 stsp return err;
7243 ffd1d5e5 2018-06-23 stsp static void
7244 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7246 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7247 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7248 fa86c4bf 2020-11-29 stsp int i = 0;
7250 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7253 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7254 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7255 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7256 fa86c4bf 2020-11-29 stsp if (!isroot)
7257 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7260 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7261 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7265 9b058f45 2022-06-30 mark static const struct got_error *
7266 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
7268 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7269 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7270 ffd1d5e5 2018-06-23 stsp int n = 0;
7272 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7273 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7274 694d3271 2020-12-01 naddy s->first_displayed_entry);
7276 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7278 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7279 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7280 94b80cfa 2022-08-01 mark if (last) {
7281 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7282 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
7284 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7285 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7286 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7290 9b058f45 2022-06-30 mark return NULL;
7293 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7294 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7295 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7297 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7298 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7299 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7301 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7302 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7303 56e0773d 2019-11-28 stsp + 1 /* slash */;
7305 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7307 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7308 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7309 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7311 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7312 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7313 d9765a41 2018-06-23 stsp while (pt) {
7314 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7315 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7316 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7317 cb2ebc8a 2018-06-23 stsp goto done;
7319 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7320 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7321 cb2ebc8a 2018-06-23 stsp goto done;
7323 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7326 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7327 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7328 ce52c690 2018-06-23 stsp goto done;
7332 ce52c690 2018-06-23 stsp if (err) {
7333 ce52c690 2018-06-23 stsp free(*path);
7334 ce52c690 2018-06-23 stsp *path = NULL;
7336 ce52c690 2018-06-23 stsp return err;
7339 ce52c690 2018-06-23 stsp static const struct got_error *
7340 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7341 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7342 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7344 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7345 ce52c690 2018-06-23 stsp char *path;
7346 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7348 a0de39f3 2019-08-09 stsp *new_view = NULL;
7350 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7352 ce52c690 2018-06-23 stsp return err;
7354 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7355 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7356 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7357 83ce39e3 2019-08-12 stsp goto done;
7360 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7361 e5a0f69f 2018-08-18 stsp if (err) {
7362 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7363 fc06ba56 2019-08-22 stsp err = NULL;
7364 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7366 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7368 83ce39e3 2019-08-12 stsp free(path);
7369 69efd4c4 2018-07-18 stsp return err;
7372 69efd4c4 2018-07-18 stsp static const struct got_error *
7373 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7374 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7376 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7377 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7378 69efd4c4 2018-07-18 stsp char *path;
7380 a0de39f3 2019-08-09 stsp *new_view = NULL;
7382 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7383 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7384 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7386 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7388 69efd4c4 2018-07-18 stsp return err;
7390 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7393 e5a0f69f 2018-08-18 stsp view_close(log_view);
7395 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7396 cb2ebc8a 2018-06-23 stsp free(path);
7397 cb2ebc8a 2018-06-23 stsp return err;
7400 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7401 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7402 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7404 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7405 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7406 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7407 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7409 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7410 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7412 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7413 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7414 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7416 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7418 bc573f3b 2021-07-10 stsp goto done;
7421 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7422 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7423 bc573f3b 2021-07-10 stsp * closed on demand.
7425 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7426 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7428 bc573f3b 2021-07-10 stsp goto done;
7429 bc573f3b 2021-07-10 stsp s->tree = s->root;
7431 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7432 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7433 ffd1d5e5 2018-06-23 stsp goto done;
7435 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7436 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7437 ffd1d5e5 2018-06-23 stsp goto done;
7440 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7441 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7442 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7443 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7444 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7445 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7446 9cd7cbd1 2020-12-07 stsp goto done;
7449 fb2756b9 2018-08-04 stsp s->repo = repo;
7451 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7452 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7453 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7454 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7456 c0b01bdb 2019-11-08 stsp goto done;
7457 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7458 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7460 c0b01bdb 2019-11-08 stsp goto done;
7461 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7462 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7463 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7465 c0b01bdb 2019-11-08 stsp goto done;
7467 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7468 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7469 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7471 11b20872 2019-11-08 stsp goto done;
7473 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7474 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7476 c0b01bdb 2019-11-08 stsp goto done;
7479 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7480 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7481 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7482 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7483 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7485 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7486 bc573f3b 2021-07-10 stsp if (commit)
7487 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7489 bc573f3b 2021-07-10 stsp close_tree_view(view);
7490 ad80ab7b 2018-08-04 stsp return err;
7493 e5a0f69f 2018-08-18 stsp static const struct got_error *
7494 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7496 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7498 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7499 fb2756b9 2018-08-04 stsp free(s->tree_label);
7500 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7501 6484ec90 2018-09-29 stsp free(s->commit_id);
7502 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7503 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7504 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7505 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7506 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7507 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7508 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7509 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7510 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7511 ad80ab7b 2018-08-04 stsp free(parent);
7514 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7515 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7516 bc573f3b 2021-07-10 stsp if (s->root)
7517 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7518 7c32bd05 2019-06-22 stsp return NULL;
7521 7c32bd05 2019-06-22 stsp static const struct got_error *
7522 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7524 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7526 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7527 7c32bd05 2019-06-22 stsp return NULL;
7530 7c32bd05 2019-06-22 stsp static int
7531 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7533 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7535 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7539 7c32bd05 2019-06-22 stsp static const struct got_error *
7540 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7542 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7543 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7545 7c32bd05 2019-06-22 stsp if (!view->searching) {
7546 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7547 7c32bd05 2019-06-22 stsp return NULL;
7550 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7551 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7552 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7553 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7554 56e0773d 2019-11-28 stsp s->selected_entry);
7556 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7558 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7559 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7561 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7562 56e0773d 2019-11-28 stsp s->selected_entry);
7565 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7566 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7567 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7568 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7570 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7573 7c32bd05 2019-06-22 stsp while (1) {
7574 56e0773d 2019-11-28 stsp if (te == NULL) {
7575 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7576 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7577 ac66afb8 2019-06-24 stsp return NULL;
7579 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7580 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7582 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7585 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7586 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7587 56e0773d 2019-11-28 stsp s->matched_entry = te;
7591 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7592 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7594 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7597 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7598 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7599 7c32bd05 2019-06-22 stsp s->selected = 0;
7602 e5a0f69f 2018-08-18 stsp return NULL;
7605 ad80ab7b 2018-08-04 stsp static const struct got_error *
7606 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7608 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7609 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7610 e5a0f69f 2018-08-18 stsp char *parent_path;
7612 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7614 e5a0f69f 2018-08-18 stsp return err;
7616 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7617 e5a0f69f 2018-08-18 stsp free(parent_path);
7619 9b058f45 2022-06-30 mark view_border(view);
7620 e5a0f69f 2018-08-18 stsp return err;
7623 94b80cfa 2022-08-01 mark static const struct got_error *
7624 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7626 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7627 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7628 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7629 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7630 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7631 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7633 94b80cfa 2022-08-01 mark g = view->gline;
7634 94b80cfa 2022-08-01 mark view->gline = 0;
7636 94b80cfa 2022-08-01 mark if (g == 0)
7638 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7639 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7641 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7642 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7643 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7645 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7646 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7647 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7649 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7650 94b80cfa 2022-08-01 mark last += off;
7652 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7653 94b80cfa 2022-08-01 mark s->selected = g - first;
7654 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7657 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7658 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7662 94b80cfa 2022-08-01 mark if (i < g) {
7663 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7665 94b80cfa 2022-08-01 mark return err;
7666 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7667 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7668 94b80cfa 2022-08-01 mark first + s->selected < g &&
7669 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7670 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7671 94b80cfa 2022-08-01 mark first += off;
7672 94b80cfa 2022-08-01 mark s->selected = g - first;
7674 94b80cfa 2022-08-01 mark } else if (i > g)
7675 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7677 94b80cfa 2022-08-01 mark if (g < nlines &&
7678 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7679 94b80cfa 2022-08-01 mark s->selected = g - 1;
7681 94b80cfa 2022-08-01 mark return NULL;
7684 e5a0f69f 2018-08-18 stsp static const struct got_error *
7685 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7687 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7688 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7689 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7690 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7692 94b80cfa 2022-08-01 mark if (view->gline)
7693 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7695 e5a0f69f 2018-08-18 stsp switch (ch) {
7698 d627976f 2023-02-01 mark case KEY_RIGHT:
7700 d627976f 2023-02-01 mark case KEY_LEFT:
7702 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
7705 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7706 640cd7ff 2022-06-22 mark view->count = 0;
7709 640cd7ff 2022-06-22 mark view->count = 0;
7710 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7712 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7715 640cd7ff 2022-06-22 mark view->count = 0;
7716 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7718 e4526bf5 2021-09-03 naddy case 'g':
7720 e4526bf5 2021-09-03 naddy case KEY_HOME:
7721 e4526bf5 2021-09-03 naddy s->selected = 0;
7722 640cd7ff 2022-06-22 mark view->count = 0;
7723 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7724 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7725 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7727 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7729 e4526bf5 2021-09-03 naddy case 'G':
7731 9b058f45 2022-06-30 mark case KEY_END: {
7732 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7734 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7735 9b058f45 2022-06-30 mark --eos; /* border */
7736 e4526bf5 2021-09-03 naddy s->selected = 0;
7737 640cd7ff 2022-06-22 mark view->count = 0;
7738 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7739 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7740 e4526bf5 2021-09-03 naddy if (te == NULL) {
7741 ad055527 2022-07-16 mark if (s->tree != s->root) {
7742 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7747 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7748 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7750 e4526bf5 2021-09-03 naddy if (n > 0)
7751 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7755 1e37a5c2 2019-05-12 jcs case KEY_UP:
7756 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7757 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7758 1e37a5c2 2019-05-12 jcs s->selected--;
7761 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7762 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7763 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7764 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7765 640cd7ff 2022-06-22 mark view->count = 0;
7767 83cc4199 2022-06-13 stsp case CTRL('u'):
7769 83cc4199 2022-06-13 stsp nscroll /= 2;
7770 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7771 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7772 ea025d1d 2020-02-22 naddy case CTRL('b'):
7774 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7775 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7776 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7777 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7779 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7780 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7782 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7783 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7784 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7785 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7786 640cd7ff 2022-06-22 mark view->count = 0;
7789 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7790 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7791 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7792 1e37a5c2 2019-05-12 jcs s->selected++;
7795 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7796 640cd7ff 2022-06-22 mark == NULL) {
7797 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7798 640cd7ff 2022-06-22 mark view->count = 0;
7801 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7803 83cc4199 2022-06-13 stsp case CTRL('d'):
7805 83cc4199 2022-06-13 stsp nscroll /= 2;
7806 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7807 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7808 ea025d1d 2020-02-22 naddy case CTRL('f'):
7810 48bb96f0 2022-06-20 naddy case ' ':
7811 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7813 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7814 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7815 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7816 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7818 640cd7ff 2022-06-22 mark view->count = 0;
7821 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7823 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7825 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7826 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7827 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7828 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7829 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7830 640cd7ff 2022-06-22 mark view->count = 0;
7833 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7834 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7836 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7837 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7838 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7839 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7840 1e37a5c2 2019-05-12 jcs s->selected_entry =
7841 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7842 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7843 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7844 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7848 1e37a5c2 2019-05-12 jcs free(parent);
7849 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7850 56e0773d 2019-11-28 stsp s->selected_entry))) {
7851 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7852 640cd7ff 2022-06-22 mark view->count = 0;
7853 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7854 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7857 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7858 941e9f74 2019-05-21 stsp if (err) {
7859 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7862 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7863 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7865 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7866 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7867 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7868 640cd7ff 2022-06-22 mark view->count = 0;
7871 640cd7ff 2022-06-22 mark view->count = 0;
7875 ffd1d5e5 2018-06-23 stsp return err;
7878 ffd1d5e5 2018-06-23 stsp __dead static void
7879 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7882 87411fa9 2022-06-16 stsp fprintf(stderr,
7883 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7884 ffd1d5e5 2018-06-23 stsp getprogname());
7888 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7889 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7891 af21bb7e 2023-04-12 mark const struct got_error *io_err, *error;
7892 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7893 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7894 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7895 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7896 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7897 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7898 4e97c21c 2020-12-06 stsp char *label = NULL;
7899 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7900 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7902 5221c383 2018-08-01 stsp struct tog_view *view;
7903 af21bb7e 2023-04-12 mark struct tog_io *tog_io = NULL;
7904 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7906 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7907 ffd1d5e5 2018-06-23 stsp switch (ch) {
7909 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7912 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7913 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7914 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7918 e99e2d15 2020-11-24 naddy usage_tree();
7919 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7923 ffd1d5e5 2018-06-23 stsp argc -= optind;
7924 ffd1d5e5 2018-06-23 stsp argv += optind;
7926 55cccc34 2020-02-20 stsp if (argc > 1)
7927 e99e2d15 2020-11-24 naddy usage_tree();
7929 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7930 0ae84acc 2022-06-15 tracey if (error != NULL)
7931 0ae84acc 2022-06-15 tracey goto done;
7933 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7934 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7935 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7936 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7937 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7938 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7939 c156c7a4 2020-12-18 stsp goto done;
7940 55cccc34 2020-02-20 stsp if (worktree)
7941 52185f70 2019-02-05 stsp repo_path =
7942 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7944 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7945 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7946 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7947 c156c7a4 2020-12-18 stsp goto done;
7951 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7952 c02c541e 2019-03-29 stsp if (error != NULL)
7953 52185f70 2019-02-05 stsp goto done;
7955 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7956 55cccc34 2020-02-20 stsp repo, worktree);
7957 55cccc34 2020-02-20 stsp if (error)
7958 55cccc34 2020-02-20 stsp goto done;
7960 af21bb7e 2023-04-12 mark error = init_curses(&tog_io);
7961 af21bb7e 2023-04-12 mark if (error)
7962 af21bb7e 2023-04-12 mark goto done;
7964 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7965 c02c541e 2019-03-29 stsp if (error)
7966 52185f70 2019-02-05 stsp goto done;
7968 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7969 51a10b52 2020-12-26 stsp if (error)
7970 51a10b52 2020-12-26 stsp goto done;
7972 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7973 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7974 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7975 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7976 4e97c21c 2020-12-06 stsp if (error)
7977 4e97c21c 2020-12-06 stsp goto done;
7978 4e97c21c 2020-12-06 stsp head_ref_name = label;
7980 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7981 4e97c21c 2020-12-06 stsp if (error == NULL)
7982 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7983 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7984 4e97c21c 2020-12-06 stsp goto done;
7985 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7986 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7987 4e97c21c 2020-12-06 stsp if (error)
7988 4e97c21c 2020-12-06 stsp goto done;
7991 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7992 a44927cc 2022-04-07 stsp if (error)
7993 a44927cc 2022-04-07 stsp goto done;
7995 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7996 5221c383 2018-08-01 stsp if (view == NULL) {
7997 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7998 5221c383 2018-08-01 stsp goto done;
8000 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8001 ad80ab7b 2018-08-04 stsp if (error)
8002 ad80ab7b 2018-08-04 stsp goto done;
8003 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8004 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
8005 d91faf3b 2020-12-01 naddy in_repo_path);
8006 55cccc34 2020-02-20 stsp if (error)
8007 55cccc34 2020-02-20 stsp goto done;
8010 55cccc34 2020-02-20 stsp if (worktree) {
8011 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8012 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8013 55cccc34 2020-02-20 stsp worktree = NULL;
8015 af21bb7e 2023-04-12 mark error = view_loop(view, tog_io);
8017 52185f70 2019-02-05 stsp free(repo_path);
8018 e4a0e26d 2020-02-20 stsp free(cwd);
8019 ffd1d5e5 2018-06-23 stsp free(commit_id);
8020 4e97c21c 2020-12-06 stsp free(label);
8022 486cd271 2020-12-06 stsp got_ref_close(ref);
8023 1d0f4054 2021-06-17 stsp if (repo) {
8024 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8025 1d0f4054 2021-06-17 stsp if (error == NULL)
8026 1d0f4054 2021-06-17 stsp error = close_err;
8028 0ae84acc 2022-06-15 tracey if (pack_fds) {
8029 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8030 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8031 0ae84acc 2022-06-15 tracey if (error == NULL)
8032 0ae84acc 2022-06-15 tracey error = pack_err;
8034 af21bb7e 2023-04-12 mark if (tog_io != NULL) {
8035 af21bb7e 2023-04-12 mark io_err = tog_io_close(tog_io);
8036 af21bb7e 2023-04-12 mark if (error == NULL)
8037 af21bb7e 2023-04-12 mark error = io_err;
8039 51a10b52 2020-12-26 stsp tog_free_refs();
8040 ffd1d5e5 2018-06-23 stsp return error;
8043 6458efa5 2020-11-24 stsp static const struct got_error *
8044 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8046 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8047 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8049 6458efa5 2020-11-24 stsp s->nrefs = 0;
8050 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8051 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
8052 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
8053 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
8054 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
8057 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8058 6458efa5 2020-11-24 stsp if (re == NULL)
8059 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8061 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8062 8924d611 2020-12-26 stsp if (re->ref == NULL)
8063 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8064 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8065 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8068 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8069 6458efa5 2020-11-24 stsp return NULL;
8073 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8075 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8077 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8078 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8079 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8080 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8085 6458efa5 2020-11-24 stsp static const struct got_error *
8086 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8088 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8089 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8091 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8092 6458efa5 2020-11-24 stsp s->repo = repo;
8094 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8095 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8097 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8099 6458efa5 2020-11-24 stsp return err;
8101 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8102 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8103 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8104 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8106 6458efa5 2020-11-24 stsp goto done;
8108 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8109 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8110 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8112 6458efa5 2020-11-24 stsp goto done;
8114 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8115 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8116 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8118 cc488aa7 2022-01-23 stsp goto done;
8120 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
8121 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
8122 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
8124 6458efa5 2020-11-24 stsp goto done;
8127 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8128 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8129 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8130 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8131 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8134 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8135 6458efa5 2020-11-24 stsp return err;
8138 6458efa5 2020-11-24 stsp static const struct got_error *
8139 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8141 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8143 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8144 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8146 6458efa5 2020-11-24 stsp return NULL;
8149 6458efa5 2020-11-24 stsp static const struct got_error *
8150 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8151 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8153 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8154 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8155 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8156 6458efa5 2020-11-24 stsp int obj_type;
8158 c42c9805 2020-11-24 stsp *commit_id = NULL;
8160 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8162 6458efa5 2020-11-24 stsp return err;
8164 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8166 6458efa5 2020-11-24 stsp goto done;
8168 6458efa5 2020-11-24 stsp switch (obj_type) {
8169 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8170 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8172 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8173 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8175 6458efa5 2020-11-24 stsp goto done;
8176 c42c9805 2020-11-24 stsp free(obj_id);
8177 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8178 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8180 6458efa5 2020-11-24 stsp goto done;
8181 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8182 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8183 c42c9805 2020-11-24 stsp goto done;
8185 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8186 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8187 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8188 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8189 c42c9805 2020-11-24 stsp goto done;
8193 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8199 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8200 c42c9805 2020-11-24 stsp if (err) {
8201 c42c9805 2020-11-24 stsp free(*commit_id);
8202 c42c9805 2020-11-24 stsp *commit_id = NULL;
8204 c42c9805 2020-11-24 stsp return err;
8207 c42c9805 2020-11-24 stsp static const struct got_error *
8208 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8209 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8211 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8212 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8213 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8215 c42c9805 2020-11-24 stsp *new_view = NULL;
8217 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8218 c42c9805 2020-11-24 stsp if (err) {
8219 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8220 c42c9805 2020-11-24 stsp return err;
8222 c42c9805 2020-11-24 stsp return NULL;
8225 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8226 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8227 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8228 6458efa5 2020-11-24 stsp goto done;
8231 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8232 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
8235 6458efa5 2020-11-24 stsp view_close(log_view);
8237 6458efa5 2020-11-24 stsp *new_view = log_view;
8238 c42c9805 2020-11-24 stsp free(commit_id);
8239 6458efa5 2020-11-24 stsp return err;
8242 ce5b7c56 2019-07-09 stsp static void
8243 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8245 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8246 34ba6917 2020-11-30 stsp int i = 0;
8248 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8251 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8252 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8253 34ba6917 2020-11-30 stsp if (re == NULL)
8255 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8256 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8260 9b058f45 2022-06-30 mark static const struct got_error *
8261 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
8263 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8264 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8265 6458efa5 2020-11-24 stsp int n = 0;
8267 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8268 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8270 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8272 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8273 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
8274 94b80cfa 2022-08-01 mark if (last) {
8275 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
8276 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
8278 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8279 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8280 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8284 9b058f45 2022-06-30 mark return NULL;
8287 6458efa5 2020-11-24 stsp static const struct got_error *
8288 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8290 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8292 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8293 6458efa5 2020-11-24 stsp return NULL;
8296 6458efa5 2020-11-24 stsp static int
8297 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8299 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8301 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8305 6458efa5 2020-11-24 stsp static const struct got_error *
8306 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8308 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8309 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8311 6458efa5 2020-11-24 stsp if (!view->searching) {
8312 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8313 6458efa5 2020-11-24 stsp return NULL;
8316 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8317 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8318 6458efa5 2020-11-24 stsp if (s->selected_entry)
8319 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8321 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8322 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8324 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8325 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8327 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8328 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8331 487cd7d2 2021-12-17 stsp if (s->selected_entry)
8332 487cd7d2 2021-12-17 stsp re = s->selected_entry;
8333 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
8334 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8336 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8339 6458efa5 2020-11-24 stsp while (1) {
8340 6458efa5 2020-11-24 stsp if (re == NULL) {
8341 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8342 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8343 6458efa5 2020-11-24 stsp return NULL;
8345 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8346 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8348 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8351 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8352 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8353 6458efa5 2020-11-24 stsp s->matched_entry = re;
8357 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8358 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8360 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8363 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8364 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8365 6458efa5 2020-11-24 stsp s->selected = 0;
8368 6458efa5 2020-11-24 stsp return NULL;
8371 6458efa5 2020-11-24 stsp static const struct got_error *
8372 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8374 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8375 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8376 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8377 6458efa5 2020-11-24 stsp char *line = NULL;
8378 6458efa5 2020-11-24 stsp wchar_t *wline;
8379 6458efa5 2020-11-24 stsp struct tog_color *tc;
8380 0ed6bc90 2023-02-01 mark int width, n, scrollx;
8381 6458efa5 2020-11-24 stsp int limit = view->nlines;
8383 6458efa5 2020-11-24 stsp werase(view->window);
8385 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8386 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8387 9b058f45 2022-06-30 mark --limit; /* border */
8389 6458efa5 2020-11-24 stsp if (limit == 0)
8390 6458efa5 2020-11-24 stsp return NULL;
8392 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8394 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8395 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8396 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8398 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8399 6458efa5 2020-11-24 stsp if (err) {
8400 6458efa5 2020-11-24 stsp free(line);
8401 6458efa5 2020-11-24 stsp return err;
8403 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8404 6458efa5 2020-11-24 stsp wstandout(view->window);
8405 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8406 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8407 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8408 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8409 6458efa5 2020-11-24 stsp wstandend(view->window);
8410 6458efa5 2020-11-24 stsp free(wline);
8411 6458efa5 2020-11-24 stsp wline = NULL;
8412 6458efa5 2020-11-24 stsp free(line);
8413 6458efa5 2020-11-24 stsp line = NULL;
8414 6458efa5 2020-11-24 stsp if (--limit <= 0)
8415 6458efa5 2020-11-24 stsp return NULL;
8418 0ed6bc90 2023-02-01 mark view->maxx = 0;
8419 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8420 6458efa5 2020-11-24 stsp char *line = NULL;
8421 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8423 b4996bee 2022-06-16 stsp if (s->show_date) {
8424 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8425 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8426 b4996bee 2022-06-16 stsp struct got_object_id *id;
8427 b4996bee 2022-06-16 stsp struct tm tm;
8430 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8432 b4996bee 2022-06-16 stsp return err;
8433 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8434 b4996bee 2022-06-16 stsp if (err) {
8435 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8437 b4996bee 2022-06-16 stsp return err;
8439 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8441 b4996bee 2022-06-16 stsp if (err) {
8443 b4996bee 2022-06-16 stsp return err;
8445 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8446 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8448 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8449 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8452 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8453 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8454 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8455 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8457 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8458 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8459 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8460 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8461 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8462 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8463 6458efa5 2020-11-24 stsp struct got_object_id *id;
8464 6458efa5 2020-11-24 stsp char *id_str;
8465 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8467 6458efa5 2020-11-24 stsp return err;
8468 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8469 6458efa5 2020-11-24 stsp if (err) {
8471 6458efa5 2020-11-24 stsp return err;
8473 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8474 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8475 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8477 6458efa5 2020-11-24 stsp free(id_str);
8478 6458efa5 2020-11-24 stsp return err;
8481 6458efa5 2020-11-24 stsp free(id_str);
8482 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8483 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8484 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8486 0ed6bc90 2023-02-01 mark /* use full line width to determine view->maxx */
8487 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8488 6458efa5 2020-11-24 stsp if (err) {
8489 6458efa5 2020-11-24 stsp free(line);
8490 6458efa5 2020-11-24 stsp return err;
8492 0ed6bc90 2023-02-01 mark view->maxx = MAX(view->maxx, width);
8493 0ed6bc90 2023-02-01 mark free(wline);
8494 0ed6bc90 2023-02-01 mark wline = NULL;
8496 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
8497 0ed6bc90 2023-02-01 mark view->ncols, 0, 0);
8498 0ed6bc90 2023-02-01 mark if (err) {
8499 0ed6bc90 2023-02-01 mark free(line);
8500 0ed6bc90 2023-02-01 mark return err;
8502 6458efa5 2020-11-24 stsp if (n == s->selected) {
8503 6458efa5 2020-11-24 stsp if (view->focussed)
8504 6458efa5 2020-11-24 stsp wstandout(view->window);
8505 6458efa5 2020-11-24 stsp s->selected_entry = re;
8507 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8509 6458efa5 2020-11-24 stsp wattr_on(view->window,
8510 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8511 0ed6bc90 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
8513 6458efa5 2020-11-24 stsp wattr_off(view->window,
8514 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8515 b637f33f 2023-02-01 mark if (width < view->ncols)
8516 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8517 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8518 6458efa5 2020-11-24 stsp wstandend(view->window);
8519 6458efa5 2020-11-24 stsp free(line);
8520 6458efa5 2020-11-24 stsp free(wline);
8521 6458efa5 2020-11-24 stsp wline = NULL;
8523 6458efa5 2020-11-24 stsp s->ndisplayed++;
8524 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8527 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8530 9b058f45 2022-06-30 mark view_border(view);
8531 6458efa5 2020-11-24 stsp return err;
8534 6458efa5 2020-11-24 stsp static const struct got_error *
8535 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8536 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8538 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8539 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8540 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8542 c42c9805 2020-11-24 stsp *new_view = NULL;
8544 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8545 c42c9805 2020-11-24 stsp if (err) {
8546 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8547 c42c9805 2020-11-24 stsp return err;
8549 c42c9805 2020-11-24 stsp return NULL;
8553 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8554 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8555 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8556 c42c9805 2020-11-24 stsp goto done;
8559 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8560 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8562 c42c9805 2020-11-24 stsp goto done;
8564 c42c9805 2020-11-24 stsp *new_view = tree_view;
8566 c42c9805 2020-11-24 stsp free(commit_id);
8567 c42c9805 2020-11-24 stsp return err;
8570 94b80cfa 2022-08-01 mark static const struct got_error *
8571 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8573 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8574 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8575 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8577 94b80cfa 2022-08-01 mark g = view->gline;
8578 94b80cfa 2022-08-01 mark view->gline = 0;
8580 94b80cfa 2022-08-01 mark if (g == 0)
8582 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8583 94b80cfa 2022-08-01 mark g = s->nrefs;
8585 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8586 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8587 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8588 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8589 94b80cfa 2022-08-01 mark return NULL;
8592 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8593 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8595 94b80cfa 2022-08-01 mark return err;
8596 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8597 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8598 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8599 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8600 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8601 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8603 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8604 94b80cfa 2022-08-01 mark s->selected = g - 1;
8606 94b80cfa 2022-08-01 mark return NULL;
8610 c42c9805 2020-11-24 stsp static const struct got_error *
8611 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8613 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8614 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8615 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8616 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8618 94b80cfa 2022-08-01 mark if (view->gline)
8619 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8621 6458efa5 2020-11-24 stsp switch (ch) {
8624 0ed6bc90 2023-02-01 mark case KEY_RIGHT:
8626 0ed6bc90 2023-02-01 mark case KEY_LEFT:
8628 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8631 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8632 640cd7ff 2022-06-22 mark view->count = 0;
8635 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8636 640cd7ff 2022-06-22 mark view->count = 0;
8639 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8640 3f6c6614 2023-01-23 mark view->action = s->sort_by_date ? "sort by date" : "sort by name";
8641 640cd7ff 2022-06-22 mark view->count = 0;
8642 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8643 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8644 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8647 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8648 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8649 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8652 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8653 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8655 6458efa5 2020-11-24 stsp case KEY_ENTER:
8656 6458efa5 2020-11-24 stsp case '\r':
8657 640cd7ff 2022-06-22 mark view->count = 0;
8658 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8660 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8663 640cd7ff 2022-06-22 mark view->count = 0;
8664 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8666 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8668 e4526bf5 2021-09-03 naddy case 'g':
8670 e4526bf5 2021-09-03 naddy case KEY_HOME:
8671 e4526bf5 2021-09-03 naddy s->selected = 0;
8672 640cd7ff 2022-06-22 mark view->count = 0;
8673 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8675 e4526bf5 2021-09-03 naddy case 'G':
8677 9b058f45 2022-06-30 mark case KEY_END: {
8678 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8680 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8681 9b058f45 2022-06-30 mark --eos; /* border */
8682 e4526bf5 2021-09-03 naddy s->selected = 0;
8683 640cd7ff 2022-06-22 mark view->count = 0;
8684 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8685 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8686 e4526bf5 2021-09-03 naddy if (re == NULL)
8688 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8689 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8691 e4526bf5 2021-09-03 naddy if (n > 0)
8692 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8696 6458efa5 2020-11-24 stsp case KEY_UP:
8697 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8698 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8699 6458efa5 2020-11-24 stsp s->selected--;
8702 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8703 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8704 640cd7ff 2022-06-22 mark view->count = 0;
8706 83cc4199 2022-06-13 stsp case CTRL('u'):
8708 83cc4199 2022-06-13 stsp nscroll /= 2;
8709 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8710 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8711 6458efa5 2020-11-24 stsp case CTRL('b'):
8713 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8714 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8715 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8716 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8717 640cd7ff 2022-06-22 mark view->count = 0;
8720 6458efa5 2020-11-24 stsp case KEY_DOWN:
8721 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8722 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8723 6458efa5 2020-11-24 stsp s->selected++;
8726 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8727 6458efa5 2020-11-24 stsp /* can't scroll any further */
8728 640cd7ff 2022-06-22 mark view->count = 0;
8731 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8733 83cc4199 2022-06-13 stsp case CTRL('d'):
8735 83cc4199 2022-06-13 stsp nscroll /= 2;
8736 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8737 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8738 6458efa5 2020-11-24 stsp case CTRL('f'):
8740 48bb96f0 2022-06-20 naddy case ' ':
8741 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8742 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8743 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8744 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8745 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8746 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8747 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8748 640cd7ff 2022-06-22 mark view->count = 0;
8751 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8753 6458efa5 2020-11-24 stsp case CTRL('l'):
8754 640cd7ff 2022-06-22 mark view->count = 0;
8755 8924d611 2020-12-26 stsp tog_free_refs();
8756 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8759 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8760 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8762 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8763 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8764 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8767 640cd7ff 2022-06-22 mark view->count = 0;
8771 6458efa5 2020-11-24 stsp return err;
8774 6458efa5 2020-11-24 stsp __dead static void
8775 6458efa5 2020-11-24 stsp usage_ref(void)
8778 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8779 6458efa5 2020-11-24 stsp getprogname());
8783 6458efa5 2020-11-24 stsp static const struct got_error *
8784 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8786 af21bb7e 2023-04-12 mark const struct got_error *io_err, *error;
8787 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8788 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8789 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8791 6458efa5 2020-11-24 stsp struct tog_view *view;
8792 af21bb7e 2023-04-12 mark struct tog_io *tog_io = NULL;
8793 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8795 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8796 6458efa5 2020-11-24 stsp switch (ch) {
8798 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8799 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8800 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8804 e99e2d15 2020-11-24 naddy usage_ref();
8805 6458efa5 2020-11-24 stsp /* NOTREACHED */
8809 6458efa5 2020-11-24 stsp argc -= optind;
8810 6458efa5 2020-11-24 stsp argv += optind;
8812 6458efa5 2020-11-24 stsp if (argc > 1)
8813 e99e2d15 2020-11-24 naddy usage_ref();
8815 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8816 0ae84acc 2022-06-15 tracey if (error != NULL)
8817 0ae84acc 2022-06-15 tracey goto done;
8819 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8820 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8821 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8822 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8823 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8824 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8825 c156c7a4 2020-12-18 stsp goto done;
8826 6458efa5 2020-11-24 stsp if (worktree)
8827 6458efa5 2020-11-24 stsp repo_path =
8828 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8830 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8831 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8832 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8833 c156c7a4 2020-12-18 stsp goto done;
8837 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8838 6458efa5 2020-11-24 stsp if (error != NULL)
8839 6458efa5 2020-11-24 stsp goto done;
8841 af21bb7e 2023-04-12 mark error = init_curses(&tog_io);
8842 af21bb7e 2023-04-12 mark if (error)
8843 af21bb7e 2023-04-12 mark goto done;
8845 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8846 51a10b52 2020-12-26 stsp if (error)
8847 51a10b52 2020-12-26 stsp goto done;
8849 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8850 6458efa5 2020-11-24 stsp if (error)
8851 6458efa5 2020-11-24 stsp goto done;
8853 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8854 6458efa5 2020-11-24 stsp if (view == NULL) {
8855 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8856 6458efa5 2020-11-24 stsp goto done;
8859 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8860 6458efa5 2020-11-24 stsp if (error)
8861 6458efa5 2020-11-24 stsp goto done;
8863 6458efa5 2020-11-24 stsp if (worktree) {
8864 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8865 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8866 6458efa5 2020-11-24 stsp worktree = NULL;
8868 af21bb7e 2023-04-12 mark error = view_loop(view, tog_io);
8870 6458efa5 2020-11-24 stsp free(repo_path);
8871 6458efa5 2020-11-24 stsp free(cwd);
8872 1d0f4054 2021-06-17 stsp if (repo) {
8873 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8874 1d0f4054 2021-06-17 stsp if (close_err)
8875 1d0f4054 2021-06-17 stsp error = close_err;
8877 0ae84acc 2022-06-15 tracey if (pack_fds) {
8878 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8879 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8880 0ae84acc 2022-06-15 tracey if (error == NULL)
8881 0ae84acc 2022-06-15 tracey error = pack_err;
8883 af21bb7e 2023-04-12 mark if (tog_io != NULL) {
8884 af21bb7e 2023-04-12 mark io_err = tog_io_close(tog_io);
8885 af21bb7e 2023-04-12 mark if (error == NULL)
8886 af21bb7e 2023-04-12 mark error = io_err;
8888 51a10b52 2020-12-26 stsp tog_free_refs();
8889 6458efa5 2020-11-24 stsp return error;
8892 ec2a9698 2022-09-15 mark static const struct got_error*
8893 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8894 ec2a9698 2022-09-15 mark const char *str)
8896 ec2a9698 2022-09-15 mark size_t len;
8898 ec2a9698 2022-09-15 mark if (win == NULL)
8899 ec2a9698 2022-09-15 mark win = stdscr;
8901 ec2a9698 2022-09-15 mark len = strlen(str);
8902 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8904 ec2a9698 2022-09-15 mark if (focus)
8905 ec2a9698 2022-09-15 mark wstandout(win);
8906 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8907 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8908 ec2a9698 2022-09-15 mark if (focus)
8909 ec2a9698 2022-09-15 mark wstandend(win);
8911 ec2a9698 2022-09-15 mark return NULL;
8914 136e2bd4 2022-07-23 mark static const struct got_error *
8915 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8919 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8920 ec2a9698 2022-09-15 mark if (p == NULL) {
8921 ec2a9698 2022-09-15 mark free(*line_offsets);
8922 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8923 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8926 ec2a9698 2022-09-15 mark *line_offsets = p;
8927 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8928 ec2a9698 2022-09-15 mark ++(*nlines);
8929 ec2a9698 2022-09-15 mark return NULL;
8932 ec2a9698 2022-09-15 mark static const struct got_error *
8933 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8937 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8938 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8939 ec2a9698 2022-09-15 mark size_t len = 1;
8941 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8944 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8945 ec2a9698 2022-09-15 mark if (t0 == NULL)
8946 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8948 ec2a9698 2022-09-15 mark len += strlen(t);
8949 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8950 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8952 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8955 ec2a9698 2022-09-15 mark return NULL;
8959 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8960 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8961 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8963 ec2a9698 2022-09-15 mark static const struct got_error *
8964 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8966 ec2a9698 2022-09-15 mark int n, len = width;
8968 ec2a9698 2022-09-15 mark if (km->keys) {
8969 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8970 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8971 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8973 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8974 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8976 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8978 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8979 ec2a9698 2022-09-15 mark if (t0 == NULL)
8980 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8982 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8983 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8984 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8985 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8986 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8987 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8988 ec2a9698 2022-09-15 mark if (n < 0) {
8990 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8992 ec2a9698 2022-09-15 mark first = 0;
8993 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8994 ec2a9698 2022-09-15 mark *off += n;
8998 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8999 ec2a9698 2022-09-15 mark if (n < 0)
9000 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
9001 ec2a9698 2022-09-15 mark *off += n;
9003 ec2a9698 2022-09-15 mark return NULL;
9006 ec2a9698 2022-09-15 mark static const struct got_error *
9007 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
9009 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9010 ec2a9698 2022-09-15 mark off_t off = 0;
9011 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
9012 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
9013 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
9014 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9015 ec2a9698 2022-09-15 mark GENERATE_HELP
9016 ec2a9698 2022-09-15 mark #undef KEYMAP_
9017 ec2a9698 2022-09-15 mark #undef KEY_
9020 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9022 ec2a9698 2022-09-15 mark return err;
9024 ec2a9698 2022-09-15 mark n = nitems(km);
9025 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
9027 ec2a9698 2022-09-15 mark return err;
9029 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
9030 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
9031 ec2a9698 2022-09-15 mark show = s->all;
9032 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
9033 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
9036 ec2a9698 2022-09-15 mark if (show) {
9037 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
9039 ec2a9698 2022-09-15 mark return err;
9040 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
9042 ec2a9698 2022-09-15 mark return err;
9045 ec2a9698 2022-09-15 mark fputc('\n', s->f);
9047 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
9048 ec2a9698 2022-09-15 mark return err;
9051 ec2a9698 2022-09-15 mark static const struct got_error *
9052 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
9055 ec2a9698 2022-09-15 mark const struct got_error *err;
9057 ec2a9698 2022-09-15 mark free(s->line_offsets);
9058 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9059 ec2a9698 2022-09-15 mark s->nlines = 0;
9061 ec2a9698 2022-09-15 mark f = got_opentemp();
9062 ec2a9698 2022-09-15 mark if (f == NULL)
9063 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
9066 ec2a9698 2022-09-15 mark err = format_help(s);
9068 ec2a9698 2022-09-15 mark return err;
9070 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
9071 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
9073 ec2a9698 2022-09-15 mark return NULL;
9076 ec2a9698 2022-09-15 mark static const struct got_error *
9077 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
9079 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
9080 ec2a9698 2022-09-15 mark return NULL;
9083 eaf2c13e 2022-09-17 mark static void
9084 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9085 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
9087 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
9089 eaf2c13e 2022-09-17 mark *f = s->f;
9090 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
9091 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
9092 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
9093 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
9094 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
9095 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
9098 ec2a9698 2022-09-15 mark static const struct got_error *
9099 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
9101 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9102 ec2a9698 2022-09-15 mark const struct got_error *err;
9103 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
9104 ec2a9698 2022-09-15 mark wchar_t *wline;
9105 ec2a9698 2022-09-15 mark char *line;
9106 ec2a9698 2022-09-15 mark ssize_t linelen;
9107 ec2a9698 2022-09-15 mark size_t linesz = 0;
9108 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
9109 ec2a9698 2022-09-15 mark int eos = view->nlines;
9111 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
9112 ec2a9698 2022-09-15 mark --eos; /* account for border */
9114 ec2a9698 2022-09-15 mark s->lineno = 0;
9115 ec2a9698 2022-09-15 mark rewind(s->f);
9116 ec2a9698 2022-09-15 mark werase(view->window);
9118 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
9119 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
9121 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
9122 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
9123 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
9125 ec2a9698 2022-09-15 mark return err;
9126 ec2a9698 2022-09-15 mark if (eos <= 1)
9127 ec2a9698 2022-09-15 mark return NULL;
9128 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
9131 ec2a9698 2022-09-15 mark s->eof = 0;
9132 ec2a9698 2022-09-15 mark view->maxx = 0;
9133 ec2a9698 2022-09-15 mark line = NULL;
9134 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
9135 ec2a9698 2022-09-15 mark attr_t attr = 0;
9137 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9138 ec2a9698 2022-09-15 mark if (linelen == -1) {
9139 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
9140 ec2a9698 2022-09-15 mark free(line);
9141 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
9143 ec2a9698 2022-09-15 mark s->eof = 1;
9146 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
9148 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9150 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
9151 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
9153 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9154 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
9155 ec2a9698 2022-09-15 mark if (err) {
9156 ec2a9698 2022-09-15 mark free(line);
9157 ec2a9698 2022-09-15 mark return err;
9159 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
9160 ec2a9698 2022-09-15 mark free(wline);
9161 ec2a9698 2022-09-15 mark wline = NULL;
9164 ec2a9698 2022-09-15 mark wattron(view->window, attr);
9165 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
9166 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9167 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
9168 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
9169 ec2a9698 2022-09-15 mark if (err) {
9170 ec2a9698 2022-09-15 mark free(line);
9171 ec2a9698 2022-09-15 mark return err;
9176 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
9177 b637f33f 2023-02-01 mark view->x, view->ncols, 0, view->x ? 1 : 0);
9178 ec2a9698 2022-09-15 mark if (err) {
9179 ec2a9698 2022-09-15 mark free(line);
9180 ec2a9698 2022-09-15 mark return err;
9182 b637f33f 2023-02-01 mark waddwstr(view->window, &wline[skip]);
9183 ec2a9698 2022-09-15 mark free(wline);
9184 ec2a9698 2022-09-15 mark wline = NULL;
9186 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
9187 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
9188 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
9190 b637f33f 2023-02-01 mark if (width < view->ncols)
9191 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
9194 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
9195 ec2a9698 2022-09-15 mark if (++nprinted == 1)
9196 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
9198 ec2a9698 2022-09-15 mark free(line);
9199 ec2a9698 2022-09-15 mark if (nprinted > 0)
9200 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9202 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
9204 ec2a9698 2022-09-15 mark view_border(view);
9206 ec2a9698 2022-09-15 mark if (s->eof) {
9207 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
9208 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
9209 ec2a9698 2022-09-15 mark view->ncols - 1);
9210 ec2a9698 2022-09-15 mark if (rc == ERR)
9211 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9213 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
9214 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
9215 ec2a9698 2022-09-15 mark wstandout(view->window);
9216 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
9217 ec2a9698 2022-09-15 mark view->ncols - 1);
9218 ec2a9698 2022-09-15 mark if (rc == ERR)
9219 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9220 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
9221 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
9222 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
9223 ec2a9698 2022-09-15 mark if (rc == ERR)
9224 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
9226 ec2a9698 2022-09-15 mark wstandend(view->window);
9229 ec2a9698 2022-09-15 mark return NULL;
9232 ec2a9698 2022-09-15 mark static const struct got_error *
9233 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9235 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9236 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9237 ec2a9698 2022-09-15 mark char *line = NULL;
9238 ec2a9698 2022-09-15 mark ssize_t linelen;
9239 ec2a9698 2022-09-15 mark size_t linesz = 0;
9240 ec2a9698 2022-09-15 mark int eos, nscroll;
9242 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
9243 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
9244 ec2a9698 2022-09-15 mark --eos; /* border */
9246 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
9248 ec2a9698 2022-09-15 mark switch (ch) {
9251 ec2a9698 2022-09-15 mark case KEY_RIGHT:
9253 ec2a9698 2022-09-15 mark case KEY_LEFT:
9255 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
9258 ec2a9698 2022-09-15 mark case KEY_HOME:
9259 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9260 ec2a9698 2022-09-15 mark view->count = 0;
9263 ec2a9698 2022-09-15 mark case KEY_END:
9264 ec2a9698 2022-09-15 mark view->count = 0;
9265 ec2a9698 2022-09-15 mark if (s->eof)
9267 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
9268 ec2a9698 2022-09-15 mark s->eof = 1;
9271 ec2a9698 2022-09-15 mark case KEY_UP:
9272 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
9273 ec2a9698 2022-09-15 mark --s->first_displayed_line;
9275 ec2a9698 2022-09-15 mark view->count = 0;
9277 ec2a9698 2022-09-15 mark case CTRL('u'):
9279 ec2a9698 2022-09-15 mark nscroll /= 2;
9280 ec2a9698 2022-09-15 mark /* FALL THROUGH */
9281 ec2a9698 2022-09-15 mark case KEY_PPAGE:
9282 ec2a9698 2022-09-15 mark case CTRL('b'):
9284 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
9285 ec2a9698 2022-09-15 mark view->count = 0;
9288 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
9289 ec2a9698 2022-09-15 mark s->first_displayed_line--;
9292 ec2a9698 2022-09-15 mark case KEY_DOWN:
9293 ec2a9698 2022-09-15 mark case CTRL('n'):
9294 ec2a9698 2022-09-15 mark if (!s->eof)
9295 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
9297 ec2a9698 2022-09-15 mark view->count = 0;
9299 ec2a9698 2022-09-15 mark case CTRL('d'):
9301 ec2a9698 2022-09-15 mark nscroll /= 2;
9302 ec2a9698 2022-09-15 mark /* FALL THROUGH */
9303 ec2a9698 2022-09-15 mark case KEY_NPAGE:
9304 ec2a9698 2022-09-15 mark case CTRL('f'):
9307 ec2a9698 2022-09-15 mark if (s->eof) {
9308 ec2a9698 2022-09-15 mark view->count = 0;
9311 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
9312 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9313 ec2a9698 2022-09-15 mark s->first_displayed_line++;
9314 ec2a9698 2022-09-15 mark if (linelen == -1) {
9315 ec2a9698 2022-09-15 mark if (feof(s->f))
9316 ec2a9698 2022-09-15 mark s->eof = 1;
9318 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
9322 ec2a9698 2022-09-15 mark free(line);
9325 ec2a9698 2022-09-15 mark view->count = 0;
9329 ec2a9698 2022-09-15 mark return err;
9332 ec2a9698 2022-09-15 mark static const struct got_error *
9333 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
9335 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9337 ec2a9698 2022-09-15 mark free(s->line_offsets);
9338 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9339 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
9340 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9342 ec2a9698 2022-09-15 mark return NULL;
9345 ec2a9698 2022-09-15 mark static const struct got_error *
9346 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
9348 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9351 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
9352 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9354 ec2a9698 2022-09-15 mark wclear(view->window);
9355 ec2a9698 2022-09-15 mark view->count = 0;
9356 ec2a9698 2022-09-15 mark view->x = 0;
9357 ec2a9698 2022-09-15 mark s->all = !s->all;
9358 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9359 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9360 ec2a9698 2022-09-15 mark s->matched_line = 0;
9362 ec2a9698 2022-09-15 mark return create_help(s);
9365 ec2a9698 2022-09-15 mark static const struct got_error *
9366 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
9368 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9369 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9371 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
9372 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9373 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9374 ec2a9698 2022-09-15 mark s->selected_line = 1;
9376 ec2a9698 2022-09-15 mark view->show = show_help_view;
9377 ec2a9698 2022-09-15 mark view->input = input_help_view;
9378 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
9379 ec2a9698 2022-09-15 mark view->close = close_help_view;
9380 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
9381 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
9382 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
9384 ec2a9698 2022-09-15 mark err = create_help(s);
9385 ec2a9698 2022-09-15 mark return err;
9388 ec2a9698 2022-09-15 mark static const struct got_error *
9389 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9390 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
9392 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
9394 136e2bd4 2022-07-23 mark *new_view = NULL;
9396 136e2bd4 2022-07-23 mark switch (request) {
9397 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
9398 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9399 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9401 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9402 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9403 136e2bd4 2022-07-23 mark view, s->repo);
9405 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9406 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9408 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9409 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9410 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9412 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9413 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9416 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9417 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9419 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9420 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9421 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9422 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9423 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9424 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9425 136e2bd4 2022-07-23 mark &view->state.tree);
9426 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9427 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9428 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9429 136e2bd4 2022-07-23 mark view->state.ref.repo);
9431 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9432 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9434 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9435 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9436 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9437 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9438 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9439 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9440 136e2bd4 2022-07-23 mark view->state.log.repo);
9441 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9442 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9443 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9444 136e2bd4 2022-07-23 mark view->state.ref.repo);
9446 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9447 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9449 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9450 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9451 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9452 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9453 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9454 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9455 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9456 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9458 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9459 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9461 136e2bd4 2022-07-23 mark view_close(*new_view);
9463 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9464 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9465 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9466 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9467 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9469 ec2a9698 2022-09-15 mark view_close(*new_view);
9472 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9475 136e2bd4 2022-07-23 mark return err;
9479 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9480 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9482 6458efa5 2020-11-24 stsp static void
9483 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9485 9b058f45 2022-06-30 mark switch (view->type) {
9486 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9487 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9488 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9489 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9493 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9494 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9496 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9497 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9500 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9501 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9502 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9504 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9505 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9506 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9508 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9509 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9510 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9516 9b058f45 2022-06-30 mark view->offset = 0;
9520 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9521 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9523 9b058f45 2022-06-30 mark static const struct got_error *
9524 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9526 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9527 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9528 9b058f45 2022-06-30 mark int *selected = NULL;
9529 9b058f45 2022-06-30 mark int header, offset;
9531 9b058f45 2022-06-30 mark switch (view->type) {
9532 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9533 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9534 9b058f45 2022-06-30 mark header = 3;
9535 9b058f45 2022-06-30 mark scrolld = NULL;
9536 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9537 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9538 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9539 9b058f45 2022-06-30 mark s->selected_line -= offset;
9540 9b058f45 2022-06-30 mark view->offset = offset;
9544 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9545 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9546 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9547 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9548 9b058f45 2022-06-30 mark selected = &s->selected;
9551 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9552 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9553 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9554 9b058f45 2022-06-30 mark header = 3;
9555 9b058f45 2022-06-30 mark selected = &s->selected;
9558 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9559 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9560 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9561 9b058f45 2022-06-30 mark header = 5;
9562 9b058f45 2022-06-30 mark selected = &s->selected;
9566 9b058f45 2022-06-30 mark selected = NULL;
9567 9b058f45 2022-06-30 mark scrolld = NULL;
9568 9b058f45 2022-06-30 mark header = 0;
9572 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9573 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9574 9b058f45 2022-06-30 mark view->offset = offset;
9575 9b058f45 2022-06-30 mark if (scrolld && offset) {
9576 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9577 9b058f45 2022-06-30 mark *selected -= offset;
9581 9b058f45 2022-06-30 mark return err;
9584 9b058f45 2022-06-30 mark static void
9585 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9589 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9590 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9591 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9592 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9594 6879ba42 2020-10-01 naddy fputc('\n', fp);
9597 4ed7e80c 2018-05-20 stsp __dead static void
9598 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9600 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9602 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9603 6879ba42 2020-10-01 naddy getprogname());
9604 6879ba42 2020-10-01 naddy if (hflag) {
9605 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9606 6879ba42 2020-10-01 naddy list_commands(fp);
9608 6879ba42 2020-10-01 naddy exit(status);
9611 c2301be8 2018-04-30 stsp static char **
9612 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9614 ee85c5e8 2020-02-29 stsp va_list ap;
9615 c2301be8 2018-04-30 stsp char **argv;
9618 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9620 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9621 c2301be8 2018-04-30 stsp if (argv == NULL)
9622 c2301be8 2018-04-30 stsp err(1, "calloc");
9623 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9624 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9625 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9626 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9629 ee85c5e8 2020-02-29 stsp va_end(ap);
9630 c2301be8 2018-04-30 stsp return argv;
9634 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9635 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9636 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9637 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9638 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9640 ee85c5e8 2020-02-29 stsp static const struct got_error *
9641 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9643 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9644 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9645 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9646 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9647 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9648 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9649 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9650 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9651 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9653 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9654 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9655 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9657 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9658 0ae84acc 2022-06-15 tracey if (error != NULL)
9659 0ae84acc 2022-06-15 tracey goto done;
9661 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9662 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9663 ee85c5e8 2020-02-29 stsp goto done;
9665 ee85c5e8 2020-02-29 stsp if (worktree)
9666 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9668 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9669 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9670 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9671 ee85c5e8 2020-02-29 stsp goto done;
9674 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9675 ee85c5e8 2020-02-29 stsp if (error != NULL)
9676 ee85c5e8 2020-02-29 stsp goto done;
9678 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9679 ee85c5e8 2020-02-29 stsp repo, worktree);
9680 ee85c5e8 2020-02-29 stsp if (error)
9681 ee85c5e8 2020-02-29 stsp goto done;
9683 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9684 84de9106 2020-12-26 stsp if (error)
9685 84de9106 2020-12-26 stsp goto done;
9686 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9687 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9688 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9689 ee85c5e8 2020-02-29 stsp if (error)
9690 ee85c5e8 2020-02-29 stsp goto done;
9692 ee85c5e8 2020-02-29 stsp if (worktree) {
9693 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9694 ee85c5e8 2020-02-29 stsp worktree = NULL;
9697 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9698 a44927cc 2022-04-07 stsp if (error)
9699 a44927cc 2022-04-07 stsp goto done;
9701 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9702 ee85c5e8 2020-02-29 stsp if (error) {
9703 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9704 ee85c5e8 2020-02-29 stsp goto done;
9705 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9706 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9707 6879ba42 2020-10-01 naddy usage(1, 1);
9708 ee85c5e8 2020-02-29 stsp /* not reached */
9711 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9712 ee85c5e8 2020-02-29 stsp if (error)
9713 ee85c5e8 2020-02-29 stsp goto done;
9715 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9717 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9718 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9720 1d0f4054 2021-06-17 stsp if (repo) {
9721 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9722 1d0f4054 2021-06-17 stsp if (error == NULL)
9723 1d0f4054 2021-06-17 stsp error = close_err;
9725 a44927cc 2022-04-07 stsp if (commit)
9726 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9727 ee85c5e8 2020-02-29 stsp if (worktree)
9728 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9729 0ae84acc 2022-06-15 tracey if (pack_fds) {
9730 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9731 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9732 0ae84acc 2022-06-15 tracey if (error == NULL)
9733 0ae84acc 2022-06-15 tracey error = pack_err;
9736 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9737 ee85c5e8 2020-02-29 stsp free(commit_id);
9738 ee85c5e8 2020-02-29 stsp free(cwd);
9739 ee85c5e8 2020-02-29 stsp free(repo_path);
9740 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9741 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9743 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9744 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9745 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9747 87670572 2020-12-26 naddy tog_free_refs();
9748 ee85c5e8 2020-02-29 stsp return error;
9752 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9754 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9755 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9756 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9757 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9758 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9759 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9760 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9762 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9764 1fa0d17d 2023-02-13 op setlocale(LC_CTYPE, "");
9766 af21bb7e 2023-04-12 mark #if !defined(PROFILE) && !defined(TOG_REGRESS)
9767 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9768 1fa0d17d 2023-02-13 op NULL) == -1)
9769 1fa0d17d 2023-02-13 op err(1, "pledge");
9772 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9773 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9775 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9776 9f7d7167 2018-04-29 stsp switch (ch) {
9778 9f7d7167 2018-04-29 stsp hflag = 1;
9781 53ccebc2 2019-07-30 stsp Vflag = 1;
9784 6879ba42 2020-10-01 naddy usage(hflag, 1);
9785 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9789 9f7d7167 2018-04-29 stsp argc -= optind;
9790 9f7d7167 2018-04-29 stsp argv += optind;
9791 9814e6a3 2020-09-27 naddy optind = 1;
9792 c2301be8 2018-04-30 stsp optreset = 1;
9794 53ccebc2 2019-07-30 stsp if (Vflag) {
9795 53ccebc2 2019-07-30 stsp got_version_print_str();
9796 6879ba42 2020-10-01 naddy return 0;
9799 c2301be8 2018-04-30 stsp if (argc == 0) {
9800 f29d3e89 2018-06-23 stsp if (hflag)
9801 6879ba42 2020-10-01 naddy usage(hflag, 0);
9802 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9803 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9805 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9809 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9810 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9811 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9812 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9813 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9819 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9820 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9821 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9822 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9823 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9824 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9827 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9828 ee85c5e8 2020-02-29 stsp if (argc != 1)
9829 6879ba42 2020-10-01 naddy usage(0, 1);
9830 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9831 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9833 ee85c5e8 2020-02-29 stsp if (hflag)
9834 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9836 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9840 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9842 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9843 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9844 a2f4a359 2020-02-28 stsp free(cmd_argv);
9847 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9848 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9849 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9850 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9851 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9852 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);