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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
21 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
23 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
24 31120ada 2018-04-30 stsp #include <errno.h>
25 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
26 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
28 9f7d7167 2018-04-29 stsp #include <curses.h>
29 9f7d7167 2018-04-29 stsp #include <panel.h>
30 9f7d7167 2018-04-29 stsp #include <locale.h>
31 61266923 2020-01-14 stsp #include <signal.h>
32 9f7d7167 2018-04-29 stsp #include <stdlib.h>
33 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
34 26ed57b2 2018-05-19 stsp #include <stdio.h>
35 9f7d7167 2018-04-29 stsp #include <getopt.h>
36 9f7d7167 2018-04-29 stsp #include <string.h>
37 9f7d7167 2018-04-29 stsp #include <err.h>
38 80ddbec8 2018-04-29 stsp #include <unistd.h>
39 26ed57b2 2018-05-19 stsp #include <limits.h>
40 61e69b96 2018-05-20 stsp #include <wchar.h>
41 788c352e 2018-06-16 stsp #include <time.h>
42 84451b3e 2018-07-10 stsp #include <pthread.h>
43 5036bf37 2018-09-24 stsp #include <libgen.h>
44 60493ae3 2019-06-20 stsp #include <regex.h>
45 3da8ef85 2021-09-21 stsp #include <sched.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"
61 66b04f8f 2023-07-19 thomas #include "got_keyword.h"
64 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
72 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
75 9f7d7167 2018-04-29 stsp #ifndef nitems
76 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
79 9f7d7167 2018-04-29 stsp struct tog_cmd {
80 c2301be8 2018-04-30 stsp const char *name;
81 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
85 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
89 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
90 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
95 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
96 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
98 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
99 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
100 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
101 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
102 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
103 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
106 d6b05b5b 2018-08-04 stsp enum tog_view_type {
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
108 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
109 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
110 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
111 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
112 fc2737d5 2022-09-15 thomas TOG_VIEW_HELP
115 fc2737d5 2022-09-15 thomas /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
116 fc2737d5 2022-09-15 thomas enum tog_keymap_type {
117 fc2737d5 2022-09-15 thomas TOG_KEYMAP_KEYS = -2,
118 fc2737d5 2022-09-15 thomas TOG_KEYMAP_GLOBAL,
119 fc2737d5 2022-09-15 thomas TOG_KEYMAP_DIFF,
120 fc2737d5 2022-09-15 thomas TOG_KEYMAP_LOG,
121 fc2737d5 2022-09-15 thomas TOG_KEYMAP_BLAME,
122 fc2737d5 2022-09-15 thomas TOG_KEYMAP_TREE,
123 fc2737d5 2022-09-15 thomas TOG_KEYMAP_REF,
124 fc2737d5 2022-09-15 thomas TOG_KEYMAP_HELP
127 a5d43cac 2022-07-01 thomas enum tog_view_mode {
128 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
129 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
130 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
133 87a675e0 2023-04-22 thomas #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
135 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
137 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
138 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
139 ba4f502b 2018-08-04 stsp struct got_object_id *id;
140 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
143 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
144 ba4f502b 2018-08-04 stsp struct commit_queue {
145 ba4f502b 2018-08-04 stsp int ncommits;
146 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
149 f26dddb7 2019-11-08 stsp struct tog_color {
150 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
151 6d17833f 2019-11-08 stsp regex_t regex;
152 6d17833f 2019-11-08 stsp short colorpair;
154 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
156 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
157 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
158 349dfd1e 2023-07-23 thomas static struct {
159 349dfd1e 2023-07-23 thomas struct got_object_id *id;
161 349dfd1e 2023-07-23 thomas char marker;
162 349dfd1e 2023-07-23 thomas } tog_base_commit;
163 8295dece 2023-10-08 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
165 2183bbf6 2022-01-23 thomas static const struct got_error *
166 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
167 2183bbf6 2022-01-23 thomas struct got_reference* re2)
169 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
170 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
171 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
173 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
174 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
175 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
176 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
177 2183bbf6 2022-01-23 thomas *cmp = -1;
178 2183bbf6 2022-01-23 thomas return NULL;
179 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
180 2183bbf6 2022-01-23 thomas *cmp = 1;
181 2183bbf6 2022-01-23 thomas return NULL;
184 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
185 2183bbf6 2022-01-23 thomas return NULL;
188 11b20872 2019-11-08 stsp static const struct got_error *
189 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
191 51a10b52 2020-12-26 stsp const struct got_error *err;
193 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
194 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
197 51a10b52 2020-12-26 stsp return err;
199 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
203 51a10b52 2020-12-26 stsp static void
204 51a10b52 2020-12-26 stsp tog_free_refs(void)
206 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
207 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
208 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
210 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
213 51a10b52 2020-12-26 stsp static const struct got_error *
214 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
215 11b20872 2019-11-08 stsp int idx, short color)
217 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
218 11b20872 2019-11-08 stsp struct tog_color *tc;
219 11b20872 2019-11-08 stsp int regerr = 0;
221 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
222 11b20872 2019-11-08 stsp return NULL;
224 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
226 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
227 11b20872 2019-11-08 stsp if (tc == NULL)
228 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
229 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
230 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
231 11b20872 2019-11-08 stsp if (regerr) {
232 11b20872 2019-11-08 stsp static char regerr_msg[512];
233 11b20872 2019-11-08 stsp static char err_msg[512];
234 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
235 11b20872 2019-11-08 stsp sizeof(regerr_msg));
236 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
237 11b20872 2019-11-08 stsp regerr_msg);
238 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
240 11b20872 2019-11-08 stsp return err;
242 11b20872 2019-11-08 stsp tc->colorpair = idx;
243 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
244 11b20872 2019-11-08 stsp return NULL;
247 11b20872 2019-11-08 stsp static void
248 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
250 11b20872 2019-11-08 stsp struct tog_color *tc;
252 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
253 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
254 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
255 11b20872 2019-11-08 stsp regfree(&tc->regex);
260 ef20f542 2022-06-26 thomas static struct tog_color *
261 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
263 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
265 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
266 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
270 11b20872 2019-11-08 stsp return NULL;
274 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
277 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
279 11b20872 2019-11-08 stsp return COLOR_CYAN;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
281 11b20872 2019-11-08 stsp return COLOR_YELLOW;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
283 11b20872 2019-11-08 stsp return COLOR_GREEN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
285 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
287 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
288 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
289 91b8c405 2020-01-25 stsp return COLOR_CYAN;
290 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
291 11b20872 2019-11-08 stsp return COLOR_GREEN;
292 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
293 11b20872 2019-11-08 stsp return COLOR_GREEN;
294 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
295 11b20872 2019-11-08 stsp return COLOR_CYAN;
296 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
297 11b20872 2019-11-08 stsp return COLOR_YELLOW;
298 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
299 6458efa5 2020-11-24 stsp return COLOR_GREEN;
300 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
301 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
302 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
303 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
304 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
305 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
311 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
313 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
315 11b20872 2019-11-08 stsp if (val == NULL)
316 11b20872 2019-11-08 stsp return default_color_value(envvar);
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
319 11b20872 2019-11-08 stsp return COLOR_BLACK;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
321 11b20872 2019-11-08 stsp return COLOR_RED;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
323 11b20872 2019-11-08 stsp return COLOR_GREEN;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
325 11b20872 2019-11-08 stsp return COLOR_YELLOW;
326 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
327 11b20872 2019-11-08 stsp return COLOR_BLUE;
328 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
329 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
330 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
331 11b20872 2019-11-08 stsp return COLOR_CYAN;
332 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
333 11b20872 2019-11-08 stsp return COLOR_WHITE;
334 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
337 11b20872 2019-11-08 stsp return default_color_value(envvar);
340 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
341 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
342 3dbaef42 2020-11-24 stsp const char *label1, *label2;
343 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
344 19a6a6b5 2022-07-01 thomas int fd1, fd2;
345 82c78e96 2022-08-06 thomas int lineno;
346 15a087fe 2019-02-21 stsp int first_displayed_line;
347 15a087fe 2019-02-21 stsp int last_displayed_line;
349 15a087fe 2019-02-21 stsp int diff_context;
350 3dbaef42 2020-11-24 stsp int ignore_whitespace;
351 64453f7e 2020-11-21 stsp int force_text_diff;
352 15a087fe 2019-02-21 stsp struct got_repository *repo;
353 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
354 fe621944 2020-11-10 stsp size_t nlines;
355 f44b1f58 2020-02-02 tracey int matched_line;
356 f44b1f58 2020-02-02 tracey int selected_line;
358 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
359 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
362 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
363 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
365 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
366 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
367 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
368 1a76625f 2018-10-22 stsp int commits_needed;
369 fb280deb 2021-08-30 stsp int load_all;
370 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
371 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
372 1a76625f 2018-10-22 stsp const char *in_repo_path;
373 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
374 1a76625f 2018-10-22 stsp struct got_repository *repo;
375 1af5eddf 2022-06-23 thomas int *pack_fds;
376 1a76625f 2018-10-22 stsp int log_complete;
377 92845f09 2023-07-26 thomas pthread_cond_t log_loaded;
378 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
379 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
380 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
381 13add988 2019-10-15 stsp int *searching;
382 13add988 2019-10-15 stsp int *search_next_done;
383 13add988 2019-10-15 stsp regex_t *regex;
384 7e8004ba 2022-09-11 thomas int *limiting;
385 7e8004ba 2022-09-11 thomas int limit_match;
386 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
387 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
388 92845f09 2023-07-26 thomas struct got_worktree *worktree;
389 92845f09 2023-07-26 thomas int need_commit_marker;
392 1a76625f 2018-10-22 stsp struct tog_log_view_state {
393 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
394 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
395 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
396 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
397 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
398 b01e7d3b 2018-08-04 stsp int selected;
399 b01e7d3b 2018-08-04 stsp char *in_repo_path;
400 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
401 b672a97a 2020-01-27 stsp int log_branches;
402 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
403 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
404 1a76625f 2018-10-22 stsp sig_atomic_t quit;
405 1a76625f 2018-10-22 stsp pthread_t thread;
406 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
407 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
408 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
409 11b20872 2019-11-08 stsp struct tog_colors colors;
410 f69c5a46 2022-07-19 thomas int use_committer;
411 7e8004ba 2022-09-11 thomas int limit_view;
412 7e8004ba 2022-09-11 thomas regex_t limit_regex;
413 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
416 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
417 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
418 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
419 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
420 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
421 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
422 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
423 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
424 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
425 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
426 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
427 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
428 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
429 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
430 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
432 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
433 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
434 e9424729 2018-08-04 stsp int nlines;
436 e9424729 2018-08-04 stsp struct tog_view *view;
437 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
441 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
442 e9424729 2018-08-04 stsp const char *path;
443 e9424729 2018-08-04 stsp struct got_repository *repo;
444 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
445 e9424729 2018-08-04 stsp int *complete;
446 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
447 fc06ba56 2019-08-22 stsp void *cancel_arg;
448 1134ebde 2023-04-22 thomas pthread_cond_t blame_complete;
451 e9424729 2018-08-04 stsp struct tog_blame {
453 be659d10 2020-11-18 stsp off_t filesize;
454 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
455 6fcac457 2018-11-19 stsp int nlines;
456 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
457 e9424729 2018-08-04 stsp pthread_t thread;
458 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
459 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
460 e9424729 2018-08-04 stsp const char *path;
461 7cd52833 2022-06-23 thomas int *pack_fds;
464 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
465 7cbe629d 2018-08-04 stsp int first_displayed_line;
466 7cbe629d 2018-08-04 stsp int last_displayed_line;
467 7cbe629d 2018-08-04 stsp int selected_line;
468 4fc71f3b 2022-07-12 thomas int last_diffed_line;
469 7cbe629d 2018-08-04 stsp int blame_complete;
472 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
473 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
474 e5a0f69f 2018-08-18 stsp char *path;
475 7cbe629d 2018-08-04 stsp struct got_repository *repo;
476 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
477 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
478 e9424729 2018-08-04 stsp struct tog_blame blame;
479 6c4c42e0 2019-06-24 stsp int matched_line;
480 11b20872 2019-11-08 stsp struct tog_colors colors;
483 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
484 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
486 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
487 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
488 ad80ab7b 2018-08-04 stsp int selected;
491 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
493 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
494 ad80ab7b 2018-08-04 stsp char *tree_label;
495 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
496 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
497 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
498 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
499 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
500 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
501 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
502 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
503 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
504 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
505 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
506 6458efa5 2020-11-24 stsp struct tog_colors colors;
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
510 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
511 6458efa5 2020-11-24 stsp struct got_reference *ref;
515 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
517 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
518 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
519 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
520 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
521 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
522 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
523 6458efa5 2020-11-24 stsp struct got_repository *repo;
524 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
525 bddb1296 2019-11-08 stsp struct tog_colors colors;
528 fc2737d5 2022-09-15 thomas struct tog_help_view_state {
530 fc2737d5 2022-09-15 thomas off_t *line_offsets;
531 fc2737d5 2022-09-15 thomas size_t nlines;
532 fc2737d5 2022-09-15 thomas int lineno;
533 fc2737d5 2022-09-15 thomas int first_displayed_line;
534 fc2737d5 2022-09-15 thomas int last_displayed_line;
536 fc2737d5 2022-09-15 thomas int matched_line;
537 fc2737d5 2022-09-15 thomas int selected_line;
539 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
542 fc2737d5 2022-09-15 thomas #define GENERATE_HELP \
543 fc2737d5 2022-09-15 thomas KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
544 fc2737d5 2022-09-15 thomas KEY_("H F1", "Open view-specific help (double tap for all help)"), \
545 fc2737d5 2022-09-15 thomas KEY_("k C-p Up", "Move cursor or page up one line"), \
546 fc2737d5 2022-09-15 thomas KEY_("j C-n Down", "Move cursor or page down one line"), \
547 fc2737d5 2022-09-15 thomas KEY_("C-b b PgUp", "Scroll the view up one page"), \
548 fc2737d5 2022-09-15 thomas KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
549 fc2737d5 2022-09-15 thomas KEY_("C-u u", "Scroll the view up one half page"), \
550 fc2737d5 2022-09-15 thomas KEY_("C-d d", "Scroll the view down one half page"), \
551 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
552 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
553 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
554 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
555 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
556 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
557 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
558 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
559 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
560 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
561 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
562 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
563 c282f787 2023-04-22 thomas KEY_("S", "Switch split-screen layout"), \
564 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
565 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
566 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
567 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
568 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
570 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
571 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
572 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
573 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
574 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
575 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
576 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
577 fc2737d5 2022-09-15 thomas " commit"), \
578 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
579 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
580 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
581 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
583 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
584 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
585 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
586 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
587 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
588 fc2737d5 2022-09-15 thomas " data"), \
589 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
590 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
591 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
592 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
593 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
594 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
595 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
597 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
598 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
599 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
600 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
601 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
602 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
603 fc2737d5 2022-09-15 thomas " selected line's commit"), \
604 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
605 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
607 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
608 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
609 fc2737d5 2022-09-15 thomas " selected file"), \
610 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
611 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
612 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
613 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
615 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
616 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
617 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
618 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
619 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
620 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
621 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
623 fc2737d5 2022-09-15 thomas struct tog_key_map {
624 fc2737d5 2022-09-15 thomas const char *keys;
625 fc2737d5 2022-09-15 thomas const char *info;
626 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
629 b85a3496 2023-04-14 thomas /* curses io for tog regress */
630 b85a3496 2023-04-14 thomas struct tog_io {
631 b85a3496 2023-04-14 thomas FILE *cin;
632 b85a3496 2023-04-14 thomas FILE *cout;
634 5b3a801d 2023-04-22 thomas FILE *sdump;
635 1fc091f3 2023-09-05 thomas char *input_str;
636 8e778ade 2023-04-22 thomas int wait_for_ui;
637 557d3365 2023-04-14 thomas } tog_io;
638 557d3365 2023-04-14 thomas static int using_mock_io;
640 13bc6832 2023-04-22 thomas #define TOG_KEY_SCRDUMP SHRT_MIN
643 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
645 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
646 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
647 669b5ffa 2018-10-07 stsp * there is enough screen estate.
649 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
650 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
652 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
653 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
654 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
656 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
657 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
659 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
661 cc3c9aac 2018-08-01 stsp struct tog_view {
662 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
663 26ed57b2 2018-05-19 stsp WINDOW *window;
664 26ed57b2 2018-05-19 stsp PANEL *panel;
665 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
666 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
667 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
668 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
669 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
670 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
671 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
672 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
673 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
675 669b5ffa 2018-10-07 stsp struct tog_view *parent;
676 669b5ffa 2018-10-07 stsp struct tog_view *child;
679 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
680 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
681 e78dc838 2020-12-04 stsp * between parent and child.
682 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
683 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
684 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
685 e78dc838 2020-12-04 stsp * situations.
687 e78dc838 2020-12-04 stsp int focus_child;
689 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
690 5dc9f4bc 2018-08-04 stsp /* type-specific state */
691 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
693 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
694 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
695 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
696 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
697 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
698 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
701 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
702 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
703 e78dc838 2020-12-04 stsp struct tog_view *, int);
704 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
705 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
706 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
708 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
709 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
710 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
711 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
712 c0c4acc8 2021-01-24 stsp int search_started;
713 60493ae3 2019-06-20 stsp int searching;
714 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
715 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
716 60493ae3 2019-06-20 stsp int search_next_done;
717 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
718 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
719 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
720 1803e47f 2019-06-22 stsp regex_t regex;
721 41605754 2020-11-12 stsp regmatch_t regmatch;
722 f2d06bef 2023-01-23 thomas const char *action;
725 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
726 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
727 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
728 78756c87 2020-11-24 stsp struct got_repository *);
729 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
730 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
731 e78dc838 2020-12-04 stsp struct tog_view *, int);
732 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
733 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
734 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
735 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
736 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
737 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
739 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
740 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
741 92845f09 2023-07-26 thomas const char *, const char *, int, struct got_worktree *);
742 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
743 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
744 e78dc838 2020-12-04 stsp struct tog_view *, int);
745 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
746 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
747 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
748 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
750 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
751 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
752 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
753 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
754 e78dc838 2020-12-04 stsp struct tog_view *, int);
755 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
756 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
757 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
758 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
759 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
761 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
762 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
763 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
764 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
765 e78dc838 2020-12-04 stsp struct tog_view *, int);
766 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
767 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
768 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
770 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
771 6458efa5 2020-11-24 stsp struct got_repository *);
772 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
773 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
774 e78dc838 2020-12-04 stsp struct tog_view *, int);
775 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
776 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
777 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
779 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
780 2a7d3cd7 2022-09-17 thomas struct tog_view *);
781 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
782 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
783 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
784 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
785 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
786 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
787 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
788 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
790 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
791 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
792 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
793 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
794 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
796 25791caa 2018-10-24 stsp static void
797 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
799 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
802 83baff54 2019-08-12 stsp static void
803 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
805 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
808 61266923 2020-01-14 stsp static void
809 61266923 2020-01-14 stsp tog_sigcont(int signo)
811 61266923 2020-01-14 stsp tog_sigcont_received = 1;
814 296152d1 2022-05-31 thomas static void
815 296152d1 2022-05-31 thomas tog_sigint(int signo)
817 296152d1 2022-05-31 thomas tog_sigint_received = 1;
820 296152d1 2022-05-31 thomas static void
821 296152d1 2022-05-31 thomas tog_sigterm(int signo)
823 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
826 296152d1 2022-05-31 thomas static int
827 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
829 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
830 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
833 e5a0f69f 2018-08-18 stsp static const struct got_error *
834 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
836 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
838 669b5ffa 2018-10-07 stsp if (view->child) {
839 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
840 669b5ffa 2018-10-07 stsp view->child = NULL;
842 e5a0f69f 2018-08-18 stsp if (view->close)
843 e5a0f69f 2018-08-18 stsp err = view->close(view);
844 ea5e7bb5 2018-08-01 stsp if (view->panel)
845 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
846 ea5e7bb5 2018-08-01 stsp if (view->window)
847 ea5e7bb5 2018-08-01 stsp delwin(view->window);
848 ea5e7bb5 2018-08-01 stsp free(view);
849 f2d749db 2022-07-12 thomas return err ? err : child_err;
852 ea5e7bb5 2018-08-01 stsp static struct tog_view *
853 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
854 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
856 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
858 ea5e7bb5 2018-08-01 stsp if (view == NULL)
859 ea5e7bb5 2018-08-01 stsp return NULL;
861 d6b05b5b 2018-08-04 stsp view->type = type;
862 f7d12f7e 2018-08-01 stsp view->lines = LINES;
863 f7d12f7e 2018-08-01 stsp view->cols = COLS;
864 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
865 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
866 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
867 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
868 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
869 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
870 96a765a8 2018-08-04 stsp view_close(view);
871 ea5e7bb5 2018-08-01 stsp return NULL;
873 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
874 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
875 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
876 96a765a8 2018-08-04 stsp view_close(view);
877 ea5e7bb5 2018-08-01 stsp return NULL;
880 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
881 ea5e7bb5 2018-08-01 stsp return view;
885 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
887 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
889 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
892 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
893 a5d43cac 2022-07-01 thomas static int
894 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
896 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
899 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
901 5c60c32a 2018-10-18 stsp static const struct got_error *
902 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
904 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
906 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
907 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
908 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
910 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
911 a5d43cac 2022-07-01 thomas view->begin_x = 0;
912 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
913 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
914 53d2bdd3 2022-07-10 thomas view->cols > 119)
915 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
917 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
918 a5d43cac 2022-07-01 thomas view->begin_y = 0;
920 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
921 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
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 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
929 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
931 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
932 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
934 5c60c32a 2018-10-18 stsp return NULL;
937 5c60c32a 2018-10-18 stsp static const struct got_error *
938 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
940 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
942 5c60c32a 2018-10-18 stsp view->begin_x = 0;
943 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
944 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
945 5c60c32a 2018-10-18 stsp view->ncols = COLS;
946 5c60c32a 2018-10-18 stsp view->lines = LINES;
947 5c60c32a 2018-10-18 stsp view->cols = COLS;
948 5c60c32a 2018-10-18 stsp err = view_resize(view);
950 5c60c32a 2018-10-18 stsp return err;
952 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
953 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
955 5c60c32a 2018-10-18 stsp return NULL;
959 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
961 5c60c32a 2018-10-18 stsp return view->parent == NULL;
964 b65b3ea0 2022-06-23 thomas static int
965 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
967 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
970 e44940c3 2022-07-01 thomas static int
971 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
973 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
976 444d5325 2022-07-03 thomas static int
977 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
979 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
980 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
983 a5d43cac 2022-07-01 thomas static void
984 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
986 a5d43cac 2022-07-01 thomas PANEL *panel;
987 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
989 a5d43cac 2022-07-01 thomas if (view->parent)
990 a5d43cac 2022-07-01 thomas return view_border(view->parent);
992 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
993 a5d43cac 2022-07-01 thomas if (panel == NULL)
996 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
997 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
998 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
999 88e8b64f 2023-04-22 thomas view->begin_x, ACS_HLINE, view->ncols);
1001 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1002 88e8b64f 2023-04-22 thomas ACS_VLINE, view->nlines);
1005 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
1006 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
1007 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
1008 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
1009 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
1011 4d8c2215 2018-08-19 stsp static const struct got_error *
1012 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
1014 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
1015 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
1017 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
1019 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1020 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1022 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1023 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1024 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1026 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1028 4918811f 2022-07-01 thomas if (view->child) {
1029 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1031 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1032 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1033 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1034 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1035 40236d76 2022-06-23 thomas ncols = COLS;
1037 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1038 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1039 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1041 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1043 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1045 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1046 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1049 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1050 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1051 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1053 a5d43cac 2022-07-01 thomas if (hs) {
1054 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1055 a5d43cac 2022-07-01 thomas if (err)
1056 a5d43cac 2022-07-01 thomas return err;
1057 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1058 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1059 a5d43cac 2022-07-01 thomas if (err)
1060 a5d43cac 2022-07-01 thomas return err;
1062 a5d43cac 2022-07-01 thomas view_border(view);
1063 a5d43cac 2022-07-01 thomas update_panels();
1064 a5d43cac 2022-07-01 thomas doupdate();
1065 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1066 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1068 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1069 40236d76 2022-06-23 thomas ncols = COLS;
1071 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1072 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1073 ea0bff04 2022-07-19 thomas if (err)
1074 ea0bff04 2022-07-19 thomas return err;
1077 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1078 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1079 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1080 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1081 40236d76 2022-06-23 thomas wclear(view->window);
1083 40236d76 2022-06-23 thomas view->nlines = nlines;
1084 40236d76 2022-06-23 thomas view->ncols = ncols;
1085 40236d76 2022-06-23 thomas view->lines = LINES;
1086 40236d76 2022-06-23 thomas view->cols = COLS;
1088 5c60c32a 2018-10-18 stsp return NULL;
1091 ea0bff04 2022-07-19 thomas static const struct got_error *
1092 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1094 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1095 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1096 3a0139e8 2022-07-22 thomas int n = 0;
1098 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1099 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1102 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1103 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1105 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1106 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1107 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1110 ea0bff04 2022-07-19 thomas return err;
1113 97cb21cd 2022-07-12 thomas static void
1114 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1116 97cb21cd 2022-07-12 thomas if (n == 0)
1119 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1120 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1121 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1123 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1124 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1125 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1126 97cb21cd 2022-07-12 thomas view->offset -= n;
1128 97cb21cd 2022-07-12 thomas view->offset = 0;
1132 53d2bdd3 2022-07-10 thomas static const struct got_error *
1133 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1135 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1136 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1138 53d2bdd3 2022-07-10 thomas if (view->parent)
1139 53d2bdd3 2022-07-10 thomas v = view->parent;
1141 53d2bdd3 2022-07-10 thomas v = view;
1143 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1144 53d2bdd3 2022-07-10 thomas return NULL;
1146 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1148 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1149 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1150 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1151 53d2bdd3 2022-07-10 thomas if (view->parent)
1152 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1154 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1155 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1156 53d2bdd3 2022-07-10 thomas view->count = 0;
1157 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1158 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1159 53d2bdd3 2022-07-10 thomas view->count = 0;
1160 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1162 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1163 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1164 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1165 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1166 53d2bdd3 2022-07-10 thomas if (err)
1167 53d2bdd3 2022-07-10 thomas return err;
1168 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1169 53d2bdd3 2022-07-10 thomas } else {
1170 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1171 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1172 53d2bdd3 2022-07-10 thomas if (view->parent)
1173 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1175 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1176 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1177 53d2bdd3 2022-07-10 thomas view->count = 0;
1178 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1179 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1180 53d2bdd3 2022-07-10 thomas view->count = 0;
1181 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1183 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1186 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1187 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1188 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1189 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1191 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1192 53d2bdd3 2022-07-10 thomas if (err)
1193 53d2bdd3 2022-07-10 thomas return err;
1194 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1195 53d2bdd3 2022-07-10 thomas if (err)
1196 53d2bdd3 2022-07-10 thomas return err;
1198 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1199 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1200 53d2bdd3 2022-07-10 thomas if (err)
1201 53d2bdd3 2022-07-10 thomas return err;
1204 3a0139e8 2022-07-22 thomas if (v->resize)
1205 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1206 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1207 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1209 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1211 53d2bdd3 2022-07-10 thomas return err;
1214 53d2bdd3 2022-07-10 thomas static void
1215 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1217 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1219 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1220 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1223 669b5ffa 2018-10-07 stsp static const struct got_error *
1224 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1226 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1228 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1229 669b5ffa 2018-10-07 stsp return NULL;
1231 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1232 669b5ffa 2018-10-07 stsp view->child = NULL;
1233 669b5ffa 2018-10-07 stsp return err;
1236 40236d76 2022-06-23 thomas static const struct got_error *
1237 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1239 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1241 669b5ffa 2018-10-07 stsp view->child = child;
1242 669b5ffa 2018-10-07 stsp child->parent = view;
1244 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1245 53d2bdd3 2022-07-10 thomas if (err)
1246 53d2bdd3 2022-07-10 thomas return err;
1248 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1249 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1251 53d2bdd3 2022-07-10 thomas return err;
1254 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1255 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1257 2a31b33b 2022-07-23 thomas static const struct got_error *
1258 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1259 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1261 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1262 2a31b33b 2022-07-23 thomas const struct got_error *err;
1263 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1265 2a31b33b 2022-07-23 thomas *requested = NULL;
1267 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1268 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1270 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1271 2a31b33b 2022-07-23 thomas if (err)
1272 2a31b33b 2022-07-23 thomas return err;
1274 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1275 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1276 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1277 2a31b33b 2022-07-23 thomas if (err)
1278 2a31b33b 2022-07-23 thomas return err;
1281 2a31b33b 2022-07-23 thomas view->focussed = 0;
1282 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1283 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1284 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1285 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1287 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1288 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1289 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1290 2a31b33b 2022-07-23 thomas if (err)
1291 2a31b33b 2022-07-23 thomas return err;
1292 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1293 2a31b33b 2022-07-23 thomas if (err)
1294 2a31b33b 2022-07-23 thomas return err;
1295 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1297 2a31b33b 2022-07-23 thomas *requested = new_view;
1299 2a31b33b 2022-07-23 thomas return NULL;
1302 34bc9ec9 2019-02-22 stsp static void
1303 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1305 25791caa 2018-10-24 stsp int cols, lines;
1306 25791caa 2018-10-24 stsp struct winsize size;
1308 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1309 25791caa 2018-10-24 stsp cols = 80; /* Default */
1310 25791caa 2018-10-24 stsp lines = 24;
1312 25791caa 2018-10-24 stsp cols = size.ws_col;
1313 25791caa 2018-10-24 stsp lines = size.ws_row;
1315 25791caa 2018-10-24 stsp resize_term(lines, cols);
1318 2b49a8ae 2019-06-22 stsp static const struct got_error *
1319 f54d892e 2023-02-17 thomas view_search_start(struct tog_view *view, int fast_refresh)
1321 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1322 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1323 2b49a8ae 2019-06-22 stsp char pattern[1024];
1326 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1327 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1328 c0c4acc8 2021-01-24 stsp view->searching = 0;
1329 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1331 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1333 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1334 2b49a8ae 2019-06-22 stsp return NULL;
1336 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1337 a5d43cac 2022-07-01 thomas v = view->child;
1338 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1339 d07291c6 2022-12-30 thomas v = view->parent;
1341 1fc091f3 2023-09-05 thomas if (tog_io.input_str != NULL) {
1342 1fc091f3 2023-09-05 thomas if (strlcpy(pattern, tog_io.input_str, sizeof(pattern)) >=
1343 1fc091f3 2023-09-05 thomas sizeof(pattern))
1344 1fc091f3 2023-09-05 thomas return got_error(GOT_ERR_NO_SPACE);
1345 1fc091f3 2023-09-05 thomas } else {
1346 1fc091f3 2023-09-05 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1347 1fc091f3 2023-09-05 thomas wclrtoeol(v->window);
1348 1fc091f3 2023-09-05 thomas nodelay(v->window, FALSE); /* block for search term input */
1349 1fc091f3 2023-09-05 thomas nocbreak();
1351 1fc091f3 2023-09-05 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1352 1fc091f3 2023-09-05 thomas wrefresh(v->window);
1353 1fc091f3 2023-09-05 thomas cbreak();
1354 1fc091f3 2023-09-05 thomas noecho();
1355 1fc091f3 2023-09-05 thomas nodelay(v->window, TRUE);
1356 1fc091f3 2023-09-05 thomas if (!fast_refresh && !using_mock_io)
1357 1fc091f3 2023-09-05 thomas halfdelay(10);
1358 1fc091f3 2023-09-05 thomas if (ret == ERR)
1359 1fc091f3 2023-09-05 thomas return NULL;
1362 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1363 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1364 7c32bd05 2019-06-22 stsp if (err) {
1365 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1366 7c32bd05 2019-06-22 stsp return err;
1368 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1369 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1370 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1371 2b49a8ae 2019-06-22 stsp view->search_next(view);
1374 2b49a8ae 2019-06-22 stsp return NULL;
1377 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1378 64486692 2022-07-07 thomas static const struct got_error *
1379 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1381 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1382 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1384 64486692 2022-07-07 thomas if (view->parent)
1385 64486692 2022-07-07 thomas v = view->parent;
1387 64486692 2022-07-07 thomas v = view;
1389 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1390 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1392 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1394 ddbc4d37 2022-07-12 thomas if (!v->child)
1395 ddbc4d37 2022-07-12 thomas return NULL;
1396 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1397 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1399 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1400 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1401 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1402 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1403 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1406 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1407 64486692 2022-07-07 thomas v->ncols = COLS;
1408 64486692 2022-07-07 thomas v->child->ncols = COLS;
1409 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1411 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1412 64486692 2022-07-07 thomas if (err)
1413 64486692 2022-07-07 thomas return err;
1415 64486692 2022-07-07 thomas v->child->mode = v->mode;
1416 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1417 64486692 2022-07-07 thomas v->focus_child = 1;
1419 64486692 2022-07-07 thomas err = view_fullscreen(v);
1420 64486692 2022-07-07 thomas if (err)
1421 64486692 2022-07-07 thomas return err;
1422 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1423 64486692 2022-07-07 thomas if (err)
1424 64486692 2022-07-07 thomas return err;
1426 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1427 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1428 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1429 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1430 cf1fe301 2022-07-29 thomas if (err)
1431 cf1fe301 2022-07-29 thomas return err;
1432 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1433 cf1fe301 2022-07-29 thomas if (err)
1434 cf1fe301 2022-07-29 thomas return err;
1435 ddbc4d37 2022-07-12 thomas } else {
1436 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1437 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1439 f4e6231a 2022-07-22 thomas if (v->resize)
1440 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1441 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1442 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1444 64486692 2022-07-07 thomas return err;
1448 b85a3496 2023-04-14 thomas * Strip trailing whitespace from str starting at byte *n;
1449 b85a3496 2023-04-14 thomas * if *n < 0, use strlen(str). Return new str length in *n.
1451 b85a3496 2023-04-14 thomas static void
1452 b85a3496 2023-04-14 thomas strip_trailing_ws(char *str, int *n)
1454 b85a3496 2023-04-14 thomas size_t x = *n;
1456 b85a3496 2023-04-14 thomas if (str == NULL || *str == '\0')
1459 b85a3496 2023-04-14 thomas if (x < 0)
1460 b85a3496 2023-04-14 thomas x = strlen(str);
1462 b85a3496 2023-04-14 thomas while (x-- > 0 && isspace((unsigned char)str[x]))
1463 b85a3496 2023-04-14 thomas str[x] = '\0';
1465 b85a3496 2023-04-14 thomas *n = x + 1;
1469 b85a3496 2023-04-14 thomas * Extract visible substring of line y from the curses screen
1470 13bc6832 2023-04-22 thomas * and strip trailing whitespace. If vline is set, overwrite
1471 13bc6832 2023-04-22 thomas * line[vline] with '|' because the ACS_VLINE character is
1472 13bc6832 2023-04-22 thomas * written out as 'x'. Write the line to file f.
1474 b85a3496 2023-04-14 thomas static const struct got_error *
1475 b85a3496 2023-04-14 thomas view_write_line(FILE *f, int y, int vline)
1477 b85a3496 2023-04-14 thomas char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1478 b85a3496 2023-04-14 thomas int r, w;
1480 b85a3496 2023-04-14 thomas r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1481 b85a3496 2023-04-14 thomas if (r == ERR)
1482 b85a3496 2023-04-14 thomas return got_error_fmt(GOT_ERR_RANGE,
1483 b85a3496 2023-04-14 thomas "failed to extract line %d", y);
1486 b85a3496 2023-04-14 thomas * In some views, lines are padded with blanks to COLS width.
1487 b85a3496 2023-04-14 thomas * Strip them so we can diff without the -b flag when testing.
1489 b85a3496 2023-04-14 thomas strip_trailing_ws(line, &r);
1491 13bc6832 2023-04-22 thomas if (vline > 0)
1492 b85a3496 2023-04-14 thomas line[vline] = '|';
1494 b85a3496 2023-04-14 thomas w = fprintf(f, "%s\n", line);
1495 b85a3496 2023-04-14 thomas if (w != r + 1) /* \n */
1496 b85a3496 2023-04-14 thomas return got_ferror(f, GOT_ERR_IO);
1498 b85a3496 2023-04-14 thomas return NULL;
1502 b85a3496 2023-04-14 thomas * Capture the visible curses screen by writing each line to the
1503 b85a3496 2023-04-14 thomas * file at the path set via the TOG_SCR_DUMP environment variable.
1505 b85a3496 2023-04-14 thomas static const struct got_error *
1506 b85a3496 2023-04-14 thomas screendump(struct tog_view *view)
1508 b85a3496 2023-04-14 thomas const struct got_error *err;
1511 5b3a801d 2023-04-22 thomas err = got_opentemp_truncate(tog_io.sdump);
1512 5b3a801d 2023-04-22 thomas if (err)
1513 5b3a801d 2023-04-22 thomas return err;
1515 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_x) ||
1516 b85a3496 2023-04-14 thomas (view->parent && view->begin_x)) {
1517 b85a3496 2023-04-14 thomas int ncols = view->child ? view->ncols : view->parent->ncols;
1519 b85a3496 2023-04-14 thomas /* vertical splitscreen */
1520 b85a3496 2023-04-14 thomas for (i = 0; i < view->nlines; ++i) {
1521 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, ncols - 1);
1522 b85a3496 2023-04-14 thomas if (err)
1523 b85a3496 2023-04-14 thomas goto done;
1525 b85a3496 2023-04-14 thomas } else {
1526 b85a3496 2023-04-14 thomas int hline = 0;
1528 b85a3496 2023-04-14 thomas /* fullscreen or horizontal splitscreen */
1529 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_y) ||
1530 b85a3496 2023-04-14 thomas (view->parent && view->begin_y)) /* hsplit */
1531 b85a3496 2023-04-14 thomas hline = view->child ?
1532 b85a3496 2023-04-14 thomas view->child->begin_y : view->begin_y;
1534 b85a3496 2023-04-14 thomas for (i = 0; i < view->lines; i++) {
1535 13bc6832 2023-04-22 thomas if (hline && i == hline - 1) {
1538 b85a3496 2023-04-14 thomas /* ACS_HLINE writes out as 'q', overwrite it */
1539 b85a3496 2023-04-14 thomas for (c = 0; c < view->cols; ++c)
1540 5b3a801d 2023-04-22 thomas fputc('-', tog_io.sdump);
1541 5b3a801d 2023-04-22 thomas fputc('\n', tog_io.sdump);
1542 b85a3496 2023-04-14 thomas continue;
1545 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, 0);
1546 b85a3496 2023-04-14 thomas if (err)
1547 b85a3496 2023-04-14 thomas goto done;
1552 b85a3496 2023-04-14 thomas return err;
1556 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1557 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1559 07b0611c 2022-06-23 thomas static int
1560 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1562 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1563 a5d43cac 2022-07-01 thomas int x, n = 0;
1565 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1566 a5d43cac 2022-07-01 thomas v = view->child;
1567 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1568 a5d43cac 2022-07-01 thomas v = view->parent;
1570 07b0611c 2022-06-23 thomas view->count = 0;
1571 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1572 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1573 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1574 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1575 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1578 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1579 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1580 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1581 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1585 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1586 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1588 07b0611c 2022-06-23 thomas if (n >= 9999999)
1589 07b0611c 2022-06-23 thomas n = 9999999;
1591 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1592 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1594 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1595 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1600 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1601 07b0611c 2022-06-23 thomas view->count = n;
1603 07b0611c 2022-06-23 thomas return c;
1606 f2d06bef 2023-01-23 thomas static void
1607 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1609 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1611 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1612 f2d06bef 2023-01-23 thomas v = view->child;
1613 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1614 f2d06bef 2023-01-23 thomas v = view->parent;
1616 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1617 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1618 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1619 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1622 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1623 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1625 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1626 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1627 f2d06bef 2023-01-23 thomas view->action = NULL;
1629 f2d06bef 2023-01-23 thomas view->action = NULL;
1633 b85a3496 2023-04-14 thomas * Read the next line from the test script and assign
1634 b85a3496 2023-04-14 thomas * key instruction to *ch. If at EOF, set the *done flag.
1636 0cf4efb1 2018-09-29 stsp static const struct got_error *
1637 a184c764 2023-04-22 thomas tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1639 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1640 b85a3496 2023-04-14 thomas char *line = NULL;
1641 b85a3496 2023-04-14 thomas size_t linesz = 0;
1642 1fc091f3 2023-09-05 thomas ssize_t n;
1645 a184c764 2023-04-22 thomas if (view->count && --view->count) {
1646 a184c764 2023-04-22 thomas *ch = view->ch;
1647 a184c764 2023-04-22 thomas return NULL;
1649 a184c764 2023-04-22 thomas *ch = -1;
1651 1fc091f3 2023-09-05 thomas if ((n = getline(&line, &linesz, script)) == -1) {
1652 b85a3496 2023-04-14 thomas if (feof(script)) {
1653 b85a3496 2023-04-14 thomas *done = 1;
1654 b85a3496 2023-04-14 thomas goto done;
1655 b85a3496 2023-04-14 thomas } else {
1656 b85a3496 2023-04-14 thomas err = got_ferror(script, GOT_ERR_IO);
1657 b85a3496 2023-04-14 thomas goto done;
1661 8e778ade 2023-04-22 thomas if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1662 8e778ade 2023-04-22 thomas tog_io.wait_for_ui = 1;
1663 8e778ade 2023-04-22 thomas else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1664 b85a3496 2023-04-14 thomas *ch = KEY_ENTER;
1665 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1666 b85a3496 2023-04-14 thomas *ch = KEY_RIGHT;
1667 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1668 b85a3496 2023-04-14 thomas *ch = KEY_LEFT;
1669 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1670 b85a3496 2023-04-14 thomas *ch = KEY_DOWN;
1671 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_UP", 6) == 0)
1672 b85a3496 2023-04-14 thomas *ch = KEY_UP;
1673 52c5094b 2023-04-28 thomas else if (strncasecmp(line, "TAB", 3) == 0)
1674 52c5094b 2023-04-28 thomas *ch = '\t';
1675 13bc6832 2023-04-22 thomas else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1676 b85a3496 2023-04-14 thomas *ch = TOG_KEY_SCRDUMP;
1677 a184c764 2023-04-22 thomas else if (isdigit((unsigned char)*line)) {
1678 a184c764 2023-04-22 thomas char *t = line;
1680 a184c764 2023-04-22 thomas while (isdigit((unsigned char)*t))
1682 a184c764 2023-04-22 thomas view->ch = *ch = *t;
1683 a184c764 2023-04-22 thomas *t = '\0';
1684 a184c764 2023-04-22 thomas /* ignore error, view->count is 0 if instruction is invalid */
1685 a184c764 2023-04-22 thomas view->count = strtonum(line, 0, INT_MAX, NULL);
1686 1fc091f3 2023-09-05 thomas } else {
1687 b85a3496 2023-04-14 thomas *ch = *line;
1688 1fc091f3 2023-09-05 thomas if (n > 2 && (*ch == '/' || *ch == '&')) {
1689 1fc091f3 2023-09-05 thomas /* skip leading keymap and trim trailing newline */
1690 1fc091f3 2023-09-05 thomas tog_io.input_str = strndup(line + 1, n - 2);
1691 1fc091f3 2023-09-05 thomas if (tog_io.input_str == NULL) {
1692 1fc091f3 2023-09-05 thomas err = got_error_from_errno("strndup");
1693 1fc091f3 2023-09-05 thomas goto done;
1699 b85a3496 2023-04-14 thomas free(line);
1700 b85a3496 2023-04-14 thomas return err;
1703 b85a3496 2023-04-14 thomas static const struct got_error *
1704 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1705 557d3365 2023-04-14 thomas struct tog_view_list_head *views, int fast_refresh)
1707 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1708 669b5ffa 2018-10-07 stsp struct tog_view *v;
1709 1a76625f 2018-10-22 stsp int ch, errcode;
1711 e5a0f69f 2018-08-18 stsp *new = NULL;
1713 f2d06bef 2023-01-23 thomas if (view->action)
1714 f2d06bef 2023-01-23 thomas action_report(view);
1716 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1717 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1718 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1719 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1720 07b0611c 2022-06-23 thomas view->count = 0;
1723 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1724 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1725 82954512 2020-02-03 stsp if (errcode)
1726 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1727 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1728 3da8ef85 2021-09-21 stsp sched_yield();
1729 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1730 82954512 2020-02-03 stsp if (errcode)
1731 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1732 82954512 2020-02-03 stsp "pthread_mutex_lock");
1733 60493ae3 2019-06-20 stsp view->search_next(view);
1734 60493ae3 2019-06-20 stsp return NULL;
1737 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1738 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1739 1a76625f 2018-10-22 stsp if (errcode)
1740 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1742 557d3365 2023-04-14 thomas if (using_mock_io) {
1743 a184c764 2023-04-22 thomas err = tog_read_script_key(tog_io.f, view, &ch, done);
1744 9d9cab5e 2023-04-22 thomas if (err) {
1745 9d9cab5e 2023-04-22 thomas errcode = pthread_mutex_lock(&tog_mutex);
1746 b85a3496 2023-04-14 thomas return err;
1748 b85a3496 2023-04-14 thomas } else if (view->count && --view->count) {
1749 634cb454 2022-07-03 thomas cbreak();
1750 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1751 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1752 b85a3496 2023-04-14 thomas /* let C-g or backspace abort unfinished count */
1753 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1754 634cb454 2022-07-03 thomas view->count = 0;
1756 634cb454 2022-07-03 thomas ch = view->ch;
1757 634cb454 2022-07-03 thomas } else {
1758 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1759 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1760 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1762 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1763 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1764 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1765 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1766 1a76625f 2018-10-22 stsp if (errcode)
1767 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1769 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1770 25791caa 2018-10-24 stsp tog_resizeterm();
1771 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1772 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1773 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1774 25791caa 2018-10-24 stsp err = view_resize(v);
1776 25791caa 2018-10-24 stsp return err;
1777 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1779 25791caa 2018-10-24 stsp return err;
1780 cdfcfb03 2020-12-06 stsp if (v->child) {
1781 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1783 cdfcfb03 2020-12-06 stsp return err;
1784 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1785 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1787 cdfcfb03 2020-12-06 stsp return err;
1788 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1789 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1790 53d2bdd3 2022-07-10 thomas if (err)
1791 53d2bdd3 2022-07-10 thomas return err;
1797 e5a0f69f 2018-08-18 stsp switch (ch) {
1798 fc2737d5 2022-09-15 thomas case '?':
1799 fc2737d5 2022-09-15 thomas case 'H':
1800 fc2737d5 2022-09-15 thomas case KEY_F(1):
1801 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1802 fc2737d5 2022-09-15 thomas err = view->reset(view);
1804 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1807 07b0611c 2022-06-23 thomas view->count = 0;
1808 1e37a5c2 2019-05-12 jcs if (view->child) {
1809 e78dc838 2020-12-04 stsp view->focussed = 0;
1810 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1811 e78dc838 2020-12-04 stsp view->focus_child = 1;
1812 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1813 e78dc838 2020-12-04 stsp view->focussed = 0;
1814 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1815 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1816 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1817 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1818 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1820 a5d43cac 2022-07-01 thomas if (err)
1821 a5d43cac 2022-07-01 thomas return err;
1823 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1824 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1825 a5d43cac 2022-07-01 thomas if (err)
1826 a5d43cac 2022-07-01 thomas return err;
1831 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1832 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1833 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1834 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1835 a5d43cac 2022-07-01 thomas if (err)
1838 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1840 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1841 9970f7fc 2020-12-03 stsp view->dying = 1;
1846 1c5e5faa 2022-06-23 thomas case 'F':
1847 07b0611c 2022-06-23 thomas view->count = 0;
1848 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1849 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1851 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1852 e78dc838 2020-12-04 stsp view->focussed = 0;
1853 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1854 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1855 53d2bdd3 2022-07-10 thomas } else {
1856 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1857 53d2bdd3 2022-07-10 thomas if (!err)
1858 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1862 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1863 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1865 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1866 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1867 e78dc838 2020-12-04 stsp view->focussed = 1;
1868 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1870 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1871 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1872 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1873 53d2bdd3 2022-07-10 thomas if (!err)
1874 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1878 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1880 a5d43cac 2022-07-01 thomas if (err)
1882 3a0139e8 2022-07-22 thomas if (view->resize) {
1883 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1884 a5d43cac 2022-07-01 thomas if (err)
1887 e0bcabc5 2023-04-28 thomas if (view->parent) {
1888 e0bcabc5 2023-04-28 thomas if (view->parent->resize) {
1889 e0bcabc5 2023-04-28 thomas err = view->parent->resize(view->parent, 0);
1890 e0bcabc5 2023-04-28 thomas if (err != NULL)
1893 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1894 e0bcabc5 2023-04-28 thomas if (err != NULL)
1897 e0bcabc5 2023-04-28 thomas err = offset_selection_down(view);
1899 64486692 2022-07-07 thomas case 'S':
1900 53d2bdd3 2022-07-10 thomas view->count = 0;
1901 64486692 2022-07-07 thomas err = switch_split(view);
1903 53d2bdd3 2022-07-10 thomas case '-':
1904 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1906 53d2bdd3 2022-07-10 thomas case '+':
1907 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1909 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1912 07b0611c 2022-06-23 thomas view->count = 0;
1913 60493ae3 2019-06-20 stsp if (view->search_start)
1914 f54d892e 2023-02-17 thomas view_search_start(view, fast_refresh);
1916 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1920 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1921 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1922 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1923 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1924 60493ae3 2019-06-20 stsp view->search_next(view);
1926 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1928 adf4c9e0 2022-07-03 thomas case 'A':
1929 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1930 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1931 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1932 f2d06bef 2023-01-23 thomas } else {
1933 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1934 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1936 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1937 adf4c9e0 2022-07-03 thomas if (v->reset) {
1938 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1939 adf4c9e0 2022-07-03 thomas if (err)
1940 adf4c9e0 2022-07-03 thomas return err;
1942 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1943 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1944 adf4c9e0 2022-07-03 thomas if (err)
1945 adf4c9e0 2022-07-03 thomas return err;
1949 b85a3496 2023-04-14 thomas case TOG_KEY_SCRDUMP:
1950 b85a3496 2023-04-14 thomas err = screendump(view);
1953 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1957 e5a0f69f 2018-08-18 stsp return err;
1960 ef20f542 2022-06-26 thomas static int
1961 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1963 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1964 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1966 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1968 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1971 669b5ffa 2018-10-07 stsp return view->focussed;
1974 bcbd79e2 2018-08-19 stsp static const struct got_error *
1975 557d3365 2023-04-14 thomas tog_io_close(void)
1977 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1979 557d3365 2023-04-14 thomas if (tog_io.cin && fclose(tog_io.cin) == EOF)
1980 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cin, GOT_ERR_IO);
1981 557d3365 2023-04-14 thomas if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1982 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cout, GOT_ERR_IO);
1983 557d3365 2023-04-14 thomas if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1984 557d3365 2023-04-14 thomas err = got_ferror(tog_io.f, GOT_ERR_IO);
1985 5b3a801d 2023-04-22 thomas if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1986 5b3a801d 2023-04-22 thomas err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1987 1fc091f3 2023-09-05 thomas if (tog_io.input_str != NULL)
1988 1fc091f3 2023-09-05 thomas free(tog_io.input_str);
1990 b85a3496 2023-04-14 thomas return err;
1993 b85a3496 2023-04-14 thomas static const struct got_error *
1994 557d3365 2023-04-14 thomas view_loop(struct tog_view *view)
1996 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1997 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1998 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1999 64486692 2022-07-07 thomas char *mode;
2000 fd823528 2018-10-22 stsp int fast_refresh = 10;
2001 1a76625f 2018-10-22 stsp int done = 0, errcode;
2003 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
2004 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
2005 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
2007 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
2009 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2010 1a76625f 2018-10-22 stsp if (errcode)
2011 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
2013 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
2014 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
2016 1004088d 2018-09-29 stsp view->focussed = 1;
2017 878940b7 2018-09-29 stsp err = view->show(view);
2019 0cf4efb1 2018-09-29 stsp return err;
2020 0cf4efb1 2018-09-29 stsp update_panels();
2021 0cf4efb1 2018-09-29 stsp doupdate();
2022 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
2023 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
2024 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
2025 557d3365 2023-04-14 thomas if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
2026 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
2028 557d3365 2023-04-14 thomas err = view_input(&new_view, &done, view, &views, fast_refresh);
2032 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
2033 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
2034 6ec3d39c 2023-01-23 thomas done = 1;
2035 6ec3d39c 2023-01-23 thomas if (done) {
2036 6ec3d39c 2023-01-23 thomas struct tog_view *v;
2039 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
2040 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
2042 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
2043 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
2044 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
2045 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
2046 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
2047 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
2048 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
2049 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
2052 6ec3d39c 2023-01-23 thomas doupdate();
2055 9970f7fc 2020-12-03 stsp if (view->dying) {
2056 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2058 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2059 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2061 e78dc838 2020-12-04 stsp else if (view->parent)
2062 669b5ffa 2018-10-07 stsp prev = view->parent;
2064 e78dc838 2020-12-04 stsp if (view->parent) {
2065 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2066 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2067 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
2068 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
2069 40236d76 2022-06-23 thomas err = view_resize(view->parent);
2070 40236d76 2022-06-23 thomas if (err)
2072 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
2073 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
2075 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2077 9970f7fc 2020-12-03 stsp err = view_close(view);
2079 e5a0f69f 2018-08-18 stsp goto done;
2081 e78dc838 2020-12-04 stsp view = NULL;
2082 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2083 e78dc838 2020-12-04 stsp if (v->focussed)
2086 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2087 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2089 e78dc838 2020-12-04 stsp view = prev;
2090 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2091 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2092 e78dc838 2020-12-04 stsp tog_view_list_head);
2094 e78dc838 2020-12-04 stsp if (view) {
2095 e78dc838 2020-12-04 stsp if (view->focus_child) {
2096 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2097 e78dc838 2020-12-04 stsp view = view->child;
2099 e78dc838 2020-12-04 stsp view->focussed = 1;
2103 bcbd79e2 2018-08-19 stsp if (new_view) {
2104 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2105 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2106 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2107 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2109 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2110 86c66b02 2018-10-18 stsp err = view_close(v);
2112 86c66b02 2018-10-18 stsp goto done;
2115 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2116 fed7eaa8 2018-10-24 stsp view = new_view;
2118 6ec3d39c 2023-01-23 thomas if (view && !done) {
2119 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2120 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2121 e78dc838 2020-12-04 stsp view = view->child;
2123 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2124 e78dc838 2020-12-04 stsp view = view->parent;
2126 e78dc838 2020-12-04 stsp show_panel(view->panel);
2127 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2128 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2129 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2130 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2132 1a76625f 2018-10-22 stsp goto done;
2134 669b5ffa 2018-10-07 stsp err = view->show(view);
2136 1a76625f 2018-10-22 stsp goto done;
2137 669b5ffa 2018-10-07 stsp if (view->child) {
2138 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2140 1a76625f 2018-10-22 stsp goto done;
2142 1a76625f 2018-10-22 stsp update_panels();
2143 1a76625f 2018-10-22 stsp doupdate();
2147 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2148 f2d749db 2022-07-12 thomas const struct got_error *close_err;
2149 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2150 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2151 f2d749db 2022-07-12 thomas close_err = view_close(view);
2152 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
2153 f2d749db 2022-07-12 thomas err = close_err;
2156 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2157 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2158 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2160 e5a0f69f 2018-08-18 stsp return err;
2163 4ed7e80c 2018-05-20 stsp __dead static void
2164 9f7d7167 2018-04-29 stsp usage_log(void)
2167 c70c5802 2018-08-01 stsp fprintf(stderr,
2168 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2169 9f7d7167 2018-04-29 stsp getprogname());
2173 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2174 80ddbec8 2018-04-29 stsp static const struct got_error *
2175 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2177 00dfcb92 2018-06-11 stsp char *vis = NULL;
2178 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2180 963b370f 2018-05-20 stsp *ws = NULL;
2181 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2182 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2183 00dfcb92 2018-06-11 stsp int vislen;
2184 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2185 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2187 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2188 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2190 00dfcb92 2018-06-11 stsp return err;
2191 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2192 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2193 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2194 a7f50699 2018-06-11 stsp goto done;
2198 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2199 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2200 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2201 a7f50699 2018-06-11 stsp goto done;
2204 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2205 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2207 00dfcb92 2018-06-11 stsp free(vis);
2208 963b370f 2018-05-20 stsp if (err) {
2209 963b370f 2018-05-20 stsp free(*ws);
2210 963b370f 2018-05-20 stsp *ws = NULL;
2211 963b370f 2018-05-20 stsp *wlen = 0;
2213 963b370f 2018-05-20 stsp return err;
2216 05171be4 2022-06-23 thomas static const struct got_error *
2217 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
2219 05171be4 2022-06-23 thomas char *dst;
2220 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
2222 05171be4 2022-06-23 thomas *ptr = NULL;
2223 05171be4 2022-06-23 thomas n = len = strlen(src);
2224 83316834 2022-06-23 thomas dst = malloc(n + 1);
2225 05171be4 2022-06-23 thomas if (dst == NULL)
2226 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
2228 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
2229 05171be4 2022-06-23 thomas const char c = src[idx];
2231 05171be4 2022-06-23 thomas if (c == '\t') {
2232 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
2233 b235ad5d 2022-06-23 thomas char *p;
2235 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
2236 83316834 2022-06-23 thomas if (p == NULL) {
2237 83316834 2022-06-23 thomas free(dst);
2238 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
2241 83316834 2022-06-23 thomas dst = p;
2242 05171be4 2022-06-23 thomas n += nb;
2243 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
2244 05171be4 2022-06-23 thomas sz += nb;
2246 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2250 05171be4 2022-06-23 thomas dst[sz] = '\0';
2251 05171be4 2022-06-23 thomas *ptr = dst;
2252 05171be4 2022-06-23 thomas return NULL;
2256 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2257 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2258 20181212 2023-06-01 thomas * Return the combined column width of all spanned wide characters in
2259 8d208d34 2022-06-23 thomas * *rcol.
2261 8d208d34 2022-06-23 thomas static int
2262 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2264 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2266 8d208d34 2022-06-23 thomas if (n == 0) {
2267 8d208d34 2022-06-23 thomas *rcol = cols;
2268 8d208d34 2022-06-23 thomas return off;
2271 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2272 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2273 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2275 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2277 8d208d34 2022-06-23 thomas if (width == -1) {
2278 8d208d34 2022-06-23 thomas width = 1;
2279 8d208d34 2022-06-23 thomas wline[i] = L'.';
2282 8d208d34 2022-06-23 thomas if (cols + width > n)
2284 8d208d34 2022-06-23 thomas cols += width;
2287 8d208d34 2022-06-23 thomas *rcol = cols;
2288 8d208d34 2022-06-23 thomas return i;
2292 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2293 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2294 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2296 f91a2b48 2022-06-23 thomas static const struct got_error *
2297 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2298 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2300 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2301 8d208d34 2022-06-23 thomas int cols;
2302 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2303 05171be4 2022-06-23 thomas char *exstr = NULL;
2304 963b370f 2018-05-20 stsp size_t wlen;
2305 8d208d34 2022-06-23 thomas int i, scrollx;
2307 963b370f 2018-05-20 stsp *wlinep = NULL;
2308 b700b5d6 2018-07-10 stsp *widthp = 0;
2310 05171be4 2022-06-23 thomas if (expand) {
2311 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2312 05171be4 2022-06-23 thomas if (err)
2313 05171be4 2022-06-23 thomas return err;
2316 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2317 05171be4 2022-06-23 thomas free(exstr);
2319 963b370f 2018-05-20 stsp return err;
2321 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2323 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2324 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2327 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2328 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2332 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2333 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2335 b700b5d6 2018-07-10 stsp if (widthp)
2336 b700b5d6 2018-07-10 stsp *widthp = cols;
2337 f91a2b48 2022-06-23 thomas if (scrollxp)
2338 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2340 963b370f 2018-05-20 stsp free(wline);
2342 963b370f 2018-05-20 stsp *wlinep = wline;
2343 963b370f 2018-05-20 stsp return err;
2346 8b473291 2019-02-21 stsp static const struct got_error*
2347 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2348 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2350 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2351 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2353 8b473291 2019-02-21 stsp const char *name;
2355 8b473291 2019-02-21 stsp *refs_str = NULL;
2357 00ddec2f 2023-05-17 thomas if (refs == NULL)
2358 00ddec2f 2023-05-17 thomas return NULL;
2360 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2361 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2362 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2365 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2366 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2368 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2369 8b473291 2019-02-21 stsp name += 5;
2370 0d095295 2023-06-01 thomas if (strncmp(name, "got/", 4) == 0)
2372 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2373 8b473291 2019-02-21 stsp name += 6;
2374 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2375 8b473291 2019-02-21 stsp name += 8;
2376 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2377 081e3dc2 2023-06-15 thomas if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2380 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2383 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2384 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2385 5d844a1e 2019-08-13 stsp if (err) {
2386 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2387 48cae60d 2020-09-22 stsp free(ref_id);
2390 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2391 5d844a1e 2019-08-13 stsp err = NULL;
2392 5d844a1e 2019-08-13 stsp tag = NULL;
2395 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2396 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2397 48cae60d 2020-09-22 stsp free(ref_id);
2399 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2400 52b5abe1 2019-08-13 stsp if (cmp != 0)
2402 8b473291 2019-02-21 stsp s = *refs_str;
2403 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2404 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2405 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2407 8b473291 2019-02-21 stsp *refs_str = NULL;
2413 8b473291 2019-02-21 stsp return err;
2416 963b370f 2018-05-20 stsp static const struct got_error *
2417 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2418 27a741e5 2019-09-11 stsp int col_tab_align)
2420 e6b8b890 2020-12-29 naddy char *smallerthan;
2422 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2423 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2424 5813d178 2019-03-09 stsp author = smallerthan + 1;
2425 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2426 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2427 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2430 5813d178 2019-03-09 stsp static const struct got_error *
2431 349dfd1e 2023-07-23 thomas draw_commit(struct tog_view *view, struct commit_queue_entry *entry,
2432 349dfd1e 2023-07-23 thomas const size_t date_display_cols, int author_display_cols)
2434 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2435 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2436 349dfd1e 2023-07-23 thomas struct got_commit_object *commit = entry->commit;
2437 349dfd1e 2023-07-23 thomas struct got_object_id *id = entry->id;
2438 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2439 61dc16bb 2023-05-17 thomas char *refs_str = NULL;
2440 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2441 5813d178 2019-03-09 stsp char *author = NULL;
2442 cabb4cfd 2023-06-01 thomas wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2443 cabb4cfd 2023-06-01 thomas int author_width, refstr_width, logmsg_width;
2444 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2445 cabb4cfd 2023-06-01 thomas int col, limit, scrollx, logmsg_x;
2446 349dfd1e 2023-07-23 thomas const int avail = view->ncols, marker_column = author_display_cols + 1;
2447 ccb26ccd 2018-11-05 stsp struct tm tm;
2448 45d799e2 2018-12-23 stsp time_t committer_time;
2449 11b20872 2019-11-08 stsp struct tog_color *tc;
2450 9472af95 2023-05-15 thomas struct got_reflist_head *refs;
2452 92845f09 2023-07-26 thomas if (tog_base_commit.id != NULL && tog_base_commit.idx == -1 &&
2453 92845f09 2023-07-26 thomas got_object_id_cmp(id, tog_base_commit.id) == 0)
2454 92845f09 2023-07-26 thomas tog_base_commit.idx = entry->idx;
2455 3a333429 2023-07-26 thomas if (tog_io.wait_for_ui && s->thread_args.need_commit_marker) {
2458 3a333429 2023-07-26 thomas rc = pthread_cond_wait(&s->thread_args.log_loaded, &tog_mutex);
2460 3a333429 2023-07-26 thomas return got_error_set_errno(rc, "pthread_cond_wait");
2463 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2464 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2465 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2466 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2467 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2469 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2470 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2472 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2473 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2475 11b20872 2019-11-08 stsp wattr_on(view->window,
2476 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2477 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2479 11b20872 2019-11-08 stsp wattr_off(view->window,
2480 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2481 27a741e5 2019-09-11 stsp col = limit;
2482 b39d25c7 2018-07-10 stsp if (col > avail)
2483 b39d25c7 2018-07-10 stsp goto done;
2485 6570a66d 2019-11-08 stsp if (avail >= 120) {
2486 6570a66d 2019-11-08 stsp char *id_str;
2487 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2489 6570a66d 2019-11-08 stsp goto done;
2490 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2492 11b20872 2019-11-08 stsp wattr_on(view->window,
2493 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2494 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2496 11b20872 2019-11-08 stsp wattr_off(view->window,
2497 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2498 6570a66d 2019-11-08 stsp free(id_str);
2500 6570a66d 2019-11-08 stsp if (col > avail)
2501 6570a66d 2019-11-08 stsp goto done;
2504 f69c5a46 2022-07-19 thomas if (s->use_committer)
2505 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2507 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2508 5813d178 2019-03-09 stsp if (author == NULL) {
2509 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2510 80ddbec8 2018-04-29 stsp goto done;
2512 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2514 bb737323 2018-05-20 stsp goto done;
2515 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2517 11b20872 2019-11-08 stsp wattr_on(view->window,
2518 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2519 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2520 bb737323 2018-05-20 stsp col += author_width;
2521 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2522 92845f09 2023-07-26 thomas if (tog_base_commit.marker != GOT_WORKTREE_STATE_UNKNOWN &&
2523 349dfd1e 2023-07-23 thomas author_width == marker_column &&
2524 9403b695 2023-08-23 thomas entry->idx == tog_base_commit.idx && !s->limit_view) {
2525 f517f81a 2023-07-23 thomas tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2527 f517f81a 2023-07-23 thomas wattr_on(view->window,
2528 f517f81a 2023-07-23 thomas COLOR_PAIR(tc->colorpair), NULL);
2529 349dfd1e 2023-07-23 thomas waddch(view->window, tog_base_commit.marker);
2531 f517f81a 2023-07-23 thomas wattr_off(view->window,
2532 f517f81a 2023-07-23 thomas COLOR_PAIR(tc->colorpair), NULL);
2534 349dfd1e 2023-07-23 thomas waddch(view->window, ' ');
2536 bb737323 2018-05-20 stsp author_width++;
2539 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2540 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2541 9c2eaf34 2018-05-20 stsp if (col > avail)
2542 9c2eaf34 2018-05-20 stsp goto done;
2544 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2546 6d9fbc00 2018-04-29 stsp goto done;
2547 bb737323 2018-05-20 stsp logmsg = logmsg0;
2548 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2550 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2551 bb737323 2018-05-20 stsp if (newline)
2552 bb737323 2018-05-20 stsp *newline = '\0';
2554 cabb4cfd 2023-06-01 thomas limit = avail - col;
2555 cabb4cfd 2023-06-01 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2556 cabb4cfd 2023-06-01 thomas limit--; /* for the border */
2558 9472af95 2023-05-15 thomas /* Prepend reference labels to log message if possible .*/
2559 9472af95 2023-05-15 thomas refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2560 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, id, s->repo);
2561 61dc16bb 2023-05-17 thomas if (err)
2562 61dc16bb 2023-05-17 thomas goto done;
2563 61dc16bb 2023-05-17 thomas if (refs_str) {
2564 cabb4cfd 2023-06-01 thomas char *rs;
2566 cabb4cfd 2023-06-01 thomas if (asprintf(&rs, "[%s]", refs_str) == -1) {
2567 9472af95 2023-05-15 thomas err = got_error_from_errno("asprintf");
2568 9472af95 2023-05-15 thomas goto done;
2570 cabb4cfd 2023-06-01 thomas err = format_line(&wrefstr, &refstr_width,
2571 cabb4cfd 2023-06-01 thomas &scrollx, rs, view->x, limit, col, 1);
2572 cabb4cfd 2023-06-01 thomas free(rs);
2573 cabb4cfd 2023-06-01 thomas if (err)
2574 cabb4cfd 2023-06-01 thomas goto done;
2575 9472af95 2023-05-15 thomas tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2577 9472af95 2023-05-15 thomas wattr_on(view->window,
2578 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2579 cabb4cfd 2023-06-01 thomas waddwstr(view->window, &wrefstr[scrollx]);
2581 9472af95 2023-05-15 thomas wattr_off(view->window,
2582 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2583 cabb4cfd 2023-06-01 thomas col += MAX(refstr_width, 0);
2584 cabb4cfd 2023-06-01 thomas if (col > avail)
2585 cabb4cfd 2023-06-01 thomas goto done;
2587 cabb4cfd 2023-06-01 thomas if (col < avail) {
2588 cabb4cfd 2023-06-01 thomas waddch(view->window, ' ');
2592 cabb4cfd 2023-06-01 thomas if (refstr_width > 0)
2593 cabb4cfd 2023-06-01 thomas logmsg_x = 0;
2595 cabb4cfd 2023-06-01 thomas int unscrolled_refstr_width;
2596 cabb4cfd 2023-06-01 thomas size_t len = wcslen(wrefstr);
2599 cabb4cfd 2023-06-01 thomas * No need to check for -1 return value here since
2600 cabb4cfd 2023-06-01 thomas * unprintables have been replaced by span_wline().
2602 cabb4cfd 2023-06-01 thomas unscrolled_refstr_width = wcswidth(wrefstr, len);
2603 cabb4cfd 2023-06-01 thomas unscrolled_refstr_width += 1; /* trailing space */
2604 cabb4cfd 2023-06-01 thomas logmsg_x = view->x - unscrolled_refstr_width;
2607 cabb4cfd 2023-06-01 thomas limit = avail - col;
2608 cabb4cfd 2023-06-01 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2609 cabb4cfd 2023-06-01 thomas limit--; /* for the border */
2611 cabb4cfd 2023-06-01 thomas logmsg_x = view->x;
2613 cabb4cfd 2023-06-01 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2614 cabb4cfd 2023-06-01 thomas limit, col, 1);
2615 cabb4cfd 2023-06-01 thomas if (err)
2616 cabb4cfd 2023-06-01 thomas goto done;
2617 cabb4cfd 2023-06-01 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2618 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2619 27a741e5 2019-09-11 stsp while (col < avail) {
2620 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2624 80ddbec8 2018-04-29 stsp free(logmsg0);
2625 bb737323 2018-05-20 stsp free(wlogmsg);
2626 cabb4cfd 2023-06-01 thomas free(wrefstr);
2627 61dc16bb 2023-05-17 thomas free(refs_str);
2628 5813d178 2019-03-09 stsp free(author);
2629 bb737323 2018-05-20 stsp free(wauthor);
2630 80ddbec8 2018-04-29 stsp free(line);
2631 80ddbec8 2018-04-29 stsp return err;
2634 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2635 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2636 899d86c2 2018-05-10 stsp struct got_object_id *id)
2638 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2639 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2641 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2642 80ddbec8 2018-04-29 stsp if (entry == NULL)
2643 899d86c2 2018-05-10 stsp return NULL;
2645 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2646 d68b9737 2022-09-05 thomas if (dup == NULL) {
2647 d68b9737 2022-09-05 thomas free(entry);
2648 d68b9737 2022-09-05 thomas return NULL;
2651 d68b9737 2022-09-05 thomas entry->id = dup;
2652 99db9666 2018-05-07 stsp entry->commit = commit;
2653 899d86c2 2018-05-10 stsp return entry;
2656 99db9666 2018-05-07 stsp static void
2657 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2659 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2661 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2662 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2663 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2664 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2665 d68b9737 2022-09-05 thomas free(entry->id);
2666 99db9666 2018-05-07 stsp free(entry);
2669 99db9666 2018-05-07 stsp static void
2670 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2672 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2673 99db9666 2018-05-07 stsp pop_commit(commits);
2676 c4972b91 2018-05-07 stsp static const struct got_error *
2677 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2678 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2680 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2681 13add988 2019-10-15 stsp regmatch_t regmatch;
2682 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2684 13add988 2019-10-15 stsp *have_match = 0;
2686 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2688 13add988 2019-10-15 stsp return err;
2690 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2692 13add988 2019-10-15 stsp goto done;
2694 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2695 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2696 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2697 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2698 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2699 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2700 13add988 2019-10-15 stsp *have_match = 1;
2702 13add988 2019-10-15 stsp free(id_str);
2703 13add988 2019-10-15 stsp free(logmsg);
2704 13add988 2019-10-15 stsp return err;
2707 13add988 2019-10-15 stsp static const struct got_error *
2708 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2710 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2713 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2714 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2715 1a76625f 2018-10-22 stsp * while updating the display.
2718 7210b715 2022-09-11 thomas struct got_object_id id;
2719 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2720 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2721 7e8004ba 2022-09-11 thomas int limit_match = 0;
2722 1a76625f 2018-10-22 stsp int errcode;
2724 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2725 4e0d2870 2020-12-07 naddy NULL, NULL);
2726 7210b715 2022-09-11 thomas if (err)
2729 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2732 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2733 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2734 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2738 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2739 1a76625f 2018-10-22 stsp if (errcode) {
2740 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2741 13add988 2019-10-15 stsp "pthread_mutex_lock");
2745 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2746 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2747 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2749 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2750 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2751 7e8004ba 2022-09-11 thomas a->limit_regex);
2752 7e8004ba 2022-09-11 thomas if (err)
2755 7e8004ba 2022-09-11 thomas if (limit_match) {
2756 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2758 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2759 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2760 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2761 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2762 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2765 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2766 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2768 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2769 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2770 7e8004ba 2022-09-11 thomas matched, entry);
2771 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2775 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2776 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2777 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2779 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2782 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2783 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2784 7c1452c1 2020-03-26 stsp int have_match;
2785 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2789 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2790 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2791 7e8004ba 2022-09-11 thomas *a->search_next_done =
2792 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2793 7e8004ba 2022-09-11 thomas } else if (have_match)
2794 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2797 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2798 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2799 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2800 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2803 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2805 9ba79e04 2018-06-11 stsp return err;
2808 2b779855 2020-12-05 naddy static void
2809 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2811 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2812 2b779855 2020-12-05 naddy int ncommits = 0;
2814 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2815 2b779855 2020-12-05 naddy while (entry) {
2816 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2817 2b779855 2020-12-05 naddy s->selected_entry = entry;
2820 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2821 2b779855 2020-12-05 naddy ncommits++;
2825 0553a4e3 2018-04-30 stsp static const struct got_error *
2826 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2828 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2829 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2830 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2831 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2832 60493ae3 2019-06-20 stsp int width;
2833 cabb4cfd 2023-06-01 thomas int ncommits, author_cols = 4, refstr_cols;
2834 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2835 8b473291 2019-02-21 stsp char *refs_str = NULL;
2836 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2837 11b20872 2019-11-08 stsp struct tog_color *tc;
2838 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2839 cabb4cfd 2023-06-01 thomas struct got_reflist_head *refs;
2841 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2842 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2844 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2845 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2846 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2848 ecb28ae0 2018-07-16 stsp return err;
2849 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2850 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2851 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2852 00ddec2f 2023-05-17 thomas s->repo);
2853 00ddec2f 2023-05-17 thomas if (err)
2854 00ddec2f 2023-05-17 thomas goto done;
2857 557d3365 2023-04-14 thomas if (s->thread_args.commits_needed == 0 && !using_mock_io)
2858 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2860 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2861 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2862 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2863 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2864 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2865 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2866 8f4ed634 2020-03-26 stsp goto done;
2869 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2870 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2872 f9686aa5 2020-03-27 stsp if (view->searching) {
2873 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2874 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2875 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2876 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2877 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2878 f9686aa5 2020-03-27 stsp search_str = "searching...";
2881 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2882 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2884 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2885 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2886 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2887 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2888 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2889 8f4ed634 2020-03-26 stsp goto done;
2893 00580e07 2023-06-01 thomas free(refs_str);
2894 00580e07 2023-06-01 thomas refs_str = NULL;
2896 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2897 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2898 91198554 2022-06-23 thomas "........................................",
2899 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2900 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2901 1a76625f 2018-10-22 stsp header = NULL;
2902 1a76625f 2018-10-22 stsp goto done;
2904 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2905 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2906 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2907 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2908 1a76625f 2018-10-22 stsp header = NULL;
2909 1a76625f 2018-10-22 stsp goto done;
2911 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2913 1a76625f 2018-10-22 stsp goto done;
2915 2814baeb 2018-08-01 stsp werase(view->window);
2917 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2918 a3404814 2018-09-02 stsp wstandout(view->window);
2919 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2921 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2922 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2923 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2924 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2928 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2929 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2930 a3404814 2018-09-02 stsp wstandend(view->window);
2931 ecb28ae0 2018-07-16 stsp free(wline);
2932 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2933 1a76625f 2018-10-22 stsp goto done;
2935 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2936 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2937 5813d178 2019-03-09 stsp ncommits = 0;
2938 05171be4 2022-06-23 thomas view->maxx = 0;
2939 5813d178 2019-03-09 stsp while (entry) {
2940 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2941 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2942 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2943 5813d178 2019-03-09 stsp int width;
2944 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2946 f69c5a46 2022-07-19 thomas if (s->use_committer)
2947 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2949 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2950 5813d178 2019-03-09 stsp if (author == NULL) {
2951 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2952 5813d178 2019-03-09 stsp goto done;
2954 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2955 27a741e5 2019-09-11 stsp date_display_cols);
2956 5813d178 2019-03-09 stsp if (author_cols < width)
2957 5813d178 2019-03-09 stsp author_cols = width;
2958 5813d178 2019-03-09 stsp free(wauthor);
2959 5813d178 2019-03-09 stsp free(author);
2960 ef944b8b 2022-07-21 thomas if (err)
2961 ef944b8b 2022-07-21 thomas goto done;
2962 cabb4cfd 2023-06-01 thomas refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2963 cabb4cfd 2023-06-01 thomas entry->id);
2964 cabb4cfd 2023-06-01 thomas err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2965 cabb4cfd 2023-06-01 thomas if (err)
2966 cabb4cfd 2023-06-01 thomas goto done;
2967 cabb4cfd 2023-06-01 thomas if (refs_str) {
2968 cabb4cfd 2023-06-01 thomas wchar_t *ws;
2969 cabb4cfd 2023-06-01 thomas err = format_line(&ws, &width, NULL, refs_str,
2970 cabb4cfd 2023-06-01 thomas 0, INT_MAX, date_display_cols + author_cols, 0);
2971 cabb4cfd 2023-06-01 thomas free(ws);
2972 00580e07 2023-06-01 thomas free(refs_str);
2973 00580e07 2023-06-01 thomas refs_str = NULL;
2974 cabb4cfd 2023-06-01 thomas if (err)
2975 cabb4cfd 2023-06-01 thomas goto done;
2976 cabb4cfd 2023-06-01 thomas refstr_cols = width + 3; /* account for [ ] + space */
2978 cabb4cfd 2023-06-01 thomas refstr_cols = 0;
2979 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2980 05171be4 2022-06-23 thomas if (err)
2981 05171be4 2022-06-23 thomas goto done;
2982 05171be4 2022-06-23 thomas msg = msg0;
2983 05171be4 2022-06-23 thomas while (*msg == '\n')
2985 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2986 331b1a16 2022-06-23 thomas *eol = '\0';
2987 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2988 cabb4cfd 2023-06-01 thomas date_display_cols + author_cols + refstr_cols, 0);
2989 331b1a16 2022-06-23 thomas if (err)
2990 331b1a16 2022-06-23 thomas goto done;
2991 cabb4cfd 2023-06-01 thomas view->maxx = MAX(view->maxx, width + refstr_cols);
2992 05171be4 2022-06-23 thomas free(msg0);
2993 331b1a16 2022-06-23 thomas free(wmsg);
2994 7ca04879 2019-10-19 stsp ncommits++;
2995 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2998 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2999 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
3000 867c6645 2018-07-10 stsp ncommits = 0;
3001 899d86c2 2018-05-10 stsp while (entry) {
3002 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
3004 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
3005 2814baeb 2018-08-01 stsp wstandout(view->window);
3006 349dfd1e 2023-07-23 thomas err = draw_commit(view, entry, date_display_cols, author_cols);
3007 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
3008 2814baeb 2018-08-01 stsp wstandend(view->window);
3010 60493ae3 2019-06-20 stsp goto done;
3011 0553a4e3 2018-04-30 stsp ncommits++;
3012 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
3013 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
3016 a5d43cac 2022-07-01 thomas view_border(view);
3018 1a76625f 2018-10-22 stsp free(id_str);
3019 8b473291 2019-02-21 stsp free(refs_str);
3020 1a76625f 2018-10-22 stsp free(ncommits_str);
3021 1a76625f 2018-10-22 stsp free(header);
3022 80ddbec8 2018-04-29 stsp return err;
3025 07b55e75 2018-05-10 stsp static void
3026 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
3028 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
3029 07b55e75 2018-05-10 stsp int nscrolled = 0;
3031 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
3032 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
3035 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
3036 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
3037 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3038 07b55e75 2018-05-10 stsp if (entry) {
3039 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
3040 07b55e75 2018-05-10 stsp nscrolled++;
3045 aa075928 2018-05-10 stsp static const struct got_error *
3046 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
3048 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
3049 5e224a3e 2019-02-22 stsp int errcode;
3051 557d3365 2023-04-14 thomas if (!using_mock_io)
3052 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while loading commits */
3054 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
3055 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
3056 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
3057 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
3058 7aafa0d1 2019-02-22 stsp if (errcode)
3059 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3060 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3063 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
3064 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
3066 7c1452c1 2020-03-26 stsp if (!wait)
3069 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
3070 ffe38506 2020-12-01 naddy show_log_view(view);
3071 7c1452c1 2020-03-26 stsp update_panels();
3072 7c1452c1 2020-03-26 stsp doupdate();
3074 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
3075 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3076 82954512 2020-02-03 stsp if (errcode)
3077 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
3078 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3080 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
3081 ffe38506 2020-12-01 naddy show_log_view(view);
3082 7c1452c1 2020-03-26 stsp update_panels();
3083 7c1452c1 2020-03-26 stsp doupdate();
3086 5e224a3e 2019-02-22 stsp return NULL;
3089 a5d43cac 2022-07-01 thomas static const struct got_error *
3090 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
3092 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
3093 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3095 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
3096 fe731b51 2022-07-14 thomas return NULL;
3098 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
3099 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
3100 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3102 a5d43cac 2022-07-01 thomas return err;
3105 5e224a3e 2019-02-22 stsp static const struct got_error *
3106 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
3108 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
3109 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
3110 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
3111 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
3113 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
3114 5e224a3e 2019-02-22 stsp return NULL;
3116 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3117 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
3118 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
3120 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
3122 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
3123 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
3124 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3126 5e224a3e 2019-02-22 stsp return err;
3130 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3131 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3134 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
3135 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
3137 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3138 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
3140 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
3141 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
3143 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3144 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
3146 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3148 dd0a52c1 2018-05-20 stsp return err;
3151 cd0acaa7 2018-05-20 stsp static const struct got_error *
3152 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3153 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
3154 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3156 cd0acaa7 2018-05-20 stsp const struct got_error *err;
3157 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
3158 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3160 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3161 15a94983 2018-12-23 stsp if (diff_view == NULL)
3162 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3164 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3165 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3166 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3167 e5a0f69f 2018-08-18 stsp if (err == NULL)
3168 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3169 cd0acaa7 2018-05-20 stsp return err;
3172 80ddbec8 2018-04-29 stsp static const struct got_error *
3173 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3174 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3176 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3178 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3179 941e9f74 2019-05-21 stsp if (parent == NULL)
3180 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3182 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3183 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3184 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3185 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3186 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3187 941e9f74 2019-05-21 stsp s->tree = subtree;
3188 941e9f74 2019-05-21 stsp s->selected = 0;
3189 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3190 941e9f74 2019-05-21 stsp return NULL;
3193 941e9f74 2019-05-21 stsp static const struct got_error *
3194 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3195 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
3197 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3198 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3199 941e9f74 2019-05-21 stsp const char *p;
3200 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3202 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3204 941e9f74 2019-05-21 stsp while (*p) {
3205 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3206 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3207 56e0773d 2019-11-28 stsp char *te_name;
3209 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3212 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3213 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3214 941e9f74 2019-05-21 stsp if (slash == NULL)
3215 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3217 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3218 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3219 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3222 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3223 56e0773d 2019-11-28 stsp if (te == NULL) {
3224 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3225 56e0773d 2019-11-28 stsp free(te_name);
3228 56e0773d 2019-11-28 stsp free(te_name);
3229 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3231 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3232 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3234 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3235 941e9f74 2019-05-21 stsp if (slash)
3236 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3238 941e9f74 2019-05-21 stsp subpath = strdup(path);
3239 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3240 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3244 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
3249 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3250 941e9f74 2019-05-21 stsp free(tree_id);
3254 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3255 941e9f74 2019-05-21 stsp if (err) {
3256 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3259 941e9f74 2019-05-21 stsp if (slash == NULL)
3261 941e9f74 2019-05-21 stsp free(subpath);
3262 941e9f74 2019-05-21 stsp subpath = NULL;
3263 941e9f74 2019-05-21 stsp p = slash;
3266 941e9f74 2019-05-21 stsp free(subpath);
3267 1a76625f 2018-10-22 stsp return err;
3270 61266923 2020-01-14 stsp static const struct got_error *
3271 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3272 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3273 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3275 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3276 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3277 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3279 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3280 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3281 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3283 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3285 55cccc34 2020-02-20 stsp return err;
3286 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3288 55cccc34 2020-02-20 stsp *new_view = tree_view;
3290 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3291 55cccc34 2020-02-20 stsp return NULL;
3293 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
3296 55cccc34 2020-02-20 stsp static const struct got_error *
3297 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3299 61266923 2020-01-14 stsp sigset_t sigset;
3300 61266923 2020-01-14 stsp int errcode;
3302 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3303 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3305 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3306 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3307 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3308 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3309 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3310 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
3311 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3312 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
3313 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3315 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3316 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3317 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3319 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3320 61266923 2020-01-14 stsp if (errcode)
3321 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3323 61266923 2020-01-14 stsp return NULL;
3326 1a76625f 2018-10-22 stsp static void *
3327 1a76625f 2018-10-22 stsp log_thread(void *arg)
3329 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3330 1a76625f 2018-10-22 stsp int errcode = 0;
3331 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3332 1a76625f 2018-10-22 stsp int done = 0;
3335 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
3336 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
3338 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
3339 f2d749db 2022-07-12 thomas if (errcode) {
3340 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
3341 61266923 2020-01-14 stsp return (void *)err;
3344 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
3345 f2d749db 2022-07-12 thomas if (err) {
3346 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3347 f2d749db 2022-07-12 thomas goto done;
3350 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3351 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3352 f2d749db 2022-07-12 thomas if (errcode) {
3353 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3354 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3355 f2d749db 2022-07-12 thomas goto done;
3357 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3358 1a76625f 2018-10-22 stsp if (err) {
3359 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3360 f2d749db 2022-07-12 thomas goto done;
3361 1a76625f 2018-10-22 stsp err = NULL;
3363 92845f09 2023-07-26 thomas a->commits_needed = 0;
3364 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3365 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3366 7e8004ba 2022-09-11 thomas if (a->limit_match)
3367 7e8004ba 2022-09-11 thomas a->commits_needed--;
3369 7e8004ba 2022-09-11 thomas a->commits_needed--;
3372 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3373 3abe8080 2019-04-10 stsp if (errcode) {
3374 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3375 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3376 f2d749db 2022-07-12 thomas goto done;
3377 3abe8080 2019-04-10 stsp } else if (*a->quit)
3379 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3380 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3381 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3382 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3383 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3384 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3385 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3386 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3389 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3390 7c1452c1 2020-03-26 stsp if (errcode) {
3391 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3392 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3393 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3394 f2d749db 2022-07-12 thomas goto done;
3397 92845f09 2023-07-26 thomas if (a->commits_needed == 0 &&
3398 92845f09 2023-07-26 thomas a->need_commit_marker && a->worktree) {
3399 92845f09 2023-07-26 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3400 92845f09 2023-07-26 thomas if (errcode) {
3401 92845f09 2023-07-26 thomas err = got_error_set_errno(errcode,
3402 92845f09 2023-07-26 thomas "pthread_mutex_unlock");
3403 92845f09 2023-07-26 thomas goto done;
3405 92845f09 2023-07-26 thomas err = got_worktree_get_state(&tog_base_commit.marker,
3406 6ebb2263 2023-07-26 thomas a->repo, a->worktree, NULL, NULL);
3407 92845f09 2023-07-26 thomas if (err)
3408 92845f09 2023-07-26 thomas goto done;
3409 92845f09 2023-07-26 thomas errcode = pthread_mutex_lock(&tog_mutex);
3410 92845f09 2023-07-26 thomas if (errcode) {
3411 92845f09 2023-07-26 thomas err = got_error_set_errno(errcode,
3412 92845f09 2023-07-26 thomas "pthread_mutex_lock");
3413 92845f09 2023-07-26 thomas goto done;
3415 92845f09 2023-07-26 thomas a->need_commit_marker = 0;
3417 92845f09 2023-07-26 thomas * The main thread did not close this
3418 92845f09 2023-07-26 thomas * work tree yet. Close it now.
3420 92845f09 2023-07-26 thomas got_worktree_close(a->worktree);
3421 92845f09 2023-07-26 thomas a->worktree = NULL;
3423 92845f09 2023-07-26 thomas if (*a->quit)
3424 92845f09 2023-07-26 thomas done = 1;
3428 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3430 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3431 92845f09 2023-07-26 thomas if (tog_io.wait_for_ui) {
3432 92845f09 2023-07-26 thomas errcode = pthread_cond_signal(
3433 92845f09 2023-07-26 thomas &a->log_loaded);
3434 92845f09 2023-07-26 thomas if (errcode && err == NULL)
3435 92845f09 2023-07-26 thomas err = got_error_set_errno(
3436 92845f09 2023-07-26 thomas errcode,
3437 92845f09 2023-07-26 thomas "pthread_cond_signal");
3440 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3441 7c1452c1 2020-03-26 stsp &tog_mutex);
3442 f2d749db 2022-07-12 thomas if (errcode) {
3443 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3444 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3445 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3446 f2d749db 2022-07-12 thomas goto done;
3448 21355643 2020-12-06 stsp if (*a->quit)
3453 3abe8080 2019-04-10 stsp a->log_complete = 1;
3454 92845f09 2023-07-26 thomas if (tog_io.wait_for_ui) {
3455 92845f09 2023-07-26 thomas errcode = pthread_cond_signal(&a->log_loaded);
3456 92845f09 2023-07-26 thomas if (errcode && err == NULL)
3457 92845f09 2023-07-26 thomas err = got_error_set_errno(errcode,
3458 92845f09 2023-07-26 thomas "pthread_cond_signal");
3461 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3462 f2d749db 2022-07-12 thomas if (errcode)
3463 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3465 f2d749db 2022-07-12 thomas if (err) {
3466 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3467 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3468 92845f09 2023-07-26 thomas if (a->worktree) {
3469 92845f09 2023-07-26 thomas got_worktree_close(a->worktree);
3470 92845f09 2023-07-26 thomas a->worktree = NULL;
3473 1a76625f 2018-10-22 stsp return (void *)err;
3476 1a76625f 2018-10-22 stsp static const struct got_error *
3477 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3479 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3480 1a76625f 2018-10-22 stsp int errcode;
3482 1a76625f 2018-10-22 stsp if (s->thread) {
3483 1a76625f 2018-10-22 stsp s->quit = 1;
3484 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3485 1a76625f 2018-10-22 stsp if (errcode)
3486 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3487 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3488 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3489 1a76625f 2018-10-22 stsp if (errcode)
3490 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3491 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3492 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3493 1a76625f 2018-10-22 stsp if (errcode)
3494 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3495 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3496 1a76625f 2018-10-22 stsp if (errcode)
3497 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3498 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3499 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3502 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3503 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3504 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3507 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3508 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3509 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3510 1af5eddf 2022-06-23 thomas if (err == NULL)
3511 1af5eddf 2022-06-23 thomas err = pack_err;
3512 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3515 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3516 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3517 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3520 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3523 9343a5fb 2018-06-23 stsp static const struct got_error *
3524 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3526 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3527 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3528 276b94a1 2020-11-13 naddy int errcode;
3530 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3532 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3533 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3534 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3536 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3537 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3538 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3540 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3541 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3542 450db6a5 2023-12-19 thomas free_colors(&s->colors);
3543 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3544 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3545 1a76625f 2018-10-22 stsp free(s->start_id);
3546 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3547 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3548 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3549 1a76625f 2018-10-22 stsp return err;
3553 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3554 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3555 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3556 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3557 7e8004ba 2022-09-11 thomas * works with very slight change.
3559 1a76625f 2018-10-22 stsp static const struct got_error *
3560 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3562 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3563 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3564 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3565 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3566 7e8004ba 2022-09-11 thomas char pattern[1024];
3567 7e8004ba 2022-09-11 thomas int ret;
3569 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3570 7e8004ba 2022-09-11 thomas v = view->child;
3571 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3572 7e8004ba 2022-09-11 thomas v = view->parent;
3574 1fc091f3 2023-09-05 thomas if (tog_io.input_str != NULL) {
3575 1fc091f3 2023-09-05 thomas if (strlcpy(pattern, tog_io.input_str, sizeof(pattern)) >=
3576 1fc091f3 2023-09-05 thomas sizeof(pattern))
3577 1fc091f3 2023-09-05 thomas return got_error(GOT_ERR_NO_SPACE);
3578 1fc091f3 2023-09-05 thomas } else {
3579 1fc091f3 2023-09-05 thomas wmove(v->window, v->nlines - 1, 0);
3580 1fc091f3 2023-09-05 thomas wclrtoeol(v->window);
3581 1fc091f3 2023-09-05 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3582 1fc091f3 2023-09-05 thomas nodelay(v->window, FALSE);
3583 1fc091f3 2023-09-05 thomas nocbreak();
3585 1fc091f3 2023-09-05 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3586 1fc091f3 2023-09-05 thomas cbreak();
3587 1fc091f3 2023-09-05 thomas noecho();
3588 1fc091f3 2023-09-05 thomas nodelay(v->window, TRUE);
3589 1fc091f3 2023-09-05 thomas if (ret == ERR)
3590 1fc091f3 2023-09-05 thomas return NULL;
3593 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3595 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3596 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3598 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3599 7e8004ba 2022-09-11 thomas return NULL;
3602 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3603 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3604 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3605 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3606 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3607 7e8004ba 2022-09-11 thomas * the queue.
3609 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3610 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3611 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3612 7e8004ba 2022-09-11 thomas s->selected = 0;
3613 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3615 7e8004ba 2022-09-11 thomas return NULL;
3618 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3619 7e8004ba 2022-09-11 thomas return NULL;
3621 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3623 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3624 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3625 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3626 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3627 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3629 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3630 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3631 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3633 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3634 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3635 7e8004ba 2022-09-11 thomas int have_match = 0;
3637 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3638 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3639 7e8004ba 2022-09-11 thomas if (err)
3640 7e8004ba 2022-09-11 thomas return err;
3642 7e8004ba 2022-09-11 thomas if (have_match) {
3643 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3645 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3646 7e8004ba 2022-09-11 thomas entry->id);
3647 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3648 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3649 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3652 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3653 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3655 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3656 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3657 7e8004ba 2022-09-11 thomas matched, entry);
3658 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3662 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3663 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3664 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3665 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3666 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3667 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3668 7e8004ba 2022-09-11 thomas if (err)
3669 7e8004ba 2022-09-11 thomas return err;
3672 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3673 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3674 7e8004ba 2022-09-11 thomas s->selected = 0;
3676 7e8004ba 2022-09-11 thomas return NULL;
3679 7e8004ba 2022-09-11 thomas static const struct got_error *
3680 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3682 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3684 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3685 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3686 60493ae3 2019-06-20 stsp return NULL;
3689 60493ae3 2019-06-20 stsp static const struct got_error *
3690 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3692 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3693 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3694 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3696 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3697 f9686aa5 2020-03-27 stsp show_log_view(view);
3698 f9686aa5 2020-03-27 stsp update_panels();
3699 f9686aa5 2020-03-27 stsp doupdate();
3701 96e2b566 2019-07-08 stsp if (s->search_entry) {
3702 1fc091f3 2023-09-05 thomas if (!using_mock_io) {
3703 1fc091f3 2023-09-05 thomas int errcode, ch;
3705 1fc091f3 2023-09-05 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3706 1fc091f3 2023-09-05 thomas if (errcode)
3707 1fc091f3 2023-09-05 thomas return got_error_set_errno(errcode,
3708 1fc091f3 2023-09-05 thomas "pthread_mutex_unlock");
3709 1fc091f3 2023-09-05 thomas ch = wgetch(view->window);
3710 1fc091f3 2023-09-05 thomas errcode = pthread_mutex_lock(&tog_mutex);
3711 1fc091f3 2023-09-05 thomas if (errcode)
3712 1fc091f3 2023-09-05 thomas return got_error_set_errno(errcode,
3713 1fc091f3 2023-09-05 thomas "pthread_mutex_lock");
3714 1fc091f3 2023-09-05 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3715 1fc091f3 2023-09-05 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
3716 1fc091f3 2023-09-05 thomas return NULL;
3719 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3720 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3722 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3723 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3724 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3726 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3727 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3728 1f4d5162 2022-06-23 thomas * might have changed.
3730 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3731 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3733 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3736 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3739 60493ae3 2019-06-20 stsp while (1) {
3740 13add988 2019-10-15 stsp int have_match = 0;
3742 60493ae3 2019-06-20 stsp if (entry == NULL) {
3743 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3744 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3745 f9967bca 2020-03-27 stsp view->search_next_done =
3746 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3747 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3748 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3749 f801134a 2019-06-25 stsp return NULL;
3752 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3753 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3754 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3756 49dff0bd 2023-08-12 thomas s->search_entry = s->selected_entry;
3757 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3758 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3761 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3762 13add988 2019-10-15 stsp &view->regex);
3765 13add988 2019-10-15 stsp if (have_match) {
3766 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3767 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3771 96e2b566 2019-07-08 stsp s->search_entry = entry;
3772 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3773 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3775 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3778 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3779 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3780 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3781 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3783 60493ae3 2019-06-20 stsp return err;
3786 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3787 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3789 60493ae3 2019-06-20 stsp return err;
3794 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3796 60493ae3 2019-06-20 stsp return NULL;
3799 60493ae3 2019-06-20 stsp static const struct got_error *
3800 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3801 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3802 92845f09 2023-07-26 thomas const char *in_repo_path, int log_branches,
3803 92845f09 2023-07-26 thomas struct got_worktree *worktree)
3805 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3806 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3807 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3808 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3809 1a76625f 2018-10-22 stsp int errcode;
3811 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3812 f135c941 2020-02-20 stsp free(s->in_repo_path);
3813 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3814 51b7e1c3 2023-04-14 thomas if (s->in_repo_path == NULL) {
3815 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("strdup");
3816 51b7e1c3 2023-04-14 thomas goto done;
3820 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3821 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3822 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3823 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3825 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3826 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3827 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3829 fb2756b9 2018-08-04 stsp s->repo = repo;
3830 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3831 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3832 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3833 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3834 9cd7cbd1 2020-12-07 stsp goto done;
3837 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3838 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3839 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3840 5036bf37 2018-09-24 stsp goto done;
3842 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3843 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3845 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3846 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3847 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3848 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3850 11b20872 2019-11-08 stsp goto done;
3851 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3852 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3853 450db6a5 2023-12-19 thomas if (err)
3854 11b20872 2019-11-08 stsp goto done;
3855 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3856 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3857 450db6a5 2023-12-19 thomas if (err)
3858 11b20872 2019-11-08 stsp goto done;
3861 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3862 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3863 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3864 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3865 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3866 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3868 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3869 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3870 1af5eddf 2022-06-23 thomas if (err)
3871 1af5eddf 2022-06-23 thomas goto done;
3873 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3874 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3875 7cd52833 2022-06-23 thomas if (err)
3876 7cd52833 2022-06-23 thomas goto done;
3877 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3878 b672a97a 2020-01-27 stsp !s->log_branches);
3880 1a76625f 2018-10-22 stsp goto done;
3881 0279329d 2024-03-30 thomas err = got_commit_graph_bfsort(thread_graph, s->start_id,
3882 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3884 c5b78334 2020-01-12 stsp goto done;
3886 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3887 1a76625f 2018-10-22 stsp if (errcode) {
3888 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3889 1a76625f 2018-10-22 stsp goto done;
3891 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3892 7c1452c1 2020-03-26 stsp if (errcode) {
3893 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3894 7c1452c1 2020-03-26 stsp goto done;
3897 92845f09 2023-07-26 thomas if (using_mock_io) {
3900 92845f09 2023-07-26 thomas rc = pthread_cond_init(&s->thread_args.log_loaded, NULL);
3902 92845f09 2023-07-26 thomas return got_error_set_errno(rc, "pthread_cond_init");
3905 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3906 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3907 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3908 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3909 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3910 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3911 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3912 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3913 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3914 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3915 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3916 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3917 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3918 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3919 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3920 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3921 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3922 92845f09 2023-07-26 thomas s->thread_args.worktree = worktree;
3923 92845f09 2023-07-26 thomas if (worktree)
3924 92845f09 2023-07-26 thomas s->thread_args.need_commit_marker = 1;
3926 51b7e1c3 2023-04-14 thomas if (err) {
3927 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
3928 51b7e1c3 2023-04-14 thomas close_log_view(view);
3929 51b7e1c3 2023-04-14 thomas view_close(view);
3931 ba4f502b 2018-08-04 stsp return err;
3934 e5a0f69f 2018-08-18 stsp static const struct got_error *
3935 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3937 f2f6d207 2020-11-24 stsp const struct got_error *err;
3938 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3940 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3941 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3942 2b380cc8 2018-10-24 stsp &s->thread_args);
3943 2b380cc8 2018-10-24 stsp if (errcode)
3944 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3945 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3946 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3948 f2f6d207 2020-11-24 stsp return err;
3952 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3955 b31f89ff 2022-07-01 thomas static void
3956 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3958 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3960 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3962 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3963 7e8004ba 2022-09-11 thomas view->count = 0;
3965 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3966 b31f89ff 2022-07-01 thomas || home)
3967 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3969 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3970 b31f89ff 2022-07-01 thomas --s->selected;
3972 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3974 b31f89ff 2022-07-01 thomas select_commit(s);
3978 b31f89ff 2022-07-01 thomas static const struct got_error *
3979 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3981 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3982 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3983 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3985 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3986 7e8004ba 2022-09-11 thomas return NULL;
3988 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3989 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3990 b31f89ff 2022-07-01 thomas return NULL;
3992 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3993 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3995 7ed048bd 2022-08-12 thomas if (!page) {
3996 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3997 b31f89ff 2022-07-01 thomas ++s->selected;
3999 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
4000 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
4001 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
4004 7ed048bd 2022-08-12 thomas s->selected = 0;
4005 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
4006 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
4007 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
4008 7ed048bd 2022-08-12 thomas if (entry == NULL)
4010 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
4011 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
4013 7ed048bd 2022-08-12 thomas if (n > 0)
4014 7ed048bd 2022-08-12 thomas s->selected = n - 1;
4015 b31f89ff 2022-07-01 thomas } else {
4016 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
4017 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
4018 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
4019 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
4021 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
4023 b31f89ff 2022-07-01 thomas if (err)
4024 b31f89ff 2022-07-01 thomas return err;
4027 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
4028 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
4030 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
4031 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
4032 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
4033 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
4036 b31f89ff 2022-07-01 thomas select_commit(s);
4038 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
4039 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
4040 b31f89ff 2022-07-01 thomas view->count = 0;
4042 b31f89ff 2022-07-01 thomas return NULL;
4045 a5d43cac 2022-07-01 thomas static void
4046 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
4051 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
4052 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
4053 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
4054 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
4055 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
4057 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
4058 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
4059 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
4060 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
4061 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
4062 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
4064 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
4068 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
4069 e5a0f69f 2018-08-18 stsp static const struct got_error *
4070 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
4072 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
4074 a5d43cac 2022-07-01 thomas view->nlines = y;
4075 64486692 2022-07-07 thomas view->ncols = COLS;
4076 a5d43cac 2022-07-01 thomas err = view_resize(view);
4077 a5d43cac 2022-07-01 thomas if (err)
4078 a5d43cac 2022-07-01 thomas return err;
4080 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
4082 a5d43cac 2022-07-01 thomas return err;
4085 07dd3ed3 2022-08-06 thomas static const struct got_error *
4086 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
4088 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
4089 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
4090 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
4092 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
4093 25100026 2022-08-13 thomas return NULL;
4095 07dd3ed3 2022-08-06 thomas g = view->gline;
4096 07dd3ed3 2022-08-06 thomas view->gline = 0;
4098 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
4099 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
4100 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
4101 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
4102 07dd3ed3 2022-08-06 thomas select_commit(s);
4103 07dd3ed3 2022-08-06 thomas return NULL;
4106 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
4107 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
4108 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
4109 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
4110 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
4111 07dd3ed3 2022-08-06 thomas if (err)
4112 07dd3ed3 2022-08-06 thomas return err;
4113 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
4114 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
4116 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
4117 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
4119 07dd3ed3 2022-08-06 thomas select_commit(s);
4120 07dd3ed3 2022-08-06 thomas return NULL;
4124 c72de8ab 2023-02-03 thomas static void
4125 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
4128 c72de8ab 2023-02-03 thomas switch (ch) {
4129 c72de8ab 2023-02-03 thomas case KEY_LEFT:
4130 c72de8ab 2023-02-03 thomas case 'h':
4131 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
4132 c72de8ab 2023-02-03 thomas if (view->x <= 0)
4133 c72de8ab 2023-02-03 thomas view->count = 0;
4135 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
4136 c72de8ab 2023-02-03 thomas case 'l':
4137 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
4138 c72de8ab 2023-02-03 thomas view->x += 2;
4140 c72de8ab 2023-02-03 thomas view->count = 0;
4142 c72de8ab 2023-02-03 thomas case '0':
4143 c72de8ab 2023-02-03 thomas view->x = 0;
4145 c72de8ab 2023-02-03 thomas case '$':
4146 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
4147 c72de8ab 2023-02-03 thomas view->count = 0;
4149 c72de8ab 2023-02-03 thomas default:
4154 a5d43cac 2022-07-01 thomas static const struct got_error *
4155 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4157 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4158 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
4159 7ed048bd 2022-08-12 thomas int eos, nscroll;
4161 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
4162 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4163 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
4164 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
4165 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4166 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
4168 c0be8933 2022-08-12 thomas if (err)
4169 c0be8933 2022-08-12 thomas return err;
4172 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
4173 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
4174 068ab281 2022-07-01 thomas --eos; /* border */
4176 07dd3ed3 2022-08-06 thomas if (view->gline)
4177 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
4179 528dedf3 2021-08-30 stsp switch (ch) {
4180 7e8004ba 2022-09-11 thomas case '&':
4181 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
4184 1e37a5c2 2019-05-12 jcs s->quit = 1;
4186 05171be4 2022-06-23 thomas case '0':
4187 05171be4 2022-06-23 thomas case '$':
4188 05171be4 2022-06-23 thomas case KEY_RIGHT:
4189 05171be4 2022-06-23 thomas case 'l':
4190 05171be4 2022-06-23 thomas case KEY_LEFT:
4191 05171be4 2022-06-23 thomas case 'h':
4192 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
4195 1e37a5c2 2019-05-12 jcs case KEY_UP:
4198 f7140bf5 2021-10-17 thomas case CTRL('p'):
4199 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
4202 aa7a1117 2023-01-09 thomas case '=':
4203 912a3f79 2021-08-30 j case KEY_HOME:
4204 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
4205 07b0611c 2022-06-23 thomas view->count = 0;
4207 70f17a53 2022-06-13 thomas case CTRL('u'):
4208 23427b14 2022-06-23 thomas case 'u':
4209 70f17a53 2022-06-13 thomas nscroll /= 2;
4210 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4211 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4212 a4292ac5 2019-05-12 jcs case CTRL('b'):
4213 1c5e5faa 2022-06-23 thomas case 'b':
4214 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
4217 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4220 f7140bf5 2021-10-17 thomas case CTRL('n'):
4221 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
4223 f69c5a46 2022-07-19 thomas case '@':
4224 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
4225 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
4226 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
4229 aa7a1117 2023-01-09 thomas case '*':
4230 912a3f79 2021-08-30 j case KEY_END: {
4231 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4232 912a3f79 2021-08-30 j * traverse them all. */
4233 07b0611c 2022-06-23 thomas view->count = 0;
4234 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
4235 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
4236 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4237 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4238 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
4241 bccd1d5d 2022-06-13 thomas case CTRL('d'):
4242 23427b14 2022-06-23 thomas case 'd':
4243 70f17a53 2022-06-13 thomas nscroll /= 2;
4244 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4245 70f17a53 2022-06-13 thomas case KEY_NPAGE:
4246 1c5e5faa 2022-06-23 thomas case CTRL('f'):
4247 4c2d69cb 2022-06-23 thomas case 'f':
4248 b31f89ff 2022-07-01 thomas case ' ':
4249 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
4251 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4252 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4253 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4254 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
4255 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
4256 2b779855 2020-12-05 naddy select_commit(s);
4257 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
4258 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4259 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4260 7e8004ba 2022-09-11 thomas s->commits->ncommits;
4261 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4264 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4265 444d5325 2022-07-03 thomas case '\r':
4266 07b0611c 2022-06-23 thomas view->count = 0;
4267 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4269 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4271 1be4947a 2022-07-22 thomas case 'T':
4272 07b0611c 2022-06-23 thomas view->count = 0;
4273 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4275 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
4277 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4278 21355643 2020-12-06 stsp case CTRL('l'):
4280 07b0611c 2022-06-23 thomas view->count = 0;
4281 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4282 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4284 21355643 2020-12-06 stsp err = stop_log_thread(s);
4286 74cfe85e 2020-10-20 stsp return err;
4287 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4288 21355643 2020-12-06 stsp char *parent_path;
4289 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4291 21355643 2020-12-06 stsp return err;
4292 21355643 2020-12-06 stsp free(s->in_repo_path);
4293 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4294 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4295 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4296 21355643 2020-12-06 stsp struct got_object_id *start_id;
4297 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4298 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4299 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4300 466429a1 2022-11-17 thomas if (err) {
4301 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
4302 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
4303 466429a1 2022-11-17 thomas return err;
4304 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
4305 466429a1 2022-11-17 thomas free(s->head_ref_name);
4306 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
4307 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
4308 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4309 466429a1 2022-11-17 thomas &tog_refs, s->repo);
4310 466429a1 2022-11-17 thomas if (err)
4311 466429a1 2022-11-17 thomas return err;
4313 21355643 2020-12-06 stsp free(s->start_id);
4314 21355643 2020-12-06 stsp s->start_id = start_id;
4315 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4316 21355643 2020-12-06 stsp } else /* 'B' */
4317 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4319 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
4320 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4321 bf7e79b3 2022-06-23 thomas if (err)
4322 bf7e79b3 2022-06-23 thomas return err;
4324 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
4325 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
4326 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
4328 21355643 2020-12-06 stsp return err;
4329 51a10b52 2020-12-26 stsp tog_free_refs();
4330 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
4332 ca51c541 2020-12-07 stsp return err;
4333 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4334 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4336 d01904d4 2019-06-25 stsp return err;
4337 0279329d 2024-03-30 thomas err = got_commit_graph_bfsort(s->thread_args.graph,
4338 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4340 b672a97a 2020-01-27 stsp return err;
4341 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
4342 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
4343 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4344 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4345 21355643 2020-12-06 stsp s->selected_entry = NULL;
4346 21355643 2020-12-06 stsp s->selected = 0;
4347 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4348 21355643 2020-12-06 stsp s->quit = 0;
4349 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
4350 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
4351 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
4352 94ecf40d 2022-07-22 thomas view->offset = 0;
4354 1be4947a 2022-07-22 thomas case 'R':
4355 07b0611c 2022-06-23 thomas view->count = 0;
4356 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
4359 07b0611c 2022-06-23 thomas view->count = 0;
4363 80ddbec8 2018-04-29 stsp return err;
4366 4ed7e80c 2018-05-20 stsp static const struct got_error *
4367 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4369 c2db6724 2019-01-04 stsp const struct got_error *error;
4371 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4372 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4373 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4375 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4376 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4378 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4379 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4381 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4382 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4384 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4385 c2db6724 2019-01-04 stsp if (error != NULL)
4386 c2db6724 2019-01-04 stsp return error;
4388 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4389 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4391 c2db6724 2019-01-04 stsp return NULL;
4394 b85a3496 2023-04-14 thomas static const struct got_error *
4395 557d3365 2023-04-14 thomas init_mock_term(const char *test_script_path)
4397 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4398 5b3a801d 2023-04-22 thomas const char *screen_dump_path;
4401 b85a3496 2023-04-14 thomas if (test_script_path == NULL || *test_script_path == '\0')
4402 fa9bb690 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4404 557d3365 2023-04-14 thomas tog_io.f = fopen(test_script_path, "re");
4405 557d3365 2023-04-14 thomas if (tog_io.f == NULL) {
4406 b85a3496 2023-04-14 thomas err = got_error_from_errno_fmt("fopen: %s",
4407 b85a3496 2023-04-14 thomas test_script_path);
4408 b85a3496 2023-04-14 thomas goto done;
4411 b85a3496 2023-04-14 thomas /* test mode, we don't want any output */
4412 557d3365 2023-04-14 thomas tog_io.cout = fopen("/dev/null", "w+");
4413 557d3365 2023-04-14 thomas if (tog_io.cout == NULL) {
4414 f4086c71 2023-04-22 thomas err = got_error_from_errno2("fopen", "/dev/null");
4415 b85a3496 2023-04-14 thomas goto done;
4418 f4086c71 2023-04-22 thomas in = dup(fileno(tog_io.cout));
4419 f4086c71 2023-04-22 thomas if (in == -1) {
4420 f4086c71 2023-04-22 thomas err = got_error_from_errno("dup");
4421 f4086c71 2023-04-22 thomas goto done;
4423 f4086c71 2023-04-22 thomas tog_io.cin = fdopen(in, "r");
4424 557d3365 2023-04-14 thomas if (tog_io.cin == NULL) {
4425 f4086c71 2023-04-22 thomas err = got_error_from_errno("fdopen");
4426 f4086c71 2023-04-22 thomas close(in);
4427 b85a3496 2023-04-14 thomas goto done;
4430 5b3a801d 2023-04-22 thomas screen_dump_path = getenv("TOG_SCR_DUMP");
4431 5b3a801d 2023-04-22 thomas if (screen_dump_path == NULL || *screen_dump_path == '\0')
4432 5b3a801d 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4433 66b04f8f 2023-07-19 thomas tog_io.sdump = fopen(screen_dump_path, "we");
4434 5b3a801d 2023-04-22 thomas if (tog_io.sdump == NULL) {
4435 5b3a801d 2023-04-22 thomas err = got_error_from_errno2("fopen", screen_dump_path);
4436 5b3a801d 2023-04-22 thomas goto done;
4439 557d3365 2023-04-14 thomas if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4440 b85a3496 2023-04-14 thomas err = got_error_from_errno("fseeko");
4441 b85a3496 2023-04-14 thomas goto done;
4444 557d3365 2023-04-14 thomas if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4445 b85a3496 2023-04-14 thomas err = got_error_msg(GOT_ERR_IO,
4446 b85a3496 2023-04-14 thomas "newterm: failed to initialise curses");
4448 557d3365 2023-04-14 thomas using_mock_io = 1;
4451 b85a3496 2023-04-14 thomas if (err)
4452 557d3365 2023-04-14 thomas tog_io_close();
4453 b85a3496 2023-04-14 thomas return err;
4456 557d3365 2023-04-14 thomas static void
4457 557d3365 2023-04-14 thomas init_curses(void)
4459 557d3365 2023-04-14 thomas if (using_mock_io) /* In test mode we use a fake terminal */
4462 557d3365 2023-04-14 thomas initscr();
4465 557d3365 2023-04-14 thomas halfdelay(1); /* Fast refresh while initial view is loading. */
4468 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4469 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4470 a915003a 2019-02-05 stsp curs_set(0);
4471 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4472 6d17833f 2019-11-08 stsp start_color();
4473 6d17833f 2019-11-08 stsp use_default_colors();
4479 c2db6724 2019-01-04 stsp static const struct got_error *
4480 349dfd1e 2023-07-23 thomas set_tog_base_commit(struct got_repository *repo, struct got_worktree *worktree)
4482 349dfd1e 2023-07-23 thomas tog_base_commit.id = got_object_id_dup(
4483 349dfd1e 2023-07-23 thomas got_worktree_get_base_commit_id(worktree));
4484 349dfd1e 2023-07-23 thomas if (tog_base_commit.id == NULL)
4485 349dfd1e 2023-07-23 thomas return got_error_from_errno( "got_object_id_dup");
4487 92845f09 2023-07-26 thomas return NULL;
4490 349dfd1e 2023-07-23 thomas static const struct got_error *
4491 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4492 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4494 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4496 f135c941 2020-02-20 stsp if (argc == 0) {
4497 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4498 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4499 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4500 f135c941 2020-02-20 stsp return NULL;
4503 f135c941 2020-02-20 stsp if (worktree) {
4504 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4507 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4509 f135c941 2020-02-20 stsp return err;
4510 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4511 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4512 bfd61697 2020-11-14 stsp p) == -1) {
4513 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4514 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4518 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4520 f135c941 2020-02-20 stsp return err;
4523 f135c941 2020-02-20 stsp static const struct got_error *
4524 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4526 1d98034b 2023-04-14 thomas const struct got_error *error;
4527 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4528 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4529 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4530 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4531 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4532 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4533 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4534 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4535 04cc582a 2018-08-01 stsp struct tog_view *view;
4536 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4538 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4539 80ddbec8 2018-04-29 stsp switch (ch) {
4541 b672a97a 2020-01-27 stsp log_branches = 1;
4544 80ddbec8 2018-04-29 stsp start_commit = optarg;
4547 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4548 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4549 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4553 17020d27 2019-03-07 stsp usage_log();
4554 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4558 80ddbec8 2018-04-29 stsp argc -= optind;
4559 80ddbec8 2018-04-29 stsp argv += optind;
4561 f135c941 2020-02-20 stsp if (argc > 1)
4562 f135c941 2020-02-20 stsp usage_log();
4564 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4565 7cd52833 2022-06-23 thomas if (error != NULL)
4566 7cd52833 2022-06-23 thomas goto done;
4568 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4569 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4570 5a5defb7 2023-07-23 thomas if (cwd == NULL) {
4571 5a5defb7 2023-07-23 thomas error = got_error_from_errno("getcwd");
4572 5a5defb7 2023-07-23 thomas goto done;
4574 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
4575 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4576 c156c7a4 2020-12-18 stsp goto done;
4577 a1fbf39a 2019-08-11 stsp if (worktree)
4578 6962eb72 2020-02-20 stsp repo_path =
4579 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4581 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4582 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4583 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4584 c156c7a4 2020-12-18 stsp goto done;
4588 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4589 80ddbec8 2018-04-29 stsp if (error != NULL)
4590 ecb28ae0 2018-07-16 stsp goto done;
4592 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4593 f135c941 2020-02-20 stsp repo, worktree);
4594 f135c941 2020-02-20 stsp if (error)
4595 f135c941 2020-02-20 stsp goto done;
4597 557d3365 2023-04-14 thomas init_curses();
4599 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4600 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4601 c02c541e 2019-03-29 stsp if (error)
4602 c02c541e 2019-03-29 stsp goto done;
4604 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4605 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4606 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4607 87670572 2020-12-26 naddy if (error)
4608 87670572 2020-12-26 naddy goto done;
4611 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4612 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4613 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4614 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4615 d8f38dc4 2020-12-05 stsp if (error)
4616 d8f38dc4 2020-12-05 stsp goto done;
4617 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4619 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4620 66b04f8f 2023-07-19 thomas repo, worktree);
4621 66b04f8f 2023-07-19 thomas if (error != NULL)
4622 66b04f8f 2023-07-19 thomas goto done;
4623 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
4624 66b04f8f 2023-07-19 thomas start_commit = keyword_idstr;
4626 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4627 d8f38dc4 2020-12-05 stsp if (error == NULL)
4628 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4629 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4630 d8f38dc4 2020-12-05 stsp goto done;
4631 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4632 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4633 d8f38dc4 2020-12-05 stsp if (error)
4634 d8f38dc4 2020-12-05 stsp goto done;
4637 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4638 04cc582a 2018-08-01 stsp if (view == NULL) {
4639 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4640 04cc582a 2018-08-01 stsp goto done;
4643 2fc00ff4 2019-08-31 stsp if (worktree) {
4644 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
4645 349dfd1e 2023-07-23 thomas if (error != NULL)
4646 349dfd1e 2023-07-23 thomas goto done;
4649 92845f09 2023-07-26 thomas error = open_log_view(view, start_id, repo, head_ref_name,
4650 92845f09 2023-07-26 thomas in_repo_path, log_branches, worktree);
4651 92845f09 2023-07-26 thomas if (error)
4652 92845f09 2023-07-26 thomas goto done;
4654 92845f09 2023-07-26 thomas if (worktree) {
4655 92845f09 2023-07-26 thomas /* The work tree will be closed by the log thread. */
4656 2fc00ff4 2019-08-31 stsp worktree = NULL;
4659 557d3365 2023-04-14 thomas error = view_loop(view);
4662 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
4663 66b04f8f 2023-07-19 thomas free(keyword_idstr);
4664 f135c941 2020-02-20 stsp free(in_repo_path);
4665 ecb28ae0 2018-07-16 stsp free(repo_path);
4666 ecb28ae0 2018-07-16 stsp free(cwd);
4667 899d86c2 2018-05-10 stsp free(start_id);
4668 d8f38dc4 2020-12-05 stsp free(label);
4670 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4671 1d0f4054 2021-06-17 stsp if (repo) {
4672 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4673 1d0f4054 2021-06-17 stsp if (error == NULL)
4674 1d0f4054 2021-06-17 stsp error = close_err;
4676 ec142235 2019-03-07 stsp if (worktree)
4677 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4678 7cd52833 2022-06-23 thomas if (pack_fds) {
4679 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4680 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4681 7cd52833 2022-06-23 thomas if (error == NULL)
4682 7cd52833 2022-06-23 thomas error = pack_err;
4684 51a10b52 2020-12-26 stsp tog_free_refs();
4685 80ddbec8 2018-04-29 stsp return error;
4688 4ed7e80c 2018-05-20 stsp __dead static void
4689 9f7d7167 2018-04-29 stsp usage_diff(void)
4692 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4693 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4697 6d17833f 2019-11-08 stsp static int
4698 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4699 41605754 2020-11-12 stsp regmatch_t *regmatch)
4701 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4704 ef20f542 2022-06-26 thomas static struct tog_color *
4705 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4707 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4709 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4710 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4711 6d17833f 2019-11-08 stsp return tc;
4714 6d17833f 2019-11-08 stsp return NULL;
4717 4ed7e80c 2018-05-20 stsp static const struct got_error *
4718 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4719 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4721 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4722 666f7b10 2022-06-23 thomas char *exstr = NULL;
4723 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4724 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4725 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4726 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4728 41605754 2020-11-12 stsp *wtotal = 0;
4730 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4731 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4733 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4734 666f7b10 2022-06-23 thomas if (err)
4735 666f7b10 2022-06-23 thomas return err;
4737 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4738 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4739 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4740 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4741 666f7b10 2022-06-23 thomas goto done;
4743 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4744 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4745 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4746 cbea3800 2022-06-23 thomas goto done;
4748 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4749 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4750 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4751 cbea3800 2022-06-23 thomas goto done;
4754 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4755 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4756 cbea3800 2022-06-23 thomas col_tab_align, 1);
4757 cbea3800 2022-06-23 thomas if (err)
4758 cbea3800 2022-06-23 thomas goto done;
4759 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4760 05171be4 2022-06-23 thomas if (n) {
4761 cbea3800 2022-06-23 thomas free(wline);
4762 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4763 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4764 cbea3800 2022-06-23 thomas if (err)
4765 cbea3800 2022-06-23 thomas goto done;
4766 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4767 cbea3800 2022-06-23 thomas wlimit -= width;
4768 cbea3800 2022-06-23 thomas *wtotal += width;
4771 41605754 2020-11-12 stsp if (wlimit > 0) {
4772 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4773 cbea3800 2022-06-23 thomas size_t wlen;
4775 cbea3800 2022-06-23 thomas free(wline);
4776 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4777 cbea3800 2022-06-23 thomas col_tab_align, 1);
4778 cbea3800 2022-06-23 thomas if (err)
4779 cbea3800 2022-06-23 thomas goto done;
4780 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4781 cbea3800 2022-06-23 thomas while (i < wlen) {
4782 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4783 cbea3800 2022-06-23 thomas if (width == -1) {
4784 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4785 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4786 cbea3800 2022-06-23 thomas goto done;
4788 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4790 1c5e5faa 2022-06-23 thomas w += width;
4793 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4794 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4795 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4796 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4797 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4798 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4799 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4803 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4804 cbea3800 2022-06-23 thomas free(wline);
4805 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4806 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4807 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4808 cbea3800 2022-06-23 thomas col_tab_align, 1);
4809 cbea3800 2022-06-23 thomas if (err)
4810 cbea3800 2022-06-23 thomas goto done;
4811 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4812 cbea3800 2022-06-23 thomas } else {
4813 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4814 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4815 cbea3800 2022-06-23 thomas if (err)
4816 cbea3800 2022-06-23 thomas goto done;
4817 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4819 cbea3800 2022-06-23 thomas *wtotal += width2;
4822 05171be4 2022-06-23 thomas free(wline);
4823 666f7b10 2022-06-23 thomas free(exstr);
4824 cbea3800 2022-06-23 thomas free(seg0);
4825 cbea3800 2022-06-23 thomas free(seg1);
4826 cbea3800 2022-06-23 thomas free(seg2);
4827 cbea3800 2022-06-23 thomas return err;
4830 07dd3ed3 2022-08-06 thomas static int
4831 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4833 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4834 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4836 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4837 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4839 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4840 fc2737d5 2022-09-15 thomas selected = first;
4841 fc2737d5 2022-09-15 thomas eof = &s->eof;
4842 fc2737d5 2022-09-15 thomas f = s->f;
4843 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4844 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4846 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4847 07dd3ed3 2022-08-06 thomas selected = first;
4848 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4849 07dd3ed3 2022-08-06 thomas f = s->f;
4850 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4851 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4853 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4854 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4855 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4856 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4858 07dd3ed3 2022-08-06 thomas return 0;
4860 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4861 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4862 07dd3ed3 2022-08-06 thomas return 0;
4863 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4864 07dd3ed3 2022-08-06 thomas rewind(f);
4865 07dd3ed3 2022-08-06 thomas *eof = 0;
4866 07dd3ed3 2022-08-06 thomas *first = 1;
4867 07dd3ed3 2022-08-06 thomas *lineno = 0;
4868 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4869 07dd3ed3 2022-08-06 thomas return 0;
4872 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4873 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4874 07dd3ed3 2022-08-06 thomas view->gline = 0;
4876 07dd3ed3 2022-08-06 thomas return 1;
4879 41605754 2020-11-12 stsp static const struct got_error *
4880 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4882 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4883 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4884 61e69b96 2018-05-20 stsp const struct got_error *err;
4885 82c78e96 2022-08-06 thomas int nprinted = 0;
4886 b304db33 2018-05-20 stsp char *line;
4887 826082fe 2020-12-10 stsp size_t linesize = 0;
4888 826082fe 2020-12-10 stsp ssize_t linelen;
4889 61e69b96 2018-05-20 stsp wchar_t *wline;
4890 e0b650dd 2018-05-20 stsp int width;
4891 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4892 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4893 fe621944 2020-11-10 stsp off_t line_offset;
4895 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4896 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4897 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4898 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4900 f7d12f7e 2018-08-01 stsp werase(view->window);
4902 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4903 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4905 a3404814 2018-09-02 stsp if (header) {
4906 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4907 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4908 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4910 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4911 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4912 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4914 135a2da0 2020-11-11 stsp free(line);
4916 a3404814 2018-09-02 stsp return err;
4918 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4919 a3404814 2018-09-02 stsp wstandout(view->window);
4920 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4921 e54cc94a 2020-11-11 stsp free(wline);
4922 e54cc94a 2020-11-11 stsp wline = NULL;
4923 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4924 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4925 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4926 a3404814 2018-09-02 stsp wstandend(view->window);
4928 a3404814 2018-09-02 stsp if (max_lines <= 1)
4929 a3404814 2018-09-02 stsp return NULL;
4930 a3404814 2018-09-02 stsp max_lines--;
4933 89f1a395 2020-12-01 naddy s->eof = 0;
4934 05171be4 2022-06-23 thomas view->maxx = 0;
4935 826082fe 2020-12-10 stsp line = NULL;
4936 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4937 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4938 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4940 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4941 826082fe 2020-12-10 stsp if (linelen == -1) {
4942 826082fe 2020-12-10 stsp if (feof(s->f)) {
4943 826082fe 2020-12-10 stsp s->eof = 1;
4946 826082fe 2020-12-10 stsp free(line);
4947 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4950 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4951 07dd3ed3 2022-08-06 thomas continue;
4952 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4953 07dd3ed3 2022-08-06 thomas continue;
4954 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4955 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4957 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4958 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4959 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4960 cbea3800 2022-06-23 thomas if (err) {
4961 cbea3800 2022-06-23 thomas free(line);
4962 cbea3800 2022-06-23 thomas return err;
4964 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4965 cbea3800 2022-06-23 thomas free(wline);
4966 cbea3800 2022-06-23 thomas wline = NULL;
4968 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4969 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4970 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4971 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4972 07dd3ed3 2022-08-06 thomas if (attr)
4973 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4974 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4975 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4976 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4977 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4978 41605754 2020-11-12 stsp if (err) {
4979 41605754 2020-11-12 stsp free(line);
4980 41605754 2020-11-12 stsp return err;
4983 f1c0ec19 2022-06-23 thomas int skip;
4984 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4985 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4986 f1c0ec19 2022-06-23 thomas if (err) {
4987 f1c0ec19 2022-06-23 thomas free(line);
4988 f1c0ec19 2022-06-23 thomas return err;
4990 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4991 f1c0ec19 2022-06-23 thomas free(wline);
4992 41605754 2020-11-12 stsp wline = NULL;
4994 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4995 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4996 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4997 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4998 07dd3ed3 2022-08-06 thomas } else {
4999 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
5000 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
5002 07dd3ed3 2022-08-06 thomas if (attr)
5003 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
5004 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
5005 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
5007 826082fe 2020-12-10 stsp free(line);
5008 fe621944 2020-11-10 stsp if (nprinted >= 1)
5009 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
5010 89f1a395 2020-12-01 naddy (nprinted - 1);
5012 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
5014 a5d43cac 2022-07-01 thomas view_border(view);
5016 89f1a395 2020-12-01 naddy if (s->eof) {
5017 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
5018 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
5019 c3e9aa98 2019-05-13 jcs nprinted++;
5022 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
5023 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
5025 c3e9aa98 2019-05-13 jcs return err;
5028 c3e9aa98 2019-05-13 jcs wstandout(view->window);
5029 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
5030 e54cc94a 2020-11-11 stsp free(wline);
5031 e54cc94a 2020-11-11 stsp wline = NULL;
5032 c3e9aa98 2019-05-13 jcs wstandend(view->window);
5035 26ed57b2 2018-05-19 stsp return NULL;
5038 abd2672a 2018-12-23 stsp static char *
5039 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
5041 09867e48 2019-08-13 stsp struct tm mytm, *tm;
5042 09867e48 2019-08-13 stsp char *p, *s;
5044 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
5045 09867e48 2019-08-13 stsp if (tm == NULL)
5046 09867e48 2019-08-13 stsp return NULL;
5047 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
5048 09867e48 2019-08-13 stsp if (s == NULL)
5049 09867e48 2019-08-13 stsp return NULL;
5050 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
5052 abd2672a 2018-12-23 stsp *p = '\0';
5056 4ed7e80c 2018-05-20 stsp static const struct got_error *
5057 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
5058 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
5060 82c78e96 2022-08-06 thomas struct got_diff_line *p;
5062 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
5063 fe621944 2020-11-10 stsp if (p == NULL)
5064 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
5065 82c78e96 2022-08-06 thomas *lines = p;
5066 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
5067 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
5068 fe621944 2020-11-10 stsp (*nlines)++;
5070 fe621944 2020-11-10 stsp return NULL;
5073 fe621944 2020-11-10 stsp static const struct got_error *
5074 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
5075 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
5077 be97ab03 2023-01-19 thomas struct got_diff_line *p;
5078 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
5079 be97ab03 2023-01-19 thomas size_t i, r;
5081 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
5082 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
5084 be97ab03 2023-01-19 thomas for (;;) {
5085 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
5086 be97ab03 2023-01-19 thomas if (r == 0) {
5087 be97ab03 2023-01-19 thomas if (ferror(src))
5088 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
5089 be97ab03 2023-01-19 thomas if (feof(src))
5092 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
5093 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
5096 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
5097 9382c10a 2023-02-23 thomas return NULL;
5100 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
5101 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
5102 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
5104 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
5105 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
5106 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
5108 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
5109 9382c10a 2023-02-23 thomas --s_nlines;
5110 9382c10a 2023-02-23 thomas ++s_lines;
5114 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
5115 be97ab03 2023-01-19 thomas if (p == NULL) {
5116 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
5117 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
5120 be97ab03 2023-01-19 thomas *d_lines = p;
5122 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
5123 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
5125 be97ab03 2023-01-19 thomas return NULL;
5128 be97ab03 2023-01-19 thomas static const struct got_error *
5129 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
5130 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
5131 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
5132 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
5134 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
5135 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
5136 15a94983 2018-12-23 stsp struct got_commit_object *commit;
5137 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5138 45d799e2 2018-12-23 stsp time_t committer_time;
5139 45d799e2 2018-12-23 stsp const char *author, *committer;
5140 8b473291 2019-02-21 stsp char *refs_str = NULL;
5141 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
5142 fe621944 2020-11-10 stsp off_t outoff = 0;
5145 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, commit_id, repo);
5146 00ddec2f 2023-05-17 thomas if (err)
5147 00ddec2f 2023-05-17 thomas return err;
5149 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5151 abd2672a 2018-12-23 stsp return err;
5153 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
5154 15a94983 2018-12-23 stsp if (err) {
5155 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
5156 15a94983 2018-12-23 stsp goto done;
5159 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5161 fe621944 2020-11-10 stsp goto done;
5163 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5164 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
5165 fe621944 2020-11-10 stsp if (n < 0) {
5166 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5167 abd2672a 2018-12-23 stsp goto done;
5169 fe621944 2020-11-10 stsp outoff += n;
5170 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5172 fe621944 2020-11-10 stsp goto done;
5174 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
5175 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
5176 fe621944 2020-11-10 stsp if (n < 0) {
5177 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5178 abd2672a 2018-12-23 stsp goto done;
5180 fe621944 2020-11-10 stsp outoff += n;
5181 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5183 fe621944 2020-11-10 stsp goto done;
5185 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
5186 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
5187 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
5188 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
5189 fe621944 2020-11-10 stsp if (n < 0) {
5190 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5191 fe621944 2020-11-10 stsp goto done;
5193 fe621944 2020-11-10 stsp outoff += n;
5194 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5195 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
5197 fe621944 2020-11-10 stsp goto done;
5199 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
5200 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
5201 48830929 2023-01-07 thomas if (datestr) {
5202 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
5203 fe621944 2020-11-10 stsp if (n < 0) {
5204 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5205 fe621944 2020-11-10 stsp goto done;
5207 fe621944 2020-11-10 stsp outoff += n;
5208 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5209 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
5211 fe621944 2020-11-10 stsp goto done;
5213 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
5214 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
5215 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
5216 ac4dc263 2021-09-24 thomas int pn = 1;
5217 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
5218 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
5219 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
5220 ac4dc263 2021-09-24 thomas if (err)
5221 ac4dc263 2021-09-24 thomas goto done;
5222 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5223 ac4dc263 2021-09-24 thomas if (n < 0) {
5224 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
5225 ac4dc263 2021-09-24 thomas goto done;
5227 ac4dc263 2021-09-24 thomas outoff += n;
5228 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5229 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
5230 ac4dc263 2021-09-24 thomas if (err)
5231 ac4dc263 2021-09-24 thomas goto done;
5232 ac4dc263 2021-09-24 thomas free(id_str);
5233 ac4dc263 2021-09-24 thomas id_str = NULL;
5237 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
5239 5943eee2 2019-08-13 stsp goto done;
5240 fe621944 2020-11-10 stsp s = logmsg;
5241 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
5242 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
5243 fe621944 2020-11-10 stsp if (n < 0) {
5244 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5245 fe621944 2020-11-10 stsp goto done;
5247 fe621944 2020-11-10 stsp outoff += n;
5248 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5249 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
5251 fe621944 2020-11-10 stsp goto done;
5254 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
5255 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5256 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
5258 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5259 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5260 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
5261 fe621944 2020-11-10 stsp if (n < 0) {
5262 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5263 fe621944 2020-11-10 stsp goto done;
5265 fe621944 2020-11-10 stsp outoff += n;
5266 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5267 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
5269 fe621944 2020-11-10 stsp goto done;
5272 0208f208 2020-05-05 stsp fputc('\n', outfile);
5274 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5275 772fcad5 2023-01-07 thomas if (err)
5276 772fcad5 2023-01-07 thomas goto done;
5278 772fcad5 2023-01-07 thomas n = fprintf(outfile,
5279 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5280 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5281 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5282 772fcad5 2023-01-07 thomas if (n < 0) {
5283 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
5284 772fcad5 2023-01-07 thomas goto done;
5286 772fcad5 2023-01-07 thomas outoff += n;
5287 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5288 772fcad5 2023-01-07 thomas if (err)
5289 772fcad5 2023-01-07 thomas goto done;
5291 772fcad5 2023-01-07 thomas fputc('\n', outfile);
5292 772fcad5 2023-01-07 thomas outoff++;
5293 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5295 abd2672a 2018-12-23 stsp free(id_str);
5296 5943eee2 2019-08-13 stsp free(logmsg);
5297 8b473291 2019-02-21 stsp free(refs_str);
5298 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5299 7510f233 2020-08-09 stsp if (err) {
5300 82c78e96 2022-08-06 thomas free(*lines);
5301 82c78e96 2022-08-06 thomas *lines = NULL;
5302 ae6a6978 2020-08-09 stsp *nlines = 0;
5304 fe621944 2020-11-10 stsp return err;
5307 abd2672a 2018-12-23 stsp static const struct got_error *
5308 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5310 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5311 2a2e5933 2023-12-26 thomas FILE *tmp_diff_file = NULL;
5312 15a94983 2018-12-23 stsp int obj_type;
5313 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
5314 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
5315 ce631d35 2023-12-19 thomas struct got_commit_object *commit2 = NULL;
5317 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
5319 82c78e96 2022-08-06 thomas free(s->lines);
5320 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
5321 82c78e96 2022-08-06 thomas if (s->lines == NULL)
5322 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5323 fe621944 2020-11-10 stsp s->nlines = 0;
5325 2a2e5933 2023-12-26 thomas if (s->f && fclose(s->f) == EOF) {
5326 2a2e5933 2023-12-26 thomas s->f = NULL;
5327 2a2e5933 2023-12-26 thomas return got_error_from_errno("fclose");
5330 2a2e5933 2023-12-26 thomas s->f = got_opentemp();
5331 2a2e5933 2023-12-26 thomas if (s->f == NULL)
5332 2a2e5933 2023-12-26 thomas return got_error_from_errno("got_opentemp");
5334 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
5335 2a2e5933 2023-12-26 thomas if (tmp_diff_file == NULL)
5336 2a2e5933 2023-12-26 thomas return got_error_from_errno("got_opentemp");
5338 15a94983 2018-12-23 stsp if (s->id1)
5339 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5341 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5343 15a94983 2018-12-23 stsp goto done;
5345 15a94983 2018-12-23 stsp switch (obj_type) {
5346 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5347 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5348 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5349 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
5350 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5353 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5354 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5355 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5356 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5357 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
5359 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5360 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5361 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5362 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5363 be97ab03 2023-01-19 thomas size_t nlines = 0;
5364 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
5365 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
5366 be97ab03 2023-01-19 thomas &changed_paths,
5367 be97ab03 2023-01-19 thomas s->ignore_whitespace,
5368 be97ab03 2023-01-19 thomas s->force_text_diff,
5369 be97ab03 2023-01-19 thomas tog_diff_algo
5372 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
5373 be97ab03 2023-01-19 thomas if (lines == NULL) {
5374 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
5375 be97ab03 2023-01-19 thomas goto done;
5378 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
5379 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
5380 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5381 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5382 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5383 be97ab03 2023-01-19 thomas if (err)
5386 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5387 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5388 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5389 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
5390 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
5391 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5392 f44b1f58 2020-02-02 tracey if (err)
5393 f44b1f58 2020-02-02 tracey goto done;
5394 f44b1f58 2020-02-02 tracey } else {
5395 ce631d35 2023-12-19 thomas err = got_object_open_as_commit(&commit2, s->repo,
5396 ce631d35 2023-12-19 thomas s->id2);
5397 ce631d35 2023-12-19 thomas if (err)
5398 ce631d35 2023-12-19 thomas goto done;
5400 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5401 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5402 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5403 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
5404 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
5405 772fcad5 2023-01-07 thomas s->ignore_whitespace,
5406 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5407 f44b1f58 2020-02-02 tracey if (err)
5408 f44b1f58 2020-02-02 tracey goto done;
5414 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5415 be97ab03 2023-01-19 thomas lines, nlines);
5419 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5423 be97ab03 2023-01-19 thomas free(lines);
5424 ce631d35 2023-12-19 thomas if (commit2 != NULL)
5425 ce631d35 2023-12-19 thomas got_object_commit_close(commit2);
5426 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5427 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5428 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5429 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5430 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
5431 48ae06ee 2018-10-18 stsp return err;
5434 f5215bb9 2019-02-22 stsp static void
5435 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5437 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5438 f5215bb9 2019-02-22 stsp update_panels();
5439 f5215bb9 2019-02-22 stsp doupdate();
5442 f44b1f58 2020-02-02 tracey static const struct got_error *
5443 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5445 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5447 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5448 f44b1f58 2020-02-02 tracey return NULL;
5451 2a7d3cd7 2022-09-17 thomas static void
5452 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5453 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
5455 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
5457 2a7d3cd7 2022-09-17 thomas *f = s->f;
5458 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
5459 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
5460 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
5461 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
5462 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
5463 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
5466 fc2737d5 2022-09-15 thomas static const struct got_error *
5467 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
5469 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5470 fc2737d5 2022-09-15 thomas FILE *f;
5471 f44b1f58 2020-02-02 tracey int lineno;
5472 78eecffc 2022-06-23 thomas char *line = NULL;
5473 826082fe 2020-12-10 stsp size_t linesize = 0;
5474 826082fe 2020-12-10 stsp ssize_t linelen;
5475 fc2737d5 2022-09-15 thomas off_t *line_offsets;
5476 fc2737d5 2022-09-15 thomas size_t nlines = 0;
5477 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
5479 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
5480 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
5481 2a7d3cd7 2022-09-17 thomas "view search not supported");
5482 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5483 fc2737d5 2022-09-15 thomas &match, &selected);
5485 f44b1f58 2020-02-02 tracey if (!view->searching) {
5486 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5487 f44b1f58 2020-02-02 tracey return NULL;
5490 fc2737d5 2022-09-15 thomas if (*match) {
5491 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5492 7303e8c8 2023-04-04 thomas lineno = *first + 1;
5494 7303e8c8 2023-04-04 thomas lineno = *first - 1;
5496 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
5498 f44b1f58 2020-02-02 tracey while (1) {
5499 f44b1f58 2020-02-02 tracey off_t offset;
5501 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
5502 fc2737d5 2022-09-15 thomas if (*match == 0) {
5503 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5507 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5508 f44b1f58 2020-02-02 tracey lineno = 1;
5510 fc2737d5 2022-09-15 thomas lineno = nlines;
5513 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5514 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5515 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5516 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5517 f44b1f58 2020-02-02 tracey free(line);
5518 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5520 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5521 78eecffc 2022-06-23 thomas if (linelen != -1) {
5522 78eecffc 2022-06-23 thomas char *exstr;
5523 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5524 78eecffc 2022-06-23 thomas if (err)
5526 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5527 78eecffc 2022-06-23 thomas &view->regmatch)) {
5528 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5529 fc2737d5 2022-09-15 thomas *match = lineno;
5530 78eecffc 2022-06-23 thomas free(exstr);
5533 78eecffc 2022-06-23 thomas free(exstr);
5535 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5536 f44b1f58 2020-02-02 tracey lineno++;
5538 f44b1f58 2020-02-02 tracey lineno--;
5540 826082fe 2020-12-10 stsp free(line);
5542 fc2737d5 2022-09-15 thomas if (*match) {
5543 fc2737d5 2022-09-15 thomas *first = *match;
5544 fc2737d5 2022-09-15 thomas *selected = 1;
5547 05171be4 2022-06-23 thomas return err;
5550 48ae06ee 2018-10-18 stsp static const struct got_error *
5551 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5553 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5554 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5556 a0f32f33 2022-06-13 thomas free(s->id1);
5557 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5558 a0f32f33 2022-06-13 thomas free(s->id2);
5559 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5560 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5561 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5562 a0f32f33 2022-06-13 thomas s->f = NULL;
5563 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5564 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5565 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5566 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5567 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5568 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5569 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5570 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5571 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5572 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5573 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5574 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5575 82c78e96 2022-08-06 thomas free(s->lines);
5576 82c78e96 2022-08-06 thomas s->lines = NULL;
5577 a0f32f33 2022-06-13 thomas s->nlines = 0;
5578 a0f32f33 2022-06-13 thomas return err;
5581 a0f32f33 2022-06-13 thomas static const struct got_error *
5582 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5583 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5584 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5585 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5587 48ae06ee 2018-10-18 stsp const struct got_error *err;
5588 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5590 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5591 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5592 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5594 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5595 51b7e1c3 2023-04-14 thomas int type1, type2;
5597 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type1, repo, id1);
5598 51b7e1c3 2023-04-14 thomas if (err)
5599 51b7e1c3 2023-04-14 thomas goto done;
5600 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type2, repo, id2);
5601 51b7e1c3 2023-04-14 thomas if (err)
5602 51b7e1c3 2023-04-14 thomas goto done;
5604 51b7e1c3 2023-04-14 thomas if (type1 != type2) {
5605 51b7e1c3 2023-04-14 thomas err = got_error(GOT_ERR_OBJ_TYPE);
5606 51b7e1c3 2023-04-14 thomas goto done;
5609 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5610 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5611 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5612 f44b1f58 2020-02-02 tracey s->repo = repo;
5613 3dbaef42 2020-11-24 stsp s->label1 = label1;
5614 3dbaef42 2020-11-24 stsp s->label2 = label2;
5616 15a94983 2018-12-23 stsp if (id1) {
5617 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5618 51b7e1c3 2023-04-14 thomas if (s->id1 == NULL) {
5619 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
5620 51b7e1c3 2023-04-14 thomas goto done;
5623 5465d566 2020-02-01 tracey s->id1 = NULL;
5625 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5626 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5627 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5628 a0f32f33 2022-06-13 thomas goto done;
5631 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5632 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5633 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5634 9a1d7435 2022-06-23 thomas goto done;
5637 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5638 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5639 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5640 a0f32f33 2022-06-13 thomas goto done;
5643 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5644 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5645 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5646 19a6a6b5 2022-07-01 thomas goto done;
5649 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5650 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5651 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5652 19a6a6b5 2022-07-01 thomas goto done;
5655 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5656 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5657 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5658 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5659 5465d566 2020-02-01 tracey s->repo = repo;
5661 557d3365 2023-04-14 thomas if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5664 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5665 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5666 6568f0aa 2022-08-06 thomas if (rc != ERR)
5667 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5668 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5669 6568f0aa 2022-08-06 thomas if (rc != ERR)
5670 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5671 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5672 6568f0aa 2022-08-06 thomas if (rc != ERR)
5673 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5674 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5675 6568f0aa 2022-08-06 thomas if (rc != ERR)
5676 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5677 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5678 6568f0aa 2022-08-06 thomas if (rc != ERR)
5679 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5680 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5681 6568f0aa 2022-08-06 thomas if (rc != ERR)
5682 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5683 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5684 6568f0aa 2022-08-06 thomas if (rc != ERR)
5685 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5686 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5687 6568f0aa 2022-08-06 thomas if (rc != ERR)
5688 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5689 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5690 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5691 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5692 a0f32f33 2022-06-13 thomas goto done;
5696 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5697 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5698 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5699 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5701 5465d566 2020-02-01 tracey err = create_diff(s);
5703 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5704 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5705 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5706 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5707 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5708 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5709 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5711 51b7e1c3 2023-04-14 thomas if (err) {
5712 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
5713 51b7e1c3 2023-04-14 thomas close_diff_view(view);
5714 51b7e1c3 2023-04-14 thomas view_close(view);
5716 e5a0f69f 2018-08-18 stsp return err;
5719 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5720 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5722 a3404814 2018-09-02 stsp const struct got_error *err;
5723 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5724 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5725 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5727 a3404814 2018-09-02 stsp if (s->id1) {
5728 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5730 a3404814 2018-09-02 stsp return err;
5731 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5733 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5735 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5737 a3404814 2018-09-02 stsp return err;
5738 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5740 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5741 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5742 a3404814 2018-09-02 stsp free(id_str1);
5743 a3404814 2018-09-02 stsp free(id_str2);
5744 a3404814 2018-09-02 stsp return err;
5746 a3404814 2018-09-02 stsp free(id_str1);
5747 a3404814 2018-09-02 stsp free(id_str2);
5749 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5750 267bb3b8 2021-08-01 stsp free(header);
5751 267bb3b8 2021-08-01 stsp return err;
5754 15a087fe 2019-02-21 stsp static const struct got_error *
5755 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5756 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5758 d7a04538 2019-02-21 stsp const struct got_error *err;
5759 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5760 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5761 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5763 15a087fe 2019-02-21 stsp free(s->id2);
5764 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5765 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5766 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5768 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5770 d7a04538 2019-02-21 stsp return err;
5771 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5772 15a087fe 2019-02-21 stsp free(s->id1);
5773 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5774 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5775 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5776 15a087fe 2019-02-21 stsp return NULL;
5779 0cf4efb1 2018-09-29 stsp static const struct got_error *
5780 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5782 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5784 adf4c9e0 2022-07-03 thomas view->count = 0;
5785 adf4c9e0 2022-07-03 thomas wclear(view->window);
5786 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5787 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5788 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5789 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5790 adf4c9e0 2022-07-03 thomas return create_diff(s);
5793 82c78e96 2022-08-06 thomas static void
5794 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5796 82c78e96 2022-08-06 thomas int start, i;
5798 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5800 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5801 82c78e96 2022-08-06 thomas if (i == 0)
5802 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5803 82c78e96 2022-08-06 thomas if (--i == start)
5804 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5807 82c78e96 2022-08-06 thomas s->selected_line = 1;
5808 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5811 82c78e96 2022-08-06 thomas static void
5812 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5814 82c78e96 2022-08-06 thomas int start, i;
5816 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5818 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5819 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5821 82c78e96 2022-08-06 thomas if (++i == start)
5822 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5825 82c78e96 2022-08-06 thomas s->selected_line = 1;
5826 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5829 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5830 4fc71f3b 2022-07-12 thomas int, int, int);
5831 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5832 4fc71f3b 2022-07-12 thomas int, int);
5834 adf4c9e0 2022-07-03 thomas static const struct got_error *
5835 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5837 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5838 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5839 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5840 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5841 826082fe 2020-12-10 stsp char *line = NULL;
5842 826082fe 2020-12-10 stsp size_t linesize = 0;
5843 826082fe 2020-12-10 stsp ssize_t linelen;
5844 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5846 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5848 e5a0f69f 2018-08-18 stsp switch (ch) {
5849 05171be4 2022-06-23 thomas case '0':
5850 05171be4 2022-06-23 thomas case '$':
5851 05171be4 2022-06-23 thomas case KEY_RIGHT:
5852 05171be4 2022-06-23 thomas case 'l':
5853 05171be4 2022-06-23 thomas case KEY_LEFT:
5854 05171be4 2022-06-23 thomas case 'h':
5855 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5859 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5860 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5861 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5862 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5863 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5865 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5866 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5867 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5868 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5869 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5871 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5874 912a3f79 2021-08-30 j case KEY_HOME:
5875 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5876 07b0611c 2022-06-23 thomas view->count = 0;
5879 912a3f79 2021-08-30 j case KEY_END:
5880 07b0611c 2022-06-23 thomas view->count = 0;
5884 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5888 1e37a5c2 2019-05-12 jcs case KEY_UP:
5889 f7140bf5 2021-10-17 thomas case CTRL('p'):
5890 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5891 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5893 07b0611c 2022-06-23 thomas view->count = 0;
5895 70f17a53 2022-06-13 thomas case CTRL('u'):
5896 23427b14 2022-06-23 thomas case 'u':
5897 70f17a53 2022-06-13 thomas nscroll /= 2;
5898 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5899 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5900 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5901 1c5e5faa 2022-06-23 thomas case 'b':
5902 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5903 07b0611c 2022-06-23 thomas view->count = 0;
5907 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5908 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5911 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5912 f7140bf5 2021-10-17 thomas case CTRL('n'):
5913 1e37a5c2 2019-05-12 jcs if (!s->eof)
5914 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5916 07b0611c 2022-06-23 thomas view->count = 0;
5918 70f17a53 2022-06-13 thomas case CTRL('d'):
5919 23427b14 2022-06-23 thomas case 'd':
5920 70f17a53 2022-06-13 thomas nscroll /= 2;
5921 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5922 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5923 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5924 1c5e5faa 2022-06-23 thomas case 'f':
5926 07b0611c 2022-06-23 thomas if (s->eof) {
5927 07b0611c 2022-06-23 thomas view->count = 0;
5931 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5932 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5933 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5934 826082fe 2020-12-10 stsp if (linelen == -1) {
5935 826082fe 2020-12-10 stsp if (feof(s->f)) {
5936 826082fe 2020-12-10 stsp s->eof = 1;
5938 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5942 826082fe 2020-12-10 stsp free(line);
5944 82c78e96 2022-08-06 thomas case '(':
5945 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5947 82c78e96 2022-08-06 thomas case ')':
5948 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5950 82c78e96 2022-08-06 thomas case '{':
5951 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5953 82c78e96 2022-08-06 thomas case '}':
5954 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5957 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5958 1e37a5c2 2019-05-12 jcs s->diff_context--;
5959 aa61903a 2021-12-31 thomas s->matched_line = 0;
5960 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5961 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5962 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5963 27829c9e 2020-11-21 stsp s->nlines) {
5964 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5965 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5968 07b0611c 2022-06-23 thomas view->count = 0;
5971 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5972 1e37a5c2 2019-05-12 jcs s->diff_context++;
5973 aa61903a 2021-12-31 thomas s->matched_line = 0;
5974 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5975 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5977 07b0611c 2022-06-23 thomas view->count = 0;
5981 777aae21 2022-07-20 thomas case 'K':
5983 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5984 4fc71f3b 2022-07-12 thomas case '>':
5985 4fc71f3b 2022-07-12 thomas case '.':
5986 777aae21 2022-07-20 thomas case 'J':
5987 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5988 07b0611c 2022-06-23 thomas view->count = 0;
5991 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5993 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5994 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5995 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5997 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5998 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5999 4fc71f3b 2022-07-12 thomas if (err)
6001 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
6003 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
6006 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
6007 4fc71f3b 2022-07-12 thomas if (err)
6009 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
6010 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
6011 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
6013 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
6014 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
6015 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
6017 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
6018 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
6019 4fc71f3b 2022-07-12 thomas if (err)
6021 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
6023 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
6025 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
6026 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
6027 4fc71f3b 2022-07-12 thomas bs->selected_line);
6028 4fc71f3b 2022-07-12 thomas if (id == NULL)
6031 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
6034 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
6035 4fc71f3b 2022-07-12 thomas if (err)
6038 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6039 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
6040 aa61903a 2021-12-31 thomas s->matched_line = 0;
6041 05171be4 2022-06-23 thomas view->x = 0;
6043 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
6044 1e37a5c2 2019-05-12 jcs err = create_diff(s);
6047 07b0611c 2022-06-23 thomas view->count = 0;
6051 bcbd79e2 2018-08-19 stsp return err;
6054 4ed7e80c 2018-05-20 stsp static const struct got_error *
6055 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
6057 1d98034b 2023-04-14 thomas const struct got_error *error;
6058 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
6059 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
6060 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
6061 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
6062 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
6063 66b04f8f 2023-07-19 thomas char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
6064 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
6065 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
6066 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
6067 3dbaef42 2020-11-24 stsp const char *errstr;
6068 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
6069 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6071 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
6072 26ed57b2 2018-05-19 stsp switch (ch) {
6074 64453f7e 2020-11-21 stsp force_text_diff = 1;
6077 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
6079 3dbaef42 2020-11-24 stsp if (errstr != NULL)
6080 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
6081 8f666e67 2022-02-12 thomas errstr, errstr);
6083 09b5bff8 2020-02-23 naddy case 'r':
6084 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
6085 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
6086 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
6088 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
6091 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
6094 17020d27 2019-03-07 stsp usage_diff();
6095 26ed57b2 2018-05-19 stsp /* NOTREACHED */
6099 26ed57b2 2018-05-19 stsp argc -= optind;
6100 26ed57b2 2018-05-19 stsp argv += optind;
6102 26ed57b2 2018-05-19 stsp if (argc == 0) {
6103 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
6104 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
6105 15a94983 2018-12-23 stsp id_str1 = argv[0];
6106 15a94983 2018-12-23 stsp id_str2 = argv[1];
6108 26ed57b2 2018-05-19 stsp usage_diff();
6110 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6111 7cd52833 2022-06-23 thomas if (error)
6112 7cd52833 2022-06-23 thomas goto done;
6114 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
6115 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6116 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6117 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6118 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
6119 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6120 c156c7a4 2020-12-18 stsp goto done;
6121 a273ac94 2020-02-23 naddy if (worktree)
6122 a273ac94 2020-02-23 naddy repo_path =
6123 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
6125 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
6126 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6127 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6128 c156c7a4 2020-12-18 stsp goto done;
6132 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6133 eb6600df 2019-01-04 stsp if (error)
6134 eb6600df 2019-01-04 stsp goto done;
6136 557d3365 2023-04-14 thomas init_curses();
6138 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6139 51a10b52 2020-12-26 stsp if (error)
6140 51a10b52 2020-12-26 stsp goto done;
6142 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6143 26ed57b2 2018-05-19 stsp if (error)
6144 26ed57b2 2018-05-19 stsp goto done;
6146 66b04f8f 2023-07-19 thomas if (id_str1 != NULL) {
6147 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6148 66b04f8f 2023-07-19 thomas repo, worktree);
6149 66b04f8f 2023-07-19 thomas if (error != NULL)
6150 66b04f8f 2023-07-19 thomas goto done;
6151 66b04f8f 2023-07-19 thomas if (keyword_idstr1 != NULL)
6152 66b04f8f 2023-07-19 thomas id_str1 = keyword_idstr1;
6154 66b04f8f 2023-07-19 thomas if (id_str2 != NULL) {
6155 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6156 66b04f8f 2023-07-19 thomas repo, worktree);
6157 66b04f8f 2023-07-19 thomas if (error != NULL)
6158 66b04f8f 2023-07-19 thomas goto done;
6159 66b04f8f 2023-07-19 thomas if (keyword_idstr2 != NULL)
6160 66b04f8f 2023-07-19 thomas id_str2 = keyword_idstr2;
6163 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
6164 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6165 26ed57b2 2018-05-19 stsp if (error)
6166 26ed57b2 2018-05-19 stsp goto done;
6168 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
6169 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6170 26ed57b2 2018-05-19 stsp if (error)
6171 26ed57b2 2018-05-19 stsp goto done;
6173 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6174 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
6175 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6176 ea5e7bb5 2018-08-01 stsp goto done;
6178 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6179 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
6180 5dc9f4bc 2018-08-04 stsp if (error)
6181 5dc9f4bc 2018-08-04 stsp goto done;
6183 349dfd1e 2023-07-23 thomas if (worktree) {
6184 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
6185 349dfd1e 2023-07-23 thomas if (error != NULL)
6186 349dfd1e 2023-07-23 thomas goto done;
6188 a7245e84 2023-10-21 thomas /* Release work tree lock. */
6189 a7245e84 2023-10-21 thomas got_worktree_close(worktree);
6190 a7245e84 2023-10-21 thomas worktree = NULL;
6193 557d3365 2023-04-14 thomas error = view_loop(view);
6196 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
6197 66b04f8f 2023-07-19 thomas free(keyword_idstr1);
6198 66b04f8f 2023-07-19 thomas free(keyword_idstr2);
6199 3dbaef42 2020-11-24 stsp free(label1);
6200 3dbaef42 2020-11-24 stsp free(label2);
6201 6f04a9f3 2023-12-19 thomas free(id1);
6202 6f04a9f3 2023-12-19 thomas free(id2);
6203 c02c541e 2019-03-29 stsp free(repo_path);
6204 a273ac94 2020-02-23 naddy free(cwd);
6205 1d0f4054 2021-06-17 stsp if (repo) {
6206 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6207 1d0f4054 2021-06-17 stsp if (error == NULL)
6208 1d0f4054 2021-06-17 stsp error = close_err;
6210 a273ac94 2020-02-23 naddy if (worktree)
6211 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
6212 7cd52833 2022-06-23 thomas if (pack_fds) {
6213 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6214 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6215 7cd52833 2022-06-23 thomas if (error == NULL)
6216 7cd52833 2022-06-23 thomas error = pack_err;
6218 51a10b52 2020-12-26 stsp tog_free_refs();
6219 26ed57b2 2018-05-19 stsp return error;
6222 4ed7e80c 2018-05-20 stsp __dead static void
6223 9f7d7167 2018-04-29 stsp usage_blame(void)
6226 91198554 2022-06-23 thomas fprintf(stderr,
6227 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
6228 9f7d7167 2018-04-29 stsp getprogname());
6232 84451b3e 2018-07-10 stsp struct tog_blame_line {
6233 84451b3e 2018-07-10 stsp int annotated;
6234 84451b3e 2018-07-10 stsp struct got_object_id *id;
6237 4ed7e80c 2018-05-20 stsp static const struct got_error *
6238 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
6240 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6241 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6242 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
6243 84451b3e 2018-07-10 stsp const struct got_error *err;
6244 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
6245 826082fe 2020-12-10 stsp char *line = NULL;
6246 826082fe 2020-12-10 stsp size_t linesize = 0;
6247 826082fe 2020-12-10 stsp ssize_t linelen;
6248 84451b3e 2018-07-10 stsp wchar_t *wline;
6249 27a741e5 2019-09-11 stsp int width;
6250 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
6251 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
6252 ab089a2a 2018-07-12 stsp char *id_str;
6253 11b20872 2019-11-08 stsp struct tog_color *tc;
6255 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
6257 ab089a2a 2018-07-12 stsp return err;
6259 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
6260 f7d12f7e 2018-08-01 stsp werase(view->window);
6262 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
6263 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6264 ab089a2a 2018-07-12 stsp free(id_str);
6265 ab089a2a 2018-07-12 stsp return err;
6268 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6269 ab089a2a 2018-07-12 stsp free(line);
6270 2550e4c3 2018-07-13 stsp line = NULL;
6272 1cae65b4 2019-09-22 stsp return err;
6273 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6274 a3404814 2018-09-02 stsp wstandout(view->window);
6275 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6277 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6278 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6279 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6280 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6282 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6283 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6284 a3404814 2018-09-02 stsp wstandend(view->window);
6285 2550e4c3 2018-07-13 stsp free(wline);
6286 2550e4c3 2018-07-13 stsp wline = NULL;
6288 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
6289 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
6291 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6292 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6295 1134ebde 2023-04-22 thomas rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6297 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_wait");
6298 1134ebde 2023-04-22 thomas tog_io.wait_for_ui = 0;
6301 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6302 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6303 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6304 ab089a2a 2018-07-12 stsp free(id_str);
6305 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6307 ab089a2a 2018-07-12 stsp free(id_str);
6308 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6309 3f60a8ef 2018-07-10 stsp free(line);
6310 2550e4c3 2018-07-13 stsp line = NULL;
6312 3f60a8ef 2018-07-10 stsp return err;
6313 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6314 2550e4c3 2018-07-13 stsp free(wline);
6315 2550e4c3 2018-07-13 stsp wline = NULL;
6316 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6317 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6319 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6320 05171be4 2022-06-23 thomas view->maxx = 0;
6321 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6322 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6323 826082fe 2020-12-10 stsp if (linelen == -1) {
6324 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6325 826082fe 2020-12-10 stsp s->eof = 1;
6328 84451b3e 2018-07-10 stsp free(line);
6329 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6331 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6332 07dd3ed3 2022-08-06 thomas continue;
6333 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
6336 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
6337 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6338 cbea3800 2022-06-23 thomas if (err) {
6339 cbea3800 2022-06-23 thomas free(line);
6340 cbea3800 2022-06-23 thomas return err;
6342 cbea3800 2022-06-23 thomas free(wline);
6343 cbea3800 2022-06-23 thomas wline = NULL;
6344 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
6346 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6347 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6349 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6350 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6351 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6352 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6353 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
6354 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6355 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6356 8d0fe45a 2019-08-12 stsp char *id_str;
6357 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
6358 91198554 2022-06-23 thomas blame_line->id);
6359 8d0fe45a 2019-08-12 stsp if (err) {
6360 8d0fe45a 2019-08-12 stsp free(line);
6361 8d0fe45a 2019-08-12 stsp return err;
6363 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6365 11b20872 2019-11-08 stsp wattr_on(view->window,
6366 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6367 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6369 11b20872 2019-11-08 stsp wattr_off(view->window,
6370 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6371 8d0fe45a 2019-08-12 stsp free(id_str);
6372 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6374 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6375 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6378 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6379 ee41ec32 2018-07-10 stsp prev_id = NULL;
6382 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6383 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6384 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6386 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6387 41605754 2020-11-12 stsp width = 9;
6388 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6389 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6390 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6391 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6392 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
6393 41605754 2020-11-12 stsp if (err) {
6394 41605754 2020-11-12 stsp free(line);
6395 41605754 2020-11-12 stsp return err;
6397 41605754 2020-11-12 stsp width += 9;
6399 923086fd 2022-06-23 thomas int skip;
6400 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
6401 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
6402 923086fd 2022-06-23 thomas if (err) {
6403 923086fd 2022-06-23 thomas free(line);
6404 923086fd 2022-06-23 thomas return err;
6406 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
6407 02bce7e2 2022-06-23 thomas width += 9;
6408 41605754 2020-11-12 stsp free(wline);
6409 41605754 2020-11-12 stsp wline = NULL;
6412 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6413 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6414 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6415 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6417 826082fe 2020-12-10 stsp free(line);
6418 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6420 a5d43cac 2022-07-01 thomas view_border(view);
6422 84451b3e 2018-07-10 stsp return NULL;
6425 84451b3e 2018-07-10 stsp static const struct got_error *
6426 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
6427 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
6429 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6430 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6431 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6432 1a76625f 2018-10-22 stsp int errcode;
6434 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6435 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6436 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6438 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6439 1a76625f 2018-10-22 stsp if (errcode)
6440 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6442 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6443 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6444 d68a0a7d 2018-07-10 stsp goto done;
6447 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6448 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6450 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6451 d68a0a7d 2018-07-10 stsp if (line->annotated)
6452 d68a0a7d 2018-07-10 stsp goto done;
6454 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6455 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6456 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6457 84451b3e 2018-07-10 stsp goto done;
6459 84451b3e 2018-07-10 stsp line->annotated = 1;
6461 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6462 1a76625f 2018-10-22 stsp if (errcode)
6463 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6464 84451b3e 2018-07-10 stsp return err;
6467 84451b3e 2018-07-10 stsp static void *
6468 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6470 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6471 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6472 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6473 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
6474 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
6476 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
6477 9117a7b7 2022-07-01 thomas if (fd1 == -1)
6478 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
6480 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
6481 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
6482 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6483 9117a7b7 2022-07-01 thomas goto done;
6486 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
6487 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
6488 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6489 9117a7b7 2022-07-01 thomas goto done;
6491 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
6492 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
6493 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6494 9117a7b7 2022-07-01 thomas goto done;
6497 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6499 9117a7b7 2022-07-01 thomas goto done;
6501 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6502 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
6503 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6504 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6505 fc06ba56 2019-08-22 stsp err = NULL;
6507 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6508 9117a7b7 2022-07-01 thomas if (errcode) {
6509 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
6510 9117a7b7 2022-07-01 thomas goto done;
6513 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6514 1d0f4054 2021-06-17 stsp if (err == NULL)
6515 1d0f4054 2021-06-17 stsp err = close_err;
6516 c9beca56 2018-07-22 stsp ta->repo = NULL;
6517 c9beca56 2018-07-22 stsp *ta->complete = 1;
6519 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6520 1134ebde 2023-04-22 thomas errcode = pthread_cond_signal(&ta->blame_complete);
6521 1134ebde 2023-04-22 thomas if (errcode && err == NULL)
6522 1134ebde 2023-04-22 thomas err = got_error_set_errno(errcode,
6523 1134ebde 2023-04-22 thomas "pthread_cond_signal");
6526 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6527 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6528 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6531 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6532 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
6533 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6534 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
6535 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
6536 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6537 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
6538 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6540 18430de3 2018-07-10 stsp return (void *)err;
6543 245d91c1 2018-07-12 stsp static struct got_object_id *
6544 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6545 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6547 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6549 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6550 8d0fe45a 2019-08-12 stsp return NULL;
6552 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6553 4fc71f3b 2022-07-12 thomas if (!line->annotated)
6554 4fc71f3b 2022-07-12 thomas return NULL;
6556 4fc71f3b 2022-07-12 thomas return line->id;
6559 4fc71f3b 2022-07-12 thomas static struct got_object_id *
6560 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6561 4fc71f3b 2022-07-12 thomas int lineno)
6563 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
6565 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6566 4fc71f3b 2022-07-12 thomas return NULL;
6568 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6569 245d91c1 2018-07-12 stsp if (!line->annotated)
6570 245d91c1 2018-07-12 stsp return NULL;
6572 245d91c1 2018-07-12 stsp return line->id;
6575 b880a918 2018-07-10 stsp static const struct got_error *
6576 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6578 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6581 245d91c1 2018-07-12 stsp if (blame->thread) {
6582 1a76625f 2018-10-22 stsp int errcode;
6583 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6584 1a76625f 2018-10-22 stsp if (errcode)
6585 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6586 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6587 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6588 1a76625f 2018-10-22 stsp if (errcode)
6589 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6590 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6591 1a76625f 2018-10-22 stsp if (errcode)
6592 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6593 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6594 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6595 245d91c1 2018-07-12 stsp err = NULL;
6596 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6598 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6599 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6600 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6601 1d0f4054 2021-06-17 stsp if (err == NULL)
6602 1d0f4054 2021-06-17 stsp err = close_err;
6603 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6605 245d91c1 2018-07-12 stsp if (blame->f) {
6606 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6607 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6608 245d91c1 2018-07-12 stsp blame->f = NULL;
6610 57670559 2018-12-23 stsp if (blame->lines) {
6611 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6612 57670559 2018-12-23 stsp free(blame->lines[i].id);
6613 57670559 2018-12-23 stsp free(blame->lines);
6614 57670559 2018-12-23 stsp blame->lines = NULL;
6616 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6617 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6618 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6619 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6620 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6621 7cd52833 2022-06-23 thomas if (err == NULL)
6622 7cd52833 2022-06-23 thomas err = pack_err;
6623 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6625 7477a2d2 2023-08-23 thomas free(blame->line_offsets);
6626 7477a2d2 2023-08-23 thomas blame->line_offsets = NULL;
6627 245d91c1 2018-07-12 stsp return err;
6630 245d91c1 2018-07-12 stsp static const struct got_error *
6631 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6633 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6634 fc06ba56 2019-08-22 stsp int *done = arg;
6635 fc06ba56 2019-08-22 stsp int errcode;
6637 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6638 fc06ba56 2019-08-22 stsp if (errcode)
6639 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6640 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6642 fc06ba56 2019-08-22 stsp if (*done)
6643 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6645 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6646 fc06ba56 2019-08-22 stsp if (errcode)
6647 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6648 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6650 fc06ba56 2019-08-22 stsp return err;
6653 fc06ba56 2019-08-22 stsp static const struct got_error *
6654 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6656 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6657 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6658 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6659 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6660 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6661 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6662 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6663 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6664 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6666 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6667 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6669 15a94983 2018-12-23 stsp return err;
6671 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6672 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6673 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6674 f4ae6ddb 2022-07-01 thomas goto done;
6677 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6678 945f9229 2022-04-16 thomas if (err)
6679 945f9229 2022-04-16 thomas goto done;
6681 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6683 84451b3e 2018-07-10 stsp goto done;
6685 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6686 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6687 84451b3e 2018-07-10 stsp goto done;
6690 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6692 a70480e0 2018-06-23 stsp goto done;
6693 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6694 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6695 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6696 84451b3e 2018-07-10 stsp goto done;
6698 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6699 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6701 1fddf795 2021-01-20 stsp goto done;
6702 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6703 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6704 84451b3e 2018-07-10 stsp goto done;
6707 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6708 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6709 b02560ec 2019-08-19 stsp blame->nlines--;
6711 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6712 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6713 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6714 84451b3e 2018-07-10 stsp goto done;
6717 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6719 bd24772e 2018-07-11 stsp goto done;
6720 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6721 7cd52833 2022-06-23 thomas pack_fds);
6722 7cd52833 2022-06-23 thomas if (err)
6723 7cd52833 2022-06-23 thomas goto done;
6725 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6726 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6727 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6728 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6729 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6730 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6731 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6732 245d91c1 2018-07-12 stsp goto done;
6734 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6736 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6737 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6738 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6739 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6740 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6741 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6742 a5388363 2020-12-01 naddy s->blame_complete = 0;
6744 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6745 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6746 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6747 f5a09613 2020-12-13 naddy s->selected_line = 1;
6749 aa61903a 2021-12-31 thomas s->matched_line = 0;
6752 945f9229 2022-04-16 thomas if (commit)
6753 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6754 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6755 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6757 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6758 27d434c2 2018-09-15 stsp free(obj_id);
6760 245d91c1 2018-07-12 stsp stop_blame(blame);
6761 245d91c1 2018-07-12 stsp return err;
6764 245d91c1 2018-07-12 stsp static const struct got_error *
6765 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6766 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6768 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6769 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6771 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6773 c4843652 2019-08-12 stsp s->path = strdup(path);
6774 c4843652 2019-08-12 stsp if (s->path == NULL)
6775 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6777 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6778 c4843652 2019-08-12 stsp if (err) {
6779 c4843652 2019-08-12 stsp free(s->path);
6780 7cbe629d 2018-08-04 stsp return err;
6783 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6784 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6785 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6786 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6787 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6788 fb2756b9 2018-08-04 stsp s->repo = repo;
6789 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6790 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6792 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6793 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6794 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6795 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6797 11b20872 2019-11-08 stsp return err;
6800 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6801 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6802 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6803 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6804 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6805 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6806 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6808 1134ebde 2023-04-22 thomas if (using_mock_io) {
6809 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6812 1134ebde 2023-04-22 thomas rc = pthread_cond_init(&bta->blame_complete, NULL);
6814 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_init");
6817 a5388363 2020-12-01 naddy return run_blame(view);
6820 e5a0f69f 2018-08-18 stsp static const struct got_error *
6821 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6823 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6824 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6826 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6827 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6829 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6830 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6831 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6832 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6833 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6836 1134ebde 2023-04-22 thomas if (using_mock_io) {
6837 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6840 1134ebde 2023-04-22 thomas rc = pthread_cond_destroy(&bta->blame_complete);
6841 1134ebde 2023-04-22 thomas if (rc && err == NULL)
6842 1134ebde 2023-04-22 thomas err = got_error_set_errno(rc, "pthread_cond_destroy");
6845 e5a0f69f 2018-08-18 stsp free(s->path);
6846 11b20872 2019-11-08 stsp free_colors(&s->colors);
6847 e5a0f69f 2018-08-18 stsp return err;
6850 7cbe629d 2018-08-04 stsp static const struct got_error *
6851 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6853 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6855 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6856 6c4c42e0 2019-06-24 stsp return NULL;
6859 2a7d3cd7 2022-09-17 thomas static void
6860 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6861 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6863 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6865 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6866 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6867 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6868 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6869 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6870 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6871 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6874 6c4c42e0 2019-06-24 stsp static const struct got_error *
6875 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6877 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6878 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6879 2b380cc8 2018-10-24 stsp int errcode;
6881 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6882 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6883 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6884 2b380cc8 2018-10-24 stsp if (errcode)
6885 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6887 557d3365 2023-04-14 thomas if (!using_mock_io)
6888 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while annotating */
6891 557d3365 2023-04-14 thomas if (s->blame_complete && !using_mock_io)
6892 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6894 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6896 a5d43cac 2022-07-01 thomas view_border(view);
6897 e5a0f69f 2018-08-18 stsp return err;
6900 e5a0f69f 2018-08-18 stsp static const struct got_error *
6901 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6902 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6904 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6905 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6907 eaeaa612 2022-07-20 thomas *new_view = NULL;
6909 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6910 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6911 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6913 92845f09 2023-07-26 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0, NULL);
6914 eaeaa612 2022-07-20 thomas if (err)
6915 eaeaa612 2022-07-20 thomas view_close(log_view);
6917 eaeaa612 2022-07-20 thomas *new_view = log_view;
6919 eaeaa612 2022-07-20 thomas return err;
6922 eaeaa612 2022-07-20 thomas static const struct got_error *
6923 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6925 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6926 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6927 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6928 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6930 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6931 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6932 a5d43cac 2022-07-01 thomas --eos; /* border */
6934 e5a0f69f 2018-08-18 stsp switch (ch) {
6935 05171be4 2022-06-23 thomas case '0':
6936 05171be4 2022-06-23 thomas case '$':
6937 05171be4 2022-06-23 thomas case KEY_RIGHT:
6938 05171be4 2022-06-23 thomas case 'l':
6939 05171be4 2022-06-23 thomas case KEY_LEFT:
6940 05171be4 2022-06-23 thomas case 'h':
6941 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6944 1e37a5c2 2019-05-12 jcs s->done = 1;
6946 4deef56f 2021-09-02 naddy case 'g':
6947 4deef56f 2021-09-02 naddy case KEY_HOME:
6948 4deef56f 2021-09-02 naddy s->selected_line = 1;
6949 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6950 07b0611c 2022-06-23 thomas view->count = 0;
6952 4deef56f 2021-09-02 naddy case 'G':
6953 4deef56f 2021-09-02 naddy case KEY_END:
6954 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6955 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6956 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6958 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6959 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6961 07b0611c 2022-06-23 thomas view->count = 0;
6964 1e37a5c2 2019-05-12 jcs case KEY_UP:
6965 f7140bf5 2021-10-17 thomas case CTRL('p'):
6966 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6967 1e37a5c2 2019-05-12 jcs s->selected_line--;
6968 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6969 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6970 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6972 07b0611c 2022-06-23 thomas view->count = 0;
6974 70f17a53 2022-06-13 thomas case CTRL('u'):
6975 23427b14 2022-06-23 thomas case 'u':
6976 70f17a53 2022-06-13 thomas nscroll /= 2;
6977 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6978 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6979 ea025d1d 2020-02-22 naddy case CTRL('b'):
6980 1c5e5faa 2022-06-23 thomas case 'b':
6981 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6982 07b0611c 2022-06-23 thomas if (view->count > 1)
6983 07b0611c 2022-06-23 thomas nscroll += nscroll;
6984 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6985 07b0611c 2022-06-23 thomas view->count = 0;
6988 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6989 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6991 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6994 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6995 f7140bf5 2021-10-17 thomas case CTRL('n'):
6996 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6997 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6998 1e37a5c2 2019-05-12 jcs s->selected_line++;
6999 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
7000 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
7002 07b0611c 2022-06-23 thomas view->count = 0;
7004 1c5e5faa 2022-06-23 thomas case 'c':
7005 1e37a5c2 2019-05-12 jcs case 'p': {
7006 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
7008 07b0611c 2022-06-23 thomas view->count = 0;
7009 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7010 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
7011 1e37a5c2 2019-05-12 jcs if (id == NULL)
7013 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
7014 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
7015 15a94983 2018-12-23 stsp struct got_object_qid *pid;
7016 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
7017 1e37a5c2 2019-05-12 jcs int obj_type;
7018 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
7019 1e37a5c2 2019-05-12 jcs s->repo, id);
7022 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
7023 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
7024 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
7025 15a94983 2018-12-23 stsp got_object_commit_close(commit);
7028 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
7029 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
7030 ec242592 2022-04-22 thomas s->repo, &pid->id);
7031 945f9229 2022-04-16 thomas if (err)
7033 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
7034 945f9229 2022-04-16 thomas pcommit, s->path);
7035 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
7036 e5a0f69f 2018-08-18 stsp if (err) {
7037 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
7038 1e37a5c2 2019-05-12 jcs err = NULL;
7039 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7042 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
7044 1e37a5c2 2019-05-12 jcs free(blob_id);
7045 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
7046 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
7047 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7050 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
7051 ec242592 2022-04-22 thomas &pid->id);
7052 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7054 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
7055 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
7057 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
7062 1e37a5c2 2019-05-12 jcs s->done = 1;
7063 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
7064 1e37a5c2 2019-05-12 jcs s->done = 0;
7065 1e37a5c2 2019-05-12 jcs if (thread_err)
7067 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
7068 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
7069 a5388363 2020-12-01 naddy err = run_blame(view);
7074 1c5e5faa 2022-06-23 thomas case 'C': {
7075 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
7077 07b0611c 2022-06-23 thomas view->count = 0;
7078 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
7079 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
7081 1e37a5c2 2019-05-12 jcs s->done = 1;
7082 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
7083 1e37a5c2 2019-05-12 jcs s->done = 0;
7084 1e37a5c2 2019-05-12 jcs if (thread_err)
7086 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
7087 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
7088 1e37a5c2 2019-05-12 jcs s->blamed_commit =
7089 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
7090 a5388363 2020-12-01 naddy err = run_blame(view);
7095 2a31b33b 2022-07-23 thomas case 'L':
7096 eaeaa612 2022-07-20 thomas view->count = 0;
7097 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
7098 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
7099 2a31b33b 2022-07-23 thomas if (s->id_to_log)
7100 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7102 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7103 1e37a5c2 2019-05-12 jcs case '\r': {
7104 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
7105 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
7106 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
7108 07b0611c 2022-06-23 thomas view->count = 0;
7109 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7110 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
7111 1e37a5c2 2019-05-12 jcs if (id == NULL)
7113 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
7116 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7117 4fc71f3b 2022-07-12 thomas if (*new_view) {
7118 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
7119 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
7120 4fc71f3b 2022-07-12 thomas if (err)
7122 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
7123 4fc71f3b 2022-07-12 thomas } else {
7124 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
7125 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
7127 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
7128 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
7129 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
7130 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
7131 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
7135 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
7136 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
7137 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7138 c68571e2 2023-07-17 thomas if (err)
7140 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
7141 4fc71f3b 2022-07-12 thomas s->selected_line;
7142 4fc71f3b 2022-07-12 thomas if (*new_view)
7143 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
7144 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7145 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7146 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7147 a5d43cac 2022-07-01 thomas if (err)
7151 e78dc838 2020-12-04 stsp view->focussed = 0;
7152 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
7153 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
7154 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
7155 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
7156 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
7157 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
7160 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
7161 40236d76 2022-06-23 thomas if (err)
7163 e78dc838 2020-12-04 stsp view->focus_child = 1;
7165 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
7170 70f17a53 2022-06-13 thomas case CTRL('d'):
7171 23427b14 2022-06-23 thomas case 'd':
7172 70f17a53 2022-06-13 thomas nscroll /= 2;
7173 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7174 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7175 ea025d1d 2020-02-22 naddy case CTRL('f'):
7176 1c5e5faa 2022-06-23 thomas case 'f':
7178 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7179 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
7180 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
7181 07b0611c 2022-06-23 thomas view->count = 0;
7184 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7185 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
7186 70f17a53 2022-06-13 thomas s->selected_line +=
7187 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
7188 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
7190 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
7191 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
7193 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
7194 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
7196 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7197 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
7198 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
7199 1e37a5c2 2019-05-12 jcs view->nlines - 2);
7203 07b0611c 2022-06-23 thomas view->count = 0;
7206 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
7209 adf4c9e0 2022-07-03 thomas static const struct got_error *
7210 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
7212 adf4c9e0 2022-07-03 thomas const struct got_error *err;
7213 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
7215 adf4c9e0 2022-07-03 thomas view->count = 0;
7216 adf4c9e0 2022-07-03 thomas s->done = 1;
7217 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
7218 adf4c9e0 2022-07-03 thomas s->done = 0;
7219 adf4c9e0 2022-07-03 thomas if (err)
7220 adf4c9e0 2022-07-03 thomas return err;
7221 adf4c9e0 2022-07-03 thomas return run_blame(view);
7224 a70480e0 2018-06-23 stsp static const struct got_error *
7225 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
7227 1d98034b 2023-04-14 thomas const struct got_error *error;
7228 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
7229 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
7230 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7231 0587e10c 2020-07-23 stsp char *link_target = NULL;
7232 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7233 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7234 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *commit_id_str = NULL;
7236 51b7e1c3 2023-04-14 thomas struct tog_view *view = NULL;
7237 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7239 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7240 a70480e0 2018-06-23 stsp switch (ch) {
7242 a70480e0 2018-06-23 stsp commit_id_str = optarg;
7245 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
7246 69069811 2018-08-02 stsp if (repo_path == NULL)
7247 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7251 17020d27 2019-03-07 stsp usage_blame();
7252 a70480e0 2018-06-23 stsp /* NOTREACHED */
7256 a70480e0 2018-06-23 stsp argc -= optind;
7257 a70480e0 2018-06-23 stsp argv += optind;
7259 f135c941 2020-02-20 stsp if (argc != 1)
7260 a70480e0 2018-06-23 stsp usage_blame();
7262 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7263 7cd52833 2022-06-23 thomas if (error != NULL)
7264 7cd52833 2022-06-23 thomas goto done;
7266 69069811 2018-08-02 stsp if (repo_path == NULL) {
7267 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7268 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7269 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7270 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
7271 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7272 c156c7a4 2020-12-18 stsp goto done;
7273 f135c941 2020-02-20 stsp if (worktree)
7274 eb41ed75 2019-02-05 stsp repo_path =
7275 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7277 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
7278 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7279 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7280 c156c7a4 2020-12-18 stsp goto done;
7284 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7285 c02c541e 2019-03-29 stsp if (error != NULL)
7286 8e94dd5b 2019-01-04 stsp goto done;
7288 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7289 f135c941 2020-02-20 stsp worktree);
7290 c02c541e 2019-03-29 stsp if (error)
7291 92205607 2019-01-04 stsp goto done;
7293 557d3365 2023-04-14 thomas init_curses();
7295 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7296 51a10b52 2020-12-26 stsp if (error)
7297 51a10b52 2020-12-26 stsp goto done;
7299 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7300 eb41ed75 2019-02-05 stsp if (error)
7301 69069811 2018-08-02 stsp goto done;
7303 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
7304 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
7305 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
7306 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7307 a70480e0 2018-06-23 stsp if (error != NULL)
7308 66b4983c 2018-06-23 stsp goto done;
7309 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7310 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7312 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7313 66b04f8f 2023-07-19 thomas repo, worktree);
7314 66b04f8f 2023-07-19 thomas if (error != NULL)
7315 66b04f8f 2023-07-19 thomas goto done;
7316 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
7317 66b04f8f 2023-07-19 thomas commit_id_str = keyword_idstr;
7319 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7320 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7322 a19e88aa 2018-06-23 stsp if (error != NULL)
7323 e1cd8fed 2018-08-01 stsp goto done;
7325 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7326 945f9229 2022-04-16 thomas if (error)
7327 945f9229 2022-04-16 thomas goto done;
7329 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7330 945f9229 2022-04-16 thomas commit, repo);
7331 7cbe629d 2018-08-04 stsp if (error)
7332 7cbe629d 2018-08-04 stsp goto done;
7334 4334634c 2023-04-22 thomas view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7335 4334634c 2023-04-22 thomas if (view == NULL) {
7336 4334634c 2023-04-22 thomas error = got_error_from_errno("view_open");
7337 4334634c 2023-04-22 thomas goto done;
7339 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7340 78756c87 2020-11-24 stsp commit_id, repo);
7341 4334634c 2023-04-22 thomas if (error != NULL) {
7342 4334634c 2023-04-22 thomas if (view->close == NULL)
7343 4334634c 2023-04-22 thomas close_blame_view(view);
7344 4334634c 2023-04-22 thomas view_close(view);
7345 0587e10c 2020-07-23 stsp goto done;
7348 12314ad4 2019-08-31 stsp if (worktree) {
7349 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
7350 349dfd1e 2023-07-23 thomas if (error != NULL)
7351 349dfd1e 2023-07-23 thomas goto done;
7353 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7354 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7355 12314ad4 2019-08-31 stsp worktree = NULL;
7358 557d3365 2023-04-14 thomas error = view_loop(view);
7361 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
7362 69069811 2018-08-02 stsp free(repo_path);
7363 f135c941 2020-02-20 stsp free(in_repo_path);
7364 0587e10c 2020-07-23 stsp free(link_target);
7365 69069811 2018-08-02 stsp free(cwd);
7366 a70480e0 2018-06-23 stsp free(commit_id);
7367 66b04f8f 2023-07-19 thomas free(keyword_idstr);
7368 945f9229 2022-04-16 thomas if (commit)
7369 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7370 eb41ed75 2019-02-05 stsp if (worktree)
7371 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7372 1d0f4054 2021-06-17 stsp if (repo) {
7373 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7374 1d0f4054 2021-06-17 stsp if (error == NULL)
7375 1d0f4054 2021-06-17 stsp error = close_err;
7377 7cd52833 2022-06-23 thomas if (pack_fds) {
7378 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7379 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7380 7cd52833 2022-06-23 thomas if (error == NULL)
7381 7cd52833 2022-06-23 thomas error = pack_err;
7383 51a10b52 2020-12-26 stsp tog_free_refs();
7384 a70480e0 2018-06-23 stsp return error;
7387 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7388 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7390 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7391 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7392 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7393 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7394 86f4aab9 2022-09-09 thomas char *index = NULL;
7395 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7396 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
7397 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7399 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7400 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7401 a5d43cac 2022-07-01 thomas --limit; /* border */
7403 f7d12f7e 2018-08-01 stsp werase(view->window);
7405 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7406 ffd1d5e5 2018-06-23 stsp return NULL;
7408 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7411 ffd1d5e5 2018-06-23 stsp return err;
7412 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7413 a3404814 2018-09-02 stsp wstandout(view->window);
7414 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7416 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7417 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7418 86f4aab9 2022-09-09 thomas free(wline);
7419 86f4aab9 2022-09-09 thomas wline = NULL;
7420 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7421 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7423 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7424 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7425 a3404814 2018-09-02 stsp wstandend(view->window);
7426 86f4aab9 2022-09-09 thomas if (--limit <= 0)
7427 86f4aab9 2022-09-09 thomas return NULL;
7429 6f5f393a 2022-08-12 thomas i += s->selected;
7430 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
7431 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
7432 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
7433 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
7435 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
7436 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
7437 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7438 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
7439 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7440 86f4aab9 2022-09-09 thomas free(index);
7442 ce52c690 2018-06-23 stsp return err;
7443 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7444 2550e4c3 2018-07-13 stsp free(wline);
7445 2550e4c3 2018-07-13 stsp wline = NULL;
7446 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7447 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7448 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7449 ffd1d5e5 2018-06-23 stsp return NULL;
7450 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7451 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7452 a1eca9bb 2018-06-23 stsp return NULL;
7454 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7455 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7456 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7457 0cf4efb1 2018-09-29 stsp if (view->focussed)
7458 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7459 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7461 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7462 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7463 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7464 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7465 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7466 ffd1d5e5 2018-06-23 stsp return NULL;
7470 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7473 c72de8ab 2023-02-03 thomas view->maxx = 0;
7474 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7475 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7476 848d6979 2019-08-12 stsp const char *modestr = "";
7477 56e0773d 2019-11-28 stsp mode_t mode;
7479 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7480 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7482 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7483 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7484 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7486 638f9024 2019-05-13 stsp return got_error_from_errno(
7487 230a42bd 2019-05-11 jcs "got_object_id_str");
7489 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7490 63c5ca5d 2019-08-24 stsp modestr = "$";
7491 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7494 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7495 d86d3b18 2020-12-01 naddy te, s->repo);
7496 0d6c6ee3 2020-05-20 stsp if (err) {
7497 0d6c6ee3 2020-05-20 stsp free(id_str);
7498 0d6c6ee3 2020-05-20 stsp return err;
7500 cbb35fac 2023-06-25 thomas for (i = 0; link_target[i] != '\0'; i++) {
7501 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7502 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7504 848d6979 2019-08-12 stsp modestr = "@";
7506 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7507 848d6979 2019-08-12 stsp modestr = "/";
7508 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7509 848d6979 2019-08-12 stsp modestr = "*";
7510 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7511 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7512 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7513 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7514 1d13200f 2018-07-12 stsp free(id_str);
7515 0d6c6ee3 2020-05-20 stsp free(link_target);
7516 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7518 1d13200f 2018-07-12 stsp free(id_str);
7519 0d6c6ee3 2020-05-20 stsp free(link_target);
7521 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
7522 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7523 ffd1d5e5 2018-06-23 stsp if (err) {
7524 ffd1d5e5 2018-06-23 stsp free(line);
7527 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
7528 c72de8ab 2023-02-03 thomas free(wline);
7529 c72de8ab 2023-02-03 thomas wline = NULL;
7531 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
7532 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
7533 c72de8ab 2023-02-03 thomas if (err) {
7534 c72de8ab 2023-02-03 thomas free(line);
7537 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7538 0cf4efb1 2018-09-29 stsp if (view->focussed)
7539 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7540 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7542 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7544 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7545 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7546 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
7548 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7549 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7550 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
7551 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7552 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7553 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7554 ffd1d5e5 2018-06-23 stsp free(line);
7555 2550e4c3 2018-07-13 stsp free(wline);
7556 2550e4c3 2018-07-13 stsp wline = NULL;
7558 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7559 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7560 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7564 ffd1d5e5 2018-06-23 stsp return err;
7567 ffd1d5e5 2018-06-23 stsp static void
7568 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7570 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7571 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7572 fa86c4bf 2020-11-29 stsp int i = 0;
7574 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7577 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7578 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7579 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7580 fa86c4bf 2020-11-29 stsp if (!isroot)
7581 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7584 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7585 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7589 a5d43cac 2022-07-01 thomas static const struct got_error *
7590 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
7592 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7593 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7594 ffd1d5e5 2018-06-23 stsp int n = 0;
7596 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7597 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7598 694d3271 2020-12-01 naddy s->first_displayed_entry);
7600 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7602 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7603 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7604 07dd3ed3 2022-08-06 thomas if (last) {
7605 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7606 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7608 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7609 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7610 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7614 a5d43cac 2022-07-01 thomas return NULL;
7617 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7618 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7619 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7621 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7622 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7623 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7625 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7626 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7627 56e0773d 2019-11-28 stsp + 1 /* slash */;
7629 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7631 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7632 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7633 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7635 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7636 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7637 d9765a41 2018-06-23 stsp while (pt) {
7638 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7639 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7640 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7641 cb2ebc8a 2018-06-23 stsp goto done;
7643 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7644 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7645 cb2ebc8a 2018-06-23 stsp goto done;
7647 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7650 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7651 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7652 ce52c690 2018-06-23 stsp goto done;
7656 ce52c690 2018-06-23 stsp if (err) {
7657 ce52c690 2018-06-23 stsp free(*path);
7658 ce52c690 2018-06-23 stsp *path = NULL;
7660 ce52c690 2018-06-23 stsp return err;
7663 ce52c690 2018-06-23 stsp static const struct got_error *
7664 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7665 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7666 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7668 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7669 ce52c690 2018-06-23 stsp char *path;
7670 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7672 a0de39f3 2019-08-09 stsp *new_view = NULL;
7674 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7676 ce52c690 2018-06-23 stsp return err;
7678 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7679 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7680 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7681 83ce39e3 2019-08-12 stsp goto done;
7684 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7685 e5a0f69f 2018-08-18 stsp if (err) {
7686 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7687 fc06ba56 2019-08-22 stsp err = NULL;
7688 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7690 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7692 83ce39e3 2019-08-12 stsp free(path);
7693 69efd4c4 2018-07-18 stsp return err;
7696 69efd4c4 2018-07-18 stsp static const struct got_error *
7697 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7698 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7700 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7701 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7702 69efd4c4 2018-07-18 stsp char *path;
7704 a0de39f3 2019-08-09 stsp *new_view = NULL;
7706 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7707 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7708 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7710 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7712 69efd4c4 2018-07-18 stsp return err;
7714 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7715 92845f09 2023-07-26 thomas path, 0, NULL);
7717 e5a0f69f 2018-08-18 stsp view_close(log_view);
7719 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7720 cb2ebc8a 2018-06-23 stsp free(path);
7721 cb2ebc8a 2018-06-23 stsp return err;
7724 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7725 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7726 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7728 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7729 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7730 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7731 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7733 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7734 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7736 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7737 51b7e1c3 2023-04-14 thomas if (s->commit_id == NULL) {
7738 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
7739 51b7e1c3 2023-04-14 thomas goto done;
7742 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7744 bc573f3b 2021-07-10 stsp goto done;
7747 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7748 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7749 bc573f3b 2021-07-10 stsp * closed on demand.
7751 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7752 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7754 bc573f3b 2021-07-10 stsp goto done;
7755 bc573f3b 2021-07-10 stsp s->tree = s->root;
7757 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7758 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7759 ffd1d5e5 2018-06-23 stsp goto done;
7761 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7762 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7763 ffd1d5e5 2018-06-23 stsp goto done;
7766 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7767 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7768 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7769 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7770 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7771 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7772 9cd7cbd1 2020-12-07 stsp goto done;
7775 fb2756b9 2018-08-04 stsp s->repo = repo;
7777 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7778 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7779 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7780 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7782 c0b01bdb 2019-11-08 stsp goto done;
7783 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7784 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7786 c0b01bdb 2019-11-08 stsp goto done;
7787 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7788 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7789 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7791 c0b01bdb 2019-11-08 stsp goto done;
7793 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7794 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7795 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7797 11b20872 2019-11-08 stsp goto done;
7799 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7800 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7802 c0b01bdb 2019-11-08 stsp goto done;
7805 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7806 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7807 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7808 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7809 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7811 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7812 bc573f3b 2021-07-10 stsp if (commit)
7813 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7814 51b7e1c3 2023-04-14 thomas if (err) {
7815 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
7816 51b7e1c3 2023-04-14 thomas close_tree_view(view);
7817 51b7e1c3 2023-04-14 thomas view_close(view);
7819 ad80ab7b 2018-08-04 stsp return err;
7822 e5a0f69f 2018-08-18 stsp static const struct got_error *
7823 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7825 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7827 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7828 fb2756b9 2018-08-04 stsp free(s->tree_label);
7829 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7830 6484ec90 2018-09-29 stsp free(s->commit_id);
7831 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7832 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7833 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7834 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7835 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7836 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7837 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7838 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7839 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7840 ad80ab7b 2018-08-04 stsp free(parent);
7843 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7844 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7845 bc573f3b 2021-07-10 stsp if (s->root)
7846 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7847 7c32bd05 2019-06-22 stsp return NULL;
7850 7c32bd05 2019-06-22 stsp static const struct got_error *
7851 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7853 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7855 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7856 7c32bd05 2019-06-22 stsp return NULL;
7859 7c32bd05 2019-06-22 stsp static int
7860 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7862 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7864 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7868 7c32bd05 2019-06-22 stsp static const struct got_error *
7869 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7871 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7872 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7874 7c32bd05 2019-06-22 stsp if (!view->searching) {
7875 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7876 7c32bd05 2019-06-22 stsp return NULL;
7879 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7880 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7881 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7882 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7883 56e0773d 2019-11-28 stsp s->selected_entry);
7885 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7887 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7888 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7890 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7891 56e0773d 2019-11-28 stsp s->selected_entry);
7894 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7895 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7896 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7897 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7899 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7902 7c32bd05 2019-06-22 stsp while (1) {
7903 56e0773d 2019-11-28 stsp if (te == NULL) {
7904 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7905 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7906 ac66afb8 2019-06-24 stsp return NULL;
7908 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7909 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7911 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7914 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7915 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7916 56e0773d 2019-11-28 stsp s->matched_entry = te;
7920 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7921 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7923 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7926 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7927 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7928 7c32bd05 2019-06-22 stsp s->selected = 0;
7931 e5a0f69f 2018-08-18 stsp return NULL;
7934 ad80ab7b 2018-08-04 stsp static const struct got_error *
7935 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7937 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7938 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7939 e5a0f69f 2018-08-18 stsp char *parent_path;
7941 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7943 e5a0f69f 2018-08-18 stsp return err;
7945 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7946 e5a0f69f 2018-08-18 stsp free(parent_path);
7948 a5d43cac 2022-07-01 thomas view_border(view);
7949 e5a0f69f 2018-08-18 stsp return err;
7952 07dd3ed3 2022-08-06 thomas static const struct got_error *
7953 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7955 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7956 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7957 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7958 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7959 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7960 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7962 07dd3ed3 2022-08-06 thomas g = view->gline;
7963 07dd3ed3 2022-08-06 thomas view->gline = 0;
7965 07dd3ed3 2022-08-06 thomas if (g == 0)
7967 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7968 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7970 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7971 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7972 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7974 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7975 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7976 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7978 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7979 07dd3ed3 2022-08-06 thomas last += off;
7981 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7982 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7983 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7986 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7987 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7988 07dd3ed3 2022-08-06 thomas i += off;
7991 07dd3ed3 2022-08-06 thomas if (i < g) {
7992 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7993 07dd3ed3 2022-08-06 thomas if (err)
7994 07dd3ed3 2022-08-06 thomas return err;
7995 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7996 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7997 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7998 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7999 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
8000 07dd3ed3 2022-08-06 thomas first += off;
8001 07dd3ed3 2022-08-06 thomas s->selected = g - first;
8003 07dd3ed3 2022-08-06 thomas } else if (i > g)
8004 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
8006 07dd3ed3 2022-08-06 thomas if (g < nlines &&
8007 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
8008 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8010 07dd3ed3 2022-08-06 thomas return NULL;
8013 e5a0f69f 2018-08-18 stsp static const struct got_error *
8014 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
8016 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
8017 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
8018 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
8019 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
8021 07dd3ed3 2022-08-06 thomas if (view->gline)
8022 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
8024 e5a0f69f 2018-08-18 stsp switch (ch) {
8025 c72de8ab 2023-02-03 thomas case '0':
8026 c72de8ab 2023-02-03 thomas case '$':
8027 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
8028 c72de8ab 2023-02-03 thomas case 'l':
8029 c72de8ab 2023-02-03 thomas case KEY_LEFT:
8030 c72de8ab 2023-02-03 thomas case 'h':
8031 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8034 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
8035 07b0611c 2022-06-23 thomas view->count = 0;
8037 1be4947a 2022-07-22 thomas case 'L':
8038 07b0611c 2022-06-23 thomas view->count = 0;
8039 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
8041 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8043 1be4947a 2022-07-22 thomas case 'R':
8044 07b0611c 2022-06-23 thomas view->count = 0;
8045 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
8047 e4526bf5 2021-09-03 naddy case 'g':
8048 aa7a1117 2023-01-09 thomas case '=':
8049 e4526bf5 2021-09-03 naddy case KEY_HOME:
8050 e4526bf5 2021-09-03 naddy s->selected = 0;
8051 07b0611c 2022-06-23 thomas view->count = 0;
8052 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
8053 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
8054 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
8056 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
8058 e4526bf5 2021-09-03 naddy case 'G':
8059 aa7a1117 2023-01-09 thomas case '*':
8060 a5d43cac 2022-07-01 thomas case KEY_END: {
8061 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
8063 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8064 a5d43cac 2022-07-01 thomas --eos; /* border */
8065 e4526bf5 2021-09-03 naddy s->selected = 0;
8066 07b0611c 2022-06-23 thomas view->count = 0;
8067 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
8068 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8069 e4526bf5 2021-09-03 naddy if (te == NULL) {
8070 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
8071 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
8076 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
8077 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
8079 e4526bf5 2021-09-03 naddy if (n > 0)
8080 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8084 1e37a5c2 2019-05-12 jcs case KEY_UP:
8085 f7140bf5 2021-10-17 thomas case CTRL('p'):
8086 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
8087 1e37a5c2 2019-05-12 jcs s->selected--;
8090 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
8091 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
8092 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
8093 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
8094 07b0611c 2022-06-23 thomas view->count = 0;
8096 70f17a53 2022-06-13 thomas case CTRL('u'):
8097 23427b14 2022-06-23 thomas case 'u':
8098 70f17a53 2022-06-13 thomas nscroll /= 2;
8099 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8100 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
8101 ea025d1d 2020-02-22 naddy case CTRL('b'):
8102 1c5e5faa 2022-06-23 thomas case 'b':
8103 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
8104 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
8105 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
8106 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
8108 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
8109 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
8111 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
8112 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
8113 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
8114 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
8115 07b0611c 2022-06-23 thomas view->count = 0;
8118 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
8119 f7140bf5 2021-10-17 thomas case CTRL('n'):
8120 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
8121 1e37a5c2 2019-05-12 jcs s->selected++;
8124 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8125 07b0611c 2022-06-23 thomas == NULL) {
8126 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
8127 07b0611c 2022-06-23 thomas view->count = 0;
8130 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
8132 70f17a53 2022-06-13 thomas case CTRL('d'):
8133 23427b14 2022-06-23 thomas case 'd':
8134 70f17a53 2022-06-13 thomas nscroll /= 2;
8135 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8136 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
8137 ea025d1d 2020-02-22 naddy case CTRL('f'):
8138 1c5e5faa 2022-06-23 thomas case 'f':
8139 4c2d69cb 2022-06-23 thomas case ' ':
8140 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8142 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
8143 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
8144 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8145 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8147 07b0611c 2022-06-23 thomas view->count = 0;
8150 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
8152 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
8154 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
8155 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
8156 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
8157 1e37a5c2 2019-05-12 jcs /* user selected '..' */
8158 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
8159 07b0611c 2022-06-23 thomas view->count = 0;
8162 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
8163 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
8165 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
8166 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
8167 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
8168 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
8169 1e37a5c2 2019-05-12 jcs s->selected_entry =
8170 1e37a5c2 2019-05-12 jcs parent->selected_entry;
8171 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
8172 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
8173 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
8174 a5d43cac 2022-07-01 thomas if (err)
8177 1e37a5c2 2019-05-12 jcs free(parent);
8178 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
8179 56e0773d 2019-11-28 stsp s->selected_entry))) {
8180 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
8181 07b0611c 2022-06-23 thomas view->count = 0;
8182 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
8183 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
8186 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
8187 941e9f74 2019-05-21 stsp if (err) {
8188 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
8191 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8192 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8194 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
8195 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8196 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
8197 07b0611c 2022-06-23 thomas view->count = 0;
8200 07b0611c 2022-06-23 thomas view->count = 0;
8204 ffd1d5e5 2018-06-23 stsp return err;
8207 ffd1d5e5 2018-06-23 stsp __dead static void
8208 ffd1d5e5 2018-06-23 stsp usage_tree(void)
8211 91198554 2022-06-23 thomas fprintf(stderr,
8212 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8213 ffd1d5e5 2018-06-23 stsp getprogname());
8217 ffd1d5e5 2018-06-23 stsp static const struct got_error *
8218 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
8220 1d98034b 2023-04-14 thomas const struct got_error *error;
8221 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
8222 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
8223 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8224 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
8225 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8226 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
8227 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *label = NULL;
8228 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
8229 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
8231 5221c383 2018-08-01 stsp struct tog_view *view;
8232 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8234 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8235 ffd1d5e5 2018-06-23 stsp switch (ch) {
8237 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
8240 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
8241 74283ab8 2020-02-07 stsp if (repo_path == NULL)
8242 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
8246 e99e2d15 2020-11-24 naddy usage_tree();
8247 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
8251 ffd1d5e5 2018-06-23 stsp argc -= optind;
8252 ffd1d5e5 2018-06-23 stsp argv += optind;
8254 55cccc34 2020-02-20 stsp if (argc > 1)
8255 e99e2d15 2020-11-24 naddy usage_tree();
8257 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8258 7cd52833 2022-06-23 thomas if (error != NULL)
8259 7cd52833 2022-06-23 thomas goto done;
8261 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
8262 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8263 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8264 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8265 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
8266 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8267 c156c7a4 2020-12-18 stsp goto done;
8268 55cccc34 2020-02-20 stsp if (worktree)
8269 52185f70 2019-02-05 stsp repo_path =
8270 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
8272 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
8273 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8274 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8275 c156c7a4 2020-12-18 stsp goto done;
8279 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8280 c02c541e 2019-03-29 stsp if (error != NULL)
8281 52185f70 2019-02-05 stsp goto done;
8283 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8284 55cccc34 2020-02-20 stsp repo, worktree);
8285 55cccc34 2020-02-20 stsp if (error)
8286 55cccc34 2020-02-20 stsp goto done;
8288 557d3365 2023-04-14 thomas init_curses();
8290 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8291 c02c541e 2019-03-29 stsp if (error)
8292 52185f70 2019-02-05 stsp goto done;
8294 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8295 51a10b52 2020-12-26 stsp if (error)
8296 51a10b52 2020-12-26 stsp goto done;
8298 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
8299 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
8300 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
8301 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8302 4e97c21c 2020-12-06 stsp if (error)
8303 4e97c21c 2020-12-06 stsp goto done;
8304 4e97c21c 2020-12-06 stsp head_ref_name = label;
8306 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8307 66b04f8f 2023-07-19 thomas repo, worktree);
8308 66b04f8f 2023-07-19 thomas if (error != NULL)
8309 66b04f8f 2023-07-19 thomas goto done;
8310 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
8311 66b04f8f 2023-07-19 thomas commit_id_arg = keyword_idstr;
8313 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
8314 4e97c21c 2020-12-06 stsp if (error == NULL)
8315 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
8316 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
8317 4e97c21c 2020-12-06 stsp goto done;
8318 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
8319 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8320 4e97c21c 2020-12-06 stsp if (error)
8321 4e97c21c 2020-12-06 stsp goto done;
8324 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8325 945f9229 2022-04-16 thomas if (error)
8326 945f9229 2022-04-16 thomas goto done;
8328 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8329 5221c383 2018-08-01 stsp if (view == NULL) {
8330 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
8331 5221c383 2018-08-01 stsp goto done;
8333 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8334 ad80ab7b 2018-08-04 stsp if (error)
8335 ad80ab7b 2018-08-04 stsp goto done;
8336 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8337 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
8338 d91faf3b 2020-12-01 naddy in_repo_path);
8339 55cccc34 2020-02-20 stsp if (error)
8340 55cccc34 2020-02-20 stsp goto done;
8343 55cccc34 2020-02-20 stsp if (worktree) {
8344 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
8345 349dfd1e 2023-07-23 thomas if (error != NULL)
8346 349dfd1e 2023-07-23 thomas goto done;
8348 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8349 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8350 55cccc34 2020-02-20 stsp worktree = NULL;
8353 557d3365 2023-04-14 thomas error = view_loop(view);
8356 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
8357 66b04f8f 2023-07-19 thomas free(keyword_idstr);
8358 52185f70 2019-02-05 stsp free(repo_path);
8359 e4a0e26d 2020-02-20 stsp free(cwd);
8360 ffd1d5e5 2018-06-23 stsp free(commit_id);
8361 4e97c21c 2020-12-06 stsp free(label);
8362 ae3e7fba 2023-12-19 thomas if (commit != NULL)
8363 ae3e7fba 2023-12-19 thomas got_object_commit_close(commit);
8365 486cd271 2020-12-06 stsp got_ref_close(ref);
8366 349dfd1e 2023-07-23 thomas if (worktree != NULL)
8367 349dfd1e 2023-07-23 thomas got_worktree_close(worktree);
8368 1d0f4054 2021-06-17 stsp if (repo) {
8369 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8370 1d0f4054 2021-06-17 stsp if (error == NULL)
8371 1d0f4054 2021-06-17 stsp error = close_err;
8373 7cd52833 2022-06-23 thomas if (pack_fds) {
8374 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8375 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8376 7cd52833 2022-06-23 thomas if (error == NULL)
8377 7cd52833 2022-06-23 thomas error = pack_err;
8379 51a10b52 2020-12-26 stsp tog_free_refs();
8380 ffd1d5e5 2018-06-23 stsp return error;
8383 6458efa5 2020-11-24 stsp static const struct got_error *
8384 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8386 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8387 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8389 6458efa5 2020-11-24 stsp s->nrefs = 0;
8390 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8391 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
8392 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
8393 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
8394 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
8397 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8398 6458efa5 2020-11-24 stsp if (re == NULL)
8399 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8401 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8402 8924d611 2020-12-26 stsp if (re->ref == NULL)
8403 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8404 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8405 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8408 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8409 6458efa5 2020-11-24 stsp return NULL;
8412 ef20f542 2022-06-26 thomas static void
8413 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8415 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8417 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8418 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8419 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8420 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8425 6458efa5 2020-11-24 stsp static const struct got_error *
8426 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8428 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8429 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8431 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8432 6458efa5 2020-11-24 stsp s->repo = repo;
8434 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8435 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8437 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8439 51b7e1c3 2023-04-14 thomas goto done;
8441 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8442 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8443 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8444 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8446 6458efa5 2020-11-24 stsp goto done;
8448 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8449 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8450 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8452 6458efa5 2020-11-24 stsp goto done;
8454 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8455 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8456 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8457 2183bbf6 2022-01-23 thomas if (err)
8458 2183bbf6 2022-01-23 thomas goto done;
8460 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
8461 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
8462 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
8464 6458efa5 2020-11-24 stsp goto done;
8467 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8468 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8469 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8470 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8471 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8473 51b7e1c3 2023-04-14 thomas if (err) {
8474 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
8475 51b7e1c3 2023-04-14 thomas close_ref_view(view);
8476 51b7e1c3 2023-04-14 thomas view_close(view);
8478 6458efa5 2020-11-24 stsp return err;
8481 6458efa5 2020-11-24 stsp static const struct got_error *
8482 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8484 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8486 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8487 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8489 6458efa5 2020-11-24 stsp return NULL;
8492 6458efa5 2020-11-24 stsp static const struct got_error *
8493 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8494 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8496 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8497 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8498 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8499 6458efa5 2020-11-24 stsp int obj_type;
8501 c42c9805 2020-11-24 stsp *commit_id = NULL;
8503 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8505 6458efa5 2020-11-24 stsp return err;
8507 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8509 6458efa5 2020-11-24 stsp goto done;
8511 6458efa5 2020-11-24 stsp switch (obj_type) {
8512 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8513 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8515 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8516 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8518 6458efa5 2020-11-24 stsp goto done;
8519 c42c9805 2020-11-24 stsp free(obj_id);
8520 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8521 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8523 6458efa5 2020-11-24 stsp goto done;
8524 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8525 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8526 c42c9805 2020-11-24 stsp goto done;
8528 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8529 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8530 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8531 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8532 c42c9805 2020-11-24 stsp goto done;
8536 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8542 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8543 c42c9805 2020-11-24 stsp if (err) {
8544 c42c9805 2020-11-24 stsp free(*commit_id);
8545 c42c9805 2020-11-24 stsp *commit_id = NULL;
8547 c42c9805 2020-11-24 stsp return err;
8550 c42c9805 2020-11-24 stsp static const struct got_error *
8551 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8552 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8554 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8555 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8556 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8558 c42c9805 2020-11-24 stsp *new_view = NULL;
8560 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8561 c42c9805 2020-11-24 stsp if (err) {
8562 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8563 c42c9805 2020-11-24 stsp return err;
8565 c42c9805 2020-11-24 stsp return NULL;
8568 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8569 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8570 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8571 6458efa5 2020-11-24 stsp goto done;
8574 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8575 92845f09 2023-07-26 thomas got_ref_get_name(re->ref), "", 0, NULL);
8578 6458efa5 2020-11-24 stsp view_close(log_view);
8580 6458efa5 2020-11-24 stsp *new_view = log_view;
8581 c42c9805 2020-11-24 stsp free(commit_id);
8582 6458efa5 2020-11-24 stsp return err;
8585 ce5b7c56 2019-07-09 stsp static void
8586 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8588 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8589 34ba6917 2020-11-30 stsp int i = 0;
8591 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8594 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8595 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8596 34ba6917 2020-11-30 stsp if (re == NULL)
8598 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8599 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8603 a5d43cac 2022-07-01 thomas static const struct got_error *
8604 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
8606 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8607 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8608 6458efa5 2020-11-24 stsp int n = 0;
8610 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8611 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8613 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8615 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8616 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
8617 07dd3ed3 2022-08-06 thomas if (last) {
8618 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
8619 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
8621 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8622 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8623 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8627 a5d43cac 2022-07-01 thomas return NULL;
8630 6458efa5 2020-11-24 stsp static const struct got_error *
8631 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8633 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8635 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8636 6458efa5 2020-11-24 stsp return NULL;
8639 6458efa5 2020-11-24 stsp static int
8640 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8642 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8644 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8648 6458efa5 2020-11-24 stsp static const struct got_error *
8649 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8651 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8652 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8654 6458efa5 2020-11-24 stsp if (!view->searching) {
8655 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8656 6458efa5 2020-11-24 stsp return NULL;
8659 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8660 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8661 6458efa5 2020-11-24 stsp if (s->selected_entry)
8662 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8664 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8665 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8667 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8668 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8670 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8671 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8674 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8675 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8676 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8677 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8679 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8682 6458efa5 2020-11-24 stsp while (1) {
8683 6458efa5 2020-11-24 stsp if (re == NULL) {
8684 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8685 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8686 6458efa5 2020-11-24 stsp return NULL;
8688 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8689 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8691 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8694 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8695 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8696 6458efa5 2020-11-24 stsp s->matched_entry = re;
8700 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8701 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8703 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8706 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8707 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8708 6458efa5 2020-11-24 stsp s->selected = 0;
8711 6458efa5 2020-11-24 stsp return NULL;
8714 6458efa5 2020-11-24 stsp static const struct got_error *
8715 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8717 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8718 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8719 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8720 6458efa5 2020-11-24 stsp char *line = NULL;
8721 6458efa5 2020-11-24 stsp wchar_t *wline;
8722 6458efa5 2020-11-24 stsp struct tog_color *tc;
8723 66a70b97 2023-02-03 thomas int width, n, scrollx;
8724 6458efa5 2020-11-24 stsp int limit = view->nlines;
8726 6458efa5 2020-11-24 stsp werase(view->window);
8728 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8729 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8730 a5d43cac 2022-07-01 thomas --limit; /* border */
8732 6458efa5 2020-11-24 stsp if (limit == 0)
8733 6458efa5 2020-11-24 stsp return NULL;
8735 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8737 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8738 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8739 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8741 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8742 6458efa5 2020-11-24 stsp if (err) {
8743 6458efa5 2020-11-24 stsp free(line);
8744 6458efa5 2020-11-24 stsp return err;
8746 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8747 6458efa5 2020-11-24 stsp wstandout(view->window);
8748 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8749 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8750 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8751 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8752 6458efa5 2020-11-24 stsp wstandend(view->window);
8753 6458efa5 2020-11-24 stsp free(wline);
8754 6458efa5 2020-11-24 stsp wline = NULL;
8755 6458efa5 2020-11-24 stsp free(line);
8756 6458efa5 2020-11-24 stsp line = NULL;
8757 6458efa5 2020-11-24 stsp if (--limit <= 0)
8758 6458efa5 2020-11-24 stsp return NULL;
8761 66a70b97 2023-02-03 thomas view->maxx = 0;
8762 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8763 6458efa5 2020-11-24 stsp char *line = NULL;
8764 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8766 84227eb1 2022-06-23 thomas if (s->show_date) {
8767 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8768 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8769 84227eb1 2022-06-23 thomas struct got_object_id *id;
8770 84227eb1 2022-06-23 thomas struct tm tm;
8771 84227eb1 2022-06-23 thomas time_t t;
8773 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8774 84227eb1 2022-06-23 thomas if (err)
8775 84227eb1 2022-06-23 thomas return err;
8776 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8777 84227eb1 2022-06-23 thomas if (err) {
8778 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8779 84227eb1 2022-06-23 thomas free(id);
8780 84227eb1 2022-06-23 thomas return err;
8782 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8784 84227eb1 2022-06-23 thomas if (err) {
8785 84227eb1 2022-06-23 thomas free(id);
8786 84227eb1 2022-06-23 thomas return err;
8788 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8789 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8790 84227eb1 2022-06-23 thomas } else {
8791 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8792 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8794 84227eb1 2022-06-23 thomas free(id);
8795 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8796 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8797 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8798 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8800 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8801 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8802 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8803 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8804 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8805 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8806 6458efa5 2020-11-24 stsp struct got_object_id *id;
8807 6458efa5 2020-11-24 stsp char *id_str;
8808 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8810 6458efa5 2020-11-24 stsp return err;
8811 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8812 6458efa5 2020-11-24 stsp if (err) {
8814 6458efa5 2020-11-24 stsp return err;
8816 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8817 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8818 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8820 6458efa5 2020-11-24 stsp free(id_str);
8821 6458efa5 2020-11-24 stsp return err;
8824 6458efa5 2020-11-24 stsp free(id_str);
8825 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8826 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8827 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8829 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8830 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8831 6458efa5 2020-11-24 stsp if (err) {
8832 6458efa5 2020-11-24 stsp free(line);
8833 6458efa5 2020-11-24 stsp return err;
8835 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8836 66a70b97 2023-02-03 thomas free(wline);
8837 66a70b97 2023-02-03 thomas wline = NULL;
8839 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8840 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8841 66a70b97 2023-02-03 thomas if (err) {
8842 66a70b97 2023-02-03 thomas free(line);
8843 66a70b97 2023-02-03 thomas return err;
8845 6458efa5 2020-11-24 stsp if (n == s->selected) {
8846 6458efa5 2020-11-24 stsp if (view->focussed)
8847 6458efa5 2020-11-24 stsp wstandout(view->window);
8848 6458efa5 2020-11-24 stsp s->selected_entry = re;
8850 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8852 6458efa5 2020-11-24 stsp wattr_on(view->window,
8853 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8854 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8856 6458efa5 2020-11-24 stsp wattr_off(view->window,
8857 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8858 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8859 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8860 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8861 6458efa5 2020-11-24 stsp wstandend(view->window);
8862 6458efa5 2020-11-24 stsp free(line);
8863 6458efa5 2020-11-24 stsp free(wline);
8864 6458efa5 2020-11-24 stsp wline = NULL;
8866 6458efa5 2020-11-24 stsp s->ndisplayed++;
8867 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8870 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8873 a5d43cac 2022-07-01 thomas view_border(view);
8874 6458efa5 2020-11-24 stsp return err;
8877 6458efa5 2020-11-24 stsp static const struct got_error *
8878 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8879 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8881 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8882 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8883 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8885 c42c9805 2020-11-24 stsp *new_view = NULL;
8887 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8888 c42c9805 2020-11-24 stsp if (err) {
8889 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8890 c42c9805 2020-11-24 stsp return err;
8892 c42c9805 2020-11-24 stsp return NULL;
8896 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8897 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8898 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8899 c42c9805 2020-11-24 stsp goto done;
8902 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8903 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8905 c42c9805 2020-11-24 stsp goto done;
8907 c42c9805 2020-11-24 stsp *new_view = tree_view;
8909 c42c9805 2020-11-24 stsp free(commit_id);
8910 c42c9805 2020-11-24 stsp return err;
8913 07dd3ed3 2022-08-06 thomas static const struct got_error *
8914 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8916 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8917 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8918 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8920 07dd3ed3 2022-08-06 thomas g = view->gline;
8921 07dd3ed3 2022-08-06 thomas view->gline = 0;
8923 07dd3ed3 2022-08-06 thomas if (g == 0)
8925 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8926 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8928 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8929 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8930 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8931 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8932 07dd3ed3 2022-08-06 thomas return NULL;
8935 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8936 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8937 07dd3ed3 2022-08-06 thomas if (err)
8938 07dd3ed3 2022-08-06 thomas return err;
8939 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8940 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8941 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8942 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8943 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8944 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8946 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8947 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8949 07dd3ed3 2022-08-06 thomas return NULL;
8953 c42c9805 2020-11-24 stsp static const struct got_error *
8954 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8956 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8957 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8958 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8959 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8961 07dd3ed3 2022-08-06 thomas if (view->gline)
8962 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8964 6458efa5 2020-11-24 stsp switch (ch) {
8965 66a70b97 2023-02-03 thomas case '0':
8966 66a70b97 2023-02-03 thomas case '$':
8967 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8968 66a70b97 2023-02-03 thomas case 'l':
8969 66a70b97 2023-02-03 thomas case KEY_LEFT:
8970 66a70b97 2023-02-03 thomas case 'h':
8971 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8974 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8975 07b0611c 2022-06-23 thomas view->count = 0;
8977 84227eb1 2022-06-23 thomas case 'm':
8978 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8979 07b0611c 2022-06-23 thomas view->count = 0;
8981 98182bd0 2021-11-20 thomas case 'o':
8982 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8983 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8984 07b0611c 2022-06-23 thomas view->count = 0;
8985 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8986 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8987 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8988 c591440f 2021-11-20 thomas if (err)
8990 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8991 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8992 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8993 3bfadbd4 2021-11-20 thomas if (err)
8995 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8996 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8998 6458efa5 2020-11-24 stsp case KEY_ENTER:
8999 6458efa5 2020-11-24 stsp case '\r':
9000 07b0611c 2022-06-23 thomas view->count = 0;
9001 6458efa5 2020-11-24 stsp if (!s->selected_entry)
9003 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
9005 1be4947a 2022-07-22 thomas case 'T':
9006 07b0611c 2022-06-23 thomas view->count = 0;
9007 c42c9805 2020-11-24 stsp if (!s->selected_entry)
9009 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
9011 e4526bf5 2021-09-03 naddy case 'g':
9012 aa7a1117 2023-01-09 thomas case '=':
9013 e4526bf5 2021-09-03 naddy case KEY_HOME:
9014 e4526bf5 2021-09-03 naddy s->selected = 0;
9015 07b0611c 2022-06-23 thomas view->count = 0;
9016 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
9018 e4526bf5 2021-09-03 naddy case 'G':
9019 aa7a1117 2023-01-09 thomas case '*':
9020 a5d43cac 2022-07-01 thomas case KEY_END: {
9021 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
9023 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
9024 a5d43cac 2022-07-01 thomas --eos; /* border */
9025 e4526bf5 2021-09-03 naddy s->selected = 0;
9026 07b0611c 2022-06-23 thomas view->count = 0;
9027 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
9028 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
9029 e4526bf5 2021-09-03 naddy if (re == NULL)
9031 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
9032 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
9034 e4526bf5 2021-09-03 naddy if (n > 0)
9035 e4526bf5 2021-09-03 naddy s->selected = n - 1;
9039 6458efa5 2020-11-24 stsp case KEY_UP:
9040 f7140bf5 2021-10-17 thomas case CTRL('p'):
9041 6458efa5 2020-11-24 stsp if (s->selected > 0) {
9042 6458efa5 2020-11-24 stsp s->selected--;
9045 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
9046 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
9047 07b0611c 2022-06-23 thomas view->count = 0;
9049 70f17a53 2022-06-13 thomas case CTRL('u'):
9050 23427b14 2022-06-23 thomas case 'u':
9051 70f17a53 2022-06-13 thomas nscroll /= 2;
9052 70f17a53 2022-06-13 thomas /* FALL THROUGH */
9053 6458efa5 2020-11-24 stsp case KEY_PPAGE:
9054 6458efa5 2020-11-24 stsp case CTRL('b'):
9055 1c5e5faa 2022-06-23 thomas case 'b':
9056 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
9057 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
9058 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
9059 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
9060 07b0611c 2022-06-23 thomas view->count = 0;
9063 6458efa5 2020-11-24 stsp case KEY_DOWN:
9064 f7140bf5 2021-10-17 thomas case CTRL('n'):
9065 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
9066 6458efa5 2020-11-24 stsp s->selected++;
9069 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9070 6458efa5 2020-11-24 stsp /* can't scroll any further */
9071 07b0611c 2022-06-23 thomas view->count = 0;
9074 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
9076 70f17a53 2022-06-13 thomas case CTRL('d'):
9077 23427b14 2022-06-23 thomas case 'd':
9078 70f17a53 2022-06-13 thomas nscroll /= 2;
9079 70f17a53 2022-06-13 thomas /* FALL THROUGH */
9080 6458efa5 2020-11-24 stsp case KEY_NPAGE:
9081 6458efa5 2020-11-24 stsp case CTRL('f'):
9082 1c5e5faa 2022-06-23 thomas case 'f':
9083 4c2d69cb 2022-06-23 thomas case ' ':
9084 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9085 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
9086 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
9087 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
9088 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
9089 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
9090 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
9091 07b0611c 2022-06-23 thomas view->count = 0;
9094 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
9096 6458efa5 2020-11-24 stsp case CTRL('l'):
9097 07b0611c 2022-06-23 thomas view->count = 0;
9098 8924d611 2020-12-26 stsp tog_free_refs();
9099 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
9102 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
9103 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
9105 6458efa5 2020-11-24 stsp case KEY_RESIZE:
9106 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
9107 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
9110 07b0611c 2022-06-23 thomas view->count = 0;
9114 6458efa5 2020-11-24 stsp return err;
9117 6458efa5 2020-11-24 stsp __dead static void
9118 6458efa5 2020-11-24 stsp usage_ref(void)
9121 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
9122 6458efa5 2020-11-24 stsp getprogname());
9126 6458efa5 2020-11-24 stsp static const struct got_error *
9127 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
9129 1d98034b 2023-04-14 thomas const struct got_error *error;
9130 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
9131 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
9132 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
9134 6458efa5 2020-11-24 stsp struct tog_view *view;
9135 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9137 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
9138 6458efa5 2020-11-24 stsp switch (ch) {
9140 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
9141 6458efa5 2020-11-24 stsp if (repo_path == NULL)
9142 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
9146 e99e2d15 2020-11-24 naddy usage_ref();
9147 6458efa5 2020-11-24 stsp /* NOTREACHED */
9151 6458efa5 2020-11-24 stsp argc -= optind;
9152 6458efa5 2020-11-24 stsp argv += optind;
9154 6458efa5 2020-11-24 stsp if (argc > 1)
9155 e99e2d15 2020-11-24 naddy usage_ref();
9157 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9158 7cd52833 2022-06-23 thomas if (error != NULL)
9159 7cd52833 2022-06-23 thomas goto done;
9161 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
9162 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
9163 c156c7a4 2020-12-18 stsp if (cwd == NULL)
9164 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
9165 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
9166 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9167 c156c7a4 2020-12-18 stsp goto done;
9168 6458efa5 2020-11-24 stsp if (worktree)
9169 6458efa5 2020-11-24 stsp repo_path =
9170 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
9172 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
9173 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
9174 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
9175 c156c7a4 2020-12-18 stsp goto done;
9179 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9180 6458efa5 2020-11-24 stsp if (error != NULL)
9181 6458efa5 2020-11-24 stsp goto done;
9183 557d3365 2023-04-14 thomas init_curses();
9185 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
9186 51a10b52 2020-12-26 stsp if (error)
9187 51a10b52 2020-12-26 stsp goto done;
9189 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9190 6458efa5 2020-11-24 stsp if (error)
9191 6458efa5 2020-11-24 stsp goto done;
9193 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9194 6458efa5 2020-11-24 stsp if (view == NULL) {
9195 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
9196 6458efa5 2020-11-24 stsp goto done;
9199 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
9200 6458efa5 2020-11-24 stsp if (error)
9201 6458efa5 2020-11-24 stsp goto done;
9203 6458efa5 2020-11-24 stsp if (worktree) {
9204 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
9205 349dfd1e 2023-07-23 thomas if (error != NULL)
9206 349dfd1e 2023-07-23 thomas goto done;
9208 6458efa5 2020-11-24 stsp /* Release work tree lock. */
9209 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
9210 6458efa5 2020-11-24 stsp worktree = NULL;
9213 557d3365 2023-04-14 thomas error = view_loop(view);
9216 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
9217 6458efa5 2020-11-24 stsp free(repo_path);
9218 6458efa5 2020-11-24 stsp free(cwd);
9219 349dfd1e 2023-07-23 thomas if (worktree != NULL)
9220 349dfd1e 2023-07-23 thomas got_worktree_close(worktree);
9221 1d0f4054 2021-06-17 stsp if (repo) {
9222 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9223 1d0f4054 2021-06-17 stsp if (close_err)
9224 1d0f4054 2021-06-17 stsp error = close_err;
9226 7cd52833 2022-06-23 thomas if (pack_fds) {
9227 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9228 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9229 7cd52833 2022-06-23 thomas if (error == NULL)
9230 7cd52833 2022-06-23 thomas error = pack_err;
9232 51a10b52 2020-12-26 stsp tog_free_refs();
9233 6458efa5 2020-11-24 stsp return error;
9236 fc2737d5 2022-09-15 thomas static const struct got_error*
9237 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9238 fc2737d5 2022-09-15 thomas const char *str)
9240 fc2737d5 2022-09-15 thomas size_t len;
9242 fc2737d5 2022-09-15 thomas if (win == NULL)
9243 fc2737d5 2022-09-15 thomas win = stdscr;
9245 fc2737d5 2022-09-15 thomas len = strlen(str);
9246 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9248 fc2737d5 2022-09-15 thomas if (focus)
9249 fc2737d5 2022-09-15 thomas wstandout(win);
9250 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
9251 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9252 fc2737d5 2022-09-15 thomas if (focus)
9253 fc2737d5 2022-09-15 thomas wstandend(win);
9255 fc2737d5 2022-09-15 thomas return NULL;
9258 2a31b33b 2022-07-23 thomas static const struct got_error *
9259 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9261 fc2737d5 2022-09-15 thomas off_t *p;
9263 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9264 fc2737d5 2022-09-15 thomas if (p == NULL) {
9265 fc2737d5 2022-09-15 thomas free(*line_offsets);
9266 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
9267 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
9270 fc2737d5 2022-09-15 thomas *line_offsets = p;
9271 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
9272 fc2737d5 2022-09-15 thomas ++(*nlines);
9273 fc2737d5 2022-09-15 thomas return NULL;
9276 fc2737d5 2022-09-15 thomas static const struct got_error *
9277 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9279 fc2737d5 2022-09-15 thomas *ret = 0;
9281 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
9282 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9283 fc2737d5 2022-09-15 thomas size_t len = 1;
9285 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
9286 fc2737d5 2022-09-15 thomas continue;
9288 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9289 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9290 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9292 fc2737d5 2022-09-15 thomas len += strlen(t);
9293 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
9294 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
9295 fc2737d5 2022-09-15 thomas free(t0);
9296 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
9299 fc2737d5 2022-09-15 thomas return NULL;
9303 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
9304 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
9305 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
9307 fc2737d5 2022-09-15 thomas static const struct got_error *
9308 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9310 fc2737d5 2022-09-15 thomas int n, len = width;
9312 fc2737d5 2022-09-15 thomas if (km->keys) {
9313 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
9314 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9315 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
9317 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9318 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
9320 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
9322 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9323 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9324 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9326 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
9327 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
9328 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
9329 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9330 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
9331 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9332 fc2737d5 2022-09-15 thomas if (n < 0) {
9333 fc2737d5 2022-09-15 thomas free(t0);
9334 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9336 fc2737d5 2022-09-15 thomas first = 0;
9337 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
9338 fc2737d5 2022-09-15 thomas *off += n;
9340 fc2737d5 2022-09-15 thomas free(t0);
9342 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9343 fc2737d5 2022-09-15 thomas if (n < 0)
9344 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9345 fc2737d5 2022-09-15 thomas *off += n;
9347 fc2737d5 2022-09-15 thomas return NULL;
9350 fc2737d5 2022-09-15 thomas static const struct got_error *
9351 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
9353 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9354 fc2737d5 2022-09-15 thomas off_t off = 0;
9355 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
9356 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
9357 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
9358 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9359 fc2737d5 2022-09-15 thomas GENERATE_HELP
9360 fc2737d5 2022-09-15 thomas #undef KEYMAP_
9361 fc2737d5 2022-09-15 thomas #undef KEY_
9364 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9365 fc2737d5 2022-09-15 thomas if (err)
9366 fc2737d5 2022-09-15 thomas return err;
9368 fc2737d5 2022-09-15 thomas n = nitems(km);
9369 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
9370 fc2737d5 2022-09-15 thomas if (err)
9371 fc2737d5 2022-09-15 thomas return err;
9373 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
9374 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
9375 fc2737d5 2022-09-15 thomas show = s->all;
9376 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
9377 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
9378 fc2737d5 2022-09-15 thomas show = 1;
9380 fc2737d5 2022-09-15 thomas if (show) {
9381 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
9382 fc2737d5 2022-09-15 thomas if (err)
9383 fc2737d5 2022-09-15 thomas return err;
9384 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9385 fc2737d5 2022-09-15 thomas if (err)
9386 fc2737d5 2022-09-15 thomas return err;
9389 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
9391 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9392 fc2737d5 2022-09-15 thomas return err;
9395 fc2737d5 2022-09-15 thomas static const struct got_error *
9396 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
9398 fc2737d5 2022-09-15 thomas FILE *f;
9399 fc2737d5 2022-09-15 thomas const struct got_error *err;
9401 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9402 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9403 fc2737d5 2022-09-15 thomas s->nlines = 0;
9405 fc2737d5 2022-09-15 thomas f = got_opentemp();
9406 fc2737d5 2022-09-15 thomas if (f == NULL)
9407 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
9408 fc2737d5 2022-09-15 thomas s->f = f;
9410 fc2737d5 2022-09-15 thomas err = format_help(s);
9411 fc2737d5 2022-09-15 thomas if (err)
9412 fc2737d5 2022-09-15 thomas return err;
9414 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
9415 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
9417 fc2737d5 2022-09-15 thomas return NULL;
9420 fc2737d5 2022-09-15 thomas static const struct got_error *
9421 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
9423 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
9424 fc2737d5 2022-09-15 thomas return NULL;
9427 2a7d3cd7 2022-09-17 thomas static void
9428 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9429 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
9431 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
9433 2a7d3cd7 2022-09-17 thomas *f = s->f;
9434 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
9435 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
9436 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
9437 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
9438 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
9439 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
9442 fc2737d5 2022-09-15 thomas static const struct got_error *
9443 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
9445 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9446 fc2737d5 2022-09-15 thomas const struct got_error *err;
9447 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
9448 fc2737d5 2022-09-15 thomas wchar_t *wline;
9449 fc2737d5 2022-09-15 thomas char *line;
9450 fc2737d5 2022-09-15 thomas ssize_t linelen;
9451 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9452 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
9453 fc2737d5 2022-09-15 thomas int eos = view->nlines;
9455 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9456 fc2737d5 2022-09-15 thomas --eos; /* account for border */
9458 fc2737d5 2022-09-15 thomas s->lineno = 0;
9459 fc2737d5 2022-09-15 thomas rewind(s->f);
9460 fc2737d5 2022-09-15 thomas werase(view->window);
9462 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
9463 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
9465 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
9466 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
9467 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
9468 fc2737d5 2022-09-15 thomas if (err)
9469 fc2737d5 2022-09-15 thomas return err;
9470 fc2737d5 2022-09-15 thomas if (eos <= 1)
9471 fc2737d5 2022-09-15 thomas return NULL;
9472 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
9473 fc2737d5 2022-09-15 thomas eos -= 2;
9475 fc2737d5 2022-09-15 thomas s->eof = 0;
9476 fc2737d5 2022-09-15 thomas view->maxx = 0;
9477 fc2737d5 2022-09-15 thomas line = NULL;
9478 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
9479 fc2737d5 2022-09-15 thomas attr_t attr = 0;
9481 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9482 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9483 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
9484 fc2737d5 2022-09-15 thomas free(line);
9485 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
9487 fc2737d5 2022-09-15 thomas s->eof = 1;
9490 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
9491 fc2737d5 2022-09-15 thomas continue;
9492 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9493 fc2737d5 2022-09-15 thomas continue;
9494 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
9495 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
9497 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9498 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
9499 fc2737d5 2022-09-15 thomas if (err) {
9500 fc2737d5 2022-09-15 thomas free(line);
9501 fc2737d5 2022-09-15 thomas return err;
9503 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
9504 fc2737d5 2022-09-15 thomas free(wline);
9505 fc2737d5 2022-09-15 thomas wline = NULL;
9507 fc2737d5 2022-09-15 thomas if (attr)
9508 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
9509 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
9510 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9511 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
9512 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
9513 fc2737d5 2022-09-15 thomas if (err) {
9514 fc2737d5 2022-09-15 thomas free(line);
9515 fc2737d5 2022-09-15 thomas return err;
9517 fc2737d5 2022-09-15 thomas } else {
9518 fc2737d5 2022-09-15 thomas int skip;
9520 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
9521 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
9522 fc2737d5 2022-09-15 thomas if (err) {
9523 fc2737d5 2022-09-15 thomas free(line);
9524 fc2737d5 2022-09-15 thomas return err;
9526 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
9527 fc2737d5 2022-09-15 thomas free(wline);
9528 fc2737d5 2022-09-15 thomas wline = NULL;
9530 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
9531 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
9532 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
9533 fc2737d5 2022-09-15 thomas } else {
9534 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
9535 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
9537 fc2737d5 2022-09-15 thomas if (attr)
9538 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
9539 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
9540 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
9542 fc2737d5 2022-09-15 thomas free(line);
9543 fc2737d5 2022-09-15 thomas if (nprinted > 0)
9544 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9546 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
9548 fc2737d5 2022-09-15 thomas view_border(view);
9550 fc2737d5 2022-09-15 thomas if (s->eof) {
9551 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
9552 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
9553 fc2737d5 2022-09-15 thomas view->ncols - 1);
9554 fc2737d5 2022-09-15 thomas if (rc == ERR)
9555 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9556 fc2737d5 2022-09-15 thomas } else {
9557 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
9558 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
9559 fc2737d5 2022-09-15 thomas wstandout(view->window);
9560 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
9561 fc2737d5 2022-09-15 thomas view->ncols - 1);
9562 fc2737d5 2022-09-15 thomas if (rc == ERR)
9563 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9564 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
9565 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
9566 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
9567 fc2737d5 2022-09-15 thomas if (rc == ERR)
9568 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
9570 fc2737d5 2022-09-15 thomas wstandend(view->window);
9573 fc2737d5 2022-09-15 thomas return NULL;
9576 fc2737d5 2022-09-15 thomas static const struct got_error *
9577 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9579 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9580 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9581 fc2737d5 2022-09-15 thomas char *line = NULL;
9582 fc2737d5 2022-09-15 thomas ssize_t linelen;
9583 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9584 fc2737d5 2022-09-15 thomas int eos, nscroll;
9586 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
9587 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9588 fc2737d5 2022-09-15 thomas --eos; /* border */
9590 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
9592 fc2737d5 2022-09-15 thomas switch (ch) {
9593 fc2737d5 2022-09-15 thomas case '0':
9594 fc2737d5 2022-09-15 thomas case '$':
9595 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
9596 fc2737d5 2022-09-15 thomas case 'l':
9597 fc2737d5 2022-09-15 thomas case KEY_LEFT:
9598 fc2737d5 2022-09-15 thomas case 'h':
9599 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
9601 fc2737d5 2022-09-15 thomas case 'g':
9602 fc2737d5 2022-09-15 thomas case KEY_HOME:
9603 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9604 fc2737d5 2022-09-15 thomas view->count = 0;
9606 fc2737d5 2022-09-15 thomas case 'G':
9607 fc2737d5 2022-09-15 thomas case KEY_END:
9608 fc2737d5 2022-09-15 thomas view->count = 0;
9609 fc2737d5 2022-09-15 thomas if (s->eof)
9611 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
9612 fc2737d5 2022-09-15 thomas s->eof = 1;
9614 fc2737d5 2022-09-15 thomas case 'k':
9615 fc2737d5 2022-09-15 thomas case KEY_UP:
9616 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
9617 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
9619 fc2737d5 2022-09-15 thomas view->count = 0;
9621 fc2737d5 2022-09-15 thomas case CTRL('u'):
9622 fc2737d5 2022-09-15 thomas case 'u':
9623 fc2737d5 2022-09-15 thomas nscroll /= 2;
9624 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9625 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
9626 fc2737d5 2022-09-15 thomas case CTRL('b'):
9627 fc2737d5 2022-09-15 thomas case 'b':
9628 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
9629 fc2737d5 2022-09-15 thomas view->count = 0;
9632 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
9633 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
9635 fc2737d5 2022-09-15 thomas case 'j':
9636 fc2737d5 2022-09-15 thomas case KEY_DOWN:
9637 fc2737d5 2022-09-15 thomas case CTRL('n'):
9638 fc2737d5 2022-09-15 thomas if (!s->eof)
9639 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9641 fc2737d5 2022-09-15 thomas view->count = 0;
9643 fc2737d5 2022-09-15 thomas case CTRL('d'):
9644 fc2737d5 2022-09-15 thomas case 'd':
9645 fc2737d5 2022-09-15 thomas nscroll /= 2;
9646 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9647 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9648 fc2737d5 2022-09-15 thomas case CTRL('f'):
9649 fc2737d5 2022-09-15 thomas case 'f':
9650 fc2737d5 2022-09-15 thomas case ' ':
9651 fc2737d5 2022-09-15 thomas if (s->eof) {
9652 fc2737d5 2022-09-15 thomas view->count = 0;
9655 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9656 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9657 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9658 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9659 fc2737d5 2022-09-15 thomas if (feof(s->f))
9660 fc2737d5 2022-09-15 thomas s->eof = 1;
9662 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9666 fc2737d5 2022-09-15 thomas free(line);
9668 fc2737d5 2022-09-15 thomas default:
9669 fc2737d5 2022-09-15 thomas view->count = 0;
9673 fc2737d5 2022-09-15 thomas return err;
9676 fc2737d5 2022-09-15 thomas static const struct got_error *
9677 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9679 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9681 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9682 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9683 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9684 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9686 fc2737d5 2022-09-15 thomas return NULL;
9689 fc2737d5 2022-09-15 thomas static const struct got_error *
9690 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9692 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9695 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9696 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9698 fc2737d5 2022-09-15 thomas wclear(view->window);
9699 fc2737d5 2022-09-15 thomas view->count = 0;
9700 fc2737d5 2022-09-15 thomas view->x = 0;
9701 fc2737d5 2022-09-15 thomas s->all = !s->all;
9702 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9703 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9704 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9706 fc2737d5 2022-09-15 thomas return create_help(s);
9709 fc2737d5 2022-09-15 thomas static const struct got_error *
9710 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9712 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9713 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9715 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9716 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9717 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9718 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9720 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9721 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9722 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9723 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9724 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9725 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9726 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9728 fc2737d5 2022-09-15 thomas err = create_help(s);
9729 fc2737d5 2022-09-15 thomas return err;
9732 fc2737d5 2022-09-15 thomas static const struct got_error *
9733 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9734 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9736 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9738 2a31b33b 2022-07-23 thomas *new_view = NULL;
9740 2a31b33b 2022-07-23 thomas switch (request) {
9741 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9742 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9743 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9745 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9746 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9747 2a31b33b 2022-07-23 thomas view, s->repo);
9749 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9750 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9752 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9753 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9754 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9756 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9757 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9758 2a31b33b 2022-07-23 thomas s->repo);
9760 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9761 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9763 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9764 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9765 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9766 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9767 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9768 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9769 2a31b33b 2022-07-23 thomas &view->state.tree);
9770 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9771 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9772 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9773 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9775 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9776 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9778 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9779 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9780 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9781 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9782 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9783 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9784 2a31b33b 2022-07-23 thomas view->state.log.repo);
9785 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9786 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9787 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9788 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9790 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9791 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9793 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9794 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9795 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9796 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9797 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9798 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9799 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9800 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9802 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9803 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9804 2a31b33b 2022-07-23 thomas if (err)
9805 2a31b33b 2022-07-23 thomas view_close(*new_view);
9807 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9808 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9809 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9810 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9811 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9812 fc2737d5 2022-09-15 thomas if (err)
9813 fc2737d5 2022-09-15 thomas view_close(*new_view);
9815 2a31b33b 2022-07-23 thomas default:
9816 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9819 2a31b33b 2022-07-23 thomas return err;
9823 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9824 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9826 6458efa5 2020-11-24 stsp static void
9827 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9829 a5d43cac 2022-07-01 thomas switch (view->type) {
9830 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9831 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9832 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9833 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9837 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9838 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9840 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9841 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9844 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9845 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9846 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9848 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9849 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9850 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9852 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9853 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9854 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9856 a5d43cac 2022-07-01 thomas default:
9860 a5d43cac 2022-07-01 thomas view->offset = 0;
9864 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9865 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9867 a5d43cac 2022-07-01 thomas static const struct got_error *
9868 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9870 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9871 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9872 a5d43cac 2022-07-01 thomas int *selected = NULL;
9873 a5d43cac 2022-07-01 thomas int header, offset;
9875 a5d43cac 2022-07-01 thomas switch (view->type) {
9876 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9877 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9878 a5d43cac 2022-07-01 thomas header = 3;
9879 a5d43cac 2022-07-01 thomas scrolld = NULL;
9880 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9881 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9882 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9883 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9884 a5d43cac 2022-07-01 thomas view->offset = offset;
9888 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9889 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9890 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9891 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9892 a5d43cac 2022-07-01 thomas selected = &s->selected;
9895 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9896 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9897 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9898 a5d43cac 2022-07-01 thomas header = 3;
9899 a5d43cac 2022-07-01 thomas selected = &s->selected;
9902 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9903 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9904 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9905 a5d43cac 2022-07-01 thomas header = 5;
9906 a5d43cac 2022-07-01 thomas selected = &s->selected;
9909 a5d43cac 2022-07-01 thomas default:
9910 a5d43cac 2022-07-01 thomas selected = NULL;
9911 a5d43cac 2022-07-01 thomas scrolld = NULL;
9912 a5d43cac 2022-07-01 thomas header = 0;
9916 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9917 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9918 a5d43cac 2022-07-01 thomas view->offset = offset;
9919 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9920 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9921 a5d43cac 2022-07-01 thomas *selected -= offset;
9925 a5d43cac 2022-07-01 thomas return err;
9928 a5d43cac 2022-07-01 thomas static void
9929 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9933 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9934 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9935 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9936 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9938 6879ba42 2020-10-01 naddy fputc('\n', fp);
9941 4ed7e80c 2018-05-20 stsp __dead static void
9942 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9944 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9946 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9947 6879ba42 2020-10-01 naddy getprogname());
9948 6879ba42 2020-10-01 naddy if (hflag) {
9949 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9950 6879ba42 2020-10-01 naddy list_commands(fp);
9952 6879ba42 2020-10-01 naddy exit(status);
9955 c2301be8 2018-04-30 stsp static char **
9956 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9958 ee85c5e8 2020-02-29 stsp va_list ap;
9959 c2301be8 2018-04-30 stsp char **argv;
9962 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9964 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9965 c2301be8 2018-04-30 stsp if (argv == NULL)
9966 c2301be8 2018-04-30 stsp err(1, "calloc");
9967 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9968 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9969 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9970 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9973 ee85c5e8 2020-02-29 stsp va_end(ap);
9974 c2301be8 2018-04-30 stsp return argv;
9978 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9979 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9980 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9981 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9982 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9984 ee85c5e8 2020-02-29 stsp static const struct got_error *
9985 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9987 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9988 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9989 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9990 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9991 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9992 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9993 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9994 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9995 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9997 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9998 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9999 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
10001 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
10002 7cd52833 2022-06-23 thomas if (error != NULL)
10003 7cd52833 2022-06-23 thomas goto done;
10005 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
10006 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
10007 ee85c5e8 2020-02-29 stsp goto done;
10009 ee85c5e8 2020-02-29 stsp if (worktree)
10010 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
10012 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
10013 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
10014 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
10015 ee85c5e8 2020-02-29 stsp goto done;
10018 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
10019 ee85c5e8 2020-02-29 stsp if (error != NULL)
10020 ee85c5e8 2020-02-29 stsp goto done;
10022 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
10023 ee85c5e8 2020-02-29 stsp repo, worktree);
10024 ee85c5e8 2020-02-29 stsp if (error)
10025 ee85c5e8 2020-02-29 stsp goto done;
10027 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
10028 84de9106 2020-12-26 stsp if (error)
10029 84de9106 2020-12-26 stsp goto done;
10030 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
10031 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
10032 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
10033 ee85c5e8 2020-02-29 stsp if (error)
10034 ee85c5e8 2020-02-29 stsp goto done;
10036 ee85c5e8 2020-02-29 stsp if (worktree) {
10037 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
10038 ee85c5e8 2020-02-29 stsp worktree = NULL;
10041 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
10042 945f9229 2022-04-16 thomas if (error)
10043 945f9229 2022-04-16 thomas goto done;
10045 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
10046 ee85c5e8 2020-02-29 stsp if (error) {
10047 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
10048 ee85c5e8 2020-02-29 stsp goto done;
10049 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
10050 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
10051 6879ba42 2020-10-01 naddy usage(1, 1);
10052 ee85c5e8 2020-02-29 stsp /* not reached */
10055 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
10056 ee85c5e8 2020-02-29 stsp if (error)
10057 ee85c5e8 2020-02-29 stsp goto done;
10059 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
10060 ee85c5e8 2020-02-29 stsp argc = 4;
10061 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
10062 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
10064 1d0f4054 2021-06-17 stsp if (repo) {
10065 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
10066 1d0f4054 2021-06-17 stsp if (error == NULL)
10067 1d0f4054 2021-06-17 stsp error = close_err;
10069 945f9229 2022-04-16 thomas if (commit)
10070 945f9229 2022-04-16 thomas got_object_commit_close(commit);
10071 ee85c5e8 2020-02-29 stsp if (worktree)
10072 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
10073 7cd52833 2022-06-23 thomas if (pack_fds) {
10074 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
10075 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
10076 7cd52833 2022-06-23 thomas if (error == NULL)
10077 7cd52833 2022-06-23 thomas error = pack_err;
10079 ee85c5e8 2020-02-29 stsp free(id);
10080 ee85c5e8 2020-02-29 stsp free(commit_id_str);
10081 ee85c5e8 2020-02-29 stsp free(commit_id);
10082 ee85c5e8 2020-02-29 stsp free(cwd);
10083 ee85c5e8 2020-02-29 stsp free(repo_path);
10084 ee85c5e8 2020-02-29 stsp free(in_repo_path);
10085 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
10087 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
10088 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
10089 ee85c5e8 2020-02-29 stsp free(cmd_argv);
10091 87670572 2020-12-26 naddy tog_free_refs();
10092 ee85c5e8 2020-02-29 stsp return error;
10096 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
10098 1d98034b 2023-04-14 thomas const struct got_error *io_err, *error = NULL;
10099 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
10100 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
10101 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
10102 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
10103 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
10104 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
10106 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
10107 557d3365 2023-04-14 thomas const char *test_script_path;
10109 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
10112 e2a79cf9 2023-07-26 thomas * Override default signal handlers before starting ncurses.
10113 e2a79cf9 2023-07-26 thomas * This should prevent ncurses from installing its own
10114 e2a79cf9 2023-07-26 thomas * broken cleanup() signal handler.
10116 e2a79cf9 2023-07-26 thomas signal(SIGWINCH, tog_sigwinch);
10117 e2a79cf9 2023-07-26 thomas signal(SIGPIPE, tog_sigpipe);
10118 e2a79cf9 2023-07-26 thomas signal(SIGCONT, tog_sigcont);
10119 e2a79cf9 2023-07-26 thomas signal(SIGINT, tog_sigint);
10120 e2a79cf9 2023-07-26 thomas signal(SIGTERM, tog_sigterm);
10123 557d3365 2023-04-14 thomas * Test mode init must happen before pledge() because "tty" will
10124 557d3365 2023-04-14 thomas * not allow TTY-related ioctls to occur via regular files.
10126 fa9bb690 2023-04-22 thomas test_script_path = getenv("TOG_TEST_SCRIPT");
10127 557d3365 2023-04-14 thomas if (test_script_path != NULL) {
10128 557d3365 2023-04-14 thomas error = init_mock_term(test_script_path);
10129 557d3365 2023-04-14 thomas if (error) {
10130 557d3365 2023-04-14 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10131 557d3365 2023-04-14 thomas return 1;
10133 5ffbe221 2023-04-22 thomas } else if (!isatty(STDIN_FILENO))
10134 5ffbe221 2023-04-22 thomas errx(1, "standard input is not a tty");
10136 557d3365 2023-04-14 thomas #if !defined(PROFILE)
10137 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
10138 a0abeae5 2023-02-17 thomas NULL) == -1)
10139 a0abeae5 2023-02-17 thomas err(1, "pledge");
10142 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
10143 9f7d7167 2018-04-29 stsp switch (ch) {
10144 9f7d7167 2018-04-29 stsp case 'h':
10145 9f7d7167 2018-04-29 stsp hflag = 1;
10147 53ccebc2 2019-07-30 stsp case 'V':
10148 53ccebc2 2019-07-30 stsp Vflag = 1;
10151 6879ba42 2020-10-01 naddy usage(hflag, 1);
10152 9f7d7167 2018-04-29 stsp /* NOTREACHED */
10156 9f7d7167 2018-04-29 stsp argc -= optind;
10157 9f7d7167 2018-04-29 stsp argv += optind;
10158 9814e6a3 2020-09-27 naddy optind = 1;
10159 c2301be8 2018-04-30 stsp optreset = 1;
10161 53ccebc2 2019-07-30 stsp if (Vflag) {
10162 53ccebc2 2019-07-30 stsp got_version_print_str();
10163 6879ba42 2020-10-01 naddy return 0;
10166 c2301be8 2018-04-30 stsp if (argc == 0) {
10167 f29d3e89 2018-06-23 stsp if (hflag)
10168 6879ba42 2020-10-01 naddy usage(hflag, 0);
10169 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
10170 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
10171 c2301be8 2018-04-30 stsp argc = 1;
10172 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
10174 6059809a 2020-12-17 stsp size_t i;
10176 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
10177 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
10178 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
10179 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
10180 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
10186 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
10187 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
10188 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
10189 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10190 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
10191 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10194 92845f09 2023-07-26 thomas tog_base_commit.idx = -1;
10195 92845f09 2023-07-26 thomas tog_base_commit.marker = GOT_WORKTREE_STATE_UNKNOWN;
10197 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
10198 ee85c5e8 2020-02-29 stsp if (argc != 1)
10199 6879ba42 2020-10-01 naddy usage(0, 1);
10200 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
10201 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
10203 ee85c5e8 2020-02-29 stsp if (hflag)
10204 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
10206 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10209 1d98034b 2023-04-14 thomas if (using_mock_io) {
10210 1d98034b 2023-04-14 thomas io_err = tog_io_close();
10211 1d98034b 2023-04-14 thomas if (error == NULL)
10212 1d98034b 2023-04-14 thomas error = io_err;
10214 9f7d7167 2018-04-29 stsp endwin();
10215 a2f4a359 2020-02-28 stsp if (cmd_argv) {
10217 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
10218 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
10219 a2f4a359 2020-02-28 stsp free(cmd_argv);
10222 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
10223 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
10224 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
10225 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
10226 66b04f8f 2023-07-19 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10227 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10228 66b04f8f 2023-07-19 thomas return 1;
10230 9f7d7167 2018-04-29 stsp return 0;