2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
28 5822e79e 2023-02-23 op #include <sha2.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
45 53ccebc2 2019-07-30 stsp #include "got_version.h"
46 9f7d7167 2018-04-29 stsp #include "got_error.h"
47 80ddbec8 2018-04-29 stsp #include "got_object.h"
48 80ddbec8 2018-04-29 stsp #include "got_reference.h"
49 80ddbec8 2018-04-29 stsp #include "got_repository.h"
50 80ddbec8 2018-04-29 stsp #include "got_diff.h"
51 511a516b 2018-05-19 stsp #include "got_opentemp.h"
52 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
53 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
54 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
55 a70480e0 2018-06-23 stsp #include "got_blame.h"
56 c2db6724 2019-01-04 stsp #include "got_privsep.h"
57 1dd54920 2019-05-11 stsp #include "got_path.h"
58 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
70 9f7d7167 2018-04-29 stsp #ifndef nitems
71 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 9f7d7167 2018-04-29 stsp struct tog_cmd {
75 c2301be8 2018-04-30 stsp const char *name;
76 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
77 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
80 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
84 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
85 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
90 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
91 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
93 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
94 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
95 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
96 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
97 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
98 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
101 d6b05b5b 2018-08-04 stsp enum tog_view_type {
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
103 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
104 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
105 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
106 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
107 ec2a9698 2022-09-15 mark TOG_VIEW_HELP
110 ec2a9698 2022-09-15 mark /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
111 ec2a9698 2022-09-15 mark enum tog_keymap_type {
112 ec2a9698 2022-09-15 mark TOG_KEYMAP_KEYS = -2,
113 ec2a9698 2022-09-15 mark TOG_KEYMAP_GLOBAL,
114 ec2a9698 2022-09-15 mark TOG_KEYMAP_DIFF,
115 ec2a9698 2022-09-15 mark TOG_KEYMAP_LOG,
116 ec2a9698 2022-09-15 mark TOG_KEYMAP_BLAME,
117 ec2a9698 2022-09-15 mark TOG_KEYMAP_TREE,
118 ec2a9698 2022-09-15 mark TOG_KEYMAP_REF,
119 ec2a9698 2022-09-15 mark TOG_KEYMAP_HELP
122 9b058f45 2022-06-30 mark enum tog_view_mode {
123 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
124 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
125 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
128 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
130 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
132 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
133 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
134 ba4f502b 2018-08-04 stsp struct got_object_id *id;
135 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
138 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
139 ba4f502b 2018-08-04 stsp struct commit_queue {
140 ba4f502b 2018-08-04 stsp int ncommits;
141 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
144 f26dddb7 2019-11-08 stsp struct tog_color {
145 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
146 6d17833f 2019-11-08 stsp regex_t regex;
147 6d17833f 2019-11-08 stsp short colorpair;
149 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
151 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
152 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
153 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
155 cc488aa7 2022-01-23 stsp static const struct got_error *
156 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
157 cc488aa7 2022-01-23 stsp struct got_reference* re2)
159 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
160 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
161 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
163 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
164 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
165 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
166 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
168 cc488aa7 2022-01-23 stsp return NULL;
169 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
171 cc488aa7 2022-01-23 stsp return NULL;
174 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
175 cc488aa7 2022-01-23 stsp return NULL;
178 11b20872 2019-11-08 stsp static const struct got_error *
179 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
181 51a10b52 2020-12-26 stsp const struct got_error *err;
183 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
184 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
187 51a10b52 2020-12-26 stsp return err;
189 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
193 51a10b52 2020-12-26 stsp static void
194 51a10b52 2020-12-26 stsp tog_free_refs(void)
196 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
197 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
198 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
200 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
203 51a10b52 2020-12-26 stsp static const struct got_error *
204 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
205 11b20872 2019-11-08 stsp int idx, short color)
207 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
208 11b20872 2019-11-08 stsp struct tog_color *tc;
209 11b20872 2019-11-08 stsp int regerr = 0;
211 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
212 11b20872 2019-11-08 stsp return NULL;
214 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
216 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
217 11b20872 2019-11-08 stsp if (tc == NULL)
218 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
219 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
220 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
221 11b20872 2019-11-08 stsp if (regerr) {
222 11b20872 2019-11-08 stsp static char regerr_msg[512];
223 11b20872 2019-11-08 stsp static char err_msg[512];
224 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
225 11b20872 2019-11-08 stsp sizeof(regerr_msg));
226 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
227 11b20872 2019-11-08 stsp regerr_msg);
228 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
230 11b20872 2019-11-08 stsp return err;
232 11b20872 2019-11-08 stsp tc->colorpair = idx;
233 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
234 11b20872 2019-11-08 stsp return NULL;
237 11b20872 2019-11-08 stsp static void
238 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
240 11b20872 2019-11-08 stsp struct tog_color *tc;
242 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
243 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
244 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
245 11b20872 2019-11-08 stsp regfree(&tc->regex);
250 336075a4 2022-06-25 op static struct tog_color *
251 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
253 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
255 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
256 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
260 11b20872 2019-11-08 stsp return NULL;
264 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
267 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
269 11b20872 2019-11-08 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
271 11b20872 2019-11-08 stsp return COLOR_YELLOW;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
273 11b20872 2019-11-08 stsp return COLOR_GREEN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
275 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
277 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
279 91b8c405 2020-01-25 stsp return COLOR_CYAN;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
281 11b20872 2019-11-08 stsp return COLOR_GREEN;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
283 11b20872 2019-11-08 stsp return COLOR_GREEN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
285 11b20872 2019-11-08 stsp return COLOR_CYAN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
287 11b20872 2019-11-08 stsp return COLOR_YELLOW;
288 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
289 6458efa5 2020-11-24 stsp return COLOR_GREEN;
290 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
291 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
294 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
295 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
301 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
303 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
305 11b20872 2019-11-08 stsp if (val == NULL)
306 11b20872 2019-11-08 stsp return default_color_value(envvar);
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
309 11b20872 2019-11-08 stsp return COLOR_BLACK;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
311 11b20872 2019-11-08 stsp return COLOR_RED;
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
313 11b20872 2019-11-08 stsp return COLOR_GREEN;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
315 11b20872 2019-11-08 stsp return COLOR_YELLOW;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
317 11b20872 2019-11-08 stsp return COLOR_BLUE;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
319 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
321 11b20872 2019-11-08 stsp return COLOR_CYAN;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
323 11b20872 2019-11-08 stsp return COLOR_WHITE;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
327 11b20872 2019-11-08 stsp return default_color_value(envvar);
330 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
331 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
332 3dbaef42 2020-11-24 stsp const char *label1, *label2;
333 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
334 f9d37699 2022-06-28 stsp int fd1, fd2;
335 c7d5c43c 2022-08-04 mark int lineno;
336 15a087fe 2019-02-21 stsp int first_displayed_line;
337 15a087fe 2019-02-21 stsp int last_displayed_line;
339 15a087fe 2019-02-21 stsp int diff_context;
340 3dbaef42 2020-11-24 stsp int ignore_whitespace;
341 64453f7e 2020-11-21 stsp int force_text_diff;
342 15a087fe 2019-02-21 stsp struct got_repository *repo;
343 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
344 fe621944 2020-11-10 stsp size_t nlines;
345 f44b1f58 2020-02-02 tracey int matched_line;
346 f44b1f58 2020-02-02 tracey int selected_line;
348 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
349 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
352 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
353 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
355 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
356 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
357 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
358 1a76625f 2018-10-22 stsp int commits_needed;
359 fb280deb 2021-08-30 stsp int load_all;
360 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
361 568eae95 2022-09-11 mark struct commit_queue *real_commits;
362 1a76625f 2018-10-22 stsp const char *in_repo_path;
363 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
364 1a76625f 2018-10-22 stsp struct got_repository *repo;
365 74467cc8 2022-06-15 stsp int *pack_fds;
366 1a76625f 2018-10-22 stsp int log_complete;
367 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
368 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
369 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
370 13add988 2019-10-15 stsp int *searching;
371 13add988 2019-10-15 stsp int *search_next_done;
372 13add988 2019-10-15 stsp regex_t *regex;
373 568eae95 2022-09-11 mark int *limiting;
374 568eae95 2022-09-11 mark int limit_match;
375 568eae95 2022-09-11 mark regex_t *limit_regex;
376 568eae95 2022-09-11 mark struct commit_queue *limit_commits;
379 1a76625f 2018-10-22 stsp struct tog_log_view_state {
380 568eae95 2022-09-11 mark struct commit_queue *commits;
381 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
382 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
383 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
384 568eae95 2022-09-11 mark struct commit_queue real_commits;
385 b01e7d3b 2018-08-04 stsp int selected;
386 b01e7d3b 2018-08-04 stsp char *in_repo_path;
387 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
388 b672a97a 2020-01-27 stsp int log_branches;
389 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
390 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
391 1a76625f 2018-10-22 stsp sig_atomic_t quit;
392 1a76625f 2018-10-22 stsp pthread_t thread;
393 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
394 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
395 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
396 11b20872 2019-11-08 stsp struct tog_colors colors;
397 10aab77f 2022-07-19 op int use_committer;
398 568eae95 2022-09-11 mark int limit_view;
399 568eae95 2022-09-11 mark regex_t limit_regex;
400 568eae95 2022-09-11 mark struct commit_queue limit_commits;
403 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
404 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
405 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
406 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
407 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
408 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
409 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
410 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
411 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
412 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
413 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
414 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
415 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
416 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
417 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
419 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
420 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
421 e9424729 2018-08-04 stsp int nlines;
423 e9424729 2018-08-04 stsp struct tog_view *view;
424 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
429 e9424729 2018-08-04 stsp const char *path;
430 e9424729 2018-08-04 stsp struct got_repository *repo;
431 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
432 e9424729 2018-08-04 stsp int *complete;
433 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
434 fc06ba56 2019-08-22 stsp void *cancel_arg;
437 e9424729 2018-08-04 stsp struct tog_blame {
439 be659d10 2020-11-18 stsp off_t filesize;
440 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
441 6fcac457 2018-11-19 stsp int nlines;
442 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
443 e9424729 2018-08-04 stsp pthread_t thread;
444 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
445 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
446 e9424729 2018-08-04 stsp const char *path;
447 0ae84acc 2022-06-15 tracey int *pack_fds;
450 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
451 7cbe629d 2018-08-04 stsp int first_displayed_line;
452 7cbe629d 2018-08-04 stsp int last_displayed_line;
453 7cbe629d 2018-08-04 stsp int selected_line;
454 c0f61fa4 2022-07-11 mark int last_diffed_line;
455 7cbe629d 2018-08-04 stsp int blame_complete;
458 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
459 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
460 e5a0f69f 2018-08-18 stsp char *path;
461 7cbe629d 2018-08-04 stsp struct got_repository *repo;
462 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
463 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
464 e9424729 2018-08-04 stsp struct tog_blame blame;
465 6c4c42e0 2019-06-24 stsp int matched_line;
466 11b20872 2019-11-08 stsp struct tog_colors colors;
469 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
470 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
471 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
472 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
473 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
474 ad80ab7b 2018-08-04 stsp int selected;
477 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
479 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
480 ad80ab7b 2018-08-04 stsp char *tree_label;
481 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
482 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
483 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
484 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
486 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
487 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
488 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
489 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
490 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
491 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
492 6458efa5 2020-11-24 stsp struct tog_colors colors;
495 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
496 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
497 6458efa5 2020-11-24 stsp struct got_reference *ref;
501 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
503 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
504 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
506 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
507 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
508 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
509 6458efa5 2020-11-24 stsp struct got_repository *repo;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
511 bddb1296 2019-11-08 stsp struct tog_colors colors;
514 ec2a9698 2022-09-15 mark struct tog_help_view_state {
516 ec2a9698 2022-09-15 mark off_t *line_offsets;
517 ec2a9698 2022-09-15 mark size_t nlines;
518 ec2a9698 2022-09-15 mark int lineno;
519 ec2a9698 2022-09-15 mark int first_displayed_line;
520 ec2a9698 2022-09-15 mark int last_displayed_line;
522 ec2a9698 2022-09-15 mark int matched_line;
523 ec2a9698 2022-09-15 mark int selected_line;
525 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
528 ec2a9698 2022-09-15 mark #define GENERATE_HELP \
529 ec2a9698 2022-09-15 mark KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
530 ec2a9698 2022-09-15 mark KEY_("H F1", "Open view-specific help (double tap for all help)"), \
531 ec2a9698 2022-09-15 mark KEY_("k C-p Up", "Move cursor or page up one line"), \
532 ec2a9698 2022-09-15 mark KEY_("j C-n Down", "Move cursor or page down one line"), \
533 ec2a9698 2022-09-15 mark KEY_("C-b b PgUp", "Scroll the view up one page"), \
534 ec2a9698 2022-09-15 mark KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
535 ec2a9698 2022-09-15 mark KEY_("C-u u", "Scroll the view up one half page"), \
536 ec2a9698 2022-09-15 mark KEY_("C-d d", "Scroll the view down one half page"), \
537 0b3f028d 2023-01-09 mark KEY_("g", "Go to line N (default: first line)"), \
538 0b3f028d 2023-01-09 mark KEY_("Home =", "Go to the first line"), \
539 0b3f028d 2023-01-09 mark KEY_("G", "Go to line N (default: last line)"), \
540 0b3f028d 2023-01-09 mark KEY_("End *", "Go to the last line"), \
541 ec2a9698 2022-09-15 mark KEY_("l Right", "Scroll the view right"), \
542 ec2a9698 2022-09-15 mark KEY_("h Left", "Scroll the view left"), \
543 ec2a9698 2022-09-15 mark KEY_("$", "Scroll view to the rightmost position"), \
544 ec2a9698 2022-09-15 mark KEY_("0", "Scroll view to the leftmost position"), \
545 ec2a9698 2022-09-15 mark KEY_("-", "Decrease size of the focussed split"), \
546 ec2a9698 2022-09-15 mark KEY_("+", "Increase size of the focussed split"), \
547 ec2a9698 2022-09-15 mark KEY_("Tab", "Switch focus between views"), \
548 ec2a9698 2022-09-15 mark KEY_("F", "Toggle fullscreen mode"), \
549 ec2a9698 2022-09-15 mark KEY_("/", "Open prompt to enter search term"), \
550 ec2a9698 2022-09-15 mark KEY_("n", "Find next line/token matching the current search term"), \
551 ec2a9698 2022-09-15 mark KEY_("N", "Find previous line/token matching the current search term"),\
552 16a2d4f0 2022-09-19 stsp KEY_("q", "Quit the focussed view; Quit help screen"), \
553 ec2a9698 2022-09-15 mark KEY_("Q", "Quit tog"), \
555 fd6badaa 2022-09-23 stsp KEYMAP_("Log view", TOG_KEYMAP_LOG), \
556 ec2a9698 2022-09-15 mark KEY_("< ,", "Move cursor up one commit"), \
557 ec2a9698 2022-09-15 mark KEY_("> .", "Move cursor down one commit"), \
558 ec2a9698 2022-09-15 mark KEY_("Enter", "Open diff view of the selected commit"), \
559 ec2a9698 2022-09-15 mark KEY_("B", "Reload the log view and toggle display of merged commits"), \
560 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
561 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the repository from the selected" \
562 ec2a9698 2022-09-15 mark " commit"), \
563 ec2a9698 2022-09-15 mark KEY_("@", "Toggle between displaying author and committer name"), \
564 ec2a9698 2022-09-15 mark KEY_("&", "Open prompt to enter term to limit commits displayed"), \
565 ec2a9698 2022-09-15 mark KEY_("C-g Backspace", "Cancel current search or log operation"), \
566 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload the log view with new commits in the repository"), \
568 fd6badaa 2022-09-23 stsp KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
569 ec2a9698 2022-09-15 mark KEY_("K < ,", "Display diff of next line in the file/log entry"), \
570 ec2a9698 2022-09-15 mark KEY_("J > .", "Display diff of previous line in the file/log entry"), \
571 ec2a9698 2022-09-15 mark KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
572 ec2a9698 2022-09-15 mark KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
573 ec2a9698 2022-09-15 mark " data"), \
574 ec2a9698 2022-09-15 mark KEY_("(", "Go to the previous file in the diff"), \
575 ec2a9698 2022-09-15 mark KEY_(")", "Go to the next file in the diff"), \
576 ec2a9698 2022-09-15 mark KEY_("{", "Go to the previous hunk in the diff"), \
577 ec2a9698 2022-09-15 mark KEY_("}", "Go to the next hunk in the diff"), \
578 ec2a9698 2022-09-15 mark KEY_("[", "Decrease the number of context lines"), \
579 ec2a9698 2022-09-15 mark KEY_("]", "Increase the number of context lines"), \
580 ec2a9698 2022-09-15 mark KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
582 fd6badaa 2022-09-23 stsp KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
583 ec2a9698 2022-09-15 mark KEY_("Enter", "Display diff view of the selected line's commit"), \
584 ec2a9698 2022-09-15 mark KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
585 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the currently selected annotated line"), \
586 ec2a9698 2022-09-15 mark KEY_("C", "Reload view with the previously blamed commit"), \
587 ec2a9698 2022-09-15 mark KEY_("c", "Reload view with the version of the file found in the" \
588 ec2a9698 2022-09-15 mark " selected line's commit"), \
589 ec2a9698 2022-09-15 mark KEY_("p", "Reload view with the version of the file found in the" \
590 ec2a9698 2022-09-15 mark " selected line's parent commit"), \
592 fd6badaa 2022-09-23 stsp KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
593 ec2a9698 2022-09-15 mark KEY_("Enter", "Enter selected directory or open blame view of the" \
594 ec2a9698 2022-09-15 mark " selected file"), \
595 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the selected entry"), \
596 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
597 ec2a9698 2022-09-15 mark KEY_("i", "Show object IDs for all tree entries"), \
598 ec2a9698 2022-09-15 mark KEY_("Backspace", "Return to the parent directory"), \
600 fd6badaa 2022-09-23 stsp KEYMAP_("Ref view", TOG_KEYMAP_REF), \
601 ec2a9698 2022-09-15 mark KEY_("Enter", "Display log view of the selected reference"), \
602 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the selected reference"), \
603 ec2a9698 2022-09-15 mark KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
604 ec2a9698 2022-09-15 mark KEY_("m", "Toggle display of last modified date for each reference"), \
605 ec2a9698 2022-09-15 mark KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
606 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload view with all repository references")
608 ec2a9698 2022-09-15 mark struct tog_key_map {
609 ec2a9698 2022-09-15 mark const char *keys;
610 ec2a9698 2022-09-15 mark const char *info;
611 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
615 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
617 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
618 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
619 669b5ffa 2018-10-07 stsp * there is enough screen estate.
621 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
622 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
624 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
625 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
626 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
628 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
629 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
631 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
633 cc3c9aac 2018-08-01 stsp struct tog_view {
634 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
635 26ed57b2 2018-05-19 stsp WINDOW *window;
636 26ed57b2 2018-05-19 stsp PANEL *panel;
637 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
638 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
639 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
640 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
641 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
642 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
643 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
644 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
645 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
647 669b5ffa 2018-10-07 stsp struct tog_view *parent;
648 669b5ffa 2018-10-07 stsp struct tog_view *child;
651 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
652 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
653 e78dc838 2020-12-04 stsp * between parent and child.
654 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
655 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
656 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
657 e78dc838 2020-12-04 stsp * situations.
659 e78dc838 2020-12-04 stsp int focus_child;
661 9b058f45 2022-06-30 mark enum tog_view_mode mode;
662 5dc9f4bc 2018-08-04 stsp /* type-specific state */
663 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
665 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
666 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
667 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
668 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
669 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
670 ec2a9698 2022-09-15 mark struct tog_help_view_state help;
673 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
674 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
675 e78dc838 2020-12-04 stsp struct tog_view *, int);
676 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
677 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
678 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
680 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
681 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
682 eaf2c13e 2022-09-17 mark void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
683 eaf2c13e 2022-09-17 mark int **, int **, int **, int **);
684 c0c4acc8 2021-01-24 stsp int search_started;
685 60493ae3 2019-06-20 stsp int searching;
686 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
687 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
688 60493ae3 2019-06-20 stsp int search_next_done;
689 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
690 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
691 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
692 1803e47f 2019-06-22 stsp regex_t regex;
693 41605754 2020-11-12 stsp regmatch_t regmatch;
694 3f6c6614 2023-01-23 mark const char *action;
697 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
698 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
699 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
700 78756c87 2020-11-24 stsp struct got_repository *);
701 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
702 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
703 e78dc838 2020-12-04 stsp struct tog_view *, int);
704 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
705 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
706 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
707 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
708 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
709 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
711 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
712 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
713 78756c87 2020-11-24 stsp const char *, const char *, int);
714 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
715 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
716 e78dc838 2020-12-04 stsp struct tog_view *, int);
717 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
718 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
719 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
720 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
722 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
723 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
724 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
725 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
726 e78dc838 2020-12-04 stsp struct tog_view *, int);
727 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
728 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
729 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
730 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
731 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
733 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
734 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
735 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
736 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
737 e78dc838 2020-12-04 stsp struct tog_view *, int);
738 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
739 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
740 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
742 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
743 6458efa5 2020-11-24 stsp struct got_repository *);
744 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
745 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
746 e78dc838 2020-12-04 stsp struct tog_view *, int);
747 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
748 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
749 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
751 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
752 eaf2c13e 2022-09-17 mark struct tog_view *);
753 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
754 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
755 eaf2c13e 2022-09-17 mark struct tog_view *, int);
756 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
757 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
758 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
759 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
760 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
762 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
763 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
764 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
765 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
766 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
768 25791caa 2018-10-24 stsp static void
769 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
771 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
774 83baff54 2019-08-12 stsp static void
775 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
777 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
780 61266923 2020-01-14 stsp static void
781 61266923 2020-01-14 stsp tog_sigcont(int signo)
783 61266923 2020-01-14 stsp tog_sigcont_received = 1;
786 2497f032 2022-05-31 stsp static void
787 2497f032 2022-05-31 stsp tog_sigint(int signo)
789 2497f032 2022-05-31 stsp tog_sigint_received = 1;
792 2497f032 2022-05-31 stsp static void
793 2497f032 2022-05-31 stsp tog_sigterm(int signo)
795 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
799 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
801 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
802 f044c841 2022-10-24 stsp tog_sigint_received || tog_sigterm_received);
805 e5a0f69f 2018-08-18 stsp static const struct got_error *
806 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
808 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
810 669b5ffa 2018-10-07 stsp if (view->child) {
811 5629093a 2022-07-11 stsp child_err = view_close(view->child);
812 669b5ffa 2018-10-07 stsp view->child = NULL;
814 e5a0f69f 2018-08-18 stsp if (view->close)
815 e5a0f69f 2018-08-18 stsp err = view->close(view);
816 ea5e7bb5 2018-08-01 stsp if (view->panel)
817 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
818 ea5e7bb5 2018-08-01 stsp if (view->window)
819 ea5e7bb5 2018-08-01 stsp delwin(view->window);
820 ea5e7bb5 2018-08-01 stsp free(view);
821 5629093a 2022-07-11 stsp return err ? err : child_err;
824 ea5e7bb5 2018-08-01 stsp static struct tog_view *
825 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
826 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
828 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
830 ea5e7bb5 2018-08-01 stsp if (view == NULL)
831 ea5e7bb5 2018-08-01 stsp return NULL;
833 d6b05b5b 2018-08-04 stsp view->type = type;
834 f7d12f7e 2018-08-01 stsp view->lines = LINES;
835 f7d12f7e 2018-08-01 stsp view->cols = COLS;
836 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
837 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
838 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
839 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
840 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
841 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
842 96a765a8 2018-08-04 stsp view_close(view);
843 ea5e7bb5 2018-08-01 stsp return NULL;
845 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
846 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
847 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
848 96a765a8 2018-08-04 stsp view_close(view);
849 ea5e7bb5 2018-08-01 stsp return NULL;
852 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
853 ea5e7bb5 2018-08-01 stsp return view;
857 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
859 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
861 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
864 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
866 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
868 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
871 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
873 5c60c32a 2018-10-18 stsp static const struct got_error *
874 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
876 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
878 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
879 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
880 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
882 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
883 9b058f45 2022-06-30 mark view->begin_x = 0;
884 571ccd73 2022-07-19 mark } else if (!view->resized) {
885 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
886 3c1dfe12 2022-07-08 mark view->cols > 119)
887 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
889 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
890 9b058f45 2022-06-30 mark view->begin_y = 0;
892 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
893 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
894 5c60c32a 2018-10-18 stsp view->lines = LINES;
895 5c60c32a 2018-10-18 stsp view->cols = COLS;
896 5c60c32a 2018-10-18 stsp err = view_resize(view);
898 5c60c32a 2018-10-18 stsp return err;
900 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
901 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
903 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
904 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
906 5c60c32a 2018-10-18 stsp return NULL;
909 5c60c32a 2018-10-18 stsp static const struct got_error *
910 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
912 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
914 5c60c32a 2018-10-18 stsp view->begin_x = 0;
915 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
916 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
917 5c60c32a 2018-10-18 stsp view->ncols = COLS;
918 5c60c32a 2018-10-18 stsp view->lines = LINES;
919 5c60c32a 2018-10-18 stsp view->cols = COLS;
920 5c60c32a 2018-10-18 stsp err = view_resize(view);
922 5c60c32a 2018-10-18 stsp return err;
924 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
925 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
927 5c60c32a 2018-10-18 stsp return NULL;
931 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
933 5c60c32a 2018-10-18 stsp return view->parent == NULL;
937 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
939 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
943 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
945 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
949 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
951 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
952 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
955 9b058f45 2022-06-30 mark static void
956 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
958 9b058f45 2022-06-30 mark PANEL *panel;
959 9b058f45 2022-06-30 mark const struct tog_view *view_above;
961 9b058f45 2022-06-30 mark if (view->parent)
962 9b058f45 2022-06-30 mark return view_border(view->parent);
964 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
965 9b058f45 2022-06-30 mark if (panel == NULL)
968 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
969 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
970 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
971 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
972 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
974 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
975 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
978 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
979 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
980 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
981 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
982 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
984 4d8c2215 2018-08-19 stsp static const struct got_error *
985 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
987 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
988 9b058f45 2022-06-30 mark int dif, nlines, ncols;
990 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
992 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
993 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
995 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
996 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
997 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
999 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1001 4dd27a72 2022-06-29 stsp if (view->child) {
1002 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
1004 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1005 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1006 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1007 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1008 0dbbbe90 2022-06-17 op ncols = COLS;
1010 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1011 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1012 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1014 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1016 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1018 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1019 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1022 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1023 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1024 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1027 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1029 9b058f45 2022-06-30 mark return err;
1030 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1031 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1033 9b058f45 2022-06-30 mark return err;
1035 9b058f45 2022-06-30 mark view_border(view);
1036 9b058f45 2022-06-30 mark update_panels();
1037 9b058f45 2022-06-30 mark doupdate();
1038 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1039 9b058f45 2022-06-30 mark nlines = view->nlines;
1041 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1042 0dbbbe90 2022-06-17 op ncols = COLS;
1044 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1045 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1047 571ccd73 2022-07-19 mark return err;
1050 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1051 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1052 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1053 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1054 0dbbbe90 2022-06-17 op wclear(view->window);
1056 0dbbbe90 2022-06-17 op view->nlines = nlines;
1057 0dbbbe90 2022-06-17 op view->ncols = ncols;
1058 0dbbbe90 2022-06-17 op view->lines = LINES;
1059 0dbbbe90 2022-06-17 op view->cols = COLS;
1061 5c60c32a 2018-10-18 stsp return NULL;
1064 571ccd73 2022-07-19 mark static const struct got_error *
1065 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1067 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1068 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1069 6fe51fee 2022-07-22 mark int n = 0;
1071 6fe51fee 2022-07-22 mark if (s->selected_entry)
1072 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1075 571ccd73 2022-07-19 mark * Request commits to account for the increased
1076 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1078 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1079 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1080 571ccd73 2022-07-19 mark err = request_log_commits(view);
1083 571ccd73 2022-07-19 mark return err;
1086 d9a7ab53 2022-07-11 mark static void
1087 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1089 d9a7ab53 2022-07-11 mark if (n == 0)
1092 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1093 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1094 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1096 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1097 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1098 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1099 d9a7ab53 2022-07-11 mark view->offset -= n;
1101 d9a7ab53 2022-07-11 mark view->offset = 0;
1105 3c1dfe12 2022-07-08 mark static const struct got_error *
1106 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1108 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1109 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1111 3c1dfe12 2022-07-08 mark if (view->parent)
1112 3c1dfe12 2022-07-08 mark v = view->parent;
1116 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1117 3c1dfe12 2022-07-08 mark return NULL;
1119 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1121 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1122 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1123 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1124 3c1dfe12 2022-07-08 mark if (view->parent)
1125 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1127 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1128 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1129 3c1dfe12 2022-07-08 mark view->count = 0;
1130 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1131 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1132 3c1dfe12 2022-07-08 mark view->count = 0;
1133 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1135 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1136 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1137 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1138 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1140 3c1dfe12 2022-07-08 mark return err;
1141 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1143 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1144 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1145 3c1dfe12 2022-07-08 mark if (view->parent)
1146 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1148 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1149 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1150 3c1dfe12 2022-07-08 mark view->count = 0;
1151 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1152 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1153 3c1dfe12 2022-07-08 mark view->count = 0;
1154 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1156 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1159 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1160 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1161 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1162 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1164 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1166 3c1dfe12 2022-07-08 mark return err;
1167 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1169 3c1dfe12 2022-07-08 mark return err;
1171 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1172 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1174 3c1dfe12 2022-07-08 mark return err;
1177 6fe51fee 2022-07-22 mark if (v->resize)
1178 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1179 6fe51fee 2022-07-22 mark else if (v->child->resize)
1180 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1182 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1184 3c1dfe12 2022-07-08 mark return err;
1187 3c1dfe12 2022-07-08 mark static void
1188 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1190 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1192 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1193 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1196 669b5ffa 2018-10-07 stsp static const struct got_error *
1197 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1199 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1201 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1202 669b5ffa 2018-10-07 stsp return NULL;
1204 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1205 669b5ffa 2018-10-07 stsp view->child = NULL;
1206 669b5ffa 2018-10-07 stsp return err;
1209 0dbbbe90 2022-06-17 op static const struct got_error *
1210 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1212 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1214 669b5ffa 2018-10-07 stsp view->child = child;
1215 669b5ffa 2018-10-07 stsp child->parent = view;
1217 3c1dfe12 2022-07-08 mark err = view_resize(view);
1219 3c1dfe12 2022-07-08 mark return err;
1221 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1222 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1224 3c1dfe12 2022-07-08 mark return err;
1227 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1228 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1230 136e2bd4 2022-07-23 mark static const struct got_error *
1231 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1232 136e2bd4 2022-07-23 mark enum tog_view_type request)
1234 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1235 136e2bd4 2022-07-23 mark const struct got_error *err;
1236 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1238 136e2bd4 2022-07-23 mark *requested = NULL;
1240 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1241 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1243 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1245 136e2bd4 2022-07-23 mark return err;
1247 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1248 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1249 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1251 136e2bd4 2022-07-23 mark return err;
1254 136e2bd4 2022-07-23 mark view->focussed = 0;
1255 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1256 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1257 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1258 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1260 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1261 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1262 136e2bd4 2022-07-23 mark err = view_close_child(view);
1264 136e2bd4 2022-07-23 mark return err;
1265 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1267 136e2bd4 2022-07-23 mark return err;
1268 136e2bd4 2022-07-23 mark view->focus_child = 1;
1270 136e2bd4 2022-07-23 mark *requested = new_view;
1272 136e2bd4 2022-07-23 mark return NULL;
1275 34bc9ec9 2019-02-22 stsp static void
1276 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1278 25791caa 2018-10-24 stsp int cols, lines;
1279 25791caa 2018-10-24 stsp struct winsize size;
1281 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1282 25791caa 2018-10-24 stsp cols = 80; /* Default */
1283 25791caa 2018-10-24 stsp lines = 24;
1285 25791caa 2018-10-24 stsp cols = size.ws_col;
1286 25791caa 2018-10-24 stsp lines = size.ws_row;
1288 25791caa 2018-10-24 stsp resize_term(lines, cols);
1291 2b49a8ae 2019-06-22 stsp static const struct got_error *
1292 602eda79 2023-02-09 mark view_search_start(struct tog_view *view, int fast_refresh)
1294 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1295 9b058f45 2022-06-30 mark struct tog_view *v = view;
1296 2b49a8ae 2019-06-22 stsp char pattern[1024];
1299 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1300 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1301 c0c4acc8 2021-01-24 stsp view->searching = 0;
1302 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1304 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1306 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1307 2b49a8ae 2019-06-22 stsp return NULL;
1309 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1310 9b058f45 2022-06-30 mark v = view->child;
1311 9d0feb8b 2022-12-27 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1312 9d0feb8b 2022-12-27 mark v = view->parent;
1314 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1315 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1317 092a9f9c 2022-12-27 mark nodelay(v->window, FALSE); /* block for search term input */
1318 2b49a8ae 2019-06-22 stsp nocbreak();
1320 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1321 9b058f45 2022-06-30 mark wrefresh(v->window);
1324 092a9f9c 2022-12-27 mark nodelay(v->window, TRUE);
1325 602eda79 2023-02-09 mark if (!fast_refresh)
1326 602eda79 2023-02-09 mark halfdelay(10);
1327 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1328 2b49a8ae 2019-06-22 stsp return NULL;
1330 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1331 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1332 7c32bd05 2019-06-22 stsp if (err) {
1333 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1334 7c32bd05 2019-06-22 stsp return err;
1336 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1337 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1338 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1339 2b49a8ae 2019-06-22 stsp view->search_next(view);
1342 2b49a8ae 2019-06-22 stsp return NULL;
1345 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1346 d2366e29 2022-07-07 mark static const struct got_error *
1347 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1349 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1350 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1352 d2366e29 2022-07-07 mark if (view->parent)
1353 d2366e29 2022-07-07 mark v = view->parent;
1357 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1358 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1360 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1362 7532ccda 2022-07-11 mark if (!v->child)
1363 7532ccda 2022-07-11 mark return NULL;
1364 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1365 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1367 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1368 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1369 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1370 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1371 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1374 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1375 d2366e29 2022-07-07 mark v->ncols = COLS;
1376 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1377 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1379 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1381 d2366e29 2022-07-07 mark return err;
1383 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1384 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1385 d2366e29 2022-07-07 mark v->focus_child = 1;
1387 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1389 d2366e29 2022-07-07 mark return err;
1390 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1392 d2366e29 2022-07-07 mark return err;
1394 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1395 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1396 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1397 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1399 279d2047 2022-07-29 stsp return err;
1400 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1402 279d2047 2022-07-29 stsp return err;
1404 7532ccda 2022-07-11 mark offset_selection_up(v);
1405 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1407 dff91825 2022-07-22 mark if (v->resize)
1408 dff91825 2022-07-22 mark err = v->resize(v, 0);
1409 dff91825 2022-07-22 mark else if (v->child->resize)
1410 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1412 d2366e29 2022-07-07 mark return err;
1416 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1417 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1419 640cd7ff 2022-06-22 mark static int
1420 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1422 9b058f45 2022-06-30 mark struct tog_view *v = view;
1423 9b058f45 2022-06-30 mark int x, n = 0;
1425 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1426 9b058f45 2022-06-30 mark v = view->child;
1427 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1428 9b058f45 2022-06-30 mark v = view->parent;
1430 640cd7ff 2022-06-22 mark view->count = 0;
1431 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1432 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1433 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1434 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1435 9b058f45 2022-06-30 mark waddch(v->window, ':');
1438 9b058f45 2022-06-30 mark x = getcurx(v->window);
1439 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1440 9b058f45 2022-06-30 mark waddch(v->window, c);
1441 9b058f45 2022-06-30 mark wrefresh(v->window);
1445 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1446 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1448 640cd7ff 2022-06-22 mark if (n >= 9999999)
1449 640cd7ff 2022-06-22 mark n = 9999999;
1451 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1452 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1454 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1455 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1460 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1461 640cd7ff 2022-06-22 mark view->count = n;
1466 3f6c6614 2023-01-23 mark static void
1467 3f6c6614 2023-01-23 mark action_report(struct tog_view *view)
1469 3f6c6614 2023-01-23 mark struct tog_view *v = view;
1471 3f6c6614 2023-01-23 mark if (view_is_hsplit_top(view))
1472 3f6c6614 2023-01-23 mark v = view->child;
1473 3f6c6614 2023-01-23 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1474 3f6c6614 2023-01-23 mark v = view->parent;
1476 3f6c6614 2023-01-23 mark wmove(v->window, v->nlines - 1, 0);
1477 3f6c6614 2023-01-23 mark wclrtoeol(v->window);
1478 3f6c6614 2023-01-23 mark wprintw(v->window, ":%s", view->action);
1479 3f6c6614 2023-01-23 mark wrefresh(v->window);
1482 3f6c6614 2023-01-23 mark * Clear action status report. Only clear in blame view
1483 3f6c6614 2023-01-23 mark * once annotating is complete, otherwise it's too fast.
1485 3f6c6614 2023-01-23 mark if (view->type == TOG_VIEW_BLAME) {
1486 3f6c6614 2023-01-23 mark if (view->state.blame.blame_complete)
1487 3f6c6614 2023-01-23 mark view->action = NULL;
1489 3f6c6614 2023-01-23 mark view->action = NULL;
1492 0cf4efb1 2018-09-29 stsp static const struct got_error *
1493 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1494 602eda79 2023-02-09 mark struct tog_view_list_head *views, int fast_refresh)
1496 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1497 669b5ffa 2018-10-07 stsp struct tog_view *v;
1498 1a76625f 2018-10-22 stsp int ch, errcode;
1500 e5a0f69f 2018-08-18 stsp *new = NULL;
1502 3f6c6614 2023-01-23 mark if (view->action)
1503 3f6c6614 2023-01-23 mark action_report(view);
1505 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1506 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1507 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1508 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1509 640cd7ff 2022-06-22 mark view->count = 0;
1512 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1513 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1514 82954512 2020-02-03 stsp if (errcode)
1515 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1516 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1517 3da8ef85 2021-09-21 stsp sched_yield();
1518 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1519 82954512 2020-02-03 stsp if (errcode)
1520 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1521 82954512 2020-02-03 stsp "pthread_mutex_lock");
1522 60493ae3 2019-06-20 stsp view->search_next(view);
1523 60493ae3 2019-06-20 stsp return NULL;
1526 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1527 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1528 1a76625f 2018-10-22 stsp if (errcode)
1529 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1530 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1531 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1533 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1534 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1535 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1536 a6d37fac 2022-07-03 mark view->count = 0;
1538 a6d37fac 2022-07-03 mark ch = view->ch;
1540 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1541 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1542 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1544 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1545 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1546 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1547 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1548 1a76625f 2018-10-22 stsp if (errcode)
1549 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1551 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1552 25791caa 2018-10-24 stsp tog_resizeterm();
1553 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1554 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1555 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1556 25791caa 2018-10-24 stsp err = view_resize(v);
1558 25791caa 2018-10-24 stsp return err;
1559 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1561 25791caa 2018-10-24 stsp return err;
1562 cdfcfb03 2020-12-06 stsp if (v->child) {
1563 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1565 cdfcfb03 2020-12-06 stsp return err;
1566 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1567 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1569 cdfcfb03 2020-12-06 stsp return err;
1570 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1571 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1573 3c1dfe12 2022-07-08 mark return err;
1579 e5a0f69f 2018-08-18 stsp switch (ch) {
1582 ec2a9698 2022-09-15 mark case KEY_F(1):
1583 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1584 ec2a9698 2022-09-15 mark err = view->reset(view);
1586 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1589 640cd7ff 2022-06-22 mark view->count = 0;
1590 1e37a5c2 2019-05-12 jcs if (view->child) {
1591 e78dc838 2020-12-04 stsp view->focussed = 0;
1592 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1593 e78dc838 2020-12-04 stsp view->focus_child = 1;
1594 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1595 e78dc838 2020-12-04 stsp view->focussed = 0;
1596 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1597 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1598 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1599 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1600 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1603 9b058f45 2022-06-30 mark return err;
1605 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1606 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1608 9b058f45 2022-06-30 mark return err;
1613 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1614 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1615 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1616 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1620 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1622 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1623 9970f7fc 2020-12-03 stsp view->dying = 1;
1629 640cd7ff 2022-06-22 mark view->count = 0;
1630 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1631 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1633 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1634 e78dc838 2020-12-04 stsp view->focussed = 0;
1635 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1636 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1638 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1640 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1644 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1645 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1647 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1648 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1649 e78dc838 2020-12-04 stsp view->focussed = 1;
1650 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1652 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1653 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1654 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1656 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1660 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1664 6fe51fee 2022-07-22 mark if (view->resize) {
1665 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1669 9b058f45 2022-06-30 mark if (view->parent)
1670 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1672 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1675 3c1dfe12 2022-07-08 mark view->count = 0;
1676 d2366e29 2022-07-07 mark err = switch_split(view);
1679 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1682 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1684 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1687 640cd7ff 2022-06-22 mark view->count = 0;
1688 60493ae3 2019-06-20 stsp if (view->search_start)
1689 602eda79 2023-02-09 mark view_search_start(view, fast_refresh);
1691 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1695 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1696 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1697 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1698 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1699 60493ae3 2019-06-20 stsp view->search_next(view);
1701 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1704 3f6c6614 2023-01-23 mark if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1705 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1706 3f6c6614 2023-01-23 mark view->action = "Patience diff algorithm";
1708 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1709 3f6c6614 2023-01-23 mark view->action = "Myers diff algorithm";
1711 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1712 917d79a7 2022-07-01 stsp if (v->reset) {
1713 917d79a7 2022-07-01 stsp err = v->reset(v);
1715 917d79a7 2022-07-01 stsp return err;
1717 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1718 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1720 917d79a7 2022-07-01 stsp return err;
1725 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1729 e5a0f69f 2018-08-18 stsp return err;
1733 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1735 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1736 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1738 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1740 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1743 669b5ffa 2018-10-07 stsp return view->focussed;
1746 bcbd79e2 2018-08-19 stsp static const struct got_error *
1747 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1749 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1750 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1751 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1752 d2366e29 2022-07-07 mark char *mode;
1753 fd823528 2018-10-22 stsp int fast_refresh = 10;
1754 1a76625f 2018-10-22 stsp int done = 0, errcode;
1756 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1757 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1758 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1760 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1762 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1763 1a76625f 2018-10-22 stsp if (errcode)
1764 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1766 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1767 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1769 1004088d 2018-09-29 stsp view->focussed = 1;
1770 878940b7 2018-09-29 stsp err = view->show(view);
1772 0cf4efb1 2018-09-29 stsp return err;
1773 0cf4efb1 2018-09-29 stsp update_panels();
1774 0cf4efb1 2018-09-29 stsp doupdate();
1775 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1776 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1777 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1778 388b24d6 2023-01-23 mark if (fast_refresh && --fast_refresh == 0)
1779 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1781 602eda79 2023-02-09 mark err = view_input(&new_view, &done, view, &views, fast_refresh);
1785 dc2c3344 2023-01-23 mark if (view->dying && view == TAILQ_FIRST(&views) &&
1786 dc2c3344 2023-01-23 mark TAILQ_NEXT(view, entry) == NULL)
1788 dc2c3344 2023-01-23 mark if (done) {
1789 dc2c3344 2023-01-23 mark struct tog_view *v;
1792 dc2c3344 2023-01-23 mark * When we quit, scroll the screen up a single line
1793 dc2c3344 2023-01-23 mark * so we don't lose any information.
1795 dc2c3344 2023-01-23 mark TAILQ_FOREACH(v, &views, entry) {
1796 dc2c3344 2023-01-23 mark wmove(v->window, 0, 0);
1797 dc2c3344 2023-01-23 mark wdeleteln(v->window);
1798 dc2c3344 2023-01-23 mark wnoutrefresh(v->window);
1799 dc2c3344 2023-01-23 mark if (v->child && !view_is_fullscreen(v)) {
1800 dc2c3344 2023-01-23 mark wmove(v->child->window, 0, 0);
1801 dc2c3344 2023-01-23 mark wdeleteln(v->child->window);
1802 dc2c3344 2023-01-23 mark wnoutrefresh(v->child->window);
1805 dc2c3344 2023-01-23 mark doupdate();
1808 9970f7fc 2020-12-03 stsp if (view->dying) {
1809 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1811 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1812 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1814 e78dc838 2020-12-04 stsp else if (view->parent)
1815 669b5ffa 2018-10-07 stsp prev = view->parent;
1817 e78dc838 2020-12-04 stsp if (view->parent) {
1818 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1819 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1820 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1821 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1822 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1825 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1826 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1828 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1830 9970f7fc 2020-12-03 stsp err = view_close(view);
1832 e5a0f69f 2018-08-18 stsp goto done;
1834 e78dc838 2020-12-04 stsp view = NULL;
1835 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1836 e78dc838 2020-12-04 stsp if (v->focussed)
1839 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1840 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1842 e78dc838 2020-12-04 stsp view = prev;
1843 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1844 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1845 e78dc838 2020-12-04 stsp tog_view_list_head);
1847 e78dc838 2020-12-04 stsp if (view) {
1848 e78dc838 2020-12-04 stsp if (view->focus_child) {
1849 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1850 e78dc838 2020-12-04 stsp view = view->child;
1852 e78dc838 2020-12-04 stsp view->focussed = 1;
1856 bcbd79e2 2018-08-19 stsp if (new_view) {
1857 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1858 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1859 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1860 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1862 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1863 86c66b02 2018-10-18 stsp err = view_close(v);
1865 86c66b02 2018-10-18 stsp goto done;
1868 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1869 fed7eaa8 2018-10-24 stsp view = new_view;
1871 dc2c3344 2023-01-23 mark if (view && !done) {
1872 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1873 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1874 e78dc838 2020-12-04 stsp view = view->child;
1876 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1877 e78dc838 2020-12-04 stsp view = view->parent;
1879 e78dc838 2020-12-04 stsp show_panel(view->panel);
1880 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1881 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1882 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1883 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1885 1a76625f 2018-10-22 stsp goto done;
1887 669b5ffa 2018-10-07 stsp err = view->show(view);
1889 1a76625f 2018-10-22 stsp goto done;
1890 669b5ffa 2018-10-07 stsp if (view->child) {
1891 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1893 1a76625f 2018-10-22 stsp goto done;
1895 1a76625f 2018-10-22 stsp update_panels();
1896 1a76625f 2018-10-22 stsp doupdate();
1900 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1901 5629093a 2022-07-11 stsp const struct got_error *close_err;
1902 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1903 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1904 5629093a 2022-07-11 stsp close_err = view_close(view);
1905 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1906 5629093a 2022-07-11 stsp err = close_err;
1909 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1910 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1911 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1913 e5a0f69f 2018-08-18 stsp return err;
1916 4ed7e80c 2018-05-20 stsp __dead static void
1917 9f7d7167 2018-04-29 stsp usage_log(void)
1920 c70c5802 2018-08-01 stsp fprintf(stderr,
1921 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1922 9f7d7167 2018-04-29 stsp getprogname());
1926 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1927 80ddbec8 2018-04-29 stsp static const struct got_error *
1928 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1930 00dfcb92 2018-06-11 stsp char *vis = NULL;
1931 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1933 963b370f 2018-05-20 stsp *ws = NULL;
1934 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1935 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1936 00dfcb92 2018-06-11 stsp int vislen;
1937 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1938 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1940 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1941 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1943 00dfcb92 2018-06-11 stsp return err;
1944 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1945 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1946 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1947 a7f50699 2018-06-11 stsp goto done;
1951 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1952 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1953 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1954 a7f50699 2018-06-11 stsp goto done;
1957 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1958 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1960 00dfcb92 2018-06-11 stsp free(vis);
1961 963b370f 2018-05-20 stsp if (err) {
1962 963b370f 2018-05-20 stsp free(*ws);
1963 963b370f 2018-05-20 stsp *ws = NULL;
1964 963b370f 2018-05-20 stsp *wlen = 0;
1966 963b370f 2018-05-20 stsp return err;
1969 145b6838 2022-06-16 stsp static const struct got_error *
1970 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1972 145b6838 2022-06-16 stsp char *dst;
1973 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1975 145b6838 2022-06-16 stsp *ptr = NULL;
1976 145b6838 2022-06-16 stsp n = len = strlen(src);
1977 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1978 145b6838 2022-06-16 stsp if (dst == NULL)
1979 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1981 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1982 145b6838 2022-06-16 stsp const char c = src[idx];
1984 145b6838 2022-06-16 stsp if (c == '\t') {
1985 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1988 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1989 6e1c41ad 2022-06-16 mark if (p == NULL) {
1990 6e1c41ad 2022-06-16 mark free(dst);
1991 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1996 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1999 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
2003 145b6838 2022-06-16 stsp dst[sz] = '\0';
2004 145b6838 2022-06-16 stsp *ptr = dst;
2005 145b6838 2022-06-16 stsp return NULL;
2009 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
2010 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
2011 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
2015 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2017 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
2019 4e4a9ac8 2022-06-17 op if (n == 0) {
2020 4e4a9ac8 2022-06-17 op *rcol = cols;
2024 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
2025 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
2026 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2028 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
2030 4e4a9ac8 2022-06-17 op if (width == -1) {
2032 4e4a9ac8 2022-06-17 op wline[i] = L'.';
2035 4e4a9ac8 2022-06-17 op if (cols + width > n)
2037 4e4a9ac8 2022-06-17 op cols += width;
2040 4e4a9ac8 2022-06-17 op *rcol = cols;
2045 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
2046 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
2047 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2049 ccda2f4d 2022-06-16 stsp static const struct got_error *
2050 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2051 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2053 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2055 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2056 145b6838 2022-06-16 stsp char *exstr = NULL;
2057 963b370f 2018-05-20 stsp size_t wlen;
2058 4e4a9ac8 2022-06-17 op int i, scrollx;
2060 963b370f 2018-05-20 stsp *wlinep = NULL;
2061 b700b5d6 2018-07-10 stsp *widthp = 0;
2063 145b6838 2022-06-16 stsp if (expand) {
2064 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2066 145b6838 2022-06-16 stsp return err;
2069 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2070 145b6838 2022-06-16 stsp free(exstr);
2072 963b370f 2018-05-20 stsp return err;
2074 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2076 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2077 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2080 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2081 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2085 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2086 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2088 b700b5d6 2018-07-10 stsp if (widthp)
2089 b700b5d6 2018-07-10 stsp *widthp = cols;
2090 ccda2f4d 2022-06-16 stsp if (scrollxp)
2091 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2093 963b370f 2018-05-20 stsp free(wline);
2095 963b370f 2018-05-20 stsp *wlinep = wline;
2096 963b370f 2018-05-20 stsp return err;
2099 8b473291 2019-02-21 stsp static const struct got_error*
2100 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2101 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2103 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2104 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2106 8b473291 2019-02-21 stsp const char *name;
2108 8b473291 2019-02-21 stsp *refs_str = NULL;
2110 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2111 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2112 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2115 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2116 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2118 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2119 8b473291 2019-02-21 stsp name += 5;
2120 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2121 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2123 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2124 8b473291 2019-02-21 stsp name += 6;
2125 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2126 8b473291 2019-02-21 stsp name += 8;
2127 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2128 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2131 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2134 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2135 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2136 5d844a1e 2019-08-13 stsp if (err) {
2137 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2138 48cae60d 2020-09-22 stsp free(ref_id);
2141 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2142 5d844a1e 2019-08-13 stsp err = NULL;
2143 5d844a1e 2019-08-13 stsp tag = NULL;
2146 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2147 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2148 48cae60d 2020-09-22 stsp free(ref_id);
2150 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2151 52b5abe1 2019-08-13 stsp if (cmp != 0)
2153 8b473291 2019-02-21 stsp s = *refs_str;
2154 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2155 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2156 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2158 8b473291 2019-02-21 stsp *refs_str = NULL;
2164 8b473291 2019-02-21 stsp return err;
2167 963b370f 2018-05-20 stsp static const struct got_error *
2168 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2169 27a741e5 2019-09-11 stsp int col_tab_align)
2171 e6b8b890 2020-12-29 naddy char *smallerthan;
2173 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2174 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2175 5813d178 2019-03-09 stsp author = smallerthan + 1;
2176 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2177 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2178 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2181 5813d178 2019-03-09 stsp static const struct got_error *
2182 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2183 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2184 8fdc79fe 2020-12-01 naddy int author_display_cols)
2186 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2187 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2188 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2189 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2190 5813d178 2019-03-09 stsp char *author = NULL;
2191 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2192 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2193 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2194 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2195 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2196 ccb26ccd 2018-11-05 stsp struct tm tm;
2197 45d799e2 2018-12-23 stsp time_t committer_time;
2198 11b20872 2019-11-08 stsp struct tog_color *tc;
2200 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2201 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2202 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2203 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2204 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2206 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2207 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2209 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2210 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2212 11b20872 2019-11-08 stsp wattr_on(view->window,
2213 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2214 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2216 11b20872 2019-11-08 stsp wattr_off(view->window,
2217 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2218 27a741e5 2019-09-11 stsp col = limit;
2219 b39d25c7 2018-07-10 stsp if (col > avail)
2220 b39d25c7 2018-07-10 stsp goto done;
2222 6570a66d 2019-11-08 stsp if (avail >= 120) {
2223 6570a66d 2019-11-08 stsp char *id_str;
2224 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2226 6570a66d 2019-11-08 stsp goto done;
2227 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2229 11b20872 2019-11-08 stsp wattr_on(view->window,
2230 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2231 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2233 11b20872 2019-11-08 stsp wattr_off(view->window,
2234 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2235 6570a66d 2019-11-08 stsp free(id_str);
2237 6570a66d 2019-11-08 stsp if (col > avail)
2238 6570a66d 2019-11-08 stsp goto done;
2241 10aab77f 2022-07-19 op if (s->use_committer)
2242 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2244 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2245 5813d178 2019-03-09 stsp if (author == NULL) {
2246 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2247 80ddbec8 2018-04-29 stsp goto done;
2249 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2251 bb737323 2018-05-20 stsp goto done;
2252 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2254 11b20872 2019-11-08 stsp wattr_on(view->window,
2255 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2256 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2257 bb737323 2018-05-20 stsp col += author_width;
2258 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2259 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2261 bb737323 2018-05-20 stsp author_width++;
2264 f0f62654 2022-09-08 mark wattr_off(view->window,
2265 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2266 9c2eaf34 2018-05-20 stsp if (col > avail)
2267 9c2eaf34 2018-05-20 stsp goto done;
2269 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2271 6d9fbc00 2018-04-29 stsp goto done;
2272 bb737323 2018-05-20 stsp logmsg = logmsg0;
2273 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2275 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2276 bb737323 2018-05-20 stsp if (newline)
2277 bb737323 2018-05-20 stsp *newline = '\0';
2278 ccda2f4d 2022-06-16 stsp limit = avail - col;
2279 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2280 4d1f6af3 2022-06-17 op limit--; /* for the border */
2281 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2282 ccda2f4d 2022-06-16 stsp limit, col, 1);
2284 29688b02 2022-06-16 stsp goto done;
2285 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2286 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2287 27a741e5 2019-09-11 stsp while (col < avail) {
2288 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2292 80ddbec8 2018-04-29 stsp free(logmsg0);
2293 bb737323 2018-05-20 stsp free(wlogmsg);
2294 5813d178 2019-03-09 stsp free(author);
2295 bb737323 2018-05-20 stsp free(wauthor);
2296 80ddbec8 2018-04-29 stsp free(line);
2297 80ddbec8 2018-04-29 stsp return err;
2300 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2301 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2302 899d86c2 2018-05-10 stsp struct got_object_id *id)
2304 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2305 e15c42de 2022-09-05 op struct got_object_id *dup;
2307 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2308 80ddbec8 2018-04-29 stsp if (entry == NULL)
2309 899d86c2 2018-05-10 stsp return NULL;
2311 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2312 e15c42de 2022-09-05 op if (dup == NULL) {
2313 e15c42de 2022-09-05 op free(entry);
2314 e15c42de 2022-09-05 op return NULL;
2317 e15c42de 2022-09-05 op entry->id = dup;
2318 99db9666 2018-05-07 stsp entry->commit = commit;
2319 899d86c2 2018-05-10 stsp return entry;
2322 99db9666 2018-05-07 stsp static void
2323 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2325 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2327 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2328 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2329 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2330 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2331 e15c42de 2022-09-05 op free(entry->id);
2332 99db9666 2018-05-07 stsp free(entry);
2335 99db9666 2018-05-07 stsp static void
2336 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2338 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2339 99db9666 2018-05-07 stsp pop_commit(commits);
2342 c4972b91 2018-05-07 stsp static const struct got_error *
2343 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2344 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2346 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2347 13add988 2019-10-15 stsp regmatch_t regmatch;
2348 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2350 13add988 2019-10-15 stsp *have_match = 0;
2352 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2354 13add988 2019-10-15 stsp return err;
2356 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2358 13add988 2019-10-15 stsp goto done;
2360 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2361 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2362 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2363 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2364 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2365 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2366 13add988 2019-10-15 stsp *have_match = 1;
2368 13add988 2019-10-15 stsp free(id_str);
2369 13add988 2019-10-15 stsp free(logmsg);
2370 13add988 2019-10-15 stsp return err;
2373 13add988 2019-10-15 stsp static const struct got_error *
2374 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2376 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2379 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2380 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2381 1a76625f 2018-10-22 stsp * while updating the display.
2384 d9787ed8 2022-09-10 op struct got_object_id id;
2385 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2386 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2387 568eae95 2022-09-11 mark int limit_match = 0;
2388 1a76625f 2018-10-22 stsp int errcode;
2390 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2391 4e0d2870 2020-12-07 naddy NULL, NULL);
2395 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2398 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2399 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2400 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2404 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2405 1a76625f 2018-10-22 stsp if (errcode) {
2406 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2407 13add988 2019-10-15 stsp "pthread_mutex_lock");
2411 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2412 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2413 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2415 568eae95 2022-09-11 mark if (*a->limiting) {
2416 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2417 568eae95 2022-09-11 mark a->limit_regex);
2421 568eae95 2022-09-11 mark if (limit_match) {
2422 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2424 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2425 568eae95 2022-09-11 mark entry->commit, entry->id);
2426 568eae95 2022-09-11 mark if (matched == NULL) {
2427 568eae95 2022-09-11 mark err = got_error_from_errno(
2428 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2431 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2432 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2434 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2435 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2436 568eae95 2022-09-11 mark matched, entry);
2437 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2441 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2442 568eae95 2022-09-11 mark * have found a match, and that it should be
2443 568eae95 2022-09-11 mark * counted as a new entry for the view.
2445 568eae95 2022-09-11 mark a->limit_match = limit_match;
2448 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2449 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2450 7c1452c1 2020-03-26 stsp int have_match;
2451 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2455 568eae95 2022-09-11 mark if (*a->limiting) {
2456 568eae95 2022-09-11 mark if (limit_match && have_match)
2457 568eae95 2022-09-11 mark *a->search_next_done =
2458 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2459 568eae95 2022-09-11 mark } else if (have_match)
2460 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2463 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2464 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2465 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2466 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2469 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2471 9ba79e04 2018-06-11 stsp return err;
2474 2b779855 2020-12-05 naddy static void
2475 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2477 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2478 2b779855 2020-12-05 naddy int ncommits = 0;
2480 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2481 2b779855 2020-12-05 naddy while (entry) {
2482 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2483 2b779855 2020-12-05 naddy s->selected_entry = entry;
2486 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2487 2b779855 2020-12-05 naddy ncommits++;
2491 0553a4e3 2018-04-30 stsp static const struct got_error *
2492 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2494 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2495 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2496 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2497 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2498 60493ae3 2019-06-20 stsp int width;
2499 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2500 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2501 8b473291 2019-02-21 stsp char *refs_str = NULL;
2502 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2503 11b20872 2019-11-08 stsp struct tog_color *tc;
2504 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2506 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2507 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2509 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2510 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2511 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2512 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2514 ecb28ae0 2018-07-16 stsp return err;
2515 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2516 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2517 d2075bf3 2020-12-25 stsp if (refs) {
2518 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2519 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2521 d2075bf3 2020-12-25 stsp goto done;
2525 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2526 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2528 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2529 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2530 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2531 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2532 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2533 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2534 8f4ed634 2020-03-26 stsp goto done;
2537 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2538 568eae95 2022-09-11 mark const char *limit_str = NULL;
2540 f9686aa5 2020-03-27 stsp if (view->searching) {
2541 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2542 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2543 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2544 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2545 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2546 f9686aa5 2020-03-27 stsp search_str = "searching...";
2549 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2550 568eae95 2022-09-11 mark limit_str = "no matches found";
2552 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2553 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2554 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2555 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2556 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2557 8f4ed634 2020-03-26 stsp goto done;
2561 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2562 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2563 87411fa9 2022-06-16 stsp "........................................",
2564 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2566 1a76625f 2018-10-22 stsp header = NULL;
2567 1a76625f 2018-10-22 stsp goto done;
2569 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2570 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2571 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2572 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2573 1a76625f 2018-10-22 stsp header = NULL;
2574 1a76625f 2018-10-22 stsp goto done;
2576 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2578 1a76625f 2018-10-22 stsp goto done;
2580 2814baeb 2018-08-01 stsp werase(view->window);
2582 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2583 a3404814 2018-09-02 stsp wstandout(view->window);
2584 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2586 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2587 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2588 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2589 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2593 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2594 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2595 a3404814 2018-09-02 stsp wstandend(view->window);
2596 ecb28ae0 2018-07-16 stsp free(wline);
2597 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2598 1a76625f 2018-10-22 stsp goto done;
2600 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2601 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2602 5813d178 2019-03-09 stsp ncommits = 0;
2603 145b6838 2022-06-16 stsp view->maxx = 0;
2604 5813d178 2019-03-09 stsp while (entry) {
2605 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2606 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2607 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2608 5813d178 2019-03-09 stsp int width;
2609 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2611 10aab77f 2022-07-19 op if (s->use_committer)
2612 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2614 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2615 5813d178 2019-03-09 stsp if (author == NULL) {
2616 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2617 5813d178 2019-03-09 stsp goto done;
2619 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2620 27a741e5 2019-09-11 stsp date_display_cols);
2621 5813d178 2019-03-09 stsp if (author_cols < width)
2622 5813d178 2019-03-09 stsp author_cols = width;
2623 5813d178 2019-03-09 stsp free(wauthor);
2624 5813d178 2019-03-09 stsp free(author);
2625 a310d9c3 2022-07-21 florian if (err)
2626 a310d9c3 2022-07-21 florian goto done;
2627 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2629 145b6838 2022-06-16 stsp goto done;
2630 145b6838 2022-06-16 stsp msg = msg0;
2631 145b6838 2022-06-16 stsp while (*msg == '\n')
2633 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2634 29688b02 2022-06-16 stsp *eol = '\0';
2635 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2636 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2638 29688b02 2022-06-16 stsp goto done;
2639 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2640 145b6838 2022-06-16 stsp free(msg0);
2641 29688b02 2022-06-16 stsp free(wmsg);
2642 7ca04879 2019-10-19 stsp ncommits++;
2643 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2646 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2647 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2648 867c6645 2018-07-10 stsp ncommits = 0;
2649 899d86c2 2018-05-10 stsp while (entry) {
2650 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2652 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2653 2814baeb 2018-08-01 stsp wstandout(view->window);
2654 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2655 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2656 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2657 2814baeb 2018-08-01 stsp wstandend(view->window);
2659 60493ae3 2019-06-20 stsp goto done;
2660 0553a4e3 2018-04-30 stsp ncommits++;
2661 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2662 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2665 9b058f45 2022-06-30 mark view_border(view);
2667 1a76625f 2018-10-22 stsp free(id_str);
2668 8b473291 2019-02-21 stsp free(refs_str);
2669 1a76625f 2018-10-22 stsp free(ncommits_str);
2670 1a76625f 2018-10-22 stsp free(header);
2671 80ddbec8 2018-04-29 stsp return err;
2674 07b55e75 2018-05-10 stsp static void
2675 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2677 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2678 07b55e75 2018-05-10 stsp int nscrolled = 0;
2680 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2681 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2684 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2685 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2686 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2687 07b55e75 2018-05-10 stsp if (entry) {
2688 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2689 07b55e75 2018-05-10 stsp nscrolled++;
2694 aa075928 2018-05-10 stsp static const struct got_error *
2695 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2697 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2698 5e224a3e 2019-02-22 stsp int errcode;
2700 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2702 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2703 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2704 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2705 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2706 7aafa0d1 2019-02-22 stsp if (errcode)
2707 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2708 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2711 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2712 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2714 7c1452c1 2020-03-26 stsp if (!wait)
2717 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2718 ffe38506 2020-12-01 naddy show_log_view(view);
2719 7c1452c1 2020-03-26 stsp update_panels();
2720 7c1452c1 2020-03-26 stsp doupdate();
2722 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2723 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2724 82954512 2020-02-03 stsp if (errcode)
2725 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2726 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2728 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2729 ffe38506 2020-12-01 naddy show_log_view(view);
2730 7c1452c1 2020-03-26 stsp update_panels();
2731 7c1452c1 2020-03-26 stsp doupdate();
2734 5e224a3e 2019-02-22 stsp return NULL;
2737 5e224a3e 2019-02-22 stsp static const struct got_error *
2738 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2740 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2741 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2743 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2744 2525dccb 2022-07-13 mark return NULL;
2746 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2747 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2748 9b058f45 2022-06-30 mark view->nscrolled = 0;
2750 9b058f45 2022-06-30 mark return err;
2753 9b058f45 2022-06-30 mark static const struct got_error *
2754 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2756 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2757 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2758 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2759 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2761 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2762 5e224a3e 2019-02-22 stsp return NULL;
2764 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2765 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2766 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2768 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2770 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2771 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2772 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2774 5e224a3e 2019-02-22 stsp return err;
2778 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2779 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2782 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2783 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
2785 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2786 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2788 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2789 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2791 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2792 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2794 9b058f45 2022-06-30 mark view->nscrolled = 0;
2796 dd0a52c1 2018-05-20 stsp return err;
2799 cd0acaa7 2018-05-20 stsp static const struct got_error *
2800 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2801 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2802 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2804 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2805 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2806 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2808 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2809 15a94983 2018-12-23 stsp if (diff_view == NULL)
2810 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2812 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2813 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2814 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2815 e5a0f69f 2018-08-18 stsp if (err == NULL)
2816 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2817 cd0acaa7 2018-05-20 stsp return err;
2820 80ddbec8 2018-04-29 stsp static const struct got_error *
2821 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2822 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2824 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2826 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2827 941e9f74 2019-05-21 stsp if (parent == NULL)
2828 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2830 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2831 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2832 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2833 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2834 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2835 941e9f74 2019-05-21 stsp s->tree = subtree;
2836 941e9f74 2019-05-21 stsp s->selected = 0;
2837 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2838 941e9f74 2019-05-21 stsp return NULL;
2841 941e9f74 2019-05-21 stsp static const struct got_error *
2842 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2843 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2845 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2846 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2847 941e9f74 2019-05-21 stsp const char *p;
2848 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2850 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2852 941e9f74 2019-05-21 stsp while (*p) {
2853 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2854 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2855 56e0773d 2019-11-28 stsp char *te_name;
2857 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2860 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2861 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2862 941e9f74 2019-05-21 stsp if (slash == NULL)
2863 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2865 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2866 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2867 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2870 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2871 56e0773d 2019-11-28 stsp if (te == NULL) {
2872 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2873 56e0773d 2019-11-28 stsp free(te_name);
2876 56e0773d 2019-11-28 stsp free(te_name);
2877 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2879 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2880 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2882 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2883 941e9f74 2019-05-21 stsp if (slash)
2884 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2886 941e9f74 2019-05-21 stsp subpath = strdup(path);
2887 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2888 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2892 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2897 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2898 941e9f74 2019-05-21 stsp free(tree_id);
2902 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2903 941e9f74 2019-05-21 stsp if (err) {
2904 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2907 941e9f74 2019-05-21 stsp if (slash == NULL)
2909 941e9f74 2019-05-21 stsp free(subpath);
2910 941e9f74 2019-05-21 stsp subpath = NULL;
2911 941e9f74 2019-05-21 stsp p = slash;
2914 941e9f74 2019-05-21 stsp free(subpath);
2915 1a76625f 2018-10-22 stsp return err;
2918 61266923 2020-01-14 stsp static const struct got_error *
2919 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2920 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2921 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2923 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2924 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2925 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2927 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2928 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2929 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2931 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2933 55cccc34 2020-02-20 stsp return err;
2934 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2936 55cccc34 2020-02-20 stsp *new_view = tree_view;
2938 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2939 55cccc34 2020-02-20 stsp return NULL;
2941 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2944 55cccc34 2020-02-20 stsp static const struct got_error *
2945 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2947 61266923 2020-01-14 stsp sigset_t sigset;
2948 61266923 2020-01-14 stsp int errcode;
2950 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2951 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2953 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2954 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2955 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2956 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2957 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2958 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2959 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2960 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2961 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2963 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2964 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2965 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2967 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2968 61266923 2020-01-14 stsp if (errcode)
2969 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2971 61266923 2020-01-14 stsp return NULL;
2974 1a76625f 2018-10-22 stsp static void *
2975 1a76625f 2018-10-22 stsp log_thread(void *arg)
2977 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2978 1a76625f 2018-10-22 stsp int errcode = 0;
2979 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2980 1a76625f 2018-10-22 stsp int done = 0;
2983 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2984 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2986 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2987 5629093a 2022-07-11 stsp if (errcode) {
2988 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2989 61266923 2020-01-14 stsp return (void *)err;
2992 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2993 5629093a 2022-07-11 stsp if (err) {
2994 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2995 5629093a 2022-07-11 stsp goto done;
2998 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2999 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3000 5629093a 2022-07-11 stsp if (errcode) {
3001 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
3002 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
3003 5629093a 2022-07-11 stsp goto done;
3005 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3006 1a76625f 2018-10-22 stsp if (err) {
3007 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3008 5629093a 2022-07-11 stsp goto done;
3009 1a76625f 2018-10-22 stsp err = NULL;
3011 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
3012 568eae95 2022-09-11 mark if (*a->limiting) {
3013 568eae95 2022-09-11 mark if (a->limit_match)
3014 568eae95 2022-09-11 mark a->commits_needed--;
3016 568eae95 2022-09-11 mark a->commits_needed--;
3019 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3020 3abe8080 2019-04-10 stsp if (errcode) {
3021 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3022 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3023 5629093a 2022-07-11 stsp goto done;
3024 3abe8080 2019-04-10 stsp } else if (*a->quit)
3026 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
3027 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3028 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
3029 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3030 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
3031 568eae95 2022-09-11 mark *a->first_displayed_entry =
3032 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
3033 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
3036 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3037 7c1452c1 2020-03-26 stsp if (errcode) {
3038 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3039 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3040 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3041 5629093a 2022-07-11 stsp goto done;
3045 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3047 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3048 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3049 7c1452c1 2020-03-26 stsp &tog_mutex);
3050 5629093a 2022-07-11 stsp if (errcode) {
3051 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3052 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3053 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
3054 5629093a 2022-07-11 stsp goto done;
3056 21355643 2020-12-06 stsp if (*a->quit)
3061 3abe8080 2019-04-10 stsp a->log_complete = 1;
3062 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3063 5629093a 2022-07-11 stsp if (errcode)
3064 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3066 5629093a 2022-07-11 stsp if (err) {
3067 5629093a 2022-07-11 stsp tog_thread_error = 1;
3068 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3070 1a76625f 2018-10-22 stsp return (void *)err;
3073 1a76625f 2018-10-22 stsp static const struct got_error *
3074 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3076 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3077 1a76625f 2018-10-22 stsp int errcode;
3079 1a76625f 2018-10-22 stsp if (s->thread) {
3080 1a76625f 2018-10-22 stsp s->quit = 1;
3081 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3082 1a76625f 2018-10-22 stsp if (errcode)
3083 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3084 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3085 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3086 1a76625f 2018-10-22 stsp if (errcode)
3087 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3088 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3089 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3090 1a76625f 2018-10-22 stsp if (errcode)
3091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3092 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3093 1a76625f 2018-10-22 stsp if (errcode)
3094 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3095 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3096 1a76625f 2018-10-22 stsp s->thread = NULL;
3099 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3100 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3101 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3104 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3105 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3106 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3107 74467cc8 2022-06-15 stsp if (err == NULL)
3108 74467cc8 2022-06-15 stsp err = pack_err;
3109 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3112 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3113 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3114 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3117 5629093a 2022-07-11 stsp return err ? err : thread_err;
3120 9343a5fb 2018-06-23 stsp static const struct got_error *
3121 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3123 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3124 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3125 276b94a1 2020-11-13 naddy int errcode;
3127 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3129 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3130 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3131 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3133 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3134 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3135 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3137 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3138 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3139 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3140 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3141 1a76625f 2018-10-22 stsp free(s->start_id);
3142 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3143 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3144 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3145 1a76625f 2018-10-22 stsp return err;
3149 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3150 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3151 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3152 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3153 568eae95 2022-09-11 mark * works with very slight change.
3155 1a76625f 2018-10-22 stsp static const struct got_error *
3156 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3158 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3159 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3160 568eae95 2022-09-11 mark struct tog_view *v = view;
3161 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3162 568eae95 2022-09-11 mark char pattern[1024];
3165 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3166 568eae95 2022-09-11 mark v = view->child;
3167 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3168 568eae95 2022-09-11 mark v = view->parent;
3170 568eae95 2022-09-11 mark /* Get the pattern */
3171 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3172 568eae95 2022-09-11 mark wclrtoeol(v->window);
3173 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3174 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3175 568eae95 2022-09-11 mark nocbreak();
3177 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3180 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3181 568eae95 2022-09-11 mark if (ret == ERR)
3182 568eae95 2022-09-11 mark return NULL;
3184 568eae95 2022-09-11 mark if (*pattern == '\0') {
3186 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3187 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3189 568eae95 2022-09-11 mark if (!s->limit_view)
3190 568eae95 2022-09-11 mark return NULL;
3193 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3194 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3195 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3196 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3197 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3198 568eae95 2022-09-11 mark * the queue.
3200 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3201 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3202 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3203 568eae95 2022-09-11 mark s->selected = 0;
3204 568eae95 2022-09-11 mark s->limit_view = 0;
3206 568eae95 2022-09-11 mark return NULL;
3209 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3210 568eae95 2022-09-11 mark return NULL;
3212 568eae95 2022-09-11 mark s->limit_view = 1;
3214 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3215 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3216 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3217 568eae95 2022-09-11 mark s->selected_entry = NULL;
3218 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3220 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3221 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3222 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3224 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3225 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3226 568eae95 2022-09-11 mark int have_match = 0;
3228 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3229 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3231 568eae95 2022-09-11 mark return err;
3233 568eae95 2022-09-11 mark if (have_match) {
3234 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3236 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3237 568eae95 2022-09-11 mark entry->id);
3238 568eae95 2022-09-11 mark if (matched == NULL) {
3239 568eae95 2022-09-11 mark err = got_error_from_errno(
3240 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3243 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3244 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3246 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3247 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3248 568eae95 2022-09-11 mark matched, entry);
3249 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3253 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3254 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3255 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3256 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3257 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3258 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3260 568eae95 2022-09-11 mark return err;
3263 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3264 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3265 568eae95 2022-09-11 mark s->selected = 0;
3267 568eae95 2022-09-11 mark return NULL;
3270 568eae95 2022-09-11 mark static const struct got_error *
3271 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3273 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3275 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3276 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3277 60493ae3 2019-06-20 stsp return NULL;
3280 60493ae3 2019-06-20 stsp static const struct got_error *
3281 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3283 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3284 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3285 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3287 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3288 f9686aa5 2020-03-27 stsp show_log_view(view);
3289 f9686aa5 2020-03-27 stsp update_panels();
3290 f9686aa5 2020-03-27 stsp doupdate();
3292 96e2b566 2019-07-08 stsp if (s->search_entry) {
3293 13add988 2019-10-15 stsp int errcode, ch;
3294 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3295 13add988 2019-10-15 stsp if (errcode)
3296 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3297 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3298 13add988 2019-10-15 stsp ch = wgetch(view->window);
3299 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3300 13add988 2019-10-15 stsp if (errcode)
3301 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3302 13add988 2019-10-15 stsp "pthread_mutex_lock");
3303 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3304 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3305 678cbce5 2019-07-28 stsp return NULL;
3307 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3308 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3310 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3311 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3312 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3314 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3315 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3316 f704b35c 2022-06-18 stsp * might have changed.
3318 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3319 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3321 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3324 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3327 60493ae3 2019-06-20 stsp while (1) {
3328 13add988 2019-10-15 stsp int have_match = 0;
3330 60493ae3 2019-06-20 stsp if (entry == NULL) {
3331 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3332 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3333 f9967bca 2020-03-27 stsp view->search_next_done =
3334 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3335 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3336 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3337 f801134a 2019-06-25 stsp return NULL;
3340 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3341 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3342 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3344 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3345 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3348 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3349 13add988 2019-10-15 stsp &view->regex);
3352 13add988 2019-10-15 stsp if (have_match) {
3353 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3354 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3358 96e2b566 2019-07-08 stsp s->search_entry = entry;
3359 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3360 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3362 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3365 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3366 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3367 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3368 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3370 60493ae3 2019-06-20 stsp return err;
3373 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3374 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3376 60493ae3 2019-06-20 stsp return err;
3381 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3383 60493ae3 2019-06-20 stsp return NULL;
3386 60493ae3 2019-06-20 stsp static const struct got_error *
3387 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3388 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3389 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3391 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3392 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3393 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3394 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3395 1a76625f 2018-10-22 stsp int errcode;
3397 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3398 f135c941 2020-02-20 stsp free(s->in_repo_path);
3399 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3400 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3401 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3404 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3405 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3406 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3407 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3409 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3410 568eae95 2022-09-11 mark s->limit_view = 0;
3411 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3413 fb2756b9 2018-08-04 stsp s->repo = repo;
3414 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3415 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3416 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3417 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3418 9cd7cbd1 2020-12-07 stsp goto done;
3421 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3422 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3423 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3424 5036bf37 2018-09-24 stsp goto done;
3426 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3427 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3429 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3430 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3431 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3432 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3434 11b20872 2019-11-08 stsp goto done;
3435 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3436 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3437 11b20872 2019-11-08 stsp if (err) {
3438 11b20872 2019-11-08 stsp free_colors(&s->colors);
3439 11b20872 2019-11-08 stsp goto done;
3441 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3442 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3443 11b20872 2019-11-08 stsp if (err) {
3444 11b20872 2019-11-08 stsp free_colors(&s->colors);
3445 11b20872 2019-11-08 stsp goto done;
3449 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3450 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3451 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3452 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3453 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3454 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3456 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3457 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3459 74467cc8 2022-06-15 stsp goto done;
3461 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3462 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3463 0ae84acc 2022-06-15 tracey if (err)
3464 0ae84acc 2022-06-15 tracey goto done;
3465 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3466 b672a97a 2020-01-27 stsp !s->log_branches);
3468 1a76625f 2018-10-22 stsp goto done;
3469 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3470 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3472 c5b78334 2020-01-12 stsp goto done;
3474 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3475 1a76625f 2018-10-22 stsp if (errcode) {
3476 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3477 1a76625f 2018-10-22 stsp goto done;
3479 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3480 7c1452c1 2020-03-26 stsp if (errcode) {
3481 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3482 7c1452c1 2020-03-26 stsp goto done;
3485 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3486 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3487 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3488 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3489 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3490 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3491 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3492 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3493 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3494 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3495 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3496 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3497 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3498 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3499 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3500 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3501 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3504 1a76625f 2018-10-22 stsp close_log_view(view);
3505 ba4f502b 2018-08-04 stsp return err;
3508 e5a0f69f 2018-08-18 stsp static const struct got_error *
3509 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3511 f2f6d207 2020-11-24 stsp const struct got_error *err;
3512 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3514 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3515 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3516 2b380cc8 2018-10-24 stsp &s->thread_args);
3517 2b380cc8 2018-10-24 stsp if (errcode)
3518 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3519 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3520 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3522 f2f6d207 2020-11-24 stsp return err;
3526 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3529 b880cc75 2022-06-30 mark static void
3530 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3532 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3534 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3536 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3537 568eae95 2022-09-11 mark view->count = 0;
3539 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3541 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3543 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3544 b880cc75 2022-06-30 mark --s->selected;
3546 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3548 b880cc75 2022-06-30 mark select_commit(s);
3552 b880cc75 2022-06-30 mark static const struct got_error *
3553 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3555 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3556 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3557 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3559 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3560 568eae95 2022-09-11 mark return NULL;
3562 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3563 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3564 b880cc75 2022-06-30 mark return NULL;
3566 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3567 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3569 631e7531 2022-08-12 mark if (!page) {
3570 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3571 b880cc75 2022-06-30 mark ++s->selected;
3573 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3574 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3575 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3578 631e7531 2022-08-12 mark s->selected = 0;
3579 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3580 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3581 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3582 631e7531 2022-08-12 mark if (entry == NULL)
3584 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3585 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3587 631e7531 2022-08-12 mark if (n > 0)
3588 631e7531 2022-08-12 mark s->selected = n - 1;
3590 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3591 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3592 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3593 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3595 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3598 b880cc75 2022-06-30 mark return err;
3601 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3602 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3604 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3605 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3606 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3607 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3610 b880cc75 2022-06-30 mark select_commit(s);
3612 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3613 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3614 b880cc75 2022-06-30 mark view->count = 0;
3616 b880cc75 2022-06-30 mark return NULL;
3619 9b058f45 2022-06-30 mark static void
3620 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3625 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3626 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3627 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3628 7532ccda 2022-07-11 mark else if (view->resized_y)
3629 7532ccda 2022-07-11 mark *y = view->resized_y;
3631 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3632 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3633 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3634 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3635 7532ccda 2022-07-11 mark else if (view->resized_x)
3636 7532ccda 2022-07-11 mark *x = view->resized_x;
3638 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3642 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3643 e5a0f69f 2018-08-18 stsp static const struct got_error *
3644 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3646 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3648 9b058f45 2022-06-30 mark view->nlines = y;
3649 d2366e29 2022-07-07 mark view->ncols = COLS;
3650 9b058f45 2022-06-30 mark err = view_resize(view);
3652 9b058f45 2022-06-30 mark return err;
3654 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3656 9b058f45 2022-06-30 mark return err;
3659 94b80cfa 2022-08-01 mark static const struct got_error *
3660 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3662 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3663 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3664 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3666 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3667 374f69dd 2022-08-13 stsp return NULL;
3669 94b80cfa 2022-08-01 mark g = view->gline;
3670 94b80cfa 2022-08-01 mark view->gline = 0;
3672 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3673 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3674 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3675 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3676 94b80cfa 2022-08-01 mark select_commit(s);
3677 94b80cfa 2022-08-01 mark return NULL;
3680 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3681 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3682 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3683 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3684 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3686 94b80cfa 2022-08-01 mark return err;
3687 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3688 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3690 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3691 94b80cfa 2022-08-01 mark s->selected = g - 1;
3693 94b80cfa 2022-08-01 mark select_commit(s);
3694 94b80cfa 2022-08-01 mark return NULL;
3698 d627976f 2023-02-01 mark static void
3699 d627976f 2023-02-01 mark horizontal_scroll_input(struct tog_view *view, int ch)
3702 d627976f 2023-02-01 mark switch (ch) {
3703 d627976f 2023-02-01 mark case KEY_LEFT:
3705 d627976f 2023-02-01 mark view->x -= MIN(view->x, 2);
3706 d627976f 2023-02-01 mark if (view->x <= 0)
3707 d627976f 2023-02-01 mark view->count = 0;
3709 d627976f 2023-02-01 mark case KEY_RIGHT:
3711 d627976f 2023-02-01 mark if (view->x + view->ncols / 2 < view->maxx)
3712 d627976f 2023-02-01 mark view->x += 2;
3714 d627976f 2023-02-01 mark view->count = 0;
3717 d627976f 2023-02-01 mark view->x = 0;
3720 d627976f 2023-02-01 mark view->x = MAX(view->maxx - view->ncols / 2, 0);
3721 d627976f 2023-02-01 mark view->count = 0;
3728 9b058f45 2022-06-30 mark static const struct got_error *
3729 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3731 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3732 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3733 631e7531 2022-08-12 mark int eos, nscroll;
3735 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3736 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3737 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3738 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3739 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3740 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3743 11edf34c 2022-08-12 mark return err;
3746 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3747 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3748 0dca135e 2022-06-30 mark --eos; /* border */
3750 94b80cfa 2022-08-01 mark if (view->gline)
3751 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3753 528dedf3 2021-08-30 stsp switch (ch) {
3755 568eae95 2022-09-11 mark err = limit_log_view(view);
3758 1e37a5c2 2019-05-12 jcs s->quit = 1;
3762 145b6838 2022-06-16 stsp case KEY_RIGHT:
3764 145b6838 2022-06-16 stsp case KEY_LEFT:
3766 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
3769 1e37a5c2 2019-05-12 jcs case KEY_UP:
3772 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3773 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3777 912a3f79 2021-08-30 j case KEY_HOME:
3778 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3779 640cd7ff 2022-06-22 mark view->count = 0;
3781 83cc4199 2022-06-13 stsp case CTRL('u'):
3783 83cc4199 2022-06-13 stsp nscroll /= 2;
3784 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3785 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3786 a4292ac5 2019-05-12 jcs case CTRL('b'):
3788 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3791 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3794 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3795 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3798 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3799 3f6c6614 2023-01-23 mark view->action = s->use_committer ?
3800 3f6c6614 2023-01-23 mark "show committer" : "show commit author";
3804 912a3f79 2021-08-30 j case KEY_END: {
3805 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3806 912a3f79 2021-08-30 j * traverse them all. */
3807 640cd7ff 2022-06-22 mark view->count = 0;
3808 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3809 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3810 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3811 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3812 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3815 80b7e8da 2022-06-11 stsp case CTRL('d'):
3817 83cc4199 2022-06-13 stsp nscroll /= 2;
3818 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3819 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3820 61417565 2022-06-20 mark case CTRL('f'):
3821 48bb96f0 2022-06-20 naddy case 'f':
3823 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3825 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3826 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3827 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3828 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3829 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3830 2b779855 2020-12-05 naddy select_commit(s);
3831 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3832 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3833 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3834 568eae95 2022-09-11 mark s->commits->ncommits;
3835 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3838 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3839 49b24ee5 2022-07-03 mark case '\r':
3840 640cd7ff 2022-06-22 mark view->count = 0;
3841 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3843 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3846 640cd7ff 2022-06-22 mark view->count = 0;
3847 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3849 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3851 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3852 21355643 2020-12-06 stsp case CTRL('l'):
3854 640cd7ff 2022-06-22 mark view->count = 0;
3855 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3856 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3858 21355643 2020-12-06 stsp err = stop_log_thread(s);
3860 74cfe85e 2020-10-20 stsp return err;
3861 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3862 21355643 2020-12-06 stsp char *parent_path;
3863 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3865 21355643 2020-12-06 stsp return err;
3866 21355643 2020-12-06 stsp free(s->in_repo_path);
3867 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3868 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3869 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3870 21355643 2020-12-06 stsp struct got_object_id *start_id;
3871 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3872 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3873 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3874 ea371198 2022-11-17 stsp if (err) {
3875 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
3876 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
3877 ea371198 2022-11-17 stsp return err;
3878 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
3879 ea371198 2022-11-17 stsp free(s->head_ref_name);
3880 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
3881 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
3882 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3883 ea371198 2022-11-17 stsp &tog_refs, s->repo);
3885 ea371198 2022-11-17 stsp return err;
3887 21355643 2020-12-06 stsp free(s->start_id);
3888 21355643 2020-12-06 stsp s->start_id = start_id;
3889 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3890 21355643 2020-12-06 stsp } else /* 'B' */
3891 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3893 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3894 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3896 b0dd8d27 2022-06-16 stsp return err;
3898 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3899 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3900 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3902 21355643 2020-12-06 stsp return err;
3903 51a10b52 2020-12-26 stsp tog_free_refs();
3904 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3906 ca51c541 2020-12-07 stsp return err;
3907 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3908 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3910 d01904d4 2019-06-25 stsp return err;
3911 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3912 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3914 b672a97a 2020-01-27 stsp return err;
3915 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3916 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3917 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3918 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3919 21355643 2020-12-06 stsp s->selected_entry = NULL;
3920 21355643 2020-12-06 stsp s->selected = 0;
3921 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3922 21355643 2020-12-06 stsp s->quit = 0;
3923 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3924 dfee752e 2022-06-18 op s->matched_entry = NULL;
3925 dfee752e 2022-06-18 op s->search_entry = NULL;
3926 01a7bcaf 2022-07-22 mark view->offset = 0;
3929 640cd7ff 2022-06-22 mark view->count = 0;
3930 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3933 640cd7ff 2022-06-22 mark view->count = 0;
3937 80ddbec8 2018-04-29 stsp return err;
3940 4ed7e80c 2018-05-20 stsp static const struct got_error *
3941 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3943 c2db6724 2019-01-04 stsp const struct got_error *error;
3945 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3946 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3947 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3949 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3950 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3952 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3953 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3955 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3956 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3958 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3959 c2db6724 2019-01-04 stsp if (error != NULL)
3960 c2db6724 2019-01-04 stsp return error;
3962 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3963 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3965 c2db6724 2019-01-04 stsp return NULL;
3968 a915003a 2019-02-05 stsp static void
3969 a915003a 2019-02-05 stsp init_curses(void)
3972 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3973 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3974 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3976 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3977 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3978 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3979 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3980 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3982 a915003a 2019-02-05 stsp initscr();
3984 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3987 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3988 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3989 a915003a 2019-02-05 stsp curs_set(0);
3990 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3991 6d17833f 2019-11-08 stsp start_color();
3992 6d17833f 2019-11-08 stsp use_default_colors();
3996 c2db6724 2019-01-04 stsp static const struct got_error *
3997 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3998 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4000 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4002 f135c941 2020-02-20 stsp if (argc == 0) {
4003 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4004 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4005 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4006 f135c941 2020-02-20 stsp return NULL;
4009 f135c941 2020-02-20 stsp if (worktree) {
4010 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4013 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4015 f135c941 2020-02-20 stsp return err;
4016 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4017 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4018 bfd61697 2020-11-14 stsp p) == -1) {
4019 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4020 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4024 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4026 f135c941 2020-02-20 stsp return err;
4029 f135c941 2020-02-20 stsp static const struct got_error *
4030 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4032 80ddbec8 2018-04-29 stsp const struct got_error *error;
4033 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4034 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4035 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4036 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4037 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4038 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4039 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4040 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4041 04cc582a 2018-08-01 stsp struct tog_view *view;
4042 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4044 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4045 80ddbec8 2018-04-29 stsp switch (ch) {
4047 b672a97a 2020-01-27 stsp log_branches = 1;
4050 80ddbec8 2018-04-29 stsp start_commit = optarg;
4053 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4054 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4055 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4059 17020d27 2019-03-07 stsp usage_log();
4060 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4064 80ddbec8 2018-04-29 stsp argc -= optind;
4065 80ddbec8 2018-04-29 stsp argv += optind;
4067 f135c941 2020-02-20 stsp if (argc > 1)
4068 f135c941 2020-02-20 stsp usage_log();
4070 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4071 0ae84acc 2022-06-15 tracey if (error != NULL)
4072 0ae84acc 2022-06-15 tracey goto done;
4074 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4075 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4076 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4077 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4078 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4079 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4080 c156c7a4 2020-12-18 stsp goto done;
4081 a1fbf39a 2019-08-11 stsp if (worktree)
4082 6962eb72 2020-02-20 stsp repo_path =
4083 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4085 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4086 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4087 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4088 c156c7a4 2020-12-18 stsp goto done;
4092 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4093 80ddbec8 2018-04-29 stsp if (error != NULL)
4094 ecb28ae0 2018-07-16 stsp goto done;
4096 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4097 f135c941 2020-02-20 stsp repo, worktree);
4098 f135c941 2020-02-20 stsp if (error)
4099 f135c941 2020-02-20 stsp goto done;
4101 f135c941 2020-02-20 stsp init_curses();
4103 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4104 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4105 c02c541e 2019-03-29 stsp if (error)
4106 c02c541e 2019-03-29 stsp goto done;
4108 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4109 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4110 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4111 87670572 2020-12-26 naddy if (error)
4112 87670572 2020-12-26 naddy goto done;
4115 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4116 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4117 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4118 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4119 d8f38dc4 2020-12-05 stsp if (error)
4120 d8f38dc4 2020-12-05 stsp goto done;
4121 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4123 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4124 d8f38dc4 2020-12-05 stsp if (error == NULL)
4125 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4126 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4127 d8f38dc4 2020-12-05 stsp goto done;
4128 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4129 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4130 d8f38dc4 2020-12-05 stsp if (error)
4131 d8f38dc4 2020-12-05 stsp goto done;
4134 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4135 04cc582a 2018-08-01 stsp if (view == NULL) {
4136 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4137 04cc582a 2018-08-01 stsp goto done;
4139 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4140 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4141 ba4f502b 2018-08-04 stsp if (error)
4142 ba4f502b 2018-08-04 stsp goto done;
4143 2fc00ff4 2019-08-31 stsp if (worktree) {
4144 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4145 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4146 2fc00ff4 2019-08-31 stsp worktree = NULL;
4148 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4150 f135c941 2020-02-20 stsp free(in_repo_path);
4151 ecb28ae0 2018-07-16 stsp free(repo_path);
4152 ecb28ae0 2018-07-16 stsp free(cwd);
4153 899d86c2 2018-05-10 stsp free(start_id);
4154 d8f38dc4 2020-12-05 stsp free(label);
4156 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4157 1d0f4054 2021-06-17 stsp if (repo) {
4158 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4159 1d0f4054 2021-06-17 stsp if (error == NULL)
4160 1d0f4054 2021-06-17 stsp error = close_err;
4162 ec142235 2019-03-07 stsp if (worktree)
4163 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4164 0ae84acc 2022-06-15 tracey if (pack_fds) {
4165 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4166 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4167 0ae84acc 2022-06-15 tracey if (error == NULL)
4168 0ae84acc 2022-06-15 tracey error = pack_err;
4170 51a10b52 2020-12-26 stsp tog_free_refs();
4171 80ddbec8 2018-04-29 stsp return error;
4174 4ed7e80c 2018-05-20 stsp __dead static void
4175 9f7d7167 2018-04-29 stsp usage_diff(void)
4178 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4179 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4183 6d17833f 2019-11-08 stsp static int
4184 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4185 41605754 2020-11-12 stsp regmatch_t *regmatch)
4187 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4190 336075a4 2022-06-25 op static struct tog_color *
4191 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4193 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4195 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4196 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4197 6d17833f 2019-11-08 stsp return tc;
4200 6d17833f 2019-11-08 stsp return NULL;
4203 4ed7e80c 2018-05-20 stsp static const struct got_error *
4204 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4205 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4207 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4208 44a87665 2022-06-16 stsp char *exstr = NULL;
4209 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4210 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4211 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4212 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4214 41605754 2020-11-12 stsp *wtotal = 0;
4216 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4217 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4219 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4221 44a87665 2022-06-16 stsp return err;
4223 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4224 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4225 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4226 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4227 44a87665 2022-06-16 stsp goto done;
4229 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4230 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4231 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4232 1853e0f4 2022-06-16 stsp goto done;
4234 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4235 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4236 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4237 1853e0f4 2022-06-16 stsp goto done;
4240 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4241 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4242 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4244 1853e0f4 2022-06-16 stsp goto done;
4245 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4247 1853e0f4 2022-06-16 stsp free(wline);
4248 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4249 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4251 1853e0f4 2022-06-16 stsp goto done;
4252 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4253 1853e0f4 2022-06-16 stsp wlimit -= width;
4254 1853e0f4 2022-06-16 stsp *wtotal += width;
4257 41605754 2020-11-12 stsp if (wlimit > 0) {
4258 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4259 1853e0f4 2022-06-16 stsp size_t wlen;
4261 1853e0f4 2022-06-16 stsp free(wline);
4262 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4263 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4265 1853e0f4 2022-06-16 stsp goto done;
4266 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4267 1853e0f4 2022-06-16 stsp while (i < wlen) {
4268 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4269 1853e0f4 2022-06-16 stsp if (width == -1) {
4270 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4271 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4272 1853e0f4 2022-06-16 stsp goto done;
4274 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4276 61417565 2022-06-20 mark w += width;
4279 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4280 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4281 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4282 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4283 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4284 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4285 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4289 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4290 1853e0f4 2022-06-16 stsp free(wline);
4291 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4292 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4293 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4294 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4296 1853e0f4 2022-06-16 stsp goto done;
4297 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4299 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4300 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4302 1853e0f4 2022-06-16 stsp goto done;
4303 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4305 1853e0f4 2022-06-16 stsp *wtotal += width2;
4308 145b6838 2022-06-16 stsp free(wline);
4309 44a87665 2022-06-16 stsp free(exstr);
4310 1853e0f4 2022-06-16 stsp free(seg0);
4311 1853e0f4 2022-06-16 stsp free(seg1);
4312 1853e0f4 2022-06-16 stsp free(seg2);
4313 1853e0f4 2022-06-16 stsp return err;
4316 94b80cfa 2022-08-01 mark static int
4317 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4319 94b80cfa 2022-08-01 mark FILE *f = NULL;
4320 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4322 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4323 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4325 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4326 ec2a9698 2022-09-15 mark selected = first;
4327 ec2a9698 2022-09-15 mark eof = &s->eof;
4329 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4330 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4332 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4333 94b80cfa 2022-08-01 mark selected = first;
4334 94b80cfa 2022-08-01 mark eof = &s->eof;
4336 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4337 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4339 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4340 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4341 94b80cfa 2022-08-01 mark eof = &s->eof;
4342 94b80cfa 2022-08-01 mark f = s->blame.f;
4346 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4347 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4349 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4350 94b80cfa 2022-08-01 mark rewind(f);
4352 94b80cfa 2022-08-01 mark *first = 1;
4353 94b80cfa 2022-08-01 mark *lineno = 0;
4354 94b80cfa 2022-08-01 mark *nprinted = 0;
4358 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4359 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4360 94b80cfa 2022-08-01 mark view->gline = 0;
4365 41605754 2020-11-12 stsp static const struct got_error *
4366 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4368 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4369 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4370 61e69b96 2018-05-20 stsp const struct got_error *err;
4371 c7d5c43c 2022-08-04 mark int nprinted = 0;
4372 b304db33 2018-05-20 stsp char *line;
4373 826082fe 2020-12-10 stsp size_t linesize = 0;
4374 826082fe 2020-12-10 stsp ssize_t linelen;
4375 61e69b96 2018-05-20 stsp wchar_t *wline;
4376 e0b650dd 2018-05-20 stsp int width;
4377 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4378 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4379 fe621944 2020-11-10 stsp off_t line_offset;
4381 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4382 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4383 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4384 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4386 f7d12f7e 2018-08-01 stsp werase(view->window);
4388 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4389 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4391 a3404814 2018-09-02 stsp if (header) {
4392 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4393 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4394 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4396 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4397 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4398 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4400 135a2da0 2020-11-11 stsp free(line);
4402 a3404814 2018-09-02 stsp return err;
4404 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4405 a3404814 2018-09-02 stsp wstandout(view->window);
4406 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4407 e54cc94a 2020-11-11 stsp free(wline);
4408 e54cc94a 2020-11-11 stsp wline = NULL;
4409 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4410 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4411 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4412 a3404814 2018-09-02 stsp wstandend(view->window);
4414 a3404814 2018-09-02 stsp if (max_lines <= 1)
4415 a3404814 2018-09-02 stsp return NULL;
4416 a3404814 2018-09-02 stsp max_lines--;
4419 89f1a395 2020-12-01 naddy s->eof = 0;
4420 145b6838 2022-06-16 stsp view->maxx = 0;
4421 826082fe 2020-12-10 stsp line = NULL;
4422 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4423 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4424 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4426 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4427 826082fe 2020-12-10 stsp if (linelen == -1) {
4428 826082fe 2020-12-10 stsp if (feof(s->f)) {
4429 826082fe 2020-12-10 stsp s->eof = 1;
4432 826082fe 2020-12-10 stsp free(line);
4433 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4436 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4438 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4440 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4441 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4443 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4444 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4445 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4446 1853e0f4 2022-06-16 stsp if (err) {
4447 1853e0f4 2022-06-16 stsp free(line);
4448 1853e0f4 2022-06-16 stsp return err;
4450 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4451 1853e0f4 2022-06-16 stsp free(wline);
4452 1853e0f4 2022-06-16 stsp wline = NULL;
4454 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4455 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4456 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4457 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4459 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4460 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4461 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4462 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4463 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4464 41605754 2020-11-12 stsp if (err) {
4465 41605754 2020-11-12 stsp free(line);
4466 41605754 2020-11-12 stsp return err;
4470 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4471 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4472 969c159c 2022-06-16 stsp if (err) {
4473 969c159c 2022-06-16 stsp free(line);
4474 969c159c 2022-06-16 stsp return err;
4476 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4477 969c159c 2022-06-16 stsp free(wline);
4478 41605754 2020-11-12 stsp wline = NULL;
4480 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4481 94b80cfa 2022-08-01 mark /* highlight full gline length */
4482 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4483 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4485 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4486 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4489 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4490 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4491 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4493 826082fe 2020-12-10 stsp free(line);
4494 fe621944 2020-11-10 stsp if (nprinted >= 1)
4495 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4496 89f1a395 2020-12-01 naddy (nprinted - 1);
4498 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4500 9b058f45 2022-06-30 mark view_border(view);
4502 89f1a395 2020-12-01 naddy if (s->eof) {
4503 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4504 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4505 c3e9aa98 2019-05-13 jcs nprinted++;
4508 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4509 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4511 c3e9aa98 2019-05-13 jcs return err;
4514 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4515 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4516 e54cc94a 2020-11-11 stsp free(wline);
4517 e54cc94a 2020-11-11 stsp wline = NULL;
4518 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4521 26ed57b2 2018-05-19 stsp return NULL;
4524 abd2672a 2018-12-23 stsp static char *
4525 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4527 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4528 09867e48 2019-08-13 stsp char *p, *s;
4530 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4531 09867e48 2019-08-13 stsp if (tm == NULL)
4532 09867e48 2019-08-13 stsp return NULL;
4533 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4534 09867e48 2019-08-13 stsp if (s == NULL)
4535 09867e48 2019-08-13 stsp return NULL;
4536 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4538 abd2672a 2018-12-23 stsp *p = '\0';
4542 4ed7e80c 2018-05-20 stsp static const struct got_error *
4543 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4544 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4546 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4548 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4549 fe621944 2020-11-10 stsp if (p == NULL)
4550 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4551 c7d5c43c 2022-08-04 mark *lines = p;
4552 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4553 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4554 fe621944 2020-11-10 stsp (*nlines)++;
4556 fe621944 2020-11-10 stsp return NULL;
4559 fe621944 2020-11-10 stsp static const struct got_error *
4560 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4561 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4563 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4564 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4565 1f3405c9 2023-01-17 mark size_t i, r;
4567 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4568 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4570 1f3405c9 2023-01-17 mark for (;;) {
4571 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4572 1f3405c9 2023-01-17 mark if (r == 0) {
4573 1f3405c9 2023-01-17 mark if (ferror(src))
4574 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4575 1f3405c9 2023-01-17 mark if (feof(src))
4578 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4579 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4582 5afebbd1 2023-02-22 mark if (s_nlines == 0 && *d_nlines == 0)
4583 5afebbd1 2023-02-22 mark return NULL;
4586 5afebbd1 2023-02-22 mark * If commit info was in dst, increment line offsets
4587 5afebbd1 2023-02-22 mark * of the appended diff content, but skip s_lines[0]
4588 5afebbd1 2023-02-22 mark * because offset zero is already in *d_lines.
4590 5afebbd1 2023-02-22 mark if (*d_nlines > 0) {
4591 5afebbd1 2023-02-22 mark for (i = 1; i < s_nlines; ++i)
4592 5afebbd1 2023-02-22 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4594 5afebbd1 2023-02-22 mark if (s_nlines > 0) {
4595 5afebbd1 2023-02-22 mark --s_nlines;
4596 5afebbd1 2023-02-22 mark ++s_lines;
4600 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4601 1f3405c9 2023-01-17 mark if (p == NULL) {
4602 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4603 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4606 1f3405c9 2023-01-17 mark *d_lines = p;
4608 5afebbd1 2023-02-22 mark memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4609 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4611 1f3405c9 2023-01-17 mark return NULL;
4614 1f3405c9 2023-01-17 mark static const struct got_error *
4615 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4616 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4617 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4618 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4620 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4621 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4622 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4623 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4624 45d799e2 2018-12-23 stsp time_t committer_time;
4625 45d799e2 2018-12-23 stsp const char *author, *committer;
4626 8b473291 2019-02-21 stsp char *refs_str = NULL;
4627 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4628 fe621944 2020-11-10 stsp off_t outoff = 0;
4631 8b473291 2019-02-21 stsp if (refs) {
4632 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4634 8b473291 2019-02-21 stsp return err;
4637 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4639 abd2672a 2018-12-23 stsp return err;
4641 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4642 15a94983 2018-12-23 stsp if (err) {
4643 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4644 15a94983 2018-12-23 stsp goto done;
4647 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4649 fe621944 2020-11-10 stsp goto done;
4651 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4652 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4653 fe621944 2020-11-10 stsp if (n < 0) {
4654 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4655 abd2672a 2018-12-23 stsp goto done;
4657 fe621944 2020-11-10 stsp outoff += n;
4658 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4660 fe621944 2020-11-10 stsp goto done;
4662 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4663 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4664 fe621944 2020-11-10 stsp if (n < 0) {
4665 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4666 abd2672a 2018-12-23 stsp goto done;
4668 fe621944 2020-11-10 stsp outoff += n;
4669 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4671 fe621944 2020-11-10 stsp goto done;
4673 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4674 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4675 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4676 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4677 fe621944 2020-11-10 stsp if (n < 0) {
4678 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4679 fe621944 2020-11-10 stsp goto done;
4681 fe621944 2020-11-10 stsp outoff += n;
4682 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4683 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4685 fe621944 2020-11-10 stsp goto done;
4687 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4688 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4689 1a99e0b4 2023-01-07 stsp if (datestr) {
4690 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4691 fe621944 2020-11-10 stsp if (n < 0) {
4692 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4693 fe621944 2020-11-10 stsp goto done;
4695 fe621944 2020-11-10 stsp outoff += n;
4696 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4697 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4699 fe621944 2020-11-10 stsp goto done;
4701 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4702 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4703 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4704 9f98ca05 2021-09-24 stsp int pn = 1;
4705 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4706 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4707 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4709 9f98ca05 2021-09-24 stsp goto done;
4710 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4711 9f98ca05 2021-09-24 stsp if (n < 0) {
4712 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4713 9f98ca05 2021-09-24 stsp goto done;
4715 9f98ca05 2021-09-24 stsp outoff += n;
4716 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4717 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4719 9f98ca05 2021-09-24 stsp goto done;
4720 9f98ca05 2021-09-24 stsp free(id_str);
4721 9f98ca05 2021-09-24 stsp id_str = NULL;
4725 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4727 5943eee2 2019-08-13 stsp goto done;
4728 fe621944 2020-11-10 stsp s = logmsg;
4729 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4730 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4731 fe621944 2020-11-10 stsp if (n < 0) {
4732 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4733 fe621944 2020-11-10 stsp goto done;
4735 fe621944 2020-11-10 stsp outoff += n;
4736 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4737 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4739 fe621944 2020-11-10 stsp goto done;
4742 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
4743 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4744 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
4746 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4747 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4748 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
4749 fe621944 2020-11-10 stsp if (n < 0) {
4750 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4751 fe621944 2020-11-10 stsp goto done;
4753 fe621944 2020-11-10 stsp outoff += n;
4754 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4755 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4757 fe621944 2020-11-10 stsp goto done;
4760 0208f208 2020-05-05 stsp fputc('\n', outfile);
4762 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4764 5191b70b 2023-01-07 mark goto done;
4766 5191b70b 2023-01-07 mark n = fprintf(outfile,
4767 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4768 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4769 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4770 5191b70b 2023-01-07 mark if (n < 0) {
4771 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
4772 5191b70b 2023-01-07 mark goto done;
4774 5191b70b 2023-01-07 mark outoff += n;
4775 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4777 5191b70b 2023-01-07 mark goto done;
4779 5191b70b 2023-01-07 mark fputc('\n', outfile);
4781 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4783 abd2672a 2018-12-23 stsp free(id_str);
4784 5943eee2 2019-08-13 stsp free(logmsg);
4785 8b473291 2019-02-21 stsp free(refs_str);
4786 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4787 7510f233 2020-08-09 stsp if (err) {
4788 c7d5c43c 2022-08-04 mark free(*lines);
4789 c7d5c43c 2022-08-04 mark *lines = NULL;
4790 ae6a6978 2020-08-09 stsp *nlines = 0;
4792 fe621944 2020-11-10 stsp return err;
4795 abd2672a 2018-12-23 stsp static const struct got_error *
4796 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4798 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4799 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
4800 15a94983 2018-12-23 stsp int obj_type;
4801 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
4802 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
4804 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
4806 c7d5c43c 2022-08-04 mark free(s->lines);
4807 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4808 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4809 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4810 fe621944 2020-11-10 stsp s->nlines = 0;
4812 511a516b 2018-05-19 stsp f = got_opentemp();
4813 48ae06ee 2018-10-18 stsp if (f == NULL) {
4814 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4815 48ae06ee 2018-10-18 stsp goto done;
4817 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
4818 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
4819 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
4820 1f3405c9 2023-01-17 mark goto done;
4822 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4823 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4824 fb43ecf1 2019-02-11 stsp goto done;
4828 15a94983 2018-12-23 stsp if (s->id1)
4829 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4831 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4833 15a94983 2018-12-23 stsp goto done;
4835 15a94983 2018-12-23 stsp switch (obj_type) {
4836 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4837 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4838 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4839 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4840 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4843 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4844 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4845 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4846 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4847 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
4849 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4850 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4851 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4852 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4853 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4854 1f3405c9 2023-01-17 mark size_t nlines = 0;
4855 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
4856 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
4857 1f3405c9 2023-01-17 mark &changed_paths,
4858 1f3405c9 2023-01-17 mark s->ignore_whitespace,
4859 1f3405c9 2023-01-17 mark s->force_text_diff,
4860 1f3405c9 2023-01-17 mark tog_diff_algo
4863 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
4864 1f3405c9 2023-01-17 mark if (lines == NULL) {
4865 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
4866 1f3405c9 2023-01-17 mark goto done;
4869 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
4870 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
4871 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4872 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
4873 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4877 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4879 3ffacbe1 2020-02-02 tracey goto done;
4880 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4881 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4882 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4883 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4884 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
4885 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4886 f44b1f58 2020-02-02 tracey if (err)
4887 f44b1f58 2020-02-02 tracey goto done;
4888 f44b1f58 2020-02-02 tracey } else {
4889 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4890 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4891 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4892 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4893 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4894 5191b70b 2023-01-07 mark s->ignore_whitespace,
4895 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4896 f44b1f58 2020-02-02 tracey if (err)
4897 f44b1f58 2020-02-02 tracey goto done;
4902 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4904 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4905 1f3405c9 2023-01-17 mark lines, nlines);
4909 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4913 1f3405c9 2023-01-17 mark free(lines);
4914 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4915 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4916 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4917 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4918 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
4919 48ae06ee 2018-10-18 stsp return err;
4922 f5215bb9 2019-02-22 stsp static void
4923 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4925 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4926 f5215bb9 2019-02-22 stsp update_panels();
4927 f5215bb9 2019-02-22 stsp doupdate();
4930 f44b1f58 2020-02-02 tracey static const struct got_error *
4931 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4933 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4935 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4936 f44b1f58 2020-02-02 tracey return NULL;
4939 eaf2c13e 2022-09-17 mark static void
4940 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4941 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4943 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4945 eaf2c13e 2022-09-17 mark *f = s->f;
4946 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4947 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4948 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4949 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4950 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4951 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4954 ec2a9698 2022-09-15 mark static const struct got_error *
4955 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4957 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4959 f44b1f58 2020-02-02 tracey int lineno;
4960 cb713507 2022-06-16 stsp char *line = NULL;
4961 826082fe 2020-12-10 stsp size_t linesize = 0;
4962 826082fe 2020-12-10 stsp ssize_t linelen;
4963 ec2a9698 2022-09-15 mark off_t *line_offsets;
4964 ec2a9698 2022-09-15 mark size_t nlines = 0;
4965 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4967 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4968 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4969 eaf2c13e 2022-09-17 mark "view search not supported");
4970 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4971 ec2a9698 2022-09-15 mark &match, &selected);
4973 f44b1f58 2020-02-02 tracey if (!view->searching) {
4974 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4975 f44b1f58 2020-02-02 tracey return NULL;
4978 ec2a9698 2022-09-15 mark if (*match) {
4979 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4980 ec2a9698 2022-09-15 mark lineno = *match + 1;
4982 ec2a9698 2022-09-15 mark lineno = *match - 1;
4984 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4986 f44b1f58 2020-02-02 tracey while (1) {
4987 f44b1f58 2020-02-02 tracey off_t offset;
4989 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4990 ec2a9698 2022-09-15 mark if (*match == 0) {
4991 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4995 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4996 f44b1f58 2020-02-02 tracey lineno = 1;
4998 ec2a9698 2022-09-15 mark lineno = nlines;
5001 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
5002 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
5003 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
5004 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
5005 f44b1f58 2020-02-02 tracey free(line);
5006 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5008 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
5009 cb713507 2022-06-16 stsp if (linelen != -1) {
5010 cb713507 2022-06-16 stsp char *exstr;
5011 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5014 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5015 cb713507 2022-06-16 stsp &view->regmatch)) {
5016 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5017 ec2a9698 2022-09-15 mark *match = lineno;
5018 cb713507 2022-06-16 stsp free(exstr);
5021 cb713507 2022-06-16 stsp free(exstr);
5023 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5024 f44b1f58 2020-02-02 tracey lineno++;
5026 f44b1f58 2020-02-02 tracey lineno--;
5028 826082fe 2020-12-10 stsp free(line);
5030 ec2a9698 2022-09-15 mark if (*match) {
5031 ec2a9698 2022-09-15 mark *first = *match;
5032 ec2a9698 2022-09-15 mark *selected = 1;
5035 145b6838 2022-06-16 stsp return err;
5038 48ae06ee 2018-10-18 stsp static const struct got_error *
5039 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
5041 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
5042 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5044 b72706c3 2022-06-01 stsp free(s->id1);
5045 b72706c3 2022-06-01 stsp s->id1 = NULL;
5046 b72706c3 2022-06-01 stsp free(s->id2);
5047 b72706c3 2022-06-01 stsp s->id2 = NULL;
5048 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
5049 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5050 b72706c3 2022-06-01 stsp s->f = NULL;
5051 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5052 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5053 b72706c3 2022-06-01 stsp s->f1 = NULL;
5054 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5055 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
5056 b72706c3 2022-06-01 stsp s->f2 = NULL;
5057 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5058 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5059 f9d37699 2022-06-28 stsp s->fd1 = -1;
5060 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5061 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
5062 f9d37699 2022-06-28 stsp s->fd2 = -1;
5063 c7d5c43c 2022-08-04 mark free(s->lines);
5064 c7d5c43c 2022-08-04 mark s->lines = NULL;
5065 b72706c3 2022-06-01 stsp s->nlines = 0;
5066 b72706c3 2022-06-01 stsp return err;
5069 b72706c3 2022-06-01 stsp static const struct got_error *
5070 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5071 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5072 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5073 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
5075 48ae06ee 2018-10-18 stsp const struct got_error *err;
5076 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5078 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
5079 f9d37699 2022-06-28 stsp s->fd1 = -1;
5080 f9d37699 2022-06-28 stsp s->fd2 = -1;
5082 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5083 15a94983 2018-12-23 stsp int type1, type2;
5084 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5086 15a94983 2018-12-23 stsp return err;
5087 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5089 15a94983 2018-12-23 stsp return err;
5091 15a94983 2018-12-23 stsp if (type1 != type2)
5092 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5094 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5095 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5096 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5097 f44b1f58 2020-02-02 tracey s->repo = repo;
5098 f44b1f58 2020-02-02 tracey s->id1 = id1;
5099 f44b1f58 2020-02-02 tracey s->id2 = id2;
5100 3dbaef42 2020-11-24 stsp s->label1 = label1;
5101 3dbaef42 2020-11-24 stsp s->label2 = label2;
5103 15a94983 2018-12-23 stsp if (id1) {
5104 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5105 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5106 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5108 5465d566 2020-02-01 tracey s->id1 = NULL;
5110 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5111 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5112 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5113 b72706c3 2022-06-01 stsp goto done;
5116 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5117 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5118 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5119 a00719e9 2022-06-17 stsp goto done;
5122 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5123 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5124 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5125 b72706c3 2022-06-01 stsp goto done;
5128 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5129 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5130 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5131 f9d37699 2022-06-28 stsp goto done;
5134 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5135 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5136 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5137 f9d37699 2022-06-28 stsp goto done;
5140 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5141 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5142 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5143 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5144 5465d566 2020-02-01 tracey s->repo = repo;
5146 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5149 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5150 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5151 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5152 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5153 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5154 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5155 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5156 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5157 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5158 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5159 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5160 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5161 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5162 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5163 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5164 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5165 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5166 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5167 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5168 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5169 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5170 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5171 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5172 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5173 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5174 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5175 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5176 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5177 b72706c3 2022-06-01 stsp goto done;
5181 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5182 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5183 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5184 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5186 5465d566 2020-02-01 tracey err = create_diff(s);
5188 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5189 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5190 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5191 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5192 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5193 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5194 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5197 b72706c3 2022-06-01 stsp close_diff_view(view);
5198 e5a0f69f 2018-08-18 stsp return err;
5201 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5202 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5204 a3404814 2018-09-02 stsp const struct got_error *err;
5205 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5206 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5207 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5209 a3404814 2018-09-02 stsp if (s->id1) {
5210 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5212 a3404814 2018-09-02 stsp return err;
5213 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5215 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5217 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5219 a3404814 2018-09-02 stsp return err;
5220 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5222 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5223 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5224 a3404814 2018-09-02 stsp free(id_str1);
5225 a3404814 2018-09-02 stsp free(id_str2);
5226 a3404814 2018-09-02 stsp return err;
5228 a3404814 2018-09-02 stsp free(id_str1);
5229 a3404814 2018-09-02 stsp free(id_str2);
5231 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5232 267bb3b8 2021-08-01 stsp free(header);
5233 267bb3b8 2021-08-01 stsp return err;
5236 15a087fe 2019-02-21 stsp static const struct got_error *
5237 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5238 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5240 d7a04538 2019-02-21 stsp const struct got_error *err;
5241 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5242 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5243 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5245 15a087fe 2019-02-21 stsp free(s->id2);
5246 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5247 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5248 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5250 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5252 d7a04538 2019-02-21 stsp return err;
5253 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5254 15a087fe 2019-02-21 stsp free(s->id1);
5255 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5256 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5257 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5258 15a087fe 2019-02-21 stsp return NULL;
5261 0cf4efb1 2018-09-29 stsp static const struct got_error *
5262 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5264 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5266 917d79a7 2022-07-01 stsp view->count = 0;
5267 917d79a7 2022-07-01 stsp wclear(view->window);
5268 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5269 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5270 917d79a7 2022-07-01 stsp s->matched_line = 0;
5271 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5272 917d79a7 2022-07-01 stsp return create_diff(s);
5275 c7d5c43c 2022-08-04 mark static void
5276 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5278 c7d5c43c 2022-08-04 mark int start, i;
5280 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5282 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5283 c7d5c43c 2022-08-04 mark if (i == 0)
5284 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5285 c7d5c43c 2022-08-04 mark if (--i == start)
5286 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5289 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5290 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5293 c7d5c43c 2022-08-04 mark static void
5294 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5296 c7d5c43c 2022-08-04 mark int start, i;
5298 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5300 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5301 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5303 c7d5c43c 2022-08-04 mark if (++i == start)
5304 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5307 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5308 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5311 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5312 c0f61fa4 2022-07-11 mark int, int, int);
5313 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5314 c0f61fa4 2022-07-11 mark int, int);
5316 917d79a7 2022-07-01 stsp static const struct got_error *
5317 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5319 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5320 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5321 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5322 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5323 826082fe 2020-12-10 stsp char *line = NULL;
5324 826082fe 2020-12-10 stsp size_t linesize = 0;
5325 826082fe 2020-12-10 stsp ssize_t linelen;
5326 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5328 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5330 e5a0f69f 2018-08-18 stsp switch (ch) {
5333 145b6838 2022-06-16 stsp case KEY_RIGHT:
5335 145b6838 2022-06-16 stsp case KEY_LEFT:
5337 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
5341 3f6c6614 2023-01-23 mark if (ch == 'a') {
5342 3f6c6614 2023-01-23 mark s->force_text_diff = !s->force_text_diff;
5343 3f6c6614 2023-01-23 mark view->action = s->force_text_diff ?
5344 3f6c6614 2023-01-23 mark "force ASCII text enabled" :
5345 3f6c6614 2023-01-23 mark "force ASCII text disabled";
5347 3f6c6614 2023-01-23 mark else if (ch == 'w') {
5348 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5349 3f6c6614 2023-01-23 mark view->action = s->ignore_whitespace ?
5350 3f6c6614 2023-01-23 mark "ignore whitespace enabled" :
5351 3f6c6614 2023-01-23 mark "ignore whitespace disabled";
5353 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5356 912a3f79 2021-08-30 j case KEY_HOME:
5357 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5358 640cd7ff 2022-06-22 mark view->count = 0;
5361 912a3f79 2021-08-30 j case KEY_END:
5362 640cd7ff 2022-06-22 mark view->count = 0;
5366 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5370 1e37a5c2 2019-05-12 jcs case KEY_UP:
5371 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5372 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5373 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5375 640cd7ff 2022-06-22 mark view->count = 0;
5377 83cc4199 2022-06-13 stsp case CTRL('u'):
5379 83cc4199 2022-06-13 stsp nscroll /= 2;
5380 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5381 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5382 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5384 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5385 640cd7ff 2022-06-22 mark view->count = 0;
5389 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5390 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5393 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5394 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5395 1e37a5c2 2019-05-12 jcs if (!s->eof)
5396 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5398 640cd7ff 2022-06-22 mark view->count = 0;
5400 83cc4199 2022-06-13 stsp case CTRL('d'):
5402 83cc4199 2022-06-13 stsp nscroll /= 2;
5403 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5404 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5405 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5408 640cd7ff 2022-06-22 mark if (s->eof) {
5409 640cd7ff 2022-06-22 mark view->count = 0;
5413 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5414 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5415 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5416 826082fe 2020-12-10 stsp if (linelen == -1) {
5417 826082fe 2020-12-10 stsp if (feof(s->f)) {
5418 826082fe 2020-12-10 stsp s->eof = 1;
5420 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5424 826082fe 2020-12-10 stsp free(line);
5427 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5430 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5433 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5436 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5439 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5440 1e37a5c2 2019-05-12 jcs s->diff_context--;
5441 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5442 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5443 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5444 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5445 27829c9e 2020-11-21 stsp s->nlines) {
5446 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5447 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5450 640cd7ff 2022-06-22 mark view->count = 0;
5453 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5454 1e37a5c2 2019-05-12 jcs s->diff_context++;
5455 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5456 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5457 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5459 640cd7ff 2022-06-22 mark view->count = 0;
5465 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5469 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5470 640cd7ff 2022-06-22 mark view->count = 0;
5473 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5475 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5476 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5477 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5479 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5480 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5483 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5485 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5488 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5491 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5492 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5493 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5495 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5496 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5497 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5499 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5500 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5503 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5505 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5507 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5508 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5509 c0f61fa4 2022-07-11 mark bs->selected_line);
5510 c0f61fa4 2022-07-11 mark if (id == NULL)
5513 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5516 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5520 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5521 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5522 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5523 145b6838 2022-06-16 stsp view->x = 0;
5525 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5526 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5529 640cd7ff 2022-06-22 mark view->count = 0;
5533 bcbd79e2 2018-08-19 stsp return err;
5536 4ed7e80c 2018-05-20 stsp static const struct got_error *
5537 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5539 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5540 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5541 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5542 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5543 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5544 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5545 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5546 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5547 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5548 3dbaef42 2020-11-24 stsp const char *errstr;
5549 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5550 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5552 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5553 26ed57b2 2018-05-19 stsp switch (ch) {
5555 64453f7e 2020-11-21 stsp force_text_diff = 1;
5558 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5560 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5561 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5562 5a20d08d 2022-02-09 op errstr, errstr);
5564 09b5bff8 2020-02-23 naddy case 'r':
5565 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5566 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5567 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5569 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5572 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5575 17020d27 2019-03-07 stsp usage_diff();
5576 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5580 26ed57b2 2018-05-19 stsp argc -= optind;
5581 26ed57b2 2018-05-19 stsp argv += optind;
5583 26ed57b2 2018-05-19 stsp if (argc == 0) {
5584 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5585 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5586 15a94983 2018-12-23 stsp id_str1 = argv[0];
5587 15a94983 2018-12-23 stsp id_str2 = argv[1];
5589 26ed57b2 2018-05-19 stsp usage_diff();
5591 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5592 0ae84acc 2022-06-15 tracey if (error)
5593 0ae84acc 2022-06-15 tracey goto done;
5595 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5596 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5597 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5598 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5599 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5600 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5601 c156c7a4 2020-12-18 stsp goto done;
5602 a273ac94 2020-02-23 naddy if (worktree)
5603 a273ac94 2020-02-23 naddy repo_path =
5604 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5606 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5607 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5608 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5609 c156c7a4 2020-12-18 stsp goto done;
5613 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5614 eb6600df 2019-01-04 stsp if (error)
5615 eb6600df 2019-01-04 stsp goto done;
5617 a273ac94 2020-02-23 naddy init_curses();
5619 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5620 51a10b52 2020-12-26 stsp if (error)
5621 51a10b52 2020-12-26 stsp goto done;
5623 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5624 26ed57b2 2018-05-19 stsp if (error)
5625 26ed57b2 2018-05-19 stsp goto done;
5627 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5628 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5629 26ed57b2 2018-05-19 stsp if (error)
5630 26ed57b2 2018-05-19 stsp goto done;
5632 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5633 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5634 26ed57b2 2018-05-19 stsp if (error)
5635 26ed57b2 2018-05-19 stsp goto done;
5637 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5638 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5639 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5640 ea5e7bb5 2018-08-01 stsp goto done;
5642 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5643 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5644 5dc9f4bc 2018-08-04 stsp if (error)
5645 5dc9f4bc 2018-08-04 stsp goto done;
5646 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5648 3dbaef42 2020-11-24 stsp free(label1);
5649 3dbaef42 2020-11-24 stsp free(label2);
5650 c02c541e 2019-03-29 stsp free(repo_path);
5651 a273ac94 2020-02-23 naddy free(cwd);
5652 1d0f4054 2021-06-17 stsp if (repo) {
5653 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5654 1d0f4054 2021-06-17 stsp if (error == NULL)
5655 1d0f4054 2021-06-17 stsp error = close_err;
5657 a273ac94 2020-02-23 naddy if (worktree)
5658 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5659 0ae84acc 2022-06-15 tracey if (pack_fds) {
5660 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5661 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5662 0ae84acc 2022-06-15 tracey if (error == NULL)
5663 0ae84acc 2022-06-15 tracey error = pack_err;
5665 51a10b52 2020-12-26 stsp tog_free_refs();
5666 26ed57b2 2018-05-19 stsp return error;
5669 4ed7e80c 2018-05-20 stsp __dead static void
5670 9f7d7167 2018-04-29 stsp usage_blame(void)
5673 87411fa9 2022-06-16 stsp fprintf(stderr,
5674 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5675 9f7d7167 2018-04-29 stsp getprogname());
5679 84451b3e 2018-07-10 stsp struct tog_blame_line {
5680 84451b3e 2018-07-10 stsp int annotated;
5681 84451b3e 2018-07-10 stsp struct got_object_id *id;
5684 4ed7e80c 2018-05-20 stsp static const struct got_error *
5685 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5687 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5688 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5689 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5690 84451b3e 2018-07-10 stsp const struct got_error *err;
5691 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5692 826082fe 2020-12-10 stsp char *line = NULL;
5693 826082fe 2020-12-10 stsp size_t linesize = 0;
5694 826082fe 2020-12-10 stsp ssize_t linelen;
5695 84451b3e 2018-07-10 stsp wchar_t *wline;
5696 27a741e5 2019-09-11 stsp int width;
5697 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5698 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5699 ab089a2a 2018-07-12 stsp char *id_str;
5700 11b20872 2019-11-08 stsp struct tog_color *tc;
5702 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5704 ab089a2a 2018-07-12 stsp return err;
5706 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5707 f7d12f7e 2018-08-01 stsp werase(view->window);
5709 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5710 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5711 ab089a2a 2018-07-12 stsp free(id_str);
5712 ab089a2a 2018-07-12 stsp return err;
5715 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5716 ab089a2a 2018-07-12 stsp free(line);
5717 2550e4c3 2018-07-13 stsp line = NULL;
5719 1cae65b4 2019-09-22 stsp return err;
5720 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5721 a3404814 2018-09-02 stsp wstandout(view->window);
5722 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5724 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5725 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5726 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5727 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5729 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5730 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5731 a3404814 2018-09-02 stsp wstandend(view->window);
5732 2550e4c3 2018-07-13 stsp free(wline);
5733 2550e4c3 2018-07-13 stsp wline = NULL;
5735 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5736 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5738 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5739 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5740 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5741 ab089a2a 2018-07-12 stsp free(id_str);
5742 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5744 ab089a2a 2018-07-12 stsp free(id_str);
5745 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5746 3f60a8ef 2018-07-10 stsp free(line);
5747 2550e4c3 2018-07-13 stsp line = NULL;
5749 3f60a8ef 2018-07-10 stsp return err;
5750 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5751 2550e4c3 2018-07-13 stsp free(wline);
5752 2550e4c3 2018-07-13 stsp wline = NULL;
5753 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5754 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5756 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5757 145b6838 2022-06-16 stsp view->maxx = 0;
5758 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5759 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5760 826082fe 2020-12-10 stsp if (linelen == -1) {
5761 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5762 826082fe 2020-12-10 stsp s->eof = 1;
5765 84451b3e 2018-07-10 stsp free(line);
5766 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5768 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5770 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5773 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5774 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5775 1853e0f4 2022-06-16 stsp if (err) {
5776 1853e0f4 2022-06-16 stsp free(line);
5777 1853e0f4 2022-06-16 stsp return err;
5779 1853e0f4 2022-06-16 stsp free(wline);
5780 1853e0f4 2022-06-16 stsp wline = NULL;
5781 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5783 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5784 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5786 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5787 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5788 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5789 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5790 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5791 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5792 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5793 8d0fe45a 2019-08-12 stsp char *id_str;
5794 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5795 87411fa9 2022-06-16 stsp blame_line->id);
5796 8d0fe45a 2019-08-12 stsp if (err) {
5797 8d0fe45a 2019-08-12 stsp free(line);
5798 8d0fe45a 2019-08-12 stsp return err;
5800 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5802 11b20872 2019-11-08 stsp wattr_on(view->window,
5803 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5804 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5806 11b20872 2019-11-08 stsp wattr_off(view->window,
5807 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5808 8d0fe45a 2019-08-12 stsp free(id_str);
5809 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5811 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5812 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5815 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5816 ee41ec32 2018-07-10 stsp prev_id = NULL;
5819 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5820 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5821 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5823 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5824 41605754 2020-11-12 stsp width = 9;
5825 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5826 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5827 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5828 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5829 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5830 41605754 2020-11-12 stsp if (err) {
5831 41605754 2020-11-12 stsp free(line);
5832 41605754 2020-11-12 stsp return err;
5834 41605754 2020-11-12 stsp width += 9;
5837 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5838 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5839 4cb9d869 2022-06-16 stsp if (err) {
5840 4cb9d869 2022-06-16 stsp free(line);
5841 4cb9d869 2022-06-16 stsp return err;
5843 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5844 a75b3e2d 2022-06-16 stsp width += 9;
5845 41605754 2020-11-12 stsp free(wline);
5846 41605754 2020-11-12 stsp wline = NULL;
5849 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5850 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5851 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5852 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5854 826082fe 2020-12-10 stsp free(line);
5855 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5857 9b058f45 2022-06-30 mark view_border(view);
5859 84451b3e 2018-07-10 stsp return NULL;
5862 84451b3e 2018-07-10 stsp static const struct got_error *
5863 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5864 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5866 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5867 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5868 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5869 1a76625f 2018-10-22 stsp int errcode;
5871 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5872 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5873 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5875 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5876 1a76625f 2018-10-22 stsp if (errcode)
5877 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5879 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5880 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5881 d68a0a7d 2018-07-10 stsp goto done;
5884 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5885 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5887 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5888 d68a0a7d 2018-07-10 stsp if (line->annotated)
5889 d68a0a7d 2018-07-10 stsp goto done;
5891 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5892 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5893 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5894 84451b3e 2018-07-10 stsp goto done;
5896 84451b3e 2018-07-10 stsp line->annotated = 1;
5898 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5899 1a76625f 2018-10-22 stsp if (errcode)
5900 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5901 84451b3e 2018-07-10 stsp return err;
5904 84451b3e 2018-07-10 stsp static void *
5905 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5907 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5908 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5909 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5910 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5911 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5913 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5914 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5915 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5917 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5918 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5919 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5920 e6e73e55 2022-06-30 tracey goto done;
5923 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5924 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5925 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5926 e6e73e55 2022-06-30 tracey goto done;
5928 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5929 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5930 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5931 e6e73e55 2022-06-30 tracey goto done;
5934 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5936 e6e73e55 2022-06-30 tracey goto done;
5938 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5939 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5940 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5941 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5942 fc06ba56 2019-08-22 stsp err = NULL;
5944 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5945 e6e73e55 2022-06-30 tracey if (errcode) {
5946 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5947 e6e73e55 2022-06-30 tracey goto done;
5950 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5951 1d0f4054 2021-06-17 stsp if (err == NULL)
5952 1d0f4054 2021-06-17 stsp err = close_err;
5953 c9beca56 2018-07-22 stsp ta->repo = NULL;
5954 c9beca56 2018-07-22 stsp *ta->complete = 1;
5956 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5957 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5958 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5961 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5962 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5963 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5964 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5965 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5966 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5967 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5968 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5970 18430de3 2018-07-10 stsp return (void *)err;
5973 245d91c1 2018-07-12 stsp static struct got_object_id *
5974 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5975 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5977 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5979 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5980 8d0fe45a 2019-08-12 stsp return NULL;
5982 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5983 c0f61fa4 2022-07-11 mark if (!line->annotated)
5984 c0f61fa4 2022-07-11 mark return NULL;
5986 c0f61fa4 2022-07-11 mark return line->id;
5989 c0f61fa4 2022-07-11 mark static struct got_object_id *
5990 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5991 c0f61fa4 2022-07-11 mark int lineno)
5993 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5995 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5996 c0f61fa4 2022-07-11 mark return NULL;
5998 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5999 245d91c1 2018-07-12 stsp if (!line->annotated)
6000 245d91c1 2018-07-12 stsp return NULL;
6002 245d91c1 2018-07-12 stsp return line->id;
6005 b880a918 2018-07-10 stsp static const struct got_error *
6006 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6008 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6011 245d91c1 2018-07-12 stsp if (blame->thread) {
6012 1a76625f 2018-10-22 stsp int errcode;
6013 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6014 1a76625f 2018-10-22 stsp if (errcode)
6015 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6016 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6017 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6018 1a76625f 2018-10-22 stsp if (errcode)
6019 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6020 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6021 1a76625f 2018-10-22 stsp if (errcode)
6022 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6023 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6024 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6025 245d91c1 2018-07-12 stsp err = NULL;
6026 245d91c1 2018-07-12 stsp blame->thread = NULL;
6028 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6029 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6030 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6031 1d0f4054 2021-06-17 stsp if (err == NULL)
6032 1d0f4054 2021-06-17 stsp err = close_err;
6033 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6035 245d91c1 2018-07-12 stsp if (blame->f) {
6036 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6037 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6038 245d91c1 2018-07-12 stsp blame->f = NULL;
6040 57670559 2018-12-23 stsp if (blame->lines) {
6041 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6042 57670559 2018-12-23 stsp free(blame->lines[i].id);
6043 57670559 2018-12-23 stsp free(blame->lines);
6044 57670559 2018-12-23 stsp blame->lines = NULL;
6046 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6047 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6048 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
6049 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6050 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
6051 0ae84acc 2022-06-15 tracey if (err == NULL)
6052 0ae84acc 2022-06-15 tracey err = pack_err;
6053 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
6055 245d91c1 2018-07-12 stsp return err;
6058 245d91c1 2018-07-12 stsp static const struct got_error *
6059 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6061 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6062 fc06ba56 2019-08-22 stsp int *done = arg;
6063 fc06ba56 2019-08-22 stsp int errcode;
6065 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6066 fc06ba56 2019-08-22 stsp if (errcode)
6067 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6068 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6070 fc06ba56 2019-08-22 stsp if (*done)
6071 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6073 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6074 fc06ba56 2019-08-22 stsp if (errcode)
6075 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6076 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6078 fc06ba56 2019-08-22 stsp return err;
6081 fc06ba56 2019-08-22 stsp static const struct got_error *
6082 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6084 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6085 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6086 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6087 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6088 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6089 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6090 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6091 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6092 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6094 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6095 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6097 15a94983 2018-12-23 stsp return err;
6099 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6100 eb81bc23 2022-06-28 tracey if (fd == -1) {
6101 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6102 eb81bc23 2022-06-28 tracey goto done;
6105 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6107 a44927cc 2022-04-07 stsp goto done;
6109 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6111 84451b3e 2018-07-10 stsp goto done;
6113 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6114 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6115 84451b3e 2018-07-10 stsp goto done;
6118 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6120 a70480e0 2018-06-23 stsp goto done;
6121 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6122 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6123 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6124 84451b3e 2018-07-10 stsp goto done;
6126 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6127 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6129 1fddf795 2021-01-20 stsp goto done;
6130 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6131 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6132 84451b3e 2018-07-10 stsp goto done;
6135 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6136 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6137 b02560ec 2019-08-19 stsp blame->nlines--;
6139 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6140 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6141 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6142 84451b3e 2018-07-10 stsp goto done;
6145 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6147 bd24772e 2018-07-11 stsp goto done;
6148 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6149 0ae84acc 2022-06-15 tracey pack_fds);
6150 0ae84acc 2022-06-15 tracey if (err)
6151 0ae84acc 2022-06-15 tracey goto done;
6153 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6154 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6155 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6156 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6157 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6158 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6159 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6160 245d91c1 2018-07-12 stsp goto done;
6162 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6164 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6165 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6166 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6167 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6168 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6169 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6170 a5388363 2020-12-01 naddy s->blame_complete = 0;
6172 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6173 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6174 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6175 f5a09613 2020-12-13 naddy s->selected_line = 1;
6177 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6180 a44927cc 2022-04-07 stsp if (commit)
6181 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6182 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6183 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6185 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6186 27d434c2 2018-09-15 stsp free(obj_id);
6188 245d91c1 2018-07-12 stsp stop_blame(blame);
6189 245d91c1 2018-07-12 stsp return err;
6192 245d91c1 2018-07-12 stsp static const struct got_error *
6193 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6194 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6196 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6197 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6199 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6201 c4843652 2019-08-12 stsp s->path = strdup(path);
6202 c4843652 2019-08-12 stsp if (s->path == NULL)
6203 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6205 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6206 c4843652 2019-08-12 stsp if (err) {
6207 c4843652 2019-08-12 stsp free(s->path);
6208 7cbe629d 2018-08-04 stsp return err;
6211 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6212 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6213 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6214 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6215 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6216 fb2756b9 2018-08-04 stsp s->repo = repo;
6217 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6218 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6220 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6221 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6222 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6223 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6225 11b20872 2019-11-08 stsp return err;
6228 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6229 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6230 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6231 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6232 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6233 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6234 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6236 a5388363 2020-12-01 naddy return run_blame(view);
6239 e5a0f69f 2018-08-18 stsp static const struct got_error *
6240 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6242 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6243 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6245 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6246 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6248 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6249 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6250 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6251 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6252 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6255 e5a0f69f 2018-08-18 stsp free(s->path);
6256 11b20872 2019-11-08 stsp free_colors(&s->colors);
6257 e5a0f69f 2018-08-18 stsp return err;
6260 7cbe629d 2018-08-04 stsp static const struct got_error *
6261 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6263 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6265 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6266 6c4c42e0 2019-06-24 stsp return NULL;
6269 eaf2c13e 2022-09-17 mark static void
6270 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6271 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6273 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6275 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6276 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6277 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6278 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6279 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6280 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6281 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6284 6c4c42e0 2019-06-24 stsp static const struct got_error *
6285 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6287 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6288 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6289 2b380cc8 2018-10-24 stsp int errcode;
6291 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6292 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6293 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6294 2b380cc8 2018-10-24 stsp if (errcode)
6295 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6297 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6300 51fe7530 2019-08-19 stsp if (s->blame_complete)
6301 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6303 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6305 9b058f45 2022-06-30 mark view_border(view);
6306 e5a0f69f 2018-08-18 stsp return err;
6309 e5a0f69f 2018-08-18 stsp static const struct got_error *
6310 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6311 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6313 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6314 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6316 05f04cdf 2022-07-20 mark *new_view = NULL;
6318 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6319 05f04cdf 2022-07-20 mark if (log_view == NULL)
6320 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6322 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6324 05f04cdf 2022-07-20 mark view_close(log_view);
6326 05f04cdf 2022-07-20 mark *new_view = log_view;
6328 05f04cdf 2022-07-20 mark return err;
6331 05f04cdf 2022-07-20 mark static const struct got_error *
6332 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6334 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6335 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6336 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6337 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6339 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6340 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6341 9b058f45 2022-06-30 mark --eos; /* border */
6343 e5a0f69f 2018-08-18 stsp switch (ch) {
6346 145b6838 2022-06-16 stsp case KEY_RIGHT:
6348 145b6838 2022-06-16 stsp case KEY_LEFT:
6350 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
6353 1e37a5c2 2019-05-12 jcs s->done = 1;
6355 4deef56f 2021-09-02 naddy case 'g':
6356 4deef56f 2021-09-02 naddy case KEY_HOME:
6357 4deef56f 2021-09-02 naddy s->selected_line = 1;
6358 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6359 640cd7ff 2022-06-22 mark view->count = 0;
6361 4deef56f 2021-09-02 naddy case 'G':
6362 4deef56f 2021-09-02 naddy case KEY_END:
6363 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6364 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6365 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6367 9b058f45 2022-06-30 mark s->selected_line = eos;
6368 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6370 640cd7ff 2022-06-22 mark view->count = 0;
6373 1e37a5c2 2019-05-12 jcs case KEY_UP:
6374 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6375 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6376 1e37a5c2 2019-05-12 jcs s->selected_line--;
6377 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6378 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6379 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6381 640cd7ff 2022-06-22 mark view->count = 0;
6383 83cc4199 2022-06-13 stsp case CTRL('u'):
6385 83cc4199 2022-06-13 stsp nscroll /= 2;
6386 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6387 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6388 ea025d1d 2020-02-22 naddy case CTRL('b'):
6390 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6391 640cd7ff 2022-06-22 mark if (view->count > 1)
6392 640cd7ff 2022-06-22 mark nscroll += nscroll;
6393 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6394 640cd7ff 2022-06-22 mark view->count = 0;
6397 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6398 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6400 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6403 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6404 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6405 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6406 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6407 1e37a5c2 2019-05-12 jcs s->selected_line++;
6408 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6409 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6411 640cd7ff 2022-06-22 mark view->count = 0;
6414 1e37a5c2 2019-05-12 jcs case 'p': {
6415 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6417 640cd7ff 2022-06-22 mark view->count = 0;
6418 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6419 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6420 1e37a5c2 2019-05-12 jcs if (id == NULL)
6422 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6423 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6424 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6425 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6426 1e37a5c2 2019-05-12 jcs int obj_type;
6427 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6428 1e37a5c2 2019-05-12 jcs s->repo, id);
6431 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6432 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6433 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6434 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6437 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6438 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6439 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6442 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6443 a44927cc 2022-04-07 stsp pcommit, s->path);
6444 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6445 e5a0f69f 2018-08-18 stsp if (err) {
6446 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6447 1e37a5c2 2019-05-12 jcs err = NULL;
6448 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6451 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6453 1e37a5c2 2019-05-12 jcs free(blob_id);
6454 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6455 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6456 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6459 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6460 d7b5a0e8 2022-04-20 stsp &pid->id);
6461 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6463 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6464 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6466 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6471 1e37a5c2 2019-05-12 jcs s->done = 1;
6472 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6473 1e37a5c2 2019-05-12 jcs s->done = 0;
6474 1e37a5c2 2019-05-12 jcs if (thread_err)
6476 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6477 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6478 a5388363 2020-12-01 naddy err = run_blame(view);
6483 61417565 2022-06-20 mark case 'C': {
6484 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6486 640cd7ff 2022-06-22 mark view->count = 0;
6487 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6488 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6490 1e37a5c2 2019-05-12 jcs s->done = 1;
6491 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6492 1e37a5c2 2019-05-12 jcs s->done = 0;
6493 1e37a5c2 2019-05-12 jcs if (thread_err)
6495 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6496 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6497 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6498 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6499 a5388363 2020-12-01 naddy err = run_blame(view);
6505 05f04cdf 2022-07-20 mark view->count = 0;
6506 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6507 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6508 136e2bd4 2022-07-23 mark if (s->id_to_log)
6509 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6511 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6512 1e37a5c2 2019-05-12 jcs case '\r': {
6513 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6514 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6515 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6517 640cd7ff 2022-06-22 mark view->count = 0;
6518 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6519 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6520 1e37a5c2 2019-05-12 jcs if (id == NULL)
6522 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6525 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6526 c0f61fa4 2022-07-11 mark if (*new_view) {
6527 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6528 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6531 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6533 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6534 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6536 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6537 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6538 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6539 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6540 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6544 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6545 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6546 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6548 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6551 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6552 c0f61fa4 2022-07-11 mark s->selected_line;
6553 c0f61fa4 2022-07-11 mark if (*new_view)
6554 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6555 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6556 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6557 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6562 e78dc838 2020-12-04 stsp view->focussed = 0;
6563 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6564 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6565 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6566 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6567 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6568 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6571 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6574 e78dc838 2020-12-04 stsp view->focus_child = 1;
6576 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6581 83cc4199 2022-06-13 stsp case CTRL('d'):
6583 83cc4199 2022-06-13 stsp nscroll /= 2;
6584 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6585 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6586 ea025d1d 2020-02-22 naddy case CTRL('f'):
6589 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6590 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6591 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6592 640cd7ff 2022-06-22 mark view->count = 0;
6595 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6596 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6597 83cc4199 2022-06-13 stsp s->selected_line +=
6598 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6599 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6601 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6602 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6604 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6605 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6607 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6608 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6609 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6610 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6614 640cd7ff 2022-06-22 mark view->count = 0;
6617 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6620 917d79a7 2022-07-01 stsp static const struct got_error *
6621 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6623 917d79a7 2022-07-01 stsp const struct got_error *err;
6624 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6626 917d79a7 2022-07-01 stsp view->count = 0;
6627 917d79a7 2022-07-01 stsp s->done = 1;
6628 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6629 917d79a7 2022-07-01 stsp s->done = 0;
6631 917d79a7 2022-07-01 stsp return err;
6632 917d79a7 2022-07-01 stsp return run_blame(view);
6635 a70480e0 2018-06-23 stsp static const struct got_error *
6636 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6638 a70480e0 2018-06-23 stsp const struct got_error *error;
6639 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6640 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6641 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6642 0587e10c 2020-07-23 stsp char *link_target = NULL;
6643 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6644 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6645 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6647 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6648 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6650 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6651 a70480e0 2018-06-23 stsp switch (ch) {
6653 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6656 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6657 69069811 2018-08-02 stsp if (repo_path == NULL)
6658 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6662 17020d27 2019-03-07 stsp usage_blame();
6663 a70480e0 2018-06-23 stsp /* NOTREACHED */
6667 a70480e0 2018-06-23 stsp argc -= optind;
6668 a70480e0 2018-06-23 stsp argv += optind;
6670 f135c941 2020-02-20 stsp if (argc != 1)
6671 a70480e0 2018-06-23 stsp usage_blame();
6673 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6674 0ae84acc 2022-06-15 tracey if (error != NULL)
6675 0ae84acc 2022-06-15 tracey goto done;
6677 69069811 2018-08-02 stsp if (repo_path == NULL) {
6678 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6679 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6680 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6681 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6682 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6683 c156c7a4 2020-12-18 stsp goto done;
6684 f135c941 2020-02-20 stsp if (worktree)
6685 eb41ed75 2019-02-05 stsp repo_path =
6686 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6688 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6689 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6690 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6691 c156c7a4 2020-12-18 stsp goto done;
6695 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6696 c02c541e 2019-03-29 stsp if (error != NULL)
6697 8e94dd5b 2019-01-04 stsp goto done;
6699 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6700 f135c941 2020-02-20 stsp worktree);
6701 c02c541e 2019-03-29 stsp if (error)
6702 92205607 2019-01-04 stsp goto done;
6704 f135c941 2020-02-20 stsp init_curses();
6706 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6707 51a10b52 2020-12-26 stsp if (error)
6708 51a10b52 2020-12-26 stsp goto done;
6710 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6711 eb41ed75 2019-02-05 stsp if (error)
6712 69069811 2018-08-02 stsp goto done;
6714 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6715 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6716 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6717 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6718 a70480e0 2018-06-23 stsp if (error != NULL)
6719 66b4983c 2018-06-23 stsp goto done;
6720 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6721 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6723 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6724 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6726 a19e88aa 2018-06-23 stsp if (error != NULL)
6727 8b473291 2019-02-21 stsp goto done;
6729 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6730 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6731 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6732 e1cd8fed 2018-08-01 stsp goto done;
6735 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6736 a44927cc 2022-04-07 stsp if (error)
6737 a44927cc 2022-04-07 stsp goto done;
6739 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6740 a44927cc 2022-04-07 stsp commit, repo);
6741 7cbe629d 2018-08-04 stsp if (error)
6742 7cbe629d 2018-08-04 stsp goto done;
6744 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6745 78756c87 2020-11-24 stsp commit_id, repo);
6746 0587e10c 2020-07-23 stsp if (error)
6747 0587e10c 2020-07-23 stsp goto done;
6748 12314ad4 2019-08-31 stsp if (worktree) {
6749 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6750 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6751 12314ad4 2019-08-31 stsp worktree = NULL;
6753 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6755 69069811 2018-08-02 stsp free(repo_path);
6756 f135c941 2020-02-20 stsp free(in_repo_path);
6757 0587e10c 2020-07-23 stsp free(link_target);
6758 69069811 2018-08-02 stsp free(cwd);
6759 a70480e0 2018-06-23 stsp free(commit_id);
6760 a44927cc 2022-04-07 stsp if (commit)
6761 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6762 eb41ed75 2019-02-05 stsp if (worktree)
6763 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6764 1d0f4054 2021-06-17 stsp if (repo) {
6765 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6766 1d0f4054 2021-06-17 stsp if (error == NULL)
6767 1d0f4054 2021-06-17 stsp error = close_err;
6769 0ae84acc 2022-06-15 tracey if (pack_fds) {
6770 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6771 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6772 0ae84acc 2022-06-15 tracey if (error == NULL)
6773 0ae84acc 2022-06-15 tracey error = pack_err;
6775 51a10b52 2020-12-26 stsp tog_free_refs();
6776 a70480e0 2018-06-23 stsp return error;
6779 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6780 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6782 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6783 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6784 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6785 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6786 0c6ad1bc 2022-09-09 mark char *index = NULL;
6787 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6788 d627976f 2023-02-01 mark int width, n, nentries, scrollx, i = 1;
6789 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6791 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6792 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6793 9b058f45 2022-06-30 mark --limit; /* border */
6795 f7d12f7e 2018-08-01 stsp werase(view->window);
6797 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6798 ffd1d5e5 2018-06-23 stsp return NULL;
6800 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6803 ffd1d5e5 2018-06-23 stsp return err;
6804 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6805 a3404814 2018-09-02 stsp wstandout(view->window);
6806 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6808 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6809 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6810 0c6ad1bc 2022-09-09 mark free(wline);
6811 0c6ad1bc 2022-09-09 mark wline = NULL;
6812 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6813 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6815 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6816 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6817 a3404814 2018-09-02 stsp wstandend(view->window);
6818 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6819 0c6ad1bc 2022-09-09 mark return NULL;
6821 df68a56b 2022-08-12 mark i += s->selected;
6822 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6823 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6824 df68a56b 2022-08-12 mark if (s->tree != s->root)
6825 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6827 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6828 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6829 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6830 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6831 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6832 0c6ad1bc 2022-09-09 mark free(index);
6834 ce52c690 2018-06-23 stsp return err;
6835 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6836 2550e4c3 2018-07-13 stsp free(wline);
6837 2550e4c3 2018-07-13 stsp wline = NULL;
6838 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6839 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6840 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6841 ffd1d5e5 2018-06-23 stsp return NULL;
6842 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6843 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6844 a1eca9bb 2018-06-23 stsp return NULL;
6846 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6847 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6848 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6849 0cf4efb1 2018-09-29 stsp if (view->focussed)
6850 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6851 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6853 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6854 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6855 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6856 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6857 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6858 ffd1d5e5 2018-06-23 stsp return NULL;
6862 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6865 d627976f 2023-02-01 mark view->maxx = 0;
6866 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6867 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6868 848d6979 2019-08-12 stsp const char *modestr = "";
6869 56e0773d 2019-11-28 stsp mode_t mode;
6871 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6872 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6874 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6875 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6876 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6878 638f9024 2019-05-13 stsp return got_error_from_errno(
6879 230a42bd 2019-05-11 jcs "got_object_id_str");
6881 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6882 63c5ca5d 2019-08-24 stsp modestr = "$";
6883 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6886 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6887 d86d3b18 2020-12-01 naddy te, s->repo);
6888 0d6c6ee3 2020-05-20 stsp if (err) {
6889 0d6c6ee3 2020-05-20 stsp free(id_str);
6890 0d6c6ee3 2020-05-20 stsp return err;
6892 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6893 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6894 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6896 848d6979 2019-08-12 stsp modestr = "@";
6898 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6899 848d6979 2019-08-12 stsp modestr = "/";
6900 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6901 848d6979 2019-08-12 stsp modestr = "*";
6902 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6903 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6904 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6905 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6906 1d13200f 2018-07-12 stsp free(id_str);
6907 0d6c6ee3 2020-05-20 stsp free(link_target);
6908 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6910 1d13200f 2018-07-12 stsp free(id_str);
6911 0d6c6ee3 2020-05-20 stsp free(link_target);
6913 d627976f 2023-02-01 mark /* use full line width to determine view->maxx */
6914 d627976f 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6915 ffd1d5e5 2018-06-23 stsp if (err) {
6916 ffd1d5e5 2018-06-23 stsp free(line);
6919 d627976f 2023-02-01 mark view->maxx = MAX(view->maxx, width);
6920 d627976f 2023-02-01 mark free(wline);
6921 d627976f 2023-02-01 mark wline = NULL;
6923 d627976f 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
6924 d627976f 2023-02-01 mark view->ncols, 0, 0);
6925 d627976f 2023-02-01 mark if (err) {
6926 d627976f 2023-02-01 mark free(line);
6929 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6930 0cf4efb1 2018-09-29 stsp if (view->focussed)
6931 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6932 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6934 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6936 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6937 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6938 d627976f 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
6940 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6941 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6942 b637f33f 2023-02-01 mark if (width < view->ncols)
6943 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6944 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6945 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6946 ffd1d5e5 2018-06-23 stsp free(line);
6947 2550e4c3 2018-07-13 stsp free(wline);
6948 2550e4c3 2018-07-13 stsp wline = NULL;
6950 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6951 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6952 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6956 ffd1d5e5 2018-06-23 stsp return err;
6959 ffd1d5e5 2018-06-23 stsp static void
6960 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6962 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6963 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6964 fa86c4bf 2020-11-29 stsp int i = 0;
6966 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6969 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6970 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6971 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6972 fa86c4bf 2020-11-29 stsp if (!isroot)
6973 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6976 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6977 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6981 9b058f45 2022-06-30 mark static const struct got_error *
6982 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6984 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6985 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6986 ffd1d5e5 2018-06-23 stsp int n = 0;
6988 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6989 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6990 694d3271 2020-12-01 naddy s->first_displayed_entry);
6992 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6994 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6995 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6996 94b80cfa 2022-08-01 mark if (last) {
6997 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6998 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
7000 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7001 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7002 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7006 9b058f45 2022-06-30 mark return NULL;
7009 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7010 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7011 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7013 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7014 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7015 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7017 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7018 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7019 56e0773d 2019-11-28 stsp + 1 /* slash */;
7021 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7023 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7024 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7025 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7027 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7028 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7029 d9765a41 2018-06-23 stsp while (pt) {
7030 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7031 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7032 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7033 cb2ebc8a 2018-06-23 stsp goto done;
7035 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7036 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7037 cb2ebc8a 2018-06-23 stsp goto done;
7039 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7042 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7043 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7044 ce52c690 2018-06-23 stsp goto done;
7048 ce52c690 2018-06-23 stsp if (err) {
7049 ce52c690 2018-06-23 stsp free(*path);
7050 ce52c690 2018-06-23 stsp *path = NULL;
7052 ce52c690 2018-06-23 stsp return err;
7055 ce52c690 2018-06-23 stsp static const struct got_error *
7056 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7057 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7058 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7060 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7061 ce52c690 2018-06-23 stsp char *path;
7062 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7064 a0de39f3 2019-08-09 stsp *new_view = NULL;
7066 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7068 ce52c690 2018-06-23 stsp return err;
7070 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7071 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7072 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7073 83ce39e3 2019-08-12 stsp goto done;
7076 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7077 e5a0f69f 2018-08-18 stsp if (err) {
7078 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7079 fc06ba56 2019-08-22 stsp err = NULL;
7080 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7082 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7084 83ce39e3 2019-08-12 stsp free(path);
7085 69efd4c4 2018-07-18 stsp return err;
7088 69efd4c4 2018-07-18 stsp static const struct got_error *
7089 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7090 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7092 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7093 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7094 69efd4c4 2018-07-18 stsp char *path;
7096 a0de39f3 2019-08-09 stsp *new_view = NULL;
7098 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7099 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7100 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7102 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7104 69efd4c4 2018-07-18 stsp return err;
7106 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7109 e5a0f69f 2018-08-18 stsp view_close(log_view);
7111 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7112 cb2ebc8a 2018-06-23 stsp free(path);
7113 cb2ebc8a 2018-06-23 stsp return err;
7116 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7117 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7118 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7120 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7121 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7122 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7123 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7125 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7126 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7128 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7129 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7130 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7132 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7134 bc573f3b 2021-07-10 stsp goto done;
7137 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7138 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7139 bc573f3b 2021-07-10 stsp * closed on demand.
7141 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7142 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7144 bc573f3b 2021-07-10 stsp goto done;
7145 bc573f3b 2021-07-10 stsp s->tree = s->root;
7147 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7148 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7149 ffd1d5e5 2018-06-23 stsp goto done;
7151 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7152 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7153 ffd1d5e5 2018-06-23 stsp goto done;
7156 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7157 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7158 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7159 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7160 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7161 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7162 9cd7cbd1 2020-12-07 stsp goto done;
7165 fb2756b9 2018-08-04 stsp s->repo = repo;
7167 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7168 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7169 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7170 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7172 c0b01bdb 2019-11-08 stsp goto done;
7173 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7174 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7176 c0b01bdb 2019-11-08 stsp goto done;
7177 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7178 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7179 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7181 c0b01bdb 2019-11-08 stsp goto done;
7183 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7184 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7185 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7187 11b20872 2019-11-08 stsp goto done;
7189 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7190 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7192 c0b01bdb 2019-11-08 stsp goto done;
7195 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7196 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7197 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7198 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7199 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7201 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7202 bc573f3b 2021-07-10 stsp if (commit)
7203 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7205 bc573f3b 2021-07-10 stsp close_tree_view(view);
7206 ad80ab7b 2018-08-04 stsp return err;
7209 e5a0f69f 2018-08-18 stsp static const struct got_error *
7210 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7212 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7214 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7215 fb2756b9 2018-08-04 stsp free(s->tree_label);
7216 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7217 6484ec90 2018-09-29 stsp free(s->commit_id);
7218 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7219 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7220 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7221 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7222 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7223 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7224 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7225 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7226 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7227 ad80ab7b 2018-08-04 stsp free(parent);
7230 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7231 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7232 bc573f3b 2021-07-10 stsp if (s->root)
7233 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7234 7c32bd05 2019-06-22 stsp return NULL;
7237 7c32bd05 2019-06-22 stsp static const struct got_error *
7238 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7240 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7242 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7243 7c32bd05 2019-06-22 stsp return NULL;
7246 7c32bd05 2019-06-22 stsp static int
7247 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7249 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7251 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7255 7c32bd05 2019-06-22 stsp static const struct got_error *
7256 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7258 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7259 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7261 7c32bd05 2019-06-22 stsp if (!view->searching) {
7262 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7263 7c32bd05 2019-06-22 stsp return NULL;
7266 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7267 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7268 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7269 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7270 56e0773d 2019-11-28 stsp s->selected_entry);
7272 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7274 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7275 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7277 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7278 56e0773d 2019-11-28 stsp s->selected_entry);
7281 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7282 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7283 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7284 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7286 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7289 7c32bd05 2019-06-22 stsp while (1) {
7290 56e0773d 2019-11-28 stsp if (te == NULL) {
7291 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7292 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7293 ac66afb8 2019-06-24 stsp return NULL;
7295 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7296 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7298 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7301 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7302 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7303 56e0773d 2019-11-28 stsp s->matched_entry = te;
7307 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7308 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7310 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7313 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7314 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7315 7c32bd05 2019-06-22 stsp s->selected = 0;
7318 e5a0f69f 2018-08-18 stsp return NULL;
7321 ad80ab7b 2018-08-04 stsp static const struct got_error *
7322 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7324 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7325 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7326 e5a0f69f 2018-08-18 stsp char *parent_path;
7328 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7330 e5a0f69f 2018-08-18 stsp return err;
7332 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7333 e5a0f69f 2018-08-18 stsp free(parent_path);
7335 9b058f45 2022-06-30 mark view_border(view);
7336 e5a0f69f 2018-08-18 stsp return err;
7339 94b80cfa 2022-08-01 mark static const struct got_error *
7340 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7342 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7343 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7344 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7345 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7346 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7347 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7349 94b80cfa 2022-08-01 mark g = view->gline;
7350 94b80cfa 2022-08-01 mark view->gline = 0;
7352 94b80cfa 2022-08-01 mark if (g == 0)
7354 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7355 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7357 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7358 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7359 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7361 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7362 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7363 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7365 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7366 94b80cfa 2022-08-01 mark last += off;
7368 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7369 94b80cfa 2022-08-01 mark s->selected = g - first;
7370 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7373 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7374 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7378 94b80cfa 2022-08-01 mark if (i < g) {
7379 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7381 94b80cfa 2022-08-01 mark return err;
7382 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7383 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7384 94b80cfa 2022-08-01 mark first + s->selected < g &&
7385 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7386 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7387 94b80cfa 2022-08-01 mark first += off;
7388 94b80cfa 2022-08-01 mark s->selected = g - first;
7390 94b80cfa 2022-08-01 mark } else if (i > g)
7391 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7393 94b80cfa 2022-08-01 mark if (g < nlines &&
7394 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7395 94b80cfa 2022-08-01 mark s->selected = g - 1;
7397 94b80cfa 2022-08-01 mark return NULL;
7400 e5a0f69f 2018-08-18 stsp static const struct got_error *
7401 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7403 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7404 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7405 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7406 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7408 94b80cfa 2022-08-01 mark if (view->gline)
7409 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7411 e5a0f69f 2018-08-18 stsp switch (ch) {
7414 d627976f 2023-02-01 mark case KEY_RIGHT:
7416 d627976f 2023-02-01 mark case KEY_LEFT:
7418 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
7421 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7422 640cd7ff 2022-06-22 mark view->count = 0;
7425 640cd7ff 2022-06-22 mark view->count = 0;
7426 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7428 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7431 640cd7ff 2022-06-22 mark view->count = 0;
7432 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7434 e4526bf5 2021-09-03 naddy case 'g':
7436 e4526bf5 2021-09-03 naddy case KEY_HOME:
7437 e4526bf5 2021-09-03 naddy s->selected = 0;
7438 640cd7ff 2022-06-22 mark view->count = 0;
7439 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7440 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7441 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7443 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7445 e4526bf5 2021-09-03 naddy case 'G':
7447 9b058f45 2022-06-30 mark case KEY_END: {
7448 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7450 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7451 9b058f45 2022-06-30 mark --eos; /* border */
7452 e4526bf5 2021-09-03 naddy s->selected = 0;
7453 640cd7ff 2022-06-22 mark view->count = 0;
7454 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7455 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7456 e4526bf5 2021-09-03 naddy if (te == NULL) {
7457 ad055527 2022-07-16 mark if (s->tree != s->root) {
7458 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7463 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7464 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7466 e4526bf5 2021-09-03 naddy if (n > 0)
7467 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7471 1e37a5c2 2019-05-12 jcs case KEY_UP:
7472 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7473 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7474 1e37a5c2 2019-05-12 jcs s->selected--;
7477 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7478 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7479 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7480 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7481 640cd7ff 2022-06-22 mark view->count = 0;
7483 83cc4199 2022-06-13 stsp case CTRL('u'):
7485 83cc4199 2022-06-13 stsp nscroll /= 2;
7486 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7487 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7488 ea025d1d 2020-02-22 naddy case CTRL('b'):
7490 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7491 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7492 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7493 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7495 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7496 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7498 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7499 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7500 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7501 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7502 640cd7ff 2022-06-22 mark view->count = 0;
7505 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7506 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7507 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7508 1e37a5c2 2019-05-12 jcs s->selected++;
7511 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7512 640cd7ff 2022-06-22 mark == NULL) {
7513 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7514 640cd7ff 2022-06-22 mark view->count = 0;
7517 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7519 83cc4199 2022-06-13 stsp case CTRL('d'):
7521 83cc4199 2022-06-13 stsp nscroll /= 2;
7522 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7523 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7524 ea025d1d 2020-02-22 naddy case CTRL('f'):
7526 48bb96f0 2022-06-20 naddy case ' ':
7527 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7529 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7530 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7531 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7532 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7534 640cd7ff 2022-06-22 mark view->count = 0;
7537 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7539 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7541 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7542 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7543 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7544 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7545 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7546 640cd7ff 2022-06-22 mark view->count = 0;
7549 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7550 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7552 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7553 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7554 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7555 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7556 1e37a5c2 2019-05-12 jcs s->selected_entry =
7557 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7558 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7559 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7560 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7564 1e37a5c2 2019-05-12 jcs free(parent);
7565 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7566 56e0773d 2019-11-28 stsp s->selected_entry))) {
7567 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7568 640cd7ff 2022-06-22 mark view->count = 0;
7569 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7570 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7573 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7574 941e9f74 2019-05-21 stsp if (err) {
7575 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7578 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7579 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7581 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7582 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7583 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7584 640cd7ff 2022-06-22 mark view->count = 0;
7587 640cd7ff 2022-06-22 mark view->count = 0;
7591 ffd1d5e5 2018-06-23 stsp return err;
7594 ffd1d5e5 2018-06-23 stsp __dead static void
7595 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7598 87411fa9 2022-06-16 stsp fprintf(stderr,
7599 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7600 ffd1d5e5 2018-06-23 stsp getprogname());
7604 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7605 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7607 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7608 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7609 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7610 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7611 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7612 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7613 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7614 4e97c21c 2020-12-06 stsp char *label = NULL;
7615 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7616 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7618 5221c383 2018-08-01 stsp struct tog_view *view;
7619 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7621 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7622 ffd1d5e5 2018-06-23 stsp switch (ch) {
7624 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7627 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7628 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7629 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7633 e99e2d15 2020-11-24 naddy usage_tree();
7634 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7638 ffd1d5e5 2018-06-23 stsp argc -= optind;
7639 ffd1d5e5 2018-06-23 stsp argv += optind;
7641 55cccc34 2020-02-20 stsp if (argc > 1)
7642 e99e2d15 2020-11-24 naddy usage_tree();
7644 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7645 0ae84acc 2022-06-15 tracey if (error != NULL)
7646 0ae84acc 2022-06-15 tracey goto done;
7648 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7649 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7650 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7651 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7652 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7653 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7654 c156c7a4 2020-12-18 stsp goto done;
7655 55cccc34 2020-02-20 stsp if (worktree)
7656 52185f70 2019-02-05 stsp repo_path =
7657 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7659 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7660 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7661 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7662 c156c7a4 2020-12-18 stsp goto done;
7666 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7667 c02c541e 2019-03-29 stsp if (error != NULL)
7668 52185f70 2019-02-05 stsp goto done;
7670 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7671 55cccc34 2020-02-20 stsp repo, worktree);
7672 55cccc34 2020-02-20 stsp if (error)
7673 55cccc34 2020-02-20 stsp goto done;
7675 55cccc34 2020-02-20 stsp init_curses();
7677 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7678 c02c541e 2019-03-29 stsp if (error)
7679 52185f70 2019-02-05 stsp goto done;
7681 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7682 51a10b52 2020-12-26 stsp if (error)
7683 51a10b52 2020-12-26 stsp goto done;
7685 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7686 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7687 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7688 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7689 4e97c21c 2020-12-06 stsp if (error)
7690 4e97c21c 2020-12-06 stsp goto done;
7691 4e97c21c 2020-12-06 stsp head_ref_name = label;
7693 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7694 4e97c21c 2020-12-06 stsp if (error == NULL)
7695 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7696 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7697 4e97c21c 2020-12-06 stsp goto done;
7698 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7699 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7700 4e97c21c 2020-12-06 stsp if (error)
7701 4e97c21c 2020-12-06 stsp goto done;
7704 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7705 a44927cc 2022-04-07 stsp if (error)
7706 a44927cc 2022-04-07 stsp goto done;
7708 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7709 5221c383 2018-08-01 stsp if (view == NULL) {
7710 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7711 5221c383 2018-08-01 stsp goto done;
7713 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7714 ad80ab7b 2018-08-04 stsp if (error)
7715 ad80ab7b 2018-08-04 stsp goto done;
7716 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7717 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7718 d91faf3b 2020-12-01 naddy in_repo_path);
7719 55cccc34 2020-02-20 stsp if (error)
7720 55cccc34 2020-02-20 stsp goto done;
7723 55cccc34 2020-02-20 stsp if (worktree) {
7724 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7725 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7726 55cccc34 2020-02-20 stsp worktree = NULL;
7728 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7730 52185f70 2019-02-05 stsp free(repo_path);
7731 e4a0e26d 2020-02-20 stsp free(cwd);
7732 ffd1d5e5 2018-06-23 stsp free(commit_id);
7733 4e97c21c 2020-12-06 stsp free(label);
7735 486cd271 2020-12-06 stsp got_ref_close(ref);
7736 1d0f4054 2021-06-17 stsp if (repo) {
7737 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7738 1d0f4054 2021-06-17 stsp if (error == NULL)
7739 1d0f4054 2021-06-17 stsp error = close_err;
7741 0ae84acc 2022-06-15 tracey if (pack_fds) {
7742 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7743 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7744 0ae84acc 2022-06-15 tracey if (error == NULL)
7745 0ae84acc 2022-06-15 tracey error = pack_err;
7747 51a10b52 2020-12-26 stsp tog_free_refs();
7748 ffd1d5e5 2018-06-23 stsp return error;
7751 6458efa5 2020-11-24 stsp static const struct got_error *
7752 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7754 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7755 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7757 6458efa5 2020-11-24 stsp s->nrefs = 0;
7758 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7759 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7760 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7761 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7762 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7765 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7766 6458efa5 2020-11-24 stsp if (re == NULL)
7767 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7769 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7770 8924d611 2020-12-26 stsp if (re->ref == NULL)
7771 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7772 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7773 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7776 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7777 6458efa5 2020-11-24 stsp return NULL;
7781 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7783 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7785 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7786 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7787 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7788 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7793 6458efa5 2020-11-24 stsp static const struct got_error *
7794 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7796 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7797 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7799 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7800 6458efa5 2020-11-24 stsp s->repo = repo;
7802 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7803 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7805 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7807 6458efa5 2020-11-24 stsp return err;
7809 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7810 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7811 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7812 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7814 6458efa5 2020-11-24 stsp goto done;
7816 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7817 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7818 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7820 6458efa5 2020-11-24 stsp goto done;
7822 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7823 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7824 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7826 cc488aa7 2022-01-23 stsp goto done;
7828 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7829 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7830 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7832 6458efa5 2020-11-24 stsp goto done;
7835 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7836 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7837 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7838 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7839 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7842 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7843 6458efa5 2020-11-24 stsp return err;
7846 6458efa5 2020-11-24 stsp static const struct got_error *
7847 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7849 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7851 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7852 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7854 6458efa5 2020-11-24 stsp return NULL;
7857 6458efa5 2020-11-24 stsp static const struct got_error *
7858 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7859 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7861 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7862 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7863 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7864 6458efa5 2020-11-24 stsp int obj_type;
7866 c42c9805 2020-11-24 stsp *commit_id = NULL;
7868 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7870 6458efa5 2020-11-24 stsp return err;
7872 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7874 6458efa5 2020-11-24 stsp goto done;
7876 6458efa5 2020-11-24 stsp switch (obj_type) {
7877 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7878 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7880 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7881 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7883 6458efa5 2020-11-24 stsp goto done;
7884 c42c9805 2020-11-24 stsp free(obj_id);
7885 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7886 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7888 6458efa5 2020-11-24 stsp goto done;
7889 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7890 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7891 c42c9805 2020-11-24 stsp goto done;
7893 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7894 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7895 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7896 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7897 c42c9805 2020-11-24 stsp goto done;
7901 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7907 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7908 c42c9805 2020-11-24 stsp if (err) {
7909 c42c9805 2020-11-24 stsp free(*commit_id);
7910 c42c9805 2020-11-24 stsp *commit_id = NULL;
7912 c42c9805 2020-11-24 stsp return err;
7915 c42c9805 2020-11-24 stsp static const struct got_error *
7916 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7917 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7919 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7920 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7921 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7923 c42c9805 2020-11-24 stsp *new_view = NULL;
7925 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7926 c42c9805 2020-11-24 stsp if (err) {
7927 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7928 c42c9805 2020-11-24 stsp return err;
7930 c42c9805 2020-11-24 stsp return NULL;
7933 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7934 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7935 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7936 6458efa5 2020-11-24 stsp goto done;
7939 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7940 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7943 6458efa5 2020-11-24 stsp view_close(log_view);
7945 6458efa5 2020-11-24 stsp *new_view = log_view;
7946 c42c9805 2020-11-24 stsp free(commit_id);
7947 6458efa5 2020-11-24 stsp return err;
7950 ce5b7c56 2019-07-09 stsp static void
7951 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7953 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7954 34ba6917 2020-11-30 stsp int i = 0;
7956 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7959 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7960 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7961 34ba6917 2020-11-30 stsp if (re == NULL)
7963 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7964 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7968 9b058f45 2022-06-30 mark static const struct got_error *
7969 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7971 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7972 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7973 6458efa5 2020-11-24 stsp int n = 0;
7975 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7976 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7978 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7980 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7981 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7982 94b80cfa 2022-08-01 mark if (last) {
7983 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7984 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7986 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7987 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7988 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7992 9b058f45 2022-06-30 mark return NULL;
7995 6458efa5 2020-11-24 stsp static const struct got_error *
7996 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7998 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8000 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8001 6458efa5 2020-11-24 stsp return NULL;
8004 6458efa5 2020-11-24 stsp static int
8005 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8007 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8009 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8013 6458efa5 2020-11-24 stsp static const struct got_error *
8014 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8016 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8017 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8019 6458efa5 2020-11-24 stsp if (!view->searching) {
8020 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8021 6458efa5 2020-11-24 stsp return NULL;
8024 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8025 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8026 6458efa5 2020-11-24 stsp if (s->selected_entry)
8027 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8029 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8030 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8032 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8033 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8035 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8036 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8039 487cd7d2 2021-12-17 stsp if (s->selected_entry)
8040 487cd7d2 2021-12-17 stsp re = s->selected_entry;
8041 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
8042 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8044 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8047 6458efa5 2020-11-24 stsp while (1) {
8048 6458efa5 2020-11-24 stsp if (re == NULL) {
8049 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8050 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8051 6458efa5 2020-11-24 stsp return NULL;
8053 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8054 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8056 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8059 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8060 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8061 6458efa5 2020-11-24 stsp s->matched_entry = re;
8065 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8066 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8068 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8071 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8072 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8073 6458efa5 2020-11-24 stsp s->selected = 0;
8076 6458efa5 2020-11-24 stsp return NULL;
8079 6458efa5 2020-11-24 stsp static const struct got_error *
8080 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8082 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8083 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8084 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8085 6458efa5 2020-11-24 stsp char *line = NULL;
8086 6458efa5 2020-11-24 stsp wchar_t *wline;
8087 6458efa5 2020-11-24 stsp struct tog_color *tc;
8088 0ed6bc90 2023-02-01 mark int width, n, scrollx;
8089 6458efa5 2020-11-24 stsp int limit = view->nlines;
8091 6458efa5 2020-11-24 stsp werase(view->window);
8093 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8094 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8095 9b058f45 2022-06-30 mark --limit; /* border */
8097 6458efa5 2020-11-24 stsp if (limit == 0)
8098 6458efa5 2020-11-24 stsp return NULL;
8100 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8102 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8103 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8104 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8106 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8107 6458efa5 2020-11-24 stsp if (err) {
8108 6458efa5 2020-11-24 stsp free(line);
8109 6458efa5 2020-11-24 stsp return err;
8111 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8112 6458efa5 2020-11-24 stsp wstandout(view->window);
8113 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8114 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8115 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8116 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8117 6458efa5 2020-11-24 stsp wstandend(view->window);
8118 6458efa5 2020-11-24 stsp free(wline);
8119 6458efa5 2020-11-24 stsp wline = NULL;
8120 6458efa5 2020-11-24 stsp free(line);
8121 6458efa5 2020-11-24 stsp line = NULL;
8122 6458efa5 2020-11-24 stsp if (--limit <= 0)
8123 6458efa5 2020-11-24 stsp return NULL;
8126 0ed6bc90 2023-02-01 mark view->maxx = 0;
8127 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8128 6458efa5 2020-11-24 stsp char *line = NULL;
8129 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8131 b4996bee 2022-06-16 stsp if (s->show_date) {
8132 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8133 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8134 b4996bee 2022-06-16 stsp struct got_object_id *id;
8135 b4996bee 2022-06-16 stsp struct tm tm;
8138 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8140 b4996bee 2022-06-16 stsp return err;
8141 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8142 b4996bee 2022-06-16 stsp if (err) {
8143 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8145 b4996bee 2022-06-16 stsp return err;
8147 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8149 b4996bee 2022-06-16 stsp if (err) {
8151 b4996bee 2022-06-16 stsp return err;
8153 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8154 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8156 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8157 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8160 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8161 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8162 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8163 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8165 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8166 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8167 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8168 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8169 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8170 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8171 6458efa5 2020-11-24 stsp struct got_object_id *id;
8172 6458efa5 2020-11-24 stsp char *id_str;
8173 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8175 6458efa5 2020-11-24 stsp return err;
8176 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8177 6458efa5 2020-11-24 stsp if (err) {
8179 6458efa5 2020-11-24 stsp return err;
8181 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8182 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8183 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8185 6458efa5 2020-11-24 stsp free(id_str);
8186 6458efa5 2020-11-24 stsp return err;
8189 6458efa5 2020-11-24 stsp free(id_str);
8190 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8191 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8192 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8194 0ed6bc90 2023-02-01 mark /* use full line width to determine view->maxx */
8195 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8196 6458efa5 2020-11-24 stsp if (err) {
8197 6458efa5 2020-11-24 stsp free(line);
8198 6458efa5 2020-11-24 stsp return err;
8200 0ed6bc90 2023-02-01 mark view->maxx = MAX(view->maxx, width);
8201 0ed6bc90 2023-02-01 mark free(wline);
8202 0ed6bc90 2023-02-01 mark wline = NULL;
8204 0ed6bc90 2023-02-01 mark err = format_line(&wline, &width, &scrollx, line, view->x,
8205 0ed6bc90 2023-02-01 mark view->ncols, 0, 0);
8206 0ed6bc90 2023-02-01 mark if (err) {
8207 0ed6bc90 2023-02-01 mark free(line);
8208 0ed6bc90 2023-02-01 mark return err;
8210 6458efa5 2020-11-24 stsp if (n == s->selected) {
8211 6458efa5 2020-11-24 stsp if (view->focussed)
8212 6458efa5 2020-11-24 stsp wstandout(view->window);
8213 6458efa5 2020-11-24 stsp s->selected_entry = re;
8215 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8217 6458efa5 2020-11-24 stsp wattr_on(view->window,
8218 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8219 0ed6bc90 2023-02-01 mark waddwstr(view->window, &wline[scrollx]);
8221 6458efa5 2020-11-24 stsp wattr_off(view->window,
8222 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8223 b637f33f 2023-02-01 mark if (width < view->ncols)
8224 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8225 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8226 6458efa5 2020-11-24 stsp wstandend(view->window);
8227 6458efa5 2020-11-24 stsp free(line);
8228 6458efa5 2020-11-24 stsp free(wline);
8229 6458efa5 2020-11-24 stsp wline = NULL;
8231 6458efa5 2020-11-24 stsp s->ndisplayed++;
8232 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8235 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8238 9b058f45 2022-06-30 mark view_border(view);
8239 6458efa5 2020-11-24 stsp return err;
8242 6458efa5 2020-11-24 stsp static const struct got_error *
8243 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8244 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8246 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8247 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8248 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8250 c42c9805 2020-11-24 stsp *new_view = NULL;
8252 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8253 c42c9805 2020-11-24 stsp if (err) {
8254 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8255 c42c9805 2020-11-24 stsp return err;
8257 c42c9805 2020-11-24 stsp return NULL;
8261 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8262 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8263 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8264 c42c9805 2020-11-24 stsp goto done;
8267 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8268 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8270 c42c9805 2020-11-24 stsp goto done;
8272 c42c9805 2020-11-24 stsp *new_view = tree_view;
8274 c42c9805 2020-11-24 stsp free(commit_id);
8275 c42c9805 2020-11-24 stsp return err;
8278 94b80cfa 2022-08-01 mark static const struct got_error *
8279 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8281 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8282 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8283 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8285 94b80cfa 2022-08-01 mark g = view->gline;
8286 94b80cfa 2022-08-01 mark view->gline = 0;
8288 94b80cfa 2022-08-01 mark if (g == 0)
8290 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8291 94b80cfa 2022-08-01 mark g = s->nrefs;
8293 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8294 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8295 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8296 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8297 94b80cfa 2022-08-01 mark return NULL;
8300 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8301 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8303 94b80cfa 2022-08-01 mark return err;
8304 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8305 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8306 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8307 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8308 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8309 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8311 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8312 94b80cfa 2022-08-01 mark s->selected = g - 1;
8314 94b80cfa 2022-08-01 mark return NULL;
8318 c42c9805 2020-11-24 stsp static const struct got_error *
8319 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8321 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8322 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8323 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8324 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8326 94b80cfa 2022-08-01 mark if (view->gline)
8327 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8329 6458efa5 2020-11-24 stsp switch (ch) {
8332 0ed6bc90 2023-02-01 mark case KEY_RIGHT:
8334 0ed6bc90 2023-02-01 mark case KEY_LEFT:
8336 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8339 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8340 640cd7ff 2022-06-22 mark view->count = 0;
8343 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8344 640cd7ff 2022-06-22 mark view->count = 0;
8347 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8348 3f6c6614 2023-01-23 mark view->action = s->sort_by_date ? "sort by date" : "sort by name";
8349 640cd7ff 2022-06-22 mark view->count = 0;
8350 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8351 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8352 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8355 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8356 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8357 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8360 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8361 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8363 6458efa5 2020-11-24 stsp case KEY_ENTER:
8364 6458efa5 2020-11-24 stsp case '\r':
8365 640cd7ff 2022-06-22 mark view->count = 0;
8366 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8368 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8371 640cd7ff 2022-06-22 mark view->count = 0;
8372 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8374 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8376 e4526bf5 2021-09-03 naddy case 'g':
8378 e4526bf5 2021-09-03 naddy case KEY_HOME:
8379 e4526bf5 2021-09-03 naddy s->selected = 0;
8380 640cd7ff 2022-06-22 mark view->count = 0;
8381 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8383 e4526bf5 2021-09-03 naddy case 'G':
8385 9b058f45 2022-06-30 mark case KEY_END: {
8386 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8388 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8389 9b058f45 2022-06-30 mark --eos; /* border */
8390 e4526bf5 2021-09-03 naddy s->selected = 0;
8391 640cd7ff 2022-06-22 mark view->count = 0;
8392 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8393 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8394 e4526bf5 2021-09-03 naddy if (re == NULL)
8396 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8397 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8399 e4526bf5 2021-09-03 naddy if (n > 0)
8400 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8404 6458efa5 2020-11-24 stsp case KEY_UP:
8405 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8406 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8407 6458efa5 2020-11-24 stsp s->selected--;
8410 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8411 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8412 640cd7ff 2022-06-22 mark view->count = 0;
8414 83cc4199 2022-06-13 stsp case CTRL('u'):
8416 83cc4199 2022-06-13 stsp nscroll /= 2;
8417 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8418 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8419 6458efa5 2020-11-24 stsp case CTRL('b'):
8421 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8422 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8423 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8424 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8425 640cd7ff 2022-06-22 mark view->count = 0;
8428 6458efa5 2020-11-24 stsp case KEY_DOWN:
8429 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8430 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8431 6458efa5 2020-11-24 stsp s->selected++;
8434 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8435 6458efa5 2020-11-24 stsp /* can't scroll any further */
8436 640cd7ff 2022-06-22 mark view->count = 0;
8439 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8441 83cc4199 2022-06-13 stsp case CTRL('d'):
8443 83cc4199 2022-06-13 stsp nscroll /= 2;
8444 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8445 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8446 6458efa5 2020-11-24 stsp case CTRL('f'):
8448 48bb96f0 2022-06-20 naddy case ' ':
8449 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8450 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8451 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8452 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8453 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8454 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8455 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8456 640cd7ff 2022-06-22 mark view->count = 0;
8459 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8461 6458efa5 2020-11-24 stsp case CTRL('l'):
8462 640cd7ff 2022-06-22 mark view->count = 0;
8463 8924d611 2020-12-26 stsp tog_free_refs();
8464 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8467 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8468 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8470 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8471 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8472 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8475 640cd7ff 2022-06-22 mark view->count = 0;
8479 6458efa5 2020-11-24 stsp return err;
8482 6458efa5 2020-11-24 stsp __dead static void
8483 6458efa5 2020-11-24 stsp usage_ref(void)
8486 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8487 6458efa5 2020-11-24 stsp getprogname());
8491 6458efa5 2020-11-24 stsp static const struct got_error *
8492 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8494 6458efa5 2020-11-24 stsp const struct got_error *error;
8495 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8496 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8497 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8499 6458efa5 2020-11-24 stsp struct tog_view *view;
8500 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8502 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8503 6458efa5 2020-11-24 stsp switch (ch) {
8505 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8506 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8507 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8511 e99e2d15 2020-11-24 naddy usage_ref();
8512 6458efa5 2020-11-24 stsp /* NOTREACHED */
8516 6458efa5 2020-11-24 stsp argc -= optind;
8517 6458efa5 2020-11-24 stsp argv += optind;
8519 6458efa5 2020-11-24 stsp if (argc > 1)
8520 e99e2d15 2020-11-24 naddy usage_ref();
8522 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8523 0ae84acc 2022-06-15 tracey if (error != NULL)
8524 0ae84acc 2022-06-15 tracey goto done;
8526 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8527 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8528 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8529 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8530 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8531 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8532 c156c7a4 2020-12-18 stsp goto done;
8533 6458efa5 2020-11-24 stsp if (worktree)
8534 6458efa5 2020-11-24 stsp repo_path =
8535 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8537 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8538 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8539 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8540 c156c7a4 2020-12-18 stsp goto done;
8544 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8545 6458efa5 2020-11-24 stsp if (error != NULL)
8546 6458efa5 2020-11-24 stsp goto done;
8548 6458efa5 2020-11-24 stsp init_curses();
8550 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8551 51a10b52 2020-12-26 stsp if (error)
8552 51a10b52 2020-12-26 stsp goto done;
8554 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8555 6458efa5 2020-11-24 stsp if (error)
8556 6458efa5 2020-11-24 stsp goto done;
8558 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8559 6458efa5 2020-11-24 stsp if (view == NULL) {
8560 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8561 6458efa5 2020-11-24 stsp goto done;
8564 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8565 6458efa5 2020-11-24 stsp if (error)
8566 6458efa5 2020-11-24 stsp goto done;
8568 6458efa5 2020-11-24 stsp if (worktree) {
8569 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8570 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8571 6458efa5 2020-11-24 stsp worktree = NULL;
8573 6458efa5 2020-11-24 stsp error = view_loop(view);
8575 6458efa5 2020-11-24 stsp free(repo_path);
8576 6458efa5 2020-11-24 stsp free(cwd);
8577 1d0f4054 2021-06-17 stsp if (repo) {
8578 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8579 1d0f4054 2021-06-17 stsp if (close_err)
8580 1d0f4054 2021-06-17 stsp error = close_err;
8582 0ae84acc 2022-06-15 tracey if (pack_fds) {
8583 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8584 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8585 0ae84acc 2022-06-15 tracey if (error == NULL)
8586 0ae84acc 2022-06-15 tracey error = pack_err;
8588 51a10b52 2020-12-26 stsp tog_free_refs();
8589 6458efa5 2020-11-24 stsp return error;
8592 ec2a9698 2022-09-15 mark static const struct got_error*
8593 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8594 ec2a9698 2022-09-15 mark const char *str)
8596 ec2a9698 2022-09-15 mark size_t len;
8598 ec2a9698 2022-09-15 mark if (win == NULL)
8599 ec2a9698 2022-09-15 mark win = stdscr;
8601 ec2a9698 2022-09-15 mark len = strlen(str);
8602 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8604 ec2a9698 2022-09-15 mark if (focus)
8605 ec2a9698 2022-09-15 mark wstandout(win);
8606 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8607 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8608 ec2a9698 2022-09-15 mark if (focus)
8609 ec2a9698 2022-09-15 mark wstandend(win);
8611 ec2a9698 2022-09-15 mark return NULL;
8614 136e2bd4 2022-07-23 mark static const struct got_error *
8615 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8619 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8620 ec2a9698 2022-09-15 mark if (p == NULL) {
8621 ec2a9698 2022-09-15 mark free(*line_offsets);
8622 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8623 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8626 ec2a9698 2022-09-15 mark *line_offsets = p;
8627 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8628 ec2a9698 2022-09-15 mark ++(*nlines);
8629 ec2a9698 2022-09-15 mark return NULL;
8632 ec2a9698 2022-09-15 mark static const struct got_error *
8633 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8637 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8638 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8639 ec2a9698 2022-09-15 mark size_t len = 1;
8641 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8644 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8645 ec2a9698 2022-09-15 mark if (t0 == NULL)
8646 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8648 ec2a9698 2022-09-15 mark len += strlen(t);
8649 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8650 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8652 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8655 ec2a9698 2022-09-15 mark return NULL;
8659 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8660 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8661 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8663 ec2a9698 2022-09-15 mark static const struct got_error *
8664 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8666 ec2a9698 2022-09-15 mark int n, len = width;
8668 ec2a9698 2022-09-15 mark if (km->keys) {
8669 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8670 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8671 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8673 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8674 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8676 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8678 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8679 ec2a9698 2022-09-15 mark if (t0 == NULL)
8680 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8682 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8683 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8684 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8685 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8686 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8687 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8688 ec2a9698 2022-09-15 mark if (n < 0) {
8690 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8692 ec2a9698 2022-09-15 mark first = 0;
8693 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8694 ec2a9698 2022-09-15 mark *off += n;
8698 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8699 ec2a9698 2022-09-15 mark if (n < 0)
8700 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8701 ec2a9698 2022-09-15 mark *off += n;
8703 ec2a9698 2022-09-15 mark return NULL;
8706 ec2a9698 2022-09-15 mark static const struct got_error *
8707 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8709 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8710 ec2a9698 2022-09-15 mark off_t off = 0;
8711 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8712 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8713 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8714 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8715 ec2a9698 2022-09-15 mark GENERATE_HELP
8716 ec2a9698 2022-09-15 mark #undef KEYMAP_
8717 ec2a9698 2022-09-15 mark #undef KEY_
8720 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8722 ec2a9698 2022-09-15 mark return err;
8724 ec2a9698 2022-09-15 mark n = nitems(km);
8725 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8727 ec2a9698 2022-09-15 mark return err;
8729 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8730 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8731 ec2a9698 2022-09-15 mark show = s->all;
8732 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8733 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8736 ec2a9698 2022-09-15 mark if (show) {
8737 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8739 ec2a9698 2022-09-15 mark return err;
8740 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8742 ec2a9698 2022-09-15 mark return err;
8745 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8747 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8748 ec2a9698 2022-09-15 mark return err;
8751 ec2a9698 2022-09-15 mark static const struct got_error *
8752 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8755 ec2a9698 2022-09-15 mark const struct got_error *err;
8757 ec2a9698 2022-09-15 mark free(s->line_offsets);
8758 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8759 ec2a9698 2022-09-15 mark s->nlines = 0;
8761 ec2a9698 2022-09-15 mark f = got_opentemp();
8762 ec2a9698 2022-09-15 mark if (f == NULL)
8763 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8766 ec2a9698 2022-09-15 mark err = format_help(s);
8768 ec2a9698 2022-09-15 mark return err;
8770 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8771 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8773 ec2a9698 2022-09-15 mark return NULL;
8776 ec2a9698 2022-09-15 mark static const struct got_error *
8777 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8779 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8780 ec2a9698 2022-09-15 mark return NULL;
8783 eaf2c13e 2022-09-17 mark static void
8784 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8785 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8787 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8789 eaf2c13e 2022-09-17 mark *f = s->f;
8790 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8791 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8792 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8793 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8794 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8795 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8798 ec2a9698 2022-09-15 mark static const struct got_error *
8799 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8801 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8802 ec2a9698 2022-09-15 mark const struct got_error *err;
8803 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8804 ec2a9698 2022-09-15 mark wchar_t *wline;
8805 ec2a9698 2022-09-15 mark char *line;
8806 ec2a9698 2022-09-15 mark ssize_t linelen;
8807 ec2a9698 2022-09-15 mark size_t linesz = 0;
8808 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8809 ec2a9698 2022-09-15 mark int eos = view->nlines;
8811 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8812 ec2a9698 2022-09-15 mark --eos; /* account for border */
8814 ec2a9698 2022-09-15 mark s->lineno = 0;
8815 ec2a9698 2022-09-15 mark rewind(s->f);
8816 ec2a9698 2022-09-15 mark werase(view->window);
8818 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8819 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8821 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8822 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8823 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8825 ec2a9698 2022-09-15 mark return err;
8826 ec2a9698 2022-09-15 mark if (eos <= 1)
8827 ec2a9698 2022-09-15 mark return NULL;
8828 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8831 ec2a9698 2022-09-15 mark s->eof = 0;
8832 ec2a9698 2022-09-15 mark view->maxx = 0;
8833 ec2a9698 2022-09-15 mark line = NULL;
8834 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8835 ec2a9698 2022-09-15 mark attr_t attr = 0;
8837 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8838 ec2a9698 2022-09-15 mark if (linelen == -1) {
8839 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8840 ec2a9698 2022-09-15 mark free(line);
8841 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8843 ec2a9698 2022-09-15 mark s->eof = 1;
8846 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8848 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8850 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8851 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8853 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8854 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8855 ec2a9698 2022-09-15 mark if (err) {
8856 ec2a9698 2022-09-15 mark free(line);
8857 ec2a9698 2022-09-15 mark return err;
8859 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8860 ec2a9698 2022-09-15 mark free(wline);
8861 ec2a9698 2022-09-15 mark wline = NULL;
8864 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8865 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8866 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8867 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8868 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8869 ec2a9698 2022-09-15 mark if (err) {
8870 ec2a9698 2022-09-15 mark free(line);
8871 ec2a9698 2022-09-15 mark return err;
8876 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8877 b637f33f 2023-02-01 mark view->x, view->ncols, 0, view->x ? 1 : 0);
8878 ec2a9698 2022-09-15 mark if (err) {
8879 ec2a9698 2022-09-15 mark free(line);
8880 ec2a9698 2022-09-15 mark return err;
8882 b637f33f 2023-02-01 mark waddwstr(view->window, &wline[skip]);
8883 ec2a9698 2022-09-15 mark free(wline);
8884 ec2a9698 2022-09-15 mark wline = NULL;
8886 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8887 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8888 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8890 b637f33f 2023-02-01 mark if (width < view->ncols)
8891 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8894 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8895 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8896 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8898 ec2a9698 2022-09-15 mark free(line);
8899 ec2a9698 2022-09-15 mark if (nprinted > 0)
8900 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8902 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8904 ec2a9698 2022-09-15 mark view_border(view);
8906 ec2a9698 2022-09-15 mark if (s->eof) {
8907 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8908 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8909 ec2a9698 2022-09-15 mark view->ncols - 1);
8910 ec2a9698 2022-09-15 mark if (rc == ERR)
8911 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8913 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8914 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8915 ec2a9698 2022-09-15 mark wstandout(view->window);
8916 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8917 ec2a9698 2022-09-15 mark view->ncols - 1);
8918 ec2a9698 2022-09-15 mark if (rc == ERR)
8919 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8920 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8921 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8922 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8923 ec2a9698 2022-09-15 mark if (rc == ERR)
8924 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8926 ec2a9698 2022-09-15 mark wstandend(view->window);
8929 ec2a9698 2022-09-15 mark return NULL;
8932 ec2a9698 2022-09-15 mark static const struct got_error *
8933 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8935 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8936 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8937 ec2a9698 2022-09-15 mark char *line = NULL;
8938 ec2a9698 2022-09-15 mark ssize_t linelen;
8939 ec2a9698 2022-09-15 mark size_t linesz = 0;
8940 ec2a9698 2022-09-15 mark int eos, nscroll;
8942 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8943 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8944 ec2a9698 2022-09-15 mark --eos; /* border */
8946 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8948 ec2a9698 2022-09-15 mark switch (ch) {
8951 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8953 ec2a9698 2022-09-15 mark case KEY_LEFT:
8955 d627976f 2023-02-01 mark horizontal_scroll_input(view, ch);
8958 ec2a9698 2022-09-15 mark case KEY_HOME:
8959 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8960 ec2a9698 2022-09-15 mark view->count = 0;
8963 ec2a9698 2022-09-15 mark case KEY_END:
8964 ec2a9698 2022-09-15 mark view->count = 0;
8965 ec2a9698 2022-09-15 mark if (s->eof)
8967 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8968 ec2a9698 2022-09-15 mark s->eof = 1;
8971 ec2a9698 2022-09-15 mark case KEY_UP:
8972 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8973 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8975 ec2a9698 2022-09-15 mark view->count = 0;
8977 ec2a9698 2022-09-15 mark case CTRL('u'):
8979 ec2a9698 2022-09-15 mark nscroll /= 2;
8980 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8981 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8982 ec2a9698 2022-09-15 mark case CTRL('b'):
8984 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8985 ec2a9698 2022-09-15 mark view->count = 0;
8988 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8989 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8992 ec2a9698 2022-09-15 mark case KEY_DOWN:
8993 ec2a9698 2022-09-15 mark case CTRL('n'):
8994 ec2a9698 2022-09-15 mark if (!s->eof)
8995 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8997 ec2a9698 2022-09-15 mark view->count = 0;
8999 ec2a9698 2022-09-15 mark case CTRL('d'):
9001 ec2a9698 2022-09-15 mark nscroll /= 2;
9002 ec2a9698 2022-09-15 mark /* FALL THROUGH */
9003 ec2a9698 2022-09-15 mark case KEY_NPAGE:
9004 ec2a9698 2022-09-15 mark case CTRL('f'):
9007 ec2a9698 2022-09-15 mark if (s->eof) {
9008 ec2a9698 2022-09-15 mark view->count = 0;
9011 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
9012 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
9013 ec2a9698 2022-09-15 mark s->first_displayed_line++;
9014 ec2a9698 2022-09-15 mark if (linelen == -1) {
9015 ec2a9698 2022-09-15 mark if (feof(s->f))
9016 ec2a9698 2022-09-15 mark s->eof = 1;
9018 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
9022 ec2a9698 2022-09-15 mark free(line);
9025 ec2a9698 2022-09-15 mark view->count = 0;
9029 ec2a9698 2022-09-15 mark return err;
9032 ec2a9698 2022-09-15 mark static const struct got_error *
9033 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
9035 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9037 ec2a9698 2022-09-15 mark free(s->line_offsets);
9038 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
9039 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
9040 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9042 ec2a9698 2022-09-15 mark return NULL;
9045 ec2a9698 2022-09-15 mark static const struct got_error *
9046 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
9048 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9051 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
9052 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
9054 ec2a9698 2022-09-15 mark wclear(view->window);
9055 ec2a9698 2022-09-15 mark view->count = 0;
9056 ec2a9698 2022-09-15 mark view->x = 0;
9057 ec2a9698 2022-09-15 mark s->all = !s->all;
9058 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9059 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9060 ec2a9698 2022-09-15 mark s->matched_line = 0;
9062 ec2a9698 2022-09-15 mark return create_help(s);
9065 ec2a9698 2022-09-15 mark static const struct got_error *
9066 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
9068 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
9069 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
9071 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
9072 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
9073 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
9074 ec2a9698 2022-09-15 mark s->selected_line = 1;
9076 ec2a9698 2022-09-15 mark view->show = show_help_view;
9077 ec2a9698 2022-09-15 mark view->input = input_help_view;
9078 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
9079 ec2a9698 2022-09-15 mark view->close = close_help_view;
9080 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
9081 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
9082 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
9084 ec2a9698 2022-09-15 mark err = create_help(s);
9085 ec2a9698 2022-09-15 mark return err;
9088 ec2a9698 2022-09-15 mark static const struct got_error *
9089 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9090 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
9092 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
9094 136e2bd4 2022-07-23 mark *new_view = NULL;
9096 136e2bd4 2022-07-23 mark switch (request) {
9097 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
9098 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9099 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9101 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9102 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9103 136e2bd4 2022-07-23 mark view, s->repo);
9105 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9106 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9108 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9109 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9110 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9112 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9113 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9116 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9117 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9119 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9120 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9121 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9122 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9123 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9124 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9125 136e2bd4 2022-07-23 mark &view->state.tree);
9126 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9127 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9128 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9129 136e2bd4 2022-07-23 mark view->state.ref.repo);
9131 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9132 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9134 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9135 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9136 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9137 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9138 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9139 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9140 136e2bd4 2022-07-23 mark view->state.log.repo);
9141 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9142 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9143 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9144 136e2bd4 2022-07-23 mark view->state.ref.repo);
9146 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9147 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9149 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9150 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9151 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9152 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9153 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9154 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9155 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9156 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9158 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9159 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9161 136e2bd4 2022-07-23 mark view_close(*new_view);
9163 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9164 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9165 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9166 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9167 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9169 ec2a9698 2022-09-15 mark view_close(*new_view);
9172 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9175 136e2bd4 2022-07-23 mark return err;
9179 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9180 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9182 6458efa5 2020-11-24 stsp static void
9183 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9185 9b058f45 2022-06-30 mark switch (view->type) {
9186 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9187 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9188 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9189 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9193 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9194 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9196 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9197 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9200 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9201 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9202 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9204 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9205 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9206 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9208 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9209 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9210 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9216 9b058f45 2022-06-30 mark view->offset = 0;
9220 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9221 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9223 9b058f45 2022-06-30 mark static const struct got_error *
9224 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9226 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9227 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9228 9b058f45 2022-06-30 mark int *selected = NULL;
9229 9b058f45 2022-06-30 mark int header, offset;
9231 9b058f45 2022-06-30 mark switch (view->type) {
9232 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9233 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9234 9b058f45 2022-06-30 mark header = 3;
9235 9b058f45 2022-06-30 mark scrolld = NULL;
9236 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9237 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9238 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9239 9b058f45 2022-06-30 mark s->selected_line -= offset;
9240 9b058f45 2022-06-30 mark view->offset = offset;
9244 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9245 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9246 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9247 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9248 9b058f45 2022-06-30 mark selected = &s->selected;
9251 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9252 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9253 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9254 9b058f45 2022-06-30 mark header = 3;
9255 9b058f45 2022-06-30 mark selected = &s->selected;
9258 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9259 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9260 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9261 9b058f45 2022-06-30 mark header = 5;
9262 9b058f45 2022-06-30 mark selected = &s->selected;
9266 9b058f45 2022-06-30 mark selected = NULL;
9267 9b058f45 2022-06-30 mark scrolld = NULL;
9268 9b058f45 2022-06-30 mark header = 0;
9272 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9273 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9274 9b058f45 2022-06-30 mark view->offset = offset;
9275 9b058f45 2022-06-30 mark if (scrolld && offset) {
9276 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9277 9b058f45 2022-06-30 mark *selected -= offset;
9281 9b058f45 2022-06-30 mark return err;
9284 9b058f45 2022-06-30 mark static void
9285 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9289 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9290 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9291 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9292 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9294 6879ba42 2020-10-01 naddy fputc('\n', fp);
9297 4ed7e80c 2018-05-20 stsp __dead static void
9298 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9300 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9302 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9303 6879ba42 2020-10-01 naddy getprogname());
9304 6879ba42 2020-10-01 naddy if (hflag) {
9305 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9306 6879ba42 2020-10-01 naddy list_commands(fp);
9308 6879ba42 2020-10-01 naddy exit(status);
9311 c2301be8 2018-04-30 stsp static char **
9312 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9314 ee85c5e8 2020-02-29 stsp va_list ap;
9315 c2301be8 2018-04-30 stsp char **argv;
9318 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9320 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9321 c2301be8 2018-04-30 stsp if (argv == NULL)
9322 c2301be8 2018-04-30 stsp err(1, "calloc");
9323 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9324 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9325 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9326 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9329 ee85c5e8 2020-02-29 stsp va_end(ap);
9330 c2301be8 2018-04-30 stsp return argv;
9334 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9335 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9336 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9337 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9338 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9340 ee85c5e8 2020-02-29 stsp static const struct got_error *
9341 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9343 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9344 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9345 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9346 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9347 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9348 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9349 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9350 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9351 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9353 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9354 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9355 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9357 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9358 0ae84acc 2022-06-15 tracey if (error != NULL)
9359 0ae84acc 2022-06-15 tracey goto done;
9361 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9362 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9363 ee85c5e8 2020-02-29 stsp goto done;
9365 ee85c5e8 2020-02-29 stsp if (worktree)
9366 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9368 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9369 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9370 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9371 ee85c5e8 2020-02-29 stsp goto done;
9374 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9375 ee85c5e8 2020-02-29 stsp if (error != NULL)
9376 ee85c5e8 2020-02-29 stsp goto done;
9378 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9379 ee85c5e8 2020-02-29 stsp repo, worktree);
9380 ee85c5e8 2020-02-29 stsp if (error)
9381 ee85c5e8 2020-02-29 stsp goto done;
9383 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9384 84de9106 2020-12-26 stsp if (error)
9385 84de9106 2020-12-26 stsp goto done;
9386 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9387 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9388 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9389 ee85c5e8 2020-02-29 stsp if (error)
9390 ee85c5e8 2020-02-29 stsp goto done;
9392 ee85c5e8 2020-02-29 stsp if (worktree) {
9393 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9394 ee85c5e8 2020-02-29 stsp worktree = NULL;
9397 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9398 a44927cc 2022-04-07 stsp if (error)
9399 a44927cc 2022-04-07 stsp goto done;
9401 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9402 ee85c5e8 2020-02-29 stsp if (error) {
9403 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9404 ee85c5e8 2020-02-29 stsp goto done;
9405 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9406 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9407 6879ba42 2020-10-01 naddy usage(1, 1);
9408 ee85c5e8 2020-02-29 stsp /* not reached */
9411 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9412 ee85c5e8 2020-02-29 stsp if (error)
9413 ee85c5e8 2020-02-29 stsp goto done;
9415 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9417 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9418 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9420 1d0f4054 2021-06-17 stsp if (repo) {
9421 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9422 1d0f4054 2021-06-17 stsp if (error == NULL)
9423 1d0f4054 2021-06-17 stsp error = close_err;
9425 a44927cc 2022-04-07 stsp if (commit)
9426 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9427 ee85c5e8 2020-02-29 stsp if (worktree)
9428 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9429 0ae84acc 2022-06-15 tracey if (pack_fds) {
9430 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9431 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9432 0ae84acc 2022-06-15 tracey if (error == NULL)
9433 0ae84acc 2022-06-15 tracey error = pack_err;
9436 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9437 ee85c5e8 2020-02-29 stsp free(commit_id);
9438 ee85c5e8 2020-02-29 stsp free(cwd);
9439 ee85c5e8 2020-02-29 stsp free(repo_path);
9440 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9441 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9443 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9444 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9445 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9447 87670572 2020-12-26 naddy tog_free_refs();
9448 ee85c5e8 2020-02-29 stsp return error;
9452 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9454 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9455 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9456 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9457 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9458 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9459 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9460 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9462 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9464 1fa0d17d 2023-02-13 op setlocale(LC_CTYPE, "");
9466 1fa0d17d 2023-02-13 op #ifndef PROFILE
9467 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9468 1fa0d17d 2023-02-13 op NULL) == -1)
9469 1fa0d17d 2023-02-13 op err(1, "pledge");
9472 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9473 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9475 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9476 9f7d7167 2018-04-29 stsp switch (ch) {
9478 9f7d7167 2018-04-29 stsp hflag = 1;
9481 53ccebc2 2019-07-30 stsp Vflag = 1;
9484 6879ba42 2020-10-01 naddy usage(hflag, 1);
9485 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9489 9f7d7167 2018-04-29 stsp argc -= optind;
9490 9f7d7167 2018-04-29 stsp argv += optind;
9491 9814e6a3 2020-09-27 naddy optind = 1;
9492 c2301be8 2018-04-30 stsp optreset = 1;
9494 53ccebc2 2019-07-30 stsp if (Vflag) {
9495 53ccebc2 2019-07-30 stsp got_version_print_str();
9496 6879ba42 2020-10-01 naddy return 0;
9499 c2301be8 2018-04-30 stsp if (argc == 0) {
9500 f29d3e89 2018-06-23 stsp if (hflag)
9501 6879ba42 2020-10-01 naddy usage(hflag, 0);
9502 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9503 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9505 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9509 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9510 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9511 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9512 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9513 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9519 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9520 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9521 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9522 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9523 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9524 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9527 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9528 ee85c5e8 2020-02-29 stsp if (argc != 1)
9529 6879ba42 2020-10-01 naddy usage(0, 1);
9530 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9531 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9533 ee85c5e8 2020-02-29 stsp if (hflag)
9534 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9536 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9540 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9542 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9543 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9544 a2f4a359 2020-02-28 stsp free(cmd_argv);
9547 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9548 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9549 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9550 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9551 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9552 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);