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 8b925c6c 2022-07-16 thomas #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 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.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 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 fc2737d5 2022-09-15 thomas TOG_VIEW_HELP
114 fc2737d5 2022-09-15 thomas /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 fc2737d5 2022-09-15 thomas enum tog_keymap_type {
116 fc2737d5 2022-09-15 thomas TOG_KEYMAP_KEYS = -2,
117 fc2737d5 2022-09-15 thomas TOG_KEYMAP_GLOBAL,
118 fc2737d5 2022-09-15 thomas TOG_KEYMAP_DIFF,
119 fc2737d5 2022-09-15 thomas TOG_KEYMAP_LOG,
120 fc2737d5 2022-09-15 thomas TOG_KEYMAP_BLAME,
121 fc2737d5 2022-09-15 thomas TOG_KEYMAP_TREE,
122 fc2737d5 2022-09-15 thomas TOG_KEYMAP_REF,
123 fc2737d5 2022-09-15 thomas TOG_KEYMAP_HELP
126 a5d43cac 2022-07-01 thomas enum tog_view_mode {
127 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
128 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
129 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
132 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
134 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
136 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
137 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
138 ba4f502b 2018-08-04 stsp struct got_object_id *id;
139 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
142 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 ba4f502b 2018-08-04 stsp struct commit_queue {
144 ba4f502b 2018-08-04 stsp int ncommits;
145 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
148 f26dddb7 2019-11-08 stsp struct tog_color {
149 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
150 6d17833f 2019-11-08 stsp regex_t regex;
151 6d17833f 2019-11-08 stsp short colorpair;
153 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
155 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
157 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
159 2183bbf6 2022-01-23 thomas static const struct got_error *
160 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 2183bbf6 2022-01-23 thomas struct got_reference* re2)
163 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
164 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
165 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
167 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
168 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
171 2183bbf6 2022-01-23 thomas *cmp = -1;
172 2183bbf6 2022-01-23 thomas return NULL;
173 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
174 2183bbf6 2022-01-23 thomas *cmp = 1;
175 2183bbf6 2022-01-23 thomas return NULL;
178 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
179 2183bbf6 2022-01-23 thomas return NULL;
182 11b20872 2019-11-08 stsp static const struct got_error *
183 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
185 51a10b52 2020-12-26 stsp const struct got_error *err;
187 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
191 51a10b52 2020-12-26 stsp return err;
193 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
197 51a10b52 2020-12-26 stsp static void
198 51a10b52 2020-12-26 stsp tog_free_refs(void)
200 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
201 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
202 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
204 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
207 51a10b52 2020-12-26 stsp static const struct got_error *
208 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
209 11b20872 2019-11-08 stsp int idx, short color)
211 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
212 11b20872 2019-11-08 stsp struct tog_color *tc;
213 11b20872 2019-11-08 stsp int regerr = 0;
215 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
216 11b20872 2019-11-08 stsp return NULL;
218 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
220 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
221 11b20872 2019-11-08 stsp if (tc == NULL)
222 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
223 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
224 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
225 11b20872 2019-11-08 stsp if (regerr) {
226 11b20872 2019-11-08 stsp static char regerr_msg[512];
227 11b20872 2019-11-08 stsp static char err_msg[512];
228 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
229 11b20872 2019-11-08 stsp sizeof(regerr_msg));
230 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
231 11b20872 2019-11-08 stsp regerr_msg);
232 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
234 11b20872 2019-11-08 stsp return err;
236 11b20872 2019-11-08 stsp tc->colorpair = idx;
237 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
238 11b20872 2019-11-08 stsp return NULL;
241 11b20872 2019-11-08 stsp static void
242 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
244 11b20872 2019-11-08 stsp struct tog_color *tc;
246 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
247 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
248 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
249 11b20872 2019-11-08 stsp regfree(&tc->regex);
254 ef20f542 2022-06-26 thomas static struct tog_color *
255 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
257 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
259 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
260 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
264 11b20872 2019-11-08 stsp return NULL;
268 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
273 11b20872 2019-11-08 stsp return COLOR_CYAN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
275 11b20872 2019-11-08 stsp return COLOR_YELLOW;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
277 11b20872 2019-11-08 stsp return COLOR_GREEN;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
283 91b8c405 2020-01-25 stsp return COLOR_CYAN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
285 11b20872 2019-11-08 stsp return COLOR_GREEN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
287 11b20872 2019-11-08 stsp return COLOR_GREEN;
288 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
289 11b20872 2019-11-08 stsp return COLOR_CYAN;
290 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
291 11b20872 2019-11-08 stsp return COLOR_YELLOW;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_GREEN;
294 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
296 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
297 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
298 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
299 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
305 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
307 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
309 11b20872 2019-11-08 stsp if (val == NULL)
310 11b20872 2019-11-08 stsp return default_color_value(envvar);
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
313 11b20872 2019-11-08 stsp return COLOR_BLACK;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
315 11b20872 2019-11-08 stsp return COLOR_RED;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
317 11b20872 2019-11-08 stsp return COLOR_GREEN;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
319 11b20872 2019-11-08 stsp return COLOR_YELLOW;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
321 11b20872 2019-11-08 stsp return COLOR_BLUE;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
323 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
325 11b20872 2019-11-08 stsp return COLOR_CYAN;
326 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
327 11b20872 2019-11-08 stsp return COLOR_WHITE;
328 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
331 11b20872 2019-11-08 stsp return default_color_value(envvar);
334 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
335 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
336 3dbaef42 2020-11-24 stsp const char *label1, *label2;
337 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
338 19a6a6b5 2022-07-01 thomas int fd1, fd2;
339 82c78e96 2022-08-06 thomas int lineno;
340 15a087fe 2019-02-21 stsp int first_displayed_line;
341 15a087fe 2019-02-21 stsp int last_displayed_line;
343 15a087fe 2019-02-21 stsp int diff_context;
344 3dbaef42 2020-11-24 stsp int ignore_whitespace;
345 64453f7e 2020-11-21 stsp int force_text_diff;
346 15a087fe 2019-02-21 stsp struct got_repository *repo;
347 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
348 fe621944 2020-11-10 stsp size_t nlines;
349 f44b1f58 2020-02-02 tracey int matched_line;
350 f44b1f58 2020-02-02 tracey int selected_line;
352 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
353 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
356 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
359 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
360 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
361 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
362 1a76625f 2018-10-22 stsp int commits_needed;
363 fb280deb 2021-08-30 stsp int load_all;
364 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
365 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
366 1a76625f 2018-10-22 stsp const char *in_repo_path;
367 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp struct got_repository *repo;
369 1af5eddf 2022-06-23 thomas int *pack_fds;
370 1a76625f 2018-10-22 stsp int log_complete;
371 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
372 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
373 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
374 13add988 2019-10-15 stsp int *searching;
375 13add988 2019-10-15 stsp int *search_next_done;
376 13add988 2019-10-15 stsp regex_t *regex;
377 7e8004ba 2022-09-11 thomas int *limiting;
378 7e8004ba 2022-09-11 thomas int limit_match;
379 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
380 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
383 1a76625f 2018-10-22 stsp struct tog_log_view_state {
384 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
385 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
386 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
387 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
388 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
389 b01e7d3b 2018-08-04 stsp int selected;
390 b01e7d3b 2018-08-04 stsp char *in_repo_path;
391 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
392 b672a97a 2020-01-27 stsp int log_branches;
393 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
394 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
395 1a76625f 2018-10-22 stsp sig_atomic_t quit;
396 1a76625f 2018-10-22 stsp pthread_t thread;
397 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
398 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
399 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
400 11b20872 2019-11-08 stsp struct tog_colors colors;
401 f69c5a46 2022-07-19 thomas int use_committer;
402 7e8004ba 2022-09-11 thomas int limit_view;
403 7e8004ba 2022-09-11 thomas regex_t limit_regex;
404 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
407 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
408 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
409 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
411 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
412 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
413 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
414 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
415 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
416 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
417 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
418 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
419 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
420 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
421 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
425 e9424729 2018-08-04 stsp int nlines;
427 e9424729 2018-08-04 stsp struct tog_view *view;
428 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
432 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
433 e9424729 2018-08-04 stsp const char *path;
434 e9424729 2018-08-04 stsp struct got_repository *repo;
435 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
436 e9424729 2018-08-04 stsp int *complete;
437 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
438 fc06ba56 2019-08-22 stsp void *cancel_arg;
441 e9424729 2018-08-04 stsp struct tog_blame {
443 be659d10 2020-11-18 stsp off_t filesize;
444 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
445 6fcac457 2018-11-19 stsp int nlines;
446 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
447 e9424729 2018-08-04 stsp pthread_t thread;
448 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
449 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
450 e9424729 2018-08-04 stsp const char *path;
451 7cd52833 2022-06-23 thomas int *pack_fds;
454 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
455 7cbe629d 2018-08-04 stsp int first_displayed_line;
456 7cbe629d 2018-08-04 stsp int last_displayed_line;
457 7cbe629d 2018-08-04 stsp int selected_line;
458 4fc71f3b 2022-07-12 thomas int last_diffed_line;
459 7cbe629d 2018-08-04 stsp int blame_complete;
462 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
463 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
464 e5a0f69f 2018-08-18 stsp char *path;
465 7cbe629d 2018-08-04 stsp struct got_repository *repo;
466 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
467 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
468 e9424729 2018-08-04 stsp struct tog_blame blame;
469 6c4c42e0 2019-06-24 stsp int matched_line;
470 11b20872 2019-11-08 stsp struct tog_colors colors;
473 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
474 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
475 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
476 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
478 ad80ab7b 2018-08-04 stsp int selected;
481 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
484 ad80ab7b 2018-08-04 stsp char *tree_label;
485 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
486 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
487 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
489 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
490 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
491 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
492 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
493 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
494 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
495 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
496 6458efa5 2020-11-24 stsp struct tog_colors colors;
499 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
500 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
501 6458efa5 2020-11-24 stsp struct got_reference *ref;
505 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
508 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
512 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 6458efa5 2020-11-24 stsp struct got_repository *repo;
514 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
515 bddb1296 2019-11-08 stsp struct tog_colors colors;
518 fc2737d5 2022-09-15 thomas struct tog_help_view_state {
520 fc2737d5 2022-09-15 thomas off_t *line_offsets;
521 fc2737d5 2022-09-15 thomas size_t nlines;
522 fc2737d5 2022-09-15 thomas int lineno;
523 fc2737d5 2022-09-15 thomas int first_displayed_line;
524 fc2737d5 2022-09-15 thomas int last_displayed_line;
526 fc2737d5 2022-09-15 thomas int matched_line;
527 fc2737d5 2022-09-15 thomas int selected_line;
529 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
532 fc2737d5 2022-09-15 thomas #define GENERATE_HELP \
533 fc2737d5 2022-09-15 thomas KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 fc2737d5 2022-09-15 thomas KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 fc2737d5 2022-09-15 thomas KEY_("k C-p Up", "Move cursor or page up one line"), \
536 fc2737d5 2022-09-15 thomas KEY_("j C-n Down", "Move cursor or page down one line"), \
537 fc2737d5 2022-09-15 thomas KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 fc2737d5 2022-09-15 thomas KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 fc2737d5 2022-09-15 thomas KEY_("C-u u", "Scroll the view up one half page"), \
540 fc2737d5 2022-09-15 thomas KEY_("C-d d", "Scroll the view down one half page"), \
541 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
542 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
543 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
544 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
545 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
546 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
547 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
548 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
549 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
550 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
551 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
552 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
553 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
554 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
555 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
556 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
557 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
559 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
560 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
561 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
562 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
563 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
564 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
565 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
566 fc2737d5 2022-09-15 thomas " commit"), \
567 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
568 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
569 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
570 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
572 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
573 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
574 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
575 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
576 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
577 fc2737d5 2022-09-15 thomas " data"), \
578 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
579 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
580 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
581 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
582 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
583 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
584 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
586 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
587 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
588 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
589 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
590 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
591 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
592 fc2737d5 2022-09-15 thomas " selected line's commit"), \
593 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
594 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
596 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
597 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
598 fc2737d5 2022-09-15 thomas " selected file"), \
599 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
600 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
601 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
602 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
604 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
605 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
606 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
607 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
608 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
609 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
610 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
612 fc2737d5 2022-09-15 thomas struct tog_key_map {
613 fc2737d5 2022-09-15 thomas const char *keys;
614 fc2737d5 2022-09-15 thomas const char *info;
615 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
619 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
621 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
622 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
623 669b5ffa 2018-10-07 stsp * there is enough screen estate.
625 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
626 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
628 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
629 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
630 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
632 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
633 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
635 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
637 cc3c9aac 2018-08-01 stsp struct tog_view {
638 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
639 26ed57b2 2018-05-19 stsp WINDOW *window;
640 26ed57b2 2018-05-19 stsp PANEL *panel;
641 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
642 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
643 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
644 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
645 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
646 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
647 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
648 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
649 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
651 669b5ffa 2018-10-07 stsp struct tog_view *parent;
652 669b5ffa 2018-10-07 stsp struct tog_view *child;
655 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
656 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
657 e78dc838 2020-12-04 stsp * between parent and child.
658 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
659 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
660 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
661 e78dc838 2020-12-04 stsp * situations.
663 e78dc838 2020-12-04 stsp int focus_child;
665 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
666 5dc9f4bc 2018-08-04 stsp /* type-specific state */
667 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
669 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
670 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
671 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
672 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
673 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
674 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
677 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
678 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
679 e78dc838 2020-12-04 stsp struct tog_view *, int);
680 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
681 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
682 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
684 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
685 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
686 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
687 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
688 c0c4acc8 2021-01-24 stsp int search_started;
689 60493ae3 2019-06-20 stsp int searching;
690 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
691 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
692 60493ae3 2019-06-20 stsp int search_next_done;
693 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
694 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
695 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
696 1803e47f 2019-06-22 stsp regex_t regex;
697 41605754 2020-11-12 stsp regmatch_t regmatch;
698 f2d06bef 2023-01-23 thomas const char *action;
701 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
702 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
703 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
704 78756c87 2020-11-24 stsp struct got_repository *);
705 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
706 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
707 e78dc838 2020-12-04 stsp struct tog_view *, int);
708 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
709 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
710 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
711 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
712 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
713 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
715 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
716 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
717 78756c87 2020-11-24 stsp const char *, const char *, int);
718 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
719 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
720 e78dc838 2020-12-04 stsp struct tog_view *, int);
721 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
722 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
723 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
724 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
726 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
727 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
728 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
729 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
730 e78dc838 2020-12-04 stsp struct tog_view *, int);
731 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
732 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
733 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
734 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
735 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
737 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
738 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
739 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
740 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
741 e78dc838 2020-12-04 stsp struct tog_view *, int);
742 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
743 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
744 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
746 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
747 6458efa5 2020-11-24 stsp struct got_repository *);
748 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
749 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
750 e78dc838 2020-12-04 stsp struct tog_view *, int);
751 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
752 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
753 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
755 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
756 2a7d3cd7 2022-09-17 thomas struct tog_view *);
757 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
758 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
759 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
760 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
761 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
762 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
763 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
764 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
766 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
767 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
768 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
769 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
770 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
772 25791caa 2018-10-24 stsp static void
773 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
775 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
778 83baff54 2019-08-12 stsp static void
779 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
781 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
784 61266923 2020-01-14 stsp static void
785 61266923 2020-01-14 stsp tog_sigcont(int signo)
787 61266923 2020-01-14 stsp tog_sigcont_received = 1;
790 296152d1 2022-05-31 thomas static void
791 296152d1 2022-05-31 thomas tog_sigint(int signo)
793 296152d1 2022-05-31 thomas tog_sigint_received = 1;
796 296152d1 2022-05-31 thomas static void
797 296152d1 2022-05-31 thomas tog_sigterm(int signo)
799 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
802 296152d1 2022-05-31 thomas static int
803 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
805 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
806 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
809 e5a0f69f 2018-08-18 stsp static const struct got_error *
810 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
812 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
814 669b5ffa 2018-10-07 stsp if (view->child) {
815 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
816 669b5ffa 2018-10-07 stsp view->child = NULL;
818 e5a0f69f 2018-08-18 stsp if (view->close)
819 e5a0f69f 2018-08-18 stsp err = view->close(view);
820 ea5e7bb5 2018-08-01 stsp if (view->panel)
821 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
822 ea5e7bb5 2018-08-01 stsp if (view->window)
823 ea5e7bb5 2018-08-01 stsp delwin(view->window);
824 ea5e7bb5 2018-08-01 stsp free(view);
825 f2d749db 2022-07-12 thomas return err ? err : child_err;
828 ea5e7bb5 2018-08-01 stsp static struct tog_view *
829 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
830 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
832 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
834 ea5e7bb5 2018-08-01 stsp if (view == NULL)
835 ea5e7bb5 2018-08-01 stsp return NULL;
837 d6b05b5b 2018-08-04 stsp view->type = type;
838 f7d12f7e 2018-08-01 stsp view->lines = LINES;
839 f7d12f7e 2018-08-01 stsp view->cols = COLS;
840 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
841 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
842 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
843 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
844 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
845 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
846 96a765a8 2018-08-04 stsp view_close(view);
847 ea5e7bb5 2018-08-01 stsp return NULL;
849 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
850 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
851 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
852 96a765a8 2018-08-04 stsp view_close(view);
853 ea5e7bb5 2018-08-01 stsp return NULL;
856 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
857 ea5e7bb5 2018-08-01 stsp return view;
861 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
863 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
865 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
868 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
869 a5d43cac 2022-07-01 thomas static int
870 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
872 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
875 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
877 5c60c32a 2018-10-18 stsp static const struct got_error *
878 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
880 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
882 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
883 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
884 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
886 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
887 a5d43cac 2022-07-01 thomas view->begin_x = 0;
888 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
889 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
890 53d2bdd3 2022-07-10 thomas view->cols > 119)
891 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
893 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
894 a5d43cac 2022-07-01 thomas view->begin_y = 0;
896 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
897 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
898 5c60c32a 2018-10-18 stsp view->lines = LINES;
899 5c60c32a 2018-10-18 stsp view->cols = COLS;
900 5c60c32a 2018-10-18 stsp err = view_resize(view);
902 5c60c32a 2018-10-18 stsp return err;
904 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
905 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
907 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
908 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
910 5c60c32a 2018-10-18 stsp return NULL;
913 5c60c32a 2018-10-18 stsp static const struct got_error *
914 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
916 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
918 5c60c32a 2018-10-18 stsp view->begin_x = 0;
919 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
920 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
921 5c60c32a 2018-10-18 stsp view->ncols = COLS;
922 5c60c32a 2018-10-18 stsp view->lines = LINES;
923 5c60c32a 2018-10-18 stsp view->cols = COLS;
924 5c60c32a 2018-10-18 stsp err = view_resize(view);
926 5c60c32a 2018-10-18 stsp return err;
928 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
929 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
931 5c60c32a 2018-10-18 stsp return NULL;
935 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
937 5c60c32a 2018-10-18 stsp return view->parent == NULL;
940 b65b3ea0 2022-06-23 thomas static int
941 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
943 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
946 e44940c3 2022-07-01 thomas static int
947 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
949 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
952 444d5325 2022-07-03 thomas static int
953 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
955 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
956 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
959 a5d43cac 2022-07-01 thomas static void
960 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
962 a5d43cac 2022-07-01 thomas PANEL *panel;
963 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
965 a5d43cac 2022-07-01 thomas if (view->parent)
966 a5d43cac 2022-07-01 thomas return view_border(view->parent);
968 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
969 a5d43cac 2022-07-01 thomas if (panel == NULL)
972 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
973 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
974 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
975 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
976 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
978 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
979 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
982 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
983 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
984 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
985 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
986 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
988 4d8c2215 2018-08-19 stsp static const struct got_error *
989 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
991 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
992 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
994 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
996 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
997 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
999 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1000 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1001 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1003 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1005 4918811f 2022-07-01 thomas if (view->child) {
1006 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1008 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1009 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1010 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1011 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1012 40236d76 2022-06-23 thomas ncols = COLS;
1014 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1015 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1016 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1018 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1020 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1022 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1023 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1026 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1027 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1028 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1030 a5d43cac 2022-07-01 thomas if (hs) {
1031 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1032 a5d43cac 2022-07-01 thomas if (err)
1033 a5d43cac 2022-07-01 thomas return err;
1034 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1035 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1036 a5d43cac 2022-07-01 thomas if (err)
1037 a5d43cac 2022-07-01 thomas return err;
1039 a5d43cac 2022-07-01 thomas view_border(view);
1040 a5d43cac 2022-07-01 thomas update_panels();
1041 a5d43cac 2022-07-01 thomas doupdate();
1042 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1043 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1045 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1046 40236d76 2022-06-23 thomas ncols = COLS;
1048 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1049 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1050 ea0bff04 2022-07-19 thomas if (err)
1051 ea0bff04 2022-07-19 thomas return err;
1054 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1055 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1056 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1057 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1058 40236d76 2022-06-23 thomas wclear(view->window);
1060 40236d76 2022-06-23 thomas view->nlines = nlines;
1061 40236d76 2022-06-23 thomas view->ncols = ncols;
1062 40236d76 2022-06-23 thomas view->lines = LINES;
1063 40236d76 2022-06-23 thomas view->cols = COLS;
1065 5c60c32a 2018-10-18 stsp return NULL;
1068 ea0bff04 2022-07-19 thomas static const struct got_error *
1069 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1071 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1072 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1073 3a0139e8 2022-07-22 thomas int n = 0;
1075 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1076 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1079 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1080 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1082 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1083 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1084 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1087 ea0bff04 2022-07-19 thomas return err;
1090 97cb21cd 2022-07-12 thomas static void
1091 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1093 97cb21cd 2022-07-12 thomas if (n == 0)
1096 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1097 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1098 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1100 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1101 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1102 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1103 97cb21cd 2022-07-12 thomas view->offset -= n;
1105 97cb21cd 2022-07-12 thomas view->offset = 0;
1109 53d2bdd3 2022-07-10 thomas static const struct got_error *
1110 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1112 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1113 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1115 53d2bdd3 2022-07-10 thomas if (view->parent)
1116 53d2bdd3 2022-07-10 thomas v = view->parent;
1118 53d2bdd3 2022-07-10 thomas v = view;
1120 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1121 53d2bdd3 2022-07-10 thomas return NULL;
1123 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1125 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1126 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1127 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1128 53d2bdd3 2022-07-10 thomas if (view->parent)
1129 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1131 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1132 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1133 53d2bdd3 2022-07-10 thomas view->count = 0;
1134 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1135 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1136 53d2bdd3 2022-07-10 thomas view->count = 0;
1137 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1139 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1140 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1141 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1142 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1143 53d2bdd3 2022-07-10 thomas if (err)
1144 53d2bdd3 2022-07-10 thomas return err;
1145 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1146 53d2bdd3 2022-07-10 thomas } else {
1147 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1148 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1149 53d2bdd3 2022-07-10 thomas if (view->parent)
1150 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1152 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1153 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1154 53d2bdd3 2022-07-10 thomas view->count = 0;
1155 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1156 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1157 53d2bdd3 2022-07-10 thomas view->count = 0;
1158 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1160 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1163 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1164 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1165 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1166 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1168 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1169 53d2bdd3 2022-07-10 thomas if (err)
1170 53d2bdd3 2022-07-10 thomas return err;
1171 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1172 53d2bdd3 2022-07-10 thomas if (err)
1173 53d2bdd3 2022-07-10 thomas return err;
1175 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1176 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1177 53d2bdd3 2022-07-10 thomas if (err)
1178 53d2bdd3 2022-07-10 thomas return err;
1181 3a0139e8 2022-07-22 thomas if (v->resize)
1182 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1183 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1184 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1186 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1188 53d2bdd3 2022-07-10 thomas return err;
1191 53d2bdd3 2022-07-10 thomas static void
1192 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1194 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1196 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1197 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1200 669b5ffa 2018-10-07 stsp static const struct got_error *
1201 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1203 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1205 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1206 669b5ffa 2018-10-07 stsp return NULL;
1208 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1209 669b5ffa 2018-10-07 stsp view->child = NULL;
1210 669b5ffa 2018-10-07 stsp return err;
1213 40236d76 2022-06-23 thomas static const struct got_error *
1214 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1216 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1218 669b5ffa 2018-10-07 stsp view->child = child;
1219 669b5ffa 2018-10-07 stsp child->parent = view;
1221 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1222 53d2bdd3 2022-07-10 thomas if (err)
1223 53d2bdd3 2022-07-10 thomas return err;
1225 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1226 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1228 53d2bdd3 2022-07-10 thomas return err;
1231 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1232 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1234 2a31b33b 2022-07-23 thomas static const struct got_error *
1235 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1236 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1238 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1239 2a31b33b 2022-07-23 thomas const struct got_error *err;
1240 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1242 2a31b33b 2022-07-23 thomas *requested = NULL;
1244 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1245 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1247 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1248 2a31b33b 2022-07-23 thomas if (err)
1249 2a31b33b 2022-07-23 thomas return err;
1251 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1252 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1253 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1254 2a31b33b 2022-07-23 thomas if (err)
1255 2a31b33b 2022-07-23 thomas return err;
1258 2a31b33b 2022-07-23 thomas view->focussed = 0;
1259 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1260 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1261 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1262 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1264 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1265 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1266 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1267 2a31b33b 2022-07-23 thomas if (err)
1268 2a31b33b 2022-07-23 thomas return err;
1269 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1270 2a31b33b 2022-07-23 thomas if (err)
1271 2a31b33b 2022-07-23 thomas return err;
1272 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1274 2a31b33b 2022-07-23 thomas *requested = new_view;
1276 2a31b33b 2022-07-23 thomas return NULL;
1279 34bc9ec9 2019-02-22 stsp static void
1280 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1282 25791caa 2018-10-24 stsp int cols, lines;
1283 25791caa 2018-10-24 stsp struct winsize size;
1285 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1286 25791caa 2018-10-24 stsp cols = 80; /* Default */
1287 25791caa 2018-10-24 stsp lines = 24;
1289 25791caa 2018-10-24 stsp cols = size.ws_col;
1290 25791caa 2018-10-24 stsp lines = size.ws_row;
1292 25791caa 2018-10-24 stsp resize_term(lines, cols);
1295 2b49a8ae 2019-06-22 stsp static const struct got_error *
1296 f54d892e 2023-02-17 thomas view_search_start(struct tog_view *view, int fast_refresh)
1298 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1299 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1300 2b49a8ae 2019-06-22 stsp char pattern[1024];
1303 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1304 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1305 c0c4acc8 2021-01-24 stsp view->searching = 0;
1306 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1308 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1310 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1311 2b49a8ae 2019-06-22 stsp return NULL;
1313 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1314 a5d43cac 2022-07-01 thomas v = view->child;
1315 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1316 d07291c6 2022-12-30 thomas v = view->parent;
1318 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1319 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1321 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1322 2b49a8ae 2019-06-22 stsp nocbreak();
1324 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1325 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1328 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1329 f54d892e 2023-02-17 thomas if (!fast_refresh)
1330 f54d892e 2023-02-17 thomas halfdelay(10);
1331 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1332 2b49a8ae 2019-06-22 stsp return NULL;
1334 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1335 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1336 7c32bd05 2019-06-22 stsp if (err) {
1337 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1338 7c32bd05 2019-06-22 stsp return err;
1340 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1341 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1342 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1343 2b49a8ae 2019-06-22 stsp view->search_next(view);
1346 2b49a8ae 2019-06-22 stsp return NULL;
1349 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1350 64486692 2022-07-07 thomas static const struct got_error *
1351 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1353 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1354 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1356 64486692 2022-07-07 thomas if (view->parent)
1357 64486692 2022-07-07 thomas v = view->parent;
1359 64486692 2022-07-07 thomas v = view;
1361 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1362 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1364 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1366 ddbc4d37 2022-07-12 thomas if (!v->child)
1367 ddbc4d37 2022-07-12 thomas return NULL;
1368 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1369 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1371 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1372 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1373 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1374 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1375 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1378 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1379 64486692 2022-07-07 thomas v->ncols = COLS;
1380 64486692 2022-07-07 thomas v->child->ncols = COLS;
1381 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1383 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1384 64486692 2022-07-07 thomas if (err)
1385 64486692 2022-07-07 thomas return err;
1387 64486692 2022-07-07 thomas v->child->mode = v->mode;
1388 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1389 64486692 2022-07-07 thomas v->focus_child = 1;
1391 64486692 2022-07-07 thomas err = view_fullscreen(v);
1392 64486692 2022-07-07 thomas if (err)
1393 64486692 2022-07-07 thomas return err;
1394 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1395 64486692 2022-07-07 thomas if (err)
1396 64486692 2022-07-07 thomas return err;
1398 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1399 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1400 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1401 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1402 cf1fe301 2022-07-29 thomas if (err)
1403 cf1fe301 2022-07-29 thomas return err;
1404 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1405 cf1fe301 2022-07-29 thomas if (err)
1406 cf1fe301 2022-07-29 thomas return err;
1407 ddbc4d37 2022-07-12 thomas } else {
1408 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1409 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1411 f4e6231a 2022-07-22 thomas if (v->resize)
1412 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1413 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1414 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1416 64486692 2022-07-07 thomas return err;
1420 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1421 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1423 07b0611c 2022-06-23 thomas static int
1424 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1426 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1427 a5d43cac 2022-07-01 thomas int x, n = 0;
1429 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1430 a5d43cac 2022-07-01 thomas v = view->child;
1431 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1432 a5d43cac 2022-07-01 thomas v = view->parent;
1434 07b0611c 2022-06-23 thomas view->count = 0;
1435 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1436 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1437 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1438 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1439 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1442 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1443 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1444 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1445 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1449 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1450 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1452 07b0611c 2022-06-23 thomas if (n >= 9999999)
1453 07b0611c 2022-06-23 thomas n = 9999999;
1455 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1456 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1458 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1459 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1464 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1465 07b0611c 2022-06-23 thomas view->count = n;
1467 07b0611c 2022-06-23 thomas return c;
1470 f2d06bef 2023-01-23 thomas static void
1471 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1473 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1475 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1476 f2d06bef 2023-01-23 thomas v = view->child;
1477 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1478 f2d06bef 2023-01-23 thomas v = view->parent;
1480 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1481 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1482 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1483 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1486 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1487 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1489 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1490 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1491 f2d06bef 2023-01-23 thomas view->action = NULL;
1493 f2d06bef 2023-01-23 thomas view->action = NULL;
1496 0cf4efb1 2018-09-29 stsp static const struct got_error *
1497 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1498 f54d892e 2023-02-17 thomas struct tog_view_list_head *views, int fast_refresh)
1500 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1501 669b5ffa 2018-10-07 stsp struct tog_view *v;
1502 1a76625f 2018-10-22 stsp int ch, errcode;
1504 e5a0f69f 2018-08-18 stsp *new = NULL;
1506 f2d06bef 2023-01-23 thomas if (view->action)
1507 f2d06bef 2023-01-23 thomas action_report(view);
1509 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1510 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1511 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1512 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1513 07b0611c 2022-06-23 thomas view->count = 0;
1516 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1517 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1518 82954512 2020-02-03 stsp if (errcode)
1519 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1520 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1521 3da8ef85 2021-09-21 stsp sched_yield();
1522 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1523 82954512 2020-02-03 stsp if (errcode)
1524 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1525 82954512 2020-02-03 stsp "pthread_mutex_lock");
1526 60493ae3 2019-06-20 stsp view->search_next(view);
1527 60493ae3 2019-06-20 stsp return NULL;
1530 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1531 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1532 1a76625f 2018-10-22 stsp if (errcode)
1533 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1534 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1535 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1536 634cb454 2022-07-03 thomas cbreak();
1537 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1538 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1539 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1540 634cb454 2022-07-03 thomas view->count = 0;
1542 634cb454 2022-07-03 thomas ch = view->ch;
1543 634cb454 2022-07-03 thomas } else {
1544 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1545 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1546 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1548 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1549 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1550 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1551 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1552 1a76625f 2018-10-22 stsp if (errcode)
1553 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1555 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1556 25791caa 2018-10-24 stsp tog_resizeterm();
1557 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1558 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1559 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1560 25791caa 2018-10-24 stsp err = view_resize(v);
1562 25791caa 2018-10-24 stsp return err;
1563 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1565 25791caa 2018-10-24 stsp return err;
1566 cdfcfb03 2020-12-06 stsp if (v->child) {
1567 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1569 cdfcfb03 2020-12-06 stsp return err;
1570 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1571 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1573 cdfcfb03 2020-12-06 stsp return err;
1574 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1575 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1576 53d2bdd3 2022-07-10 thomas if (err)
1577 53d2bdd3 2022-07-10 thomas return err;
1583 e5a0f69f 2018-08-18 stsp switch (ch) {
1584 fc2737d5 2022-09-15 thomas case '?':
1585 fc2737d5 2022-09-15 thomas case 'H':
1586 fc2737d5 2022-09-15 thomas case KEY_F(1):
1587 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1588 fc2737d5 2022-09-15 thomas err = view->reset(view);
1590 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1593 07b0611c 2022-06-23 thomas view->count = 0;
1594 1e37a5c2 2019-05-12 jcs if (view->child) {
1595 e78dc838 2020-12-04 stsp view->focussed = 0;
1596 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1597 e78dc838 2020-12-04 stsp view->focus_child = 1;
1598 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1599 e78dc838 2020-12-04 stsp view->focussed = 0;
1600 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1601 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1602 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1603 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1604 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1606 a5d43cac 2022-07-01 thomas if (err)
1607 a5d43cac 2022-07-01 thomas return err;
1609 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1610 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1611 a5d43cac 2022-07-01 thomas if (err)
1612 a5d43cac 2022-07-01 thomas return err;
1617 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1618 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1619 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1620 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1621 a5d43cac 2022-07-01 thomas if (err)
1624 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1626 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1627 9970f7fc 2020-12-03 stsp view->dying = 1;
1632 1c5e5faa 2022-06-23 thomas case 'F':
1633 07b0611c 2022-06-23 thomas view->count = 0;
1634 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1635 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1637 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1638 e78dc838 2020-12-04 stsp view->focussed = 0;
1639 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1640 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1641 53d2bdd3 2022-07-10 thomas } else {
1642 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1643 53d2bdd3 2022-07-10 thomas if (!err)
1644 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1648 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1649 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1651 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1652 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1653 e78dc838 2020-12-04 stsp view->focussed = 1;
1654 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1656 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1657 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1658 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1659 53d2bdd3 2022-07-10 thomas if (!err)
1660 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1664 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1666 a5d43cac 2022-07-01 thomas if (err)
1668 3a0139e8 2022-07-22 thomas if (view->resize) {
1669 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1670 a5d43cac 2022-07-01 thomas if (err)
1673 a5d43cac 2022-07-01 thomas if (view->parent)
1674 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1675 a5d43cac 2022-07-01 thomas if (!err)
1676 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1678 64486692 2022-07-07 thomas case 'S':
1679 53d2bdd3 2022-07-10 thomas view->count = 0;
1680 64486692 2022-07-07 thomas err = switch_split(view);
1682 53d2bdd3 2022-07-10 thomas case '-':
1683 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1685 53d2bdd3 2022-07-10 thomas case '+':
1686 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1688 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1691 07b0611c 2022-06-23 thomas view->count = 0;
1692 60493ae3 2019-06-20 stsp if (view->search_start)
1693 f54d892e 2023-02-17 thomas view_search_start(view, fast_refresh);
1695 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1699 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1700 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1701 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1702 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1703 60493ae3 2019-06-20 stsp view->search_next(view);
1705 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1707 adf4c9e0 2022-07-03 thomas case 'A':
1708 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1709 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1710 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1711 f2d06bef 2023-01-23 thomas } else {
1712 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1713 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1715 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1716 adf4c9e0 2022-07-03 thomas if (v->reset) {
1717 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1718 adf4c9e0 2022-07-03 thomas if (err)
1719 adf4c9e0 2022-07-03 thomas return err;
1721 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1722 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1723 adf4c9e0 2022-07-03 thomas if (err)
1724 adf4c9e0 2022-07-03 thomas return err;
1729 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1733 e5a0f69f 2018-08-18 stsp return err;
1736 ef20f542 2022-06-26 thomas static int
1737 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1739 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1740 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1742 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1744 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1747 669b5ffa 2018-10-07 stsp return view->focussed;
1750 bcbd79e2 2018-08-19 stsp static const struct got_error *
1751 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1753 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1754 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1755 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1756 64486692 2022-07-07 thomas char *mode;
1757 fd823528 2018-10-22 stsp int fast_refresh = 10;
1758 1a76625f 2018-10-22 stsp int done = 0, errcode;
1760 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1761 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1762 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1764 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1766 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1767 1a76625f 2018-10-22 stsp if (errcode)
1768 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1770 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1771 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1773 1004088d 2018-09-29 stsp view->focussed = 1;
1774 878940b7 2018-09-29 stsp err = view->show(view);
1776 0cf4efb1 2018-09-29 stsp return err;
1777 0cf4efb1 2018-09-29 stsp update_panels();
1778 0cf4efb1 2018-09-29 stsp doupdate();
1779 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1780 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1781 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1782 b185e033 2023-01-23 thomas if (fast_refresh && --fast_refresh == 0)
1783 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1785 f54d892e 2023-02-17 thomas err = view_input(&new_view, &done, view, &views, fast_refresh);
1789 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
1790 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
1791 6ec3d39c 2023-01-23 thomas done = 1;
1792 6ec3d39c 2023-01-23 thomas if (done) {
1793 6ec3d39c 2023-01-23 thomas struct tog_view *v;
1796 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
1797 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
1799 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
1800 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
1801 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
1802 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
1803 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
1804 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
1805 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
1806 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
1809 6ec3d39c 2023-01-23 thomas doupdate();
1812 9970f7fc 2020-12-03 stsp if (view->dying) {
1813 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1815 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1816 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1818 e78dc838 2020-12-04 stsp else if (view->parent)
1819 669b5ffa 2018-10-07 stsp prev = view->parent;
1821 e78dc838 2020-12-04 stsp if (view->parent) {
1822 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1823 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1824 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1825 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1826 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1827 40236d76 2022-06-23 thomas if (err)
1829 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1830 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1832 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1834 9970f7fc 2020-12-03 stsp err = view_close(view);
1836 e5a0f69f 2018-08-18 stsp goto done;
1838 e78dc838 2020-12-04 stsp view = NULL;
1839 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1840 e78dc838 2020-12-04 stsp if (v->focussed)
1843 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1844 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1846 e78dc838 2020-12-04 stsp view = prev;
1847 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1848 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1849 e78dc838 2020-12-04 stsp tog_view_list_head);
1851 e78dc838 2020-12-04 stsp if (view) {
1852 e78dc838 2020-12-04 stsp if (view->focus_child) {
1853 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1854 e78dc838 2020-12-04 stsp view = view->child;
1856 e78dc838 2020-12-04 stsp view->focussed = 1;
1860 bcbd79e2 2018-08-19 stsp if (new_view) {
1861 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1862 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1863 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1864 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1866 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1867 86c66b02 2018-10-18 stsp err = view_close(v);
1869 86c66b02 2018-10-18 stsp goto done;
1872 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1873 fed7eaa8 2018-10-24 stsp view = new_view;
1875 6ec3d39c 2023-01-23 thomas if (view && !done) {
1876 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1877 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1878 e78dc838 2020-12-04 stsp view = view->child;
1880 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1881 e78dc838 2020-12-04 stsp view = view->parent;
1883 e78dc838 2020-12-04 stsp show_panel(view->panel);
1884 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1885 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1886 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1887 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1889 1a76625f 2018-10-22 stsp goto done;
1891 669b5ffa 2018-10-07 stsp err = view->show(view);
1893 1a76625f 2018-10-22 stsp goto done;
1894 669b5ffa 2018-10-07 stsp if (view->child) {
1895 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1897 1a76625f 2018-10-22 stsp goto done;
1899 1a76625f 2018-10-22 stsp update_panels();
1900 1a76625f 2018-10-22 stsp doupdate();
1904 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1905 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1906 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1907 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1908 f2d749db 2022-07-12 thomas close_err = view_close(view);
1909 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1910 f2d749db 2022-07-12 thomas err = close_err;
1913 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1914 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1915 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1917 e5a0f69f 2018-08-18 stsp return err;
1920 4ed7e80c 2018-05-20 stsp __dead static void
1921 9f7d7167 2018-04-29 stsp usage_log(void)
1924 c70c5802 2018-08-01 stsp fprintf(stderr,
1925 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1926 9f7d7167 2018-04-29 stsp getprogname());
1930 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1931 80ddbec8 2018-04-29 stsp static const struct got_error *
1932 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1934 00dfcb92 2018-06-11 stsp char *vis = NULL;
1935 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1937 963b370f 2018-05-20 stsp *ws = NULL;
1938 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1939 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1940 00dfcb92 2018-06-11 stsp int vislen;
1941 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1942 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1944 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1945 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1947 00dfcb92 2018-06-11 stsp return err;
1948 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1949 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1950 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1951 a7f50699 2018-06-11 stsp goto done;
1955 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1956 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1957 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1958 a7f50699 2018-06-11 stsp goto done;
1961 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1962 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1964 00dfcb92 2018-06-11 stsp free(vis);
1965 963b370f 2018-05-20 stsp if (err) {
1966 963b370f 2018-05-20 stsp free(*ws);
1967 963b370f 2018-05-20 stsp *ws = NULL;
1968 963b370f 2018-05-20 stsp *wlen = 0;
1970 963b370f 2018-05-20 stsp return err;
1973 05171be4 2022-06-23 thomas static const struct got_error *
1974 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1976 05171be4 2022-06-23 thomas char *dst;
1977 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1979 05171be4 2022-06-23 thomas *ptr = NULL;
1980 05171be4 2022-06-23 thomas n = len = strlen(src);
1981 83316834 2022-06-23 thomas dst = malloc(n + 1);
1982 05171be4 2022-06-23 thomas if (dst == NULL)
1983 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1985 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1986 05171be4 2022-06-23 thomas const char c = src[idx];
1988 05171be4 2022-06-23 thomas if (c == '\t') {
1989 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1990 b235ad5d 2022-06-23 thomas char *p;
1992 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1993 83316834 2022-06-23 thomas if (p == NULL) {
1994 83316834 2022-06-23 thomas free(dst);
1995 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1998 83316834 2022-06-23 thomas dst = p;
1999 05171be4 2022-06-23 thomas n += nb;
2000 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
2001 05171be4 2022-06-23 thomas sz += nb;
2003 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2007 05171be4 2022-06-23 thomas dst[sz] = '\0';
2008 05171be4 2022-06-23 thomas *ptr = dst;
2009 05171be4 2022-06-23 thomas return NULL;
2013 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2014 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2015 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
2016 8d208d34 2022-06-23 thomas * *rcol.
2018 8d208d34 2022-06-23 thomas static int
2019 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2021 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2023 8d208d34 2022-06-23 thomas if (n == 0) {
2024 8d208d34 2022-06-23 thomas *rcol = cols;
2025 8d208d34 2022-06-23 thomas return off;
2028 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2029 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2030 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2032 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2034 8d208d34 2022-06-23 thomas if (width == -1) {
2035 8d208d34 2022-06-23 thomas width = 1;
2036 8d208d34 2022-06-23 thomas wline[i] = L'.';
2039 8d208d34 2022-06-23 thomas if (cols + width > n)
2041 8d208d34 2022-06-23 thomas cols += width;
2044 8d208d34 2022-06-23 thomas *rcol = cols;
2045 8d208d34 2022-06-23 thomas return i;
2049 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2050 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2051 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2053 f91a2b48 2022-06-23 thomas static const struct got_error *
2054 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2055 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2057 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2058 8d208d34 2022-06-23 thomas int cols;
2059 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2060 05171be4 2022-06-23 thomas char *exstr = NULL;
2061 963b370f 2018-05-20 stsp size_t wlen;
2062 8d208d34 2022-06-23 thomas int i, scrollx;
2064 963b370f 2018-05-20 stsp *wlinep = NULL;
2065 b700b5d6 2018-07-10 stsp *widthp = 0;
2067 05171be4 2022-06-23 thomas if (expand) {
2068 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2069 05171be4 2022-06-23 thomas if (err)
2070 05171be4 2022-06-23 thomas return err;
2073 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2074 05171be4 2022-06-23 thomas free(exstr);
2076 963b370f 2018-05-20 stsp return err;
2078 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2080 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2081 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2084 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2085 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2089 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2090 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2092 b700b5d6 2018-07-10 stsp if (widthp)
2093 b700b5d6 2018-07-10 stsp *widthp = cols;
2094 f91a2b48 2022-06-23 thomas if (scrollxp)
2095 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2097 963b370f 2018-05-20 stsp free(wline);
2099 963b370f 2018-05-20 stsp *wlinep = wline;
2100 963b370f 2018-05-20 stsp return err;
2103 8b473291 2019-02-21 stsp static const struct got_error*
2104 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2105 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2107 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2108 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2110 8b473291 2019-02-21 stsp const char *name;
2112 8b473291 2019-02-21 stsp *refs_str = NULL;
2114 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2115 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2116 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2119 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2120 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2122 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2123 8b473291 2019-02-21 stsp name += 5;
2124 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2125 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2127 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2128 8b473291 2019-02-21 stsp name += 6;
2129 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2130 8b473291 2019-02-21 stsp name += 8;
2131 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2132 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2135 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2138 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2139 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2140 5d844a1e 2019-08-13 stsp if (err) {
2141 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2142 48cae60d 2020-09-22 stsp free(ref_id);
2145 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2146 5d844a1e 2019-08-13 stsp err = NULL;
2147 5d844a1e 2019-08-13 stsp tag = NULL;
2150 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2151 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2152 48cae60d 2020-09-22 stsp free(ref_id);
2154 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2155 52b5abe1 2019-08-13 stsp if (cmp != 0)
2157 8b473291 2019-02-21 stsp s = *refs_str;
2158 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2159 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2160 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2162 8b473291 2019-02-21 stsp *refs_str = NULL;
2168 8b473291 2019-02-21 stsp return err;
2171 963b370f 2018-05-20 stsp static const struct got_error *
2172 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2173 27a741e5 2019-09-11 stsp int col_tab_align)
2175 e6b8b890 2020-12-29 naddy char *smallerthan;
2177 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2178 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2179 5813d178 2019-03-09 stsp author = smallerthan + 1;
2180 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2181 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2182 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2185 5813d178 2019-03-09 stsp static const struct got_error *
2186 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2187 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2188 8fdc79fe 2020-12-01 naddy int author_display_cols)
2190 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2191 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2192 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2193 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2194 5813d178 2019-03-09 stsp char *author = NULL;
2195 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2196 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2197 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2198 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2199 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2200 ccb26ccd 2018-11-05 stsp struct tm tm;
2201 45d799e2 2018-12-23 stsp time_t committer_time;
2202 11b20872 2019-11-08 stsp struct tog_color *tc;
2204 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2205 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2206 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2207 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2208 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2210 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2211 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2213 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2214 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2216 11b20872 2019-11-08 stsp wattr_on(view->window,
2217 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2218 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2220 11b20872 2019-11-08 stsp wattr_off(view->window,
2221 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2222 27a741e5 2019-09-11 stsp col = limit;
2223 b39d25c7 2018-07-10 stsp if (col > avail)
2224 b39d25c7 2018-07-10 stsp goto done;
2226 6570a66d 2019-11-08 stsp if (avail >= 120) {
2227 6570a66d 2019-11-08 stsp char *id_str;
2228 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2230 6570a66d 2019-11-08 stsp goto done;
2231 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2233 11b20872 2019-11-08 stsp wattr_on(view->window,
2234 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2235 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2237 11b20872 2019-11-08 stsp wattr_off(view->window,
2238 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2239 6570a66d 2019-11-08 stsp free(id_str);
2241 6570a66d 2019-11-08 stsp if (col > avail)
2242 6570a66d 2019-11-08 stsp goto done;
2245 f69c5a46 2022-07-19 thomas if (s->use_committer)
2246 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2248 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2249 5813d178 2019-03-09 stsp if (author == NULL) {
2250 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2251 80ddbec8 2018-04-29 stsp goto done;
2253 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2255 bb737323 2018-05-20 stsp goto done;
2256 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2258 11b20872 2019-11-08 stsp wattr_on(view->window,
2259 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2260 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2261 bb737323 2018-05-20 stsp col += author_width;
2262 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2263 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2265 bb737323 2018-05-20 stsp author_width++;
2268 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2269 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2270 9c2eaf34 2018-05-20 stsp if (col > avail)
2271 9c2eaf34 2018-05-20 stsp goto done;
2273 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2275 6d9fbc00 2018-04-29 stsp goto done;
2276 bb737323 2018-05-20 stsp logmsg = logmsg0;
2277 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2279 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2280 bb737323 2018-05-20 stsp if (newline)
2281 bb737323 2018-05-20 stsp *newline = '\0';
2282 f91a2b48 2022-06-23 thomas limit = avail - col;
2283 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2284 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2285 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2286 f91a2b48 2022-06-23 thomas limit, col, 1);
2287 331b1a16 2022-06-23 thomas if (err)
2288 331b1a16 2022-06-23 thomas goto done;
2289 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2290 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2291 27a741e5 2019-09-11 stsp while (col < avail) {
2292 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2296 80ddbec8 2018-04-29 stsp free(logmsg0);
2297 bb737323 2018-05-20 stsp free(wlogmsg);
2298 5813d178 2019-03-09 stsp free(author);
2299 bb737323 2018-05-20 stsp free(wauthor);
2300 80ddbec8 2018-04-29 stsp free(line);
2301 80ddbec8 2018-04-29 stsp return err;
2304 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2305 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2306 899d86c2 2018-05-10 stsp struct got_object_id *id)
2308 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2309 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2311 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2312 80ddbec8 2018-04-29 stsp if (entry == NULL)
2313 899d86c2 2018-05-10 stsp return NULL;
2315 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2316 d68b9737 2022-09-05 thomas if (dup == NULL) {
2317 d68b9737 2022-09-05 thomas free(entry);
2318 d68b9737 2022-09-05 thomas return NULL;
2321 d68b9737 2022-09-05 thomas entry->id = dup;
2322 99db9666 2018-05-07 stsp entry->commit = commit;
2323 899d86c2 2018-05-10 stsp return entry;
2326 99db9666 2018-05-07 stsp static void
2327 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2329 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2331 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2332 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2333 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2334 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2335 d68b9737 2022-09-05 thomas free(entry->id);
2336 99db9666 2018-05-07 stsp free(entry);
2339 99db9666 2018-05-07 stsp static void
2340 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2342 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2343 99db9666 2018-05-07 stsp pop_commit(commits);
2346 c4972b91 2018-05-07 stsp static const struct got_error *
2347 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2348 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2350 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2351 13add988 2019-10-15 stsp regmatch_t regmatch;
2352 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2354 13add988 2019-10-15 stsp *have_match = 0;
2356 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2358 13add988 2019-10-15 stsp return err;
2360 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2362 13add988 2019-10-15 stsp goto done;
2364 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2365 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2366 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2367 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2368 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2369 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2370 13add988 2019-10-15 stsp *have_match = 1;
2372 13add988 2019-10-15 stsp free(id_str);
2373 13add988 2019-10-15 stsp free(logmsg);
2374 13add988 2019-10-15 stsp return err;
2377 13add988 2019-10-15 stsp static const struct got_error *
2378 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2380 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2383 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2384 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2385 1a76625f 2018-10-22 stsp * while updating the display.
2388 7210b715 2022-09-11 thomas struct got_object_id id;
2389 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2390 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2391 7e8004ba 2022-09-11 thomas int limit_match = 0;
2392 1a76625f 2018-10-22 stsp int errcode;
2394 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2395 4e0d2870 2020-12-07 naddy NULL, NULL);
2396 7210b715 2022-09-11 thomas if (err)
2399 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2402 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2403 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2404 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2408 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2409 1a76625f 2018-10-22 stsp if (errcode) {
2410 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2411 13add988 2019-10-15 stsp "pthread_mutex_lock");
2415 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2416 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2417 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2419 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2420 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2421 7e8004ba 2022-09-11 thomas a->limit_regex);
2422 7e8004ba 2022-09-11 thomas if (err)
2425 7e8004ba 2022-09-11 thomas if (limit_match) {
2426 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2428 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2429 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2430 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2431 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2432 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2435 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2436 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2438 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2439 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2440 7e8004ba 2022-09-11 thomas matched, entry);
2441 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2445 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2446 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2447 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2449 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2452 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2453 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2454 7c1452c1 2020-03-26 stsp int have_match;
2455 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2459 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2460 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2461 7e8004ba 2022-09-11 thomas *a->search_next_done =
2462 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2463 7e8004ba 2022-09-11 thomas } else if (have_match)
2464 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2467 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2468 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2469 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2470 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2473 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2475 9ba79e04 2018-06-11 stsp return err;
2478 2b779855 2020-12-05 naddy static void
2479 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2481 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2482 2b779855 2020-12-05 naddy int ncommits = 0;
2484 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2485 2b779855 2020-12-05 naddy while (entry) {
2486 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2487 2b779855 2020-12-05 naddy s->selected_entry = entry;
2490 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2491 2b779855 2020-12-05 naddy ncommits++;
2495 0553a4e3 2018-04-30 stsp static const struct got_error *
2496 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2498 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2499 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2500 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2501 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2502 60493ae3 2019-06-20 stsp int width;
2503 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2504 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2505 8b473291 2019-02-21 stsp char *refs_str = NULL;
2506 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2507 11b20872 2019-11-08 stsp struct tog_color *tc;
2508 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2510 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2511 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2513 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2514 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2515 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2516 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2518 ecb28ae0 2018-07-16 stsp return err;
2519 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2520 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2521 d2075bf3 2020-12-25 stsp if (refs) {
2522 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2523 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2525 d2075bf3 2020-12-25 stsp goto done;
2529 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2530 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2532 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2533 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2534 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2535 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2536 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2537 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2538 8f4ed634 2020-03-26 stsp goto done;
2541 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2542 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2544 f9686aa5 2020-03-27 stsp if (view->searching) {
2545 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2546 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2547 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2548 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2549 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2550 f9686aa5 2020-03-27 stsp search_str = "searching...";
2553 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2554 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2556 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2557 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2558 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2559 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2560 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2561 8f4ed634 2020-03-26 stsp goto done;
2565 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2566 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2567 91198554 2022-06-23 thomas "........................................",
2568 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2569 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2570 1a76625f 2018-10-22 stsp header = NULL;
2571 1a76625f 2018-10-22 stsp goto done;
2573 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2574 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2575 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2576 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2577 1a76625f 2018-10-22 stsp header = NULL;
2578 1a76625f 2018-10-22 stsp goto done;
2580 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2582 1a76625f 2018-10-22 stsp goto done;
2584 2814baeb 2018-08-01 stsp werase(view->window);
2586 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2587 a3404814 2018-09-02 stsp wstandout(view->window);
2588 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2590 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2591 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2592 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2593 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2597 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2598 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2599 a3404814 2018-09-02 stsp wstandend(view->window);
2600 ecb28ae0 2018-07-16 stsp free(wline);
2601 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2602 1a76625f 2018-10-22 stsp goto done;
2604 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2605 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2606 5813d178 2019-03-09 stsp ncommits = 0;
2607 05171be4 2022-06-23 thomas view->maxx = 0;
2608 5813d178 2019-03-09 stsp while (entry) {
2609 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2610 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2611 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2612 5813d178 2019-03-09 stsp int width;
2613 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2615 f69c5a46 2022-07-19 thomas if (s->use_committer)
2616 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2618 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2619 5813d178 2019-03-09 stsp if (author == NULL) {
2620 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2621 5813d178 2019-03-09 stsp goto done;
2623 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2624 27a741e5 2019-09-11 stsp date_display_cols);
2625 5813d178 2019-03-09 stsp if (author_cols < width)
2626 5813d178 2019-03-09 stsp author_cols = width;
2627 5813d178 2019-03-09 stsp free(wauthor);
2628 5813d178 2019-03-09 stsp free(author);
2629 ef944b8b 2022-07-21 thomas if (err)
2630 ef944b8b 2022-07-21 thomas goto done;
2631 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2632 05171be4 2022-06-23 thomas if (err)
2633 05171be4 2022-06-23 thomas goto done;
2634 05171be4 2022-06-23 thomas msg = msg0;
2635 05171be4 2022-06-23 thomas while (*msg == '\n')
2637 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2638 331b1a16 2022-06-23 thomas *eol = '\0';
2639 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2640 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2641 331b1a16 2022-06-23 thomas if (err)
2642 331b1a16 2022-06-23 thomas goto done;
2643 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2644 05171be4 2022-06-23 thomas free(msg0);
2645 331b1a16 2022-06-23 thomas free(wmsg);
2646 7ca04879 2019-10-19 stsp ncommits++;
2647 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2650 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2651 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2652 867c6645 2018-07-10 stsp ncommits = 0;
2653 899d86c2 2018-05-10 stsp while (entry) {
2654 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2656 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2657 2814baeb 2018-08-01 stsp wstandout(view->window);
2658 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2659 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2660 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2661 2814baeb 2018-08-01 stsp wstandend(view->window);
2663 60493ae3 2019-06-20 stsp goto done;
2664 0553a4e3 2018-04-30 stsp ncommits++;
2665 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2666 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2669 a5d43cac 2022-07-01 thomas view_border(view);
2671 1a76625f 2018-10-22 stsp free(id_str);
2672 8b473291 2019-02-21 stsp free(refs_str);
2673 1a76625f 2018-10-22 stsp free(ncommits_str);
2674 1a76625f 2018-10-22 stsp free(header);
2675 80ddbec8 2018-04-29 stsp return err;
2678 07b55e75 2018-05-10 stsp static void
2679 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2681 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2682 07b55e75 2018-05-10 stsp int nscrolled = 0;
2684 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2685 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2688 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2689 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2690 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2691 07b55e75 2018-05-10 stsp if (entry) {
2692 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2693 07b55e75 2018-05-10 stsp nscrolled++;
2698 aa075928 2018-05-10 stsp static const struct got_error *
2699 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2701 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2702 5e224a3e 2019-02-22 stsp int errcode;
2704 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2706 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2707 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2708 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2709 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2710 7aafa0d1 2019-02-22 stsp if (errcode)
2711 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2712 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2715 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2716 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2718 7c1452c1 2020-03-26 stsp if (!wait)
2721 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2722 ffe38506 2020-12-01 naddy show_log_view(view);
2723 7c1452c1 2020-03-26 stsp update_panels();
2724 7c1452c1 2020-03-26 stsp doupdate();
2726 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2727 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2728 82954512 2020-02-03 stsp if (errcode)
2729 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2730 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2732 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2733 ffe38506 2020-12-01 naddy show_log_view(view);
2734 7c1452c1 2020-03-26 stsp update_panels();
2735 7c1452c1 2020-03-26 stsp doupdate();
2738 5e224a3e 2019-02-22 stsp return NULL;
2741 a5d43cac 2022-07-01 thomas static const struct got_error *
2742 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2744 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2745 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2747 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2748 fe731b51 2022-07-14 thomas return NULL;
2750 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2751 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2752 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2754 a5d43cac 2022-07-01 thomas return err;
2757 5e224a3e 2019-02-22 stsp static const struct got_error *
2758 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2760 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2761 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2762 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2763 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2765 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2766 5e224a3e 2019-02-22 stsp return NULL;
2768 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2769 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2770 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2772 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2774 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2775 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2776 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2778 5e224a3e 2019-02-22 stsp return err;
2782 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2783 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2786 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2787 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
2789 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2790 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2792 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2793 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2795 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2796 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2798 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2800 dd0a52c1 2018-05-20 stsp return err;
2803 cd0acaa7 2018-05-20 stsp static const struct got_error *
2804 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2805 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2806 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2808 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2809 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2810 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2812 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2813 15a94983 2018-12-23 stsp if (diff_view == NULL)
2814 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2816 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2817 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2818 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2819 e5a0f69f 2018-08-18 stsp if (err == NULL)
2820 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2821 cd0acaa7 2018-05-20 stsp return err;
2824 80ddbec8 2018-04-29 stsp static const struct got_error *
2825 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2826 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2828 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2830 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2831 941e9f74 2019-05-21 stsp if (parent == NULL)
2832 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2834 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2835 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2836 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2837 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2838 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2839 941e9f74 2019-05-21 stsp s->tree = subtree;
2840 941e9f74 2019-05-21 stsp s->selected = 0;
2841 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2842 941e9f74 2019-05-21 stsp return NULL;
2845 941e9f74 2019-05-21 stsp static const struct got_error *
2846 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2847 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2849 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2850 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2851 941e9f74 2019-05-21 stsp const char *p;
2852 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2854 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2856 941e9f74 2019-05-21 stsp while (*p) {
2857 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2858 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2859 56e0773d 2019-11-28 stsp char *te_name;
2861 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2864 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2865 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2866 941e9f74 2019-05-21 stsp if (slash == NULL)
2867 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2869 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2870 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2871 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2874 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2875 56e0773d 2019-11-28 stsp if (te == NULL) {
2876 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2877 56e0773d 2019-11-28 stsp free(te_name);
2880 56e0773d 2019-11-28 stsp free(te_name);
2881 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2883 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2884 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2886 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2887 941e9f74 2019-05-21 stsp if (slash)
2888 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2890 941e9f74 2019-05-21 stsp subpath = strdup(path);
2891 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2892 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2896 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2901 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2902 941e9f74 2019-05-21 stsp free(tree_id);
2906 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2907 941e9f74 2019-05-21 stsp if (err) {
2908 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2911 941e9f74 2019-05-21 stsp if (slash == NULL)
2913 941e9f74 2019-05-21 stsp free(subpath);
2914 941e9f74 2019-05-21 stsp subpath = NULL;
2915 941e9f74 2019-05-21 stsp p = slash;
2918 941e9f74 2019-05-21 stsp free(subpath);
2919 1a76625f 2018-10-22 stsp return err;
2922 61266923 2020-01-14 stsp static const struct got_error *
2923 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2924 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2925 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2927 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2928 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2929 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2931 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2932 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2933 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2935 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2937 55cccc34 2020-02-20 stsp return err;
2938 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2940 55cccc34 2020-02-20 stsp *new_view = tree_view;
2942 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2943 55cccc34 2020-02-20 stsp return NULL;
2945 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2948 55cccc34 2020-02-20 stsp static const struct got_error *
2949 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2951 61266923 2020-01-14 stsp sigset_t sigset;
2952 61266923 2020-01-14 stsp int errcode;
2954 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2955 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2957 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2958 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2959 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2960 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2961 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2962 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2963 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2964 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2965 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2967 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2968 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2969 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2971 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2972 61266923 2020-01-14 stsp if (errcode)
2973 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2975 61266923 2020-01-14 stsp return NULL;
2978 1a76625f 2018-10-22 stsp static void *
2979 1a76625f 2018-10-22 stsp log_thread(void *arg)
2981 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2982 1a76625f 2018-10-22 stsp int errcode = 0;
2983 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2984 1a76625f 2018-10-22 stsp int done = 0;
2987 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2988 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2990 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2991 f2d749db 2022-07-12 thomas if (errcode) {
2992 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2993 61266923 2020-01-14 stsp return (void *)err;
2996 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2997 f2d749db 2022-07-12 thomas if (err) {
2998 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2999 f2d749db 2022-07-12 thomas goto done;
3002 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3003 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3004 f2d749db 2022-07-12 thomas if (errcode) {
3005 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3006 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3007 f2d749db 2022-07-12 thomas goto done;
3009 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3010 1a76625f 2018-10-22 stsp if (err) {
3011 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3012 f2d749db 2022-07-12 thomas goto done;
3013 1a76625f 2018-10-22 stsp err = NULL;
3015 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3016 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3017 7e8004ba 2022-09-11 thomas if (a->limit_match)
3018 7e8004ba 2022-09-11 thomas a->commits_needed--;
3020 7e8004ba 2022-09-11 thomas a->commits_needed--;
3023 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3024 3abe8080 2019-04-10 stsp if (errcode) {
3025 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3026 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3027 f2d749db 2022-07-12 thomas goto done;
3028 3abe8080 2019-04-10 stsp } else if (*a->quit)
3030 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3031 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3032 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3033 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3034 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3035 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3036 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3037 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3040 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3041 7c1452c1 2020-03-26 stsp if (errcode) {
3042 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3043 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3044 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3045 f2d749db 2022-07-12 thomas goto done;
3049 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3051 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3052 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3053 7c1452c1 2020-03-26 stsp &tog_mutex);
3054 f2d749db 2022-07-12 thomas if (errcode) {
3055 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3056 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3057 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3058 f2d749db 2022-07-12 thomas goto done;
3060 21355643 2020-12-06 stsp if (*a->quit)
3065 3abe8080 2019-04-10 stsp a->log_complete = 1;
3066 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3067 f2d749db 2022-07-12 thomas if (errcode)
3068 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3070 f2d749db 2022-07-12 thomas if (err) {
3071 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3072 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3074 1a76625f 2018-10-22 stsp return (void *)err;
3077 1a76625f 2018-10-22 stsp static const struct got_error *
3078 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3080 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3081 1a76625f 2018-10-22 stsp int errcode;
3083 1a76625f 2018-10-22 stsp if (s->thread) {
3084 1a76625f 2018-10-22 stsp s->quit = 1;
3085 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
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_cond_signal");
3089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3090 1a76625f 2018-10-22 stsp if (errcode)
3091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3092 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3093 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3094 1a76625f 2018-10-22 stsp if (errcode)
3095 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3096 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3097 1a76625f 2018-10-22 stsp if (errcode)
3098 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3099 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3100 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3103 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3104 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3105 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3108 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3109 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3110 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3111 1af5eddf 2022-06-23 thomas if (err == NULL)
3112 1af5eddf 2022-06-23 thomas err = pack_err;
3113 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3116 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3117 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3118 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3121 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3124 9343a5fb 2018-06-23 stsp static const struct got_error *
3125 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3127 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3128 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3129 276b94a1 2020-11-13 naddy int errcode;
3131 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3133 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
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 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3138 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3139 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3141 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3142 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3143 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3144 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3145 1a76625f 2018-10-22 stsp free(s->start_id);
3146 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3147 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3148 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3149 1a76625f 2018-10-22 stsp return err;
3153 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3154 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3155 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3156 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3157 7e8004ba 2022-09-11 thomas * works with very slight change.
3159 1a76625f 2018-10-22 stsp static const struct got_error *
3160 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3162 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3163 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3164 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3165 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3166 7e8004ba 2022-09-11 thomas char pattern[1024];
3167 7e8004ba 2022-09-11 thomas int ret;
3169 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3170 7e8004ba 2022-09-11 thomas v = view->child;
3171 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3172 7e8004ba 2022-09-11 thomas v = view->parent;
3174 7e8004ba 2022-09-11 thomas /* Get the pattern */
3175 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3176 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3177 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3178 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3179 7e8004ba 2022-09-11 thomas nocbreak();
3181 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3182 7e8004ba 2022-09-11 thomas cbreak();
3183 7e8004ba 2022-09-11 thomas noecho();
3184 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3185 7e8004ba 2022-09-11 thomas if (ret == ERR)
3186 7e8004ba 2022-09-11 thomas return NULL;
3188 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3190 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3191 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3193 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3194 7e8004ba 2022-09-11 thomas return NULL;
3197 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3198 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3199 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3200 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3201 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3202 7e8004ba 2022-09-11 thomas * the queue.
3204 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3205 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3206 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3207 7e8004ba 2022-09-11 thomas s->selected = 0;
3208 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3210 7e8004ba 2022-09-11 thomas return NULL;
3213 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3214 7e8004ba 2022-09-11 thomas return NULL;
3216 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3218 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3219 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3220 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3221 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3222 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3224 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3225 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3226 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3228 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3229 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3230 7e8004ba 2022-09-11 thomas int have_match = 0;
3232 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3233 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3234 7e8004ba 2022-09-11 thomas if (err)
3235 7e8004ba 2022-09-11 thomas return err;
3237 7e8004ba 2022-09-11 thomas if (have_match) {
3238 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3240 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3241 7e8004ba 2022-09-11 thomas entry->id);
3242 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3243 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3244 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3247 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3248 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3250 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3251 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3252 7e8004ba 2022-09-11 thomas matched, entry);
3253 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3257 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3258 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3259 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3260 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3261 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3262 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3263 7e8004ba 2022-09-11 thomas if (err)
3264 7e8004ba 2022-09-11 thomas return err;
3267 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3268 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3269 7e8004ba 2022-09-11 thomas s->selected = 0;
3271 7e8004ba 2022-09-11 thomas return NULL;
3274 7e8004ba 2022-09-11 thomas static const struct got_error *
3275 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3277 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3279 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3280 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3281 60493ae3 2019-06-20 stsp return NULL;
3284 60493ae3 2019-06-20 stsp static const struct got_error *
3285 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3287 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3288 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3289 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3291 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3292 f9686aa5 2020-03-27 stsp show_log_view(view);
3293 f9686aa5 2020-03-27 stsp update_panels();
3294 f9686aa5 2020-03-27 stsp doupdate();
3296 96e2b566 2019-07-08 stsp if (s->search_entry) {
3297 13add988 2019-10-15 stsp int errcode, ch;
3298 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3299 13add988 2019-10-15 stsp if (errcode)
3300 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3301 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3302 13add988 2019-10-15 stsp ch = wgetch(view->window);
3303 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3304 13add988 2019-10-15 stsp if (errcode)
3305 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3306 13add988 2019-10-15 stsp "pthread_mutex_lock");
3307 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3308 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3309 678cbce5 2019-07-28 stsp return NULL;
3311 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3312 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3314 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3315 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3316 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3318 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3319 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3320 1f4d5162 2022-06-23 thomas * might have changed.
3322 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3323 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3325 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3328 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3331 60493ae3 2019-06-20 stsp while (1) {
3332 13add988 2019-10-15 stsp int have_match = 0;
3334 60493ae3 2019-06-20 stsp if (entry == NULL) {
3335 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3336 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3337 f9967bca 2020-03-27 stsp view->search_next_done =
3338 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3339 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3340 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3341 f801134a 2019-06-25 stsp return NULL;
3344 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3345 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3346 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3348 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3349 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3352 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3353 13add988 2019-10-15 stsp &view->regex);
3356 13add988 2019-10-15 stsp if (have_match) {
3357 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3358 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3362 96e2b566 2019-07-08 stsp s->search_entry = entry;
3363 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3364 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3366 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3369 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3370 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3371 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3372 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3374 60493ae3 2019-06-20 stsp return err;
3377 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3378 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3380 60493ae3 2019-06-20 stsp return err;
3385 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3387 60493ae3 2019-06-20 stsp return NULL;
3390 60493ae3 2019-06-20 stsp static const struct got_error *
3391 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3392 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3393 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3395 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3396 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3397 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3398 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3399 1a76625f 2018-10-22 stsp int errcode;
3401 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3402 f135c941 2020-02-20 stsp free(s->in_repo_path);
3403 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3404 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3405 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3408 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3409 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3410 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3411 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3413 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3414 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3415 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3417 fb2756b9 2018-08-04 stsp s->repo = repo;
3418 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3419 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3420 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3421 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3422 9cd7cbd1 2020-12-07 stsp goto done;
3425 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3426 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3427 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3428 5036bf37 2018-09-24 stsp goto done;
3430 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3431 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3433 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3434 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3435 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3436 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3438 11b20872 2019-11-08 stsp goto done;
3439 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3440 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3441 11b20872 2019-11-08 stsp if (err) {
3442 11b20872 2019-11-08 stsp free_colors(&s->colors);
3443 11b20872 2019-11-08 stsp goto done;
3445 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3446 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3447 11b20872 2019-11-08 stsp if (err) {
3448 11b20872 2019-11-08 stsp free_colors(&s->colors);
3449 11b20872 2019-11-08 stsp goto done;
3453 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3454 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3455 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3456 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3457 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3458 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3460 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3461 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3462 1af5eddf 2022-06-23 thomas if (err)
3463 1af5eddf 2022-06-23 thomas goto done;
3465 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3466 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3467 7cd52833 2022-06-23 thomas if (err)
3468 7cd52833 2022-06-23 thomas goto done;
3469 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3470 b672a97a 2020-01-27 stsp !s->log_branches);
3472 1a76625f 2018-10-22 stsp goto done;
3473 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3474 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3476 c5b78334 2020-01-12 stsp goto done;
3478 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3479 1a76625f 2018-10-22 stsp if (errcode) {
3480 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3481 1a76625f 2018-10-22 stsp goto done;
3483 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3484 7c1452c1 2020-03-26 stsp if (errcode) {
3485 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3486 7c1452c1 2020-03-26 stsp goto done;
3489 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3490 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3491 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3492 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3493 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3494 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3495 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3496 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3497 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3498 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3499 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3500 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3501 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3502 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3503 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3504 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3505 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3508 1a76625f 2018-10-22 stsp close_log_view(view);
3509 ba4f502b 2018-08-04 stsp return err;
3512 e5a0f69f 2018-08-18 stsp static const struct got_error *
3513 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3515 f2f6d207 2020-11-24 stsp const struct got_error *err;
3516 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3518 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3519 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3520 2b380cc8 2018-10-24 stsp &s->thread_args);
3521 2b380cc8 2018-10-24 stsp if (errcode)
3522 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3523 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3524 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3526 f2f6d207 2020-11-24 stsp return err;
3530 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3533 b31f89ff 2022-07-01 thomas static void
3534 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3536 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3538 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3540 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3541 7e8004ba 2022-09-11 thomas view->count = 0;
3543 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3544 b31f89ff 2022-07-01 thomas || home)
3545 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3547 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3548 b31f89ff 2022-07-01 thomas --s->selected;
3550 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3552 b31f89ff 2022-07-01 thomas select_commit(s);
3556 b31f89ff 2022-07-01 thomas static const struct got_error *
3557 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3559 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3560 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3561 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3563 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3564 7e8004ba 2022-09-11 thomas return NULL;
3566 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3567 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3568 b31f89ff 2022-07-01 thomas return NULL;
3570 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3571 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3573 7ed048bd 2022-08-12 thomas if (!page) {
3574 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3575 b31f89ff 2022-07-01 thomas ++s->selected;
3577 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3578 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3579 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3582 7ed048bd 2022-08-12 thomas s->selected = 0;
3583 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3584 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3585 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3586 7ed048bd 2022-08-12 thomas if (entry == NULL)
3588 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3589 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3591 7ed048bd 2022-08-12 thomas if (n > 0)
3592 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3593 b31f89ff 2022-07-01 thomas } else {
3594 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3595 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3596 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3597 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3599 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3601 b31f89ff 2022-07-01 thomas if (err)
3602 b31f89ff 2022-07-01 thomas return err;
3605 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3606 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3608 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3609 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3610 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3611 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3614 b31f89ff 2022-07-01 thomas select_commit(s);
3616 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3617 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3618 b31f89ff 2022-07-01 thomas view->count = 0;
3620 b31f89ff 2022-07-01 thomas return NULL;
3623 a5d43cac 2022-07-01 thomas static void
3624 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3629 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3630 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3631 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3632 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3633 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3635 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3636 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3637 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3638 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3639 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3640 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3642 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3646 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3647 e5a0f69f 2018-08-18 stsp static const struct got_error *
3648 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3650 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3652 a5d43cac 2022-07-01 thomas view->nlines = y;
3653 64486692 2022-07-07 thomas view->ncols = COLS;
3654 a5d43cac 2022-07-01 thomas err = view_resize(view);
3655 a5d43cac 2022-07-01 thomas if (err)
3656 a5d43cac 2022-07-01 thomas return err;
3658 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3660 a5d43cac 2022-07-01 thomas return err;
3663 07dd3ed3 2022-08-06 thomas static const struct got_error *
3664 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3666 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3667 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3668 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3670 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3671 25100026 2022-08-13 thomas return NULL;
3673 07dd3ed3 2022-08-06 thomas g = view->gline;
3674 07dd3ed3 2022-08-06 thomas view->gline = 0;
3676 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3677 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3678 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3679 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3680 07dd3ed3 2022-08-06 thomas select_commit(s);
3681 07dd3ed3 2022-08-06 thomas return NULL;
3684 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3685 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3686 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3687 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3688 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3689 07dd3ed3 2022-08-06 thomas if (err)
3690 07dd3ed3 2022-08-06 thomas return err;
3691 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3692 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3694 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3695 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3697 07dd3ed3 2022-08-06 thomas select_commit(s);
3698 07dd3ed3 2022-08-06 thomas return NULL;
3702 c72de8ab 2023-02-03 thomas static void
3703 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
3706 c72de8ab 2023-02-03 thomas switch (ch) {
3707 c72de8ab 2023-02-03 thomas case KEY_LEFT:
3708 c72de8ab 2023-02-03 thomas case 'h':
3709 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
3710 c72de8ab 2023-02-03 thomas if (view->x <= 0)
3711 c72de8ab 2023-02-03 thomas view->count = 0;
3713 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
3714 c72de8ab 2023-02-03 thomas case 'l':
3715 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
3716 c72de8ab 2023-02-03 thomas view->x += 2;
3718 c72de8ab 2023-02-03 thomas view->count = 0;
3720 c72de8ab 2023-02-03 thomas case '0':
3721 c72de8ab 2023-02-03 thomas view->x = 0;
3723 c72de8ab 2023-02-03 thomas case '$':
3724 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3725 c72de8ab 2023-02-03 thomas view->count = 0;
3727 c72de8ab 2023-02-03 thomas default:
3732 a5d43cac 2022-07-01 thomas static const struct got_error *
3733 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3735 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3736 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3737 7ed048bd 2022-08-12 thomas int eos, nscroll;
3739 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3740 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3741 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3742 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3743 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3744 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3746 c0be8933 2022-08-12 thomas if (err)
3747 c0be8933 2022-08-12 thomas return err;
3750 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3751 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3752 068ab281 2022-07-01 thomas --eos; /* border */
3754 07dd3ed3 2022-08-06 thomas if (view->gline)
3755 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3757 528dedf3 2021-08-30 stsp switch (ch) {
3758 7e8004ba 2022-09-11 thomas case '&':
3759 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3762 1e37a5c2 2019-05-12 jcs s->quit = 1;
3764 05171be4 2022-06-23 thomas case '0':
3765 05171be4 2022-06-23 thomas case '$':
3766 05171be4 2022-06-23 thomas case KEY_RIGHT:
3767 05171be4 2022-06-23 thomas case 'l':
3768 05171be4 2022-06-23 thomas case KEY_LEFT:
3769 05171be4 2022-06-23 thomas case 'h':
3770 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
3773 1e37a5c2 2019-05-12 jcs case KEY_UP:
3776 f7140bf5 2021-10-17 thomas case CTRL('p'):
3777 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3780 aa7a1117 2023-01-09 thomas case '=':
3781 912a3f79 2021-08-30 j case KEY_HOME:
3782 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3783 07b0611c 2022-06-23 thomas view->count = 0;
3785 70f17a53 2022-06-13 thomas case CTRL('u'):
3786 23427b14 2022-06-23 thomas case 'u':
3787 70f17a53 2022-06-13 thomas nscroll /= 2;
3788 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3789 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3790 a4292ac5 2019-05-12 jcs case CTRL('b'):
3791 1c5e5faa 2022-06-23 thomas case 'b':
3792 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3795 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3798 f7140bf5 2021-10-17 thomas case CTRL('n'):
3799 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3801 f69c5a46 2022-07-19 thomas case '@':
3802 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3803 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
3804 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
3807 aa7a1117 2023-01-09 thomas case '*':
3808 912a3f79 2021-08-30 j case KEY_END: {
3809 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3810 912a3f79 2021-08-30 j * traverse them all. */
3811 07b0611c 2022-06-23 thomas view->count = 0;
3812 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3813 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3814 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3815 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3816 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3819 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3820 23427b14 2022-06-23 thomas case 'd':
3821 70f17a53 2022-06-13 thomas nscroll /= 2;
3822 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3823 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3824 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3825 4c2d69cb 2022-06-23 thomas case 'f':
3826 b31f89ff 2022-07-01 thomas case ' ':
3827 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3829 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3830 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3831 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3832 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3833 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3834 2b779855 2020-12-05 naddy select_commit(s);
3835 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3836 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3837 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3838 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3839 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3842 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3843 444d5325 2022-07-03 thomas case '\r':
3844 07b0611c 2022-06-23 thomas view->count = 0;
3845 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3847 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3849 1be4947a 2022-07-22 thomas case 'T':
3850 07b0611c 2022-06-23 thomas view->count = 0;
3851 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3853 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3855 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3856 21355643 2020-12-06 stsp case CTRL('l'):
3858 07b0611c 2022-06-23 thomas view->count = 0;
3859 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3860 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3862 21355643 2020-12-06 stsp err = stop_log_thread(s);
3864 74cfe85e 2020-10-20 stsp return err;
3865 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3866 21355643 2020-12-06 stsp char *parent_path;
3867 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3869 21355643 2020-12-06 stsp return err;
3870 21355643 2020-12-06 stsp free(s->in_repo_path);
3871 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3872 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3873 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3874 21355643 2020-12-06 stsp struct got_object_id *start_id;
3875 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3876 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3877 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3878 466429a1 2022-11-17 thomas if (err) {
3879 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
3880 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
3881 466429a1 2022-11-17 thomas return err;
3882 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
3883 466429a1 2022-11-17 thomas free(s->head_ref_name);
3884 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
3885 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
3886 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3887 466429a1 2022-11-17 thomas &tog_refs, s->repo);
3888 466429a1 2022-11-17 thomas if (err)
3889 466429a1 2022-11-17 thomas return err;
3891 21355643 2020-12-06 stsp free(s->start_id);
3892 21355643 2020-12-06 stsp s->start_id = start_id;
3893 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3894 21355643 2020-12-06 stsp } else /* 'B' */
3895 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3897 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3898 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3899 bf7e79b3 2022-06-23 thomas if (err)
3900 bf7e79b3 2022-06-23 thomas return err;
3902 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3903 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3904 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3906 21355643 2020-12-06 stsp return err;
3907 51a10b52 2020-12-26 stsp tog_free_refs();
3908 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3910 ca51c541 2020-12-07 stsp return err;
3911 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3912 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3914 d01904d4 2019-06-25 stsp return err;
3915 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3916 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3918 b672a97a 2020-01-27 stsp return err;
3919 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3920 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3921 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3922 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3923 21355643 2020-12-06 stsp s->selected_entry = NULL;
3924 21355643 2020-12-06 stsp s->selected = 0;
3925 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3926 21355643 2020-12-06 stsp s->quit = 0;
3927 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3928 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3929 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3930 94ecf40d 2022-07-22 thomas view->offset = 0;
3932 1be4947a 2022-07-22 thomas case 'R':
3933 07b0611c 2022-06-23 thomas view->count = 0;
3934 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3937 07b0611c 2022-06-23 thomas view->count = 0;
3941 80ddbec8 2018-04-29 stsp return err;
3944 4ed7e80c 2018-05-20 stsp static const struct got_error *
3945 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3947 c2db6724 2019-01-04 stsp const struct got_error *error;
3949 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3950 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3951 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3953 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3954 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3956 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3957 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3959 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3960 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3962 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3963 c2db6724 2019-01-04 stsp if (error != NULL)
3964 c2db6724 2019-01-04 stsp return error;
3966 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3967 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3969 c2db6724 2019-01-04 stsp return NULL;
3972 a915003a 2019-02-05 stsp static void
3973 a915003a 2019-02-05 stsp init_curses(void)
3976 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3977 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3978 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3980 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3981 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3982 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3983 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3984 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3986 a915003a 2019-02-05 stsp initscr();
3988 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3991 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3992 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3993 a915003a 2019-02-05 stsp curs_set(0);
3994 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3995 6d17833f 2019-11-08 stsp start_color();
3996 6d17833f 2019-11-08 stsp use_default_colors();
4000 c2db6724 2019-01-04 stsp static const struct got_error *
4001 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4002 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4004 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4006 f135c941 2020-02-20 stsp if (argc == 0) {
4007 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4008 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4009 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4010 f135c941 2020-02-20 stsp return NULL;
4013 f135c941 2020-02-20 stsp if (worktree) {
4014 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4017 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4019 f135c941 2020-02-20 stsp return err;
4020 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4021 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4022 bfd61697 2020-11-14 stsp p) == -1) {
4023 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4024 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4028 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4030 f135c941 2020-02-20 stsp return err;
4033 f135c941 2020-02-20 stsp static const struct got_error *
4034 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4036 80ddbec8 2018-04-29 stsp const struct got_error *error;
4037 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4038 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4039 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4040 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4041 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4042 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4043 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4044 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4045 04cc582a 2018-08-01 stsp struct tog_view *view;
4046 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4048 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4049 80ddbec8 2018-04-29 stsp switch (ch) {
4051 b672a97a 2020-01-27 stsp log_branches = 1;
4054 80ddbec8 2018-04-29 stsp start_commit = optarg;
4057 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4058 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4059 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4063 17020d27 2019-03-07 stsp usage_log();
4064 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4068 80ddbec8 2018-04-29 stsp argc -= optind;
4069 80ddbec8 2018-04-29 stsp argv += optind;
4071 f135c941 2020-02-20 stsp if (argc > 1)
4072 f135c941 2020-02-20 stsp usage_log();
4074 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4075 7cd52833 2022-06-23 thomas if (error != NULL)
4076 7cd52833 2022-06-23 thomas goto done;
4078 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4079 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4080 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4081 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4082 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4083 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4084 c156c7a4 2020-12-18 stsp goto done;
4085 a1fbf39a 2019-08-11 stsp if (worktree)
4086 6962eb72 2020-02-20 stsp repo_path =
4087 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4089 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4090 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4091 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4092 c156c7a4 2020-12-18 stsp goto done;
4096 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4097 80ddbec8 2018-04-29 stsp if (error != NULL)
4098 ecb28ae0 2018-07-16 stsp goto done;
4100 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4101 f135c941 2020-02-20 stsp repo, worktree);
4102 f135c941 2020-02-20 stsp if (error)
4103 f135c941 2020-02-20 stsp goto done;
4105 f135c941 2020-02-20 stsp init_curses();
4107 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4108 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4109 c02c541e 2019-03-29 stsp if (error)
4110 c02c541e 2019-03-29 stsp goto done;
4112 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4113 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4114 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4115 87670572 2020-12-26 naddy if (error)
4116 87670572 2020-12-26 naddy goto done;
4119 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4120 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4121 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4122 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4123 d8f38dc4 2020-12-05 stsp if (error)
4124 d8f38dc4 2020-12-05 stsp goto done;
4125 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4127 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4128 d8f38dc4 2020-12-05 stsp if (error == NULL)
4129 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4130 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4131 d8f38dc4 2020-12-05 stsp goto done;
4132 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4133 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4134 d8f38dc4 2020-12-05 stsp if (error)
4135 d8f38dc4 2020-12-05 stsp goto done;
4138 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4139 04cc582a 2018-08-01 stsp if (view == NULL) {
4140 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4141 04cc582a 2018-08-01 stsp goto done;
4143 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4144 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4145 ba4f502b 2018-08-04 stsp if (error)
4146 ba4f502b 2018-08-04 stsp goto done;
4147 2fc00ff4 2019-08-31 stsp if (worktree) {
4148 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4149 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4150 2fc00ff4 2019-08-31 stsp worktree = NULL;
4152 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4154 f135c941 2020-02-20 stsp free(in_repo_path);
4155 ecb28ae0 2018-07-16 stsp free(repo_path);
4156 ecb28ae0 2018-07-16 stsp free(cwd);
4157 899d86c2 2018-05-10 stsp free(start_id);
4158 d8f38dc4 2020-12-05 stsp free(label);
4160 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4161 1d0f4054 2021-06-17 stsp if (repo) {
4162 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4163 1d0f4054 2021-06-17 stsp if (error == NULL)
4164 1d0f4054 2021-06-17 stsp error = close_err;
4166 ec142235 2019-03-07 stsp if (worktree)
4167 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4168 7cd52833 2022-06-23 thomas if (pack_fds) {
4169 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4170 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4171 7cd52833 2022-06-23 thomas if (error == NULL)
4172 7cd52833 2022-06-23 thomas error = pack_err;
4174 51a10b52 2020-12-26 stsp tog_free_refs();
4175 80ddbec8 2018-04-29 stsp return error;
4178 4ed7e80c 2018-05-20 stsp __dead static void
4179 9f7d7167 2018-04-29 stsp usage_diff(void)
4182 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4183 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4187 6d17833f 2019-11-08 stsp static int
4188 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4189 41605754 2020-11-12 stsp regmatch_t *regmatch)
4191 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4194 ef20f542 2022-06-26 thomas static struct tog_color *
4195 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4197 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4199 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4200 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4201 6d17833f 2019-11-08 stsp return tc;
4204 6d17833f 2019-11-08 stsp return NULL;
4207 4ed7e80c 2018-05-20 stsp static const struct got_error *
4208 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4209 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4211 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4212 666f7b10 2022-06-23 thomas char *exstr = NULL;
4213 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4214 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4215 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4216 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4218 41605754 2020-11-12 stsp *wtotal = 0;
4220 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4221 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4223 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4224 666f7b10 2022-06-23 thomas if (err)
4225 666f7b10 2022-06-23 thomas return err;
4227 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4228 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4229 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4230 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4231 666f7b10 2022-06-23 thomas goto done;
4233 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4234 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4235 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4236 cbea3800 2022-06-23 thomas goto done;
4238 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4239 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4240 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4241 cbea3800 2022-06-23 thomas goto done;
4244 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4245 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4246 cbea3800 2022-06-23 thomas col_tab_align, 1);
4247 cbea3800 2022-06-23 thomas if (err)
4248 cbea3800 2022-06-23 thomas goto done;
4249 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4250 05171be4 2022-06-23 thomas if (n) {
4251 cbea3800 2022-06-23 thomas free(wline);
4252 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4253 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4254 cbea3800 2022-06-23 thomas if (err)
4255 cbea3800 2022-06-23 thomas goto done;
4256 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4257 cbea3800 2022-06-23 thomas wlimit -= width;
4258 cbea3800 2022-06-23 thomas *wtotal += width;
4261 41605754 2020-11-12 stsp if (wlimit > 0) {
4262 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4263 cbea3800 2022-06-23 thomas size_t wlen;
4265 cbea3800 2022-06-23 thomas free(wline);
4266 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4267 cbea3800 2022-06-23 thomas col_tab_align, 1);
4268 cbea3800 2022-06-23 thomas if (err)
4269 cbea3800 2022-06-23 thomas goto done;
4270 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4271 cbea3800 2022-06-23 thomas while (i < wlen) {
4272 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4273 cbea3800 2022-06-23 thomas if (width == -1) {
4274 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4275 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4276 cbea3800 2022-06-23 thomas goto done;
4278 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4280 1c5e5faa 2022-06-23 thomas w += width;
4283 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4284 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4285 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4286 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4287 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4288 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4289 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4293 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4294 cbea3800 2022-06-23 thomas free(wline);
4295 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4296 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4297 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4298 cbea3800 2022-06-23 thomas col_tab_align, 1);
4299 cbea3800 2022-06-23 thomas if (err)
4300 cbea3800 2022-06-23 thomas goto done;
4301 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4302 cbea3800 2022-06-23 thomas } else {
4303 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4304 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4305 cbea3800 2022-06-23 thomas if (err)
4306 cbea3800 2022-06-23 thomas goto done;
4307 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4309 cbea3800 2022-06-23 thomas *wtotal += width2;
4312 05171be4 2022-06-23 thomas free(wline);
4313 666f7b10 2022-06-23 thomas free(exstr);
4314 cbea3800 2022-06-23 thomas free(seg0);
4315 cbea3800 2022-06-23 thomas free(seg1);
4316 cbea3800 2022-06-23 thomas free(seg2);
4317 cbea3800 2022-06-23 thomas return err;
4320 07dd3ed3 2022-08-06 thomas static int
4321 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4323 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4324 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4326 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4327 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4329 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4330 fc2737d5 2022-09-15 thomas selected = first;
4331 fc2737d5 2022-09-15 thomas eof = &s->eof;
4332 fc2737d5 2022-09-15 thomas f = s->f;
4333 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4334 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4336 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4337 07dd3ed3 2022-08-06 thomas selected = first;
4338 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4339 07dd3ed3 2022-08-06 thomas f = s->f;
4340 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4341 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4343 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4344 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4345 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4346 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4348 07dd3ed3 2022-08-06 thomas return 0;
4350 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4351 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4352 07dd3ed3 2022-08-06 thomas return 0;
4353 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4354 07dd3ed3 2022-08-06 thomas rewind(f);
4355 07dd3ed3 2022-08-06 thomas *eof = 0;
4356 07dd3ed3 2022-08-06 thomas *first = 1;
4357 07dd3ed3 2022-08-06 thomas *lineno = 0;
4358 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4359 07dd3ed3 2022-08-06 thomas return 0;
4362 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4363 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4364 07dd3ed3 2022-08-06 thomas view->gline = 0;
4366 07dd3ed3 2022-08-06 thomas return 1;
4369 41605754 2020-11-12 stsp static const struct got_error *
4370 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4372 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4373 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4374 61e69b96 2018-05-20 stsp const struct got_error *err;
4375 82c78e96 2022-08-06 thomas int nprinted = 0;
4376 b304db33 2018-05-20 stsp char *line;
4377 826082fe 2020-12-10 stsp size_t linesize = 0;
4378 826082fe 2020-12-10 stsp ssize_t linelen;
4379 61e69b96 2018-05-20 stsp wchar_t *wline;
4380 e0b650dd 2018-05-20 stsp int width;
4381 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4382 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4383 fe621944 2020-11-10 stsp off_t line_offset;
4385 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4386 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4387 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4388 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4390 f7d12f7e 2018-08-01 stsp werase(view->window);
4392 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4393 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4395 a3404814 2018-09-02 stsp if (header) {
4396 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4397 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4398 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4400 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4401 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4402 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4404 135a2da0 2020-11-11 stsp free(line);
4406 a3404814 2018-09-02 stsp return err;
4408 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4409 a3404814 2018-09-02 stsp wstandout(view->window);
4410 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4411 e54cc94a 2020-11-11 stsp free(wline);
4412 e54cc94a 2020-11-11 stsp wline = NULL;
4413 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4414 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4415 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4416 a3404814 2018-09-02 stsp wstandend(view->window);
4418 a3404814 2018-09-02 stsp if (max_lines <= 1)
4419 a3404814 2018-09-02 stsp return NULL;
4420 a3404814 2018-09-02 stsp max_lines--;
4423 89f1a395 2020-12-01 naddy s->eof = 0;
4424 05171be4 2022-06-23 thomas view->maxx = 0;
4425 826082fe 2020-12-10 stsp line = NULL;
4426 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4427 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4428 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4430 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4431 826082fe 2020-12-10 stsp if (linelen == -1) {
4432 826082fe 2020-12-10 stsp if (feof(s->f)) {
4433 826082fe 2020-12-10 stsp s->eof = 1;
4436 826082fe 2020-12-10 stsp free(line);
4437 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4440 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4441 07dd3ed3 2022-08-06 thomas continue;
4442 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4443 07dd3ed3 2022-08-06 thomas continue;
4444 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4445 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4447 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4448 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4449 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4450 cbea3800 2022-06-23 thomas if (err) {
4451 cbea3800 2022-06-23 thomas free(line);
4452 cbea3800 2022-06-23 thomas return err;
4454 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4455 cbea3800 2022-06-23 thomas free(wline);
4456 cbea3800 2022-06-23 thomas wline = NULL;
4458 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4459 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4460 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4461 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4462 07dd3ed3 2022-08-06 thomas if (attr)
4463 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4464 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4465 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4466 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4467 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4468 41605754 2020-11-12 stsp if (err) {
4469 41605754 2020-11-12 stsp free(line);
4470 41605754 2020-11-12 stsp return err;
4473 f1c0ec19 2022-06-23 thomas int skip;
4474 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4475 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4476 f1c0ec19 2022-06-23 thomas if (err) {
4477 f1c0ec19 2022-06-23 thomas free(line);
4478 f1c0ec19 2022-06-23 thomas return err;
4480 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4481 f1c0ec19 2022-06-23 thomas free(wline);
4482 41605754 2020-11-12 stsp wline = NULL;
4484 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4485 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4486 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4487 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4488 07dd3ed3 2022-08-06 thomas } else {
4489 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4490 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4492 07dd3ed3 2022-08-06 thomas if (attr)
4493 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4494 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4495 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4497 826082fe 2020-12-10 stsp free(line);
4498 fe621944 2020-11-10 stsp if (nprinted >= 1)
4499 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4500 89f1a395 2020-12-01 naddy (nprinted - 1);
4502 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4504 a5d43cac 2022-07-01 thomas view_border(view);
4506 89f1a395 2020-12-01 naddy if (s->eof) {
4507 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4508 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4509 c3e9aa98 2019-05-13 jcs nprinted++;
4512 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4513 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4515 c3e9aa98 2019-05-13 jcs return err;
4518 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4519 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4520 e54cc94a 2020-11-11 stsp free(wline);
4521 e54cc94a 2020-11-11 stsp wline = NULL;
4522 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4525 26ed57b2 2018-05-19 stsp return NULL;
4528 abd2672a 2018-12-23 stsp static char *
4529 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4531 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4532 09867e48 2019-08-13 stsp char *p, *s;
4534 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4535 09867e48 2019-08-13 stsp if (tm == NULL)
4536 09867e48 2019-08-13 stsp return NULL;
4537 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4538 09867e48 2019-08-13 stsp if (s == NULL)
4539 09867e48 2019-08-13 stsp return NULL;
4540 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4542 abd2672a 2018-12-23 stsp *p = '\0';
4546 4ed7e80c 2018-05-20 stsp static const struct got_error *
4547 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4548 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4550 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4552 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4553 fe621944 2020-11-10 stsp if (p == NULL)
4554 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4555 82c78e96 2022-08-06 thomas *lines = p;
4556 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4557 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4558 fe621944 2020-11-10 stsp (*nlines)++;
4560 fe621944 2020-11-10 stsp return NULL;
4563 fe621944 2020-11-10 stsp static const struct got_error *
4564 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4565 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
4567 be97ab03 2023-01-19 thomas struct got_diff_line *p;
4568 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
4569 be97ab03 2023-01-19 thomas size_t i, r;
4571 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
4572 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
4574 be97ab03 2023-01-19 thomas for (;;) {
4575 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
4576 be97ab03 2023-01-19 thomas if (r == 0) {
4577 be97ab03 2023-01-19 thomas if (ferror(src))
4578 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
4579 be97ab03 2023-01-19 thomas if (feof(src))
4582 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
4583 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
4586 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
4587 9382c10a 2023-02-23 thomas return NULL;
4590 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
4591 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
4592 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
4594 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
4595 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
4596 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4598 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
4599 9382c10a 2023-02-23 thomas --s_nlines;
4600 9382c10a 2023-02-23 thomas ++s_lines;
4604 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4605 be97ab03 2023-01-19 thomas if (p == NULL) {
4606 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
4607 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
4610 be97ab03 2023-01-19 thomas *d_lines = p;
4612 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4613 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
4615 be97ab03 2023-01-19 thomas return NULL;
4618 be97ab03 2023-01-19 thomas static const struct got_error *
4619 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4620 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4621 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4622 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
4624 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4625 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4626 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4627 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4628 45d799e2 2018-12-23 stsp time_t committer_time;
4629 45d799e2 2018-12-23 stsp const char *author, *committer;
4630 8b473291 2019-02-21 stsp char *refs_str = NULL;
4631 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4632 fe621944 2020-11-10 stsp off_t outoff = 0;
4635 8b473291 2019-02-21 stsp if (refs) {
4636 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4638 8b473291 2019-02-21 stsp return err;
4641 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4643 abd2672a 2018-12-23 stsp return err;
4645 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4646 15a94983 2018-12-23 stsp if (err) {
4647 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4648 15a94983 2018-12-23 stsp goto done;
4651 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4653 fe621944 2020-11-10 stsp goto done;
4655 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4656 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4657 fe621944 2020-11-10 stsp if (n < 0) {
4658 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4659 abd2672a 2018-12-23 stsp goto done;
4661 fe621944 2020-11-10 stsp outoff += n;
4662 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4664 fe621944 2020-11-10 stsp goto done;
4666 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4667 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4668 fe621944 2020-11-10 stsp if (n < 0) {
4669 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4670 abd2672a 2018-12-23 stsp goto done;
4672 fe621944 2020-11-10 stsp outoff += n;
4673 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4675 fe621944 2020-11-10 stsp goto done;
4677 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
4678 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
4679 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
4680 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
4681 fe621944 2020-11-10 stsp if (n < 0) {
4682 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4683 fe621944 2020-11-10 stsp goto done;
4685 fe621944 2020-11-10 stsp outoff += n;
4686 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4687 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
4689 fe621944 2020-11-10 stsp goto done;
4691 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
4692 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
4693 48830929 2023-01-07 thomas if (datestr) {
4694 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
4695 fe621944 2020-11-10 stsp if (n < 0) {
4696 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4697 fe621944 2020-11-10 stsp goto done;
4699 fe621944 2020-11-10 stsp outoff += n;
4700 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4701 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
4703 fe621944 2020-11-10 stsp goto done;
4705 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4706 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4707 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4708 ac4dc263 2021-09-24 thomas int pn = 1;
4709 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4710 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4711 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4712 ac4dc263 2021-09-24 thomas if (err)
4713 ac4dc263 2021-09-24 thomas goto done;
4714 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4715 ac4dc263 2021-09-24 thomas if (n < 0) {
4716 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4717 ac4dc263 2021-09-24 thomas goto done;
4719 ac4dc263 2021-09-24 thomas outoff += n;
4720 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4721 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4722 ac4dc263 2021-09-24 thomas if (err)
4723 ac4dc263 2021-09-24 thomas goto done;
4724 ac4dc263 2021-09-24 thomas free(id_str);
4725 ac4dc263 2021-09-24 thomas id_str = NULL;
4729 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4731 5943eee2 2019-08-13 stsp goto done;
4732 fe621944 2020-11-10 stsp s = logmsg;
4733 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4734 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4735 fe621944 2020-11-10 stsp if (n < 0) {
4736 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4737 fe621944 2020-11-10 stsp goto done;
4739 fe621944 2020-11-10 stsp outoff += n;
4740 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4741 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4743 fe621944 2020-11-10 stsp goto done;
4746 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
4747 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4748 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
4750 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4751 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4752 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
4753 fe621944 2020-11-10 stsp if (n < 0) {
4754 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4755 fe621944 2020-11-10 stsp goto done;
4757 fe621944 2020-11-10 stsp outoff += n;
4758 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4759 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4761 fe621944 2020-11-10 stsp goto done;
4764 0208f208 2020-05-05 stsp fputc('\n', outfile);
4766 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4767 772fcad5 2023-01-07 thomas if (err)
4768 772fcad5 2023-01-07 thomas goto done;
4770 772fcad5 2023-01-07 thomas n = fprintf(outfile,
4771 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4772 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4773 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4774 772fcad5 2023-01-07 thomas if (n < 0) {
4775 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
4776 772fcad5 2023-01-07 thomas goto done;
4778 772fcad5 2023-01-07 thomas outoff += n;
4779 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4780 772fcad5 2023-01-07 thomas if (err)
4781 772fcad5 2023-01-07 thomas goto done;
4783 772fcad5 2023-01-07 thomas fputc('\n', outfile);
4784 772fcad5 2023-01-07 thomas outoff++;
4785 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4787 abd2672a 2018-12-23 stsp free(id_str);
4788 5943eee2 2019-08-13 stsp free(logmsg);
4789 8b473291 2019-02-21 stsp free(refs_str);
4790 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4791 7510f233 2020-08-09 stsp if (err) {
4792 82c78e96 2022-08-06 thomas free(*lines);
4793 82c78e96 2022-08-06 thomas *lines = NULL;
4794 ae6a6978 2020-08-09 stsp *nlines = 0;
4796 fe621944 2020-11-10 stsp return err;
4799 abd2672a 2018-12-23 stsp static const struct got_error *
4800 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4802 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4803 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
4804 15a94983 2018-12-23 stsp int obj_type;
4805 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
4806 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
4808 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
4810 82c78e96 2022-08-06 thomas free(s->lines);
4811 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4812 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4813 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4814 fe621944 2020-11-10 stsp s->nlines = 0;
4816 511a516b 2018-05-19 stsp f = got_opentemp();
4817 48ae06ee 2018-10-18 stsp if (f == NULL) {
4818 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4819 48ae06ee 2018-10-18 stsp goto done;
4821 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
4822 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
4823 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
4824 be97ab03 2023-01-19 thomas goto done;
4826 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4827 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4828 fb43ecf1 2019-02-11 stsp goto done;
4832 15a94983 2018-12-23 stsp if (s->id1)
4833 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4835 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4837 15a94983 2018-12-23 stsp goto done;
4839 15a94983 2018-12-23 stsp switch (obj_type) {
4840 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4841 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4842 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4843 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4844 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4847 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4848 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4849 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4850 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4851 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
4853 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4854 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4855 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4856 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4857 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4858 be97ab03 2023-01-19 thomas size_t nlines = 0;
4859 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
4860 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
4861 be97ab03 2023-01-19 thomas &changed_paths,
4862 be97ab03 2023-01-19 thomas s->ignore_whitespace,
4863 be97ab03 2023-01-19 thomas s->force_text_diff,
4864 be97ab03 2023-01-19 thomas tog_diff_algo
4867 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
4868 be97ab03 2023-01-19 thomas if (lines == NULL) {
4869 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
4870 be97ab03 2023-01-19 thomas goto done;
4873 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
4874 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
4875 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4876 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4877 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4878 be97ab03 2023-01-19 thomas if (err)
4881 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4883 3ffacbe1 2020-02-02 tracey goto done;
4884 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4885 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4886 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4887 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4888 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
4889 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
4890 f44b1f58 2020-02-02 tracey if (err)
4891 f44b1f58 2020-02-02 tracey goto done;
4892 f44b1f58 2020-02-02 tracey } else {
4893 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4894 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4895 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4896 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4897 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4898 772fcad5 2023-01-07 thomas s->ignore_whitespace,
4899 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
4900 f44b1f58 2020-02-02 tracey if (err)
4901 f44b1f58 2020-02-02 tracey goto done;
4906 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4908 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4909 be97ab03 2023-01-19 thomas lines, nlines);
4913 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4917 be97ab03 2023-01-19 thomas free(lines);
4918 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4919 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4920 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4921 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4922 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
4923 48ae06ee 2018-10-18 stsp return err;
4926 f5215bb9 2019-02-22 stsp static void
4927 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4929 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4930 f5215bb9 2019-02-22 stsp update_panels();
4931 f5215bb9 2019-02-22 stsp doupdate();
4934 f44b1f58 2020-02-02 tracey static const struct got_error *
4935 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4937 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4939 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4940 f44b1f58 2020-02-02 tracey return NULL;
4943 2a7d3cd7 2022-09-17 thomas static void
4944 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4945 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
4947 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
4949 2a7d3cd7 2022-09-17 thomas *f = s->f;
4950 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
4951 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
4952 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
4953 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
4954 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
4955 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
4958 fc2737d5 2022-09-15 thomas static const struct got_error *
4959 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
4961 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4962 fc2737d5 2022-09-15 thomas FILE *f;
4963 f44b1f58 2020-02-02 tracey int lineno;
4964 78eecffc 2022-06-23 thomas char *line = NULL;
4965 826082fe 2020-12-10 stsp size_t linesize = 0;
4966 826082fe 2020-12-10 stsp ssize_t linelen;
4967 fc2737d5 2022-09-15 thomas off_t *line_offsets;
4968 fc2737d5 2022-09-15 thomas size_t nlines = 0;
4969 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
4971 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
4972 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
4973 2a7d3cd7 2022-09-17 thomas "view search not supported");
4974 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4975 fc2737d5 2022-09-15 thomas &match, &selected);
4977 f44b1f58 2020-02-02 tracey if (!view->searching) {
4978 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4979 f44b1f58 2020-02-02 tracey return NULL;
4982 fc2737d5 2022-09-15 thomas if (*match) {
4983 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4984 fc2737d5 2022-09-15 thomas lineno = *match + 1;
4986 fc2737d5 2022-09-15 thomas lineno = *match - 1;
4988 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
4990 f44b1f58 2020-02-02 tracey while (1) {
4991 f44b1f58 2020-02-02 tracey off_t offset;
4993 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
4994 fc2737d5 2022-09-15 thomas if (*match == 0) {
4995 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4999 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5000 f44b1f58 2020-02-02 tracey lineno = 1;
5002 fc2737d5 2022-09-15 thomas lineno = nlines;
5005 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5006 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5007 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5008 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5009 f44b1f58 2020-02-02 tracey free(line);
5010 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5012 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5013 78eecffc 2022-06-23 thomas if (linelen != -1) {
5014 78eecffc 2022-06-23 thomas char *exstr;
5015 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5016 78eecffc 2022-06-23 thomas if (err)
5018 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5019 78eecffc 2022-06-23 thomas &view->regmatch)) {
5020 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5021 fc2737d5 2022-09-15 thomas *match = lineno;
5022 78eecffc 2022-06-23 thomas free(exstr);
5025 78eecffc 2022-06-23 thomas free(exstr);
5027 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5028 f44b1f58 2020-02-02 tracey lineno++;
5030 f44b1f58 2020-02-02 tracey lineno--;
5032 826082fe 2020-12-10 stsp free(line);
5034 fc2737d5 2022-09-15 thomas if (*match) {
5035 fc2737d5 2022-09-15 thomas *first = *match;
5036 fc2737d5 2022-09-15 thomas *selected = 1;
5039 05171be4 2022-06-23 thomas return err;
5042 48ae06ee 2018-10-18 stsp static const struct got_error *
5043 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5045 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5046 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5048 a0f32f33 2022-06-13 thomas free(s->id1);
5049 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5050 a0f32f33 2022-06-13 thomas free(s->id2);
5051 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5052 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5053 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5054 a0f32f33 2022-06-13 thomas s->f = NULL;
5055 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5056 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5057 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5058 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5059 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5060 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5061 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5062 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5063 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5064 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5065 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5066 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5067 82c78e96 2022-08-06 thomas free(s->lines);
5068 82c78e96 2022-08-06 thomas s->lines = NULL;
5069 a0f32f33 2022-06-13 thomas s->nlines = 0;
5070 a0f32f33 2022-06-13 thomas return err;
5073 a0f32f33 2022-06-13 thomas static const struct got_error *
5074 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5075 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5076 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5077 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5079 48ae06ee 2018-10-18 stsp const struct got_error *err;
5080 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5082 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5083 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5084 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5086 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5087 15a94983 2018-12-23 stsp int type1, type2;
5088 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5090 15a94983 2018-12-23 stsp return err;
5091 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5093 15a94983 2018-12-23 stsp return err;
5095 15a94983 2018-12-23 stsp if (type1 != type2)
5096 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5098 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5099 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5100 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5101 f44b1f58 2020-02-02 tracey s->repo = repo;
5102 f44b1f58 2020-02-02 tracey s->id1 = id1;
5103 f44b1f58 2020-02-02 tracey s->id2 = id2;
5104 3dbaef42 2020-11-24 stsp s->label1 = label1;
5105 3dbaef42 2020-11-24 stsp s->label2 = label2;
5107 15a94983 2018-12-23 stsp if (id1) {
5108 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5109 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5110 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5112 5465d566 2020-02-01 tracey s->id1 = NULL;
5114 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5115 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5116 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5117 a0f32f33 2022-06-13 thomas goto done;
5120 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5121 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5122 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5123 9a1d7435 2022-06-23 thomas goto done;
5126 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5127 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5128 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5129 a0f32f33 2022-06-13 thomas goto done;
5132 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5133 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5134 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5135 19a6a6b5 2022-07-01 thomas goto done;
5138 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5139 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5140 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5141 19a6a6b5 2022-07-01 thomas goto done;
5144 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5145 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5146 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5147 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5148 5465d566 2020-02-01 tracey s->repo = repo;
5150 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5153 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5154 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5155 6568f0aa 2022-08-06 thomas if (rc != ERR)
5156 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5157 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5158 6568f0aa 2022-08-06 thomas if (rc != ERR)
5159 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5160 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5161 6568f0aa 2022-08-06 thomas if (rc != ERR)
5162 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5163 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5164 6568f0aa 2022-08-06 thomas if (rc != ERR)
5165 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5166 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5167 6568f0aa 2022-08-06 thomas if (rc != ERR)
5168 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5169 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5170 6568f0aa 2022-08-06 thomas if (rc != ERR)
5171 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5172 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5173 6568f0aa 2022-08-06 thomas if (rc != ERR)
5174 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5175 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5176 6568f0aa 2022-08-06 thomas if (rc != ERR)
5177 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5178 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5179 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5180 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5181 a0f32f33 2022-06-13 thomas goto done;
5185 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5186 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5187 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5188 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5190 5465d566 2020-02-01 tracey err = create_diff(s);
5192 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5193 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5194 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5195 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5196 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5197 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5198 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5200 a0f32f33 2022-06-13 thomas if (err)
5201 a0f32f33 2022-06-13 thomas close_diff_view(view);
5202 e5a0f69f 2018-08-18 stsp return err;
5205 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5206 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5208 a3404814 2018-09-02 stsp const struct got_error *err;
5209 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5210 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5211 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5213 a3404814 2018-09-02 stsp if (s->id1) {
5214 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5216 a3404814 2018-09-02 stsp return err;
5217 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5219 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5221 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5223 a3404814 2018-09-02 stsp return err;
5224 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5226 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5227 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5228 a3404814 2018-09-02 stsp free(id_str1);
5229 a3404814 2018-09-02 stsp free(id_str2);
5230 a3404814 2018-09-02 stsp return err;
5232 a3404814 2018-09-02 stsp free(id_str1);
5233 a3404814 2018-09-02 stsp free(id_str2);
5235 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5236 267bb3b8 2021-08-01 stsp free(header);
5237 267bb3b8 2021-08-01 stsp return err;
5240 15a087fe 2019-02-21 stsp static const struct got_error *
5241 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5242 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5244 d7a04538 2019-02-21 stsp const struct got_error *err;
5245 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5246 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5247 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5249 15a087fe 2019-02-21 stsp free(s->id2);
5250 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5251 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5252 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5254 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5256 d7a04538 2019-02-21 stsp return err;
5257 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5258 15a087fe 2019-02-21 stsp free(s->id1);
5259 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5260 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5261 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5262 15a087fe 2019-02-21 stsp return NULL;
5265 0cf4efb1 2018-09-29 stsp static const struct got_error *
5266 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5268 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5270 adf4c9e0 2022-07-03 thomas view->count = 0;
5271 adf4c9e0 2022-07-03 thomas wclear(view->window);
5272 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5273 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5274 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5275 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5276 adf4c9e0 2022-07-03 thomas return create_diff(s);
5279 82c78e96 2022-08-06 thomas static void
5280 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5282 82c78e96 2022-08-06 thomas int start, i;
5284 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5286 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5287 82c78e96 2022-08-06 thomas if (i == 0)
5288 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5289 82c78e96 2022-08-06 thomas if (--i == start)
5290 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5293 82c78e96 2022-08-06 thomas s->selected_line = 1;
5294 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5297 82c78e96 2022-08-06 thomas static void
5298 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5300 82c78e96 2022-08-06 thomas int start, i;
5302 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5304 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5305 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5307 82c78e96 2022-08-06 thomas if (++i == start)
5308 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5311 82c78e96 2022-08-06 thomas s->selected_line = 1;
5312 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5315 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5316 4fc71f3b 2022-07-12 thomas int, int, int);
5317 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5318 4fc71f3b 2022-07-12 thomas int, int);
5320 adf4c9e0 2022-07-03 thomas static const struct got_error *
5321 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5323 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5324 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5325 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5326 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5327 826082fe 2020-12-10 stsp char *line = NULL;
5328 826082fe 2020-12-10 stsp size_t linesize = 0;
5329 826082fe 2020-12-10 stsp ssize_t linelen;
5330 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5332 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5334 e5a0f69f 2018-08-18 stsp switch (ch) {
5335 05171be4 2022-06-23 thomas case '0':
5336 05171be4 2022-06-23 thomas case '$':
5337 05171be4 2022-06-23 thomas case KEY_RIGHT:
5338 05171be4 2022-06-23 thomas case 'l':
5339 05171be4 2022-06-23 thomas case KEY_LEFT:
5340 05171be4 2022-06-23 thomas case 'h':
5341 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5345 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5346 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5347 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5348 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5349 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5351 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5352 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5353 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5354 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5355 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5357 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5360 912a3f79 2021-08-30 j case KEY_HOME:
5361 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5362 07b0611c 2022-06-23 thomas view->count = 0;
5365 912a3f79 2021-08-30 j case KEY_END:
5366 07b0611c 2022-06-23 thomas view->count = 0;
5370 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5374 1e37a5c2 2019-05-12 jcs case KEY_UP:
5375 f7140bf5 2021-10-17 thomas case CTRL('p'):
5376 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5377 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5379 07b0611c 2022-06-23 thomas view->count = 0;
5381 70f17a53 2022-06-13 thomas case CTRL('u'):
5382 23427b14 2022-06-23 thomas case 'u':
5383 70f17a53 2022-06-13 thomas nscroll /= 2;
5384 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5385 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5386 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5387 1c5e5faa 2022-06-23 thomas case 'b':
5388 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5389 07b0611c 2022-06-23 thomas view->count = 0;
5393 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5394 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5397 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5398 f7140bf5 2021-10-17 thomas case CTRL('n'):
5399 1e37a5c2 2019-05-12 jcs if (!s->eof)
5400 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5402 07b0611c 2022-06-23 thomas view->count = 0;
5404 70f17a53 2022-06-13 thomas case CTRL('d'):
5405 23427b14 2022-06-23 thomas case 'd':
5406 70f17a53 2022-06-13 thomas nscroll /= 2;
5407 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5408 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5409 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5410 1c5e5faa 2022-06-23 thomas case 'f':
5412 07b0611c 2022-06-23 thomas if (s->eof) {
5413 07b0611c 2022-06-23 thomas view->count = 0;
5417 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5418 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5419 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5420 826082fe 2020-12-10 stsp if (linelen == -1) {
5421 826082fe 2020-12-10 stsp if (feof(s->f)) {
5422 826082fe 2020-12-10 stsp s->eof = 1;
5424 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5428 826082fe 2020-12-10 stsp free(line);
5430 82c78e96 2022-08-06 thomas case '(':
5431 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5433 82c78e96 2022-08-06 thomas case ')':
5434 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5436 82c78e96 2022-08-06 thomas case '{':
5437 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5439 82c78e96 2022-08-06 thomas case '}':
5440 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5443 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5444 1e37a5c2 2019-05-12 jcs s->diff_context--;
5445 aa61903a 2021-12-31 thomas s->matched_line = 0;
5446 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5447 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5448 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5449 27829c9e 2020-11-21 stsp s->nlines) {
5450 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5451 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5454 07b0611c 2022-06-23 thomas view->count = 0;
5457 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5458 1e37a5c2 2019-05-12 jcs s->diff_context++;
5459 aa61903a 2021-12-31 thomas s->matched_line = 0;
5460 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5461 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5463 07b0611c 2022-06-23 thomas view->count = 0;
5467 777aae21 2022-07-20 thomas case 'K':
5469 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5470 4fc71f3b 2022-07-12 thomas case '>':
5471 4fc71f3b 2022-07-12 thomas case '.':
5472 777aae21 2022-07-20 thomas case 'J':
5473 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5474 07b0611c 2022-06-23 thomas view->count = 0;
5477 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5479 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5480 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5481 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5483 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5484 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5485 4fc71f3b 2022-07-12 thomas if (err)
5487 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5489 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5492 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5493 4fc71f3b 2022-07-12 thomas if (err)
5495 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5496 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5497 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5499 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5500 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5501 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5503 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5504 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5505 4fc71f3b 2022-07-12 thomas if (err)
5507 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5509 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5511 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5512 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5513 4fc71f3b 2022-07-12 thomas bs->selected_line);
5514 4fc71f3b 2022-07-12 thomas if (id == NULL)
5517 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5520 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5521 4fc71f3b 2022-07-12 thomas if (err)
5524 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5525 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5526 aa61903a 2021-12-31 thomas s->matched_line = 0;
5527 05171be4 2022-06-23 thomas view->x = 0;
5529 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5530 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5533 07b0611c 2022-06-23 thomas view->count = 0;
5537 bcbd79e2 2018-08-19 stsp return err;
5540 4ed7e80c 2018-05-20 stsp static const struct got_error *
5541 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5543 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5544 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5545 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5546 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5547 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5548 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5549 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5550 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5551 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5552 3dbaef42 2020-11-24 stsp const char *errstr;
5553 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5554 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5556 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5557 26ed57b2 2018-05-19 stsp switch (ch) {
5559 64453f7e 2020-11-21 stsp force_text_diff = 1;
5562 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5564 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5565 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5566 8f666e67 2022-02-12 thomas errstr, errstr);
5568 09b5bff8 2020-02-23 naddy case 'r':
5569 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5570 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5571 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5573 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5576 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5579 17020d27 2019-03-07 stsp usage_diff();
5580 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5584 26ed57b2 2018-05-19 stsp argc -= optind;
5585 26ed57b2 2018-05-19 stsp argv += optind;
5587 26ed57b2 2018-05-19 stsp if (argc == 0) {
5588 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5589 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5590 15a94983 2018-12-23 stsp id_str1 = argv[0];
5591 15a94983 2018-12-23 stsp id_str2 = argv[1];
5593 26ed57b2 2018-05-19 stsp usage_diff();
5595 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5596 7cd52833 2022-06-23 thomas if (error)
5597 7cd52833 2022-06-23 thomas goto done;
5599 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5600 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5601 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5602 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5603 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5604 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5605 c156c7a4 2020-12-18 stsp goto done;
5606 a273ac94 2020-02-23 naddy if (worktree)
5607 a273ac94 2020-02-23 naddy repo_path =
5608 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5610 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5611 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5612 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5613 c156c7a4 2020-12-18 stsp goto done;
5617 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5618 eb6600df 2019-01-04 stsp if (error)
5619 eb6600df 2019-01-04 stsp goto done;
5621 a273ac94 2020-02-23 naddy init_curses();
5623 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5624 51a10b52 2020-12-26 stsp if (error)
5625 51a10b52 2020-12-26 stsp goto done;
5627 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5628 26ed57b2 2018-05-19 stsp if (error)
5629 26ed57b2 2018-05-19 stsp goto done;
5631 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5632 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5633 26ed57b2 2018-05-19 stsp if (error)
5634 26ed57b2 2018-05-19 stsp goto done;
5636 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5637 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5638 26ed57b2 2018-05-19 stsp if (error)
5639 26ed57b2 2018-05-19 stsp goto done;
5641 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5642 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5643 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5644 ea5e7bb5 2018-08-01 stsp goto done;
5646 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5647 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5648 5dc9f4bc 2018-08-04 stsp if (error)
5649 5dc9f4bc 2018-08-04 stsp goto done;
5650 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5652 3dbaef42 2020-11-24 stsp free(label1);
5653 3dbaef42 2020-11-24 stsp free(label2);
5654 c02c541e 2019-03-29 stsp free(repo_path);
5655 a273ac94 2020-02-23 naddy free(cwd);
5656 1d0f4054 2021-06-17 stsp if (repo) {
5657 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5658 1d0f4054 2021-06-17 stsp if (error == NULL)
5659 1d0f4054 2021-06-17 stsp error = close_err;
5661 a273ac94 2020-02-23 naddy if (worktree)
5662 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5663 7cd52833 2022-06-23 thomas if (pack_fds) {
5664 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5665 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5666 7cd52833 2022-06-23 thomas if (error == NULL)
5667 7cd52833 2022-06-23 thomas error = pack_err;
5669 51a10b52 2020-12-26 stsp tog_free_refs();
5670 26ed57b2 2018-05-19 stsp return error;
5673 4ed7e80c 2018-05-20 stsp __dead static void
5674 9f7d7167 2018-04-29 stsp usage_blame(void)
5677 91198554 2022-06-23 thomas fprintf(stderr,
5678 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5679 9f7d7167 2018-04-29 stsp getprogname());
5683 84451b3e 2018-07-10 stsp struct tog_blame_line {
5684 84451b3e 2018-07-10 stsp int annotated;
5685 84451b3e 2018-07-10 stsp struct got_object_id *id;
5688 4ed7e80c 2018-05-20 stsp static const struct got_error *
5689 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5691 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5692 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5693 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5694 84451b3e 2018-07-10 stsp const struct got_error *err;
5695 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5696 826082fe 2020-12-10 stsp char *line = NULL;
5697 826082fe 2020-12-10 stsp size_t linesize = 0;
5698 826082fe 2020-12-10 stsp ssize_t linelen;
5699 84451b3e 2018-07-10 stsp wchar_t *wline;
5700 27a741e5 2019-09-11 stsp int width;
5701 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5702 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5703 ab089a2a 2018-07-12 stsp char *id_str;
5704 11b20872 2019-11-08 stsp struct tog_color *tc;
5706 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5708 ab089a2a 2018-07-12 stsp return err;
5710 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5711 f7d12f7e 2018-08-01 stsp werase(view->window);
5713 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5714 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5715 ab089a2a 2018-07-12 stsp free(id_str);
5716 ab089a2a 2018-07-12 stsp return err;
5719 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5720 ab089a2a 2018-07-12 stsp free(line);
5721 2550e4c3 2018-07-13 stsp line = NULL;
5723 1cae65b4 2019-09-22 stsp return err;
5724 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5725 a3404814 2018-09-02 stsp wstandout(view->window);
5726 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5728 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5729 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5730 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5731 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5733 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5734 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5735 a3404814 2018-09-02 stsp wstandend(view->window);
5736 2550e4c3 2018-07-13 stsp free(wline);
5737 2550e4c3 2018-07-13 stsp wline = NULL;
5739 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5740 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5742 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5743 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5744 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5745 ab089a2a 2018-07-12 stsp free(id_str);
5746 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5748 ab089a2a 2018-07-12 stsp free(id_str);
5749 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5750 3f60a8ef 2018-07-10 stsp free(line);
5751 2550e4c3 2018-07-13 stsp line = NULL;
5753 3f60a8ef 2018-07-10 stsp return err;
5754 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5755 2550e4c3 2018-07-13 stsp free(wline);
5756 2550e4c3 2018-07-13 stsp wline = NULL;
5757 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5758 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5760 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5761 05171be4 2022-06-23 thomas view->maxx = 0;
5762 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5763 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5764 826082fe 2020-12-10 stsp if (linelen == -1) {
5765 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5766 826082fe 2020-12-10 stsp s->eof = 1;
5769 84451b3e 2018-07-10 stsp free(line);
5770 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5772 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5773 07dd3ed3 2022-08-06 thomas continue;
5774 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5777 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5778 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5779 cbea3800 2022-06-23 thomas if (err) {
5780 cbea3800 2022-06-23 thomas free(line);
5781 cbea3800 2022-06-23 thomas return err;
5783 cbea3800 2022-06-23 thomas free(wline);
5784 cbea3800 2022-06-23 thomas wline = NULL;
5785 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5787 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5788 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5790 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5791 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5792 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5793 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5794 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5795 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5796 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5797 8d0fe45a 2019-08-12 stsp char *id_str;
5798 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5799 91198554 2022-06-23 thomas blame_line->id);
5800 8d0fe45a 2019-08-12 stsp if (err) {
5801 8d0fe45a 2019-08-12 stsp free(line);
5802 8d0fe45a 2019-08-12 stsp return err;
5804 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5806 11b20872 2019-11-08 stsp wattr_on(view->window,
5807 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5808 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5810 11b20872 2019-11-08 stsp wattr_off(view->window,
5811 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5812 8d0fe45a 2019-08-12 stsp free(id_str);
5813 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5815 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5816 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5819 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5820 ee41ec32 2018-07-10 stsp prev_id = NULL;
5823 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5824 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5825 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5827 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5828 41605754 2020-11-12 stsp width = 9;
5829 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5830 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5831 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5832 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5833 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5834 41605754 2020-11-12 stsp if (err) {
5835 41605754 2020-11-12 stsp free(line);
5836 41605754 2020-11-12 stsp return err;
5838 41605754 2020-11-12 stsp width += 9;
5840 923086fd 2022-06-23 thomas int skip;
5841 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5842 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5843 923086fd 2022-06-23 thomas if (err) {
5844 923086fd 2022-06-23 thomas free(line);
5845 923086fd 2022-06-23 thomas return err;
5847 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5848 02bce7e2 2022-06-23 thomas width += 9;
5849 41605754 2020-11-12 stsp free(wline);
5850 41605754 2020-11-12 stsp wline = NULL;
5853 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5854 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5855 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5856 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5858 826082fe 2020-12-10 stsp free(line);
5859 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5861 a5d43cac 2022-07-01 thomas view_border(view);
5863 84451b3e 2018-07-10 stsp return NULL;
5866 84451b3e 2018-07-10 stsp static const struct got_error *
5867 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5868 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5870 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5871 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5872 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5873 1a76625f 2018-10-22 stsp int errcode;
5875 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5876 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5877 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5879 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5880 1a76625f 2018-10-22 stsp if (errcode)
5881 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5883 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5884 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5885 d68a0a7d 2018-07-10 stsp goto done;
5888 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5889 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5891 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5892 d68a0a7d 2018-07-10 stsp if (line->annotated)
5893 d68a0a7d 2018-07-10 stsp goto done;
5895 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5896 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5897 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5898 84451b3e 2018-07-10 stsp goto done;
5900 84451b3e 2018-07-10 stsp line->annotated = 1;
5902 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5903 1a76625f 2018-10-22 stsp if (errcode)
5904 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5905 84451b3e 2018-07-10 stsp return err;
5908 84451b3e 2018-07-10 stsp static void *
5909 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5911 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5912 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5913 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5914 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5915 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5917 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5918 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5919 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5921 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5922 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5923 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5924 9117a7b7 2022-07-01 thomas goto done;
5927 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5928 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5929 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5930 9117a7b7 2022-07-01 thomas goto done;
5932 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5933 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5934 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5935 9117a7b7 2022-07-01 thomas goto done;
5938 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5940 9117a7b7 2022-07-01 thomas goto done;
5942 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5943 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5944 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5945 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5946 fc06ba56 2019-08-22 stsp err = NULL;
5948 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5949 9117a7b7 2022-07-01 thomas if (errcode) {
5950 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5951 9117a7b7 2022-07-01 thomas goto done;
5954 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5955 1d0f4054 2021-06-17 stsp if (err == NULL)
5956 1d0f4054 2021-06-17 stsp err = close_err;
5957 c9beca56 2018-07-22 stsp ta->repo = NULL;
5958 c9beca56 2018-07-22 stsp *ta->complete = 1;
5960 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5961 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5962 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5965 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5966 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5967 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5968 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5969 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5970 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5971 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5972 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5974 18430de3 2018-07-10 stsp return (void *)err;
5977 245d91c1 2018-07-12 stsp static struct got_object_id *
5978 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5979 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5981 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5983 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5984 8d0fe45a 2019-08-12 stsp return NULL;
5986 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5987 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5988 4fc71f3b 2022-07-12 thomas return NULL;
5990 4fc71f3b 2022-07-12 thomas return line->id;
5993 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5994 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5995 4fc71f3b 2022-07-12 thomas int lineno)
5997 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5999 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6000 4fc71f3b 2022-07-12 thomas return NULL;
6002 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6003 245d91c1 2018-07-12 stsp if (!line->annotated)
6004 245d91c1 2018-07-12 stsp return NULL;
6006 245d91c1 2018-07-12 stsp return line->id;
6009 b880a918 2018-07-10 stsp static const struct got_error *
6010 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6012 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6015 245d91c1 2018-07-12 stsp if (blame->thread) {
6016 1a76625f 2018-10-22 stsp int errcode;
6017 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6018 1a76625f 2018-10-22 stsp if (errcode)
6019 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6020 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6021 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6022 1a76625f 2018-10-22 stsp if (errcode)
6023 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6024 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6025 1a76625f 2018-10-22 stsp if (errcode)
6026 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6027 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6028 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6029 245d91c1 2018-07-12 stsp err = NULL;
6030 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6032 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6033 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6034 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6035 1d0f4054 2021-06-17 stsp if (err == NULL)
6036 1d0f4054 2021-06-17 stsp err = close_err;
6037 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6039 245d91c1 2018-07-12 stsp if (blame->f) {
6040 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6041 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6042 245d91c1 2018-07-12 stsp blame->f = NULL;
6044 57670559 2018-12-23 stsp if (blame->lines) {
6045 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6046 57670559 2018-12-23 stsp free(blame->lines[i].id);
6047 57670559 2018-12-23 stsp free(blame->lines);
6048 57670559 2018-12-23 stsp blame->lines = NULL;
6050 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6051 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6052 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6053 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6054 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6055 7cd52833 2022-06-23 thomas if (err == NULL)
6056 7cd52833 2022-06-23 thomas err = pack_err;
6057 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6059 245d91c1 2018-07-12 stsp return err;
6062 245d91c1 2018-07-12 stsp static const struct got_error *
6063 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6065 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6066 fc06ba56 2019-08-22 stsp int *done = arg;
6067 fc06ba56 2019-08-22 stsp int errcode;
6069 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6070 fc06ba56 2019-08-22 stsp if (errcode)
6071 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6072 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6074 fc06ba56 2019-08-22 stsp if (*done)
6075 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6077 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6078 fc06ba56 2019-08-22 stsp if (errcode)
6079 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6080 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6082 fc06ba56 2019-08-22 stsp return err;
6085 fc06ba56 2019-08-22 stsp static const struct got_error *
6086 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6088 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6089 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6090 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6091 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6092 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6093 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6094 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6095 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6096 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6098 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6099 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6101 15a94983 2018-12-23 stsp return err;
6103 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6104 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6105 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6106 f4ae6ddb 2022-07-01 thomas goto done;
6109 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6110 945f9229 2022-04-16 thomas if (err)
6111 945f9229 2022-04-16 thomas goto done;
6113 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6115 84451b3e 2018-07-10 stsp goto done;
6117 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6118 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6119 84451b3e 2018-07-10 stsp goto done;
6122 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6124 a70480e0 2018-06-23 stsp goto done;
6125 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6126 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6127 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6128 84451b3e 2018-07-10 stsp goto done;
6130 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6131 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6133 1fddf795 2021-01-20 stsp goto done;
6134 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6135 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6136 84451b3e 2018-07-10 stsp goto done;
6139 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6140 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6141 b02560ec 2019-08-19 stsp blame->nlines--;
6143 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6144 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6145 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6146 84451b3e 2018-07-10 stsp goto done;
6149 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6151 bd24772e 2018-07-11 stsp goto done;
6152 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6153 7cd52833 2022-06-23 thomas pack_fds);
6154 7cd52833 2022-06-23 thomas if (err)
6155 7cd52833 2022-06-23 thomas goto done;
6157 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6158 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6159 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6160 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6161 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6162 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6163 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6164 245d91c1 2018-07-12 stsp goto done;
6166 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6168 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6169 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6170 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6171 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6172 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6173 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6174 a5388363 2020-12-01 naddy s->blame_complete = 0;
6176 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6177 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6178 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6179 f5a09613 2020-12-13 naddy s->selected_line = 1;
6181 aa61903a 2021-12-31 thomas s->matched_line = 0;
6184 945f9229 2022-04-16 thomas if (commit)
6185 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6186 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6187 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6189 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6190 27d434c2 2018-09-15 stsp free(obj_id);
6192 245d91c1 2018-07-12 stsp stop_blame(blame);
6193 245d91c1 2018-07-12 stsp return err;
6196 245d91c1 2018-07-12 stsp static const struct got_error *
6197 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6198 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6200 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6201 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6203 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6205 c4843652 2019-08-12 stsp s->path = strdup(path);
6206 c4843652 2019-08-12 stsp if (s->path == NULL)
6207 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6209 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6210 c4843652 2019-08-12 stsp if (err) {
6211 c4843652 2019-08-12 stsp free(s->path);
6212 7cbe629d 2018-08-04 stsp return err;
6215 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6216 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6217 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6218 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6219 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6220 fb2756b9 2018-08-04 stsp s->repo = repo;
6221 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6222 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6224 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6225 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6226 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6227 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6229 11b20872 2019-11-08 stsp return err;
6232 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6233 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6234 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6235 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6236 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6237 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6238 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6240 a5388363 2020-12-01 naddy return run_blame(view);
6243 e5a0f69f 2018-08-18 stsp static const struct got_error *
6244 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6246 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6247 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6249 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6250 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6252 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6253 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6254 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6255 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6256 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6259 e5a0f69f 2018-08-18 stsp free(s->path);
6260 11b20872 2019-11-08 stsp free_colors(&s->colors);
6261 e5a0f69f 2018-08-18 stsp return err;
6264 7cbe629d 2018-08-04 stsp static const struct got_error *
6265 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6267 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6269 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6270 6c4c42e0 2019-06-24 stsp return NULL;
6273 2a7d3cd7 2022-09-17 thomas static void
6274 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6275 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6277 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6279 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6280 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6281 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6282 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6283 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6284 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6285 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6288 6c4c42e0 2019-06-24 stsp static const struct got_error *
6289 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6291 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6292 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6293 2b380cc8 2018-10-24 stsp int errcode;
6295 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6296 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6297 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6298 2b380cc8 2018-10-24 stsp if (errcode)
6299 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6301 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6304 51fe7530 2019-08-19 stsp if (s->blame_complete)
6305 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6307 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6309 a5d43cac 2022-07-01 thomas view_border(view);
6310 e5a0f69f 2018-08-18 stsp return err;
6313 e5a0f69f 2018-08-18 stsp static const struct got_error *
6314 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6315 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6317 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6318 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6320 eaeaa612 2022-07-20 thomas *new_view = NULL;
6322 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6323 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6324 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6326 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6327 eaeaa612 2022-07-20 thomas if (err)
6328 eaeaa612 2022-07-20 thomas view_close(log_view);
6330 eaeaa612 2022-07-20 thomas *new_view = log_view;
6332 eaeaa612 2022-07-20 thomas return err;
6335 eaeaa612 2022-07-20 thomas static const struct got_error *
6336 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6338 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6339 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6340 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6341 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6343 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6344 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6345 a5d43cac 2022-07-01 thomas --eos; /* border */
6347 e5a0f69f 2018-08-18 stsp switch (ch) {
6348 05171be4 2022-06-23 thomas case '0':
6349 05171be4 2022-06-23 thomas case '$':
6350 05171be4 2022-06-23 thomas case KEY_RIGHT:
6351 05171be4 2022-06-23 thomas case 'l':
6352 05171be4 2022-06-23 thomas case KEY_LEFT:
6353 05171be4 2022-06-23 thomas case 'h':
6354 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6357 1e37a5c2 2019-05-12 jcs s->done = 1;
6359 4deef56f 2021-09-02 naddy case 'g':
6360 4deef56f 2021-09-02 naddy case KEY_HOME:
6361 4deef56f 2021-09-02 naddy s->selected_line = 1;
6362 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6363 07b0611c 2022-06-23 thomas view->count = 0;
6365 4deef56f 2021-09-02 naddy case 'G':
6366 4deef56f 2021-09-02 naddy case KEY_END:
6367 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6368 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6369 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6371 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6372 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6374 07b0611c 2022-06-23 thomas view->count = 0;
6377 1e37a5c2 2019-05-12 jcs case KEY_UP:
6378 f7140bf5 2021-10-17 thomas case CTRL('p'):
6379 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6380 1e37a5c2 2019-05-12 jcs s->selected_line--;
6381 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6382 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6383 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6385 07b0611c 2022-06-23 thomas view->count = 0;
6387 70f17a53 2022-06-13 thomas case CTRL('u'):
6388 23427b14 2022-06-23 thomas case 'u':
6389 70f17a53 2022-06-13 thomas nscroll /= 2;
6390 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6391 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6392 ea025d1d 2020-02-22 naddy case CTRL('b'):
6393 1c5e5faa 2022-06-23 thomas case 'b':
6394 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6395 07b0611c 2022-06-23 thomas if (view->count > 1)
6396 07b0611c 2022-06-23 thomas nscroll += nscroll;
6397 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6398 07b0611c 2022-06-23 thomas view->count = 0;
6401 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6402 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6404 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6407 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6408 f7140bf5 2021-10-17 thomas case CTRL('n'):
6409 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6410 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6411 1e37a5c2 2019-05-12 jcs s->selected_line++;
6412 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6413 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6415 07b0611c 2022-06-23 thomas view->count = 0;
6417 1c5e5faa 2022-06-23 thomas case 'c':
6418 1e37a5c2 2019-05-12 jcs case 'p': {
6419 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6421 07b0611c 2022-06-23 thomas view->count = 0;
6422 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6423 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6424 1e37a5c2 2019-05-12 jcs if (id == NULL)
6426 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6427 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6428 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6429 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6430 1e37a5c2 2019-05-12 jcs int obj_type;
6431 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6432 1e37a5c2 2019-05-12 jcs s->repo, id);
6435 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6436 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6437 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6438 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6441 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6442 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6443 ec242592 2022-04-22 thomas s->repo, &pid->id);
6444 945f9229 2022-04-16 thomas if (err)
6446 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6447 945f9229 2022-04-16 thomas pcommit, s->path);
6448 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6449 e5a0f69f 2018-08-18 stsp if (err) {
6450 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6451 1e37a5c2 2019-05-12 jcs err = NULL;
6452 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6455 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6457 1e37a5c2 2019-05-12 jcs free(blob_id);
6458 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6459 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6460 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6463 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6464 ec242592 2022-04-22 thomas &pid->id);
6465 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6467 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6468 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6470 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6475 1e37a5c2 2019-05-12 jcs s->done = 1;
6476 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6477 1e37a5c2 2019-05-12 jcs s->done = 0;
6478 1e37a5c2 2019-05-12 jcs if (thread_err)
6480 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6481 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6482 a5388363 2020-12-01 naddy err = run_blame(view);
6487 1c5e5faa 2022-06-23 thomas case 'C': {
6488 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6490 07b0611c 2022-06-23 thomas view->count = 0;
6491 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6492 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6494 1e37a5c2 2019-05-12 jcs s->done = 1;
6495 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6496 1e37a5c2 2019-05-12 jcs s->done = 0;
6497 1e37a5c2 2019-05-12 jcs if (thread_err)
6499 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6500 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6501 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6502 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6503 a5388363 2020-12-01 naddy err = run_blame(view);
6508 2a31b33b 2022-07-23 thomas case 'L':
6509 eaeaa612 2022-07-20 thomas view->count = 0;
6510 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6511 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6512 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6513 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6515 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6516 1e37a5c2 2019-05-12 jcs case '\r': {
6517 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6518 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6519 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6521 07b0611c 2022-06-23 thomas view->count = 0;
6522 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6523 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6524 1e37a5c2 2019-05-12 jcs if (id == NULL)
6526 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6529 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6530 4fc71f3b 2022-07-12 thomas if (*new_view) {
6531 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6532 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6533 4fc71f3b 2022-07-12 thomas if (err)
6535 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6536 4fc71f3b 2022-07-12 thomas } else {
6537 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6538 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6540 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6541 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6542 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6543 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6544 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6548 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6549 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6550 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6552 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6555 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6556 4fc71f3b 2022-07-12 thomas s->selected_line;
6557 4fc71f3b 2022-07-12 thomas if (*new_view)
6558 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6559 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6560 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6561 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6562 a5d43cac 2022-07-01 thomas if (err)
6566 e78dc838 2020-12-04 stsp view->focussed = 0;
6567 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6568 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6569 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6570 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6571 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6572 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6575 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6576 40236d76 2022-06-23 thomas if (err)
6578 e78dc838 2020-12-04 stsp view->focus_child = 1;
6580 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6585 70f17a53 2022-06-13 thomas case CTRL('d'):
6586 23427b14 2022-06-23 thomas case 'd':
6587 70f17a53 2022-06-13 thomas nscroll /= 2;
6588 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6589 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6590 ea025d1d 2020-02-22 naddy case CTRL('f'):
6591 1c5e5faa 2022-06-23 thomas case 'f':
6593 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6594 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6595 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6596 07b0611c 2022-06-23 thomas view->count = 0;
6599 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6600 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6601 70f17a53 2022-06-13 thomas s->selected_line +=
6602 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6603 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6605 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6606 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6608 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6609 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6611 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6612 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6613 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6614 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6618 07b0611c 2022-06-23 thomas view->count = 0;
6621 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6624 adf4c9e0 2022-07-03 thomas static const struct got_error *
6625 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6627 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6628 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6630 adf4c9e0 2022-07-03 thomas view->count = 0;
6631 adf4c9e0 2022-07-03 thomas s->done = 1;
6632 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6633 adf4c9e0 2022-07-03 thomas s->done = 0;
6634 adf4c9e0 2022-07-03 thomas if (err)
6635 adf4c9e0 2022-07-03 thomas return err;
6636 adf4c9e0 2022-07-03 thomas return run_blame(view);
6639 a70480e0 2018-06-23 stsp static const struct got_error *
6640 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6642 a70480e0 2018-06-23 stsp const struct got_error *error;
6643 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6644 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6645 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6646 0587e10c 2020-07-23 stsp char *link_target = NULL;
6647 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6648 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6649 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6651 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6652 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6654 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6655 a70480e0 2018-06-23 stsp switch (ch) {
6657 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6660 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6661 69069811 2018-08-02 stsp if (repo_path == NULL)
6662 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6666 17020d27 2019-03-07 stsp usage_blame();
6667 a70480e0 2018-06-23 stsp /* NOTREACHED */
6671 a70480e0 2018-06-23 stsp argc -= optind;
6672 a70480e0 2018-06-23 stsp argv += optind;
6674 f135c941 2020-02-20 stsp if (argc != 1)
6675 a70480e0 2018-06-23 stsp usage_blame();
6677 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6678 7cd52833 2022-06-23 thomas if (error != NULL)
6679 7cd52833 2022-06-23 thomas goto done;
6681 69069811 2018-08-02 stsp if (repo_path == NULL) {
6682 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6683 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6684 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6685 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6686 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6687 c156c7a4 2020-12-18 stsp goto done;
6688 f135c941 2020-02-20 stsp if (worktree)
6689 eb41ed75 2019-02-05 stsp repo_path =
6690 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6692 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6693 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6694 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6695 c156c7a4 2020-12-18 stsp goto done;
6699 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6700 c02c541e 2019-03-29 stsp if (error != NULL)
6701 8e94dd5b 2019-01-04 stsp goto done;
6703 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6704 f135c941 2020-02-20 stsp worktree);
6705 c02c541e 2019-03-29 stsp if (error)
6706 92205607 2019-01-04 stsp goto done;
6708 f135c941 2020-02-20 stsp init_curses();
6710 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6711 51a10b52 2020-12-26 stsp if (error)
6712 51a10b52 2020-12-26 stsp goto done;
6714 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6715 eb41ed75 2019-02-05 stsp if (error)
6716 69069811 2018-08-02 stsp goto done;
6718 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6719 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6720 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6721 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6722 a70480e0 2018-06-23 stsp if (error != NULL)
6723 66b4983c 2018-06-23 stsp goto done;
6724 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6725 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6727 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6728 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6730 a19e88aa 2018-06-23 stsp if (error != NULL)
6731 8b473291 2019-02-21 stsp goto done;
6733 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6734 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6735 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6736 e1cd8fed 2018-08-01 stsp goto done;
6739 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6740 945f9229 2022-04-16 thomas if (error)
6741 945f9229 2022-04-16 thomas goto done;
6743 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6744 945f9229 2022-04-16 thomas commit, repo);
6745 7cbe629d 2018-08-04 stsp if (error)
6746 7cbe629d 2018-08-04 stsp goto done;
6748 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6749 78756c87 2020-11-24 stsp commit_id, repo);
6750 0587e10c 2020-07-23 stsp if (error)
6751 0587e10c 2020-07-23 stsp goto done;
6752 12314ad4 2019-08-31 stsp if (worktree) {
6753 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6754 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6755 12314ad4 2019-08-31 stsp worktree = NULL;
6757 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6759 69069811 2018-08-02 stsp free(repo_path);
6760 f135c941 2020-02-20 stsp free(in_repo_path);
6761 0587e10c 2020-07-23 stsp free(link_target);
6762 69069811 2018-08-02 stsp free(cwd);
6763 a70480e0 2018-06-23 stsp free(commit_id);
6764 945f9229 2022-04-16 thomas if (commit)
6765 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6766 eb41ed75 2019-02-05 stsp if (worktree)
6767 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6768 1d0f4054 2021-06-17 stsp if (repo) {
6769 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6770 1d0f4054 2021-06-17 stsp if (error == NULL)
6771 1d0f4054 2021-06-17 stsp error = close_err;
6773 7cd52833 2022-06-23 thomas if (pack_fds) {
6774 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6775 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6776 7cd52833 2022-06-23 thomas if (error == NULL)
6777 7cd52833 2022-06-23 thomas error = pack_err;
6779 51a10b52 2020-12-26 stsp tog_free_refs();
6780 a70480e0 2018-06-23 stsp return error;
6783 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6784 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6786 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6787 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6788 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6789 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6790 86f4aab9 2022-09-09 thomas char *index = NULL;
6791 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6792 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
6793 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6795 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6796 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6797 a5d43cac 2022-07-01 thomas --limit; /* border */
6799 f7d12f7e 2018-08-01 stsp werase(view->window);
6801 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6802 ffd1d5e5 2018-06-23 stsp return NULL;
6804 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6807 ffd1d5e5 2018-06-23 stsp return err;
6808 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6809 a3404814 2018-09-02 stsp wstandout(view->window);
6810 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6812 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6813 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6814 86f4aab9 2022-09-09 thomas free(wline);
6815 86f4aab9 2022-09-09 thomas wline = NULL;
6816 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6817 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6819 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6820 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6821 a3404814 2018-09-02 stsp wstandend(view->window);
6822 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6823 86f4aab9 2022-09-09 thomas return NULL;
6825 6f5f393a 2022-08-12 thomas i += s->selected;
6826 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6827 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6828 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6829 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6831 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6832 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6833 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6834 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6835 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6836 86f4aab9 2022-09-09 thomas free(index);
6838 ce52c690 2018-06-23 stsp return err;
6839 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6840 2550e4c3 2018-07-13 stsp free(wline);
6841 2550e4c3 2018-07-13 stsp wline = NULL;
6842 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6843 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6844 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6845 ffd1d5e5 2018-06-23 stsp return NULL;
6846 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6847 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6848 a1eca9bb 2018-06-23 stsp return NULL;
6850 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6851 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6852 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6853 0cf4efb1 2018-09-29 stsp if (view->focussed)
6854 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6855 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6857 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6858 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6859 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6860 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6861 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6862 ffd1d5e5 2018-06-23 stsp return NULL;
6866 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6869 c72de8ab 2023-02-03 thomas view->maxx = 0;
6870 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6871 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6872 848d6979 2019-08-12 stsp const char *modestr = "";
6873 56e0773d 2019-11-28 stsp mode_t mode;
6875 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6876 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6878 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6879 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6880 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6882 638f9024 2019-05-13 stsp return got_error_from_errno(
6883 230a42bd 2019-05-11 jcs "got_object_id_str");
6885 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6886 63c5ca5d 2019-08-24 stsp modestr = "$";
6887 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6890 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6891 d86d3b18 2020-12-01 naddy te, s->repo);
6892 0d6c6ee3 2020-05-20 stsp if (err) {
6893 0d6c6ee3 2020-05-20 stsp free(id_str);
6894 0d6c6ee3 2020-05-20 stsp return err;
6896 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6897 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6898 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6900 848d6979 2019-08-12 stsp modestr = "@";
6902 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6903 848d6979 2019-08-12 stsp modestr = "/";
6904 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6905 848d6979 2019-08-12 stsp modestr = "*";
6906 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6907 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6908 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6909 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6910 1d13200f 2018-07-12 stsp free(id_str);
6911 0d6c6ee3 2020-05-20 stsp free(link_target);
6912 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6914 1d13200f 2018-07-12 stsp free(id_str);
6915 0d6c6ee3 2020-05-20 stsp free(link_target);
6917 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
6918 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6919 ffd1d5e5 2018-06-23 stsp if (err) {
6920 ffd1d5e5 2018-06-23 stsp free(line);
6923 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
6924 c72de8ab 2023-02-03 thomas free(wline);
6925 c72de8ab 2023-02-03 thomas wline = NULL;
6927 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
6928 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
6929 c72de8ab 2023-02-03 thomas if (err) {
6930 c72de8ab 2023-02-03 thomas free(line);
6933 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6934 0cf4efb1 2018-09-29 stsp if (view->focussed)
6935 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6936 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6938 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6940 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6941 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6942 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
6944 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6945 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6946 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
6947 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6948 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6949 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6950 ffd1d5e5 2018-06-23 stsp free(line);
6951 2550e4c3 2018-07-13 stsp free(wline);
6952 2550e4c3 2018-07-13 stsp wline = NULL;
6954 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6955 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6956 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6960 ffd1d5e5 2018-06-23 stsp return err;
6963 ffd1d5e5 2018-06-23 stsp static void
6964 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6966 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6967 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6968 fa86c4bf 2020-11-29 stsp int i = 0;
6970 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6973 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6974 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6975 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6976 fa86c4bf 2020-11-29 stsp if (!isroot)
6977 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6980 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6981 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6985 a5d43cac 2022-07-01 thomas static const struct got_error *
6986 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6988 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6989 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6990 ffd1d5e5 2018-06-23 stsp int n = 0;
6992 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6993 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6994 694d3271 2020-12-01 naddy s->first_displayed_entry);
6996 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6998 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6999 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7000 07dd3ed3 2022-08-06 thomas if (last) {
7001 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7002 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7004 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7005 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7006 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7010 a5d43cac 2022-07-01 thomas return NULL;
7013 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7014 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7015 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7017 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7018 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7019 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7021 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7022 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7023 56e0773d 2019-11-28 stsp + 1 /* slash */;
7025 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7027 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7028 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7029 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7031 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7032 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7033 d9765a41 2018-06-23 stsp while (pt) {
7034 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7035 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7036 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7037 cb2ebc8a 2018-06-23 stsp goto done;
7039 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7040 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7041 cb2ebc8a 2018-06-23 stsp goto done;
7043 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7046 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7047 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7048 ce52c690 2018-06-23 stsp goto done;
7052 ce52c690 2018-06-23 stsp if (err) {
7053 ce52c690 2018-06-23 stsp free(*path);
7054 ce52c690 2018-06-23 stsp *path = NULL;
7056 ce52c690 2018-06-23 stsp return err;
7059 ce52c690 2018-06-23 stsp static const struct got_error *
7060 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7061 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7062 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7064 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7065 ce52c690 2018-06-23 stsp char *path;
7066 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7068 a0de39f3 2019-08-09 stsp *new_view = NULL;
7070 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7072 ce52c690 2018-06-23 stsp return err;
7074 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7075 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7076 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7077 83ce39e3 2019-08-12 stsp goto done;
7080 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7081 e5a0f69f 2018-08-18 stsp if (err) {
7082 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7083 fc06ba56 2019-08-22 stsp err = NULL;
7084 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7086 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7088 83ce39e3 2019-08-12 stsp free(path);
7089 69efd4c4 2018-07-18 stsp return err;
7092 69efd4c4 2018-07-18 stsp static const struct got_error *
7093 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7094 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7096 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7097 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7098 69efd4c4 2018-07-18 stsp char *path;
7100 a0de39f3 2019-08-09 stsp *new_view = NULL;
7102 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7103 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7104 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7106 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7108 69efd4c4 2018-07-18 stsp return err;
7110 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7113 e5a0f69f 2018-08-18 stsp view_close(log_view);
7115 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7116 cb2ebc8a 2018-06-23 stsp free(path);
7117 cb2ebc8a 2018-06-23 stsp return err;
7120 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7121 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7122 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7124 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7125 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7126 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7127 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7129 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7130 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7132 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7133 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7134 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7136 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7138 bc573f3b 2021-07-10 stsp goto done;
7141 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7142 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7143 bc573f3b 2021-07-10 stsp * closed on demand.
7145 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7146 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7148 bc573f3b 2021-07-10 stsp goto done;
7149 bc573f3b 2021-07-10 stsp s->tree = s->root;
7151 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7152 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7153 ffd1d5e5 2018-06-23 stsp goto done;
7155 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7156 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7157 ffd1d5e5 2018-06-23 stsp goto done;
7160 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7161 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7162 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7163 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7164 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7165 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7166 9cd7cbd1 2020-12-07 stsp goto done;
7169 fb2756b9 2018-08-04 stsp s->repo = repo;
7171 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7172 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7173 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7174 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7176 c0b01bdb 2019-11-08 stsp goto done;
7177 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7178 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7180 c0b01bdb 2019-11-08 stsp goto done;
7181 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7182 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7183 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7185 c0b01bdb 2019-11-08 stsp goto done;
7187 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7188 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7189 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7191 11b20872 2019-11-08 stsp goto done;
7193 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7194 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7196 c0b01bdb 2019-11-08 stsp goto done;
7199 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7200 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7201 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7202 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7203 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7205 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7206 bc573f3b 2021-07-10 stsp if (commit)
7207 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7209 bc573f3b 2021-07-10 stsp close_tree_view(view);
7210 ad80ab7b 2018-08-04 stsp return err;
7213 e5a0f69f 2018-08-18 stsp static const struct got_error *
7214 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7216 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7218 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7219 fb2756b9 2018-08-04 stsp free(s->tree_label);
7220 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7221 6484ec90 2018-09-29 stsp free(s->commit_id);
7222 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7223 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7224 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7225 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7226 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7227 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7228 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7229 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7230 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7231 ad80ab7b 2018-08-04 stsp free(parent);
7234 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7235 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7236 bc573f3b 2021-07-10 stsp if (s->root)
7237 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7238 7c32bd05 2019-06-22 stsp return NULL;
7241 7c32bd05 2019-06-22 stsp static const struct got_error *
7242 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7244 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7246 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7247 7c32bd05 2019-06-22 stsp return NULL;
7250 7c32bd05 2019-06-22 stsp static int
7251 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7253 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7255 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7259 7c32bd05 2019-06-22 stsp static const struct got_error *
7260 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7262 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7263 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7265 7c32bd05 2019-06-22 stsp if (!view->searching) {
7266 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7267 7c32bd05 2019-06-22 stsp return NULL;
7270 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7271 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7272 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7273 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7274 56e0773d 2019-11-28 stsp s->selected_entry);
7276 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7278 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7279 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7281 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7282 56e0773d 2019-11-28 stsp s->selected_entry);
7285 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7286 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7287 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7288 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7290 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7293 7c32bd05 2019-06-22 stsp while (1) {
7294 56e0773d 2019-11-28 stsp if (te == NULL) {
7295 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7296 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7297 ac66afb8 2019-06-24 stsp return NULL;
7299 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7300 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7302 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7305 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7306 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7307 56e0773d 2019-11-28 stsp s->matched_entry = te;
7311 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7312 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7314 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7317 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7318 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7319 7c32bd05 2019-06-22 stsp s->selected = 0;
7322 e5a0f69f 2018-08-18 stsp return NULL;
7325 ad80ab7b 2018-08-04 stsp static const struct got_error *
7326 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7328 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7329 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7330 e5a0f69f 2018-08-18 stsp char *parent_path;
7332 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7334 e5a0f69f 2018-08-18 stsp return err;
7336 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7337 e5a0f69f 2018-08-18 stsp free(parent_path);
7339 a5d43cac 2022-07-01 thomas view_border(view);
7340 e5a0f69f 2018-08-18 stsp return err;
7343 07dd3ed3 2022-08-06 thomas static const struct got_error *
7344 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7346 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7347 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7348 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7349 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7350 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7351 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7353 07dd3ed3 2022-08-06 thomas g = view->gline;
7354 07dd3ed3 2022-08-06 thomas view->gline = 0;
7356 07dd3ed3 2022-08-06 thomas if (g == 0)
7358 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7359 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7361 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7362 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7363 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7365 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7366 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7367 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7369 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7370 07dd3ed3 2022-08-06 thomas last += off;
7372 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7373 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7374 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7377 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7378 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7379 07dd3ed3 2022-08-06 thomas i += off;
7382 07dd3ed3 2022-08-06 thomas if (i < g) {
7383 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7384 07dd3ed3 2022-08-06 thomas if (err)
7385 07dd3ed3 2022-08-06 thomas return err;
7386 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7387 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7388 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7389 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7390 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7391 07dd3ed3 2022-08-06 thomas first += off;
7392 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7394 07dd3ed3 2022-08-06 thomas } else if (i > g)
7395 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7397 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7398 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7399 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7401 07dd3ed3 2022-08-06 thomas return NULL;
7404 e5a0f69f 2018-08-18 stsp static const struct got_error *
7405 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7407 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7408 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7409 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7410 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7412 07dd3ed3 2022-08-06 thomas if (view->gline)
7413 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7415 e5a0f69f 2018-08-18 stsp switch (ch) {
7416 c72de8ab 2023-02-03 thomas case '0':
7417 c72de8ab 2023-02-03 thomas case '$':
7418 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
7419 c72de8ab 2023-02-03 thomas case 'l':
7420 c72de8ab 2023-02-03 thomas case KEY_LEFT:
7421 c72de8ab 2023-02-03 thomas case 'h':
7422 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
7425 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7426 07b0611c 2022-06-23 thomas view->count = 0;
7428 1be4947a 2022-07-22 thomas case 'L':
7429 07b0611c 2022-06-23 thomas view->count = 0;
7430 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7432 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7434 1be4947a 2022-07-22 thomas case 'R':
7435 07b0611c 2022-06-23 thomas view->count = 0;
7436 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7438 e4526bf5 2021-09-03 naddy case 'g':
7439 aa7a1117 2023-01-09 thomas case '=':
7440 e4526bf5 2021-09-03 naddy case KEY_HOME:
7441 e4526bf5 2021-09-03 naddy s->selected = 0;
7442 07b0611c 2022-06-23 thomas view->count = 0;
7443 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7444 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7445 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7447 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7449 e4526bf5 2021-09-03 naddy case 'G':
7450 aa7a1117 2023-01-09 thomas case '*':
7451 a5d43cac 2022-07-01 thomas case KEY_END: {
7452 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7454 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7455 a5d43cac 2022-07-01 thomas --eos; /* border */
7456 e4526bf5 2021-09-03 naddy s->selected = 0;
7457 07b0611c 2022-06-23 thomas view->count = 0;
7458 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7459 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7460 e4526bf5 2021-09-03 naddy if (te == NULL) {
7461 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7462 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7467 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7468 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7470 e4526bf5 2021-09-03 naddy if (n > 0)
7471 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7475 1e37a5c2 2019-05-12 jcs case KEY_UP:
7476 f7140bf5 2021-10-17 thomas case CTRL('p'):
7477 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7478 1e37a5c2 2019-05-12 jcs s->selected--;
7481 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7482 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7483 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7484 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7485 07b0611c 2022-06-23 thomas view->count = 0;
7487 70f17a53 2022-06-13 thomas case CTRL('u'):
7488 23427b14 2022-06-23 thomas case 'u':
7489 70f17a53 2022-06-13 thomas nscroll /= 2;
7490 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7491 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7492 ea025d1d 2020-02-22 naddy case CTRL('b'):
7493 1c5e5faa 2022-06-23 thomas case 'b':
7494 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7495 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7496 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7497 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7499 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7500 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7502 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7503 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7504 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7505 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7506 07b0611c 2022-06-23 thomas view->count = 0;
7509 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7510 f7140bf5 2021-10-17 thomas case CTRL('n'):
7511 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7512 1e37a5c2 2019-05-12 jcs s->selected++;
7515 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7516 07b0611c 2022-06-23 thomas == NULL) {
7517 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7518 07b0611c 2022-06-23 thomas view->count = 0;
7521 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7523 70f17a53 2022-06-13 thomas case CTRL('d'):
7524 23427b14 2022-06-23 thomas case 'd':
7525 70f17a53 2022-06-13 thomas nscroll /= 2;
7526 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7527 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7528 ea025d1d 2020-02-22 naddy case CTRL('f'):
7529 1c5e5faa 2022-06-23 thomas case 'f':
7530 4c2d69cb 2022-06-23 thomas case ' ':
7531 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7533 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7534 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7535 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7536 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7538 07b0611c 2022-06-23 thomas view->count = 0;
7541 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7543 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7545 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7546 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7547 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7548 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7549 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7550 07b0611c 2022-06-23 thomas view->count = 0;
7553 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7554 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7556 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7557 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7558 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7559 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7560 1e37a5c2 2019-05-12 jcs s->selected_entry =
7561 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7562 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7563 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7564 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7565 a5d43cac 2022-07-01 thomas if (err)
7568 1e37a5c2 2019-05-12 jcs free(parent);
7569 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7570 56e0773d 2019-11-28 stsp s->selected_entry))) {
7571 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7572 07b0611c 2022-06-23 thomas view->count = 0;
7573 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7574 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7577 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7578 941e9f74 2019-05-21 stsp if (err) {
7579 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7582 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7583 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7585 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7586 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7587 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7588 07b0611c 2022-06-23 thomas view->count = 0;
7591 07b0611c 2022-06-23 thomas view->count = 0;
7595 ffd1d5e5 2018-06-23 stsp return err;
7598 ffd1d5e5 2018-06-23 stsp __dead static void
7599 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7602 91198554 2022-06-23 thomas fprintf(stderr,
7603 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7604 ffd1d5e5 2018-06-23 stsp getprogname());
7608 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7609 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7611 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7612 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7613 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7614 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7615 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7616 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7617 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7618 4e97c21c 2020-12-06 stsp char *label = NULL;
7619 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7620 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7622 5221c383 2018-08-01 stsp struct tog_view *view;
7623 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7625 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7626 ffd1d5e5 2018-06-23 stsp switch (ch) {
7628 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7631 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7632 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7633 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7637 e99e2d15 2020-11-24 naddy usage_tree();
7638 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7642 ffd1d5e5 2018-06-23 stsp argc -= optind;
7643 ffd1d5e5 2018-06-23 stsp argv += optind;
7645 55cccc34 2020-02-20 stsp if (argc > 1)
7646 e99e2d15 2020-11-24 naddy usage_tree();
7648 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7649 7cd52833 2022-06-23 thomas if (error != NULL)
7650 7cd52833 2022-06-23 thomas goto done;
7652 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7653 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7654 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7655 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7656 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7657 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7658 c156c7a4 2020-12-18 stsp goto done;
7659 55cccc34 2020-02-20 stsp if (worktree)
7660 52185f70 2019-02-05 stsp repo_path =
7661 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7663 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7664 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7665 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7666 c156c7a4 2020-12-18 stsp goto done;
7670 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7671 c02c541e 2019-03-29 stsp if (error != NULL)
7672 52185f70 2019-02-05 stsp goto done;
7674 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7675 55cccc34 2020-02-20 stsp repo, worktree);
7676 55cccc34 2020-02-20 stsp if (error)
7677 55cccc34 2020-02-20 stsp goto done;
7679 55cccc34 2020-02-20 stsp init_curses();
7681 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7682 c02c541e 2019-03-29 stsp if (error)
7683 52185f70 2019-02-05 stsp goto done;
7685 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7686 51a10b52 2020-12-26 stsp if (error)
7687 51a10b52 2020-12-26 stsp goto done;
7689 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7690 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7691 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7692 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7693 4e97c21c 2020-12-06 stsp if (error)
7694 4e97c21c 2020-12-06 stsp goto done;
7695 4e97c21c 2020-12-06 stsp head_ref_name = label;
7697 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7698 4e97c21c 2020-12-06 stsp if (error == NULL)
7699 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7700 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7701 4e97c21c 2020-12-06 stsp goto done;
7702 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7703 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7704 4e97c21c 2020-12-06 stsp if (error)
7705 4e97c21c 2020-12-06 stsp goto done;
7708 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7709 945f9229 2022-04-16 thomas if (error)
7710 945f9229 2022-04-16 thomas goto done;
7712 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7713 5221c383 2018-08-01 stsp if (view == NULL) {
7714 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7715 5221c383 2018-08-01 stsp goto done;
7717 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7718 ad80ab7b 2018-08-04 stsp if (error)
7719 ad80ab7b 2018-08-04 stsp goto done;
7720 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7721 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7722 d91faf3b 2020-12-01 naddy in_repo_path);
7723 55cccc34 2020-02-20 stsp if (error)
7724 55cccc34 2020-02-20 stsp goto done;
7727 55cccc34 2020-02-20 stsp if (worktree) {
7728 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7729 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7730 55cccc34 2020-02-20 stsp worktree = NULL;
7732 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7734 52185f70 2019-02-05 stsp free(repo_path);
7735 e4a0e26d 2020-02-20 stsp free(cwd);
7736 ffd1d5e5 2018-06-23 stsp free(commit_id);
7737 4e97c21c 2020-12-06 stsp free(label);
7739 486cd271 2020-12-06 stsp got_ref_close(ref);
7740 1d0f4054 2021-06-17 stsp if (repo) {
7741 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7742 1d0f4054 2021-06-17 stsp if (error == NULL)
7743 1d0f4054 2021-06-17 stsp error = close_err;
7745 7cd52833 2022-06-23 thomas if (pack_fds) {
7746 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7747 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7748 7cd52833 2022-06-23 thomas if (error == NULL)
7749 7cd52833 2022-06-23 thomas error = pack_err;
7751 51a10b52 2020-12-26 stsp tog_free_refs();
7752 ffd1d5e5 2018-06-23 stsp return error;
7755 6458efa5 2020-11-24 stsp static const struct got_error *
7756 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7758 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7759 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7761 6458efa5 2020-11-24 stsp s->nrefs = 0;
7762 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7763 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7764 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7765 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7766 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7769 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7770 6458efa5 2020-11-24 stsp if (re == NULL)
7771 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7773 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7774 8924d611 2020-12-26 stsp if (re->ref == NULL)
7775 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7776 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7777 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7780 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7781 6458efa5 2020-11-24 stsp return NULL;
7784 ef20f542 2022-06-26 thomas static void
7785 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7787 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7789 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7790 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7791 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7792 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7797 6458efa5 2020-11-24 stsp static const struct got_error *
7798 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7800 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7801 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7803 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7804 6458efa5 2020-11-24 stsp s->repo = repo;
7806 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7807 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7809 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7811 6458efa5 2020-11-24 stsp return err;
7813 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7814 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7815 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7816 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7818 6458efa5 2020-11-24 stsp goto done;
7820 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7821 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7822 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7824 6458efa5 2020-11-24 stsp goto done;
7826 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7827 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7828 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7829 2183bbf6 2022-01-23 thomas if (err)
7830 2183bbf6 2022-01-23 thomas goto done;
7832 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7833 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7834 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7836 6458efa5 2020-11-24 stsp goto done;
7839 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7840 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7841 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7842 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7843 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7846 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7847 6458efa5 2020-11-24 stsp return err;
7850 6458efa5 2020-11-24 stsp static const struct got_error *
7851 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7853 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7855 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7856 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7858 6458efa5 2020-11-24 stsp return NULL;
7861 6458efa5 2020-11-24 stsp static const struct got_error *
7862 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7863 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7865 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7866 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7867 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7868 6458efa5 2020-11-24 stsp int obj_type;
7870 c42c9805 2020-11-24 stsp *commit_id = NULL;
7872 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7874 6458efa5 2020-11-24 stsp return err;
7876 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7878 6458efa5 2020-11-24 stsp goto done;
7880 6458efa5 2020-11-24 stsp switch (obj_type) {
7881 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7882 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7884 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7885 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7887 6458efa5 2020-11-24 stsp goto done;
7888 c42c9805 2020-11-24 stsp free(obj_id);
7889 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7890 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7892 6458efa5 2020-11-24 stsp goto done;
7893 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7894 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7895 c42c9805 2020-11-24 stsp goto done;
7897 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7898 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7899 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7900 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7901 c42c9805 2020-11-24 stsp goto done;
7905 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7911 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7912 c42c9805 2020-11-24 stsp if (err) {
7913 c42c9805 2020-11-24 stsp free(*commit_id);
7914 c42c9805 2020-11-24 stsp *commit_id = NULL;
7916 c42c9805 2020-11-24 stsp return err;
7919 c42c9805 2020-11-24 stsp static const struct got_error *
7920 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7921 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7923 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7924 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7925 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7927 c42c9805 2020-11-24 stsp *new_view = NULL;
7929 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7930 c42c9805 2020-11-24 stsp if (err) {
7931 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7932 c42c9805 2020-11-24 stsp return err;
7934 c42c9805 2020-11-24 stsp return NULL;
7937 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7938 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7939 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7940 6458efa5 2020-11-24 stsp goto done;
7943 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7944 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7947 6458efa5 2020-11-24 stsp view_close(log_view);
7949 6458efa5 2020-11-24 stsp *new_view = log_view;
7950 c42c9805 2020-11-24 stsp free(commit_id);
7951 6458efa5 2020-11-24 stsp return err;
7954 ce5b7c56 2019-07-09 stsp static void
7955 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7957 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7958 34ba6917 2020-11-30 stsp int i = 0;
7960 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7963 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7964 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7965 34ba6917 2020-11-30 stsp if (re == NULL)
7967 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7968 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7972 a5d43cac 2022-07-01 thomas static const struct got_error *
7973 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7975 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7976 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7977 6458efa5 2020-11-24 stsp int n = 0;
7979 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7980 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7982 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7984 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7985 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7986 07dd3ed3 2022-08-06 thomas if (last) {
7987 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7988 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7990 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7991 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7992 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7996 a5d43cac 2022-07-01 thomas return NULL;
7999 6458efa5 2020-11-24 stsp static const struct got_error *
8000 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8002 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8004 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8005 6458efa5 2020-11-24 stsp return NULL;
8008 6458efa5 2020-11-24 stsp static int
8009 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8011 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8013 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8017 6458efa5 2020-11-24 stsp static const struct got_error *
8018 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8020 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8021 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8023 6458efa5 2020-11-24 stsp if (!view->searching) {
8024 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8025 6458efa5 2020-11-24 stsp return NULL;
8028 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8029 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8030 6458efa5 2020-11-24 stsp if (s->selected_entry)
8031 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8033 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8034 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8036 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8037 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8039 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8040 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8043 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8044 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8045 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8046 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8048 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8051 6458efa5 2020-11-24 stsp while (1) {
8052 6458efa5 2020-11-24 stsp if (re == NULL) {
8053 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8054 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8055 6458efa5 2020-11-24 stsp return NULL;
8057 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8058 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8060 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8063 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8064 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8065 6458efa5 2020-11-24 stsp s->matched_entry = re;
8069 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8070 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8072 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8075 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8076 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8077 6458efa5 2020-11-24 stsp s->selected = 0;
8080 6458efa5 2020-11-24 stsp return NULL;
8083 6458efa5 2020-11-24 stsp static const struct got_error *
8084 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8086 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8087 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8088 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8089 6458efa5 2020-11-24 stsp char *line = NULL;
8090 6458efa5 2020-11-24 stsp wchar_t *wline;
8091 6458efa5 2020-11-24 stsp struct tog_color *tc;
8092 66a70b97 2023-02-03 thomas int width, n, scrollx;
8093 6458efa5 2020-11-24 stsp int limit = view->nlines;
8095 6458efa5 2020-11-24 stsp werase(view->window);
8097 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8098 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8099 a5d43cac 2022-07-01 thomas --limit; /* border */
8101 6458efa5 2020-11-24 stsp if (limit == 0)
8102 6458efa5 2020-11-24 stsp return NULL;
8104 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8106 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8107 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8108 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8110 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8111 6458efa5 2020-11-24 stsp if (err) {
8112 6458efa5 2020-11-24 stsp free(line);
8113 6458efa5 2020-11-24 stsp return err;
8115 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8116 6458efa5 2020-11-24 stsp wstandout(view->window);
8117 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8118 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8119 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8120 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8121 6458efa5 2020-11-24 stsp wstandend(view->window);
8122 6458efa5 2020-11-24 stsp free(wline);
8123 6458efa5 2020-11-24 stsp wline = NULL;
8124 6458efa5 2020-11-24 stsp free(line);
8125 6458efa5 2020-11-24 stsp line = NULL;
8126 6458efa5 2020-11-24 stsp if (--limit <= 0)
8127 6458efa5 2020-11-24 stsp return NULL;
8130 66a70b97 2023-02-03 thomas view->maxx = 0;
8131 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8132 6458efa5 2020-11-24 stsp char *line = NULL;
8133 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8135 84227eb1 2022-06-23 thomas if (s->show_date) {
8136 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8137 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8138 84227eb1 2022-06-23 thomas struct got_object_id *id;
8139 84227eb1 2022-06-23 thomas struct tm tm;
8140 84227eb1 2022-06-23 thomas time_t t;
8142 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8143 84227eb1 2022-06-23 thomas if (err)
8144 84227eb1 2022-06-23 thomas return err;
8145 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8146 84227eb1 2022-06-23 thomas if (err) {
8147 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8148 84227eb1 2022-06-23 thomas free(id);
8149 84227eb1 2022-06-23 thomas return err;
8151 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8153 84227eb1 2022-06-23 thomas if (err) {
8154 84227eb1 2022-06-23 thomas free(id);
8155 84227eb1 2022-06-23 thomas return err;
8157 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8158 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8159 84227eb1 2022-06-23 thomas } else {
8160 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8161 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8163 84227eb1 2022-06-23 thomas free(id);
8164 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8165 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8166 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8167 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8169 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8170 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8171 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8172 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8173 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8174 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8175 6458efa5 2020-11-24 stsp struct got_object_id *id;
8176 6458efa5 2020-11-24 stsp char *id_str;
8177 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8179 6458efa5 2020-11-24 stsp return err;
8180 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8181 6458efa5 2020-11-24 stsp if (err) {
8183 6458efa5 2020-11-24 stsp return err;
8185 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8186 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8187 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8189 6458efa5 2020-11-24 stsp free(id_str);
8190 6458efa5 2020-11-24 stsp return err;
8193 6458efa5 2020-11-24 stsp free(id_str);
8194 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8195 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8196 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8198 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8199 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8200 6458efa5 2020-11-24 stsp if (err) {
8201 6458efa5 2020-11-24 stsp free(line);
8202 6458efa5 2020-11-24 stsp return err;
8204 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8205 66a70b97 2023-02-03 thomas free(wline);
8206 66a70b97 2023-02-03 thomas wline = NULL;
8208 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8209 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8210 66a70b97 2023-02-03 thomas if (err) {
8211 66a70b97 2023-02-03 thomas free(line);
8212 66a70b97 2023-02-03 thomas return err;
8214 6458efa5 2020-11-24 stsp if (n == s->selected) {
8215 6458efa5 2020-11-24 stsp if (view->focussed)
8216 6458efa5 2020-11-24 stsp wstandout(view->window);
8217 6458efa5 2020-11-24 stsp s->selected_entry = re;
8219 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8221 6458efa5 2020-11-24 stsp wattr_on(view->window,
8222 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8223 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8225 6458efa5 2020-11-24 stsp wattr_off(view->window,
8226 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8227 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8228 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8229 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8230 6458efa5 2020-11-24 stsp wstandend(view->window);
8231 6458efa5 2020-11-24 stsp free(line);
8232 6458efa5 2020-11-24 stsp free(wline);
8233 6458efa5 2020-11-24 stsp wline = NULL;
8235 6458efa5 2020-11-24 stsp s->ndisplayed++;
8236 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8239 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8242 a5d43cac 2022-07-01 thomas view_border(view);
8243 6458efa5 2020-11-24 stsp return err;
8246 6458efa5 2020-11-24 stsp static const struct got_error *
8247 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8248 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8250 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8251 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8252 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8254 c42c9805 2020-11-24 stsp *new_view = NULL;
8256 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8257 c42c9805 2020-11-24 stsp if (err) {
8258 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8259 c42c9805 2020-11-24 stsp return err;
8261 c42c9805 2020-11-24 stsp return NULL;
8265 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8266 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8267 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8268 c42c9805 2020-11-24 stsp goto done;
8271 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8272 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8274 c42c9805 2020-11-24 stsp goto done;
8276 c42c9805 2020-11-24 stsp *new_view = tree_view;
8278 c42c9805 2020-11-24 stsp free(commit_id);
8279 c42c9805 2020-11-24 stsp return err;
8282 07dd3ed3 2022-08-06 thomas static const struct got_error *
8283 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8285 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8286 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8287 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8289 07dd3ed3 2022-08-06 thomas g = view->gline;
8290 07dd3ed3 2022-08-06 thomas view->gline = 0;
8292 07dd3ed3 2022-08-06 thomas if (g == 0)
8294 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8295 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8297 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8298 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8299 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8300 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8301 07dd3ed3 2022-08-06 thomas return NULL;
8304 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8305 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8306 07dd3ed3 2022-08-06 thomas if (err)
8307 07dd3ed3 2022-08-06 thomas return err;
8308 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8309 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8310 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8311 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8312 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8313 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8315 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8316 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8318 07dd3ed3 2022-08-06 thomas return NULL;
8322 c42c9805 2020-11-24 stsp static const struct got_error *
8323 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8325 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8326 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8327 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8328 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8330 07dd3ed3 2022-08-06 thomas if (view->gline)
8331 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8333 6458efa5 2020-11-24 stsp switch (ch) {
8334 66a70b97 2023-02-03 thomas case '0':
8335 66a70b97 2023-02-03 thomas case '$':
8336 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8337 66a70b97 2023-02-03 thomas case 'l':
8338 66a70b97 2023-02-03 thomas case KEY_LEFT:
8339 66a70b97 2023-02-03 thomas case 'h':
8340 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8343 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8344 07b0611c 2022-06-23 thomas view->count = 0;
8346 84227eb1 2022-06-23 thomas case 'm':
8347 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8348 07b0611c 2022-06-23 thomas view->count = 0;
8350 98182bd0 2021-11-20 thomas case 'o':
8351 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8352 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8353 07b0611c 2022-06-23 thomas view->count = 0;
8354 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8355 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8356 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8357 c591440f 2021-11-20 thomas if (err)
8359 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8360 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8361 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8362 3bfadbd4 2021-11-20 thomas if (err)
8364 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8365 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8367 6458efa5 2020-11-24 stsp case KEY_ENTER:
8368 6458efa5 2020-11-24 stsp case '\r':
8369 07b0611c 2022-06-23 thomas view->count = 0;
8370 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8372 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8374 1be4947a 2022-07-22 thomas case 'T':
8375 07b0611c 2022-06-23 thomas view->count = 0;
8376 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8378 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8380 e4526bf5 2021-09-03 naddy case 'g':
8381 aa7a1117 2023-01-09 thomas case '=':
8382 e4526bf5 2021-09-03 naddy case KEY_HOME:
8383 e4526bf5 2021-09-03 naddy s->selected = 0;
8384 07b0611c 2022-06-23 thomas view->count = 0;
8385 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8387 e4526bf5 2021-09-03 naddy case 'G':
8388 aa7a1117 2023-01-09 thomas case '*':
8389 a5d43cac 2022-07-01 thomas case KEY_END: {
8390 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8392 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8393 a5d43cac 2022-07-01 thomas --eos; /* border */
8394 e4526bf5 2021-09-03 naddy s->selected = 0;
8395 07b0611c 2022-06-23 thomas view->count = 0;
8396 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8397 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8398 e4526bf5 2021-09-03 naddy if (re == NULL)
8400 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8401 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8403 e4526bf5 2021-09-03 naddy if (n > 0)
8404 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8408 6458efa5 2020-11-24 stsp case KEY_UP:
8409 f7140bf5 2021-10-17 thomas case CTRL('p'):
8410 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8411 6458efa5 2020-11-24 stsp s->selected--;
8414 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8415 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8416 07b0611c 2022-06-23 thomas view->count = 0;
8418 70f17a53 2022-06-13 thomas case CTRL('u'):
8419 23427b14 2022-06-23 thomas case 'u':
8420 70f17a53 2022-06-13 thomas nscroll /= 2;
8421 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8422 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8423 6458efa5 2020-11-24 stsp case CTRL('b'):
8424 1c5e5faa 2022-06-23 thomas case 'b':
8425 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8426 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8427 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8428 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8429 07b0611c 2022-06-23 thomas view->count = 0;
8432 6458efa5 2020-11-24 stsp case KEY_DOWN:
8433 f7140bf5 2021-10-17 thomas case CTRL('n'):
8434 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8435 6458efa5 2020-11-24 stsp s->selected++;
8438 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8439 6458efa5 2020-11-24 stsp /* can't scroll any further */
8440 07b0611c 2022-06-23 thomas view->count = 0;
8443 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8445 70f17a53 2022-06-13 thomas case CTRL('d'):
8446 23427b14 2022-06-23 thomas case 'd':
8447 70f17a53 2022-06-13 thomas nscroll /= 2;
8448 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8449 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8450 6458efa5 2020-11-24 stsp case CTRL('f'):
8451 1c5e5faa 2022-06-23 thomas case 'f':
8452 4c2d69cb 2022-06-23 thomas case ' ':
8453 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8454 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8455 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8456 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8457 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8458 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8459 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8460 07b0611c 2022-06-23 thomas view->count = 0;
8463 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8465 6458efa5 2020-11-24 stsp case CTRL('l'):
8466 07b0611c 2022-06-23 thomas view->count = 0;
8467 8924d611 2020-12-26 stsp tog_free_refs();
8468 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8471 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8472 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8474 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8475 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8476 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8479 07b0611c 2022-06-23 thomas view->count = 0;
8483 6458efa5 2020-11-24 stsp return err;
8486 6458efa5 2020-11-24 stsp __dead static void
8487 6458efa5 2020-11-24 stsp usage_ref(void)
8490 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8491 6458efa5 2020-11-24 stsp getprogname());
8495 6458efa5 2020-11-24 stsp static const struct got_error *
8496 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8498 6458efa5 2020-11-24 stsp const struct got_error *error;
8499 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8500 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8501 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8503 6458efa5 2020-11-24 stsp struct tog_view *view;
8504 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8506 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8507 6458efa5 2020-11-24 stsp switch (ch) {
8509 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8510 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8511 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8515 e99e2d15 2020-11-24 naddy usage_ref();
8516 6458efa5 2020-11-24 stsp /* NOTREACHED */
8520 6458efa5 2020-11-24 stsp argc -= optind;
8521 6458efa5 2020-11-24 stsp argv += optind;
8523 6458efa5 2020-11-24 stsp if (argc > 1)
8524 e99e2d15 2020-11-24 naddy usage_ref();
8526 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8527 7cd52833 2022-06-23 thomas if (error != NULL)
8528 7cd52833 2022-06-23 thomas goto done;
8530 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8531 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8532 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8533 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8534 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8535 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8536 c156c7a4 2020-12-18 stsp goto done;
8537 6458efa5 2020-11-24 stsp if (worktree)
8538 6458efa5 2020-11-24 stsp repo_path =
8539 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8541 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8542 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8543 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8544 c156c7a4 2020-12-18 stsp goto done;
8548 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8549 6458efa5 2020-11-24 stsp if (error != NULL)
8550 6458efa5 2020-11-24 stsp goto done;
8552 6458efa5 2020-11-24 stsp init_curses();
8554 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8555 51a10b52 2020-12-26 stsp if (error)
8556 51a10b52 2020-12-26 stsp goto done;
8558 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8559 6458efa5 2020-11-24 stsp if (error)
8560 6458efa5 2020-11-24 stsp goto done;
8562 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8563 6458efa5 2020-11-24 stsp if (view == NULL) {
8564 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8565 6458efa5 2020-11-24 stsp goto done;
8568 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8569 6458efa5 2020-11-24 stsp if (error)
8570 6458efa5 2020-11-24 stsp goto done;
8572 6458efa5 2020-11-24 stsp if (worktree) {
8573 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8574 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8575 6458efa5 2020-11-24 stsp worktree = NULL;
8577 6458efa5 2020-11-24 stsp error = view_loop(view);
8579 6458efa5 2020-11-24 stsp free(repo_path);
8580 6458efa5 2020-11-24 stsp free(cwd);
8581 1d0f4054 2021-06-17 stsp if (repo) {
8582 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8583 1d0f4054 2021-06-17 stsp if (close_err)
8584 1d0f4054 2021-06-17 stsp error = close_err;
8586 7cd52833 2022-06-23 thomas if (pack_fds) {
8587 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8588 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8589 7cd52833 2022-06-23 thomas if (error == NULL)
8590 7cd52833 2022-06-23 thomas error = pack_err;
8592 51a10b52 2020-12-26 stsp tog_free_refs();
8593 6458efa5 2020-11-24 stsp return error;
8596 fc2737d5 2022-09-15 thomas static const struct got_error*
8597 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8598 fc2737d5 2022-09-15 thomas const char *str)
8600 fc2737d5 2022-09-15 thomas size_t len;
8602 fc2737d5 2022-09-15 thomas if (win == NULL)
8603 fc2737d5 2022-09-15 thomas win = stdscr;
8605 fc2737d5 2022-09-15 thomas len = strlen(str);
8606 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8608 fc2737d5 2022-09-15 thomas if (focus)
8609 fc2737d5 2022-09-15 thomas wstandout(win);
8610 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8611 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8612 fc2737d5 2022-09-15 thomas if (focus)
8613 fc2737d5 2022-09-15 thomas wstandend(win);
8615 fc2737d5 2022-09-15 thomas return NULL;
8618 2a31b33b 2022-07-23 thomas static const struct got_error *
8619 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8621 fc2737d5 2022-09-15 thomas off_t *p;
8623 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8624 fc2737d5 2022-09-15 thomas if (p == NULL) {
8625 fc2737d5 2022-09-15 thomas free(*line_offsets);
8626 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8627 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8630 fc2737d5 2022-09-15 thomas *line_offsets = p;
8631 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8632 fc2737d5 2022-09-15 thomas ++(*nlines);
8633 fc2737d5 2022-09-15 thomas return NULL;
8636 fc2737d5 2022-09-15 thomas static const struct got_error *
8637 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8639 fc2737d5 2022-09-15 thomas *ret = 0;
8641 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8642 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8643 fc2737d5 2022-09-15 thomas size_t len = 1;
8645 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8646 fc2737d5 2022-09-15 thomas continue;
8648 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8649 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8650 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8652 fc2737d5 2022-09-15 thomas len += strlen(t);
8653 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8654 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8655 fc2737d5 2022-09-15 thomas free(t0);
8656 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8659 fc2737d5 2022-09-15 thomas return NULL;
8663 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8664 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8665 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8667 fc2737d5 2022-09-15 thomas static const struct got_error *
8668 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8670 fc2737d5 2022-09-15 thomas int n, len = width;
8672 fc2737d5 2022-09-15 thomas if (km->keys) {
8673 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8674 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8675 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8677 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8678 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8680 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8682 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8683 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8684 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8686 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8687 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8688 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8689 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8690 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8691 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8692 fc2737d5 2022-09-15 thomas if (n < 0) {
8693 fc2737d5 2022-09-15 thomas free(t0);
8694 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8696 fc2737d5 2022-09-15 thomas first = 0;
8697 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8698 fc2737d5 2022-09-15 thomas *off += n;
8700 fc2737d5 2022-09-15 thomas free(t0);
8702 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8703 fc2737d5 2022-09-15 thomas if (n < 0)
8704 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8705 fc2737d5 2022-09-15 thomas *off += n;
8707 fc2737d5 2022-09-15 thomas return NULL;
8710 fc2737d5 2022-09-15 thomas static const struct got_error *
8711 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
8713 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8714 fc2737d5 2022-09-15 thomas off_t off = 0;
8715 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
8716 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
8717 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
8718 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8719 fc2737d5 2022-09-15 thomas GENERATE_HELP
8720 fc2737d5 2022-09-15 thomas #undef KEYMAP_
8721 fc2737d5 2022-09-15 thomas #undef KEY_
8724 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8725 fc2737d5 2022-09-15 thomas if (err)
8726 fc2737d5 2022-09-15 thomas return err;
8728 fc2737d5 2022-09-15 thomas n = nitems(km);
8729 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
8730 fc2737d5 2022-09-15 thomas if (err)
8731 fc2737d5 2022-09-15 thomas return err;
8733 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
8734 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
8735 fc2737d5 2022-09-15 thomas show = s->all;
8736 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
8737 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
8738 fc2737d5 2022-09-15 thomas show = 1;
8740 fc2737d5 2022-09-15 thomas if (show) {
8741 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
8742 fc2737d5 2022-09-15 thomas if (err)
8743 fc2737d5 2022-09-15 thomas return err;
8744 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8745 fc2737d5 2022-09-15 thomas if (err)
8746 fc2737d5 2022-09-15 thomas return err;
8749 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
8751 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8752 fc2737d5 2022-09-15 thomas return err;
8755 fc2737d5 2022-09-15 thomas static const struct got_error *
8756 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
8758 fc2737d5 2022-09-15 thomas FILE *f;
8759 fc2737d5 2022-09-15 thomas const struct got_error *err;
8761 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8762 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8763 fc2737d5 2022-09-15 thomas s->nlines = 0;
8765 fc2737d5 2022-09-15 thomas f = got_opentemp();
8766 fc2737d5 2022-09-15 thomas if (f == NULL)
8767 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
8768 fc2737d5 2022-09-15 thomas s->f = f;
8770 fc2737d5 2022-09-15 thomas err = format_help(s);
8771 fc2737d5 2022-09-15 thomas if (err)
8772 fc2737d5 2022-09-15 thomas return err;
8774 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
8775 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
8777 fc2737d5 2022-09-15 thomas return NULL;
8780 fc2737d5 2022-09-15 thomas static const struct got_error *
8781 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
8783 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
8784 fc2737d5 2022-09-15 thomas return NULL;
8787 2a7d3cd7 2022-09-17 thomas static void
8788 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8789 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
8791 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
8793 2a7d3cd7 2022-09-17 thomas *f = s->f;
8794 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
8795 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
8796 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
8797 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
8798 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
8799 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
8802 fc2737d5 2022-09-15 thomas static const struct got_error *
8803 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
8805 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8806 fc2737d5 2022-09-15 thomas const struct got_error *err;
8807 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
8808 fc2737d5 2022-09-15 thomas wchar_t *wline;
8809 fc2737d5 2022-09-15 thomas char *line;
8810 fc2737d5 2022-09-15 thomas ssize_t linelen;
8811 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8812 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
8813 fc2737d5 2022-09-15 thomas int eos = view->nlines;
8815 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8816 fc2737d5 2022-09-15 thomas --eos; /* account for border */
8818 fc2737d5 2022-09-15 thomas s->lineno = 0;
8819 fc2737d5 2022-09-15 thomas rewind(s->f);
8820 fc2737d5 2022-09-15 thomas werase(view->window);
8822 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
8823 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
8825 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
8826 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
8827 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
8828 fc2737d5 2022-09-15 thomas if (err)
8829 fc2737d5 2022-09-15 thomas return err;
8830 fc2737d5 2022-09-15 thomas if (eos <= 1)
8831 fc2737d5 2022-09-15 thomas return NULL;
8832 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
8833 fc2737d5 2022-09-15 thomas eos -= 2;
8835 fc2737d5 2022-09-15 thomas s->eof = 0;
8836 fc2737d5 2022-09-15 thomas view->maxx = 0;
8837 fc2737d5 2022-09-15 thomas line = NULL;
8838 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
8839 fc2737d5 2022-09-15 thomas attr_t attr = 0;
8841 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8842 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8843 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
8844 fc2737d5 2022-09-15 thomas free(line);
8845 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
8847 fc2737d5 2022-09-15 thomas s->eof = 1;
8850 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
8851 fc2737d5 2022-09-15 thomas continue;
8852 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8853 fc2737d5 2022-09-15 thomas continue;
8854 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
8855 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
8857 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8858 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
8859 fc2737d5 2022-09-15 thomas if (err) {
8860 fc2737d5 2022-09-15 thomas free(line);
8861 fc2737d5 2022-09-15 thomas return err;
8863 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
8864 fc2737d5 2022-09-15 thomas free(wline);
8865 fc2737d5 2022-09-15 thomas wline = NULL;
8867 fc2737d5 2022-09-15 thomas if (attr)
8868 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
8869 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
8870 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8871 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
8872 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
8873 fc2737d5 2022-09-15 thomas if (err) {
8874 fc2737d5 2022-09-15 thomas free(line);
8875 fc2737d5 2022-09-15 thomas return err;
8877 fc2737d5 2022-09-15 thomas } else {
8878 fc2737d5 2022-09-15 thomas int skip;
8880 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
8881 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
8882 fc2737d5 2022-09-15 thomas if (err) {
8883 fc2737d5 2022-09-15 thomas free(line);
8884 fc2737d5 2022-09-15 thomas return err;
8886 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
8887 fc2737d5 2022-09-15 thomas free(wline);
8888 fc2737d5 2022-09-15 thomas wline = NULL;
8890 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
8891 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
8892 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
8893 fc2737d5 2022-09-15 thomas } else {
8894 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8895 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
8897 fc2737d5 2022-09-15 thomas if (attr)
8898 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
8899 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
8900 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
8902 fc2737d5 2022-09-15 thomas free(line);
8903 fc2737d5 2022-09-15 thomas if (nprinted > 0)
8904 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8906 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
8908 fc2737d5 2022-09-15 thomas view_border(view);
8910 fc2737d5 2022-09-15 thomas if (s->eof) {
8911 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
8912 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
8913 fc2737d5 2022-09-15 thomas view->ncols - 1);
8914 fc2737d5 2022-09-15 thomas if (rc == ERR)
8915 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8916 fc2737d5 2022-09-15 thomas } else {
8917 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
8918 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
8919 fc2737d5 2022-09-15 thomas wstandout(view->window);
8920 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
8921 fc2737d5 2022-09-15 thomas view->ncols - 1);
8922 fc2737d5 2022-09-15 thomas if (rc == ERR)
8923 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8924 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
8925 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
8926 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
8927 fc2737d5 2022-09-15 thomas if (rc == ERR)
8928 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
8930 fc2737d5 2022-09-15 thomas wstandend(view->window);
8933 fc2737d5 2022-09-15 thomas return NULL;
8936 fc2737d5 2022-09-15 thomas static const struct got_error *
8937 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8939 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8940 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8941 fc2737d5 2022-09-15 thomas char *line = NULL;
8942 fc2737d5 2022-09-15 thomas ssize_t linelen;
8943 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8944 fc2737d5 2022-09-15 thomas int eos, nscroll;
8946 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
8947 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8948 fc2737d5 2022-09-15 thomas --eos; /* border */
8950 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
8952 fc2737d5 2022-09-15 thomas switch (ch) {
8953 fc2737d5 2022-09-15 thomas case '0':
8954 fc2737d5 2022-09-15 thomas case '$':
8955 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
8956 fc2737d5 2022-09-15 thomas case 'l':
8957 fc2737d5 2022-09-15 thomas case KEY_LEFT:
8958 fc2737d5 2022-09-15 thomas case 'h':
8959 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8961 fc2737d5 2022-09-15 thomas case 'g':
8962 fc2737d5 2022-09-15 thomas case KEY_HOME:
8963 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8964 fc2737d5 2022-09-15 thomas view->count = 0;
8966 fc2737d5 2022-09-15 thomas case 'G':
8967 fc2737d5 2022-09-15 thomas case KEY_END:
8968 fc2737d5 2022-09-15 thomas view->count = 0;
8969 fc2737d5 2022-09-15 thomas if (s->eof)
8971 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
8972 fc2737d5 2022-09-15 thomas s->eof = 1;
8974 fc2737d5 2022-09-15 thomas case 'k':
8975 fc2737d5 2022-09-15 thomas case KEY_UP:
8976 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
8977 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
8979 fc2737d5 2022-09-15 thomas view->count = 0;
8981 fc2737d5 2022-09-15 thomas case CTRL('u'):
8982 fc2737d5 2022-09-15 thomas case 'u':
8983 fc2737d5 2022-09-15 thomas nscroll /= 2;
8984 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8985 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
8986 fc2737d5 2022-09-15 thomas case CTRL('b'):
8987 fc2737d5 2022-09-15 thomas case 'b':
8988 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
8989 fc2737d5 2022-09-15 thomas view->count = 0;
8992 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
8993 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
8995 fc2737d5 2022-09-15 thomas case 'j':
8996 fc2737d5 2022-09-15 thomas case KEY_DOWN:
8997 fc2737d5 2022-09-15 thomas case CTRL('n'):
8998 fc2737d5 2022-09-15 thomas if (!s->eof)
8999 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9001 fc2737d5 2022-09-15 thomas view->count = 0;
9003 fc2737d5 2022-09-15 thomas case CTRL('d'):
9004 fc2737d5 2022-09-15 thomas case 'd':
9005 fc2737d5 2022-09-15 thomas nscroll /= 2;
9006 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9007 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9008 fc2737d5 2022-09-15 thomas case CTRL('f'):
9009 fc2737d5 2022-09-15 thomas case 'f':
9010 fc2737d5 2022-09-15 thomas case ' ':
9011 fc2737d5 2022-09-15 thomas if (s->eof) {
9012 fc2737d5 2022-09-15 thomas view->count = 0;
9015 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9016 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9017 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9018 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9019 fc2737d5 2022-09-15 thomas if (feof(s->f))
9020 fc2737d5 2022-09-15 thomas s->eof = 1;
9022 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9026 fc2737d5 2022-09-15 thomas free(line);
9028 fc2737d5 2022-09-15 thomas default:
9029 fc2737d5 2022-09-15 thomas view->count = 0;
9033 fc2737d5 2022-09-15 thomas return err;
9036 fc2737d5 2022-09-15 thomas static const struct got_error *
9037 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9039 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9041 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9042 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9043 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9044 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9046 fc2737d5 2022-09-15 thomas return NULL;
9049 fc2737d5 2022-09-15 thomas static const struct got_error *
9050 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9052 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9055 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9056 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9058 fc2737d5 2022-09-15 thomas wclear(view->window);
9059 fc2737d5 2022-09-15 thomas view->count = 0;
9060 fc2737d5 2022-09-15 thomas view->x = 0;
9061 fc2737d5 2022-09-15 thomas s->all = !s->all;
9062 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9063 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9064 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9066 fc2737d5 2022-09-15 thomas return create_help(s);
9069 fc2737d5 2022-09-15 thomas static const struct got_error *
9070 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9072 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9073 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9075 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9076 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9077 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9078 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9080 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9081 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9082 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9083 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9084 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9085 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9086 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9088 fc2737d5 2022-09-15 thomas err = create_help(s);
9089 fc2737d5 2022-09-15 thomas return err;
9092 fc2737d5 2022-09-15 thomas static const struct got_error *
9093 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9094 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9096 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9098 2a31b33b 2022-07-23 thomas *new_view = NULL;
9100 2a31b33b 2022-07-23 thomas switch (request) {
9101 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9102 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9103 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9105 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9106 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9107 2a31b33b 2022-07-23 thomas view, s->repo);
9109 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9110 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9112 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9113 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9114 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9116 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9117 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9118 2a31b33b 2022-07-23 thomas s->repo);
9120 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9121 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9123 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9124 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9125 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9126 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9127 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9128 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9129 2a31b33b 2022-07-23 thomas &view->state.tree);
9130 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9131 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9132 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9133 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9135 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9136 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9138 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9139 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9140 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9141 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9142 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9143 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9144 2a31b33b 2022-07-23 thomas view->state.log.repo);
9145 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9146 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9147 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9148 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9150 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9151 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9153 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9154 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9155 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9156 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9157 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9158 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9159 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9160 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9162 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9163 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9164 2a31b33b 2022-07-23 thomas if (err)
9165 2a31b33b 2022-07-23 thomas view_close(*new_view);
9167 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9168 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9169 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9170 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9171 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9172 fc2737d5 2022-09-15 thomas if (err)
9173 fc2737d5 2022-09-15 thomas view_close(*new_view);
9175 2a31b33b 2022-07-23 thomas default:
9176 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9179 2a31b33b 2022-07-23 thomas return err;
9183 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9184 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9186 6458efa5 2020-11-24 stsp static void
9187 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9189 a5d43cac 2022-07-01 thomas switch (view->type) {
9190 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9191 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9192 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9193 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9197 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9198 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9200 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9201 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9204 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9205 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9206 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9208 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9209 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9210 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9212 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9213 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9214 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9216 a5d43cac 2022-07-01 thomas default:
9220 a5d43cac 2022-07-01 thomas view->offset = 0;
9224 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9225 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9227 a5d43cac 2022-07-01 thomas static const struct got_error *
9228 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9230 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9231 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9232 a5d43cac 2022-07-01 thomas int *selected = NULL;
9233 a5d43cac 2022-07-01 thomas int header, offset;
9235 a5d43cac 2022-07-01 thomas switch (view->type) {
9236 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9237 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9238 a5d43cac 2022-07-01 thomas header = 3;
9239 a5d43cac 2022-07-01 thomas scrolld = NULL;
9240 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9241 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9242 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9243 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9244 a5d43cac 2022-07-01 thomas view->offset = offset;
9248 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9249 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9250 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9251 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9252 a5d43cac 2022-07-01 thomas selected = &s->selected;
9255 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9256 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9257 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9258 a5d43cac 2022-07-01 thomas header = 3;
9259 a5d43cac 2022-07-01 thomas selected = &s->selected;
9262 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9263 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9264 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9265 a5d43cac 2022-07-01 thomas header = 5;
9266 a5d43cac 2022-07-01 thomas selected = &s->selected;
9269 a5d43cac 2022-07-01 thomas default:
9270 a5d43cac 2022-07-01 thomas selected = NULL;
9271 a5d43cac 2022-07-01 thomas scrolld = NULL;
9272 a5d43cac 2022-07-01 thomas header = 0;
9276 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9277 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9278 a5d43cac 2022-07-01 thomas view->offset = offset;
9279 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9280 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9281 a5d43cac 2022-07-01 thomas *selected -= offset;
9285 a5d43cac 2022-07-01 thomas return err;
9288 a5d43cac 2022-07-01 thomas static void
9289 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9293 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9294 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9295 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9296 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9298 6879ba42 2020-10-01 naddy fputc('\n', fp);
9301 4ed7e80c 2018-05-20 stsp __dead static void
9302 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9304 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9306 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9307 6879ba42 2020-10-01 naddy getprogname());
9308 6879ba42 2020-10-01 naddy if (hflag) {
9309 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9310 6879ba42 2020-10-01 naddy list_commands(fp);
9312 6879ba42 2020-10-01 naddy exit(status);
9315 c2301be8 2018-04-30 stsp static char **
9316 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9318 ee85c5e8 2020-02-29 stsp va_list ap;
9319 c2301be8 2018-04-30 stsp char **argv;
9322 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9324 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9325 c2301be8 2018-04-30 stsp if (argv == NULL)
9326 c2301be8 2018-04-30 stsp err(1, "calloc");
9327 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9328 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9329 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9330 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9333 ee85c5e8 2020-02-29 stsp va_end(ap);
9334 c2301be8 2018-04-30 stsp return argv;
9338 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9339 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9340 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9341 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9342 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9344 ee85c5e8 2020-02-29 stsp static const struct got_error *
9345 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9347 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9348 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9349 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9350 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9351 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9352 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9353 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9354 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9355 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9357 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9358 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9359 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9361 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9362 7cd52833 2022-06-23 thomas if (error != NULL)
9363 7cd52833 2022-06-23 thomas goto done;
9365 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9366 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9367 ee85c5e8 2020-02-29 stsp goto done;
9369 ee85c5e8 2020-02-29 stsp if (worktree)
9370 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9372 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9373 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9374 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9375 ee85c5e8 2020-02-29 stsp goto done;
9378 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9379 ee85c5e8 2020-02-29 stsp if (error != NULL)
9380 ee85c5e8 2020-02-29 stsp goto done;
9382 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9383 ee85c5e8 2020-02-29 stsp repo, worktree);
9384 ee85c5e8 2020-02-29 stsp if (error)
9385 ee85c5e8 2020-02-29 stsp goto done;
9387 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9388 84de9106 2020-12-26 stsp if (error)
9389 84de9106 2020-12-26 stsp goto done;
9390 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9391 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9392 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9393 ee85c5e8 2020-02-29 stsp if (error)
9394 ee85c5e8 2020-02-29 stsp goto done;
9396 ee85c5e8 2020-02-29 stsp if (worktree) {
9397 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9398 ee85c5e8 2020-02-29 stsp worktree = NULL;
9401 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9402 945f9229 2022-04-16 thomas if (error)
9403 945f9229 2022-04-16 thomas goto done;
9405 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9406 ee85c5e8 2020-02-29 stsp if (error) {
9407 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9408 ee85c5e8 2020-02-29 stsp goto done;
9409 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9410 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9411 6879ba42 2020-10-01 naddy usage(1, 1);
9412 ee85c5e8 2020-02-29 stsp /* not reached */
9415 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9416 ee85c5e8 2020-02-29 stsp if (error)
9417 ee85c5e8 2020-02-29 stsp goto done;
9419 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9421 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9422 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9424 1d0f4054 2021-06-17 stsp if (repo) {
9425 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9426 1d0f4054 2021-06-17 stsp if (error == NULL)
9427 1d0f4054 2021-06-17 stsp error = close_err;
9429 945f9229 2022-04-16 thomas if (commit)
9430 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9431 ee85c5e8 2020-02-29 stsp if (worktree)
9432 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9433 7cd52833 2022-06-23 thomas if (pack_fds) {
9434 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9435 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9436 7cd52833 2022-06-23 thomas if (error == NULL)
9437 7cd52833 2022-06-23 thomas error = pack_err;
9440 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9441 ee85c5e8 2020-02-29 stsp free(commit_id);
9442 ee85c5e8 2020-02-29 stsp free(cwd);
9443 ee85c5e8 2020-02-29 stsp free(repo_path);
9444 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9445 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9447 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9448 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9449 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9451 87670572 2020-12-26 naddy tog_free_refs();
9452 ee85c5e8 2020-02-29 stsp return error;
9456 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9458 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9459 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9460 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9461 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9462 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9463 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9464 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9466 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9468 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
9470 a0abeae5 2023-02-17 thomas #ifndef PROFILE
9471 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9472 a0abeae5 2023-02-17 thomas NULL) == -1)
9473 a0abeae5 2023-02-17 thomas err(1, "pledge");
9476 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9477 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9479 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9480 9f7d7167 2018-04-29 stsp switch (ch) {
9482 9f7d7167 2018-04-29 stsp hflag = 1;
9485 53ccebc2 2019-07-30 stsp Vflag = 1;
9488 6879ba42 2020-10-01 naddy usage(hflag, 1);
9489 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9493 9f7d7167 2018-04-29 stsp argc -= optind;
9494 9f7d7167 2018-04-29 stsp argv += optind;
9495 9814e6a3 2020-09-27 naddy optind = 1;
9496 c2301be8 2018-04-30 stsp optreset = 1;
9498 53ccebc2 2019-07-30 stsp if (Vflag) {
9499 53ccebc2 2019-07-30 stsp got_version_print_str();
9500 6879ba42 2020-10-01 naddy return 0;
9503 c2301be8 2018-04-30 stsp if (argc == 0) {
9504 f29d3e89 2018-06-23 stsp if (hflag)
9505 6879ba42 2020-10-01 naddy usage(hflag, 0);
9506 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9507 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9509 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9513 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9514 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9515 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9516 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9517 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9523 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9524 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9525 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9526 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9527 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9528 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9531 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9532 ee85c5e8 2020-02-29 stsp if (argc != 1)
9533 6879ba42 2020-10-01 naddy usage(0, 1);
9534 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9535 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9537 ee85c5e8 2020-02-29 stsp if (hflag)
9538 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9540 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9544 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9546 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9547 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9548 a2f4a359 2020-02-28 stsp free(cmd_argv);
9551 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9552 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9553 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9554 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9555 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9556 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);