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 91db2202 2023-04-22 op #define HSPLIT_SCALE 0.3f /* 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;
435 8496bf63 2023-04-20 mark pthread_cond_t blame_complete;
438 e9424729 2018-08-04 stsp struct tog_blame {
440 be659d10 2020-11-18 stsp off_t filesize;
441 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
442 6fcac457 2018-11-19 stsp int nlines;
443 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
444 e9424729 2018-08-04 stsp pthread_t thread;
445 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
446 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
447 e9424729 2018-08-04 stsp const char *path;
448 0ae84acc 2022-06-15 tracey int *pack_fds;
451 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
452 7cbe629d 2018-08-04 stsp int first_displayed_line;
453 7cbe629d 2018-08-04 stsp int last_displayed_line;
454 7cbe629d 2018-08-04 stsp int selected_line;
455 c0f61fa4 2022-07-11 mark int last_diffed_line;
456 7cbe629d 2018-08-04 stsp int blame_complete;
459 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
460 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
461 e5a0f69f 2018-08-18 stsp char *path;
462 7cbe629d 2018-08-04 stsp struct got_repository *repo;
463 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
464 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
465 e9424729 2018-08-04 stsp struct tog_blame blame;
466 6c4c42e0 2019-06-24 stsp int matched_line;
467 11b20872 2019-11-08 stsp struct tog_colors colors;
470 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
471 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
472 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
473 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
474 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
475 ad80ab7b 2018-08-04 stsp int selected;
478 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
480 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
481 ad80ab7b 2018-08-04 stsp char *tree_label;
482 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
483 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
484 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
485 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
486 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
487 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
488 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
489 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
490 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
491 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
492 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
493 6458efa5 2020-11-24 stsp struct tog_colors colors;
496 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
497 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
498 6458efa5 2020-11-24 stsp struct got_reference *ref;
502 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
504 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
505 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
506 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
507 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
508 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
509 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
510 6458efa5 2020-11-24 stsp struct got_repository *repo;
511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
512 bddb1296 2019-11-08 stsp struct tog_colors colors;
515 ec2a9698 2022-09-15 mark struct tog_help_view_state {
517 ec2a9698 2022-09-15 mark off_t *line_offsets;
518 ec2a9698 2022-09-15 mark size_t nlines;
519 ec2a9698 2022-09-15 mark int lineno;
520 ec2a9698 2022-09-15 mark int first_displayed_line;
521 ec2a9698 2022-09-15 mark int last_displayed_line;
523 ec2a9698 2022-09-15 mark int matched_line;
524 ec2a9698 2022-09-15 mark int selected_line;
526 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
529 ec2a9698 2022-09-15 mark #define GENERATE_HELP \
530 ec2a9698 2022-09-15 mark KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
531 ec2a9698 2022-09-15 mark KEY_("H F1", "Open view-specific help (double tap for all help)"), \
532 ec2a9698 2022-09-15 mark KEY_("k C-p Up", "Move cursor or page up one line"), \
533 ec2a9698 2022-09-15 mark KEY_("j C-n Down", "Move cursor or page down one line"), \
534 ec2a9698 2022-09-15 mark KEY_("C-b b PgUp", "Scroll the view up one page"), \
535 ec2a9698 2022-09-15 mark KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
536 ec2a9698 2022-09-15 mark KEY_("C-u u", "Scroll the view up one half page"), \
537 ec2a9698 2022-09-15 mark KEY_("C-d d", "Scroll the view down one half page"), \
538 0b3f028d 2023-01-09 mark KEY_("g", "Go to line N (default: first line)"), \
539 0b3f028d 2023-01-09 mark KEY_("Home =", "Go to the first line"), \
540 0b3f028d 2023-01-09 mark KEY_("G", "Go to line N (default: last line)"), \
541 0b3f028d 2023-01-09 mark KEY_("End *", "Go to the last line"), \
542 ec2a9698 2022-09-15 mark KEY_("l Right", "Scroll the view right"), \
543 ec2a9698 2022-09-15 mark KEY_("h Left", "Scroll the view left"), \
544 ec2a9698 2022-09-15 mark KEY_("$", "Scroll view to the rightmost position"), \
545 ec2a9698 2022-09-15 mark KEY_("0", "Scroll view to the leftmost position"), \
546 ec2a9698 2022-09-15 mark KEY_("-", "Decrease size of the focussed split"), \
547 ec2a9698 2022-09-15 mark KEY_("+", "Increase size of the focussed split"), \
548 ec2a9698 2022-09-15 mark KEY_("Tab", "Switch focus between views"), \
549 ec2a9698 2022-09-15 mark KEY_("F", "Toggle fullscreen mode"), \
550 132d5247 2023-04-14 stsp KEY_("S", "Switch split-screen layout"), \
551 ec2a9698 2022-09-15 mark KEY_("/", "Open prompt to enter search term"), \
552 ec2a9698 2022-09-15 mark KEY_("n", "Find next line/token matching the current search term"), \
553 ec2a9698 2022-09-15 mark KEY_("N", "Find previous line/token matching the current search term"),\
554 16a2d4f0 2022-09-19 stsp KEY_("q", "Quit the focussed view; Quit help screen"), \
555 ec2a9698 2022-09-15 mark KEY_("Q", "Quit tog"), \
557 fd6badaa 2022-09-23 stsp KEYMAP_("Log view", TOG_KEYMAP_LOG), \
558 ec2a9698 2022-09-15 mark KEY_("< ,", "Move cursor up one commit"), \
559 ec2a9698 2022-09-15 mark KEY_("> .", "Move cursor down one commit"), \
560 ec2a9698 2022-09-15 mark KEY_("Enter", "Open diff view of the selected commit"), \
561 ec2a9698 2022-09-15 mark KEY_("B", "Reload the log view and toggle display of merged commits"), \
562 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
563 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the repository from the selected" \
564 ec2a9698 2022-09-15 mark " commit"), \
565 ec2a9698 2022-09-15 mark KEY_("@", "Toggle between displaying author and committer name"), \
566 ec2a9698 2022-09-15 mark KEY_("&", "Open prompt to enter term to limit commits displayed"), \
567 ec2a9698 2022-09-15 mark KEY_("C-g Backspace", "Cancel current search or log operation"), \
568 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload the log view with new commits in the repository"), \
570 fd6badaa 2022-09-23 stsp KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
571 ec2a9698 2022-09-15 mark KEY_("K < ,", "Display diff of next line in the file/log entry"), \
572 ec2a9698 2022-09-15 mark KEY_("J > .", "Display diff of previous line in the file/log entry"), \
573 ec2a9698 2022-09-15 mark KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
574 ec2a9698 2022-09-15 mark KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
575 ec2a9698 2022-09-15 mark " data"), \
576 ec2a9698 2022-09-15 mark KEY_("(", "Go to the previous file in the diff"), \
577 ec2a9698 2022-09-15 mark KEY_(")", "Go to the next file in the diff"), \
578 ec2a9698 2022-09-15 mark KEY_("{", "Go to the previous hunk in the diff"), \
579 ec2a9698 2022-09-15 mark KEY_("}", "Go to the next hunk in the diff"), \
580 ec2a9698 2022-09-15 mark KEY_("[", "Decrease the number of context lines"), \
581 ec2a9698 2022-09-15 mark KEY_("]", "Increase the number of context lines"), \
582 ec2a9698 2022-09-15 mark KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
584 fd6badaa 2022-09-23 stsp KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
585 ec2a9698 2022-09-15 mark KEY_("Enter", "Display diff view of the selected line's commit"), \
586 ec2a9698 2022-09-15 mark KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
587 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the currently selected annotated line"), \
588 ec2a9698 2022-09-15 mark KEY_("C", "Reload view with the previously blamed commit"), \
589 ec2a9698 2022-09-15 mark KEY_("c", "Reload view with the version of the file found in the" \
590 ec2a9698 2022-09-15 mark " selected line's commit"), \
591 ec2a9698 2022-09-15 mark KEY_("p", "Reload view with the version of the file found in the" \
592 ec2a9698 2022-09-15 mark " selected line's parent commit"), \
594 fd6badaa 2022-09-23 stsp KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
595 ec2a9698 2022-09-15 mark KEY_("Enter", "Enter selected directory or open blame view of the" \
596 ec2a9698 2022-09-15 mark " selected file"), \
597 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the selected entry"), \
598 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
599 ec2a9698 2022-09-15 mark KEY_("i", "Show object IDs for all tree entries"), \
600 ec2a9698 2022-09-15 mark KEY_("Backspace", "Return to the parent directory"), \
602 fd6badaa 2022-09-23 stsp KEYMAP_("Ref view", TOG_KEYMAP_REF), \
603 ec2a9698 2022-09-15 mark KEY_("Enter", "Display log view of the selected reference"), \
604 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the selected reference"), \
605 ec2a9698 2022-09-15 mark KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
606 ec2a9698 2022-09-15 mark KEY_("m", "Toggle display of last modified date for each reference"), \
607 ec2a9698 2022-09-15 mark KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
608 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload view with all repository references")
610 ec2a9698 2022-09-15 mark struct tog_key_map {
611 ec2a9698 2022-09-15 mark const char *keys;
612 ec2a9698 2022-09-15 mark const char *info;
613 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
616 af21bb7e 2023-04-12 mark /* curses io for tog regress */
617 af21bb7e 2023-04-12 mark struct tog_io {
619 af21bb7e 2023-04-12 mark FILE *cout;
622 8d212112 2023-04-16 mark int wait_for_ui;
624 098596c5 2023-04-14 stsp static int using_mock_io;
626 81641b41 2023-04-20 mark #define TOG_KEY_SCRDUMP SHRT_MIN
629 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
631 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
632 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
633 669b5ffa 2018-10-07 stsp * there is enough screen estate.
635 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
636 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
638 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
639 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
640 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
642 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
643 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
645 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
647 cc3c9aac 2018-08-01 stsp struct tog_view {
648 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
649 26ed57b2 2018-05-19 stsp WINDOW *window;
650 26ed57b2 2018-05-19 stsp PANEL *panel;
651 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
652 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
653 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
654 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
655 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
656 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
657 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
658 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
659 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
661 669b5ffa 2018-10-07 stsp struct tog_view *parent;
662 669b5ffa 2018-10-07 stsp struct tog_view *child;
665 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
666 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
667 e78dc838 2020-12-04 stsp * between parent and child.
668 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
669 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
670 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
671 e78dc838 2020-12-04 stsp * situations.
673 e78dc838 2020-12-04 stsp int focus_child;
675 9b058f45 2022-06-30 mark enum tog_view_mode mode;
676 5dc9f4bc 2018-08-04 stsp /* type-specific state */
677 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
679 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
680 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
681 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
682 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
683 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
684 ec2a9698 2022-09-15 mark struct tog_help_view_state help;
687 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
688 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
689 e78dc838 2020-12-04 stsp struct tog_view *, int);
690 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
691 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
692 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
694 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
695 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
696 eaf2c13e 2022-09-17 mark void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
697 eaf2c13e 2022-09-17 mark int **, int **, int **, int **);
698 c0c4acc8 2021-01-24 stsp int search_started;
699 60493ae3 2019-06-20 stsp int searching;
700 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
701 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
702 60493ae3 2019-06-20 stsp int search_next_done;
703 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
704 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
705 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
706 1803e47f 2019-06-22 stsp regex_t regex;
707 41605754 2020-11-12 stsp regmatch_t regmatch;
708 3f6c6614 2023-01-23 mark const char *action;
711 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
712 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
713 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
714 78756c87 2020-11-24 stsp struct got_repository *);
715 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
716 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
717 e78dc838 2020-12-04 stsp struct tog_view *, int);
718 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
719 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
720 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
721 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
722 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
723 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
725 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
726 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
727 78756c87 2020-11-24 stsp const char *, const char *, int);
728 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
729 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
730 e78dc838 2020-12-04 stsp struct tog_view *, int);
731 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
732 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
733 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
734 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
736 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
737 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
738 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
739 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
740 e78dc838 2020-12-04 stsp struct tog_view *, int);
741 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
742 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
743 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
744 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
745 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
747 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
748 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
749 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
750 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
751 e78dc838 2020-12-04 stsp struct tog_view *, int);
752 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
753 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
754 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
756 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
757 6458efa5 2020-11-24 stsp struct got_repository *);
758 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
759 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
760 e78dc838 2020-12-04 stsp struct tog_view *, int);
761 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
762 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
763 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
765 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
766 eaf2c13e 2022-09-17 mark struct tog_view *);
767 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
768 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
769 eaf2c13e 2022-09-17 mark struct tog_view *, int);
770 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
771 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
772 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
773 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
774 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
776 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
777 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
778 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
779 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
780 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
782 25791caa 2018-10-24 stsp static void
783 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
785 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
788 83baff54 2019-08-12 stsp static void
789 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
791 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
794 61266923 2020-01-14 stsp static void
795 61266923 2020-01-14 stsp tog_sigcont(int signo)
797 61266923 2020-01-14 stsp tog_sigcont_received = 1;
800 2497f032 2022-05-31 stsp static void
801 2497f032 2022-05-31 stsp tog_sigint(int signo)
803 2497f032 2022-05-31 stsp tog_sigint_received = 1;
806 2497f032 2022-05-31 stsp static void
807 2497f032 2022-05-31 stsp tog_sigterm(int signo)
809 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
813 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
815 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
816 f044c841 2022-10-24 stsp tog_sigint_received || tog_sigterm_received);
819 e5a0f69f 2018-08-18 stsp static const struct got_error *
820 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
822 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
824 669b5ffa 2018-10-07 stsp if (view->child) {
825 5629093a 2022-07-11 stsp child_err = view_close(view->child);
826 669b5ffa 2018-10-07 stsp view->child = NULL;
828 e5a0f69f 2018-08-18 stsp if (view->close)
829 e5a0f69f 2018-08-18 stsp err = view->close(view);
830 ea5e7bb5 2018-08-01 stsp if (view->panel)
831 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
832 ea5e7bb5 2018-08-01 stsp if (view->window)
833 ea5e7bb5 2018-08-01 stsp delwin(view->window);
834 ea5e7bb5 2018-08-01 stsp free(view);
835 5629093a 2022-07-11 stsp return err ? err : child_err;
838 ea5e7bb5 2018-08-01 stsp static struct tog_view *
839 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
840 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
842 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
844 ea5e7bb5 2018-08-01 stsp if (view == NULL)
845 ea5e7bb5 2018-08-01 stsp return NULL;
847 d6b05b5b 2018-08-04 stsp view->type = type;
848 f7d12f7e 2018-08-01 stsp view->lines = LINES;
849 f7d12f7e 2018-08-01 stsp view->cols = COLS;
850 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
851 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
852 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
853 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
854 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
855 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
856 96a765a8 2018-08-04 stsp view_close(view);
857 ea5e7bb5 2018-08-01 stsp return NULL;
859 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
860 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
861 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
862 96a765a8 2018-08-04 stsp view_close(view);
863 ea5e7bb5 2018-08-01 stsp return NULL;
866 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
867 ea5e7bb5 2018-08-01 stsp return view;
871 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
873 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
875 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
878 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
880 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
882 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
885 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
887 5c60c32a 2018-10-18 stsp static const struct got_error *
888 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
890 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
892 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
893 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
894 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
896 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
897 9b058f45 2022-06-30 mark view->begin_x = 0;
898 571ccd73 2022-07-19 mark } else if (!view->resized) {
899 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
900 3c1dfe12 2022-07-08 mark view->cols > 119)
901 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
903 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
904 9b058f45 2022-06-30 mark view->begin_y = 0;
906 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
907 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
908 5c60c32a 2018-10-18 stsp view->lines = LINES;
909 5c60c32a 2018-10-18 stsp view->cols = COLS;
910 5c60c32a 2018-10-18 stsp err = view_resize(view);
912 5c60c32a 2018-10-18 stsp return err;
914 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
915 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
917 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
918 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
920 5c60c32a 2018-10-18 stsp return NULL;
923 5c60c32a 2018-10-18 stsp static const struct got_error *
924 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
926 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
928 5c60c32a 2018-10-18 stsp view->begin_x = 0;
929 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
930 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
931 5c60c32a 2018-10-18 stsp view->ncols = COLS;
932 5c60c32a 2018-10-18 stsp view->lines = LINES;
933 5c60c32a 2018-10-18 stsp view->cols = COLS;
934 5c60c32a 2018-10-18 stsp err = view_resize(view);
936 5c60c32a 2018-10-18 stsp return err;
938 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
939 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
941 5c60c32a 2018-10-18 stsp return NULL;
945 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
947 5c60c32a 2018-10-18 stsp return view->parent == NULL;
951 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
953 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
957 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
959 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
963 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
965 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
966 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
969 9b058f45 2022-06-30 mark static void
970 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
972 9b058f45 2022-06-30 mark PANEL *panel;
973 9b058f45 2022-06-30 mark const struct tog_view *view_above;
975 9b058f45 2022-06-30 mark if (view->parent)
976 9b058f45 2022-06-30 mark return view_border(view->parent);
978 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
979 9b058f45 2022-06-30 mark if (panel == NULL)
982 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
983 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
984 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
985 c62d44e7 2023-04-20 naddy view->begin_x, ACS_HLINE, view->ncols);
987 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
988 c62d44e7 2023-04-20 naddy ACS_VLINE, view->nlines);
991 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
992 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
993 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
994 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
995 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
997 4d8c2215 2018-08-19 stsp static const struct got_error *
998 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
1000 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
1001 9b058f45 2022-06-30 mark int dif, nlines, ncols;
1003 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
1005 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1006 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1008 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1009 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1010 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1012 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1014 4dd27a72 2022-06-29 stsp if (view->child) {
1015 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
1017 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1018 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1019 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1020 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1021 0dbbbe90 2022-06-17 op ncols = COLS;
1023 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1024 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1025 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1027 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1029 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1031 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1032 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1035 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1036 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1037 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1040 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1042 9b058f45 2022-06-30 mark return err;
1043 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1044 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1046 9b058f45 2022-06-30 mark return err;
1048 9b058f45 2022-06-30 mark view_border(view);
1049 9b058f45 2022-06-30 mark update_panels();
1050 9b058f45 2022-06-30 mark doupdate();
1051 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1052 9b058f45 2022-06-30 mark nlines = view->nlines;
1054 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1055 0dbbbe90 2022-06-17 op ncols = COLS;
1057 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1058 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1060 571ccd73 2022-07-19 mark return err;
1063 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1064 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1065 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1066 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1067 0dbbbe90 2022-06-17 op wclear(view->window);
1069 0dbbbe90 2022-06-17 op view->nlines = nlines;
1070 0dbbbe90 2022-06-17 op view->ncols = ncols;
1071 0dbbbe90 2022-06-17 op view->lines = LINES;
1072 0dbbbe90 2022-06-17 op view->cols = COLS;
1074 5c60c32a 2018-10-18 stsp return NULL;
1077 571ccd73 2022-07-19 mark static const struct got_error *
1078 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1080 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1081 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1082 6fe51fee 2022-07-22 mark int n = 0;
1084 6fe51fee 2022-07-22 mark if (s->selected_entry)
1085 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1088 571ccd73 2022-07-19 mark * Request commits to account for the increased
1089 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1091 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1092 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1093 571ccd73 2022-07-19 mark err = request_log_commits(view);
1096 571ccd73 2022-07-19 mark return err;
1099 d9a7ab53 2022-07-11 mark static void
1100 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1102 d9a7ab53 2022-07-11 mark if (n == 0)
1105 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1106 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1107 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1109 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1110 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1111 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1112 d9a7ab53 2022-07-11 mark view->offset -= n;
1114 d9a7ab53 2022-07-11 mark view->offset = 0;
1118 3c1dfe12 2022-07-08 mark static const struct got_error *
1119 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1121 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1122 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1124 3c1dfe12 2022-07-08 mark if (view->parent)
1125 3c1dfe12 2022-07-08 mark v = view->parent;
1129 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1130 3c1dfe12 2022-07-08 mark return NULL;
1132 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1134 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1135 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1136 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1137 3c1dfe12 2022-07-08 mark if (view->parent)
1138 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1140 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1141 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1142 3c1dfe12 2022-07-08 mark view->count = 0;
1143 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1144 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1145 3c1dfe12 2022-07-08 mark view->count = 0;
1146 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1148 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1149 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1150 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1151 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1153 3c1dfe12 2022-07-08 mark return err;
1154 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1156 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1157 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1158 3c1dfe12 2022-07-08 mark if (view->parent)
1159 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1161 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1162 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1163 3c1dfe12 2022-07-08 mark view->count = 0;
1164 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1165 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1166 3c1dfe12 2022-07-08 mark view->count = 0;
1167 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1169 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1172 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1173 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1174 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1175 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1177 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1179 3c1dfe12 2022-07-08 mark return err;
1180 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1182 3c1dfe12 2022-07-08 mark return err;
1184 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1185 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1187 3c1dfe12 2022-07-08 mark return err;
1190 6fe51fee 2022-07-22 mark if (v->resize)
1191 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1192 6fe51fee 2022-07-22 mark else if (v->child->resize)
1193 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1195 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1197 3c1dfe12 2022-07-08 mark return err;
1200 3c1dfe12 2022-07-08 mark static void
1201 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1203 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1205 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1206 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1209 669b5ffa 2018-10-07 stsp static const struct got_error *
1210 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1212 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1214 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1215 669b5ffa 2018-10-07 stsp return NULL;
1217 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1218 669b5ffa 2018-10-07 stsp view->child = NULL;
1219 669b5ffa 2018-10-07 stsp return err;
1222 0dbbbe90 2022-06-17 op static const struct got_error *
1223 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1225 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1227 669b5ffa 2018-10-07 stsp view->child = child;
1228 669b5ffa 2018-10-07 stsp child->parent = view;
1230 3c1dfe12 2022-07-08 mark err = view_resize(view);
1232 3c1dfe12 2022-07-08 mark return err;
1234 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1235 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1237 3c1dfe12 2022-07-08 mark return err;
1240 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1241 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1243 136e2bd4 2022-07-23 mark static const struct got_error *
1244 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1245 136e2bd4 2022-07-23 mark enum tog_view_type request)
1247 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1248 136e2bd4 2022-07-23 mark const struct got_error *err;
1249 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1251 136e2bd4 2022-07-23 mark *requested = NULL;
1253 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1254 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1256 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1258 136e2bd4 2022-07-23 mark return err;
1260 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1261 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1262 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1264 136e2bd4 2022-07-23 mark return err;
1267 136e2bd4 2022-07-23 mark view->focussed = 0;
1268 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1269 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1270 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1271 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1273 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1274 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1275 136e2bd4 2022-07-23 mark err = view_close_child(view);
1277 136e2bd4 2022-07-23 mark return err;
1278 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1280 136e2bd4 2022-07-23 mark return err;
1281 136e2bd4 2022-07-23 mark view->focus_child = 1;
1283 136e2bd4 2022-07-23 mark *requested = new_view;
1285 136e2bd4 2022-07-23 mark return NULL;
1288 34bc9ec9 2019-02-22 stsp static void
1289 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1291 25791caa 2018-10-24 stsp int cols, lines;
1292 25791caa 2018-10-24 stsp struct winsize size;
1294 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1295 25791caa 2018-10-24 stsp cols = 80; /* Default */
1296 25791caa 2018-10-24 stsp lines = 24;
1298 25791caa 2018-10-24 stsp cols = size.ws_col;
1299 25791caa 2018-10-24 stsp lines = size.ws_row;
1301 25791caa 2018-10-24 stsp resize_term(lines, cols);
1304 2b49a8ae 2019-06-22 stsp static const struct got_error *
1305 602eda79 2023-02-09 mark view_search_start(struct tog_view *view, int fast_refresh)
1307 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1308 9b058f45 2022-06-30 mark struct tog_view *v = view;
1309 2b49a8ae 2019-06-22 stsp char pattern[1024];
1312 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1313 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1314 c0c4acc8 2021-01-24 stsp view->searching = 0;
1315 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1317 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1319 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1320 2b49a8ae 2019-06-22 stsp return NULL;
1322 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1323 9b058f45 2022-06-30 mark v = view->child;
1324 9d0feb8b 2022-12-27 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1325 9d0feb8b 2022-12-27 mark v = view->parent;
1327 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1328 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1330 092a9f9c 2022-12-27 mark nodelay(v->window, FALSE); /* block for search term input */
1331 2b49a8ae 2019-06-22 stsp nocbreak();
1333 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1334 9b058f45 2022-06-30 mark wrefresh(v->window);
1337 092a9f9c 2022-12-27 mark nodelay(v->window, TRUE);
1338 098596c5 2023-04-14 stsp if (!fast_refresh && !using_mock_io)
1339 602eda79 2023-02-09 mark halfdelay(10);
1340 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1341 2b49a8ae 2019-06-22 stsp return NULL;
1343 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1344 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1345 7c32bd05 2019-06-22 stsp if (err) {
1346 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1347 7c32bd05 2019-06-22 stsp return err;
1349 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1350 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1351 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1352 2b49a8ae 2019-06-22 stsp view->search_next(view);
1355 2b49a8ae 2019-06-22 stsp return NULL;
1358 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1359 d2366e29 2022-07-07 mark static const struct got_error *
1360 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1362 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1363 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1365 d2366e29 2022-07-07 mark if (view->parent)
1366 d2366e29 2022-07-07 mark v = view->parent;
1370 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1371 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1373 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1375 7532ccda 2022-07-11 mark if (!v->child)
1376 7532ccda 2022-07-11 mark return NULL;
1377 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1378 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1380 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1381 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1382 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1383 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1384 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1387 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1388 d2366e29 2022-07-07 mark v->ncols = COLS;
1389 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1390 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1392 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1394 d2366e29 2022-07-07 mark return err;
1396 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1397 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1398 d2366e29 2022-07-07 mark v->focus_child = 1;
1400 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1402 d2366e29 2022-07-07 mark return err;
1403 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1405 d2366e29 2022-07-07 mark return err;
1407 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1408 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1409 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1410 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1412 279d2047 2022-07-29 stsp return err;
1413 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1415 279d2047 2022-07-29 stsp return err;
1417 7532ccda 2022-07-11 mark offset_selection_up(v);
1418 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1420 dff91825 2022-07-22 mark if (v->resize)
1421 dff91825 2022-07-22 mark err = v->resize(v, 0);
1422 dff91825 2022-07-22 mark else if (v->child->resize)
1423 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1425 d2366e29 2022-07-07 mark return err;
1429 af21bb7e 2023-04-12 mark * Strip trailing whitespace from str starting at byte *n;
1430 af21bb7e 2023-04-12 mark * if *n < 0, use strlen(str). Return new str length in *n.
1432 af21bb7e 2023-04-12 mark static void
1433 af21bb7e 2023-04-12 mark strip_trailing_ws(char *str, int *n)
1435 af21bb7e 2023-04-12 mark size_t x = *n;
1437 af21bb7e 2023-04-12 mark if (str == NULL || *str == '\0')
1440 af21bb7e 2023-04-12 mark if (x < 0)
1441 af21bb7e 2023-04-12 mark x = strlen(str);
1443 af21bb7e 2023-04-12 mark while (x-- > 0 && isspace((unsigned char)str[x]))
1444 af21bb7e 2023-04-12 mark str[x] = '\0';
1446 af21bb7e 2023-04-12 mark *n = x + 1;
1450 af21bb7e 2023-04-12 mark * Extract visible substring of line y from the curses screen
1451 81641b41 2023-04-20 mark * and strip trailing whitespace. If vline is set, overwrite
1452 81641b41 2023-04-20 mark * line[vline] with '|' because the ACS_VLINE character is
1453 81641b41 2023-04-20 mark * written out as 'x'. Write the line to file f.
1455 af21bb7e 2023-04-12 mark static const struct got_error *
1456 af21bb7e 2023-04-12 mark view_write_line(FILE *f, int y, int vline)
1458 af21bb7e 2023-04-12 mark char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1461 af21bb7e 2023-04-12 mark r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1462 af21bb7e 2023-04-12 mark if (r == ERR)
1463 af21bb7e 2023-04-12 mark return got_error_fmt(GOT_ERR_RANGE,
1464 af21bb7e 2023-04-12 mark "failed to extract line %d", y);
1467 af21bb7e 2023-04-12 mark * In some views, lines are padded with blanks to COLS width.
1468 af21bb7e 2023-04-12 mark * Strip them so we can diff without the -b flag when testing.
1470 af21bb7e 2023-04-12 mark strip_trailing_ws(line, &r);
1472 81641b41 2023-04-20 mark if (vline > 0)
1473 af21bb7e 2023-04-12 mark line[vline] = '|';
1475 af21bb7e 2023-04-12 mark w = fprintf(f, "%s\n", line);
1476 af21bb7e 2023-04-12 mark if (w != r + 1) /* \n */
1477 af21bb7e 2023-04-12 mark return got_ferror(f, GOT_ERR_IO);
1479 af21bb7e 2023-04-12 mark return NULL;
1483 af21bb7e 2023-04-12 mark * Capture the visible curses screen by writing each line to the
1484 af21bb7e 2023-04-12 mark * file at the path set via the TOG_SCR_DUMP environment variable.
1486 af21bb7e 2023-04-12 mark static const struct got_error *
1487 af21bb7e 2023-04-12 mark screendump(struct tog_view *view)
1489 af21bb7e 2023-04-12 mark const struct got_error *err;
1492 3aa652ef 2023-04-21 op err = got_opentemp_truncate(tog_io.sdump);
1496 af21bb7e 2023-04-12 mark if ((view->child && view->child->begin_x) ||
1497 af21bb7e 2023-04-12 mark (view->parent && view->begin_x)) {
1498 af21bb7e 2023-04-12 mark int ncols = view->child ? view->ncols : view->parent->ncols;
1500 af21bb7e 2023-04-12 mark /* vertical splitscreen */
1501 af21bb7e 2023-04-12 mark for (i = 0; i < view->nlines; ++i) {
1502 3aa652ef 2023-04-21 op err = view_write_line(tog_io.sdump, i, ncols - 1);
1504 af21bb7e 2023-04-12 mark goto done;
1507 af21bb7e 2023-04-12 mark int hline = 0;
1509 af21bb7e 2023-04-12 mark /* fullscreen or horizontal splitscreen */
1510 af21bb7e 2023-04-12 mark if ((view->child && view->child->begin_y) ||
1511 af21bb7e 2023-04-12 mark (view->parent && view->begin_y)) /* hsplit */
1512 af21bb7e 2023-04-12 mark hline = view->child ?
1513 af21bb7e 2023-04-12 mark view->child->begin_y : view->begin_y;
1515 af21bb7e 2023-04-12 mark for (i = 0; i < view->lines; i++) {
1516 81641b41 2023-04-20 mark if (hline && i == hline - 1) {
1519 af21bb7e 2023-04-12 mark /* ACS_HLINE writes out as 'q', overwrite it */
1520 af21bb7e 2023-04-12 mark for (c = 0; c < view->cols; ++c)
1521 3aa652ef 2023-04-21 op fputc('-', tog_io.sdump);
1522 3aa652ef 2023-04-21 op fputc('\n', tog_io.sdump);
1526 3aa652ef 2023-04-21 op err = view_write_line(tog_io.sdump, i, 0);
1528 af21bb7e 2023-04-12 mark goto done;
1533 af21bb7e 2023-04-12 mark return err;
1537 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1538 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1540 640cd7ff 2022-06-22 mark static int
1541 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1543 9b058f45 2022-06-30 mark struct tog_view *v = view;
1544 9b058f45 2022-06-30 mark int x, n = 0;
1546 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1547 9b058f45 2022-06-30 mark v = view->child;
1548 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1549 9b058f45 2022-06-30 mark v = view->parent;
1551 640cd7ff 2022-06-22 mark view->count = 0;
1552 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1553 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1554 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1555 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1556 9b058f45 2022-06-30 mark waddch(v->window, ':');
1559 9b058f45 2022-06-30 mark x = getcurx(v->window);
1560 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1561 9b058f45 2022-06-30 mark waddch(v->window, c);
1562 9b058f45 2022-06-30 mark wrefresh(v->window);
1566 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1567 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1569 640cd7ff 2022-06-22 mark if (n >= 9999999)
1570 640cd7ff 2022-06-22 mark n = 9999999;
1572 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1573 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1575 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1576 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1581 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1582 640cd7ff 2022-06-22 mark view->count = n;
1587 3f6c6614 2023-01-23 mark static void
1588 3f6c6614 2023-01-23 mark action_report(struct tog_view *view)
1590 3f6c6614 2023-01-23 mark struct tog_view *v = view;
1592 3f6c6614 2023-01-23 mark if (view_is_hsplit_top(view))
1593 3f6c6614 2023-01-23 mark v = view->child;
1594 3f6c6614 2023-01-23 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1595 3f6c6614 2023-01-23 mark v = view->parent;
1597 3f6c6614 2023-01-23 mark wmove(v->window, v->nlines - 1, 0);
1598 3f6c6614 2023-01-23 mark wclrtoeol(v->window);
1599 3f6c6614 2023-01-23 mark wprintw(v->window, ":%s", view->action);
1600 3f6c6614 2023-01-23 mark wrefresh(v->window);
1603 3f6c6614 2023-01-23 mark * Clear action status report. Only clear in blame view
1604 3f6c6614 2023-01-23 mark * once annotating is complete, otherwise it's too fast.
1606 3f6c6614 2023-01-23 mark if (view->type == TOG_VIEW_BLAME) {
1607 3f6c6614 2023-01-23 mark if (view->state.blame.blame_complete)
1608 3f6c6614 2023-01-23 mark view->action = NULL;
1610 3f6c6614 2023-01-23 mark view->action = NULL;
1614 af21bb7e 2023-04-12 mark * Read the next line from the test script and assign
1615 af21bb7e 2023-04-12 mark * key instruction to *ch. If at EOF, set the *done flag.
1617 0cf4efb1 2018-09-29 stsp static const struct got_error *
1618 d9bb8469 2023-04-20 mark tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1620 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
1621 af21bb7e 2023-04-12 mark char *line = NULL;
1622 af21bb7e 2023-04-12 mark size_t linesz = 0;
1624 d9bb8469 2023-04-20 mark if (view->count && --view->count) {
1625 d9bb8469 2023-04-20 mark *ch = view->ch;
1626 d9bb8469 2023-04-20 mark return NULL;
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;
1640 8d212112 2023-04-16 mark if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1641 8d212112 2023-04-16 mark tog_io.wait_for_ui = 1;
1642 8d212112 2023-04-16 mark else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1643 af21bb7e 2023-04-12 mark *ch = KEY_ENTER;
1644 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1645 af21bb7e 2023-04-12 mark *ch = KEY_RIGHT;
1646 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1647 af21bb7e 2023-04-12 mark *ch = KEY_LEFT;
1648 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1649 af21bb7e 2023-04-12 mark *ch = KEY_DOWN;
1650 af21bb7e 2023-04-12 mark else if (strncasecmp(line, "KEY_UP", 6) == 0)
1651 af21bb7e 2023-04-12 mark *ch = KEY_UP;
1652 c671dc65 2023-04-24 mark else if (strncasecmp(line, "TAB", 3) == 0)
1653 c671dc65 2023-04-24 mark *ch = '\t';
1654 81641b41 2023-04-20 mark else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1655 af21bb7e 2023-04-12 mark *ch = TOG_KEY_SCRDUMP;
1656 d9bb8469 2023-04-20 mark else if (isdigit((unsigned char)*line)) {
1657 d9bb8469 2023-04-20 mark char *t = line;
1659 d9bb8469 2023-04-20 mark while (isdigit((unsigned char)*t))
1661 d9bb8469 2023-04-20 mark view->ch = *ch = *t;
1662 d9bb8469 2023-04-20 mark *t = '\0';
1663 d9bb8469 2023-04-20 mark /* ignore error, view->count is 0 if instruction is invalid */
1664 d9bb8469 2023-04-20 mark view->count = strtonum(line, 0, INT_MAX, NULL);
1666 af21bb7e 2023-04-12 mark *ch = *line;
1669 af21bb7e 2023-04-12 mark free(line);
1670 af21bb7e 2023-04-12 mark return err;
1673 af21bb7e 2023-04-12 mark static const struct got_error *
1674 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1675 098596c5 2023-04-14 stsp struct tog_view_list_head *views, int fast_refresh)
1677 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1678 669b5ffa 2018-10-07 stsp struct tog_view *v;
1679 1a76625f 2018-10-22 stsp int ch, errcode;
1681 e5a0f69f 2018-08-18 stsp *new = NULL;
1683 3f6c6614 2023-01-23 mark if (view->action)
1684 3f6c6614 2023-01-23 mark action_report(view);
1686 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1687 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1688 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1689 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1690 640cd7ff 2022-06-22 mark view->count = 0;
1693 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1694 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1695 82954512 2020-02-03 stsp if (errcode)
1696 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1697 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1698 3da8ef85 2021-09-21 stsp sched_yield();
1699 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1700 82954512 2020-02-03 stsp if (errcode)
1701 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1702 82954512 2020-02-03 stsp "pthread_mutex_lock");
1703 60493ae3 2019-06-20 stsp view->search_next(view);
1704 60493ae3 2019-06-20 stsp return NULL;
1707 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1708 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1709 1a76625f 2018-10-22 stsp if (errcode)
1710 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1712 098596c5 2023-04-14 stsp if (using_mock_io) {
1713 d9bb8469 2023-04-20 mark err = tog_read_script_key(tog_io.f, view, &ch, done);
1714 c736b84a 2023-04-16 mark if (err) {
1715 c736b84a 2023-04-16 mark errcode = pthread_mutex_lock(&tog_mutex);
1716 af21bb7e 2023-04-12 mark return err;
1718 af21bb7e 2023-04-12 mark } else if (view->count && --view->count) {
1720 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1721 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1722 af21bb7e 2023-04-12 mark /* let C-g or backspace abort unfinished count */
1723 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1724 a6d37fac 2022-07-03 mark view->count = 0;
1726 a6d37fac 2022-07-03 mark ch = view->ch;
1728 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1729 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1730 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1732 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1733 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1734 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1735 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1736 1a76625f 2018-10-22 stsp if (errcode)
1737 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1739 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1740 25791caa 2018-10-24 stsp tog_resizeterm();
1741 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1742 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1743 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1744 25791caa 2018-10-24 stsp err = view_resize(v);
1746 25791caa 2018-10-24 stsp return err;
1747 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1749 25791caa 2018-10-24 stsp return err;
1750 cdfcfb03 2020-12-06 stsp if (v->child) {
1751 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1753 cdfcfb03 2020-12-06 stsp return err;
1754 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1755 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1757 cdfcfb03 2020-12-06 stsp return err;
1758 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1759 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1761 3c1dfe12 2022-07-08 mark return err;
1767 e5a0f69f 2018-08-18 stsp switch (ch) {
1770 ec2a9698 2022-09-15 mark case KEY_F(1):
1771 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1772 ec2a9698 2022-09-15 mark err = view->reset(view);
1774 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1777 640cd7ff 2022-06-22 mark view->count = 0;
1778 1e37a5c2 2019-05-12 jcs if (view->child) {
1779 e78dc838 2020-12-04 stsp view->focussed = 0;
1780 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1781 e78dc838 2020-12-04 stsp view->focus_child = 1;
1782 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1783 e78dc838 2020-12-04 stsp view->focussed = 0;
1784 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1785 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1786 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1787 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1788 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1791 9b058f45 2022-06-30 mark return err;
1793 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1794 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1796 9b058f45 2022-06-30 mark return err;
1801 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1802 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1803 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1804 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1808 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1810 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1811 9970f7fc 2020-12-03 stsp view->dying = 1;
1817 640cd7ff 2022-06-22 mark view->count = 0;
1818 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1819 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1821 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1822 e78dc838 2020-12-04 stsp view->focussed = 0;
1823 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1824 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1826 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1828 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1832 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1833 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1835 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1836 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1837 e78dc838 2020-12-04 stsp view->focussed = 1;
1838 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1840 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1841 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1842 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1844 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1848 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1852 6fe51fee 2022-07-22 mark if (view->resize) {
1853 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1857 e56a1796 2023-04-24 mark if (view->parent) {
1858 e56a1796 2023-04-24 mark if (view->parent->resize) {
1859 e56a1796 2023-04-24 mark err = view->parent->resize(view->parent, 0);
1860 e56a1796 2023-04-24 mark if (err != NULL)
1863 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1864 e56a1796 2023-04-24 mark if (err != NULL)
1867 e56a1796 2023-04-24 mark err = offset_selection_down(view);
1870 3c1dfe12 2022-07-08 mark view->count = 0;
1871 d2366e29 2022-07-07 mark err = switch_split(view);
1874 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1877 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1879 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1882 640cd7ff 2022-06-22 mark view->count = 0;
1883 60493ae3 2019-06-20 stsp if (view->search_start)
1884 602eda79 2023-02-09 mark view_search_start(view, fast_refresh);
1886 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1890 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1891 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1892 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1893 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1894 60493ae3 2019-06-20 stsp view->search_next(view);
1896 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1899 3f6c6614 2023-01-23 mark if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1900 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1901 3f6c6614 2023-01-23 mark view->action = "Patience diff algorithm";
1903 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1904 3f6c6614 2023-01-23 mark view->action = "Myers diff algorithm";
1906 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1907 917d79a7 2022-07-01 stsp if (v->reset) {
1908 917d79a7 2022-07-01 stsp err = v->reset(v);
1910 917d79a7 2022-07-01 stsp return err;
1912 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1913 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1915 917d79a7 2022-07-01 stsp return err;
1919 af21bb7e 2023-04-12 mark case TOG_KEY_SCRDUMP:
1920 af21bb7e 2023-04-12 mark err = screendump(view);
1923 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1927 e5a0f69f 2018-08-18 stsp return err;
1931 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1933 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1934 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1936 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1938 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1941 669b5ffa 2018-10-07 stsp return view->focussed;
1944 bcbd79e2 2018-08-19 stsp static const struct got_error *
1945 098596c5 2023-04-14 stsp tog_io_close(void)
1947 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1949 098596c5 2023-04-14 stsp if (tog_io.cin && fclose(tog_io.cin) == EOF)
1950 098596c5 2023-04-14 stsp err = got_ferror(tog_io.cin, GOT_ERR_IO);
1951 098596c5 2023-04-14 stsp if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1952 098596c5 2023-04-14 stsp err = got_ferror(tog_io.cout, GOT_ERR_IO);
1953 098596c5 2023-04-14 stsp if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1954 098596c5 2023-04-14 stsp err = got_ferror(tog_io.f, GOT_ERR_IO);
1955 3aa652ef 2023-04-21 op if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1956 3aa652ef 2023-04-21 op err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1958 af21bb7e 2023-04-12 mark return err;
1961 af21bb7e 2023-04-12 mark static const struct got_error *
1962 098596c5 2023-04-14 stsp view_loop(struct tog_view *view)
1964 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
1965 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1966 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1967 d2366e29 2022-07-07 mark char *mode;
1968 fd823528 2018-10-22 stsp int fast_refresh = 10;
1969 1a76625f 2018-10-22 stsp int done = 0, errcode;
1971 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1972 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1973 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1975 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1977 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1978 1a76625f 2018-10-22 stsp if (errcode)
1979 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1981 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1982 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1984 1004088d 2018-09-29 stsp view->focussed = 1;
1985 878940b7 2018-09-29 stsp err = view->show(view);
1987 0cf4efb1 2018-09-29 stsp return err;
1988 0cf4efb1 2018-09-29 stsp update_panels();
1989 0cf4efb1 2018-09-29 stsp doupdate();
1990 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1991 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1992 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1993 098596c5 2023-04-14 stsp if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1994 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1996 098596c5 2023-04-14 stsp err = view_input(&new_view, &done, view, &views, fast_refresh);
2000 dc2c3344 2023-01-23 mark if (view->dying && view == TAILQ_FIRST(&views) &&
2001 dc2c3344 2023-01-23 mark TAILQ_NEXT(view, entry) == NULL)
2003 dc2c3344 2023-01-23 mark if (done) {
2004 dc2c3344 2023-01-23 mark struct tog_view *v;
2007 dc2c3344 2023-01-23 mark * When we quit, scroll the screen up a single line
2008 dc2c3344 2023-01-23 mark * so we don't lose any information.
2010 dc2c3344 2023-01-23 mark TAILQ_FOREACH(v, &views, entry) {
2011 dc2c3344 2023-01-23 mark wmove(v->window, 0, 0);
2012 dc2c3344 2023-01-23 mark wdeleteln(v->window);
2013 dc2c3344 2023-01-23 mark wnoutrefresh(v->window);
2014 dc2c3344 2023-01-23 mark if (v->child && !view_is_fullscreen(v)) {
2015 dc2c3344 2023-01-23 mark wmove(v->child->window, 0, 0);
2016 dc2c3344 2023-01-23 mark wdeleteln(v->child->window);
2017 dc2c3344 2023-01-23 mark wnoutrefresh(v->child->window);
2020 dc2c3344 2023-01-23 mark doupdate();
2023 9970f7fc 2020-12-03 stsp if (view->dying) {
2024 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2026 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2027 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2029 e78dc838 2020-12-04 stsp else if (view->parent)
2030 669b5ffa 2018-10-07 stsp prev = view->parent;
2032 e78dc838 2020-12-04 stsp if (view->parent) {
2033 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2034 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2035 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
2036 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
2037 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
2040 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
2041 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
2043 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2045 9970f7fc 2020-12-03 stsp err = view_close(view);
2047 e5a0f69f 2018-08-18 stsp goto done;
2049 e78dc838 2020-12-04 stsp view = NULL;
2050 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2051 e78dc838 2020-12-04 stsp if (v->focussed)
2054 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2055 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2057 e78dc838 2020-12-04 stsp view = prev;
2058 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2059 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2060 e78dc838 2020-12-04 stsp tog_view_list_head);
2062 e78dc838 2020-12-04 stsp if (view) {
2063 e78dc838 2020-12-04 stsp if (view->focus_child) {
2064 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2065 e78dc838 2020-12-04 stsp view = view->child;
2067 e78dc838 2020-12-04 stsp view->focussed = 1;
2071 bcbd79e2 2018-08-19 stsp if (new_view) {
2072 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2073 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2074 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2075 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2077 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2078 86c66b02 2018-10-18 stsp err = view_close(v);
2080 86c66b02 2018-10-18 stsp goto done;
2083 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2084 fed7eaa8 2018-10-24 stsp view = new_view;
2086 dc2c3344 2023-01-23 mark if (view && !done) {
2087 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2088 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2089 e78dc838 2020-12-04 stsp view = view->child;
2091 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2092 e78dc838 2020-12-04 stsp view = view->parent;
2094 e78dc838 2020-12-04 stsp show_panel(view->panel);
2095 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2096 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2097 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2098 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2100 1a76625f 2018-10-22 stsp goto done;
2102 669b5ffa 2018-10-07 stsp err = view->show(view);
2104 1a76625f 2018-10-22 stsp goto done;
2105 669b5ffa 2018-10-07 stsp if (view->child) {
2106 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2108 1a76625f 2018-10-22 stsp goto done;
2110 1a76625f 2018-10-22 stsp update_panels();
2111 1a76625f 2018-10-22 stsp doupdate();
2115 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2116 5629093a 2022-07-11 stsp const struct got_error *close_err;
2117 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2118 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2119 5629093a 2022-07-11 stsp close_err = view_close(view);
2120 5629093a 2022-07-11 stsp if (close_err && err == NULL)
2121 5629093a 2022-07-11 stsp err = close_err;
2124 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2125 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2126 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2128 e5a0f69f 2018-08-18 stsp return err;
2131 4ed7e80c 2018-05-20 stsp __dead static void
2132 9f7d7167 2018-04-29 stsp usage_log(void)
2135 c70c5802 2018-08-01 stsp fprintf(stderr,
2136 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2137 9f7d7167 2018-04-29 stsp getprogname());
2141 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2142 80ddbec8 2018-04-29 stsp static const struct got_error *
2143 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2145 00dfcb92 2018-06-11 stsp char *vis = NULL;
2146 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2148 963b370f 2018-05-20 stsp *ws = NULL;
2149 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2150 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2151 00dfcb92 2018-06-11 stsp int vislen;
2152 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2153 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2155 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2156 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2158 00dfcb92 2018-06-11 stsp return err;
2159 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2160 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2161 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2162 a7f50699 2018-06-11 stsp goto done;
2166 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2167 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2168 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2169 a7f50699 2018-06-11 stsp goto done;
2172 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2173 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2175 00dfcb92 2018-06-11 stsp free(vis);
2176 963b370f 2018-05-20 stsp if (err) {
2177 963b370f 2018-05-20 stsp free(*ws);
2178 963b370f 2018-05-20 stsp *ws = NULL;
2179 963b370f 2018-05-20 stsp *wlen = 0;
2181 963b370f 2018-05-20 stsp return err;
2184 145b6838 2022-06-16 stsp static const struct got_error *
2185 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
2187 145b6838 2022-06-16 stsp char *dst;
2188 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
2190 145b6838 2022-06-16 stsp *ptr = NULL;
2191 145b6838 2022-06-16 stsp n = len = strlen(src);
2192 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
2193 145b6838 2022-06-16 stsp if (dst == NULL)
2194 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
2196 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
2197 145b6838 2022-06-16 stsp const char c = src[idx];
2199 145b6838 2022-06-16 stsp if (c == '\t') {
2200 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
2203 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
2204 6e1c41ad 2022-06-16 mark if (p == NULL) {
2205 6e1c41ad 2022-06-16 mark free(dst);
2206 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
2211 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
2214 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
2218 145b6838 2022-06-16 stsp dst[sz] = '\0';
2219 145b6838 2022-06-16 stsp *ptr = dst;
2220 145b6838 2022-06-16 stsp return NULL;
2224 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
2225 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
2226 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
2230 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2232 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
2234 4e4a9ac8 2022-06-17 op if (n == 0) {
2235 4e4a9ac8 2022-06-17 op *rcol = cols;
2239 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
2240 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
2241 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2243 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
2245 4e4a9ac8 2022-06-17 op if (width == -1) {
2247 4e4a9ac8 2022-06-17 op wline[i] = L'.';
2250 4e4a9ac8 2022-06-17 op if (cols + width > n)
2252 4e4a9ac8 2022-06-17 op cols += width;
2255 4e4a9ac8 2022-06-17 op *rcol = cols;
2260 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
2261 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
2262 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2264 ccda2f4d 2022-06-16 stsp static const struct got_error *
2265 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2266 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2268 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2270 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2271 145b6838 2022-06-16 stsp char *exstr = NULL;
2272 963b370f 2018-05-20 stsp size_t wlen;
2273 4e4a9ac8 2022-06-17 op int i, scrollx;
2275 963b370f 2018-05-20 stsp *wlinep = NULL;
2276 b700b5d6 2018-07-10 stsp *widthp = 0;
2278 145b6838 2022-06-16 stsp if (expand) {
2279 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2281 145b6838 2022-06-16 stsp return err;
2284 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2285 145b6838 2022-06-16 stsp free(exstr);
2287 963b370f 2018-05-20 stsp return err;
2289 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2291 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2292 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2295 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2296 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2300 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2301 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2303 b700b5d6 2018-07-10 stsp if (widthp)
2304 b700b5d6 2018-07-10 stsp *widthp = cols;
2305 ccda2f4d 2022-06-16 stsp if (scrollxp)
2306 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2308 963b370f 2018-05-20 stsp free(wline);
2310 963b370f 2018-05-20 stsp *wlinep = wline;
2311 963b370f 2018-05-20 stsp return err;
2314 8b473291 2019-02-21 stsp static const struct got_error*
2315 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2316 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2318 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2319 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2321 8b473291 2019-02-21 stsp const char *name;
2323 8b473291 2019-02-21 stsp *refs_str = NULL;
2325 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2326 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2327 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2330 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2331 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2333 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2334 8b473291 2019-02-21 stsp name += 5;
2335 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2336 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2338 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2339 8b473291 2019-02-21 stsp name += 6;
2340 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2341 8b473291 2019-02-21 stsp name += 8;
2342 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2343 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2346 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2349 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2350 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2351 5d844a1e 2019-08-13 stsp if (err) {
2352 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2353 48cae60d 2020-09-22 stsp free(ref_id);
2356 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2357 5d844a1e 2019-08-13 stsp err = NULL;
2358 5d844a1e 2019-08-13 stsp tag = NULL;
2361 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2362 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2363 48cae60d 2020-09-22 stsp free(ref_id);
2365 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2366 52b5abe1 2019-08-13 stsp if (cmp != 0)
2368 8b473291 2019-02-21 stsp s = *refs_str;
2369 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2370 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2371 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2373 8b473291 2019-02-21 stsp *refs_str = NULL;
2379 8b473291 2019-02-21 stsp return err;
2382 963b370f 2018-05-20 stsp static const struct got_error *
2383 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2384 27a741e5 2019-09-11 stsp int col_tab_align)
2386 e6b8b890 2020-12-29 naddy char *smallerthan;
2388 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2389 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2390 5813d178 2019-03-09 stsp author = smallerthan + 1;
2391 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2392 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2393 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2396 5813d178 2019-03-09 stsp static const struct got_error *
2397 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2398 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2399 8fdc79fe 2020-12-01 naddy int author_display_cols)
2401 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2402 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2403 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2404 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2405 5813d178 2019-03-09 stsp char *author = NULL;
2406 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2407 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2408 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2409 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2410 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2411 ccb26ccd 2018-11-05 stsp struct tm tm;
2412 45d799e2 2018-12-23 stsp time_t committer_time;
2413 11b20872 2019-11-08 stsp struct tog_color *tc;
2415 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2416 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2417 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2418 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2419 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2421 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2422 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2424 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2425 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2427 11b20872 2019-11-08 stsp wattr_on(view->window,
2428 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2429 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2431 11b20872 2019-11-08 stsp wattr_off(view->window,
2432 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2433 27a741e5 2019-09-11 stsp col = limit;
2434 b39d25c7 2018-07-10 stsp if (col > avail)
2435 b39d25c7 2018-07-10 stsp goto done;
2437 6570a66d 2019-11-08 stsp if (avail >= 120) {
2438 6570a66d 2019-11-08 stsp char *id_str;
2439 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2441 6570a66d 2019-11-08 stsp goto done;
2442 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2444 11b20872 2019-11-08 stsp wattr_on(view->window,
2445 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2446 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2448 11b20872 2019-11-08 stsp wattr_off(view->window,
2449 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2450 6570a66d 2019-11-08 stsp free(id_str);
2452 6570a66d 2019-11-08 stsp if (col > avail)
2453 6570a66d 2019-11-08 stsp goto done;
2456 10aab77f 2022-07-19 op if (s->use_committer)
2457 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2459 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2460 5813d178 2019-03-09 stsp if (author == NULL) {
2461 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2462 80ddbec8 2018-04-29 stsp goto done;
2464 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2466 bb737323 2018-05-20 stsp goto done;
2467 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2469 11b20872 2019-11-08 stsp wattr_on(view->window,
2470 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2471 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2472 bb737323 2018-05-20 stsp col += author_width;
2473 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2474 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2476 bb737323 2018-05-20 stsp author_width++;
2479 f0f62654 2022-09-08 mark wattr_off(view->window,
2480 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2481 9c2eaf34 2018-05-20 stsp if (col > avail)
2482 9c2eaf34 2018-05-20 stsp goto done;
2484 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2486 6d9fbc00 2018-04-29 stsp goto done;
2487 bb737323 2018-05-20 stsp logmsg = logmsg0;
2488 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2490 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2491 bb737323 2018-05-20 stsp if (newline)
2492 bb737323 2018-05-20 stsp *newline = '\0';
2493 ccda2f4d 2022-06-16 stsp limit = avail - col;
2494 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2495 4d1f6af3 2022-06-17 op limit--; /* for the border */
2496 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2497 ccda2f4d 2022-06-16 stsp limit, col, 1);
2499 29688b02 2022-06-16 stsp goto done;
2500 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2501 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2502 27a741e5 2019-09-11 stsp while (col < avail) {
2503 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2507 80ddbec8 2018-04-29 stsp free(logmsg0);
2508 bb737323 2018-05-20 stsp free(wlogmsg);
2509 5813d178 2019-03-09 stsp free(author);
2510 bb737323 2018-05-20 stsp free(wauthor);
2511 80ddbec8 2018-04-29 stsp free(line);
2512 80ddbec8 2018-04-29 stsp return err;
2515 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2516 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2517 899d86c2 2018-05-10 stsp struct got_object_id *id)
2519 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2520 e15c42de 2022-09-05 op struct got_object_id *dup;
2522 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2523 80ddbec8 2018-04-29 stsp if (entry == NULL)
2524 899d86c2 2018-05-10 stsp return NULL;
2526 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2527 e15c42de 2022-09-05 op if (dup == NULL) {
2528 e15c42de 2022-09-05 op free(entry);
2529 e15c42de 2022-09-05 op return NULL;
2532 e15c42de 2022-09-05 op entry->id = dup;
2533 99db9666 2018-05-07 stsp entry->commit = commit;
2534 899d86c2 2018-05-10 stsp return entry;
2537 99db9666 2018-05-07 stsp static void
2538 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2540 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2542 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2543 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2544 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2545 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2546 e15c42de 2022-09-05 op free(entry->id);
2547 99db9666 2018-05-07 stsp free(entry);
2550 99db9666 2018-05-07 stsp static void
2551 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2553 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2554 99db9666 2018-05-07 stsp pop_commit(commits);
2557 c4972b91 2018-05-07 stsp static const struct got_error *
2558 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2559 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2561 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2562 13add988 2019-10-15 stsp regmatch_t regmatch;
2563 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2565 13add988 2019-10-15 stsp *have_match = 0;
2567 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2569 13add988 2019-10-15 stsp return err;
2571 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2573 13add988 2019-10-15 stsp goto done;
2575 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2576 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2577 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2578 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2579 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2580 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2581 13add988 2019-10-15 stsp *have_match = 1;
2583 13add988 2019-10-15 stsp free(id_str);
2584 13add988 2019-10-15 stsp free(logmsg);
2585 13add988 2019-10-15 stsp return err;
2588 13add988 2019-10-15 stsp static const struct got_error *
2589 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2591 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2594 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2595 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2596 1a76625f 2018-10-22 stsp * while updating the display.
2599 d9787ed8 2022-09-10 op struct got_object_id id;
2600 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2601 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2602 568eae95 2022-09-11 mark int limit_match = 0;
2603 1a76625f 2018-10-22 stsp int errcode;
2605 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2606 4e0d2870 2020-12-07 naddy NULL, NULL);
2610 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2613 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2614 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2615 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2619 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2620 1a76625f 2018-10-22 stsp if (errcode) {
2621 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2622 13add988 2019-10-15 stsp "pthread_mutex_lock");
2626 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2627 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2628 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2630 568eae95 2022-09-11 mark if (*a->limiting) {
2631 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2632 568eae95 2022-09-11 mark a->limit_regex);
2636 568eae95 2022-09-11 mark if (limit_match) {
2637 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2639 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2640 568eae95 2022-09-11 mark entry->commit, entry->id);
2641 568eae95 2022-09-11 mark if (matched == NULL) {
2642 568eae95 2022-09-11 mark err = got_error_from_errno(
2643 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2646 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2647 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2649 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2650 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2651 568eae95 2022-09-11 mark matched, entry);
2652 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2656 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2657 568eae95 2022-09-11 mark * have found a match, and that it should be
2658 568eae95 2022-09-11 mark * counted as a new entry for the view.
2660 568eae95 2022-09-11 mark a->limit_match = limit_match;
2663 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2664 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2665 7c1452c1 2020-03-26 stsp int have_match;
2666 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2670 568eae95 2022-09-11 mark if (*a->limiting) {
2671 568eae95 2022-09-11 mark if (limit_match && have_match)
2672 568eae95 2022-09-11 mark *a->search_next_done =
2673 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2674 568eae95 2022-09-11 mark } else if (have_match)
2675 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2678 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2679 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2680 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2681 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2684 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2686 9ba79e04 2018-06-11 stsp return err;
2689 2b779855 2020-12-05 naddy static void
2690 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2692 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2693 2b779855 2020-12-05 naddy int ncommits = 0;
2695 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2696 2b779855 2020-12-05 naddy while (entry) {
2697 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2698 2b779855 2020-12-05 naddy s->selected_entry = entry;
2701 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2702 2b779855 2020-12-05 naddy ncommits++;
2706 0553a4e3 2018-04-30 stsp static const struct got_error *
2707 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2709 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2710 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2711 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2712 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2713 60493ae3 2019-06-20 stsp int width;
2714 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2715 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2716 8b473291 2019-02-21 stsp char *refs_str = NULL;
2717 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2718 11b20872 2019-11-08 stsp struct tog_color *tc;
2719 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2721 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2722 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2724 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2725 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2726 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2727 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2729 ecb28ae0 2018-07-16 stsp return err;
2730 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2731 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2732 d2075bf3 2020-12-25 stsp if (refs) {
2733 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2734 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2736 d2075bf3 2020-12-25 stsp goto done;
2740 098596c5 2023-04-14 stsp if (s->thread_args.commits_needed == 0 && !using_mock_io)
2741 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2743 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2744 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2745 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2746 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2747 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2748 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2749 8f4ed634 2020-03-26 stsp goto done;
2752 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2753 568eae95 2022-09-11 mark const char *limit_str = NULL;
2755 f9686aa5 2020-03-27 stsp if (view->searching) {
2756 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2757 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2758 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2759 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2760 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2761 f9686aa5 2020-03-27 stsp search_str = "searching...";
2764 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2765 568eae95 2022-09-11 mark limit_str = "no matches found";
2767 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2768 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2769 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2770 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2771 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2772 8f4ed634 2020-03-26 stsp goto done;
2776 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2777 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2778 87411fa9 2022-06-16 stsp "........................................",
2779 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2780 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2781 1a76625f 2018-10-22 stsp header = NULL;
2782 1a76625f 2018-10-22 stsp goto done;
2784 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2785 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2786 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2787 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2788 1a76625f 2018-10-22 stsp header = NULL;
2789 1a76625f 2018-10-22 stsp goto done;
2791 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2793 1a76625f 2018-10-22 stsp goto done;
2795 2814baeb 2018-08-01 stsp werase(view->window);
2797 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2798 a3404814 2018-09-02 stsp wstandout(view->window);
2799 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2801 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2802 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2803 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2804 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2808 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2809 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2810 a3404814 2018-09-02 stsp wstandend(view->window);
2811 ecb28ae0 2018-07-16 stsp free(wline);
2812 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2813 1a76625f 2018-10-22 stsp goto done;
2815 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2816 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2817 5813d178 2019-03-09 stsp ncommits = 0;
2818 145b6838 2022-06-16 stsp view->maxx = 0;
2819 5813d178 2019-03-09 stsp while (entry) {
2820 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2821 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2822 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2823 5813d178 2019-03-09 stsp int width;
2824 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2826 10aab77f 2022-07-19 op if (s->use_committer)
2827 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2829 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2830 5813d178 2019-03-09 stsp if (author == NULL) {
2831 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2832 5813d178 2019-03-09 stsp goto done;
2834 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2835 27a741e5 2019-09-11 stsp date_display_cols);
2836 5813d178 2019-03-09 stsp if (author_cols < width)
2837 5813d178 2019-03-09 stsp author_cols = width;
2838 5813d178 2019-03-09 stsp free(wauthor);
2839 5813d178 2019-03-09 stsp free(author);
2840 a310d9c3 2022-07-21 florian if (err)
2841 a310d9c3 2022-07-21 florian goto done;
2842 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2844 145b6838 2022-06-16 stsp goto done;
2845 145b6838 2022-06-16 stsp msg = msg0;
2846 145b6838 2022-06-16 stsp while (*msg == '\n')
2848 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2849 29688b02 2022-06-16 stsp *eol = '\0';
2850 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2851 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2853 29688b02 2022-06-16 stsp goto done;
2854 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2855 145b6838 2022-06-16 stsp free(msg0);
2856 29688b02 2022-06-16 stsp free(wmsg);
2857 7ca04879 2019-10-19 stsp ncommits++;
2858 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2861 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2862 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2863 867c6645 2018-07-10 stsp ncommits = 0;
2864 899d86c2 2018-05-10 stsp while (entry) {
2865 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2867 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2868 2814baeb 2018-08-01 stsp wstandout(view->window);
2869 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2870 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2871 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2872 2814baeb 2018-08-01 stsp wstandend(view->window);
2874 60493ae3 2019-06-20 stsp goto done;
2875 0553a4e3 2018-04-30 stsp ncommits++;
2876 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2877 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2880 9b058f45 2022-06-30 mark view_border(view);
2882 1a76625f 2018-10-22 stsp free(id_str);
2883 8b473291 2019-02-21 stsp free(refs_str);
2884 1a76625f 2018-10-22 stsp free(ncommits_str);
2885 1a76625f 2018-10-22 stsp free(header);
2886 80ddbec8 2018-04-29 stsp return err;
2889 07b55e75 2018-05-10 stsp static void
2890 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2892 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2893 07b55e75 2018-05-10 stsp int nscrolled = 0;
2895 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2896 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2899 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2900 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2901 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2902 07b55e75 2018-05-10 stsp if (entry) {
2903 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2904 07b55e75 2018-05-10 stsp nscrolled++;
2909 aa075928 2018-05-10 stsp static const struct got_error *
2910 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2912 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2913 5e224a3e 2019-02-22 stsp int errcode;
2915 098596c5 2023-04-14 stsp if (!using_mock_io)
2916 098596c5 2023-04-14 stsp halfdelay(1); /* fast refresh while loading commits */
2918 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2919 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2920 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2921 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2922 7aafa0d1 2019-02-22 stsp if (errcode)
2923 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2924 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2927 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2928 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2930 7c1452c1 2020-03-26 stsp if (!wait)
2933 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2934 ffe38506 2020-12-01 naddy show_log_view(view);
2935 7c1452c1 2020-03-26 stsp update_panels();
2936 7c1452c1 2020-03-26 stsp doupdate();
2938 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2939 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2940 82954512 2020-02-03 stsp if (errcode)
2941 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2942 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2944 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2945 ffe38506 2020-12-01 naddy show_log_view(view);
2946 7c1452c1 2020-03-26 stsp update_panels();
2947 7c1452c1 2020-03-26 stsp doupdate();
2950 5e224a3e 2019-02-22 stsp return NULL;
2953 5e224a3e 2019-02-22 stsp static const struct got_error *
2954 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2956 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2957 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2959 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2960 2525dccb 2022-07-13 mark return NULL;
2962 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2963 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2964 9b058f45 2022-06-30 mark view->nscrolled = 0;
2966 9b058f45 2022-06-30 mark return err;
2969 9b058f45 2022-06-30 mark static const struct got_error *
2970 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2972 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2973 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2974 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2975 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2977 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2978 5e224a3e 2019-02-22 stsp return NULL;
2980 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2981 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2982 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2984 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2986 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2987 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2988 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2990 5e224a3e 2019-02-22 stsp return err;
2994 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2995 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2998 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2999 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
3001 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3002 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
3004 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
3005 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
3007 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3008 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
3010 9b058f45 2022-06-30 mark view->nscrolled = 0;
3012 dd0a52c1 2018-05-20 stsp return err;
3015 cd0acaa7 2018-05-20 stsp static const struct got_error *
3016 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3017 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
3018 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3020 cd0acaa7 2018-05-20 stsp const struct got_error *err;
3021 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
3022 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3024 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3025 15a94983 2018-12-23 stsp if (diff_view == NULL)
3026 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3028 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3029 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3030 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3031 e5a0f69f 2018-08-18 stsp if (err == NULL)
3032 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3033 cd0acaa7 2018-05-20 stsp return err;
3036 80ddbec8 2018-04-29 stsp static const struct got_error *
3037 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3038 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3040 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3042 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3043 941e9f74 2019-05-21 stsp if (parent == NULL)
3044 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3046 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3047 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3048 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3049 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3050 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3051 941e9f74 2019-05-21 stsp s->tree = subtree;
3052 941e9f74 2019-05-21 stsp s->selected = 0;
3053 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3054 941e9f74 2019-05-21 stsp return NULL;
3057 941e9f74 2019-05-21 stsp static const struct got_error *
3058 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3059 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
3061 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3062 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3063 941e9f74 2019-05-21 stsp const char *p;
3064 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3066 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3068 941e9f74 2019-05-21 stsp while (*p) {
3069 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3070 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3071 56e0773d 2019-11-28 stsp char *te_name;
3073 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3076 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3077 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3078 941e9f74 2019-05-21 stsp if (slash == NULL)
3079 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3081 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3082 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3083 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3086 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3087 56e0773d 2019-11-28 stsp if (te == NULL) {
3088 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3089 56e0773d 2019-11-28 stsp free(te_name);
3092 56e0773d 2019-11-28 stsp free(te_name);
3093 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3095 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3096 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3098 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3099 941e9f74 2019-05-21 stsp if (slash)
3100 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3102 941e9f74 2019-05-21 stsp subpath = strdup(path);
3103 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3104 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3108 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
3113 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3114 941e9f74 2019-05-21 stsp free(tree_id);
3118 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3119 941e9f74 2019-05-21 stsp if (err) {
3120 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3123 941e9f74 2019-05-21 stsp if (slash == NULL)
3125 941e9f74 2019-05-21 stsp free(subpath);
3126 941e9f74 2019-05-21 stsp subpath = NULL;
3127 941e9f74 2019-05-21 stsp p = slash;
3130 941e9f74 2019-05-21 stsp free(subpath);
3131 1a76625f 2018-10-22 stsp return err;
3134 61266923 2020-01-14 stsp static const struct got_error *
3135 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3136 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3137 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3139 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3140 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3141 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3143 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3144 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3145 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3147 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3149 55cccc34 2020-02-20 stsp return err;
3150 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3152 55cccc34 2020-02-20 stsp *new_view = tree_view;
3154 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3155 55cccc34 2020-02-20 stsp return NULL;
3157 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
3160 55cccc34 2020-02-20 stsp static const struct got_error *
3161 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3163 61266923 2020-01-14 stsp sigset_t sigset;
3164 61266923 2020-01-14 stsp int errcode;
3166 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3167 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3169 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3170 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3171 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3172 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3173 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
3174 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
3175 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3176 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
3177 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
3179 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3180 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3181 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3183 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3184 61266923 2020-01-14 stsp if (errcode)
3185 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3187 61266923 2020-01-14 stsp return NULL;
3190 1a76625f 2018-10-22 stsp static void *
3191 1a76625f 2018-10-22 stsp log_thread(void *arg)
3193 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3194 1a76625f 2018-10-22 stsp int errcode = 0;
3195 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3196 1a76625f 2018-10-22 stsp int done = 0;
3199 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
3200 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
3202 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
3203 5629093a 2022-07-11 stsp if (errcode) {
3204 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
3205 61266923 2020-01-14 stsp return (void *)err;
3208 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
3209 5629093a 2022-07-11 stsp if (err) {
3210 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3211 5629093a 2022-07-11 stsp goto done;
3214 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
3215 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3216 5629093a 2022-07-11 stsp if (errcode) {
3217 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
3218 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
3219 5629093a 2022-07-11 stsp goto done;
3221 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3222 1a76625f 2018-10-22 stsp if (err) {
3223 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3224 5629093a 2022-07-11 stsp goto done;
3225 1a76625f 2018-10-22 stsp err = NULL;
3227 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
3228 568eae95 2022-09-11 mark if (*a->limiting) {
3229 568eae95 2022-09-11 mark if (a->limit_match)
3230 568eae95 2022-09-11 mark a->commits_needed--;
3232 568eae95 2022-09-11 mark a->commits_needed--;
3235 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3236 3abe8080 2019-04-10 stsp if (errcode) {
3237 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3238 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3239 5629093a 2022-07-11 stsp goto done;
3240 3abe8080 2019-04-10 stsp } else if (*a->quit)
3242 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
3243 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3244 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
3245 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3246 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
3247 568eae95 2022-09-11 mark *a->first_displayed_entry =
3248 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
3249 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
3252 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3253 7c1452c1 2020-03-26 stsp if (errcode) {
3254 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3255 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3256 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3257 5629093a 2022-07-11 stsp goto done;
3261 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3263 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3264 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3265 7c1452c1 2020-03-26 stsp &tog_mutex);
3266 5629093a 2022-07-11 stsp if (errcode) {
3267 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3268 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3269 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3270 5629093a 2022-07-11 stsp goto done;
3272 21355643 2020-12-06 stsp if (*a->quit)
3277 3abe8080 2019-04-10 stsp a->log_complete = 1;
3278 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3279 5629093a 2022-07-11 stsp if (errcode)
3280 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3282 5629093a 2022-07-11 stsp if (err) {
3283 5629093a 2022-07-11 stsp tog_thread_error = 1;
3284 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3286 1a76625f 2018-10-22 stsp return (void *)err;
3289 1a76625f 2018-10-22 stsp static const struct got_error *
3290 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3292 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3293 1a76625f 2018-10-22 stsp int errcode;
3295 1a76625f 2018-10-22 stsp if (s->thread) {
3296 1a76625f 2018-10-22 stsp s->quit = 1;
3297 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3298 1a76625f 2018-10-22 stsp if (errcode)
3299 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3300 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3301 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3302 1a76625f 2018-10-22 stsp if (errcode)
3303 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3304 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3305 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3306 1a76625f 2018-10-22 stsp if (errcode)
3307 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3308 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3309 1a76625f 2018-10-22 stsp if (errcode)
3310 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3311 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3312 1a76625f 2018-10-22 stsp s->thread = NULL;
3315 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3316 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3317 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3320 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3321 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3322 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3323 74467cc8 2022-06-15 stsp if (err == NULL)
3324 74467cc8 2022-06-15 stsp err = pack_err;
3325 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3328 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3329 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3330 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3333 5629093a 2022-07-11 stsp return err ? err : thread_err;
3336 9343a5fb 2018-06-23 stsp static const struct got_error *
3337 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3339 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3340 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3341 276b94a1 2020-11-13 naddy int errcode;
3343 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3345 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3346 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3347 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3349 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3350 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3351 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3353 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3354 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3355 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3356 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3357 1a76625f 2018-10-22 stsp free(s->start_id);
3358 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3359 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3360 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3361 1a76625f 2018-10-22 stsp return err;
3365 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3366 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3367 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3368 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3369 568eae95 2022-09-11 mark * works with very slight change.
3371 1a76625f 2018-10-22 stsp static const struct got_error *
3372 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3374 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3375 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3376 568eae95 2022-09-11 mark struct tog_view *v = view;
3377 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3378 568eae95 2022-09-11 mark char pattern[1024];
3381 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3382 568eae95 2022-09-11 mark v = view->child;
3383 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3384 568eae95 2022-09-11 mark v = view->parent;
3386 568eae95 2022-09-11 mark /* Get the pattern */
3387 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3388 568eae95 2022-09-11 mark wclrtoeol(v->window);
3389 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3390 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3391 568eae95 2022-09-11 mark nocbreak();
3393 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3396 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3397 568eae95 2022-09-11 mark if (ret == ERR)
3398 568eae95 2022-09-11 mark return NULL;
3400 568eae95 2022-09-11 mark if (*pattern == '\0') {
3402 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3403 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3405 568eae95 2022-09-11 mark if (!s->limit_view)
3406 568eae95 2022-09-11 mark return NULL;
3409 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3410 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3411 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3412 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3413 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3414 568eae95 2022-09-11 mark * the queue.
3416 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3417 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3418 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3419 568eae95 2022-09-11 mark s->selected = 0;
3420 568eae95 2022-09-11 mark s->limit_view = 0;
3422 568eae95 2022-09-11 mark return NULL;
3425 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3426 568eae95 2022-09-11 mark return NULL;
3428 568eae95 2022-09-11 mark s->limit_view = 1;
3430 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3431 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3432 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3433 568eae95 2022-09-11 mark s->selected_entry = NULL;
3434 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3436 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3437 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3438 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3440 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3441 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3442 568eae95 2022-09-11 mark int have_match = 0;
3444 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3445 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3447 568eae95 2022-09-11 mark return err;
3449 568eae95 2022-09-11 mark if (have_match) {
3450 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3452 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3453 568eae95 2022-09-11 mark entry->id);
3454 568eae95 2022-09-11 mark if (matched == NULL) {
3455 568eae95 2022-09-11 mark err = got_error_from_errno(
3456 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3459 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3460 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3462 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3463 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3464 568eae95 2022-09-11 mark matched, entry);
3465 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3469 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3470 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3471 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3472 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3473 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3474 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3476 568eae95 2022-09-11 mark return err;
3479 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3480 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3481 568eae95 2022-09-11 mark s->selected = 0;
3483 568eae95 2022-09-11 mark return NULL;
3486 568eae95 2022-09-11 mark static const struct got_error *
3487 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3489 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3491 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3492 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3493 60493ae3 2019-06-20 stsp return NULL;
3496 60493ae3 2019-06-20 stsp static const struct got_error *
3497 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3499 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3500 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3501 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3503 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3504 f9686aa5 2020-03-27 stsp show_log_view(view);
3505 f9686aa5 2020-03-27 stsp update_panels();
3506 f9686aa5 2020-03-27 stsp doupdate();
3508 96e2b566 2019-07-08 stsp if (s->search_entry) {
3509 13add988 2019-10-15 stsp int errcode, ch;
3510 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3511 13add988 2019-10-15 stsp if (errcode)
3512 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3513 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3514 13add988 2019-10-15 stsp ch = wgetch(view->window);
3515 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3516 13add988 2019-10-15 stsp if (errcode)
3517 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3518 13add988 2019-10-15 stsp "pthread_mutex_lock");
3519 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3520 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3521 678cbce5 2019-07-28 stsp return NULL;
3523 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3524 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3526 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3527 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3528 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3530 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3531 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3532 f704b35c 2022-06-18 stsp * might have changed.
3534 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3535 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3537 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3540 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3543 60493ae3 2019-06-20 stsp while (1) {
3544 13add988 2019-10-15 stsp int have_match = 0;
3546 60493ae3 2019-06-20 stsp if (entry == NULL) {
3547 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3548 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3549 f9967bca 2020-03-27 stsp view->search_next_done =
3550 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3551 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3552 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3553 f801134a 2019-06-25 stsp return NULL;
3556 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3557 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3558 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3560 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3561 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3564 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3565 13add988 2019-10-15 stsp &view->regex);
3568 13add988 2019-10-15 stsp if (have_match) {
3569 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3570 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3574 96e2b566 2019-07-08 stsp s->search_entry = entry;
3575 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3576 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3578 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3581 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3582 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3583 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3584 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3586 60493ae3 2019-06-20 stsp return err;
3589 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3590 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3592 60493ae3 2019-06-20 stsp return err;
3597 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3599 60493ae3 2019-06-20 stsp return NULL;
3602 60493ae3 2019-06-20 stsp static const struct got_error *
3603 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3604 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3605 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3607 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3608 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3609 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3610 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3611 1a76625f 2018-10-22 stsp int errcode;
3613 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3614 f135c941 2020-02-20 stsp free(s->in_repo_path);
3615 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3616 2ca2f982 2023-04-14 mark if (s->in_repo_path == NULL) {
3617 2ca2f982 2023-04-14 mark err = got_error_from_errno("strdup");
3618 2ca2f982 2023-04-14 mark goto done;
3622 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3623 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3624 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3625 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3627 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3628 568eae95 2022-09-11 mark s->limit_view = 0;
3629 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3631 fb2756b9 2018-08-04 stsp s->repo = repo;
3632 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3633 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3634 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3635 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3636 9cd7cbd1 2020-12-07 stsp goto done;
3639 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3640 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3641 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3642 5036bf37 2018-09-24 stsp goto done;
3644 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3645 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3647 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3648 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3649 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3650 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3652 11b20872 2019-11-08 stsp goto done;
3653 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3654 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3655 11b20872 2019-11-08 stsp if (err) {
3656 11b20872 2019-11-08 stsp free_colors(&s->colors);
3657 11b20872 2019-11-08 stsp goto done;
3659 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3660 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3661 11b20872 2019-11-08 stsp if (err) {
3662 11b20872 2019-11-08 stsp free_colors(&s->colors);
3663 11b20872 2019-11-08 stsp goto done;
3667 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3668 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3669 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3670 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3671 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3672 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3674 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3675 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3677 74467cc8 2022-06-15 stsp goto done;
3679 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3680 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3681 0ae84acc 2022-06-15 tracey if (err)
3682 0ae84acc 2022-06-15 tracey goto done;
3683 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3684 b672a97a 2020-01-27 stsp !s->log_branches);
3686 1a76625f 2018-10-22 stsp goto done;
3687 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3688 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3690 c5b78334 2020-01-12 stsp goto done;
3692 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3693 1a76625f 2018-10-22 stsp if (errcode) {
3694 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3695 1a76625f 2018-10-22 stsp goto done;
3697 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3698 7c1452c1 2020-03-26 stsp if (errcode) {
3699 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3700 7c1452c1 2020-03-26 stsp goto done;
3703 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3704 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3705 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3706 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3707 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3708 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3709 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3710 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3711 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3712 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3713 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3714 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3715 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3716 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3717 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3718 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3719 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3721 2ca2f982 2023-04-14 mark if (err) {
3722 2ca2f982 2023-04-14 mark if (view->close == NULL)
3723 2ca2f982 2023-04-14 mark close_log_view(view);
3724 2ca2f982 2023-04-14 mark view_close(view);
3726 ba4f502b 2018-08-04 stsp return err;
3729 e5a0f69f 2018-08-18 stsp static const struct got_error *
3730 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3732 f2f6d207 2020-11-24 stsp const struct got_error *err;
3733 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3735 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3736 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3737 2b380cc8 2018-10-24 stsp &s->thread_args);
3738 2b380cc8 2018-10-24 stsp if (errcode)
3739 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3740 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3741 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3743 f2f6d207 2020-11-24 stsp return err;
3747 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3750 b880cc75 2022-06-30 mark static void
3751 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3753 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3755 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3757 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3758 568eae95 2022-09-11 mark view->count = 0;
3760 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3762 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3764 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3765 b880cc75 2022-06-30 mark --s->selected;
3767 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3769 b880cc75 2022-06-30 mark select_commit(s);
3773 b880cc75 2022-06-30 mark static const struct got_error *
3774 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3776 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3777 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3778 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3780 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3781 568eae95 2022-09-11 mark return NULL;
3783 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3784 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3785 b880cc75 2022-06-30 mark return NULL;
3787 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3788 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3790 631e7531 2022-08-12 mark if (!page) {
3791 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3792 b880cc75 2022-06-30 mark ++s->selected;
3794 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3795 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3796 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3799 631e7531 2022-08-12 mark s->selected = 0;
3800 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3801 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3802 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3803 631e7531 2022-08-12 mark if (entry == NULL)
3805 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3806 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3808 631e7531 2022-08-12 mark if (n > 0)
3809 631e7531 2022-08-12 mark s->selected = n - 1;
3811 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3812 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3813 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3814 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3816 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3819 b880cc75 2022-06-30 mark return err;
3822 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3823 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3825 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3826 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3827 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3828 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3831 b880cc75 2022-06-30 mark select_commit(s);
3833 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3834 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3835 b880cc75 2022-06-30 mark view->count = 0;
3837 b880cc75 2022-06-30 mark return NULL;
3840 9b058f45 2022-06-30 mark static void
3841 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3846 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3847 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3848 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3849 7532ccda 2022-07-11 mark else if (view->resized_y)
3850 7532ccda 2022-07-11 mark *y = view->resized_y;
3852 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3853 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3854 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3855 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3856 7532ccda 2022-07-11 mark else if (view->resized_x)
3857 7532ccda 2022-07-11 mark *x = view->resized_x;
3859 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3863 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3864 e5a0f69f 2018-08-18 stsp static const struct got_error *
3865 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3867 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3869 9b058f45 2022-06-30 mark view->nlines = y;
3870 d2366e29 2022-07-07 mark view->ncols = COLS;
3871 9b058f45 2022-06-30 mark err = view_resize(view);
3873 9b058f45 2022-06-30 mark return err;
3875 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3877 9b058f45 2022-06-30 mark return err;
3880 94b80cfa 2022-08-01 mark static const struct got_error *
3881 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3883 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3884 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3885 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3887 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3888 374f69dd 2022-08-13 stsp return NULL;
3890 94b80cfa 2022-08-01 mark g = view->gline;
3891 94b80cfa 2022-08-01 mark view->gline = 0;
3893 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3894 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3895 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3896 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3897 94b80cfa 2022-08-01 mark select_commit(s);
3898 94b80cfa 2022-08-01 mark return NULL;
3901 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3902 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3903 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3904 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3905 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3907 94b80cfa 2022-08-01 mark return err;
3908 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3909 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3911 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3912 94b80cfa 2022-08-01 mark s->selected = g - 1;
3914 94b80cfa 2022-08-01 mark select_commit(s);
3915 94b80cfa 2022-08-01 mark return NULL;
3919 d627976f 2023-02-01 mark static void
3920 d627976f 2023-02-01 mark horizontal_scroll_input(struct tog_view *view, int ch)
3923 d627976f 2023-02-01 mark switch (ch) {
3924 d627976f 2023-02-01 mark case KEY_LEFT:
3926 d627976f 2023-02-01 mark view->x -= MIN(view->x, 2);
3927 d627976f 2023-02-01 mark if (view->x <= 0)
3928 d627976f 2023-02-01 mark view->count = 0;
3930 d627976f 2023-02-01 mark case KEY_RIGHT:
3932 d627976f 2023-02-01 mark if (view->x + view->ncols / 2 < view->maxx)
3933 d627976f 2023-02-01 mark view->x += 2;
3935 d627976f 2023-02-01 mark view->count = 0;
3938 d627976f 2023-02-01 mark view->x = 0;
3941 d627976f 2023-02-01 mark view->x = MAX(view->maxx - view->ncols / 2, 0);
3942 d627976f 2023-02-01 mark view->count = 0;
3949 9b058f45 2022-06-30 mark static const struct got_error *
3950 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3952 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3953 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3954 631e7531 2022-08-12 mark int eos, nscroll;
3956 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3957 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3958 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3959 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3960 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3961 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3964 11edf34c 2022-08-12 mark return err;
3967 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3968 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3969 0dca135e 2022-06-30 mark --eos; /* border */
3971 94b80cfa 2022-08-01 mark if (view->gline)
3972 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3974 528dedf3 2021-08-30 stsp switch (ch) {
3976 568eae95 2022-09-11 mark err = limit_log_view(view);
3979 1e37a5c2 2019-05-12 jcs s->quit = 1;
3983 145b6838 2022-06-16 stsp case KEY_RIGHT:
3985 145b6838 2022-06-16 stsp case KEY_LEFT:
3987 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
3990 1e37a5c2 2019-05-12 jcs case KEY_UP:
3993 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3994 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3998 912a3f79 2021-08-30 j case KEY_HOME:
3999 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
4000 640cd7ff 2022-06-22 mark view->count = 0;
4002 83cc4199 2022-06-13 stsp case CTRL('u'):
4004 83cc4199 2022-06-13 stsp nscroll /= 2;
4005 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4006 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4007 a4292ac5 2019-05-12 jcs case CTRL('b'):
4009 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
4012 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4015 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4016 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
4019 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
4020 3f6c6614 2023-01-23 mark view->action = s->use_committer ?
4021 3f6c6614 2023-01-23 mark "show committer" : "show commit author";
4025 912a3f79 2021-08-30 j case KEY_END: {
4026 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4027 912a3f79 2021-08-30 j * traverse them all. */
4028 640cd7ff 2022-06-22 mark view->count = 0;
4029 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
4030 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
4031 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4032 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
4033 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
4036 80b7e8da 2022-06-11 stsp case CTRL('d'):
4038 83cc4199 2022-06-13 stsp nscroll /= 2;
4039 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4040 83cc4199 2022-06-13 stsp case KEY_NPAGE:
4041 61417565 2022-06-20 mark case CTRL('f'):
4042 48bb96f0 2022-06-20 naddy case 'f':
4044 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
4046 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4047 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4048 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4049 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
4050 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
4051 2b779855 2020-12-05 naddy select_commit(s);
4052 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
4053 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4054 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4055 568eae95 2022-09-11 mark s->commits->ncommits;
4056 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4059 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4060 49b24ee5 2022-07-03 mark case '\r':
4061 640cd7ff 2022-06-22 mark view->count = 0;
4062 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4064 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4067 640cd7ff 2022-06-22 mark view->count = 0;
4068 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4070 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
4072 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4073 21355643 2020-12-06 stsp case CTRL('l'):
4075 640cd7ff 2022-06-22 mark view->count = 0;
4076 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4077 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4079 21355643 2020-12-06 stsp err = stop_log_thread(s);
4081 74cfe85e 2020-10-20 stsp return err;
4082 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4083 21355643 2020-12-06 stsp char *parent_path;
4084 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4086 21355643 2020-12-06 stsp return err;
4087 21355643 2020-12-06 stsp free(s->in_repo_path);
4088 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4089 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4090 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4091 21355643 2020-12-06 stsp struct got_object_id *start_id;
4092 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4093 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4094 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4095 ea371198 2022-11-17 stsp if (err) {
4096 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
4097 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
4098 ea371198 2022-11-17 stsp return err;
4099 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
4100 ea371198 2022-11-17 stsp free(s->head_ref_name);
4101 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
4102 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
4103 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4104 ea371198 2022-11-17 stsp &tog_refs, s->repo);
4106 ea371198 2022-11-17 stsp return err;
4108 21355643 2020-12-06 stsp free(s->start_id);
4109 21355643 2020-12-06 stsp s->start_id = start_id;
4110 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4111 21355643 2020-12-06 stsp } else /* 'B' */
4112 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4114 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
4115 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4117 b0dd8d27 2022-06-16 stsp return err;
4119 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
4120 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
4121 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
4123 21355643 2020-12-06 stsp return err;
4124 51a10b52 2020-12-26 stsp tog_free_refs();
4125 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
4127 ca51c541 2020-12-07 stsp return err;
4128 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4129 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4131 d01904d4 2019-06-25 stsp return err;
4132 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4133 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4135 b672a97a 2020-01-27 stsp return err;
4136 568eae95 2022-09-11 mark free_commits(&s->real_commits);
4137 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
4138 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4139 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4140 21355643 2020-12-06 stsp s->selected_entry = NULL;
4141 21355643 2020-12-06 stsp s->selected = 0;
4142 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4143 21355643 2020-12-06 stsp s->quit = 0;
4144 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
4145 dfee752e 2022-06-18 op s->matched_entry = NULL;
4146 dfee752e 2022-06-18 op s->search_entry = NULL;
4147 01a7bcaf 2022-07-22 mark view->offset = 0;
4150 640cd7ff 2022-06-22 mark view->count = 0;
4151 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
4154 640cd7ff 2022-06-22 mark view->count = 0;
4158 80ddbec8 2018-04-29 stsp return err;
4161 4ed7e80c 2018-05-20 stsp static const struct got_error *
4162 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4164 c2db6724 2019-01-04 stsp const struct got_error *error;
4166 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4167 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4168 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4170 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4171 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4173 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4174 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4176 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4177 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4179 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4180 c2db6724 2019-01-04 stsp if (error != NULL)
4181 c2db6724 2019-01-04 stsp return error;
4183 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4184 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4186 c2db6724 2019-01-04 stsp return NULL;
4189 af21bb7e 2023-04-12 mark static const struct got_error *
4190 098596c5 2023-04-14 stsp init_mock_term(const char *test_script_path)
4192 af21bb7e 2023-04-12 mark const struct got_error *err = NULL;
4193 3aa652ef 2023-04-21 op const char *screen_dump_path;
4196 af21bb7e 2023-04-12 mark if (test_script_path == NULL || *test_script_path == '\0')
4197 5bde4759 2023-04-15 mark return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4199 098596c5 2023-04-14 stsp tog_io.f = fopen(test_script_path, "re");
4200 098596c5 2023-04-14 stsp if (tog_io.f == NULL) {
4201 af21bb7e 2023-04-12 mark err = got_error_from_errno_fmt("fopen: %s",
4202 af21bb7e 2023-04-12 mark test_script_path);
4203 af21bb7e 2023-04-12 mark goto done;
4206 af21bb7e 2023-04-12 mark /* test mode, we don't want any output */
4207 098596c5 2023-04-14 stsp tog_io.cout = fopen("/dev/null", "w+");
4208 098596c5 2023-04-14 stsp if (tog_io.cout == NULL) {
4209 906c73f6 2023-04-21 op err = got_error_from_errno2("fopen", "/dev/null");
4210 af21bb7e 2023-04-12 mark goto done;
4213 906c73f6 2023-04-21 op in = dup(fileno(tog_io.cout));
4214 906c73f6 2023-04-21 op if (in == -1) {
4215 906c73f6 2023-04-21 op err = got_error_from_errno("dup");
4218 906c73f6 2023-04-21 op tog_io.cin = fdopen(in, "r");
4219 098596c5 2023-04-14 stsp if (tog_io.cin == NULL) {
4220 906c73f6 2023-04-21 op err = got_error_from_errno("fdopen");
4222 af21bb7e 2023-04-12 mark goto done;
4225 3aa652ef 2023-04-21 op screen_dump_path = getenv("TOG_SCR_DUMP");
4226 3aa652ef 2023-04-21 op if (screen_dump_path == NULL || *screen_dump_path == '\0')
4227 3aa652ef 2023-04-21 op return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4228 3aa652ef 2023-04-21 op tog_io.sdump = fopen(screen_dump_path, "wex");
4229 3aa652ef 2023-04-21 op if (tog_io.sdump == NULL) {
4230 3aa652ef 2023-04-21 op err = got_error_from_errno2("fopen", screen_dump_path);
4234 098596c5 2023-04-14 stsp if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4235 af21bb7e 2023-04-12 mark err = got_error_from_errno("fseeko");
4236 af21bb7e 2023-04-12 mark goto done;
4239 098596c5 2023-04-14 stsp if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4240 af21bb7e 2023-04-12 mark err = got_error_msg(GOT_ERR_IO,
4241 af21bb7e 2023-04-12 mark "newterm: failed to initialise curses");
4243 098596c5 2023-04-14 stsp using_mock_io = 1;
4247 098596c5 2023-04-14 stsp tog_io_close();
4248 af21bb7e 2023-04-12 mark return err;
4251 098596c5 2023-04-14 stsp static void
4252 098596c5 2023-04-14 stsp init_curses(void)
4255 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
4256 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
4257 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
4259 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
4260 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
4261 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
4262 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
4263 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
4265 098596c5 2023-04-14 stsp if (using_mock_io) /* In test mode we use a fake terminal */
4268 098596c5 2023-04-14 stsp initscr();
4271 098596c5 2023-04-14 stsp halfdelay(1); /* Fast refresh while initial view is loading. */
4274 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4275 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4276 a915003a 2019-02-05 stsp curs_set(0);
4277 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4278 6d17833f 2019-11-08 stsp start_color();
4279 6d17833f 2019-11-08 stsp use_default_colors();
4285 c2db6724 2019-01-04 stsp static const struct got_error *
4286 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4287 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4289 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4291 f135c941 2020-02-20 stsp if (argc == 0) {
4292 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4293 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4294 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4295 f135c941 2020-02-20 stsp return NULL;
4298 f135c941 2020-02-20 stsp if (worktree) {
4299 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4302 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4304 f135c941 2020-02-20 stsp return err;
4305 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4306 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4307 bfd61697 2020-11-14 stsp p) == -1) {
4308 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4309 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4313 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4315 f135c941 2020-02-20 stsp return err;
4318 f135c941 2020-02-20 stsp static const struct got_error *
4319 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4321 7483826e 2023-04-14 mark const struct got_error *error;
4322 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4323 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4324 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4325 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4326 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4327 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4328 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4329 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4330 04cc582a 2018-08-01 stsp struct tog_view *view;
4331 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4333 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4334 80ddbec8 2018-04-29 stsp switch (ch) {
4336 b672a97a 2020-01-27 stsp log_branches = 1;
4339 80ddbec8 2018-04-29 stsp start_commit = optarg;
4342 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4343 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4344 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4348 17020d27 2019-03-07 stsp usage_log();
4349 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4353 80ddbec8 2018-04-29 stsp argc -= optind;
4354 80ddbec8 2018-04-29 stsp argv += optind;
4356 f135c941 2020-02-20 stsp if (argc > 1)
4357 f135c941 2020-02-20 stsp usage_log();
4359 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4360 0ae84acc 2022-06-15 tracey if (error != NULL)
4361 0ae84acc 2022-06-15 tracey goto done;
4363 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4364 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4365 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4366 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4367 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4368 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4369 c156c7a4 2020-12-18 stsp goto done;
4370 a1fbf39a 2019-08-11 stsp if (worktree)
4371 6962eb72 2020-02-20 stsp repo_path =
4372 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4374 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4375 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4376 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4377 c156c7a4 2020-12-18 stsp goto done;
4381 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4382 80ddbec8 2018-04-29 stsp if (error != NULL)
4383 ecb28ae0 2018-07-16 stsp goto done;
4385 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4386 f135c941 2020-02-20 stsp repo, worktree);
4387 f135c941 2020-02-20 stsp if (error)
4388 f135c941 2020-02-20 stsp goto done;
4390 098596c5 2023-04-14 stsp init_curses();
4392 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4393 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4394 c02c541e 2019-03-29 stsp if (error)
4395 c02c541e 2019-03-29 stsp goto done;
4397 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4398 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4399 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4400 87670572 2020-12-26 naddy if (error)
4401 87670572 2020-12-26 naddy goto done;
4404 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4405 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4406 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4407 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4408 d8f38dc4 2020-12-05 stsp if (error)
4409 d8f38dc4 2020-12-05 stsp goto done;
4410 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4412 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4413 d8f38dc4 2020-12-05 stsp if (error == NULL)
4414 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4415 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4416 d8f38dc4 2020-12-05 stsp goto done;
4417 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4418 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4419 d8f38dc4 2020-12-05 stsp if (error)
4420 d8f38dc4 2020-12-05 stsp goto done;
4423 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4424 04cc582a 2018-08-01 stsp if (view == NULL) {
4425 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4426 04cc582a 2018-08-01 stsp goto done;
4428 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4429 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4430 ba4f502b 2018-08-04 stsp if (error)
4431 ba4f502b 2018-08-04 stsp goto done;
4432 2fc00ff4 2019-08-31 stsp if (worktree) {
4433 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4434 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4435 2fc00ff4 2019-08-31 stsp worktree = NULL;
4437 098596c5 2023-04-14 stsp error = view_loop(view);
4439 f135c941 2020-02-20 stsp free(in_repo_path);
4440 ecb28ae0 2018-07-16 stsp free(repo_path);
4441 ecb28ae0 2018-07-16 stsp free(cwd);
4442 899d86c2 2018-05-10 stsp free(start_id);
4443 d8f38dc4 2020-12-05 stsp free(label);
4445 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4446 1d0f4054 2021-06-17 stsp if (repo) {
4447 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4448 1d0f4054 2021-06-17 stsp if (error == NULL)
4449 1d0f4054 2021-06-17 stsp error = close_err;
4451 ec142235 2019-03-07 stsp if (worktree)
4452 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4453 0ae84acc 2022-06-15 tracey if (pack_fds) {
4454 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4455 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4456 0ae84acc 2022-06-15 tracey if (error == NULL)
4457 0ae84acc 2022-06-15 tracey error = pack_err;
4459 51a10b52 2020-12-26 stsp tog_free_refs();
4460 80ddbec8 2018-04-29 stsp return error;
4463 4ed7e80c 2018-05-20 stsp __dead static void
4464 9f7d7167 2018-04-29 stsp usage_diff(void)
4467 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4468 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4472 6d17833f 2019-11-08 stsp static int
4473 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4474 41605754 2020-11-12 stsp regmatch_t *regmatch)
4476 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4479 336075a4 2022-06-25 op static struct tog_color *
4480 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4482 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4484 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4485 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4486 6d17833f 2019-11-08 stsp return tc;
4489 6d17833f 2019-11-08 stsp return NULL;
4492 4ed7e80c 2018-05-20 stsp static const struct got_error *
4493 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4494 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4496 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4497 44a87665 2022-06-16 stsp char *exstr = NULL;
4498 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4499 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4500 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4501 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4503 41605754 2020-11-12 stsp *wtotal = 0;
4505 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4506 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4508 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4510 44a87665 2022-06-16 stsp return err;
4512 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4513 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4514 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4515 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4516 44a87665 2022-06-16 stsp goto done;
4518 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4519 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4520 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4521 1853e0f4 2022-06-16 stsp goto done;
4523 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4524 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4525 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4526 1853e0f4 2022-06-16 stsp goto done;
4529 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4530 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 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 n = MAX(width0 - skipcol, 0);
4536 1853e0f4 2022-06-16 stsp free(wline);
4537 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4538 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4540 1853e0f4 2022-06-16 stsp goto done;
4541 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4542 1853e0f4 2022-06-16 stsp wlimit -= width;
4543 1853e0f4 2022-06-16 stsp *wtotal += width;
4546 41605754 2020-11-12 stsp if (wlimit > 0) {
4547 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4548 1853e0f4 2022-06-16 stsp size_t wlen;
4550 1853e0f4 2022-06-16 stsp free(wline);
4551 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4552 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4554 1853e0f4 2022-06-16 stsp goto done;
4555 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4556 1853e0f4 2022-06-16 stsp while (i < wlen) {
4557 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4558 1853e0f4 2022-06-16 stsp if (width == -1) {
4559 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4560 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4561 1853e0f4 2022-06-16 stsp goto done;
4563 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4565 61417565 2022-06-20 mark w += width;
4568 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4569 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4570 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4571 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4572 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4573 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4574 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4578 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4579 1853e0f4 2022-06-16 stsp free(wline);
4580 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4581 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4582 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4583 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4585 1853e0f4 2022-06-16 stsp goto done;
4586 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4588 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4589 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4591 1853e0f4 2022-06-16 stsp goto done;
4592 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4594 1853e0f4 2022-06-16 stsp *wtotal += width2;
4597 145b6838 2022-06-16 stsp free(wline);
4598 44a87665 2022-06-16 stsp free(exstr);
4599 1853e0f4 2022-06-16 stsp free(seg0);
4600 1853e0f4 2022-06-16 stsp free(seg1);
4601 1853e0f4 2022-06-16 stsp free(seg2);
4602 1853e0f4 2022-06-16 stsp return err;
4605 94b80cfa 2022-08-01 mark static int
4606 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4608 94b80cfa 2022-08-01 mark FILE *f = NULL;
4609 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4611 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4612 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4614 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4615 ec2a9698 2022-09-15 mark selected = first;
4616 ec2a9698 2022-09-15 mark eof = &s->eof;
4618 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4619 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4621 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4622 94b80cfa 2022-08-01 mark selected = first;
4623 94b80cfa 2022-08-01 mark eof = &s->eof;
4625 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4626 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4628 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4629 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4630 94b80cfa 2022-08-01 mark eof = &s->eof;
4631 94b80cfa 2022-08-01 mark f = s->blame.f;
4635 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4636 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4638 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4639 94b80cfa 2022-08-01 mark rewind(f);
4641 94b80cfa 2022-08-01 mark *first = 1;
4642 94b80cfa 2022-08-01 mark *lineno = 0;
4643 94b80cfa 2022-08-01 mark *nprinted = 0;
4647 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4648 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4649 94b80cfa 2022-08-01 mark view->gline = 0;
4654 41605754 2020-11-12 stsp static const struct got_error *
4655 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4657 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4658 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4659 61e69b96 2018-05-20 stsp const struct got_error *err;
4660 c7d5c43c 2022-08-04 mark int nprinted = 0;
4661 b304db33 2018-05-20 stsp char *line;
4662 826082fe 2020-12-10 stsp size_t linesize = 0;
4663 826082fe 2020-12-10 stsp ssize_t linelen;
4664 61e69b96 2018-05-20 stsp wchar_t *wline;
4665 e0b650dd 2018-05-20 stsp int width;
4666 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4667 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4668 fe621944 2020-11-10 stsp off_t line_offset;
4670 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4671 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4672 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4673 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4675 f7d12f7e 2018-08-01 stsp werase(view->window);
4677 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4678 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4680 a3404814 2018-09-02 stsp if (header) {
4681 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4682 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4683 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4685 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4686 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4687 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4689 135a2da0 2020-11-11 stsp free(line);
4691 a3404814 2018-09-02 stsp return err;
4693 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4694 a3404814 2018-09-02 stsp wstandout(view->window);
4695 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4696 e54cc94a 2020-11-11 stsp free(wline);
4697 e54cc94a 2020-11-11 stsp wline = NULL;
4698 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4699 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4700 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4701 a3404814 2018-09-02 stsp wstandend(view->window);
4703 a3404814 2018-09-02 stsp if (max_lines <= 1)
4704 a3404814 2018-09-02 stsp return NULL;
4705 a3404814 2018-09-02 stsp max_lines--;
4708 89f1a395 2020-12-01 naddy s->eof = 0;
4709 145b6838 2022-06-16 stsp view->maxx = 0;
4710 826082fe 2020-12-10 stsp line = NULL;
4711 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4712 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4713 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4715 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4716 826082fe 2020-12-10 stsp if (linelen == -1) {
4717 826082fe 2020-12-10 stsp if (feof(s->f)) {
4718 826082fe 2020-12-10 stsp s->eof = 1;
4721 826082fe 2020-12-10 stsp free(line);
4722 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4725 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4727 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4729 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4730 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4732 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4733 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4734 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4735 1853e0f4 2022-06-16 stsp if (err) {
4736 1853e0f4 2022-06-16 stsp free(line);
4737 1853e0f4 2022-06-16 stsp return err;
4739 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4740 1853e0f4 2022-06-16 stsp free(wline);
4741 1853e0f4 2022-06-16 stsp wline = NULL;
4743 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4744 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4745 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4746 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4748 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4749 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4750 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4751 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4752 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4753 41605754 2020-11-12 stsp if (err) {
4754 41605754 2020-11-12 stsp free(line);
4755 41605754 2020-11-12 stsp return err;
4759 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4760 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4761 969c159c 2022-06-16 stsp if (err) {
4762 969c159c 2022-06-16 stsp free(line);
4763 969c159c 2022-06-16 stsp return err;
4765 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4766 969c159c 2022-06-16 stsp free(wline);
4767 41605754 2020-11-12 stsp wline = NULL;
4769 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4770 94b80cfa 2022-08-01 mark /* highlight full gline length */
4771 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4772 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4774 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4775 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4778 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4779 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4780 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4782 826082fe 2020-12-10 stsp free(line);
4783 fe621944 2020-11-10 stsp if (nprinted >= 1)
4784 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4785 89f1a395 2020-12-01 naddy (nprinted - 1);
4787 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4789 9b058f45 2022-06-30 mark view_border(view);
4791 89f1a395 2020-12-01 naddy if (s->eof) {
4792 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4793 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4794 c3e9aa98 2019-05-13 jcs nprinted++;
4797 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4798 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4800 c3e9aa98 2019-05-13 jcs return err;
4803 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4804 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4805 e54cc94a 2020-11-11 stsp free(wline);
4806 e54cc94a 2020-11-11 stsp wline = NULL;
4807 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4810 26ed57b2 2018-05-19 stsp return NULL;
4813 abd2672a 2018-12-23 stsp static char *
4814 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4816 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4817 09867e48 2019-08-13 stsp char *p, *s;
4819 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4820 09867e48 2019-08-13 stsp if (tm == NULL)
4821 09867e48 2019-08-13 stsp return NULL;
4822 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4823 09867e48 2019-08-13 stsp if (s == NULL)
4824 09867e48 2019-08-13 stsp return NULL;
4825 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4827 abd2672a 2018-12-23 stsp *p = '\0';
4831 4ed7e80c 2018-05-20 stsp static const struct got_error *
4832 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4833 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4835 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4837 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4838 fe621944 2020-11-10 stsp if (p == NULL)
4839 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4840 c7d5c43c 2022-08-04 mark *lines = p;
4841 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4842 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4843 fe621944 2020-11-10 stsp (*nlines)++;
4845 fe621944 2020-11-10 stsp return NULL;
4848 fe621944 2020-11-10 stsp static const struct got_error *
4849 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4850 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4852 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4853 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4854 1f3405c9 2023-01-17 mark size_t i, r;
4856 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4857 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4859 1f3405c9 2023-01-17 mark for (;;) {
4860 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4861 1f3405c9 2023-01-17 mark if (r == 0) {
4862 1f3405c9 2023-01-17 mark if (ferror(src))
4863 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4864 1f3405c9 2023-01-17 mark if (feof(src))
4867 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4868 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4871 5afebbd1 2023-02-22 mark if (s_nlines == 0 && *d_nlines == 0)
4872 5afebbd1 2023-02-22 mark return NULL;
4875 5afebbd1 2023-02-22 mark * If commit info was in dst, increment line offsets
4876 5afebbd1 2023-02-22 mark * of the appended diff content, but skip s_lines[0]
4877 5afebbd1 2023-02-22 mark * because offset zero is already in *d_lines.
4879 5afebbd1 2023-02-22 mark if (*d_nlines > 0) {
4880 5afebbd1 2023-02-22 mark for (i = 1; i < s_nlines; ++i)
4881 5afebbd1 2023-02-22 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4883 5afebbd1 2023-02-22 mark if (s_nlines > 0) {
4884 5afebbd1 2023-02-22 mark --s_nlines;
4885 5afebbd1 2023-02-22 mark ++s_lines;
4889 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4890 1f3405c9 2023-01-17 mark if (p == NULL) {
4891 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4892 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4895 1f3405c9 2023-01-17 mark *d_lines = p;
4897 5afebbd1 2023-02-22 mark memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4898 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4900 1f3405c9 2023-01-17 mark return NULL;
4903 1f3405c9 2023-01-17 mark static const struct got_error *
4904 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4905 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4906 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4907 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4909 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4910 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4911 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4912 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4913 45d799e2 2018-12-23 stsp time_t committer_time;
4914 45d799e2 2018-12-23 stsp const char *author, *committer;
4915 8b473291 2019-02-21 stsp char *refs_str = NULL;
4916 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4917 fe621944 2020-11-10 stsp off_t outoff = 0;
4920 8b473291 2019-02-21 stsp if (refs) {
4921 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4923 8b473291 2019-02-21 stsp return err;
4926 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4928 abd2672a 2018-12-23 stsp return err;
4930 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4931 15a94983 2018-12-23 stsp if (err) {
4932 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4933 15a94983 2018-12-23 stsp goto done;
4936 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4938 fe621944 2020-11-10 stsp goto done;
4940 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4941 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4942 fe621944 2020-11-10 stsp if (n < 0) {
4943 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4944 abd2672a 2018-12-23 stsp goto done;
4946 fe621944 2020-11-10 stsp outoff += n;
4947 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4949 fe621944 2020-11-10 stsp goto done;
4951 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4952 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4953 fe621944 2020-11-10 stsp if (n < 0) {
4954 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4955 abd2672a 2018-12-23 stsp goto done;
4957 fe621944 2020-11-10 stsp outoff += n;
4958 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4960 fe621944 2020-11-10 stsp goto done;
4962 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4963 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4964 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4965 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4966 fe621944 2020-11-10 stsp if (n < 0) {
4967 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4968 fe621944 2020-11-10 stsp goto done;
4970 fe621944 2020-11-10 stsp outoff += n;
4971 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4972 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4974 fe621944 2020-11-10 stsp goto done;
4976 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4977 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4978 1a99e0b4 2023-01-07 stsp if (datestr) {
4979 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4980 fe621944 2020-11-10 stsp if (n < 0) {
4981 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4982 fe621944 2020-11-10 stsp goto done;
4984 fe621944 2020-11-10 stsp outoff += n;
4985 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4986 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4988 fe621944 2020-11-10 stsp goto done;
4990 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4991 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4992 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4993 9f98ca05 2021-09-24 stsp int pn = 1;
4994 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4995 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4996 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4998 9f98ca05 2021-09-24 stsp goto done;
4999 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5000 9f98ca05 2021-09-24 stsp if (n < 0) {
5001 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
5002 9f98ca05 2021-09-24 stsp goto done;
5004 9f98ca05 2021-09-24 stsp outoff += n;
5005 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
5006 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
5008 9f98ca05 2021-09-24 stsp goto done;
5009 9f98ca05 2021-09-24 stsp free(id_str);
5010 9f98ca05 2021-09-24 stsp id_str = NULL;
5014 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
5016 5943eee2 2019-08-13 stsp goto done;
5017 fe621944 2020-11-10 stsp s = logmsg;
5018 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
5019 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
5020 fe621944 2020-11-10 stsp if (n < 0) {
5021 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5022 fe621944 2020-11-10 stsp goto done;
5024 fe621944 2020-11-10 stsp outoff += n;
5025 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
5026 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
5028 fe621944 2020-11-10 stsp goto done;
5031 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
5032 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5033 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
5035 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5036 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5037 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
5038 fe621944 2020-11-10 stsp if (n < 0) {
5039 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5040 fe621944 2020-11-10 stsp goto done;
5042 fe621944 2020-11-10 stsp outoff += n;
5043 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
5044 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
5046 fe621944 2020-11-10 stsp goto done;
5049 0208f208 2020-05-05 stsp fputc('\n', outfile);
5051 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5053 5191b70b 2023-01-07 mark goto done;
5055 5191b70b 2023-01-07 mark n = fprintf(outfile,
5056 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5057 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5058 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5059 5191b70b 2023-01-07 mark if (n < 0) {
5060 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
5061 5191b70b 2023-01-07 mark goto done;
5063 5191b70b 2023-01-07 mark outoff += n;
5064 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5066 5191b70b 2023-01-07 mark goto done;
5068 5191b70b 2023-01-07 mark fputc('\n', outfile);
5070 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5072 abd2672a 2018-12-23 stsp free(id_str);
5073 5943eee2 2019-08-13 stsp free(logmsg);
5074 8b473291 2019-02-21 stsp free(refs_str);
5075 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5076 7510f233 2020-08-09 stsp if (err) {
5077 c7d5c43c 2022-08-04 mark free(*lines);
5078 c7d5c43c 2022-08-04 mark *lines = NULL;
5079 ae6a6978 2020-08-09 stsp *nlines = 0;
5081 fe621944 2020-11-10 stsp return err;
5084 abd2672a 2018-12-23 stsp static const struct got_error *
5085 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5087 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5088 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
5089 15a94983 2018-12-23 stsp int obj_type;
5090 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
5091 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
5093 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
5095 c7d5c43c 2022-08-04 mark free(s->lines);
5096 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
5097 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
5098 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5099 fe621944 2020-11-10 stsp s->nlines = 0;
5101 511a516b 2018-05-19 stsp f = got_opentemp();
5102 48ae06ee 2018-10-18 stsp if (f == NULL) {
5103 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5104 48ae06ee 2018-10-18 stsp goto done;
5106 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
5107 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
5108 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
5109 1f3405c9 2023-01-17 mark goto done;
5111 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5112 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5113 fb43ecf1 2019-02-11 stsp goto done;
5117 15a94983 2018-12-23 stsp if (s->id1)
5118 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5120 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5122 15a94983 2018-12-23 stsp goto done;
5124 15a94983 2018-12-23 stsp switch (obj_type) {
5125 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5126 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5127 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5128 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
5129 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5132 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5133 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5134 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5135 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
5136 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
5138 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5139 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5140 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5141 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5142 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5143 1f3405c9 2023-01-17 mark size_t nlines = 0;
5144 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
5145 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
5146 1f3405c9 2023-01-17 mark &changed_paths,
5147 1f3405c9 2023-01-17 mark s->ignore_whitespace,
5148 1f3405c9 2023-01-17 mark s->force_text_diff,
5149 1f3405c9 2023-01-17 mark tog_diff_algo
5152 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
5153 1f3405c9 2023-01-17 mark if (lines == NULL) {
5154 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
5155 1f3405c9 2023-01-17 mark goto done;
5158 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
5159 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
5160 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5161 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
5162 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5166 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5168 3ffacbe1 2020-02-02 tracey goto done;
5169 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5170 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5171 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5172 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
5173 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
5174 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
5175 f44b1f58 2020-02-02 tracey if (err)
5176 f44b1f58 2020-02-02 tracey goto done;
5177 f44b1f58 2020-02-02 tracey } else {
5178 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5179 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5180 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5181 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
5182 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
5183 5191b70b 2023-01-07 mark s->ignore_whitespace,
5184 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
5185 f44b1f58 2020-02-02 tracey if (err)
5186 f44b1f58 2020-02-02 tracey goto done;
5191 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5193 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5194 1f3405c9 2023-01-17 mark lines, nlines);
5198 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5202 1f3405c9 2023-01-17 mark free(lines);
5203 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5204 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5205 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5206 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5207 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
5208 48ae06ee 2018-10-18 stsp return err;
5211 f5215bb9 2019-02-22 stsp static void
5212 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5214 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5215 f5215bb9 2019-02-22 stsp update_panels();
5216 f5215bb9 2019-02-22 stsp doupdate();
5219 f44b1f58 2020-02-02 tracey static const struct got_error *
5220 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5222 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5224 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5225 f44b1f58 2020-02-02 tracey return NULL;
5228 eaf2c13e 2022-09-17 mark static void
5229 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5230 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
5232 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
5234 eaf2c13e 2022-09-17 mark *f = s->f;
5235 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
5236 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
5237 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
5238 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
5239 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
5240 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
5243 ec2a9698 2022-09-15 mark static const struct got_error *
5244 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
5246 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5248 f44b1f58 2020-02-02 tracey int lineno;
5249 cb713507 2022-06-16 stsp char *line = NULL;
5250 826082fe 2020-12-10 stsp size_t linesize = 0;
5251 826082fe 2020-12-10 stsp ssize_t linelen;
5252 ec2a9698 2022-09-15 mark off_t *line_offsets;
5253 ec2a9698 2022-09-15 mark size_t nlines = 0;
5254 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
5256 eaf2c13e 2022-09-17 mark if (!view->search_setup)
5257 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
5258 eaf2c13e 2022-09-17 mark "view search not supported");
5259 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5260 ec2a9698 2022-09-15 mark &match, &selected);
5262 f44b1f58 2020-02-02 tracey if (!view->searching) {
5263 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5264 f44b1f58 2020-02-02 tracey return NULL;
5267 ec2a9698 2022-09-15 mark if (*match) {
5268 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5269 8c28a2ab 2023-04-02 mark lineno = *first + 1;
5271 8c28a2ab 2023-04-02 mark lineno = *first - 1;
5273 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
5275 f44b1f58 2020-02-02 tracey while (1) {
5276 f44b1f58 2020-02-02 tracey off_t offset;
5278 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
5279 ec2a9698 2022-09-15 mark if (*match == 0) {
5280 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5284 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5285 f44b1f58 2020-02-02 tracey lineno = 1;
5287 ec2a9698 2022-09-15 mark lineno = nlines;
5290 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
5291 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
5292 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
5293 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
5294 f44b1f58 2020-02-02 tracey free(line);
5295 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5297 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
5298 cb713507 2022-06-16 stsp if (linelen != -1) {
5299 cb713507 2022-06-16 stsp char *exstr;
5300 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5303 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5304 cb713507 2022-06-16 stsp &view->regmatch)) {
5305 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5306 ec2a9698 2022-09-15 mark *match = lineno;
5307 cb713507 2022-06-16 stsp free(exstr);
5310 cb713507 2022-06-16 stsp free(exstr);
5312 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5313 f44b1f58 2020-02-02 tracey lineno++;
5315 f44b1f58 2020-02-02 tracey lineno--;
5317 826082fe 2020-12-10 stsp free(line);
5319 ec2a9698 2022-09-15 mark if (*match) {
5320 ec2a9698 2022-09-15 mark *first = *match;
5321 ec2a9698 2022-09-15 mark *selected = 1;
5324 145b6838 2022-06-16 stsp return err;
5327 48ae06ee 2018-10-18 stsp static const struct got_error *
5328 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
5330 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
5331 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5333 b72706c3 2022-06-01 stsp free(s->id1);
5334 b72706c3 2022-06-01 stsp s->id1 = NULL;
5335 b72706c3 2022-06-01 stsp free(s->id2);
5336 b72706c3 2022-06-01 stsp s->id2 = NULL;
5337 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
5338 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5339 b72706c3 2022-06-01 stsp s->f = NULL;
5340 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5341 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5342 b72706c3 2022-06-01 stsp s->f1 = NULL;
5343 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5344 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5345 b72706c3 2022-06-01 stsp s->f2 = NULL;
5346 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5347 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5348 f9d37699 2022-06-28 stsp s->fd1 = -1;
5349 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5350 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5351 f9d37699 2022-06-28 stsp s->fd2 = -1;
5352 c7d5c43c 2022-08-04 mark free(s->lines);
5353 c7d5c43c 2022-08-04 mark s->lines = NULL;
5354 b72706c3 2022-06-01 stsp s->nlines = 0;
5355 b72706c3 2022-06-01 stsp return err;
5358 b72706c3 2022-06-01 stsp static const struct got_error *
5359 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5360 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5361 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5362 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
5364 48ae06ee 2018-10-18 stsp const struct got_error *err;
5365 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5367 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
5368 f9d37699 2022-06-28 stsp s->fd1 = -1;
5369 f9d37699 2022-06-28 stsp s->fd2 = -1;
5371 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5372 2ca2f982 2023-04-14 mark int type1, type2;
5374 2ca2f982 2023-04-14 mark err = got_object_get_type(&type1, repo, id1);
5376 2ca2f982 2023-04-14 mark goto done;
5377 2ca2f982 2023-04-14 mark err = got_object_get_type(&type2, repo, id2);
5379 2ca2f982 2023-04-14 mark goto done;
5381 2ca2f982 2023-04-14 mark if (type1 != type2) {
5382 2ca2f982 2023-04-14 mark err = got_error(GOT_ERR_OBJ_TYPE);
5383 2ca2f982 2023-04-14 mark goto done;
5386 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5387 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5388 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5389 f44b1f58 2020-02-02 tracey s->repo = repo;
5390 f44b1f58 2020-02-02 tracey s->id1 = id1;
5391 f44b1f58 2020-02-02 tracey s->id2 = id2;
5392 3dbaef42 2020-11-24 stsp s->label1 = label1;
5393 3dbaef42 2020-11-24 stsp s->label2 = label2;
5395 15a94983 2018-12-23 stsp if (id1) {
5396 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5397 2ca2f982 2023-04-14 mark if (s->id1 == NULL) {
5398 2ca2f982 2023-04-14 mark err = got_error_from_errno("got_object_id_dup");
5399 2ca2f982 2023-04-14 mark goto done;
5402 5465d566 2020-02-01 tracey s->id1 = NULL;
5404 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5405 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5406 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5407 b72706c3 2022-06-01 stsp goto done;
5410 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5411 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5412 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5413 a00719e9 2022-06-17 stsp goto done;
5416 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5417 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5418 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5419 b72706c3 2022-06-01 stsp goto done;
5422 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5423 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5424 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5425 f9d37699 2022-06-28 stsp goto done;
5428 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5429 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5430 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5431 f9d37699 2022-06-28 stsp goto done;
5434 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5435 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5436 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5437 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5438 5465d566 2020-02-01 tracey s->repo = repo;
5440 098596c5 2023-04-14 stsp if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5443 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5444 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5445 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5446 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5447 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5448 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5449 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5450 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5451 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5452 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5453 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5454 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5455 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5456 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5457 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5458 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5459 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5460 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5461 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5462 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5463 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5464 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5465 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5466 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5467 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5468 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5469 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5470 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5471 b72706c3 2022-06-01 stsp goto done;
5475 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5476 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5477 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5478 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5480 5465d566 2020-02-01 tracey err = create_diff(s);
5482 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5483 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5484 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5485 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5486 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5487 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5488 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5490 2ca2f982 2023-04-14 mark if (err) {
5491 2ca2f982 2023-04-14 mark if (view->close == NULL)
5492 2ca2f982 2023-04-14 mark close_diff_view(view);
5493 2ca2f982 2023-04-14 mark view_close(view);
5495 e5a0f69f 2018-08-18 stsp return err;
5498 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5499 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5501 a3404814 2018-09-02 stsp const struct got_error *err;
5502 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5503 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5504 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5506 a3404814 2018-09-02 stsp if (s->id1) {
5507 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5509 a3404814 2018-09-02 stsp return err;
5510 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5512 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5514 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5516 a3404814 2018-09-02 stsp return err;
5517 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5519 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5520 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5521 a3404814 2018-09-02 stsp free(id_str1);
5522 a3404814 2018-09-02 stsp free(id_str2);
5523 a3404814 2018-09-02 stsp return err;
5525 a3404814 2018-09-02 stsp free(id_str1);
5526 a3404814 2018-09-02 stsp free(id_str2);
5528 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5529 267bb3b8 2021-08-01 stsp free(header);
5530 267bb3b8 2021-08-01 stsp return err;
5533 15a087fe 2019-02-21 stsp static const struct got_error *
5534 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5535 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5537 d7a04538 2019-02-21 stsp const struct got_error *err;
5538 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5539 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5540 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5542 15a087fe 2019-02-21 stsp free(s->id2);
5543 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5544 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5545 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5547 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5549 d7a04538 2019-02-21 stsp return err;
5550 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5551 15a087fe 2019-02-21 stsp free(s->id1);
5552 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5553 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5554 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5555 15a087fe 2019-02-21 stsp return NULL;
5558 0cf4efb1 2018-09-29 stsp static const struct got_error *
5559 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5561 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5563 917d79a7 2022-07-01 stsp view->count = 0;
5564 917d79a7 2022-07-01 stsp wclear(view->window);
5565 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5566 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5567 917d79a7 2022-07-01 stsp s->matched_line = 0;
5568 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5569 917d79a7 2022-07-01 stsp return create_diff(s);
5572 c7d5c43c 2022-08-04 mark static void
5573 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5575 c7d5c43c 2022-08-04 mark int start, i;
5577 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5579 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5580 c7d5c43c 2022-08-04 mark if (i == 0)
5581 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5582 c7d5c43c 2022-08-04 mark if (--i == start)
5583 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5586 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5587 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5590 c7d5c43c 2022-08-04 mark static void
5591 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5593 c7d5c43c 2022-08-04 mark int start, i;
5595 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5597 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5598 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5600 c7d5c43c 2022-08-04 mark if (++i == start)
5601 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5604 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5605 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5608 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5609 c0f61fa4 2022-07-11 mark int, int, int);
5610 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5611 c0f61fa4 2022-07-11 mark int, int);
5613 917d79a7 2022-07-01 stsp static const struct got_error *
5614 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5616 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5617 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5618 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5619 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5620 826082fe 2020-12-10 stsp char *line = NULL;
5621 826082fe 2020-12-10 stsp size_t linesize = 0;
5622 826082fe 2020-12-10 stsp ssize_t linelen;
5623 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5625 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5627 e5a0f69f 2018-08-18 stsp switch (ch) {
5630 145b6838 2022-06-16 stsp case KEY_RIGHT:
5632 145b6838 2022-06-16 stsp case KEY_LEFT:
5634 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
5638 3f6c6614 2023-01-23 mark if (ch == 'a') {
5639 3f6c6614 2023-01-23 mark s->force_text_diff = !s->force_text_diff;
5640 3f6c6614 2023-01-23 mark view->action = s->force_text_diff ?
5641 3f6c6614 2023-01-23 mark "force ASCII text enabled" :
5642 3f6c6614 2023-01-23 mark "force ASCII text disabled";
5644 3f6c6614 2023-01-23 mark else if (ch == 'w') {
5645 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5646 3f6c6614 2023-01-23 mark view->action = s->ignore_whitespace ?
5647 3f6c6614 2023-01-23 mark "ignore whitespace enabled" :
5648 3f6c6614 2023-01-23 mark "ignore whitespace disabled";
5650 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5653 912a3f79 2021-08-30 j case KEY_HOME:
5654 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5655 640cd7ff 2022-06-22 mark view->count = 0;
5658 912a3f79 2021-08-30 j case KEY_END:
5659 640cd7ff 2022-06-22 mark view->count = 0;
5663 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5667 1e37a5c2 2019-05-12 jcs case KEY_UP:
5668 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5669 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5670 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5672 640cd7ff 2022-06-22 mark view->count = 0;
5674 83cc4199 2022-06-13 stsp case CTRL('u'):
5676 83cc4199 2022-06-13 stsp nscroll /= 2;
5677 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5678 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5679 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5681 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5682 640cd7ff 2022-06-22 mark view->count = 0;
5686 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5687 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5690 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5691 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5692 1e37a5c2 2019-05-12 jcs if (!s->eof)
5693 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5695 640cd7ff 2022-06-22 mark view->count = 0;
5697 83cc4199 2022-06-13 stsp case CTRL('d'):
5699 83cc4199 2022-06-13 stsp nscroll /= 2;
5700 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5701 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5702 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5705 640cd7ff 2022-06-22 mark if (s->eof) {
5706 640cd7ff 2022-06-22 mark view->count = 0;
5710 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5711 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5712 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5713 826082fe 2020-12-10 stsp if (linelen == -1) {
5714 826082fe 2020-12-10 stsp if (feof(s->f)) {
5715 826082fe 2020-12-10 stsp s->eof = 1;
5717 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5721 826082fe 2020-12-10 stsp free(line);
5724 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5727 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5730 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5733 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5736 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5737 1e37a5c2 2019-05-12 jcs s->diff_context--;
5738 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5739 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5740 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5741 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5742 27829c9e 2020-11-21 stsp s->nlines) {
5743 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5744 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5747 640cd7ff 2022-06-22 mark view->count = 0;
5750 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5751 1e37a5c2 2019-05-12 jcs s->diff_context++;
5752 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5753 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5754 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5756 640cd7ff 2022-06-22 mark view->count = 0;
5762 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5766 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5767 640cd7ff 2022-06-22 mark view->count = 0;
5770 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5772 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5773 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5774 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5776 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5777 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5780 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5782 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5785 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5788 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5789 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5790 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5792 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5793 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5794 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5796 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5797 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5800 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5802 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5804 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5805 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5806 c0f61fa4 2022-07-11 mark bs->selected_line);
5807 c0f61fa4 2022-07-11 mark if (id == NULL)
5810 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5813 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5817 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5818 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5819 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5820 145b6838 2022-06-16 stsp view->x = 0;
5822 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5823 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5826 640cd7ff 2022-06-22 mark view->count = 0;
5830 bcbd79e2 2018-08-19 stsp return err;
5833 4ed7e80c 2018-05-20 stsp static const struct got_error *
5834 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5836 7483826e 2023-04-14 mark const struct got_error *error;
5837 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5838 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5839 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5840 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5841 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5842 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5843 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5844 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5845 3dbaef42 2020-11-24 stsp const char *errstr;
5846 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5847 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5849 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5850 26ed57b2 2018-05-19 stsp switch (ch) {
5852 64453f7e 2020-11-21 stsp force_text_diff = 1;
5855 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5857 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5858 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5859 5a20d08d 2022-02-09 op errstr, errstr);
5861 09b5bff8 2020-02-23 naddy case 'r':
5862 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5863 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5864 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5866 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5869 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5872 17020d27 2019-03-07 stsp usage_diff();
5873 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5877 26ed57b2 2018-05-19 stsp argc -= optind;
5878 26ed57b2 2018-05-19 stsp argv += optind;
5880 26ed57b2 2018-05-19 stsp if (argc == 0) {
5881 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5882 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5883 15a94983 2018-12-23 stsp id_str1 = argv[0];
5884 15a94983 2018-12-23 stsp id_str2 = argv[1];
5886 26ed57b2 2018-05-19 stsp usage_diff();
5888 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5889 0ae84acc 2022-06-15 tracey if (error)
5890 0ae84acc 2022-06-15 tracey goto done;
5892 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5893 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5894 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5895 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5896 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5897 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5898 c156c7a4 2020-12-18 stsp goto done;
5899 a273ac94 2020-02-23 naddy if (worktree)
5900 a273ac94 2020-02-23 naddy repo_path =
5901 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5903 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5904 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5905 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5906 c156c7a4 2020-12-18 stsp goto done;
5910 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5911 eb6600df 2019-01-04 stsp if (error)
5912 eb6600df 2019-01-04 stsp goto done;
5914 098596c5 2023-04-14 stsp init_curses();
5916 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5917 51a10b52 2020-12-26 stsp if (error)
5918 51a10b52 2020-12-26 stsp goto done;
5920 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5921 26ed57b2 2018-05-19 stsp if (error)
5922 26ed57b2 2018-05-19 stsp goto done;
5924 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5925 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5926 26ed57b2 2018-05-19 stsp if (error)
5927 26ed57b2 2018-05-19 stsp goto done;
5929 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5930 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5931 26ed57b2 2018-05-19 stsp if (error)
5932 26ed57b2 2018-05-19 stsp goto done;
5934 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5935 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5936 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5937 ea5e7bb5 2018-08-01 stsp goto done;
5939 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5940 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5941 5dc9f4bc 2018-08-04 stsp if (error)
5942 5dc9f4bc 2018-08-04 stsp goto done;
5943 098596c5 2023-04-14 stsp error = view_loop(view);
5945 3dbaef42 2020-11-24 stsp free(label1);
5946 3dbaef42 2020-11-24 stsp free(label2);
5947 c02c541e 2019-03-29 stsp free(repo_path);
5948 a273ac94 2020-02-23 naddy free(cwd);
5949 1d0f4054 2021-06-17 stsp if (repo) {
5950 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5951 1d0f4054 2021-06-17 stsp if (error == NULL)
5952 1d0f4054 2021-06-17 stsp error = close_err;
5954 a273ac94 2020-02-23 naddy if (worktree)
5955 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5956 0ae84acc 2022-06-15 tracey if (pack_fds) {
5957 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5958 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5959 0ae84acc 2022-06-15 tracey if (error == NULL)
5960 0ae84acc 2022-06-15 tracey error = pack_err;
5962 51a10b52 2020-12-26 stsp tog_free_refs();
5963 26ed57b2 2018-05-19 stsp return error;
5966 4ed7e80c 2018-05-20 stsp __dead static void
5967 9f7d7167 2018-04-29 stsp usage_blame(void)
5970 87411fa9 2022-06-16 stsp fprintf(stderr,
5971 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5972 9f7d7167 2018-04-29 stsp getprogname());
5976 84451b3e 2018-07-10 stsp struct tog_blame_line {
5977 84451b3e 2018-07-10 stsp int annotated;
5978 84451b3e 2018-07-10 stsp struct got_object_id *id;
5981 4ed7e80c 2018-05-20 stsp static const struct got_error *
5982 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5984 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5985 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5986 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5987 84451b3e 2018-07-10 stsp const struct got_error *err;
5988 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5989 826082fe 2020-12-10 stsp char *line = NULL;
5990 826082fe 2020-12-10 stsp size_t linesize = 0;
5991 826082fe 2020-12-10 stsp ssize_t linelen;
5992 84451b3e 2018-07-10 stsp wchar_t *wline;
5993 27a741e5 2019-09-11 stsp int width;
5994 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5995 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5996 ab089a2a 2018-07-12 stsp char *id_str;
5997 11b20872 2019-11-08 stsp struct tog_color *tc;
5999 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
6001 ab089a2a 2018-07-12 stsp return err;
6003 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
6004 f7d12f7e 2018-08-01 stsp werase(view->window);
6006 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
6007 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6008 ab089a2a 2018-07-12 stsp free(id_str);
6009 ab089a2a 2018-07-12 stsp return err;
6012 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6013 ab089a2a 2018-07-12 stsp free(line);
6014 2550e4c3 2018-07-13 stsp line = NULL;
6016 1cae65b4 2019-09-22 stsp return err;
6017 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6018 a3404814 2018-09-02 stsp wstandout(view->window);
6019 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6021 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6022 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6023 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6024 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6026 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6027 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6028 a3404814 2018-09-02 stsp wstandend(view->window);
6029 2550e4c3 2018-07-13 stsp free(wline);
6030 2550e4c3 2018-07-13 stsp wline = NULL;
6032 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
6033 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
6035 8496bf63 2023-04-20 mark if (tog_io.wait_for_ui) {
6036 8496bf63 2023-04-20 mark struct tog_blame_thread_args *bta = &s->blame.thread_args;
6039 8496bf63 2023-04-20 mark rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6041 8496bf63 2023-04-20 mark return got_error_set_errno(rc, "pthread_cond_wait");
6042 8496bf63 2023-04-20 mark tog_io.wait_for_ui = 0;
6045 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6046 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6047 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6048 ab089a2a 2018-07-12 stsp free(id_str);
6049 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6051 ab089a2a 2018-07-12 stsp free(id_str);
6052 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6053 3f60a8ef 2018-07-10 stsp free(line);
6054 2550e4c3 2018-07-13 stsp line = NULL;
6056 3f60a8ef 2018-07-10 stsp return err;
6057 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6058 2550e4c3 2018-07-13 stsp free(wline);
6059 2550e4c3 2018-07-13 stsp wline = NULL;
6060 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6061 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6063 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6064 145b6838 2022-06-16 stsp view->maxx = 0;
6065 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6066 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6067 826082fe 2020-12-10 stsp if (linelen == -1) {
6068 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6069 826082fe 2020-12-10 stsp s->eof = 1;
6072 84451b3e 2018-07-10 stsp free(line);
6073 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6075 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6077 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
6080 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
6081 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6082 1853e0f4 2022-06-16 stsp if (err) {
6083 1853e0f4 2022-06-16 stsp free(line);
6084 1853e0f4 2022-06-16 stsp return err;
6086 1853e0f4 2022-06-16 stsp free(wline);
6087 1853e0f4 2022-06-16 stsp wline = NULL;
6088 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
6090 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
6091 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6093 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6094 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6095 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6096 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6097 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
6098 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6099 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6100 8d0fe45a 2019-08-12 stsp char *id_str;
6101 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
6102 87411fa9 2022-06-16 stsp blame_line->id);
6103 8d0fe45a 2019-08-12 stsp if (err) {
6104 8d0fe45a 2019-08-12 stsp free(line);
6105 8d0fe45a 2019-08-12 stsp return err;
6107 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6109 11b20872 2019-11-08 stsp wattr_on(view->window,
6110 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6111 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6113 11b20872 2019-11-08 stsp wattr_off(view->window,
6114 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6115 8d0fe45a 2019-08-12 stsp free(id_str);
6116 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6118 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6119 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6122 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6123 ee41ec32 2018-07-10 stsp prev_id = NULL;
6126 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
6127 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6128 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6130 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6131 41605754 2020-11-12 stsp width = 9;
6132 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6133 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6134 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6135 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6136 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
6137 41605754 2020-11-12 stsp if (err) {
6138 41605754 2020-11-12 stsp free(line);
6139 41605754 2020-11-12 stsp return err;
6141 41605754 2020-11-12 stsp width += 9;
6144 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
6145 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
6146 4cb9d869 2022-06-16 stsp if (err) {
6147 4cb9d869 2022-06-16 stsp free(line);
6148 4cb9d869 2022-06-16 stsp return err;
6150 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
6151 a75b3e2d 2022-06-16 stsp width += 9;
6152 41605754 2020-11-12 stsp free(wline);
6153 41605754 2020-11-12 stsp wline = NULL;
6156 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6157 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6158 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6159 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6161 826082fe 2020-12-10 stsp free(line);
6162 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6164 9b058f45 2022-06-30 mark view_border(view);
6166 84451b3e 2018-07-10 stsp return NULL;
6169 84451b3e 2018-07-10 stsp static const struct got_error *
6170 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
6171 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
6173 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6174 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6175 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6176 1a76625f 2018-10-22 stsp int errcode;
6178 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6179 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6180 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6182 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6183 1a76625f 2018-10-22 stsp if (errcode)
6184 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6186 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6187 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6188 d68a0a7d 2018-07-10 stsp goto done;
6191 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6192 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6194 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6195 d68a0a7d 2018-07-10 stsp if (line->annotated)
6196 d68a0a7d 2018-07-10 stsp goto done;
6198 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6199 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6200 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6201 84451b3e 2018-07-10 stsp goto done;
6203 84451b3e 2018-07-10 stsp line->annotated = 1;
6205 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6206 1a76625f 2018-10-22 stsp if (errcode)
6207 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6208 84451b3e 2018-07-10 stsp return err;
6211 84451b3e 2018-07-10 stsp static void *
6212 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6214 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6215 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6216 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6217 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
6218 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
6220 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
6221 e6e73e55 2022-06-30 tracey if (fd1 == -1)
6222 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
6224 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
6225 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
6226 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
6227 e6e73e55 2022-06-30 tracey goto done;
6230 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
6231 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
6232 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
6233 e6e73e55 2022-06-30 tracey goto done;
6235 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
6236 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
6237 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
6238 e6e73e55 2022-06-30 tracey goto done;
6241 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6243 e6e73e55 2022-06-30 tracey goto done;
6245 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6246 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
6247 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6248 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6249 fc06ba56 2019-08-22 stsp err = NULL;
6251 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6252 e6e73e55 2022-06-30 tracey if (errcode) {
6253 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
6254 e6e73e55 2022-06-30 tracey goto done;
6257 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6258 1d0f4054 2021-06-17 stsp if (err == NULL)
6259 1d0f4054 2021-06-17 stsp err = close_err;
6260 c9beca56 2018-07-22 stsp ta->repo = NULL;
6261 c9beca56 2018-07-22 stsp *ta->complete = 1;
6263 8496bf63 2023-04-20 mark if (tog_io.wait_for_ui) {
6264 8496bf63 2023-04-20 mark errcode = pthread_cond_signal(&ta->blame_complete);
6265 8496bf63 2023-04-20 mark if (errcode && err == NULL)
6266 8496bf63 2023-04-20 mark err = got_error_set_errno(errcode,
6267 8496bf63 2023-04-20 mark "pthread_cond_signal");
6270 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6271 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6272 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6275 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6276 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
6277 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6278 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
6279 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
6280 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
6281 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
6282 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
6284 18430de3 2018-07-10 stsp return (void *)err;
6287 245d91c1 2018-07-12 stsp static struct got_object_id *
6288 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6289 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6291 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6293 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6294 8d0fe45a 2019-08-12 stsp return NULL;
6296 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6297 c0f61fa4 2022-07-11 mark if (!line->annotated)
6298 c0f61fa4 2022-07-11 mark return NULL;
6300 c0f61fa4 2022-07-11 mark return line->id;
6303 c0f61fa4 2022-07-11 mark static struct got_object_id *
6304 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6305 c0f61fa4 2022-07-11 mark int lineno)
6307 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
6309 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
6310 c0f61fa4 2022-07-11 mark return NULL;
6312 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
6313 245d91c1 2018-07-12 stsp if (!line->annotated)
6314 245d91c1 2018-07-12 stsp return NULL;
6316 245d91c1 2018-07-12 stsp return line->id;
6319 b880a918 2018-07-10 stsp static const struct got_error *
6320 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6322 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6325 245d91c1 2018-07-12 stsp if (blame->thread) {
6326 1a76625f 2018-10-22 stsp int errcode;
6327 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6328 1a76625f 2018-10-22 stsp if (errcode)
6329 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6330 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6331 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6332 1a76625f 2018-10-22 stsp if (errcode)
6333 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6334 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6335 1a76625f 2018-10-22 stsp if (errcode)
6336 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6337 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6338 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6339 245d91c1 2018-07-12 stsp err = NULL;
6340 245d91c1 2018-07-12 stsp blame->thread = NULL;
6342 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6343 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6344 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6345 1d0f4054 2021-06-17 stsp if (err == NULL)
6346 1d0f4054 2021-06-17 stsp err = close_err;
6347 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6349 245d91c1 2018-07-12 stsp if (blame->f) {
6350 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6351 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6352 245d91c1 2018-07-12 stsp blame->f = NULL;
6354 57670559 2018-12-23 stsp if (blame->lines) {
6355 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6356 57670559 2018-12-23 stsp free(blame->lines[i].id);
6357 57670559 2018-12-23 stsp free(blame->lines);
6358 57670559 2018-12-23 stsp blame->lines = NULL;
6360 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6361 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6362 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
6363 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6364 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
6365 0ae84acc 2022-06-15 tracey if (err == NULL)
6366 0ae84acc 2022-06-15 tracey err = pack_err;
6367 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
6369 245d91c1 2018-07-12 stsp return err;
6372 245d91c1 2018-07-12 stsp static const struct got_error *
6373 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6375 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6376 fc06ba56 2019-08-22 stsp int *done = arg;
6377 fc06ba56 2019-08-22 stsp int errcode;
6379 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6380 fc06ba56 2019-08-22 stsp if (errcode)
6381 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6382 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6384 fc06ba56 2019-08-22 stsp if (*done)
6385 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6387 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6388 fc06ba56 2019-08-22 stsp if (errcode)
6389 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6390 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6392 fc06ba56 2019-08-22 stsp return err;
6395 fc06ba56 2019-08-22 stsp static const struct got_error *
6396 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6398 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6399 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6400 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6401 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6402 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6403 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6404 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6405 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6406 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6408 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6409 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6411 15a94983 2018-12-23 stsp return err;
6413 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6414 eb81bc23 2022-06-28 tracey if (fd == -1) {
6415 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6416 eb81bc23 2022-06-28 tracey goto done;
6419 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6421 a44927cc 2022-04-07 stsp goto done;
6423 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6425 84451b3e 2018-07-10 stsp goto done;
6427 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6428 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6429 84451b3e 2018-07-10 stsp goto done;
6432 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6434 a70480e0 2018-06-23 stsp goto done;
6435 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6436 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6437 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6438 84451b3e 2018-07-10 stsp goto done;
6440 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6441 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6443 1fddf795 2021-01-20 stsp goto done;
6444 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6445 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6446 84451b3e 2018-07-10 stsp goto done;
6449 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6450 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6451 b02560ec 2019-08-19 stsp blame->nlines--;
6453 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6454 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6455 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6456 84451b3e 2018-07-10 stsp goto done;
6459 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6461 bd24772e 2018-07-11 stsp goto done;
6462 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6463 0ae84acc 2022-06-15 tracey pack_fds);
6464 0ae84acc 2022-06-15 tracey if (err)
6465 0ae84acc 2022-06-15 tracey goto done;
6467 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6468 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6469 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6470 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6471 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6472 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6473 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6474 245d91c1 2018-07-12 stsp goto done;
6476 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6478 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6479 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6480 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6481 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6482 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6483 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6484 a5388363 2020-12-01 naddy s->blame_complete = 0;
6486 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6487 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6488 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6489 f5a09613 2020-12-13 naddy s->selected_line = 1;
6491 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6494 a44927cc 2022-04-07 stsp if (commit)
6495 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6496 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6497 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6499 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6500 27d434c2 2018-09-15 stsp free(obj_id);
6502 245d91c1 2018-07-12 stsp stop_blame(blame);
6503 245d91c1 2018-07-12 stsp return err;
6506 245d91c1 2018-07-12 stsp static const struct got_error *
6507 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6508 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6510 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6511 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6513 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6515 c4843652 2019-08-12 stsp s->path = strdup(path);
6516 c4843652 2019-08-12 stsp if (s->path == NULL)
6517 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6519 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6520 c4843652 2019-08-12 stsp if (err) {
6521 c4843652 2019-08-12 stsp free(s->path);
6522 7cbe629d 2018-08-04 stsp return err;
6525 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6526 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6527 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6528 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6529 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6530 fb2756b9 2018-08-04 stsp s->repo = repo;
6531 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6532 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6534 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6535 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6536 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6537 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6539 11b20872 2019-11-08 stsp return err;
6542 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6543 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6544 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6545 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6546 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6547 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6548 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6550 8496bf63 2023-04-20 mark if (using_mock_io) {
6551 8496bf63 2023-04-20 mark struct tog_blame_thread_args *bta = &s->blame.thread_args;
6554 8496bf63 2023-04-20 mark rc = pthread_cond_init(&bta->blame_complete, NULL);
6556 8496bf63 2023-04-20 mark return got_error_set_errno(rc, "pthread_cond_init");
6559 a5388363 2020-12-01 naddy return run_blame(view);
6562 e5a0f69f 2018-08-18 stsp static const struct got_error *
6563 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6565 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6566 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6568 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6569 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6571 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6572 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6573 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6574 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6575 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6578 8496bf63 2023-04-20 mark if (using_mock_io) {
6579 8496bf63 2023-04-20 mark struct tog_blame_thread_args *bta = &s->blame.thread_args;
6582 8496bf63 2023-04-20 mark rc = pthread_cond_destroy(&bta->blame_complete);
6583 8496bf63 2023-04-20 mark if (rc && err == NULL)
6584 8496bf63 2023-04-20 mark err = got_error_set_errno(rc, "pthread_cond_destroy");
6587 e5a0f69f 2018-08-18 stsp free(s->path);
6588 11b20872 2019-11-08 stsp free_colors(&s->colors);
6589 e5a0f69f 2018-08-18 stsp return err;
6592 7cbe629d 2018-08-04 stsp static const struct got_error *
6593 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6595 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6597 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6598 6c4c42e0 2019-06-24 stsp return NULL;
6601 eaf2c13e 2022-09-17 mark static void
6602 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6603 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6605 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6607 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6608 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6609 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6610 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6611 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6612 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6613 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6616 6c4c42e0 2019-06-24 stsp static const struct got_error *
6617 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6619 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6620 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6621 2b380cc8 2018-10-24 stsp int errcode;
6623 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6624 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6625 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6626 2b380cc8 2018-10-24 stsp if (errcode)
6627 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6629 098596c5 2023-04-14 stsp if (!using_mock_io)
6630 098596c5 2023-04-14 stsp halfdelay(1); /* fast refresh while annotating */
6633 098596c5 2023-04-14 stsp if (s->blame_complete && !using_mock_io)
6634 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6636 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6638 9b058f45 2022-06-30 mark view_border(view);
6639 e5a0f69f 2018-08-18 stsp return err;
6642 e5a0f69f 2018-08-18 stsp static const struct got_error *
6643 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6644 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6646 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6647 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6649 05f04cdf 2022-07-20 mark *new_view = NULL;
6651 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6652 05f04cdf 2022-07-20 mark if (log_view == NULL)
6653 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6655 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6657 05f04cdf 2022-07-20 mark view_close(log_view);
6659 05f04cdf 2022-07-20 mark *new_view = log_view;
6661 05f04cdf 2022-07-20 mark return err;
6664 05f04cdf 2022-07-20 mark static const struct got_error *
6665 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6667 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6668 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6669 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6670 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6672 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6673 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6674 9b058f45 2022-06-30 mark --eos; /* border */
6676 e5a0f69f 2018-08-18 stsp switch (ch) {
6679 145b6838 2022-06-16 stsp case KEY_RIGHT:
6681 145b6838 2022-06-16 stsp case KEY_LEFT:
6683 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
6686 1e37a5c2 2019-05-12 jcs s->done = 1;
6688 4deef56f 2021-09-02 naddy case 'g':
6689 4deef56f 2021-09-02 naddy case KEY_HOME:
6690 4deef56f 2021-09-02 naddy s->selected_line = 1;
6691 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6692 640cd7ff 2022-06-22 mark view->count = 0;
6694 4deef56f 2021-09-02 naddy case 'G':
6695 4deef56f 2021-09-02 naddy case KEY_END:
6696 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6697 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6698 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6700 9b058f45 2022-06-30 mark s->selected_line = eos;
6701 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6703 640cd7ff 2022-06-22 mark view->count = 0;
6706 1e37a5c2 2019-05-12 jcs case KEY_UP:
6707 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6708 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6709 1e37a5c2 2019-05-12 jcs s->selected_line--;
6710 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6711 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6712 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6714 640cd7ff 2022-06-22 mark view->count = 0;
6716 83cc4199 2022-06-13 stsp case CTRL('u'):
6718 83cc4199 2022-06-13 stsp nscroll /= 2;
6719 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6720 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6721 ea025d1d 2020-02-22 naddy case CTRL('b'):
6723 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6724 640cd7ff 2022-06-22 mark if (view->count > 1)
6725 640cd7ff 2022-06-22 mark nscroll += nscroll;
6726 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6727 640cd7ff 2022-06-22 mark view->count = 0;
6730 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6731 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6733 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6736 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6737 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6738 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6739 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6740 1e37a5c2 2019-05-12 jcs s->selected_line++;
6741 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6742 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6744 640cd7ff 2022-06-22 mark view->count = 0;
6747 1e37a5c2 2019-05-12 jcs case 'p': {
6748 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6750 640cd7ff 2022-06-22 mark view->count = 0;
6751 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6752 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6753 1e37a5c2 2019-05-12 jcs if (id == NULL)
6755 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6756 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6757 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6758 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6759 1e37a5c2 2019-05-12 jcs int obj_type;
6760 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6761 1e37a5c2 2019-05-12 jcs s->repo, id);
6764 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6765 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6766 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6767 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6770 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6771 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6772 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6775 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6776 a44927cc 2022-04-07 stsp pcommit, s->path);
6777 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6778 e5a0f69f 2018-08-18 stsp if (err) {
6779 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6780 1e37a5c2 2019-05-12 jcs err = NULL;
6781 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6784 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6786 1e37a5c2 2019-05-12 jcs free(blob_id);
6787 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6788 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6789 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6792 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6793 d7b5a0e8 2022-04-20 stsp &pid->id);
6794 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6796 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6797 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6799 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6804 1e37a5c2 2019-05-12 jcs s->done = 1;
6805 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6806 1e37a5c2 2019-05-12 jcs s->done = 0;
6807 1e37a5c2 2019-05-12 jcs if (thread_err)
6809 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6810 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6811 a5388363 2020-12-01 naddy err = run_blame(view);
6816 61417565 2022-06-20 mark case 'C': {
6817 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6819 640cd7ff 2022-06-22 mark view->count = 0;
6820 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6821 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6823 1e37a5c2 2019-05-12 jcs s->done = 1;
6824 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6825 1e37a5c2 2019-05-12 jcs s->done = 0;
6826 1e37a5c2 2019-05-12 jcs if (thread_err)
6828 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6829 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6830 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6831 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6832 a5388363 2020-12-01 naddy err = run_blame(view);
6838 05f04cdf 2022-07-20 mark view->count = 0;
6839 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6840 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6841 136e2bd4 2022-07-23 mark if (s->id_to_log)
6842 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6844 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6845 1e37a5c2 2019-05-12 jcs case '\r': {
6846 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6847 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6848 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6850 640cd7ff 2022-06-22 mark view->count = 0;
6851 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6852 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6853 1e37a5c2 2019-05-12 jcs if (id == NULL)
6855 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6858 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6859 c0f61fa4 2022-07-11 mark if (*new_view) {
6860 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6861 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6864 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6866 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6867 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6869 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6870 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6871 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6872 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6873 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6877 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6878 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6879 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6881 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6884 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6885 c0f61fa4 2022-07-11 mark s->selected_line;
6886 c0f61fa4 2022-07-11 mark if (*new_view)
6887 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6888 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6889 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6890 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6895 e78dc838 2020-12-04 stsp view->focussed = 0;
6896 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6897 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6898 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6899 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6900 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6901 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6904 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6907 e78dc838 2020-12-04 stsp view->focus_child = 1;
6909 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6914 83cc4199 2022-06-13 stsp case CTRL('d'):
6916 83cc4199 2022-06-13 stsp nscroll /= 2;
6917 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6918 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6919 ea025d1d 2020-02-22 naddy case CTRL('f'):
6922 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6923 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6924 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6925 640cd7ff 2022-06-22 mark view->count = 0;
6928 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6929 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6930 83cc4199 2022-06-13 stsp s->selected_line +=
6931 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6932 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6934 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6935 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6937 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6938 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6940 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6941 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6942 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6943 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6947 640cd7ff 2022-06-22 mark view->count = 0;
6950 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6953 917d79a7 2022-07-01 stsp static const struct got_error *
6954 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6956 917d79a7 2022-07-01 stsp const struct got_error *err;
6957 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6959 917d79a7 2022-07-01 stsp view->count = 0;
6960 917d79a7 2022-07-01 stsp s->done = 1;
6961 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6962 917d79a7 2022-07-01 stsp s->done = 0;
6964 917d79a7 2022-07-01 stsp return err;
6965 917d79a7 2022-07-01 stsp return run_blame(view);
6968 a70480e0 2018-06-23 stsp static const struct got_error *
6969 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6971 7483826e 2023-04-14 mark const struct got_error *error;
6972 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6973 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6974 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6975 0587e10c 2020-07-23 stsp char *link_target = NULL;
6976 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6977 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6978 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6980 2ca2f982 2023-04-14 mark struct tog_view *view = NULL;
6981 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6983 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6984 a70480e0 2018-06-23 stsp switch (ch) {
6986 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6989 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6990 69069811 2018-08-02 stsp if (repo_path == NULL)
6991 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6995 17020d27 2019-03-07 stsp usage_blame();
6996 a70480e0 2018-06-23 stsp /* NOTREACHED */
7000 a70480e0 2018-06-23 stsp argc -= optind;
7001 a70480e0 2018-06-23 stsp argv += optind;
7003 f135c941 2020-02-20 stsp if (argc != 1)
7004 a70480e0 2018-06-23 stsp usage_blame();
7006 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7007 0ae84acc 2022-06-15 tracey if (error != NULL)
7008 0ae84acc 2022-06-15 tracey goto done;
7010 69069811 2018-08-02 stsp if (repo_path == NULL) {
7011 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7012 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7013 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7014 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7015 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7016 c156c7a4 2020-12-18 stsp goto done;
7017 f135c941 2020-02-20 stsp if (worktree)
7018 eb41ed75 2019-02-05 stsp repo_path =
7019 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7021 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
7022 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7023 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7024 c156c7a4 2020-12-18 stsp goto done;
7028 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7029 c02c541e 2019-03-29 stsp if (error != NULL)
7030 8e94dd5b 2019-01-04 stsp goto done;
7032 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7033 f135c941 2020-02-20 stsp worktree);
7034 c02c541e 2019-03-29 stsp if (error)
7035 92205607 2019-01-04 stsp goto done;
7037 098596c5 2023-04-14 stsp init_curses();
7039 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7040 51a10b52 2020-12-26 stsp if (error)
7041 51a10b52 2020-12-26 stsp goto done;
7043 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7044 eb41ed75 2019-02-05 stsp if (error)
7045 69069811 2018-08-02 stsp goto done;
7047 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
7048 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
7049 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
7050 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7051 a70480e0 2018-06-23 stsp if (error != NULL)
7052 66b4983c 2018-06-23 stsp goto done;
7053 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7054 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7056 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7057 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7059 a19e88aa 2018-06-23 stsp if (error != NULL)
7060 e1cd8fed 2018-08-01 stsp goto done;
7062 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7063 a44927cc 2022-04-07 stsp if (error)
7064 a44927cc 2022-04-07 stsp goto done;
7066 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7067 a44927cc 2022-04-07 stsp commit, repo);
7068 7cbe629d 2018-08-04 stsp if (error)
7069 7cbe629d 2018-08-04 stsp goto done;
7071 e08111f2 2023-04-16 mark view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7072 e08111f2 2023-04-16 mark if (view == NULL) {
7073 e08111f2 2023-04-16 mark error = got_error_from_errno("view_open");
7074 e08111f2 2023-04-16 mark goto done;
7076 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7077 78756c87 2020-11-24 stsp commit_id, repo);
7078 e08111f2 2023-04-16 mark if (error != NULL) {
7079 e08111f2 2023-04-16 mark if (view->close == NULL)
7080 e08111f2 2023-04-16 mark close_blame_view(view);
7081 e08111f2 2023-04-16 mark view_close(view);
7082 0587e10c 2020-07-23 stsp goto done;
7084 12314ad4 2019-08-31 stsp if (worktree) {
7085 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7086 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7087 12314ad4 2019-08-31 stsp worktree = NULL;
7089 098596c5 2023-04-14 stsp error = view_loop(view);
7091 69069811 2018-08-02 stsp free(repo_path);
7092 f135c941 2020-02-20 stsp free(in_repo_path);
7093 0587e10c 2020-07-23 stsp free(link_target);
7094 69069811 2018-08-02 stsp free(cwd);
7095 a70480e0 2018-06-23 stsp free(commit_id);
7096 a44927cc 2022-04-07 stsp if (commit)
7097 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7098 eb41ed75 2019-02-05 stsp if (worktree)
7099 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7100 1d0f4054 2021-06-17 stsp if (repo) {
7101 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7102 1d0f4054 2021-06-17 stsp if (error == NULL)
7103 1d0f4054 2021-06-17 stsp error = close_err;
7105 0ae84acc 2022-06-15 tracey if (pack_fds) {
7106 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7107 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7108 0ae84acc 2022-06-15 tracey if (error == NULL)
7109 0ae84acc 2022-06-15 tracey error = pack_err;
7111 51a10b52 2020-12-26 stsp tog_free_refs();
7112 a70480e0 2018-06-23 stsp return error;
7115 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7116 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7118 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7119 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7120 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7121 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7122 0c6ad1bc 2022-09-09 mark char *index = NULL;
7123 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7124 d627976f 2023-02-01 mark int width, n, nentries, scrollx, i = 1;
7125 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7127 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7128 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7129 9b058f45 2022-06-30 mark --limit; /* border */
7131 f7d12f7e 2018-08-01 stsp werase(view->window);
7133 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7134 ffd1d5e5 2018-06-23 stsp return NULL;
7136 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7139 ffd1d5e5 2018-06-23 stsp return err;
7140 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7141 a3404814 2018-09-02 stsp wstandout(view->window);
7142 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7144 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7145 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7146 0c6ad1bc 2022-09-09 mark free(wline);
7147 0c6ad1bc 2022-09-09 mark wline = NULL;
7148 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
7149 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
7151 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7152 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7153 a3404814 2018-09-02 stsp wstandend(view->window);
7154 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
7155 0c6ad1bc 2022-09-09 mark return NULL;
7157 df68a56b 2022-08-12 mark i += s->selected;
7158 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
7159 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
7160 df68a56b 2022-08-12 mark if (s->tree != s->root)
7161 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
7163 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
7164 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
7165 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7166 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
7167 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7168 0c6ad1bc 2022-09-09 mark free(index);
7170 ce52c690 2018-06-23 stsp return err;
7171 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7172 2550e4c3 2018-07-13 stsp free(wline);
7173 2550e4c3 2018-07-13 stsp wline = NULL;
7174 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7175 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7176 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7177 ffd1d5e5 2018-06-23 stsp return NULL;
7178 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7179 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7180 a1eca9bb 2018-06-23 stsp return NULL;
7182 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7183 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7184 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7185 0cf4efb1 2018-09-29 stsp if (view->focussed)
7186 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7187 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7189 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7190 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7191 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7192 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7193 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7194 ffd1d5e5 2018-06-23 stsp return NULL;
7198 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7201 d627976f 2023-02-01 mark view->maxx = 0;
7202 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7203 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7204 848d6979 2019-08-12 stsp const char *modestr = "";
7205 56e0773d 2019-11-28 stsp mode_t mode;
7207 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7208 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7210 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7211 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7212 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7214 638f9024 2019-05-13 stsp return got_error_from_errno(
7215 230a42bd 2019-05-11 jcs "got_object_id_str");
7217 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7218 63c5ca5d 2019-08-24 stsp modestr = "$";
7219 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7222 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7223 d86d3b18 2020-12-01 naddy te, s->repo);
7224 0d6c6ee3 2020-05-20 stsp if (err) {
7225 0d6c6ee3 2020-05-20 stsp free(id_str);
7226 0d6c6ee3 2020-05-20 stsp return err;
7228 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
7229 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7230 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7232 848d6979 2019-08-12 stsp modestr = "@";
7234 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7235 848d6979 2019-08-12 stsp modestr = "/";
7236 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7237 848d6979 2019-08-12 stsp modestr = "*";
7238 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7239 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7240 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7241 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7242 1d13200f 2018-07-12 stsp free(id_str);
7243 0d6c6ee3 2020-05-20 stsp free(link_target);
7244 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7246 1d13200f 2018-07-12 stsp free(id_str);
7247 0d6c6ee3 2020-05-20 stsp free(link_target);
7249 d627976f 2023-02-01 mark /* use full line width to determine view->maxx */
7250 d627976f 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7251 ffd1d5e5 2018-06-23 stsp if (err) {
7252 ffd1d5e5 2018-06-23 stsp free(line);
7255 d627976f 2023-02-01 mark view->maxx = MAX(view->maxx, width);
7256 d627976f 2023-02-01 mark free(wline);
7257 d627976f 2023-02-01 mark wline = NULL;
7259 d627976f 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
7260 d627976f 2023-02-01 mark view->ncols, 0, 0);
7261 d627976f 2023-02-01 mark if (err) {
7262 d627976f 2023-02-01 mark free(line);
7265 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7266 0cf4efb1 2018-09-29 stsp if (view->focussed)
7267 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7268 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7270 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7272 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7273 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7274 d627976f 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
7276 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7277 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7278 b637f33f 2023-02-01 mark if (width < view->ncols)
7279 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7280 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7281 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7282 ffd1d5e5 2018-06-23 stsp free(line);
7283 2550e4c3 2018-07-13 stsp free(wline);
7284 2550e4c3 2018-07-13 stsp wline = NULL;
7286 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7287 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7288 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7292 ffd1d5e5 2018-06-23 stsp return err;
7295 ffd1d5e5 2018-06-23 stsp static void
7296 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7298 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7299 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7300 fa86c4bf 2020-11-29 stsp int i = 0;
7302 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7305 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7306 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7307 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7308 fa86c4bf 2020-11-29 stsp if (!isroot)
7309 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7312 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7313 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7317 9b058f45 2022-06-30 mark static const struct got_error *
7318 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
7320 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7321 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7322 ffd1d5e5 2018-06-23 stsp int n = 0;
7324 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7325 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7326 694d3271 2020-12-01 naddy s->first_displayed_entry);
7328 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7330 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7331 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7332 94b80cfa 2022-08-01 mark if (last) {
7333 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7334 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
7336 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7337 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7338 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7342 9b058f45 2022-06-30 mark return NULL;
7345 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7346 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7347 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7349 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7350 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7351 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7353 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7354 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7355 56e0773d 2019-11-28 stsp + 1 /* slash */;
7357 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7359 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7360 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7361 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7363 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7364 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7365 d9765a41 2018-06-23 stsp while (pt) {
7366 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7367 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7368 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7369 cb2ebc8a 2018-06-23 stsp goto done;
7371 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7372 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7373 cb2ebc8a 2018-06-23 stsp goto done;
7375 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7378 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7379 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7380 ce52c690 2018-06-23 stsp goto done;
7384 ce52c690 2018-06-23 stsp if (err) {
7385 ce52c690 2018-06-23 stsp free(*path);
7386 ce52c690 2018-06-23 stsp *path = NULL;
7388 ce52c690 2018-06-23 stsp return err;
7391 ce52c690 2018-06-23 stsp static const struct got_error *
7392 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7393 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7394 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7396 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7397 ce52c690 2018-06-23 stsp char *path;
7398 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7400 a0de39f3 2019-08-09 stsp *new_view = NULL;
7402 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7404 ce52c690 2018-06-23 stsp return err;
7406 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7407 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7408 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7409 83ce39e3 2019-08-12 stsp goto done;
7412 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7413 e5a0f69f 2018-08-18 stsp if (err) {
7414 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7415 fc06ba56 2019-08-22 stsp err = NULL;
7416 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7418 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7420 83ce39e3 2019-08-12 stsp free(path);
7421 69efd4c4 2018-07-18 stsp return err;
7424 69efd4c4 2018-07-18 stsp static const struct got_error *
7425 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7426 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7428 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7429 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7430 69efd4c4 2018-07-18 stsp char *path;
7432 a0de39f3 2019-08-09 stsp *new_view = NULL;
7434 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7435 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7436 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7438 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7440 69efd4c4 2018-07-18 stsp return err;
7442 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7445 e5a0f69f 2018-08-18 stsp view_close(log_view);
7447 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7448 cb2ebc8a 2018-06-23 stsp free(path);
7449 cb2ebc8a 2018-06-23 stsp return err;
7452 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7453 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7454 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7456 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7457 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7458 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7459 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7461 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7462 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7464 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7465 2ca2f982 2023-04-14 mark if (s->commit_id == NULL) {
7466 2ca2f982 2023-04-14 mark err = got_error_from_errno("got_object_id_dup");
7467 2ca2f982 2023-04-14 mark goto done;
7470 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7472 bc573f3b 2021-07-10 stsp goto done;
7475 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7476 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7477 bc573f3b 2021-07-10 stsp * closed on demand.
7479 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7480 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7482 bc573f3b 2021-07-10 stsp goto done;
7483 bc573f3b 2021-07-10 stsp s->tree = s->root;
7485 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7486 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7487 ffd1d5e5 2018-06-23 stsp goto done;
7489 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7490 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7491 ffd1d5e5 2018-06-23 stsp goto done;
7494 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7495 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7496 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7497 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7498 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7499 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7500 9cd7cbd1 2020-12-07 stsp goto done;
7503 fb2756b9 2018-08-04 stsp s->repo = repo;
7505 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7506 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7507 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7508 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7510 c0b01bdb 2019-11-08 stsp goto done;
7511 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7512 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7514 c0b01bdb 2019-11-08 stsp goto done;
7515 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7516 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7517 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7519 c0b01bdb 2019-11-08 stsp goto done;
7521 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7522 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7523 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7525 11b20872 2019-11-08 stsp goto done;
7527 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7528 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7530 c0b01bdb 2019-11-08 stsp goto done;
7533 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7534 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7535 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7536 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7537 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7539 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7540 bc573f3b 2021-07-10 stsp if (commit)
7541 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7542 2ca2f982 2023-04-14 mark if (err) {
7543 2ca2f982 2023-04-14 mark if (view->close == NULL)
7544 2ca2f982 2023-04-14 mark close_tree_view(view);
7545 2ca2f982 2023-04-14 mark view_close(view);
7547 ad80ab7b 2018-08-04 stsp return err;
7550 e5a0f69f 2018-08-18 stsp static const struct got_error *
7551 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7553 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7555 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7556 fb2756b9 2018-08-04 stsp free(s->tree_label);
7557 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7558 6484ec90 2018-09-29 stsp free(s->commit_id);
7559 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7560 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7561 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7562 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7563 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7564 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7565 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7566 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7567 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7568 ad80ab7b 2018-08-04 stsp free(parent);
7571 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7572 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7573 bc573f3b 2021-07-10 stsp if (s->root)
7574 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7575 7c32bd05 2019-06-22 stsp return NULL;
7578 7c32bd05 2019-06-22 stsp static const struct got_error *
7579 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7581 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7583 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7584 7c32bd05 2019-06-22 stsp return NULL;
7587 7c32bd05 2019-06-22 stsp static int
7588 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7590 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7592 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7596 7c32bd05 2019-06-22 stsp static const struct got_error *
7597 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7599 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7600 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7602 7c32bd05 2019-06-22 stsp if (!view->searching) {
7603 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7604 7c32bd05 2019-06-22 stsp return NULL;
7607 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7608 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7609 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7610 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7611 56e0773d 2019-11-28 stsp s->selected_entry);
7613 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7615 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7616 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7618 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7619 56e0773d 2019-11-28 stsp s->selected_entry);
7622 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7623 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7624 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7625 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7627 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7630 7c32bd05 2019-06-22 stsp while (1) {
7631 56e0773d 2019-11-28 stsp if (te == NULL) {
7632 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7633 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7634 ac66afb8 2019-06-24 stsp return NULL;
7636 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7637 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7639 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7642 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7643 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7644 56e0773d 2019-11-28 stsp s->matched_entry = te;
7648 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7649 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7651 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7654 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7655 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7656 7c32bd05 2019-06-22 stsp s->selected = 0;
7659 e5a0f69f 2018-08-18 stsp return NULL;
7662 ad80ab7b 2018-08-04 stsp static const struct got_error *
7663 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7665 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7666 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7667 e5a0f69f 2018-08-18 stsp char *parent_path;
7669 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7671 e5a0f69f 2018-08-18 stsp return err;
7673 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7674 e5a0f69f 2018-08-18 stsp free(parent_path);
7676 9b058f45 2022-06-30 mark view_border(view);
7677 e5a0f69f 2018-08-18 stsp return err;
7680 94b80cfa 2022-08-01 mark static const struct got_error *
7681 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7683 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7684 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7685 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7686 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7687 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7688 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7690 94b80cfa 2022-08-01 mark g = view->gline;
7691 94b80cfa 2022-08-01 mark view->gline = 0;
7693 94b80cfa 2022-08-01 mark if (g == 0)
7695 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7696 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7698 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7699 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7700 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7702 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7703 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7704 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7706 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7707 94b80cfa 2022-08-01 mark last += off;
7709 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7710 94b80cfa 2022-08-01 mark s->selected = g - first;
7711 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7714 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7715 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7719 94b80cfa 2022-08-01 mark if (i < g) {
7720 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7722 94b80cfa 2022-08-01 mark return err;
7723 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7724 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7725 94b80cfa 2022-08-01 mark first + s->selected < g &&
7726 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7727 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7728 94b80cfa 2022-08-01 mark first += off;
7729 94b80cfa 2022-08-01 mark s->selected = g - first;
7731 94b80cfa 2022-08-01 mark } else if (i > g)
7732 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7734 94b80cfa 2022-08-01 mark if (g < nlines &&
7735 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7736 94b80cfa 2022-08-01 mark s->selected = g - 1;
7738 94b80cfa 2022-08-01 mark return NULL;
7741 e5a0f69f 2018-08-18 stsp static const struct got_error *
7742 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7744 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7745 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7746 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7747 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7749 94b80cfa 2022-08-01 mark if (view->gline)
7750 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7752 e5a0f69f 2018-08-18 stsp switch (ch) {
7755 d627976f 2023-02-01 mark case KEY_RIGHT:
7757 d627976f 2023-02-01 mark case KEY_LEFT:
7759 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
7762 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7763 640cd7ff 2022-06-22 mark view->count = 0;
7766 640cd7ff 2022-06-22 mark view->count = 0;
7767 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7769 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7772 640cd7ff 2022-06-22 mark view->count = 0;
7773 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7775 e4526bf5 2021-09-03 naddy case 'g':
7777 e4526bf5 2021-09-03 naddy case KEY_HOME:
7778 e4526bf5 2021-09-03 naddy s->selected = 0;
7779 640cd7ff 2022-06-22 mark view->count = 0;
7780 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7781 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7782 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7784 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7786 e4526bf5 2021-09-03 naddy case 'G':
7788 9b058f45 2022-06-30 mark case KEY_END: {
7789 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7791 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7792 9b058f45 2022-06-30 mark --eos; /* border */
7793 e4526bf5 2021-09-03 naddy s->selected = 0;
7794 640cd7ff 2022-06-22 mark view->count = 0;
7795 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7796 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7797 e4526bf5 2021-09-03 naddy if (te == NULL) {
7798 ad055527 2022-07-16 mark if (s->tree != s->root) {
7799 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7804 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7805 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7807 e4526bf5 2021-09-03 naddy if (n > 0)
7808 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7812 1e37a5c2 2019-05-12 jcs case KEY_UP:
7813 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7814 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7815 1e37a5c2 2019-05-12 jcs s->selected--;
7818 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7819 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7820 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7821 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7822 640cd7ff 2022-06-22 mark view->count = 0;
7824 83cc4199 2022-06-13 stsp case CTRL('u'):
7826 83cc4199 2022-06-13 stsp nscroll /= 2;
7827 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7828 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7829 ea025d1d 2020-02-22 naddy case CTRL('b'):
7831 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7832 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7833 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7834 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7836 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7837 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7839 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7840 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7841 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7842 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7843 640cd7ff 2022-06-22 mark view->count = 0;
7846 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7847 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7848 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7849 1e37a5c2 2019-05-12 jcs s->selected++;
7852 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7853 640cd7ff 2022-06-22 mark == NULL) {
7854 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7855 640cd7ff 2022-06-22 mark view->count = 0;
7858 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7860 83cc4199 2022-06-13 stsp case CTRL('d'):
7862 83cc4199 2022-06-13 stsp nscroll /= 2;
7863 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7864 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7865 ea025d1d 2020-02-22 naddy case CTRL('f'):
7867 48bb96f0 2022-06-20 naddy case ' ':
7868 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7870 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7871 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7872 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7873 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7875 640cd7ff 2022-06-22 mark view->count = 0;
7878 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7880 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7882 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7883 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7884 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7885 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7886 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7887 640cd7ff 2022-06-22 mark view->count = 0;
7890 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7891 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7893 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7894 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7895 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7896 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7897 1e37a5c2 2019-05-12 jcs s->selected_entry =
7898 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7899 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7900 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7901 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7905 1e37a5c2 2019-05-12 jcs free(parent);
7906 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7907 56e0773d 2019-11-28 stsp s->selected_entry))) {
7908 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7909 640cd7ff 2022-06-22 mark view->count = 0;
7910 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7911 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7914 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7915 941e9f74 2019-05-21 stsp if (err) {
7916 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7919 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7920 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7922 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7923 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7924 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7925 640cd7ff 2022-06-22 mark view->count = 0;
7928 640cd7ff 2022-06-22 mark view->count = 0;
7932 ffd1d5e5 2018-06-23 stsp return err;
7935 ffd1d5e5 2018-06-23 stsp __dead static void
7936 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7939 87411fa9 2022-06-16 stsp fprintf(stderr,
7940 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7941 ffd1d5e5 2018-06-23 stsp getprogname());
7945 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7946 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7948 7483826e 2023-04-14 mark const struct got_error *error;
7949 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7950 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7951 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7952 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7953 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7954 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7955 4e97c21c 2020-12-06 stsp char *label = NULL;
7956 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7957 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7959 5221c383 2018-08-01 stsp struct tog_view *view;
7960 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7962 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7963 ffd1d5e5 2018-06-23 stsp switch (ch) {
7965 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7968 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7969 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7970 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7974 e99e2d15 2020-11-24 naddy usage_tree();
7975 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7979 ffd1d5e5 2018-06-23 stsp argc -= optind;
7980 ffd1d5e5 2018-06-23 stsp argv += optind;
7982 55cccc34 2020-02-20 stsp if (argc > 1)
7983 e99e2d15 2020-11-24 naddy usage_tree();
7985 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7986 0ae84acc 2022-06-15 tracey if (error != NULL)
7987 0ae84acc 2022-06-15 tracey goto done;
7989 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7990 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7991 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7992 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7993 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7994 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7995 c156c7a4 2020-12-18 stsp goto done;
7996 55cccc34 2020-02-20 stsp if (worktree)
7997 52185f70 2019-02-05 stsp repo_path =
7998 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
8000 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
8001 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8002 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8003 c156c7a4 2020-12-18 stsp goto done;
8007 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8008 c02c541e 2019-03-29 stsp if (error != NULL)
8009 52185f70 2019-02-05 stsp goto done;
8011 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8012 55cccc34 2020-02-20 stsp repo, worktree);
8013 55cccc34 2020-02-20 stsp if (error)
8014 55cccc34 2020-02-20 stsp goto done;
8016 098596c5 2023-04-14 stsp init_curses();
8018 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8019 c02c541e 2019-03-29 stsp if (error)
8020 52185f70 2019-02-05 stsp goto done;
8022 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8023 51a10b52 2020-12-26 stsp if (error)
8024 51a10b52 2020-12-26 stsp goto done;
8026 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
8027 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
8028 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
8029 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8030 4e97c21c 2020-12-06 stsp if (error)
8031 4e97c21c 2020-12-06 stsp goto done;
8032 4e97c21c 2020-12-06 stsp head_ref_name = label;
8034 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
8035 4e97c21c 2020-12-06 stsp if (error == NULL)
8036 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
8037 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
8038 4e97c21c 2020-12-06 stsp goto done;
8039 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
8040 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8041 4e97c21c 2020-12-06 stsp if (error)
8042 4e97c21c 2020-12-06 stsp goto done;
8045 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8046 a44927cc 2022-04-07 stsp if (error)
8047 a44927cc 2022-04-07 stsp goto done;
8049 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8050 5221c383 2018-08-01 stsp if (view == NULL) {
8051 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
8052 5221c383 2018-08-01 stsp goto done;
8054 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8055 ad80ab7b 2018-08-04 stsp if (error)
8056 ad80ab7b 2018-08-04 stsp goto done;
8057 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8058 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
8059 d91faf3b 2020-12-01 naddy in_repo_path);
8060 55cccc34 2020-02-20 stsp if (error)
8061 55cccc34 2020-02-20 stsp goto done;
8064 55cccc34 2020-02-20 stsp if (worktree) {
8065 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8066 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8067 55cccc34 2020-02-20 stsp worktree = NULL;
8069 098596c5 2023-04-14 stsp error = view_loop(view);
8071 52185f70 2019-02-05 stsp free(repo_path);
8072 e4a0e26d 2020-02-20 stsp free(cwd);
8073 ffd1d5e5 2018-06-23 stsp free(commit_id);
8074 4e97c21c 2020-12-06 stsp free(label);
8076 486cd271 2020-12-06 stsp got_ref_close(ref);
8077 1d0f4054 2021-06-17 stsp if (repo) {
8078 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8079 1d0f4054 2021-06-17 stsp if (error == NULL)
8080 1d0f4054 2021-06-17 stsp error = close_err;
8082 0ae84acc 2022-06-15 tracey if (pack_fds) {
8083 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8084 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8085 0ae84acc 2022-06-15 tracey if (error == NULL)
8086 0ae84acc 2022-06-15 tracey error = pack_err;
8088 51a10b52 2020-12-26 stsp tog_free_refs();
8089 ffd1d5e5 2018-06-23 stsp return error;
8092 6458efa5 2020-11-24 stsp static const struct got_error *
8093 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8095 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8096 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8098 6458efa5 2020-11-24 stsp s->nrefs = 0;
8099 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8100 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
8101 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
8102 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
8103 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
8106 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8107 6458efa5 2020-11-24 stsp if (re == NULL)
8108 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8110 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8111 8924d611 2020-12-26 stsp if (re->ref == NULL)
8112 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8113 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8114 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8117 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8118 6458efa5 2020-11-24 stsp return NULL;
8122 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8124 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8126 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8127 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8128 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8129 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8134 6458efa5 2020-11-24 stsp static const struct got_error *
8135 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8137 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8138 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8140 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8141 6458efa5 2020-11-24 stsp s->repo = repo;
8143 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8144 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8146 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8148 2ca2f982 2023-04-14 mark goto done;
8150 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8151 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8152 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8153 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8155 6458efa5 2020-11-24 stsp goto done;
8157 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8158 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8159 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8161 6458efa5 2020-11-24 stsp goto done;
8163 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8164 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8165 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8167 cc488aa7 2022-01-23 stsp goto done;
8169 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
8170 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
8171 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
8173 6458efa5 2020-11-24 stsp goto done;
8176 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8177 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8178 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8179 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8180 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8182 2ca2f982 2023-04-14 mark if (err) {
8183 2ca2f982 2023-04-14 mark if (view->close == NULL)
8184 2ca2f982 2023-04-14 mark close_ref_view(view);
8185 2ca2f982 2023-04-14 mark view_close(view);
8187 6458efa5 2020-11-24 stsp return err;
8190 6458efa5 2020-11-24 stsp static const struct got_error *
8191 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8193 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8195 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8196 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8198 6458efa5 2020-11-24 stsp return NULL;
8201 6458efa5 2020-11-24 stsp static const struct got_error *
8202 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8203 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8205 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8206 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8207 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8208 6458efa5 2020-11-24 stsp int obj_type;
8210 c42c9805 2020-11-24 stsp *commit_id = NULL;
8212 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8214 6458efa5 2020-11-24 stsp return err;
8216 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8218 6458efa5 2020-11-24 stsp goto done;
8220 6458efa5 2020-11-24 stsp switch (obj_type) {
8221 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8222 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8224 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8225 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8227 6458efa5 2020-11-24 stsp goto done;
8228 c42c9805 2020-11-24 stsp free(obj_id);
8229 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8230 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8232 6458efa5 2020-11-24 stsp goto done;
8233 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8234 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8235 c42c9805 2020-11-24 stsp goto done;
8237 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8238 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8239 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8240 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8241 c42c9805 2020-11-24 stsp goto done;
8245 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8251 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8252 c42c9805 2020-11-24 stsp if (err) {
8253 c42c9805 2020-11-24 stsp free(*commit_id);
8254 c42c9805 2020-11-24 stsp *commit_id = NULL;
8256 c42c9805 2020-11-24 stsp return err;
8259 c42c9805 2020-11-24 stsp static const struct got_error *
8260 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8261 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8263 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8264 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8265 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8267 c42c9805 2020-11-24 stsp *new_view = NULL;
8269 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8270 c42c9805 2020-11-24 stsp if (err) {
8271 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8272 c42c9805 2020-11-24 stsp return err;
8274 c42c9805 2020-11-24 stsp return NULL;
8277 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8278 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8279 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8280 6458efa5 2020-11-24 stsp goto done;
8283 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8284 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
8287 6458efa5 2020-11-24 stsp view_close(log_view);
8289 6458efa5 2020-11-24 stsp *new_view = log_view;
8290 c42c9805 2020-11-24 stsp free(commit_id);
8291 6458efa5 2020-11-24 stsp return err;
8294 ce5b7c56 2019-07-09 stsp static void
8295 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8297 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8298 34ba6917 2020-11-30 stsp int i = 0;
8300 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8303 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8304 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8305 34ba6917 2020-11-30 stsp if (re == NULL)
8307 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8308 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8312 9b058f45 2022-06-30 mark static const struct got_error *
8313 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
8315 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8316 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8317 6458efa5 2020-11-24 stsp int n = 0;
8319 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8320 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8322 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8324 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8325 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
8326 94b80cfa 2022-08-01 mark if (last) {
8327 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
8328 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
8330 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8331 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8332 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8336 9b058f45 2022-06-30 mark return NULL;
8339 6458efa5 2020-11-24 stsp static const struct got_error *
8340 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8342 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8344 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8345 6458efa5 2020-11-24 stsp return NULL;
8348 6458efa5 2020-11-24 stsp static int
8349 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8351 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8353 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8357 6458efa5 2020-11-24 stsp static const struct got_error *
8358 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8360 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8361 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8363 6458efa5 2020-11-24 stsp if (!view->searching) {
8364 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8365 6458efa5 2020-11-24 stsp return NULL;
8368 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8369 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8370 6458efa5 2020-11-24 stsp if (s->selected_entry)
8371 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8373 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8374 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8376 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8377 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8379 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8380 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8383 487cd7d2 2021-12-17 stsp if (s->selected_entry)
8384 487cd7d2 2021-12-17 stsp re = s->selected_entry;
8385 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
8386 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8388 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8391 6458efa5 2020-11-24 stsp while (1) {
8392 6458efa5 2020-11-24 stsp if (re == NULL) {
8393 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8394 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8395 6458efa5 2020-11-24 stsp return NULL;
8397 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8398 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8400 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8403 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8404 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8405 6458efa5 2020-11-24 stsp s->matched_entry = re;
8409 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8410 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8412 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8415 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8416 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8417 6458efa5 2020-11-24 stsp s->selected = 0;
8420 6458efa5 2020-11-24 stsp return NULL;
8423 6458efa5 2020-11-24 stsp static const struct got_error *
8424 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8426 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8427 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8428 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8429 6458efa5 2020-11-24 stsp char *line = NULL;
8430 6458efa5 2020-11-24 stsp wchar_t *wline;
8431 6458efa5 2020-11-24 stsp struct tog_color *tc;
8432 0ed6bc90 2023-02-01 mark int width, n, scrollx;
8433 6458efa5 2020-11-24 stsp int limit = view->nlines;
8435 6458efa5 2020-11-24 stsp werase(view->window);
8437 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8438 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8439 9b058f45 2022-06-30 mark --limit; /* border */
8441 6458efa5 2020-11-24 stsp if (limit == 0)
8442 6458efa5 2020-11-24 stsp return NULL;
8444 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8446 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8447 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8448 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8450 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8451 6458efa5 2020-11-24 stsp if (err) {
8452 6458efa5 2020-11-24 stsp free(line);
8453 6458efa5 2020-11-24 stsp return err;
8455 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8456 6458efa5 2020-11-24 stsp wstandout(view->window);
8457 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8458 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8459 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8460 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8461 6458efa5 2020-11-24 stsp wstandend(view->window);
8462 6458efa5 2020-11-24 stsp free(wline);
8463 6458efa5 2020-11-24 stsp wline = NULL;
8464 6458efa5 2020-11-24 stsp free(line);
8465 6458efa5 2020-11-24 stsp line = NULL;
8466 6458efa5 2020-11-24 stsp if (--limit <= 0)
8467 6458efa5 2020-11-24 stsp return NULL;
8470 0ed6bc90 2023-02-01 mark view->maxx = 0;
8471 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8472 6458efa5 2020-11-24 stsp char *line = NULL;
8473 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8475 b4996bee 2022-06-16 stsp if (s->show_date) {
8476 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8477 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8478 b4996bee 2022-06-16 stsp struct got_object_id *id;
8479 b4996bee 2022-06-16 stsp struct tm tm;
8482 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8484 b4996bee 2022-06-16 stsp return err;
8485 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8486 b4996bee 2022-06-16 stsp if (err) {
8487 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8489 b4996bee 2022-06-16 stsp return err;
8491 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8493 b4996bee 2022-06-16 stsp if (err) {
8495 b4996bee 2022-06-16 stsp return err;
8497 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8498 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8500 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8501 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8504 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8505 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8506 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8507 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8509 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8510 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8511 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8512 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8513 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8514 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8515 6458efa5 2020-11-24 stsp struct got_object_id *id;
8516 6458efa5 2020-11-24 stsp char *id_str;
8517 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8519 6458efa5 2020-11-24 stsp return err;
8520 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8521 6458efa5 2020-11-24 stsp if (err) {
8523 6458efa5 2020-11-24 stsp return err;
8525 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8526 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8527 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8529 6458efa5 2020-11-24 stsp free(id_str);
8530 6458efa5 2020-11-24 stsp return err;
8533 6458efa5 2020-11-24 stsp free(id_str);
8534 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8535 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8536 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8538 0ed6bc90 2023-02-01 mark /* use full line width to determine view->maxx */
8539 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8540 6458efa5 2020-11-24 stsp if (err) {
8541 6458efa5 2020-11-24 stsp free(line);
8542 6458efa5 2020-11-24 stsp return err;
8544 0ed6bc90 2023-02-01 mark view->maxx = MAX(view->maxx, width);
8545 0ed6bc90 2023-02-01 mark free(wline);
8546 0ed6bc90 2023-02-01 mark wline = NULL;
8548 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
8549 0ed6bc90 2023-02-01 mark view->ncols, 0, 0);
8550 0ed6bc90 2023-02-01 mark if (err) {
8551 0ed6bc90 2023-02-01 mark free(line);
8552 0ed6bc90 2023-02-01 mark return err;
8554 6458efa5 2020-11-24 stsp if (n == s->selected) {
8555 6458efa5 2020-11-24 stsp if (view->focussed)
8556 6458efa5 2020-11-24 stsp wstandout(view->window);
8557 6458efa5 2020-11-24 stsp s->selected_entry = re;
8559 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8561 6458efa5 2020-11-24 stsp wattr_on(view->window,
8562 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8563 0ed6bc90 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
8565 6458efa5 2020-11-24 stsp wattr_off(view->window,
8566 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8567 b637f33f 2023-02-01 mark if (width < view->ncols)
8568 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8569 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8570 6458efa5 2020-11-24 stsp wstandend(view->window);
8571 6458efa5 2020-11-24 stsp free(line);
8572 6458efa5 2020-11-24 stsp free(wline);
8573 6458efa5 2020-11-24 stsp wline = NULL;
8575 6458efa5 2020-11-24 stsp s->ndisplayed++;
8576 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8579 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8582 9b058f45 2022-06-30 mark view_border(view);
8583 6458efa5 2020-11-24 stsp return err;
8586 6458efa5 2020-11-24 stsp static const struct got_error *
8587 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8588 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8590 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8591 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8592 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8594 c42c9805 2020-11-24 stsp *new_view = NULL;
8596 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8597 c42c9805 2020-11-24 stsp if (err) {
8598 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8599 c42c9805 2020-11-24 stsp return err;
8601 c42c9805 2020-11-24 stsp return NULL;
8605 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8606 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8607 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8608 c42c9805 2020-11-24 stsp goto done;
8611 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8612 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8614 c42c9805 2020-11-24 stsp goto done;
8616 c42c9805 2020-11-24 stsp *new_view = tree_view;
8618 c42c9805 2020-11-24 stsp free(commit_id);
8619 c42c9805 2020-11-24 stsp return err;
8622 94b80cfa 2022-08-01 mark static const struct got_error *
8623 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8625 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8626 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8627 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8629 94b80cfa 2022-08-01 mark g = view->gline;
8630 94b80cfa 2022-08-01 mark view->gline = 0;
8632 94b80cfa 2022-08-01 mark if (g == 0)
8634 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8635 94b80cfa 2022-08-01 mark g = s->nrefs;
8637 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8638 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8639 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8640 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8641 94b80cfa 2022-08-01 mark return NULL;
8644 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8645 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8647 94b80cfa 2022-08-01 mark return err;
8648 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8649 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8650 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8651 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8652 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8653 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8655 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8656 94b80cfa 2022-08-01 mark s->selected = g - 1;
8658 94b80cfa 2022-08-01 mark return NULL;
8662 c42c9805 2020-11-24 stsp static const struct got_error *
8663 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8665 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8666 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8667 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8668 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8670 94b80cfa 2022-08-01 mark if (view->gline)
8671 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8673 6458efa5 2020-11-24 stsp switch (ch) {
8676 0ed6bc90 2023-02-01 mark case KEY_RIGHT:
8678 0ed6bc90 2023-02-01 mark case KEY_LEFT:
8680 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8683 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8684 640cd7ff 2022-06-22 mark view->count = 0;
8687 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8688 640cd7ff 2022-06-22 mark view->count = 0;
8691 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8692 3f6c6614 2023-01-23 mark view->action = s->sort_by_date ? "sort by date" : "sort by name";
8693 640cd7ff 2022-06-22 mark view->count = 0;
8694 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8695 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8696 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8699 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8700 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8701 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8704 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8705 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8707 6458efa5 2020-11-24 stsp case KEY_ENTER:
8708 6458efa5 2020-11-24 stsp case '\r':
8709 640cd7ff 2022-06-22 mark view->count = 0;
8710 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8712 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8715 640cd7ff 2022-06-22 mark view->count = 0;
8716 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8718 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8720 e4526bf5 2021-09-03 naddy case 'g':
8722 e4526bf5 2021-09-03 naddy case KEY_HOME:
8723 e4526bf5 2021-09-03 naddy s->selected = 0;
8724 640cd7ff 2022-06-22 mark view->count = 0;
8725 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8727 e4526bf5 2021-09-03 naddy case 'G':
8729 9b058f45 2022-06-30 mark case KEY_END: {
8730 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8732 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8733 9b058f45 2022-06-30 mark --eos; /* border */
8734 e4526bf5 2021-09-03 naddy s->selected = 0;
8735 640cd7ff 2022-06-22 mark view->count = 0;
8736 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8737 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8738 e4526bf5 2021-09-03 naddy if (re == NULL)
8740 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8741 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8743 e4526bf5 2021-09-03 naddy if (n > 0)
8744 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8748 6458efa5 2020-11-24 stsp case KEY_UP:
8749 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8750 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8751 6458efa5 2020-11-24 stsp s->selected--;
8754 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8755 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8756 640cd7ff 2022-06-22 mark view->count = 0;
8758 83cc4199 2022-06-13 stsp case CTRL('u'):
8760 83cc4199 2022-06-13 stsp nscroll /= 2;
8761 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8762 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8763 6458efa5 2020-11-24 stsp case CTRL('b'):
8765 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8766 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8767 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8768 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8769 640cd7ff 2022-06-22 mark view->count = 0;
8772 6458efa5 2020-11-24 stsp case KEY_DOWN:
8773 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8774 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8775 6458efa5 2020-11-24 stsp s->selected++;
8778 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8779 6458efa5 2020-11-24 stsp /* can't scroll any further */
8780 640cd7ff 2022-06-22 mark view->count = 0;
8783 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8785 83cc4199 2022-06-13 stsp case CTRL('d'):
8787 83cc4199 2022-06-13 stsp nscroll /= 2;
8788 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8789 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8790 6458efa5 2020-11-24 stsp case CTRL('f'):
8792 48bb96f0 2022-06-20 naddy case ' ':
8793 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8794 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8795 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8796 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8797 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8798 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8799 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8800 640cd7ff 2022-06-22 mark view->count = 0;
8803 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8805 6458efa5 2020-11-24 stsp case CTRL('l'):
8806 640cd7ff 2022-06-22 mark view->count = 0;
8807 8924d611 2020-12-26 stsp tog_free_refs();
8808 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8811 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8812 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8814 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8815 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8816 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8819 640cd7ff 2022-06-22 mark view->count = 0;
8823 6458efa5 2020-11-24 stsp return err;
8826 6458efa5 2020-11-24 stsp __dead static void
8827 6458efa5 2020-11-24 stsp usage_ref(void)
8830 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8831 6458efa5 2020-11-24 stsp getprogname());
8835 6458efa5 2020-11-24 stsp static const struct got_error *
8836 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8838 7483826e 2023-04-14 mark const struct got_error *error;
8839 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8840 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8841 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8843 6458efa5 2020-11-24 stsp struct tog_view *view;
8844 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8846 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8847 6458efa5 2020-11-24 stsp switch (ch) {
8849 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8850 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8851 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8855 e99e2d15 2020-11-24 naddy usage_ref();
8856 6458efa5 2020-11-24 stsp /* NOTREACHED */
8860 6458efa5 2020-11-24 stsp argc -= optind;
8861 6458efa5 2020-11-24 stsp argv += optind;
8863 6458efa5 2020-11-24 stsp if (argc > 1)
8864 e99e2d15 2020-11-24 naddy usage_ref();
8866 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8867 0ae84acc 2022-06-15 tracey if (error != NULL)
8868 0ae84acc 2022-06-15 tracey goto done;
8870 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8871 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8872 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8873 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8874 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8875 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8876 c156c7a4 2020-12-18 stsp goto done;
8877 6458efa5 2020-11-24 stsp if (worktree)
8878 6458efa5 2020-11-24 stsp repo_path =
8879 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8881 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8882 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8883 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8884 c156c7a4 2020-12-18 stsp goto done;
8888 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8889 6458efa5 2020-11-24 stsp if (error != NULL)
8890 6458efa5 2020-11-24 stsp goto done;
8892 098596c5 2023-04-14 stsp init_curses();
8894 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8895 51a10b52 2020-12-26 stsp if (error)
8896 51a10b52 2020-12-26 stsp goto done;
8898 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8899 6458efa5 2020-11-24 stsp if (error)
8900 6458efa5 2020-11-24 stsp goto done;
8902 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8903 6458efa5 2020-11-24 stsp if (view == NULL) {
8904 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8905 6458efa5 2020-11-24 stsp goto done;
8908 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8909 6458efa5 2020-11-24 stsp if (error)
8910 6458efa5 2020-11-24 stsp goto done;
8912 6458efa5 2020-11-24 stsp if (worktree) {
8913 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8914 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8915 6458efa5 2020-11-24 stsp worktree = NULL;
8917 098596c5 2023-04-14 stsp error = view_loop(view);
8919 6458efa5 2020-11-24 stsp free(repo_path);
8920 6458efa5 2020-11-24 stsp free(cwd);
8921 1d0f4054 2021-06-17 stsp if (repo) {
8922 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8923 1d0f4054 2021-06-17 stsp if (close_err)
8924 1d0f4054 2021-06-17 stsp error = close_err;
8926 0ae84acc 2022-06-15 tracey if (pack_fds) {
8927 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8928 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8929 0ae84acc 2022-06-15 tracey if (error == NULL)
8930 0ae84acc 2022-06-15 tracey error = pack_err;
8932 51a10b52 2020-12-26 stsp tog_free_refs();
8933 6458efa5 2020-11-24 stsp return error;
8936 ec2a9698 2022-09-15 mark static const struct got_error*
8937 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8938 ec2a9698 2022-09-15 mark const char *str)
8940 ec2a9698 2022-09-15 mark size_t len;
8942 ec2a9698 2022-09-15 mark if (win == NULL)
8943 ec2a9698 2022-09-15 mark win = stdscr;
8945 ec2a9698 2022-09-15 mark len = strlen(str);
8946 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8948 ec2a9698 2022-09-15 mark if (focus)
8949 ec2a9698 2022-09-15 mark wstandout(win);
8950 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8951 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8952 ec2a9698 2022-09-15 mark if (focus)
8953 ec2a9698 2022-09-15 mark wstandend(win);
8955 ec2a9698 2022-09-15 mark return NULL;
8958 136e2bd4 2022-07-23 mark static const struct got_error *
8959 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8963 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8964 ec2a9698 2022-09-15 mark if (p == NULL) {
8965 ec2a9698 2022-09-15 mark free(*line_offsets);
8966 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8967 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8970 ec2a9698 2022-09-15 mark *line_offsets = p;
8971 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8972 ec2a9698 2022-09-15 mark ++(*nlines);
8973 ec2a9698 2022-09-15 mark return NULL;
8976 ec2a9698 2022-09-15 mark static const struct got_error *
8977 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8981 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8982 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8983 ec2a9698 2022-09-15 mark size_t len = 1;
8985 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8988 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8989 ec2a9698 2022-09-15 mark if (t0 == NULL)
8990 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8992 ec2a9698 2022-09-15 mark len += strlen(t);
8993 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8994 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8996 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8999 ec2a9698 2022-09-15 mark return NULL;
9003 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
9004 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
9005 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
9007 ec2a9698 2022-09-15 mark static const struct got_error *
9008 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9010 ec2a9698 2022-09-15 mark int n, len = width;
9012 ec2a9698 2022-09-15 mark if (km->keys) {
9013 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
9014 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9015 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
9017 ec2a9698 2022-09-15 mark char *t0, *t, *k;
9018 ec2a9698 2022-09-15 mark int cs, s, first = 1;
9020 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
9022 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
9023 ec2a9698 2022-09-15 mark if (t0 == NULL)
9024 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
9026 ec2a9698 2022-09-15 mark len = strlen(km->keys);
9027 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
9028 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
9029 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9030 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
9031 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9032 ec2a9698 2022-09-15 mark if (n < 0) {
9034 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
9036 ec2a9698 2022-09-15 mark first = 0;
9037 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
9038 ec2a9698 2022-09-15 mark *off += n;
9042 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9043 ec2a9698 2022-09-15 mark if (n < 0)
9044 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
9045 ec2a9698 2022-09-15 mark *off += n;
9047 ec2a9698 2022-09-15 mark return NULL;
9050 ec2a9698 2022-09-15 mark static const struct got_error *
9051 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
9053 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9054 ec2a9698 2022-09-15 mark off_t off = 0;
9055 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
9056 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
9057 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
9058 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9059 ec2a9698 2022-09-15 mark GENERATE_HELP
9060 ec2a9698 2022-09-15 mark #undef KEYMAP_
9061 ec2a9698 2022-09-15 mark #undef KEY_
9064 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9066 ec2a9698 2022-09-15 mark return err;
9068 ec2a9698 2022-09-15 mark n = nitems(km);
9069 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
9071 ec2a9698 2022-09-15 mark return err;
9073 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
9074 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
9075 ec2a9698 2022-09-15 mark show = s->all;
9076 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
9077 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
9080 ec2a9698 2022-09-15 mark if (show) {
9081 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
9083 ec2a9698 2022-09-15 mark return err;
9084 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
9086 ec2a9698 2022-09-15 mark return err;
9089 ec2a9698 2022-09-15 mark fputc('\n', s->f);
9091 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
9092 ec2a9698 2022-09-15 mark return err;
9095 ec2a9698 2022-09-15 mark static const struct got_error *
9096 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
9099 ec2a9698 2022-09-15 mark const struct got_error *err;
9101 ec2a9698 2022-09-15 mark free(s->line_offsets);
9102 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9103 ec2a9698 2022-09-15 mark s->nlines = 0;
9105 ec2a9698 2022-09-15 mark f = got_opentemp();
9106 ec2a9698 2022-09-15 mark if (f == NULL)
9107 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
9110 ec2a9698 2022-09-15 mark err = format_help(s);
9112 ec2a9698 2022-09-15 mark return err;
9114 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
9115 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
9117 ec2a9698 2022-09-15 mark return NULL;
9120 ec2a9698 2022-09-15 mark static const struct got_error *
9121 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
9123 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
9124 ec2a9698 2022-09-15 mark return NULL;
9127 eaf2c13e 2022-09-17 mark static void
9128 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9129 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
9131 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
9133 eaf2c13e 2022-09-17 mark *f = s->f;
9134 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
9135 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
9136 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
9137 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
9138 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
9139 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
9142 ec2a9698 2022-09-15 mark static const struct got_error *
9143 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
9145 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9146 ec2a9698 2022-09-15 mark const struct got_error *err;
9147 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
9148 ec2a9698 2022-09-15 mark wchar_t *wline;
9149 ec2a9698 2022-09-15 mark char *line;
9150 ec2a9698 2022-09-15 mark ssize_t linelen;
9151 ec2a9698 2022-09-15 mark size_t linesz = 0;
9152 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
9153 ec2a9698 2022-09-15 mark int eos = view->nlines;
9155 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
9156 ec2a9698 2022-09-15 mark --eos; /* account for border */
9158 ec2a9698 2022-09-15 mark s->lineno = 0;
9159 ec2a9698 2022-09-15 mark rewind(s->f);
9160 ec2a9698 2022-09-15 mark werase(view->window);
9162 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
9163 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
9165 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
9166 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
9167 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
9169 ec2a9698 2022-09-15 mark return err;
9170 ec2a9698 2022-09-15 mark if (eos <= 1)
9171 ec2a9698 2022-09-15 mark return NULL;
9172 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
9175 ec2a9698 2022-09-15 mark s->eof = 0;
9176 ec2a9698 2022-09-15 mark view->maxx = 0;
9177 ec2a9698 2022-09-15 mark line = NULL;
9178 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
9179 ec2a9698 2022-09-15 mark attr_t attr = 0;
9181 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9182 ec2a9698 2022-09-15 mark if (linelen == -1) {
9183 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
9184 ec2a9698 2022-09-15 mark free(line);
9185 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
9187 ec2a9698 2022-09-15 mark s->eof = 1;
9190 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
9192 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9194 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
9195 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
9197 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9198 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
9199 ec2a9698 2022-09-15 mark if (err) {
9200 ec2a9698 2022-09-15 mark free(line);
9201 ec2a9698 2022-09-15 mark return err;
9203 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
9204 ec2a9698 2022-09-15 mark free(wline);
9205 ec2a9698 2022-09-15 mark wline = NULL;
9208 ec2a9698 2022-09-15 mark wattron(view->window, attr);
9209 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
9210 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9211 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
9212 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
9213 ec2a9698 2022-09-15 mark if (err) {
9214 ec2a9698 2022-09-15 mark free(line);
9215 ec2a9698 2022-09-15 mark return err;
9220 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
9221 b637f33f 2023-02-01 mark view->x, view->ncols, 0, view->x ? 1 : 0);
9222 ec2a9698 2022-09-15 mark if (err) {
9223 ec2a9698 2022-09-15 mark free(line);
9224 ec2a9698 2022-09-15 mark return err;
9226 b637f33f 2023-02-01 mark waddwstr(view->window, &wline[skip]);
9227 ec2a9698 2022-09-15 mark free(wline);
9228 ec2a9698 2022-09-15 mark wline = NULL;
9230 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
9231 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
9232 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
9234 b637f33f 2023-02-01 mark if (width < view->ncols)
9235 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
9238 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
9239 ec2a9698 2022-09-15 mark if (++nprinted == 1)
9240 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
9242 ec2a9698 2022-09-15 mark free(line);
9243 ec2a9698 2022-09-15 mark if (nprinted > 0)
9244 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9246 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
9248 ec2a9698 2022-09-15 mark view_border(view);
9250 ec2a9698 2022-09-15 mark if (s->eof) {
9251 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
9252 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
9253 ec2a9698 2022-09-15 mark view->ncols - 1);
9254 ec2a9698 2022-09-15 mark if (rc == ERR)
9255 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9257 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
9258 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
9259 ec2a9698 2022-09-15 mark wstandout(view->window);
9260 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
9261 ec2a9698 2022-09-15 mark view->ncols - 1);
9262 ec2a9698 2022-09-15 mark if (rc == ERR)
9263 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9264 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
9265 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
9266 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
9267 ec2a9698 2022-09-15 mark if (rc == ERR)
9268 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
9270 ec2a9698 2022-09-15 mark wstandend(view->window);
9273 ec2a9698 2022-09-15 mark return NULL;
9276 ec2a9698 2022-09-15 mark static const struct got_error *
9277 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9279 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9280 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9281 ec2a9698 2022-09-15 mark char *line = NULL;
9282 ec2a9698 2022-09-15 mark ssize_t linelen;
9283 ec2a9698 2022-09-15 mark size_t linesz = 0;
9284 ec2a9698 2022-09-15 mark int eos, nscroll;
9286 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
9287 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
9288 ec2a9698 2022-09-15 mark --eos; /* border */
9290 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
9292 ec2a9698 2022-09-15 mark switch (ch) {
9295 ec2a9698 2022-09-15 mark case KEY_RIGHT:
9297 ec2a9698 2022-09-15 mark case KEY_LEFT:
9299 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
9302 ec2a9698 2022-09-15 mark case KEY_HOME:
9303 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9304 ec2a9698 2022-09-15 mark view->count = 0;
9307 ec2a9698 2022-09-15 mark case KEY_END:
9308 ec2a9698 2022-09-15 mark view->count = 0;
9309 ec2a9698 2022-09-15 mark if (s->eof)
9311 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
9312 ec2a9698 2022-09-15 mark s->eof = 1;
9315 ec2a9698 2022-09-15 mark case KEY_UP:
9316 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
9317 ec2a9698 2022-09-15 mark --s->first_displayed_line;
9319 ec2a9698 2022-09-15 mark view->count = 0;
9321 ec2a9698 2022-09-15 mark case CTRL('u'):
9323 ec2a9698 2022-09-15 mark nscroll /= 2;
9324 ec2a9698 2022-09-15 mark /* FALL THROUGH */
9325 ec2a9698 2022-09-15 mark case KEY_PPAGE:
9326 ec2a9698 2022-09-15 mark case CTRL('b'):
9328 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
9329 ec2a9698 2022-09-15 mark view->count = 0;
9332 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
9333 ec2a9698 2022-09-15 mark s->first_displayed_line--;
9336 ec2a9698 2022-09-15 mark case KEY_DOWN:
9337 ec2a9698 2022-09-15 mark case CTRL('n'):
9338 ec2a9698 2022-09-15 mark if (!s->eof)
9339 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
9341 ec2a9698 2022-09-15 mark view->count = 0;
9343 ec2a9698 2022-09-15 mark case CTRL('d'):
9345 ec2a9698 2022-09-15 mark nscroll /= 2;
9346 ec2a9698 2022-09-15 mark /* FALL THROUGH */
9347 ec2a9698 2022-09-15 mark case KEY_NPAGE:
9348 ec2a9698 2022-09-15 mark case CTRL('f'):
9351 ec2a9698 2022-09-15 mark if (s->eof) {
9352 ec2a9698 2022-09-15 mark view->count = 0;
9355 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
9356 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9357 ec2a9698 2022-09-15 mark s->first_displayed_line++;
9358 ec2a9698 2022-09-15 mark if (linelen == -1) {
9359 ec2a9698 2022-09-15 mark if (feof(s->f))
9360 ec2a9698 2022-09-15 mark s->eof = 1;
9362 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
9366 ec2a9698 2022-09-15 mark free(line);
9369 ec2a9698 2022-09-15 mark view->count = 0;
9373 ec2a9698 2022-09-15 mark return err;
9376 ec2a9698 2022-09-15 mark static const struct got_error *
9377 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
9379 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9381 ec2a9698 2022-09-15 mark free(s->line_offsets);
9382 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9383 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
9384 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9386 ec2a9698 2022-09-15 mark return NULL;
9389 ec2a9698 2022-09-15 mark static const struct got_error *
9390 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
9392 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9395 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
9396 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9398 ec2a9698 2022-09-15 mark wclear(view->window);
9399 ec2a9698 2022-09-15 mark view->count = 0;
9400 ec2a9698 2022-09-15 mark view->x = 0;
9401 ec2a9698 2022-09-15 mark s->all = !s->all;
9402 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9403 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9404 ec2a9698 2022-09-15 mark s->matched_line = 0;
9406 ec2a9698 2022-09-15 mark return create_help(s);
9409 ec2a9698 2022-09-15 mark static const struct got_error *
9410 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
9412 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9413 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9415 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
9416 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9417 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9418 ec2a9698 2022-09-15 mark s->selected_line = 1;
9420 ec2a9698 2022-09-15 mark view->show = show_help_view;
9421 ec2a9698 2022-09-15 mark view->input = input_help_view;
9422 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
9423 ec2a9698 2022-09-15 mark view->close = close_help_view;
9424 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
9425 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
9426 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
9428 ec2a9698 2022-09-15 mark err = create_help(s);
9429 ec2a9698 2022-09-15 mark return err;
9432 ec2a9698 2022-09-15 mark static const struct got_error *
9433 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9434 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
9436 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
9438 136e2bd4 2022-07-23 mark *new_view = NULL;
9440 136e2bd4 2022-07-23 mark switch (request) {
9441 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
9442 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9443 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9445 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9446 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9447 136e2bd4 2022-07-23 mark view, s->repo);
9449 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9450 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9452 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9453 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9454 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9456 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9457 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9460 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9461 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9463 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9464 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9465 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9466 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9467 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9468 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9469 136e2bd4 2022-07-23 mark &view->state.tree);
9470 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9471 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9472 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9473 136e2bd4 2022-07-23 mark view->state.ref.repo);
9475 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9476 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9478 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9479 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9480 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9481 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9482 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9483 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9484 136e2bd4 2022-07-23 mark view->state.log.repo);
9485 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9486 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9487 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9488 136e2bd4 2022-07-23 mark view->state.ref.repo);
9490 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9491 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9493 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9494 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9495 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9496 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9497 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9498 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9499 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9500 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9502 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9503 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9505 136e2bd4 2022-07-23 mark view_close(*new_view);
9507 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9508 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9509 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9510 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9511 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9513 ec2a9698 2022-09-15 mark view_close(*new_view);
9516 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9519 136e2bd4 2022-07-23 mark return err;
9523 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9524 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9526 6458efa5 2020-11-24 stsp static void
9527 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9529 9b058f45 2022-06-30 mark switch (view->type) {
9530 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9531 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9532 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9533 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9537 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9538 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9540 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9541 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9544 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9545 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9546 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9548 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9549 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9550 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9552 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9553 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9554 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9560 9b058f45 2022-06-30 mark view->offset = 0;
9564 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9565 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9567 9b058f45 2022-06-30 mark static const struct got_error *
9568 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9570 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9571 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9572 9b058f45 2022-06-30 mark int *selected = NULL;
9573 9b058f45 2022-06-30 mark int header, offset;
9575 9b058f45 2022-06-30 mark switch (view->type) {
9576 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9577 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9578 9b058f45 2022-06-30 mark header = 3;
9579 9b058f45 2022-06-30 mark scrolld = NULL;
9580 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9581 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9582 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9583 9b058f45 2022-06-30 mark s->selected_line -= offset;
9584 9b058f45 2022-06-30 mark view->offset = offset;
9588 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9589 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9590 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9591 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9592 9b058f45 2022-06-30 mark selected = &s->selected;
9595 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9596 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9597 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9598 9b058f45 2022-06-30 mark header = 3;
9599 9b058f45 2022-06-30 mark selected = &s->selected;
9602 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9603 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9604 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9605 9b058f45 2022-06-30 mark header = 5;
9606 9b058f45 2022-06-30 mark selected = &s->selected;
9610 9b058f45 2022-06-30 mark selected = NULL;
9611 9b058f45 2022-06-30 mark scrolld = NULL;
9612 9b058f45 2022-06-30 mark header = 0;
9616 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9617 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9618 9b058f45 2022-06-30 mark view->offset = offset;
9619 9b058f45 2022-06-30 mark if (scrolld && offset) {
9620 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9621 9b058f45 2022-06-30 mark *selected -= offset;
9625 9b058f45 2022-06-30 mark return err;
9628 9b058f45 2022-06-30 mark static void
9629 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9633 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9634 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9635 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9636 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9638 6879ba42 2020-10-01 naddy fputc('\n', fp);
9641 4ed7e80c 2018-05-20 stsp __dead static void
9642 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9644 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9646 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9647 6879ba42 2020-10-01 naddy getprogname());
9648 6879ba42 2020-10-01 naddy if (hflag) {
9649 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9650 6879ba42 2020-10-01 naddy list_commands(fp);
9652 6879ba42 2020-10-01 naddy exit(status);
9655 c2301be8 2018-04-30 stsp static char **
9656 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9658 ee85c5e8 2020-02-29 stsp va_list ap;
9659 c2301be8 2018-04-30 stsp char **argv;
9662 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9664 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9665 c2301be8 2018-04-30 stsp if (argv == NULL)
9666 c2301be8 2018-04-30 stsp err(1, "calloc");
9667 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9668 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9669 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9670 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9673 ee85c5e8 2020-02-29 stsp va_end(ap);
9674 c2301be8 2018-04-30 stsp return argv;
9678 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9679 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9680 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9681 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9682 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9684 ee85c5e8 2020-02-29 stsp static const struct got_error *
9685 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9687 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9688 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9689 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9690 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9691 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9692 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9693 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9694 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9695 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9697 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9698 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9699 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9701 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9702 0ae84acc 2022-06-15 tracey if (error != NULL)
9703 0ae84acc 2022-06-15 tracey goto done;
9705 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9706 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9707 ee85c5e8 2020-02-29 stsp goto done;
9709 ee85c5e8 2020-02-29 stsp if (worktree)
9710 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9712 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9713 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9714 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9715 ee85c5e8 2020-02-29 stsp goto done;
9718 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9719 ee85c5e8 2020-02-29 stsp if (error != NULL)
9720 ee85c5e8 2020-02-29 stsp goto done;
9722 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9723 ee85c5e8 2020-02-29 stsp repo, worktree);
9724 ee85c5e8 2020-02-29 stsp if (error)
9725 ee85c5e8 2020-02-29 stsp goto done;
9727 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9728 84de9106 2020-12-26 stsp if (error)
9729 84de9106 2020-12-26 stsp goto done;
9730 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9731 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9732 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9733 ee85c5e8 2020-02-29 stsp if (error)
9734 ee85c5e8 2020-02-29 stsp goto done;
9736 ee85c5e8 2020-02-29 stsp if (worktree) {
9737 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9738 ee85c5e8 2020-02-29 stsp worktree = NULL;
9741 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9742 a44927cc 2022-04-07 stsp if (error)
9743 a44927cc 2022-04-07 stsp goto done;
9745 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9746 ee85c5e8 2020-02-29 stsp if (error) {
9747 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9748 ee85c5e8 2020-02-29 stsp goto done;
9749 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9750 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9751 6879ba42 2020-10-01 naddy usage(1, 1);
9752 ee85c5e8 2020-02-29 stsp /* not reached */
9755 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9756 ee85c5e8 2020-02-29 stsp if (error)
9757 ee85c5e8 2020-02-29 stsp goto done;
9759 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9761 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9762 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9764 1d0f4054 2021-06-17 stsp if (repo) {
9765 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9766 1d0f4054 2021-06-17 stsp if (error == NULL)
9767 1d0f4054 2021-06-17 stsp error = close_err;
9769 a44927cc 2022-04-07 stsp if (commit)
9770 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9771 ee85c5e8 2020-02-29 stsp if (worktree)
9772 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9773 0ae84acc 2022-06-15 tracey if (pack_fds) {
9774 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9775 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9776 0ae84acc 2022-06-15 tracey if (error == NULL)
9777 0ae84acc 2022-06-15 tracey error = pack_err;
9780 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9781 ee85c5e8 2020-02-29 stsp free(commit_id);
9782 ee85c5e8 2020-02-29 stsp free(cwd);
9783 ee85c5e8 2020-02-29 stsp free(repo_path);
9784 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9785 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9787 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9788 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9789 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9791 87670572 2020-12-26 naddy tog_free_refs();
9792 ee85c5e8 2020-02-29 stsp return error;
9796 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9798 7483826e 2023-04-14 mark const struct got_error *io_err, *error = NULL;
9799 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9800 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9801 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9802 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9803 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9804 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9806 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9807 098596c5 2023-04-14 stsp const char *test_script_path;
9809 1fa0d17d 2023-02-13 op setlocale(LC_CTYPE, "");
9812 098596c5 2023-04-14 stsp * Test mode init must happen before pledge() because "tty" will
9813 098596c5 2023-04-14 stsp * not allow TTY-related ioctls to occur via regular files.
9815 5bde4759 2023-04-15 mark test_script_path = getenv("TOG_TEST_SCRIPT");
9816 098596c5 2023-04-14 stsp if (test_script_path != NULL) {
9817 098596c5 2023-04-14 stsp error = init_mock_term(test_script_path);
9818 098596c5 2023-04-14 stsp if (error) {
9819 098596c5 2023-04-14 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9822 69b9e75f 2023-04-21 op } else if (!isatty(STDIN_FILENO))
9823 69b9e75f 2023-04-21 op errx(1, "standard input is not a tty");
9825 098596c5 2023-04-14 stsp #if !defined(PROFILE)
9826 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9827 1fa0d17d 2023-02-13 op NULL) == -1)
9828 1fa0d17d 2023-02-13 op err(1, "pledge");
9831 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9832 9f7d7167 2018-04-29 stsp switch (ch) {
9834 9f7d7167 2018-04-29 stsp hflag = 1;
9837 53ccebc2 2019-07-30 stsp Vflag = 1;
9840 6879ba42 2020-10-01 naddy usage(hflag, 1);
9841 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9845 9f7d7167 2018-04-29 stsp argc -= optind;
9846 9f7d7167 2018-04-29 stsp argv += optind;
9847 9814e6a3 2020-09-27 naddy optind = 1;
9848 c2301be8 2018-04-30 stsp optreset = 1;
9850 53ccebc2 2019-07-30 stsp if (Vflag) {
9851 53ccebc2 2019-07-30 stsp got_version_print_str();
9852 6879ba42 2020-10-01 naddy return 0;
9855 c2301be8 2018-04-30 stsp if (argc == 0) {
9856 f29d3e89 2018-06-23 stsp if (hflag)
9857 6879ba42 2020-10-01 naddy usage(hflag, 0);
9858 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9859 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9861 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9865 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9866 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9867 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9868 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9869 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9875 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9876 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9877 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9878 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9879 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9880 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9883 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9884 ee85c5e8 2020-02-29 stsp if (argc != 1)
9885 6879ba42 2020-10-01 naddy usage(0, 1);
9886 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9887 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9889 ee85c5e8 2020-02-29 stsp if (hflag)
9890 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9892 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9895 7483826e 2023-04-14 mark if (using_mock_io) {
9896 7483826e 2023-04-14 mark io_err = tog_io_close();
9897 7483826e 2023-04-14 mark if (error == NULL)
9898 7483826e 2023-04-14 mark error = io_err;
9901 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9903 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9904 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9905 a2f4a359 2020-02-28 stsp free(cmd_argv);
9908 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9909 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9910 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9911 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9912 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9913 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);