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 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3543 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3544 1a76625f 2018-10-22 stsp free(s->start_id);
3545 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3546 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3547 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3548 1a76625f 2018-10-22 stsp return err;
3552 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3553 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3554 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3555 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3556 7e8004ba 2022-09-11 thomas * works with very slight change.
3558 1a76625f 2018-10-22 stsp static const struct got_error *
3559 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3561 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3562 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3563 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3564 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3565 7e8004ba 2022-09-11 thomas char pattern[1024];
3566 7e8004ba 2022-09-11 thomas int ret;
3568 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3569 7e8004ba 2022-09-11 thomas v = view->child;
3570 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3571 7e8004ba 2022-09-11 thomas v = view->parent;
3573 1fc091f3 2023-09-05 thomas if (tog_io.input_str != NULL) {
3574 1fc091f3 2023-09-05 thomas if (strlcpy(pattern, tog_io.input_str, sizeof(pattern)) >=
3575 1fc091f3 2023-09-05 thomas sizeof(pattern))
3576 1fc091f3 2023-09-05 thomas return got_error(GOT_ERR_NO_SPACE);
3577 1fc091f3 2023-09-05 thomas } else {
3578 1fc091f3 2023-09-05 thomas wmove(v->window, v->nlines - 1, 0);
3579 1fc091f3 2023-09-05 thomas wclrtoeol(v->window);
3580 1fc091f3 2023-09-05 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3581 1fc091f3 2023-09-05 thomas nodelay(v->window, FALSE);
3582 1fc091f3 2023-09-05 thomas nocbreak();
3584 1fc091f3 2023-09-05 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3585 1fc091f3 2023-09-05 thomas cbreak();
3586 1fc091f3 2023-09-05 thomas noecho();
3587 1fc091f3 2023-09-05 thomas nodelay(v->window, TRUE);
3588 1fc091f3 2023-09-05 thomas if (ret == ERR)
3589 1fc091f3 2023-09-05 thomas return NULL;
3592 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3594 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3595 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3597 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3598 7e8004ba 2022-09-11 thomas return NULL;
3601 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3602 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3603 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3604 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3605 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3606 7e8004ba 2022-09-11 thomas * the queue.
3608 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3609 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3610 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3611 7e8004ba 2022-09-11 thomas s->selected = 0;
3612 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3614 7e8004ba 2022-09-11 thomas return NULL;
3617 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3618 7e8004ba 2022-09-11 thomas return NULL;
3620 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3622 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3623 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3624 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3625 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3626 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3628 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3629 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3630 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3632 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3633 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3634 7e8004ba 2022-09-11 thomas int have_match = 0;
3636 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3637 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3638 7e8004ba 2022-09-11 thomas if (err)
3639 7e8004ba 2022-09-11 thomas return err;
3641 7e8004ba 2022-09-11 thomas if (have_match) {
3642 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3644 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3645 7e8004ba 2022-09-11 thomas entry->id);
3646 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3647 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3648 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3651 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3652 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3654 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3655 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3656 7e8004ba 2022-09-11 thomas matched, entry);
3657 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3661 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3662 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3663 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3664 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3665 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3666 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3667 7e8004ba 2022-09-11 thomas if (err)
3668 7e8004ba 2022-09-11 thomas return err;
3671 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3672 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3673 7e8004ba 2022-09-11 thomas s->selected = 0;
3675 7e8004ba 2022-09-11 thomas return NULL;
3678 7e8004ba 2022-09-11 thomas static const struct got_error *
3679 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3681 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3683 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3684 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3685 60493ae3 2019-06-20 stsp return NULL;
3688 60493ae3 2019-06-20 stsp static const struct got_error *
3689 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3691 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3692 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3693 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3695 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3696 f9686aa5 2020-03-27 stsp show_log_view(view);
3697 f9686aa5 2020-03-27 stsp update_panels();
3698 f9686aa5 2020-03-27 stsp doupdate();
3700 96e2b566 2019-07-08 stsp if (s->search_entry) {
3701 1fc091f3 2023-09-05 thomas if (!using_mock_io) {
3702 1fc091f3 2023-09-05 thomas int errcode, ch;
3704 1fc091f3 2023-09-05 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3705 1fc091f3 2023-09-05 thomas if (errcode)
3706 1fc091f3 2023-09-05 thomas return got_error_set_errno(errcode,
3707 1fc091f3 2023-09-05 thomas "pthread_mutex_unlock");
3708 1fc091f3 2023-09-05 thomas ch = wgetch(view->window);
3709 1fc091f3 2023-09-05 thomas errcode = pthread_mutex_lock(&tog_mutex);
3710 1fc091f3 2023-09-05 thomas if (errcode)
3711 1fc091f3 2023-09-05 thomas return got_error_set_errno(errcode,
3712 1fc091f3 2023-09-05 thomas "pthread_mutex_lock");
3713 1fc091f3 2023-09-05 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3714 1fc091f3 2023-09-05 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
3715 1fc091f3 2023-09-05 thomas return NULL;
3718 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3719 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3721 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3722 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3723 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3725 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3726 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3727 1f4d5162 2022-06-23 thomas * might have changed.
3729 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3730 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3732 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3735 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3738 60493ae3 2019-06-20 stsp while (1) {
3739 13add988 2019-10-15 stsp int have_match = 0;
3741 60493ae3 2019-06-20 stsp if (entry == NULL) {
3742 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3743 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3744 f9967bca 2020-03-27 stsp view->search_next_done =
3745 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3746 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3747 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3748 f801134a 2019-06-25 stsp return NULL;
3751 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3752 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3753 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3755 49dff0bd 2023-08-12 thomas s->search_entry = s->selected_entry;
3756 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3757 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3760 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3761 13add988 2019-10-15 stsp &view->regex);
3764 13add988 2019-10-15 stsp if (have_match) {
3765 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3766 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3770 96e2b566 2019-07-08 stsp s->search_entry = entry;
3771 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3772 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3774 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3777 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3778 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3779 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3780 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3782 60493ae3 2019-06-20 stsp return err;
3785 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3786 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3788 60493ae3 2019-06-20 stsp return err;
3793 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3795 60493ae3 2019-06-20 stsp return NULL;
3798 60493ae3 2019-06-20 stsp static const struct got_error *
3799 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3800 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3801 92845f09 2023-07-26 thomas const char *in_repo_path, int log_branches,
3802 92845f09 2023-07-26 thomas struct got_worktree *worktree)
3804 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3805 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3806 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3807 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3808 1a76625f 2018-10-22 stsp int errcode;
3810 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3811 f135c941 2020-02-20 stsp free(s->in_repo_path);
3812 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3813 51b7e1c3 2023-04-14 thomas if (s->in_repo_path == NULL) {
3814 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("strdup");
3815 51b7e1c3 2023-04-14 thomas goto done;
3819 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3820 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3821 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3822 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3824 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3825 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3826 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3828 fb2756b9 2018-08-04 stsp s->repo = repo;
3829 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3830 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3831 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3832 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3833 9cd7cbd1 2020-12-07 stsp goto done;
3836 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3837 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3838 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3839 5036bf37 2018-09-24 stsp goto done;
3841 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3842 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3844 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3845 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3846 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3847 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3849 11b20872 2019-11-08 stsp goto done;
3850 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3851 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3852 11b20872 2019-11-08 stsp if (err) {
3853 11b20872 2019-11-08 stsp free_colors(&s->colors);
3854 11b20872 2019-11-08 stsp goto done;
3856 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3857 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3858 11b20872 2019-11-08 stsp if (err) {
3859 11b20872 2019-11-08 stsp free_colors(&s->colors);
3860 11b20872 2019-11-08 stsp goto done;
3864 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3865 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3866 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3867 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3868 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3869 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3871 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3872 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3873 1af5eddf 2022-06-23 thomas if (err)
3874 1af5eddf 2022-06-23 thomas goto done;
3876 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3877 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3878 7cd52833 2022-06-23 thomas if (err)
3879 7cd52833 2022-06-23 thomas goto done;
3880 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3881 b672a97a 2020-01-27 stsp !s->log_branches);
3883 1a76625f 2018-10-22 stsp goto done;
3884 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3885 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3887 c5b78334 2020-01-12 stsp goto done;
3889 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3890 1a76625f 2018-10-22 stsp if (errcode) {
3891 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3892 1a76625f 2018-10-22 stsp goto done;
3894 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3895 7c1452c1 2020-03-26 stsp if (errcode) {
3896 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3897 7c1452c1 2020-03-26 stsp goto done;
3900 92845f09 2023-07-26 thomas if (using_mock_io) {
3903 92845f09 2023-07-26 thomas rc = pthread_cond_init(&s->thread_args.log_loaded, NULL);
3905 92845f09 2023-07-26 thomas return got_error_set_errno(rc, "pthread_cond_init");
3908 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3909 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3910 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3911 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3912 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3913 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3914 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3915 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3916 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3917 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3918 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3919 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3920 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3921 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3922 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3923 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3924 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3925 92845f09 2023-07-26 thomas s->thread_args.worktree = worktree;
3926 92845f09 2023-07-26 thomas if (worktree)
3927 92845f09 2023-07-26 thomas s->thread_args.need_commit_marker = 1;
3929 51b7e1c3 2023-04-14 thomas if (err) {
3930 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
3931 51b7e1c3 2023-04-14 thomas close_log_view(view);
3932 51b7e1c3 2023-04-14 thomas view_close(view);
3934 ba4f502b 2018-08-04 stsp return err;
3937 e5a0f69f 2018-08-18 stsp static const struct got_error *
3938 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3940 f2f6d207 2020-11-24 stsp const struct got_error *err;
3941 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3943 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3944 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3945 2b380cc8 2018-10-24 stsp &s->thread_args);
3946 2b380cc8 2018-10-24 stsp if (errcode)
3947 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3948 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3949 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3951 f2f6d207 2020-11-24 stsp return err;
3955 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3958 b31f89ff 2022-07-01 thomas static void
3959 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3961 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3963 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3965 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3966 7e8004ba 2022-09-11 thomas view->count = 0;
3968 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3969 b31f89ff 2022-07-01 thomas || home)
3970 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3972 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3973 b31f89ff 2022-07-01 thomas --s->selected;
3975 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3977 b31f89ff 2022-07-01 thomas select_commit(s);
3981 b31f89ff 2022-07-01 thomas static const struct got_error *
3982 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3984 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3985 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3986 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3988 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3989 7e8004ba 2022-09-11 thomas return NULL;
3991 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3992 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3993 b31f89ff 2022-07-01 thomas return NULL;
3995 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3996 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3998 7ed048bd 2022-08-12 thomas if (!page) {
3999 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
4000 b31f89ff 2022-07-01 thomas ++s->selected;
4002 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
4003 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
4004 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
4007 7ed048bd 2022-08-12 thomas s->selected = 0;
4008 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
4009 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
4010 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
4011 7ed048bd 2022-08-12 thomas if (entry == NULL)
4013 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
4014 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
4016 7ed048bd 2022-08-12 thomas if (n > 0)
4017 7ed048bd 2022-08-12 thomas s->selected = n - 1;
4018 b31f89ff 2022-07-01 thomas } else {
4019 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
4020 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
4021 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
4022 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
4024 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
4026 b31f89ff 2022-07-01 thomas if (err)
4027 b31f89ff 2022-07-01 thomas return err;
4030 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
4031 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
4033 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
4034 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
4035 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
4036 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
4039 b31f89ff 2022-07-01 thomas select_commit(s);
4041 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
4042 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
4043 b31f89ff 2022-07-01 thomas view->count = 0;
4045 b31f89ff 2022-07-01 thomas return NULL;
4048 a5d43cac 2022-07-01 thomas static void
4049 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
4054 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
4055 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
4056 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
4057 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
4058 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
4060 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
4061 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
4062 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
4063 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
4064 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
4065 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
4067 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
4071 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
4072 e5a0f69f 2018-08-18 stsp static const struct got_error *
4073 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
4075 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
4077 a5d43cac 2022-07-01 thomas view->nlines = y;
4078 64486692 2022-07-07 thomas view->ncols = COLS;
4079 a5d43cac 2022-07-01 thomas err = view_resize(view);
4080 a5d43cac 2022-07-01 thomas if (err)
4081 a5d43cac 2022-07-01 thomas return err;
4083 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
4085 a5d43cac 2022-07-01 thomas return err;
4088 07dd3ed3 2022-08-06 thomas static const struct got_error *
4089 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
4091 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
4092 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
4093 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
4095 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
4096 25100026 2022-08-13 thomas return NULL;
4098 07dd3ed3 2022-08-06 thomas g = view->gline;
4099 07dd3ed3 2022-08-06 thomas view->gline = 0;
4101 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
4102 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
4103 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
4104 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
4105 07dd3ed3 2022-08-06 thomas select_commit(s);
4106 07dd3ed3 2022-08-06 thomas return NULL;
4109 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
4110 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
4111 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
4112 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
4113 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
4114 07dd3ed3 2022-08-06 thomas if (err)
4115 07dd3ed3 2022-08-06 thomas return err;
4116 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
4117 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
4119 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
4120 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
4122 07dd3ed3 2022-08-06 thomas select_commit(s);
4123 07dd3ed3 2022-08-06 thomas return NULL;
4127 c72de8ab 2023-02-03 thomas static void
4128 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
4131 c72de8ab 2023-02-03 thomas switch (ch) {
4132 c72de8ab 2023-02-03 thomas case KEY_LEFT:
4133 c72de8ab 2023-02-03 thomas case 'h':
4134 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
4135 c72de8ab 2023-02-03 thomas if (view->x <= 0)
4136 c72de8ab 2023-02-03 thomas view->count = 0;
4138 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
4139 c72de8ab 2023-02-03 thomas case 'l':
4140 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
4141 c72de8ab 2023-02-03 thomas view->x += 2;
4143 c72de8ab 2023-02-03 thomas view->count = 0;
4145 c72de8ab 2023-02-03 thomas case '0':
4146 c72de8ab 2023-02-03 thomas view->x = 0;
4148 c72de8ab 2023-02-03 thomas case '$':
4149 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
4150 c72de8ab 2023-02-03 thomas view->count = 0;
4152 c72de8ab 2023-02-03 thomas default:
4157 a5d43cac 2022-07-01 thomas static const struct got_error *
4158 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4160 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4161 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
4162 7ed048bd 2022-08-12 thomas int eos, nscroll;
4164 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
4165 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4166 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
4167 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
4168 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4169 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
4171 c0be8933 2022-08-12 thomas if (err)
4172 c0be8933 2022-08-12 thomas return err;
4175 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
4176 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
4177 068ab281 2022-07-01 thomas --eos; /* border */
4179 07dd3ed3 2022-08-06 thomas if (view->gline)
4180 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
4182 528dedf3 2021-08-30 stsp switch (ch) {
4183 7e8004ba 2022-09-11 thomas case '&':
4184 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
4187 1e37a5c2 2019-05-12 jcs s->quit = 1;
4189 05171be4 2022-06-23 thomas case '0':
4190 05171be4 2022-06-23 thomas case '$':
4191 05171be4 2022-06-23 thomas case KEY_RIGHT:
4192 05171be4 2022-06-23 thomas case 'l':
4193 05171be4 2022-06-23 thomas case KEY_LEFT:
4194 05171be4 2022-06-23 thomas case 'h':
4195 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
4198 1e37a5c2 2019-05-12 jcs case KEY_UP:
4201 f7140bf5 2021-10-17 thomas case CTRL('p'):
4202 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
4205 aa7a1117 2023-01-09 thomas case '=':
4206 912a3f79 2021-08-30 j case KEY_HOME:
4207 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
4208 07b0611c 2022-06-23 thomas view->count = 0;
4210 70f17a53 2022-06-13 thomas case CTRL('u'):
4211 23427b14 2022-06-23 thomas case 'u':
4212 70f17a53 2022-06-13 thomas nscroll /= 2;
4213 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4214 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4215 a4292ac5 2019-05-12 jcs case CTRL('b'):
4216 1c5e5faa 2022-06-23 thomas case 'b':
4217 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
4220 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4223 f7140bf5 2021-10-17 thomas case CTRL('n'):
4224 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
4226 f69c5a46 2022-07-19 thomas case '@':
4227 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
4228 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
4229 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
4232 aa7a1117 2023-01-09 thomas case '*':
4233 912a3f79 2021-08-30 j case KEY_END: {
4234 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4235 912a3f79 2021-08-30 j * traverse them all. */
4236 07b0611c 2022-06-23 thomas view->count = 0;
4237 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
4238 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
4239 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4240 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4241 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
4244 bccd1d5d 2022-06-13 thomas case CTRL('d'):
4245 23427b14 2022-06-23 thomas case 'd':
4246 70f17a53 2022-06-13 thomas nscroll /= 2;
4247 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4248 70f17a53 2022-06-13 thomas case KEY_NPAGE:
4249 1c5e5faa 2022-06-23 thomas case CTRL('f'):
4250 4c2d69cb 2022-06-23 thomas case 'f':
4251 b31f89ff 2022-07-01 thomas case ' ':
4252 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
4254 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4255 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4256 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4257 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
4258 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
4259 2b779855 2020-12-05 naddy select_commit(s);
4260 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
4261 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4262 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4263 7e8004ba 2022-09-11 thomas s->commits->ncommits;
4264 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4267 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4268 444d5325 2022-07-03 thomas case '\r':
4269 07b0611c 2022-06-23 thomas view->count = 0;
4270 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4272 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4274 1be4947a 2022-07-22 thomas case 'T':
4275 07b0611c 2022-06-23 thomas view->count = 0;
4276 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4278 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
4280 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4281 21355643 2020-12-06 stsp case CTRL('l'):
4283 07b0611c 2022-06-23 thomas view->count = 0;
4284 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4285 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4287 21355643 2020-12-06 stsp err = stop_log_thread(s);
4289 74cfe85e 2020-10-20 stsp return err;
4290 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4291 21355643 2020-12-06 stsp char *parent_path;
4292 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4294 21355643 2020-12-06 stsp return err;
4295 21355643 2020-12-06 stsp free(s->in_repo_path);
4296 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4297 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4298 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4299 21355643 2020-12-06 stsp struct got_object_id *start_id;
4300 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4301 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4302 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4303 466429a1 2022-11-17 thomas if (err) {
4304 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
4305 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
4306 466429a1 2022-11-17 thomas return err;
4307 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
4308 466429a1 2022-11-17 thomas free(s->head_ref_name);
4309 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
4310 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
4311 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4312 466429a1 2022-11-17 thomas &tog_refs, s->repo);
4313 466429a1 2022-11-17 thomas if (err)
4314 466429a1 2022-11-17 thomas return err;
4316 21355643 2020-12-06 stsp free(s->start_id);
4317 21355643 2020-12-06 stsp s->start_id = start_id;
4318 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4319 21355643 2020-12-06 stsp } else /* 'B' */
4320 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4322 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
4323 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4324 bf7e79b3 2022-06-23 thomas if (err)
4325 bf7e79b3 2022-06-23 thomas return err;
4327 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
4328 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
4329 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
4331 21355643 2020-12-06 stsp return err;
4332 51a10b52 2020-12-26 stsp tog_free_refs();
4333 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
4335 ca51c541 2020-12-07 stsp return err;
4336 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4337 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4339 d01904d4 2019-06-25 stsp return err;
4340 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4341 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4343 b672a97a 2020-01-27 stsp return err;
4344 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
4345 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
4346 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4347 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4348 21355643 2020-12-06 stsp s->selected_entry = NULL;
4349 21355643 2020-12-06 stsp s->selected = 0;
4350 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4351 21355643 2020-12-06 stsp s->quit = 0;
4352 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
4353 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
4354 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
4355 94ecf40d 2022-07-22 thomas view->offset = 0;
4357 1be4947a 2022-07-22 thomas case 'R':
4358 07b0611c 2022-06-23 thomas view->count = 0;
4359 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
4362 07b0611c 2022-06-23 thomas view->count = 0;
4366 80ddbec8 2018-04-29 stsp return err;
4369 4ed7e80c 2018-05-20 stsp static const struct got_error *
4370 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4372 c2db6724 2019-01-04 stsp const struct got_error *error;
4374 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4375 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4376 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4378 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4379 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4381 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4382 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4384 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4385 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4387 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4388 c2db6724 2019-01-04 stsp if (error != NULL)
4389 c2db6724 2019-01-04 stsp return error;
4391 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4392 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4394 c2db6724 2019-01-04 stsp return NULL;
4397 b85a3496 2023-04-14 thomas static const struct got_error *
4398 557d3365 2023-04-14 thomas init_mock_term(const char *test_script_path)
4400 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4401 5b3a801d 2023-04-22 thomas const char *screen_dump_path;
4404 b85a3496 2023-04-14 thomas if (test_script_path == NULL || *test_script_path == '\0')
4405 fa9bb690 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4407 557d3365 2023-04-14 thomas tog_io.f = fopen(test_script_path, "re");
4408 557d3365 2023-04-14 thomas if (tog_io.f == NULL) {
4409 b85a3496 2023-04-14 thomas err = got_error_from_errno_fmt("fopen: %s",
4410 b85a3496 2023-04-14 thomas test_script_path);
4411 b85a3496 2023-04-14 thomas goto done;
4414 b85a3496 2023-04-14 thomas /* test mode, we don't want any output */
4415 557d3365 2023-04-14 thomas tog_io.cout = fopen("/dev/null", "w+");
4416 557d3365 2023-04-14 thomas if (tog_io.cout == NULL) {
4417 f4086c71 2023-04-22 thomas err = got_error_from_errno2("fopen", "/dev/null");
4418 b85a3496 2023-04-14 thomas goto done;
4421 f4086c71 2023-04-22 thomas in = dup(fileno(tog_io.cout));
4422 f4086c71 2023-04-22 thomas if (in == -1) {
4423 f4086c71 2023-04-22 thomas err = got_error_from_errno("dup");
4424 f4086c71 2023-04-22 thomas goto done;
4426 f4086c71 2023-04-22 thomas tog_io.cin = fdopen(in, "r");
4427 557d3365 2023-04-14 thomas if (tog_io.cin == NULL) {
4428 f4086c71 2023-04-22 thomas err = got_error_from_errno("fdopen");
4429 f4086c71 2023-04-22 thomas close(in);
4430 b85a3496 2023-04-14 thomas goto done;
4433 5b3a801d 2023-04-22 thomas screen_dump_path = getenv("TOG_SCR_DUMP");
4434 5b3a801d 2023-04-22 thomas if (screen_dump_path == NULL || *screen_dump_path == '\0')
4435 5b3a801d 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4436 66b04f8f 2023-07-19 thomas tog_io.sdump = fopen(screen_dump_path, "we");
4437 5b3a801d 2023-04-22 thomas if (tog_io.sdump == NULL) {
4438 5b3a801d 2023-04-22 thomas err = got_error_from_errno2("fopen", screen_dump_path);
4439 5b3a801d 2023-04-22 thomas goto done;
4442 557d3365 2023-04-14 thomas if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4443 b85a3496 2023-04-14 thomas err = got_error_from_errno("fseeko");
4444 b85a3496 2023-04-14 thomas goto done;
4447 557d3365 2023-04-14 thomas if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4448 b85a3496 2023-04-14 thomas err = got_error_msg(GOT_ERR_IO,
4449 b85a3496 2023-04-14 thomas "newterm: failed to initialise curses");
4451 557d3365 2023-04-14 thomas using_mock_io = 1;
4454 b85a3496 2023-04-14 thomas if (err)
4455 557d3365 2023-04-14 thomas tog_io_close();
4456 b85a3496 2023-04-14 thomas return err;
4459 557d3365 2023-04-14 thomas static void
4460 557d3365 2023-04-14 thomas init_curses(void)
4462 557d3365 2023-04-14 thomas if (using_mock_io) /* In test mode we use a fake terminal */
4465 557d3365 2023-04-14 thomas initscr();
4468 557d3365 2023-04-14 thomas halfdelay(1); /* Fast refresh while initial view is loading. */
4471 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4472 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4473 a915003a 2019-02-05 stsp curs_set(0);
4474 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4475 6d17833f 2019-11-08 stsp start_color();
4476 6d17833f 2019-11-08 stsp use_default_colors();
4482 c2db6724 2019-01-04 stsp static const struct got_error *
4483 349dfd1e 2023-07-23 thomas set_tog_base_commit(struct got_repository *repo, struct got_worktree *worktree)
4485 349dfd1e 2023-07-23 thomas tog_base_commit.id = got_object_id_dup(
4486 349dfd1e 2023-07-23 thomas got_worktree_get_base_commit_id(worktree));
4487 349dfd1e 2023-07-23 thomas if (tog_base_commit.id == NULL)
4488 349dfd1e 2023-07-23 thomas return got_error_from_errno( "got_object_id_dup");
4490 92845f09 2023-07-26 thomas return NULL;
4493 349dfd1e 2023-07-23 thomas static const struct got_error *
4494 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4495 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4497 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4499 f135c941 2020-02-20 stsp if (argc == 0) {
4500 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4501 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4502 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4503 f135c941 2020-02-20 stsp return NULL;
4506 f135c941 2020-02-20 stsp if (worktree) {
4507 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4510 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4512 f135c941 2020-02-20 stsp return err;
4513 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4514 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4515 bfd61697 2020-11-14 stsp p) == -1) {
4516 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4517 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4521 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4523 f135c941 2020-02-20 stsp return err;
4526 f135c941 2020-02-20 stsp static const struct got_error *
4527 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4529 1d98034b 2023-04-14 thomas const struct got_error *error;
4530 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4531 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4532 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4533 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4534 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4535 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4536 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4537 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4538 04cc582a 2018-08-01 stsp struct tog_view *view;
4539 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4541 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4542 80ddbec8 2018-04-29 stsp switch (ch) {
4544 b672a97a 2020-01-27 stsp log_branches = 1;
4547 80ddbec8 2018-04-29 stsp start_commit = optarg;
4550 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4551 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4552 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4556 17020d27 2019-03-07 stsp usage_log();
4557 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4561 80ddbec8 2018-04-29 stsp argc -= optind;
4562 80ddbec8 2018-04-29 stsp argv += optind;
4564 f135c941 2020-02-20 stsp if (argc > 1)
4565 f135c941 2020-02-20 stsp usage_log();
4567 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4568 7cd52833 2022-06-23 thomas if (error != NULL)
4569 7cd52833 2022-06-23 thomas goto done;
4571 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4572 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4573 5a5defb7 2023-07-23 thomas if (cwd == NULL) {
4574 5a5defb7 2023-07-23 thomas error = got_error_from_errno("getcwd");
4575 5a5defb7 2023-07-23 thomas goto done;
4577 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
4578 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4579 c156c7a4 2020-12-18 stsp goto done;
4580 a1fbf39a 2019-08-11 stsp if (worktree)
4581 6962eb72 2020-02-20 stsp repo_path =
4582 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4584 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4585 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4586 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4587 c156c7a4 2020-12-18 stsp goto done;
4591 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4592 80ddbec8 2018-04-29 stsp if (error != NULL)
4593 ecb28ae0 2018-07-16 stsp goto done;
4595 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4596 f135c941 2020-02-20 stsp repo, worktree);
4597 f135c941 2020-02-20 stsp if (error)
4598 f135c941 2020-02-20 stsp goto done;
4600 557d3365 2023-04-14 thomas init_curses();
4602 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4603 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4604 c02c541e 2019-03-29 stsp if (error)
4605 c02c541e 2019-03-29 stsp goto done;
4607 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4608 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4609 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4610 87670572 2020-12-26 naddy if (error)
4611 87670572 2020-12-26 naddy goto done;
4614 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4615 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4616 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4617 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4618 d8f38dc4 2020-12-05 stsp if (error)
4619 d8f38dc4 2020-12-05 stsp goto done;
4620 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4622 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4623 66b04f8f 2023-07-19 thomas repo, worktree);
4624 66b04f8f 2023-07-19 thomas if (error != NULL)
4625 66b04f8f 2023-07-19 thomas goto done;
4626 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
4627 66b04f8f 2023-07-19 thomas start_commit = keyword_idstr;
4629 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4630 d8f38dc4 2020-12-05 stsp if (error == NULL)
4631 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4632 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4633 d8f38dc4 2020-12-05 stsp goto done;
4634 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4635 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4636 d8f38dc4 2020-12-05 stsp if (error)
4637 d8f38dc4 2020-12-05 stsp goto done;
4640 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4641 04cc582a 2018-08-01 stsp if (view == NULL) {
4642 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4643 04cc582a 2018-08-01 stsp goto done;
4646 2fc00ff4 2019-08-31 stsp if (worktree) {
4647 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
4648 349dfd1e 2023-07-23 thomas if (error != NULL)
4649 349dfd1e 2023-07-23 thomas goto done;
4652 92845f09 2023-07-26 thomas error = open_log_view(view, start_id, repo, head_ref_name,
4653 92845f09 2023-07-26 thomas in_repo_path, log_branches, worktree);
4654 92845f09 2023-07-26 thomas if (error)
4655 92845f09 2023-07-26 thomas goto done;
4657 92845f09 2023-07-26 thomas if (worktree) {
4658 92845f09 2023-07-26 thomas /* The work tree will be closed by the log thread. */
4659 2fc00ff4 2019-08-31 stsp worktree = NULL;
4662 557d3365 2023-04-14 thomas error = view_loop(view);
4665 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
4666 66b04f8f 2023-07-19 thomas free(keyword_idstr);
4667 f135c941 2020-02-20 stsp free(in_repo_path);
4668 ecb28ae0 2018-07-16 stsp free(repo_path);
4669 ecb28ae0 2018-07-16 stsp free(cwd);
4670 899d86c2 2018-05-10 stsp free(start_id);
4671 d8f38dc4 2020-12-05 stsp free(label);
4673 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4674 1d0f4054 2021-06-17 stsp if (repo) {
4675 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4676 1d0f4054 2021-06-17 stsp if (error == NULL)
4677 1d0f4054 2021-06-17 stsp error = close_err;
4679 ec142235 2019-03-07 stsp if (worktree)
4680 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4681 7cd52833 2022-06-23 thomas if (pack_fds) {
4682 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4683 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4684 7cd52833 2022-06-23 thomas if (error == NULL)
4685 7cd52833 2022-06-23 thomas error = pack_err;
4687 51a10b52 2020-12-26 stsp tog_free_refs();
4688 80ddbec8 2018-04-29 stsp return error;
4691 4ed7e80c 2018-05-20 stsp __dead static void
4692 9f7d7167 2018-04-29 stsp usage_diff(void)
4695 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4696 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4700 6d17833f 2019-11-08 stsp static int
4701 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4702 41605754 2020-11-12 stsp regmatch_t *regmatch)
4704 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4707 ef20f542 2022-06-26 thomas static struct tog_color *
4708 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4710 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4712 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4713 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4714 6d17833f 2019-11-08 stsp return tc;
4717 6d17833f 2019-11-08 stsp return NULL;
4720 4ed7e80c 2018-05-20 stsp static const struct got_error *
4721 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4722 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4724 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4725 666f7b10 2022-06-23 thomas char *exstr = NULL;
4726 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4727 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4728 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4729 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4731 41605754 2020-11-12 stsp *wtotal = 0;
4733 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4734 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4736 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4737 666f7b10 2022-06-23 thomas if (err)
4738 666f7b10 2022-06-23 thomas return err;
4740 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4741 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4742 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4743 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4744 666f7b10 2022-06-23 thomas goto done;
4746 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4747 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4748 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4749 cbea3800 2022-06-23 thomas goto done;
4751 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4752 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4753 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4754 cbea3800 2022-06-23 thomas goto done;
4757 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4758 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4759 cbea3800 2022-06-23 thomas col_tab_align, 1);
4760 cbea3800 2022-06-23 thomas if (err)
4761 cbea3800 2022-06-23 thomas goto done;
4762 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4763 05171be4 2022-06-23 thomas if (n) {
4764 cbea3800 2022-06-23 thomas free(wline);
4765 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4766 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4767 cbea3800 2022-06-23 thomas if (err)
4768 cbea3800 2022-06-23 thomas goto done;
4769 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4770 cbea3800 2022-06-23 thomas wlimit -= width;
4771 cbea3800 2022-06-23 thomas *wtotal += width;
4774 41605754 2020-11-12 stsp if (wlimit > 0) {
4775 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4776 cbea3800 2022-06-23 thomas size_t wlen;
4778 cbea3800 2022-06-23 thomas free(wline);
4779 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4780 cbea3800 2022-06-23 thomas col_tab_align, 1);
4781 cbea3800 2022-06-23 thomas if (err)
4782 cbea3800 2022-06-23 thomas goto done;
4783 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4784 cbea3800 2022-06-23 thomas while (i < wlen) {
4785 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4786 cbea3800 2022-06-23 thomas if (width == -1) {
4787 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4788 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4789 cbea3800 2022-06-23 thomas goto done;
4791 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4793 1c5e5faa 2022-06-23 thomas w += width;
4796 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4797 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4798 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4799 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4800 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4801 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4802 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4806 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4807 cbea3800 2022-06-23 thomas free(wline);
4808 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4809 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4810 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4811 cbea3800 2022-06-23 thomas col_tab_align, 1);
4812 cbea3800 2022-06-23 thomas if (err)
4813 cbea3800 2022-06-23 thomas goto done;
4814 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4815 cbea3800 2022-06-23 thomas } else {
4816 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4817 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4818 cbea3800 2022-06-23 thomas if (err)
4819 cbea3800 2022-06-23 thomas goto done;
4820 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4822 cbea3800 2022-06-23 thomas *wtotal += width2;
4825 05171be4 2022-06-23 thomas free(wline);
4826 666f7b10 2022-06-23 thomas free(exstr);
4827 cbea3800 2022-06-23 thomas free(seg0);
4828 cbea3800 2022-06-23 thomas free(seg1);
4829 cbea3800 2022-06-23 thomas free(seg2);
4830 cbea3800 2022-06-23 thomas return err;
4833 07dd3ed3 2022-08-06 thomas static int
4834 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4836 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4837 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4839 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4840 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4842 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4843 fc2737d5 2022-09-15 thomas selected = first;
4844 fc2737d5 2022-09-15 thomas eof = &s->eof;
4845 fc2737d5 2022-09-15 thomas f = s->f;
4846 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4847 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4849 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4850 07dd3ed3 2022-08-06 thomas selected = first;
4851 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4852 07dd3ed3 2022-08-06 thomas f = s->f;
4853 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4854 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4856 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4857 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4858 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4859 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4861 07dd3ed3 2022-08-06 thomas return 0;
4863 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4864 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4865 07dd3ed3 2022-08-06 thomas return 0;
4866 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4867 07dd3ed3 2022-08-06 thomas rewind(f);
4868 07dd3ed3 2022-08-06 thomas *eof = 0;
4869 07dd3ed3 2022-08-06 thomas *first = 1;
4870 07dd3ed3 2022-08-06 thomas *lineno = 0;
4871 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4872 07dd3ed3 2022-08-06 thomas return 0;
4875 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4876 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4877 07dd3ed3 2022-08-06 thomas view->gline = 0;
4879 07dd3ed3 2022-08-06 thomas return 1;
4882 41605754 2020-11-12 stsp static const struct got_error *
4883 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4885 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4886 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4887 61e69b96 2018-05-20 stsp const struct got_error *err;
4888 82c78e96 2022-08-06 thomas int nprinted = 0;
4889 b304db33 2018-05-20 stsp char *line;
4890 826082fe 2020-12-10 stsp size_t linesize = 0;
4891 826082fe 2020-12-10 stsp ssize_t linelen;
4892 61e69b96 2018-05-20 stsp wchar_t *wline;
4893 e0b650dd 2018-05-20 stsp int width;
4894 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4895 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4896 fe621944 2020-11-10 stsp off_t line_offset;
4898 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4899 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4900 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4901 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4903 f7d12f7e 2018-08-01 stsp werase(view->window);
4905 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4906 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4908 a3404814 2018-09-02 stsp if (header) {
4909 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4910 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4911 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4913 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4914 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4915 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4917 135a2da0 2020-11-11 stsp free(line);
4919 a3404814 2018-09-02 stsp return err;
4921 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4922 a3404814 2018-09-02 stsp wstandout(view->window);
4923 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4924 e54cc94a 2020-11-11 stsp free(wline);
4925 e54cc94a 2020-11-11 stsp wline = NULL;
4926 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4927 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4928 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4929 a3404814 2018-09-02 stsp wstandend(view->window);
4931 a3404814 2018-09-02 stsp if (max_lines <= 1)
4932 a3404814 2018-09-02 stsp return NULL;
4933 a3404814 2018-09-02 stsp max_lines--;
4936 89f1a395 2020-12-01 naddy s->eof = 0;
4937 05171be4 2022-06-23 thomas view->maxx = 0;
4938 826082fe 2020-12-10 stsp line = NULL;
4939 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4940 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4941 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4943 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4944 826082fe 2020-12-10 stsp if (linelen == -1) {
4945 826082fe 2020-12-10 stsp if (feof(s->f)) {
4946 826082fe 2020-12-10 stsp s->eof = 1;
4949 826082fe 2020-12-10 stsp free(line);
4950 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4953 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4954 07dd3ed3 2022-08-06 thomas continue;
4955 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4956 07dd3ed3 2022-08-06 thomas continue;
4957 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4958 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4960 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4961 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4962 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4963 cbea3800 2022-06-23 thomas if (err) {
4964 cbea3800 2022-06-23 thomas free(line);
4965 cbea3800 2022-06-23 thomas return err;
4967 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4968 cbea3800 2022-06-23 thomas free(wline);
4969 cbea3800 2022-06-23 thomas wline = NULL;
4971 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4972 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4973 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4974 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4975 07dd3ed3 2022-08-06 thomas if (attr)
4976 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4977 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4978 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4979 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4980 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4981 41605754 2020-11-12 stsp if (err) {
4982 41605754 2020-11-12 stsp free(line);
4983 41605754 2020-11-12 stsp return err;
4986 f1c0ec19 2022-06-23 thomas int skip;
4987 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4988 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4989 f1c0ec19 2022-06-23 thomas if (err) {
4990 f1c0ec19 2022-06-23 thomas free(line);
4991 f1c0ec19 2022-06-23 thomas return err;
4993 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4994 f1c0ec19 2022-06-23 thomas free(wline);
4995 41605754 2020-11-12 stsp wline = NULL;
4997 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4998 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4999 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
5000 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
5001 07dd3ed3 2022-08-06 thomas } else {
5002 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
5003 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
5005 07dd3ed3 2022-08-06 thomas if (attr)
5006 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
5007 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
5008 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
5010 826082fe 2020-12-10 stsp free(line);
5011 fe621944 2020-11-10 stsp if (nprinted >= 1)
5012 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
5013 89f1a395 2020-12-01 naddy (nprinted - 1);
5015 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
5017 a5d43cac 2022-07-01 thomas view_border(view);
5019 89f1a395 2020-12-01 naddy if (s->eof) {
5020 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
5021 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
5022 c3e9aa98 2019-05-13 jcs nprinted++;
5025 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
5026 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
5028 c3e9aa98 2019-05-13 jcs return err;
5031 c3e9aa98 2019-05-13 jcs wstandout(view->window);
5032 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
5033 e54cc94a 2020-11-11 stsp free(wline);
5034 e54cc94a 2020-11-11 stsp wline = NULL;
5035 c3e9aa98 2019-05-13 jcs wstandend(view->window);
5038 26ed57b2 2018-05-19 stsp return NULL;
5041 abd2672a 2018-12-23 stsp static char *
5042 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
5044 09867e48 2019-08-13 stsp struct tm mytm, *tm;
5045 09867e48 2019-08-13 stsp char *p, *s;
5047 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
5048 09867e48 2019-08-13 stsp if (tm == NULL)
5049 09867e48 2019-08-13 stsp return NULL;
5050 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
5051 09867e48 2019-08-13 stsp if (s == NULL)
5052 09867e48 2019-08-13 stsp return NULL;
5053 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
5055 abd2672a 2018-12-23 stsp *p = '\0';
5059 4ed7e80c 2018-05-20 stsp static const struct got_error *
5060 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
5061 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
5063 82c78e96 2022-08-06 thomas struct got_diff_line *p;
5065 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
5066 fe621944 2020-11-10 stsp if (p == NULL)
5067 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
5068 82c78e96 2022-08-06 thomas *lines = p;
5069 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
5070 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
5071 fe621944 2020-11-10 stsp (*nlines)++;
5073 fe621944 2020-11-10 stsp return NULL;
5076 fe621944 2020-11-10 stsp static const struct got_error *
5077 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
5078 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
5080 be97ab03 2023-01-19 thomas struct got_diff_line *p;
5081 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
5082 be97ab03 2023-01-19 thomas size_t i, r;
5084 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
5085 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
5087 be97ab03 2023-01-19 thomas for (;;) {
5088 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
5089 be97ab03 2023-01-19 thomas if (r == 0) {
5090 be97ab03 2023-01-19 thomas if (ferror(src))
5091 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
5092 be97ab03 2023-01-19 thomas if (feof(src))
5095 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
5096 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
5099 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
5100 9382c10a 2023-02-23 thomas return NULL;
5103 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
5104 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
5105 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
5107 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
5108 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
5109 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
5111 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
5112 9382c10a 2023-02-23 thomas --s_nlines;
5113 9382c10a 2023-02-23 thomas ++s_lines;
5117 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
5118 be97ab03 2023-01-19 thomas if (p == NULL) {
5119 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
5120 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
5123 be97ab03 2023-01-19 thomas *d_lines = p;
5125 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
5126 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
5128 be97ab03 2023-01-19 thomas return NULL;
5131 be97ab03 2023-01-19 thomas static const struct got_error *
5132 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
5133 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
5134 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
5135 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
5137 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
5138 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
5139 15a94983 2018-12-23 stsp struct got_commit_object *commit;
5140 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5141 45d799e2 2018-12-23 stsp time_t committer_time;
5142 45d799e2 2018-12-23 stsp const char *author, *committer;
5143 8b473291 2019-02-21 stsp char *refs_str = NULL;
5144 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
5145 fe621944 2020-11-10 stsp off_t outoff = 0;
5148 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, commit_id, repo);
5149 00ddec2f 2023-05-17 thomas if (err)
5150 00ddec2f 2023-05-17 thomas return err;
5152 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5154 abd2672a 2018-12-23 stsp return err;
5156 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
5157 15a94983 2018-12-23 stsp if (err) {
5158 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
5159 15a94983 2018-12-23 stsp goto done;
5162 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5164 fe621944 2020-11-10 stsp goto done;
5166 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5167 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
5168 fe621944 2020-11-10 stsp if (n < 0) {
5169 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5170 abd2672a 2018-12-23 stsp goto done;
5172 fe621944 2020-11-10 stsp outoff += n;
5173 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5175 fe621944 2020-11-10 stsp goto done;
5177 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
5178 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
5179 fe621944 2020-11-10 stsp if (n < 0) {
5180 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5181 abd2672a 2018-12-23 stsp goto done;
5183 fe621944 2020-11-10 stsp outoff += n;
5184 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5186 fe621944 2020-11-10 stsp goto done;
5188 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
5189 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
5190 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
5191 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
5192 fe621944 2020-11-10 stsp if (n < 0) {
5193 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5194 fe621944 2020-11-10 stsp goto done;
5196 fe621944 2020-11-10 stsp outoff += n;
5197 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5198 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
5200 fe621944 2020-11-10 stsp goto done;
5202 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
5203 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
5204 48830929 2023-01-07 thomas if (datestr) {
5205 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
5206 fe621944 2020-11-10 stsp if (n < 0) {
5207 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5208 fe621944 2020-11-10 stsp goto done;
5210 fe621944 2020-11-10 stsp outoff += n;
5211 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5212 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
5214 fe621944 2020-11-10 stsp goto done;
5216 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
5217 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
5218 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
5219 ac4dc263 2021-09-24 thomas int pn = 1;
5220 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
5221 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
5222 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
5223 ac4dc263 2021-09-24 thomas if (err)
5224 ac4dc263 2021-09-24 thomas goto done;
5225 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5226 ac4dc263 2021-09-24 thomas if (n < 0) {
5227 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
5228 ac4dc263 2021-09-24 thomas goto done;
5230 ac4dc263 2021-09-24 thomas outoff += n;
5231 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5232 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
5233 ac4dc263 2021-09-24 thomas if (err)
5234 ac4dc263 2021-09-24 thomas goto done;
5235 ac4dc263 2021-09-24 thomas free(id_str);
5236 ac4dc263 2021-09-24 thomas id_str = NULL;
5240 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
5242 5943eee2 2019-08-13 stsp goto done;
5243 fe621944 2020-11-10 stsp s = logmsg;
5244 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
5245 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
5246 fe621944 2020-11-10 stsp if (n < 0) {
5247 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5248 fe621944 2020-11-10 stsp goto done;
5250 fe621944 2020-11-10 stsp outoff += n;
5251 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5252 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
5254 fe621944 2020-11-10 stsp goto done;
5257 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
5258 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5259 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
5261 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5262 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5263 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
5264 fe621944 2020-11-10 stsp if (n < 0) {
5265 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5266 fe621944 2020-11-10 stsp goto done;
5268 fe621944 2020-11-10 stsp outoff += n;
5269 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5270 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
5272 fe621944 2020-11-10 stsp goto done;
5275 0208f208 2020-05-05 stsp fputc('\n', outfile);
5277 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5278 772fcad5 2023-01-07 thomas if (err)
5279 772fcad5 2023-01-07 thomas goto done;
5281 772fcad5 2023-01-07 thomas n = fprintf(outfile,
5282 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5283 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5284 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5285 772fcad5 2023-01-07 thomas if (n < 0) {
5286 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
5287 772fcad5 2023-01-07 thomas goto done;
5289 772fcad5 2023-01-07 thomas outoff += n;
5290 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5291 772fcad5 2023-01-07 thomas if (err)
5292 772fcad5 2023-01-07 thomas goto done;
5294 772fcad5 2023-01-07 thomas fputc('\n', outfile);
5295 772fcad5 2023-01-07 thomas outoff++;
5296 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5298 abd2672a 2018-12-23 stsp free(id_str);
5299 5943eee2 2019-08-13 stsp free(logmsg);
5300 8b473291 2019-02-21 stsp free(refs_str);
5301 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5302 7510f233 2020-08-09 stsp if (err) {
5303 82c78e96 2022-08-06 thomas free(*lines);
5304 82c78e96 2022-08-06 thomas *lines = NULL;
5305 ae6a6978 2020-08-09 stsp *nlines = 0;
5307 fe621944 2020-11-10 stsp return err;
5310 abd2672a 2018-12-23 stsp static const struct got_error *
5311 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5313 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5314 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
5315 15a94983 2018-12-23 stsp int obj_type;
5316 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
5317 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
5319 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
5321 82c78e96 2022-08-06 thomas free(s->lines);
5322 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
5323 82c78e96 2022-08-06 thomas if (s->lines == NULL)
5324 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5325 fe621944 2020-11-10 stsp s->nlines = 0;
5327 511a516b 2018-05-19 stsp f = got_opentemp();
5328 48ae06ee 2018-10-18 stsp if (f == NULL) {
5329 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5330 48ae06ee 2018-10-18 stsp goto done;
5332 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
5333 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
5334 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
5335 be97ab03 2023-01-19 thomas goto done;
5337 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5338 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5339 fb43ecf1 2019-02-11 stsp goto done;
5343 15a94983 2018-12-23 stsp if (s->id1)
5344 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5346 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5348 15a94983 2018-12-23 stsp goto done;
5350 15a94983 2018-12-23 stsp switch (obj_type) {
5351 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5352 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5353 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5354 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
5355 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5358 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5359 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5360 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5361 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5362 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
5364 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5365 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5366 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5367 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5368 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5369 be97ab03 2023-01-19 thomas size_t nlines = 0;
5370 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
5371 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
5372 be97ab03 2023-01-19 thomas &changed_paths,
5373 be97ab03 2023-01-19 thomas s->ignore_whitespace,
5374 be97ab03 2023-01-19 thomas s->force_text_diff,
5375 be97ab03 2023-01-19 thomas tog_diff_algo
5378 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
5379 be97ab03 2023-01-19 thomas if (lines == NULL) {
5380 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
5381 be97ab03 2023-01-19 thomas goto done;
5384 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
5385 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
5386 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5387 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5388 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5389 be97ab03 2023-01-19 thomas if (err)
5392 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5394 3ffacbe1 2020-02-02 tracey goto done;
5395 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5396 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5397 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5398 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
5399 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
5400 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5401 f44b1f58 2020-02-02 tracey if (err)
5402 f44b1f58 2020-02-02 tracey goto done;
5403 f44b1f58 2020-02-02 tracey } else {
5404 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5405 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5406 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5407 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
5408 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
5409 772fcad5 2023-01-07 thomas s->ignore_whitespace,
5410 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5411 f44b1f58 2020-02-02 tracey if (err)
5412 f44b1f58 2020-02-02 tracey goto done;
5417 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5419 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5420 be97ab03 2023-01-19 thomas lines, nlines);
5424 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5428 be97ab03 2023-01-19 thomas free(lines);
5429 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5430 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5431 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5432 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5433 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
5434 48ae06ee 2018-10-18 stsp return err;
5437 f5215bb9 2019-02-22 stsp static void
5438 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5440 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5441 f5215bb9 2019-02-22 stsp update_panels();
5442 f5215bb9 2019-02-22 stsp doupdate();
5445 f44b1f58 2020-02-02 tracey static const struct got_error *
5446 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5448 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5450 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5451 f44b1f58 2020-02-02 tracey return NULL;
5454 2a7d3cd7 2022-09-17 thomas static void
5455 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5456 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
5458 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
5460 2a7d3cd7 2022-09-17 thomas *f = s->f;
5461 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
5462 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
5463 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
5464 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
5465 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
5466 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
5469 fc2737d5 2022-09-15 thomas static const struct got_error *
5470 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
5472 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5473 fc2737d5 2022-09-15 thomas FILE *f;
5474 f44b1f58 2020-02-02 tracey int lineno;
5475 78eecffc 2022-06-23 thomas char *line = NULL;
5476 826082fe 2020-12-10 stsp size_t linesize = 0;
5477 826082fe 2020-12-10 stsp ssize_t linelen;
5478 fc2737d5 2022-09-15 thomas off_t *line_offsets;
5479 fc2737d5 2022-09-15 thomas size_t nlines = 0;
5480 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
5482 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
5483 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
5484 2a7d3cd7 2022-09-17 thomas "view search not supported");
5485 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5486 fc2737d5 2022-09-15 thomas &match, &selected);
5488 f44b1f58 2020-02-02 tracey if (!view->searching) {
5489 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5490 f44b1f58 2020-02-02 tracey return NULL;
5493 fc2737d5 2022-09-15 thomas if (*match) {
5494 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5495 7303e8c8 2023-04-04 thomas lineno = *first + 1;
5497 7303e8c8 2023-04-04 thomas lineno = *first - 1;
5499 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
5501 f44b1f58 2020-02-02 tracey while (1) {
5502 f44b1f58 2020-02-02 tracey off_t offset;
5504 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
5505 fc2737d5 2022-09-15 thomas if (*match == 0) {
5506 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5510 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5511 f44b1f58 2020-02-02 tracey lineno = 1;
5513 fc2737d5 2022-09-15 thomas lineno = nlines;
5516 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5517 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5518 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5519 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5520 f44b1f58 2020-02-02 tracey free(line);
5521 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5523 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5524 78eecffc 2022-06-23 thomas if (linelen != -1) {
5525 78eecffc 2022-06-23 thomas char *exstr;
5526 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5527 78eecffc 2022-06-23 thomas if (err)
5529 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5530 78eecffc 2022-06-23 thomas &view->regmatch)) {
5531 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5532 fc2737d5 2022-09-15 thomas *match = lineno;
5533 78eecffc 2022-06-23 thomas free(exstr);
5536 78eecffc 2022-06-23 thomas free(exstr);
5538 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5539 f44b1f58 2020-02-02 tracey lineno++;
5541 f44b1f58 2020-02-02 tracey lineno--;
5543 826082fe 2020-12-10 stsp free(line);
5545 fc2737d5 2022-09-15 thomas if (*match) {
5546 fc2737d5 2022-09-15 thomas *first = *match;
5547 fc2737d5 2022-09-15 thomas *selected = 1;
5550 05171be4 2022-06-23 thomas return err;
5553 48ae06ee 2018-10-18 stsp static const struct got_error *
5554 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5556 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5557 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5559 a0f32f33 2022-06-13 thomas free(s->id1);
5560 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5561 a0f32f33 2022-06-13 thomas free(s->id2);
5562 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5563 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5564 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5565 a0f32f33 2022-06-13 thomas s->f = NULL;
5566 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5567 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5568 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5569 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5570 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5571 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5572 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5573 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5574 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5575 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5576 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5577 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5578 82c78e96 2022-08-06 thomas free(s->lines);
5579 82c78e96 2022-08-06 thomas s->lines = NULL;
5580 a0f32f33 2022-06-13 thomas s->nlines = 0;
5581 a0f32f33 2022-06-13 thomas return err;
5584 a0f32f33 2022-06-13 thomas static const struct got_error *
5585 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5586 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5587 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5588 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5590 48ae06ee 2018-10-18 stsp const struct got_error *err;
5591 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5593 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5594 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5595 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5597 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5598 51b7e1c3 2023-04-14 thomas int type1, type2;
5600 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type1, repo, id1);
5601 51b7e1c3 2023-04-14 thomas if (err)
5602 51b7e1c3 2023-04-14 thomas goto done;
5603 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type2, repo, id2);
5604 51b7e1c3 2023-04-14 thomas if (err)
5605 51b7e1c3 2023-04-14 thomas goto done;
5607 51b7e1c3 2023-04-14 thomas if (type1 != type2) {
5608 51b7e1c3 2023-04-14 thomas err = got_error(GOT_ERR_OBJ_TYPE);
5609 51b7e1c3 2023-04-14 thomas goto done;
5612 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5613 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5614 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5615 f44b1f58 2020-02-02 tracey s->repo = repo;
5616 f44b1f58 2020-02-02 tracey s->id1 = id1;
5617 f44b1f58 2020-02-02 tracey s->id2 = id2;
5618 3dbaef42 2020-11-24 stsp s->label1 = label1;
5619 3dbaef42 2020-11-24 stsp s->label2 = label2;
5621 15a94983 2018-12-23 stsp if (id1) {
5622 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5623 51b7e1c3 2023-04-14 thomas if (s->id1 == NULL) {
5624 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
5625 51b7e1c3 2023-04-14 thomas goto done;
5628 5465d566 2020-02-01 tracey s->id1 = NULL;
5630 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5631 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5632 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5633 a0f32f33 2022-06-13 thomas goto done;
5636 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5637 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5638 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5639 9a1d7435 2022-06-23 thomas goto done;
5642 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5643 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5644 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5645 a0f32f33 2022-06-13 thomas goto done;
5648 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5649 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5650 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5651 19a6a6b5 2022-07-01 thomas goto done;
5654 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5655 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5656 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5657 19a6a6b5 2022-07-01 thomas goto done;
5660 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5661 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5662 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5663 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5664 5465d566 2020-02-01 tracey s->repo = repo;
5666 557d3365 2023-04-14 thomas if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5669 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5670 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5671 6568f0aa 2022-08-06 thomas if (rc != ERR)
5672 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5673 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5674 6568f0aa 2022-08-06 thomas if (rc != ERR)
5675 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5676 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5677 6568f0aa 2022-08-06 thomas if (rc != ERR)
5678 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5679 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5680 6568f0aa 2022-08-06 thomas if (rc != ERR)
5681 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5682 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5683 6568f0aa 2022-08-06 thomas if (rc != ERR)
5684 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5685 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5686 6568f0aa 2022-08-06 thomas if (rc != ERR)
5687 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5688 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5689 6568f0aa 2022-08-06 thomas if (rc != ERR)
5690 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5691 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5692 6568f0aa 2022-08-06 thomas if (rc != ERR)
5693 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5694 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5695 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5696 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5697 a0f32f33 2022-06-13 thomas goto done;
5701 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5702 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5703 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5704 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5706 5465d566 2020-02-01 tracey err = create_diff(s);
5708 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5709 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5710 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5711 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5712 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5713 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5714 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5716 51b7e1c3 2023-04-14 thomas if (err) {
5717 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
5718 51b7e1c3 2023-04-14 thomas close_diff_view(view);
5719 51b7e1c3 2023-04-14 thomas view_close(view);
5721 e5a0f69f 2018-08-18 stsp return err;
5724 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5725 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5727 a3404814 2018-09-02 stsp const struct got_error *err;
5728 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5729 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5730 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5732 a3404814 2018-09-02 stsp if (s->id1) {
5733 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5735 a3404814 2018-09-02 stsp return err;
5736 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5738 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5740 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5742 a3404814 2018-09-02 stsp return err;
5743 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5745 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5746 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5747 a3404814 2018-09-02 stsp free(id_str1);
5748 a3404814 2018-09-02 stsp free(id_str2);
5749 a3404814 2018-09-02 stsp return err;
5751 a3404814 2018-09-02 stsp free(id_str1);
5752 a3404814 2018-09-02 stsp free(id_str2);
5754 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5755 267bb3b8 2021-08-01 stsp free(header);
5756 267bb3b8 2021-08-01 stsp return err;
5759 15a087fe 2019-02-21 stsp static const struct got_error *
5760 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5761 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5763 d7a04538 2019-02-21 stsp const struct got_error *err;
5764 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5765 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5766 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5768 15a087fe 2019-02-21 stsp free(s->id2);
5769 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5770 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5771 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5773 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5775 d7a04538 2019-02-21 stsp return err;
5776 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5777 15a087fe 2019-02-21 stsp free(s->id1);
5778 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5779 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5780 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5781 15a087fe 2019-02-21 stsp return NULL;
5784 0cf4efb1 2018-09-29 stsp static const struct got_error *
5785 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5787 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5789 adf4c9e0 2022-07-03 thomas view->count = 0;
5790 adf4c9e0 2022-07-03 thomas wclear(view->window);
5791 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5792 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5793 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5794 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5795 adf4c9e0 2022-07-03 thomas return create_diff(s);
5798 82c78e96 2022-08-06 thomas static void
5799 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5801 82c78e96 2022-08-06 thomas int start, i;
5803 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5805 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5806 82c78e96 2022-08-06 thomas if (i == 0)
5807 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5808 82c78e96 2022-08-06 thomas if (--i == start)
5809 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5812 82c78e96 2022-08-06 thomas s->selected_line = 1;
5813 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5816 82c78e96 2022-08-06 thomas static void
5817 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5819 82c78e96 2022-08-06 thomas int start, i;
5821 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5823 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5824 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5826 82c78e96 2022-08-06 thomas if (++i == start)
5827 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5830 82c78e96 2022-08-06 thomas s->selected_line = 1;
5831 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5834 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5835 4fc71f3b 2022-07-12 thomas int, int, int);
5836 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5837 4fc71f3b 2022-07-12 thomas int, int);
5839 adf4c9e0 2022-07-03 thomas static const struct got_error *
5840 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5842 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5843 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5844 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5845 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5846 826082fe 2020-12-10 stsp char *line = NULL;
5847 826082fe 2020-12-10 stsp size_t linesize = 0;
5848 826082fe 2020-12-10 stsp ssize_t linelen;
5849 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5851 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5853 e5a0f69f 2018-08-18 stsp switch (ch) {
5854 05171be4 2022-06-23 thomas case '0':
5855 05171be4 2022-06-23 thomas case '$':
5856 05171be4 2022-06-23 thomas case KEY_RIGHT:
5857 05171be4 2022-06-23 thomas case 'l':
5858 05171be4 2022-06-23 thomas case KEY_LEFT:
5859 05171be4 2022-06-23 thomas case 'h':
5860 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5864 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5865 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5866 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5867 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5868 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5870 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5871 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5872 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5873 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5874 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5876 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5879 912a3f79 2021-08-30 j case KEY_HOME:
5880 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5881 07b0611c 2022-06-23 thomas view->count = 0;
5884 912a3f79 2021-08-30 j case KEY_END:
5885 07b0611c 2022-06-23 thomas view->count = 0;
5889 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5893 1e37a5c2 2019-05-12 jcs case KEY_UP:
5894 f7140bf5 2021-10-17 thomas case CTRL('p'):
5895 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5896 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5898 07b0611c 2022-06-23 thomas view->count = 0;
5900 70f17a53 2022-06-13 thomas case CTRL('u'):
5901 23427b14 2022-06-23 thomas case 'u':
5902 70f17a53 2022-06-13 thomas nscroll /= 2;
5903 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5904 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5905 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5906 1c5e5faa 2022-06-23 thomas case 'b':
5907 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5908 07b0611c 2022-06-23 thomas view->count = 0;
5912 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5913 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5916 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5917 f7140bf5 2021-10-17 thomas case CTRL('n'):
5918 1e37a5c2 2019-05-12 jcs if (!s->eof)
5919 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5921 07b0611c 2022-06-23 thomas view->count = 0;
5923 70f17a53 2022-06-13 thomas case CTRL('d'):
5924 23427b14 2022-06-23 thomas case 'd':
5925 70f17a53 2022-06-13 thomas nscroll /= 2;
5926 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5927 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5928 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5929 1c5e5faa 2022-06-23 thomas case 'f':
5931 07b0611c 2022-06-23 thomas if (s->eof) {
5932 07b0611c 2022-06-23 thomas view->count = 0;
5936 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5937 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5938 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5939 826082fe 2020-12-10 stsp if (linelen == -1) {
5940 826082fe 2020-12-10 stsp if (feof(s->f)) {
5941 826082fe 2020-12-10 stsp s->eof = 1;
5943 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5947 826082fe 2020-12-10 stsp free(line);
5949 82c78e96 2022-08-06 thomas case '(':
5950 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5952 82c78e96 2022-08-06 thomas case ')':
5953 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5955 82c78e96 2022-08-06 thomas case '{':
5956 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5958 82c78e96 2022-08-06 thomas case '}':
5959 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5962 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5963 1e37a5c2 2019-05-12 jcs s->diff_context--;
5964 aa61903a 2021-12-31 thomas s->matched_line = 0;
5965 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5966 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5967 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5968 27829c9e 2020-11-21 stsp s->nlines) {
5969 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5970 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5973 07b0611c 2022-06-23 thomas view->count = 0;
5976 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5977 1e37a5c2 2019-05-12 jcs s->diff_context++;
5978 aa61903a 2021-12-31 thomas s->matched_line = 0;
5979 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5980 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5982 07b0611c 2022-06-23 thomas view->count = 0;
5986 777aae21 2022-07-20 thomas case 'K':
5988 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5989 4fc71f3b 2022-07-12 thomas case '>':
5990 4fc71f3b 2022-07-12 thomas case '.':
5991 777aae21 2022-07-20 thomas case 'J':
5992 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5993 07b0611c 2022-06-23 thomas view->count = 0;
5996 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5998 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5999 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
6000 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
6002 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
6003 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
6004 4fc71f3b 2022-07-12 thomas if (err)
6006 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
6008 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
6011 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
6012 4fc71f3b 2022-07-12 thomas if (err)
6014 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
6015 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
6016 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
6018 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
6019 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
6020 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
6022 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
6023 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
6024 4fc71f3b 2022-07-12 thomas if (err)
6026 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
6028 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
6030 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
6031 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
6032 4fc71f3b 2022-07-12 thomas bs->selected_line);
6033 4fc71f3b 2022-07-12 thomas if (id == NULL)
6036 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
6039 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
6040 4fc71f3b 2022-07-12 thomas if (err)
6043 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6044 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
6045 aa61903a 2021-12-31 thomas s->matched_line = 0;
6046 05171be4 2022-06-23 thomas view->x = 0;
6048 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
6049 1e37a5c2 2019-05-12 jcs err = create_diff(s);
6052 07b0611c 2022-06-23 thomas view->count = 0;
6056 bcbd79e2 2018-08-19 stsp return err;
6059 4ed7e80c 2018-05-20 stsp static const struct got_error *
6060 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
6062 1d98034b 2023-04-14 thomas const struct got_error *error;
6063 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
6064 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
6065 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
6066 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
6067 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
6068 66b04f8f 2023-07-19 thomas char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
6069 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
6070 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
6071 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
6072 3dbaef42 2020-11-24 stsp const char *errstr;
6073 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
6074 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6076 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
6077 26ed57b2 2018-05-19 stsp switch (ch) {
6079 64453f7e 2020-11-21 stsp force_text_diff = 1;
6082 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
6084 3dbaef42 2020-11-24 stsp if (errstr != NULL)
6085 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
6086 8f666e67 2022-02-12 thomas errstr, errstr);
6088 09b5bff8 2020-02-23 naddy case 'r':
6089 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
6090 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
6091 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
6093 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
6096 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
6099 17020d27 2019-03-07 stsp usage_diff();
6100 26ed57b2 2018-05-19 stsp /* NOTREACHED */
6104 26ed57b2 2018-05-19 stsp argc -= optind;
6105 26ed57b2 2018-05-19 stsp argv += optind;
6107 26ed57b2 2018-05-19 stsp if (argc == 0) {
6108 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
6109 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
6110 15a94983 2018-12-23 stsp id_str1 = argv[0];
6111 15a94983 2018-12-23 stsp id_str2 = argv[1];
6113 26ed57b2 2018-05-19 stsp usage_diff();
6115 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6116 7cd52833 2022-06-23 thomas if (error)
6117 7cd52833 2022-06-23 thomas goto done;
6119 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
6120 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6121 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6122 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6123 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
6124 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6125 c156c7a4 2020-12-18 stsp goto done;
6126 a273ac94 2020-02-23 naddy if (worktree)
6127 a273ac94 2020-02-23 naddy repo_path =
6128 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
6130 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
6131 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6132 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6133 c156c7a4 2020-12-18 stsp goto done;
6137 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6138 eb6600df 2019-01-04 stsp if (error)
6139 eb6600df 2019-01-04 stsp goto done;
6141 557d3365 2023-04-14 thomas init_curses();
6143 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6144 51a10b52 2020-12-26 stsp if (error)
6145 51a10b52 2020-12-26 stsp goto done;
6147 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6148 26ed57b2 2018-05-19 stsp if (error)
6149 26ed57b2 2018-05-19 stsp goto done;
6151 66b04f8f 2023-07-19 thomas if (id_str1 != NULL) {
6152 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6153 66b04f8f 2023-07-19 thomas repo, worktree);
6154 66b04f8f 2023-07-19 thomas if (error != NULL)
6155 66b04f8f 2023-07-19 thomas goto done;
6156 66b04f8f 2023-07-19 thomas if (keyword_idstr1 != NULL)
6157 66b04f8f 2023-07-19 thomas id_str1 = keyword_idstr1;
6159 66b04f8f 2023-07-19 thomas if (id_str2 != NULL) {
6160 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6161 66b04f8f 2023-07-19 thomas repo, worktree);
6162 66b04f8f 2023-07-19 thomas if (error != NULL)
6163 66b04f8f 2023-07-19 thomas goto done;
6164 66b04f8f 2023-07-19 thomas if (keyword_idstr2 != NULL)
6165 66b04f8f 2023-07-19 thomas id_str2 = keyword_idstr2;
6168 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
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 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
6174 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6175 26ed57b2 2018-05-19 stsp if (error)
6176 26ed57b2 2018-05-19 stsp goto done;
6178 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6179 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
6180 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6181 ea5e7bb5 2018-08-01 stsp goto done;
6183 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6184 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
6185 5dc9f4bc 2018-08-04 stsp if (error)
6186 5dc9f4bc 2018-08-04 stsp goto done;
6188 349dfd1e 2023-07-23 thomas if (worktree) {
6189 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
6190 349dfd1e 2023-07-23 thomas if (error != NULL)
6191 349dfd1e 2023-07-23 thomas goto done;
6193 a7245e84 2023-10-21 thomas /* Release work tree lock. */
6194 a7245e84 2023-10-21 thomas got_worktree_close(worktree);
6195 a7245e84 2023-10-21 thomas worktree = NULL;
6198 557d3365 2023-04-14 thomas error = view_loop(view);
6201 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
6202 66b04f8f 2023-07-19 thomas free(keyword_idstr1);
6203 66b04f8f 2023-07-19 thomas free(keyword_idstr2);
6204 3dbaef42 2020-11-24 stsp free(label1);
6205 3dbaef42 2020-11-24 stsp free(label2);
6206 c02c541e 2019-03-29 stsp free(repo_path);
6207 a273ac94 2020-02-23 naddy free(cwd);
6208 1d0f4054 2021-06-17 stsp if (repo) {
6209 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6210 1d0f4054 2021-06-17 stsp if (error == NULL)
6211 1d0f4054 2021-06-17 stsp error = close_err;
6213 a273ac94 2020-02-23 naddy if (worktree)
6214 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
6215 7cd52833 2022-06-23 thomas if (pack_fds) {
6216 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6217 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6218 7cd52833 2022-06-23 thomas if (error == NULL)
6219 7cd52833 2022-06-23 thomas error = pack_err;
6221 51a10b52 2020-12-26 stsp tog_free_refs();
6222 26ed57b2 2018-05-19 stsp return error;
6225 4ed7e80c 2018-05-20 stsp __dead static void
6226 9f7d7167 2018-04-29 stsp usage_blame(void)
6229 91198554 2022-06-23 thomas fprintf(stderr,
6230 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
6231 9f7d7167 2018-04-29 stsp getprogname());
6235 84451b3e 2018-07-10 stsp struct tog_blame_line {
6236 84451b3e 2018-07-10 stsp int annotated;
6237 84451b3e 2018-07-10 stsp struct got_object_id *id;
6240 4ed7e80c 2018-05-20 stsp static const struct got_error *
6241 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
6243 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6244 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6245 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
6246 84451b3e 2018-07-10 stsp const struct got_error *err;
6247 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
6248 826082fe 2020-12-10 stsp char *line = NULL;
6249 826082fe 2020-12-10 stsp size_t linesize = 0;
6250 826082fe 2020-12-10 stsp ssize_t linelen;
6251 84451b3e 2018-07-10 stsp wchar_t *wline;
6252 27a741e5 2019-09-11 stsp int width;
6253 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
6254 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
6255 ab089a2a 2018-07-12 stsp char *id_str;
6256 11b20872 2019-11-08 stsp struct tog_color *tc;
6258 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
6260 ab089a2a 2018-07-12 stsp return err;
6262 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
6263 f7d12f7e 2018-08-01 stsp werase(view->window);
6265 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
6266 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6267 ab089a2a 2018-07-12 stsp free(id_str);
6268 ab089a2a 2018-07-12 stsp return err;
6271 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6272 ab089a2a 2018-07-12 stsp free(line);
6273 2550e4c3 2018-07-13 stsp line = NULL;
6275 1cae65b4 2019-09-22 stsp return err;
6276 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6277 a3404814 2018-09-02 stsp wstandout(view->window);
6278 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6280 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6281 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6282 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6283 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6285 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6286 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6287 a3404814 2018-09-02 stsp wstandend(view->window);
6288 2550e4c3 2018-07-13 stsp free(wline);
6289 2550e4c3 2018-07-13 stsp wline = NULL;
6291 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
6292 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
6294 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6295 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6298 1134ebde 2023-04-22 thomas rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6300 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_wait");
6301 1134ebde 2023-04-22 thomas tog_io.wait_for_ui = 0;
6304 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6305 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6306 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6307 ab089a2a 2018-07-12 stsp free(id_str);
6308 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6310 ab089a2a 2018-07-12 stsp free(id_str);
6311 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6312 3f60a8ef 2018-07-10 stsp free(line);
6313 2550e4c3 2018-07-13 stsp line = NULL;
6315 3f60a8ef 2018-07-10 stsp return err;
6316 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6317 2550e4c3 2018-07-13 stsp free(wline);
6318 2550e4c3 2018-07-13 stsp wline = NULL;
6319 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6320 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6322 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6323 05171be4 2022-06-23 thomas view->maxx = 0;
6324 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6325 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6326 826082fe 2020-12-10 stsp if (linelen == -1) {
6327 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6328 826082fe 2020-12-10 stsp s->eof = 1;
6331 84451b3e 2018-07-10 stsp free(line);
6332 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6334 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6335 07dd3ed3 2022-08-06 thomas continue;
6336 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
6339 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
6340 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6341 cbea3800 2022-06-23 thomas if (err) {
6342 cbea3800 2022-06-23 thomas free(line);
6343 cbea3800 2022-06-23 thomas return err;
6345 cbea3800 2022-06-23 thomas free(wline);
6346 cbea3800 2022-06-23 thomas wline = NULL;
6347 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
6349 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6350 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6352 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6353 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6354 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6355 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6356 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
6357 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6358 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6359 8d0fe45a 2019-08-12 stsp char *id_str;
6360 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
6361 91198554 2022-06-23 thomas blame_line->id);
6362 8d0fe45a 2019-08-12 stsp if (err) {
6363 8d0fe45a 2019-08-12 stsp free(line);
6364 8d0fe45a 2019-08-12 stsp return err;
6366 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6368 11b20872 2019-11-08 stsp wattr_on(view->window,
6369 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6370 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6372 11b20872 2019-11-08 stsp wattr_off(view->window,
6373 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6374 8d0fe45a 2019-08-12 stsp free(id_str);
6375 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6377 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6378 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6381 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6382 ee41ec32 2018-07-10 stsp prev_id = NULL;
6385 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6386 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6387 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6389 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6390 41605754 2020-11-12 stsp width = 9;
6391 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6392 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6393 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6394 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6395 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
6396 41605754 2020-11-12 stsp if (err) {
6397 41605754 2020-11-12 stsp free(line);
6398 41605754 2020-11-12 stsp return err;
6400 41605754 2020-11-12 stsp width += 9;
6402 923086fd 2022-06-23 thomas int skip;
6403 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
6404 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
6405 923086fd 2022-06-23 thomas if (err) {
6406 923086fd 2022-06-23 thomas free(line);
6407 923086fd 2022-06-23 thomas return err;
6409 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
6410 02bce7e2 2022-06-23 thomas width += 9;
6411 41605754 2020-11-12 stsp free(wline);
6412 41605754 2020-11-12 stsp wline = NULL;
6415 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6416 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6417 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6418 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6420 826082fe 2020-12-10 stsp free(line);
6421 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6423 a5d43cac 2022-07-01 thomas view_border(view);
6425 84451b3e 2018-07-10 stsp return NULL;
6428 84451b3e 2018-07-10 stsp static const struct got_error *
6429 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
6430 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
6432 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6433 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6434 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6435 1a76625f 2018-10-22 stsp int errcode;
6437 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6438 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6439 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6441 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6442 1a76625f 2018-10-22 stsp if (errcode)
6443 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6445 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6446 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6447 d68a0a7d 2018-07-10 stsp goto done;
6450 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6451 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6453 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6454 d68a0a7d 2018-07-10 stsp if (line->annotated)
6455 d68a0a7d 2018-07-10 stsp goto done;
6457 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6458 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6459 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6460 84451b3e 2018-07-10 stsp goto done;
6462 84451b3e 2018-07-10 stsp line->annotated = 1;
6464 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6465 1a76625f 2018-10-22 stsp if (errcode)
6466 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6467 84451b3e 2018-07-10 stsp return err;
6470 84451b3e 2018-07-10 stsp static void *
6471 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6473 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6474 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6475 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6476 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
6477 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
6479 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
6480 9117a7b7 2022-07-01 thomas if (fd1 == -1)
6481 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
6483 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
6484 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
6485 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6486 9117a7b7 2022-07-01 thomas goto done;
6489 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
6490 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
6491 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6492 9117a7b7 2022-07-01 thomas goto done;
6494 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
6495 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
6496 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6497 9117a7b7 2022-07-01 thomas goto done;
6500 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6502 9117a7b7 2022-07-01 thomas goto done;
6504 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6505 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
6506 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6507 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6508 fc06ba56 2019-08-22 stsp err = NULL;
6510 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6511 9117a7b7 2022-07-01 thomas if (errcode) {
6512 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
6513 9117a7b7 2022-07-01 thomas goto done;
6516 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6517 1d0f4054 2021-06-17 stsp if (err == NULL)
6518 1d0f4054 2021-06-17 stsp err = close_err;
6519 c9beca56 2018-07-22 stsp ta->repo = NULL;
6520 c9beca56 2018-07-22 stsp *ta->complete = 1;
6522 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6523 1134ebde 2023-04-22 thomas errcode = pthread_cond_signal(&ta->blame_complete);
6524 1134ebde 2023-04-22 thomas if (errcode && err == NULL)
6525 1134ebde 2023-04-22 thomas err = got_error_set_errno(errcode,
6526 1134ebde 2023-04-22 thomas "pthread_cond_signal");
6529 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6530 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6531 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6534 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6535 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
6536 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6537 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
6538 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
6539 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6540 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
6541 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6543 18430de3 2018-07-10 stsp return (void *)err;
6546 245d91c1 2018-07-12 stsp static struct got_object_id *
6547 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6548 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6550 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6552 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6553 8d0fe45a 2019-08-12 stsp return NULL;
6555 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6556 4fc71f3b 2022-07-12 thomas if (!line->annotated)
6557 4fc71f3b 2022-07-12 thomas return NULL;
6559 4fc71f3b 2022-07-12 thomas return line->id;
6562 4fc71f3b 2022-07-12 thomas static struct got_object_id *
6563 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6564 4fc71f3b 2022-07-12 thomas int lineno)
6566 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
6568 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6569 4fc71f3b 2022-07-12 thomas return NULL;
6571 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6572 245d91c1 2018-07-12 stsp if (!line->annotated)
6573 245d91c1 2018-07-12 stsp return NULL;
6575 245d91c1 2018-07-12 stsp return line->id;
6578 b880a918 2018-07-10 stsp static const struct got_error *
6579 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6581 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6584 245d91c1 2018-07-12 stsp if (blame->thread) {
6585 1a76625f 2018-10-22 stsp int errcode;
6586 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6587 1a76625f 2018-10-22 stsp if (errcode)
6588 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6589 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6590 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6591 1a76625f 2018-10-22 stsp if (errcode)
6592 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6593 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6594 1a76625f 2018-10-22 stsp if (errcode)
6595 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6596 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6597 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6598 245d91c1 2018-07-12 stsp err = NULL;
6599 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6601 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6602 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6603 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6604 1d0f4054 2021-06-17 stsp if (err == NULL)
6605 1d0f4054 2021-06-17 stsp err = close_err;
6606 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6608 245d91c1 2018-07-12 stsp if (blame->f) {
6609 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6610 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6611 245d91c1 2018-07-12 stsp blame->f = NULL;
6613 57670559 2018-12-23 stsp if (blame->lines) {
6614 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6615 57670559 2018-12-23 stsp free(blame->lines[i].id);
6616 57670559 2018-12-23 stsp free(blame->lines);
6617 57670559 2018-12-23 stsp blame->lines = NULL;
6619 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6620 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6621 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6622 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6623 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6624 7cd52833 2022-06-23 thomas if (err == NULL)
6625 7cd52833 2022-06-23 thomas err = pack_err;
6626 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6628 7477a2d2 2023-08-23 thomas free(blame->line_offsets);
6629 7477a2d2 2023-08-23 thomas blame->line_offsets = NULL;
6630 245d91c1 2018-07-12 stsp return err;
6633 245d91c1 2018-07-12 stsp static const struct got_error *
6634 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6636 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6637 fc06ba56 2019-08-22 stsp int *done = arg;
6638 fc06ba56 2019-08-22 stsp int errcode;
6640 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6641 fc06ba56 2019-08-22 stsp if (errcode)
6642 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6643 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6645 fc06ba56 2019-08-22 stsp if (*done)
6646 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6648 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6649 fc06ba56 2019-08-22 stsp if (errcode)
6650 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6651 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6653 fc06ba56 2019-08-22 stsp return err;
6656 fc06ba56 2019-08-22 stsp static const struct got_error *
6657 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6659 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6660 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6661 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6662 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6663 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6664 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6665 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6666 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6667 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6669 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6670 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6672 15a94983 2018-12-23 stsp return err;
6674 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6675 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6676 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6677 f4ae6ddb 2022-07-01 thomas goto done;
6680 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6681 945f9229 2022-04-16 thomas if (err)
6682 945f9229 2022-04-16 thomas goto done;
6684 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6686 84451b3e 2018-07-10 stsp goto done;
6688 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6689 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6690 84451b3e 2018-07-10 stsp goto done;
6693 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6695 a70480e0 2018-06-23 stsp goto done;
6696 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6697 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6698 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6699 84451b3e 2018-07-10 stsp goto done;
6701 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6702 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6704 1fddf795 2021-01-20 stsp goto done;
6705 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6706 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6707 84451b3e 2018-07-10 stsp goto done;
6710 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6711 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6712 b02560ec 2019-08-19 stsp blame->nlines--;
6714 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6715 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6716 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6717 84451b3e 2018-07-10 stsp goto done;
6720 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6722 bd24772e 2018-07-11 stsp goto done;
6723 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6724 7cd52833 2022-06-23 thomas pack_fds);
6725 7cd52833 2022-06-23 thomas if (err)
6726 7cd52833 2022-06-23 thomas goto done;
6728 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6729 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6730 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6731 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6732 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6733 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6734 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6735 245d91c1 2018-07-12 stsp goto done;
6737 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6739 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6740 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6741 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6742 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6743 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6744 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6745 a5388363 2020-12-01 naddy s->blame_complete = 0;
6747 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6748 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6749 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6750 f5a09613 2020-12-13 naddy s->selected_line = 1;
6752 aa61903a 2021-12-31 thomas s->matched_line = 0;
6755 945f9229 2022-04-16 thomas if (commit)
6756 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6757 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6758 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6760 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6761 27d434c2 2018-09-15 stsp free(obj_id);
6763 245d91c1 2018-07-12 stsp stop_blame(blame);
6764 245d91c1 2018-07-12 stsp return err;
6767 245d91c1 2018-07-12 stsp static const struct got_error *
6768 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6769 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6771 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6772 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6774 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6776 c4843652 2019-08-12 stsp s->path = strdup(path);
6777 c4843652 2019-08-12 stsp if (s->path == NULL)
6778 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6780 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6781 c4843652 2019-08-12 stsp if (err) {
6782 c4843652 2019-08-12 stsp free(s->path);
6783 7cbe629d 2018-08-04 stsp return err;
6786 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6787 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6788 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6789 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6790 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6791 fb2756b9 2018-08-04 stsp s->repo = repo;
6792 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6793 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6795 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6796 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6797 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6798 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6800 11b20872 2019-11-08 stsp return err;
6803 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6804 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6805 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6806 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6807 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6808 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6809 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6811 1134ebde 2023-04-22 thomas if (using_mock_io) {
6812 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6815 1134ebde 2023-04-22 thomas rc = pthread_cond_init(&bta->blame_complete, NULL);
6817 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_init");
6820 a5388363 2020-12-01 naddy return run_blame(view);
6823 e5a0f69f 2018-08-18 stsp static const struct got_error *
6824 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6826 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6827 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6829 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6830 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6832 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6833 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6834 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6835 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6836 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6839 1134ebde 2023-04-22 thomas if (using_mock_io) {
6840 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6843 1134ebde 2023-04-22 thomas rc = pthread_cond_destroy(&bta->blame_complete);
6844 1134ebde 2023-04-22 thomas if (rc && err == NULL)
6845 1134ebde 2023-04-22 thomas err = got_error_set_errno(rc, "pthread_cond_destroy");
6848 e5a0f69f 2018-08-18 stsp free(s->path);
6849 11b20872 2019-11-08 stsp free_colors(&s->colors);
6850 e5a0f69f 2018-08-18 stsp return err;
6853 7cbe629d 2018-08-04 stsp static const struct got_error *
6854 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6856 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6858 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6859 6c4c42e0 2019-06-24 stsp return NULL;
6862 2a7d3cd7 2022-09-17 thomas static void
6863 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6864 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6866 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6868 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6869 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6870 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6871 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6872 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6873 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6874 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6877 6c4c42e0 2019-06-24 stsp static const struct got_error *
6878 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6880 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6881 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6882 2b380cc8 2018-10-24 stsp int errcode;
6884 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6885 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6886 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6887 2b380cc8 2018-10-24 stsp if (errcode)
6888 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6890 557d3365 2023-04-14 thomas if (!using_mock_io)
6891 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while annotating */
6894 557d3365 2023-04-14 thomas if (s->blame_complete && !using_mock_io)
6895 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6897 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6899 a5d43cac 2022-07-01 thomas view_border(view);
6900 e5a0f69f 2018-08-18 stsp return err;
6903 e5a0f69f 2018-08-18 stsp static const struct got_error *
6904 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6905 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6907 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6908 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6910 eaeaa612 2022-07-20 thomas *new_view = NULL;
6912 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6913 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6914 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6916 92845f09 2023-07-26 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0, NULL);
6917 eaeaa612 2022-07-20 thomas if (err)
6918 eaeaa612 2022-07-20 thomas view_close(log_view);
6920 eaeaa612 2022-07-20 thomas *new_view = log_view;
6922 eaeaa612 2022-07-20 thomas return err;
6925 eaeaa612 2022-07-20 thomas static const struct got_error *
6926 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6928 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6929 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6930 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6931 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6933 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6934 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6935 a5d43cac 2022-07-01 thomas --eos; /* border */
6937 e5a0f69f 2018-08-18 stsp switch (ch) {
6938 05171be4 2022-06-23 thomas case '0':
6939 05171be4 2022-06-23 thomas case '$':
6940 05171be4 2022-06-23 thomas case KEY_RIGHT:
6941 05171be4 2022-06-23 thomas case 'l':
6942 05171be4 2022-06-23 thomas case KEY_LEFT:
6943 05171be4 2022-06-23 thomas case 'h':
6944 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6947 1e37a5c2 2019-05-12 jcs s->done = 1;
6949 4deef56f 2021-09-02 naddy case 'g':
6950 4deef56f 2021-09-02 naddy case KEY_HOME:
6951 4deef56f 2021-09-02 naddy s->selected_line = 1;
6952 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6953 07b0611c 2022-06-23 thomas view->count = 0;
6955 4deef56f 2021-09-02 naddy case 'G':
6956 4deef56f 2021-09-02 naddy case KEY_END:
6957 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6958 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6959 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6961 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6962 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6964 07b0611c 2022-06-23 thomas view->count = 0;
6967 1e37a5c2 2019-05-12 jcs case KEY_UP:
6968 f7140bf5 2021-10-17 thomas case CTRL('p'):
6969 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6970 1e37a5c2 2019-05-12 jcs s->selected_line--;
6971 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6972 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6973 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6975 07b0611c 2022-06-23 thomas view->count = 0;
6977 70f17a53 2022-06-13 thomas case CTRL('u'):
6978 23427b14 2022-06-23 thomas case 'u':
6979 70f17a53 2022-06-13 thomas nscroll /= 2;
6980 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6981 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6982 ea025d1d 2020-02-22 naddy case CTRL('b'):
6983 1c5e5faa 2022-06-23 thomas case 'b':
6984 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6985 07b0611c 2022-06-23 thomas if (view->count > 1)
6986 07b0611c 2022-06-23 thomas nscroll += nscroll;
6987 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6988 07b0611c 2022-06-23 thomas view->count = 0;
6991 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6992 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6994 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6997 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6998 f7140bf5 2021-10-17 thomas case CTRL('n'):
6999 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
7000 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
7001 1e37a5c2 2019-05-12 jcs s->selected_line++;
7002 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
7003 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
7005 07b0611c 2022-06-23 thomas view->count = 0;
7007 1c5e5faa 2022-06-23 thomas case 'c':
7008 1e37a5c2 2019-05-12 jcs case 'p': {
7009 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
7011 07b0611c 2022-06-23 thomas view->count = 0;
7012 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7013 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
7014 1e37a5c2 2019-05-12 jcs if (id == NULL)
7016 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
7017 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
7018 15a94983 2018-12-23 stsp struct got_object_qid *pid;
7019 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
7020 1e37a5c2 2019-05-12 jcs int obj_type;
7021 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
7022 1e37a5c2 2019-05-12 jcs s->repo, id);
7025 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
7026 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
7027 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
7028 15a94983 2018-12-23 stsp got_object_commit_close(commit);
7031 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
7032 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
7033 ec242592 2022-04-22 thomas s->repo, &pid->id);
7034 945f9229 2022-04-16 thomas if (err)
7036 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
7037 945f9229 2022-04-16 thomas pcommit, s->path);
7038 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
7039 e5a0f69f 2018-08-18 stsp if (err) {
7040 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
7041 1e37a5c2 2019-05-12 jcs err = NULL;
7042 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7045 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
7047 1e37a5c2 2019-05-12 jcs free(blob_id);
7048 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
7049 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
7050 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7053 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
7054 ec242592 2022-04-22 thomas &pid->id);
7055 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7057 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
7058 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
7060 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
7065 1e37a5c2 2019-05-12 jcs s->done = 1;
7066 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
7067 1e37a5c2 2019-05-12 jcs s->done = 0;
7068 1e37a5c2 2019-05-12 jcs if (thread_err)
7070 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
7071 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
7072 a5388363 2020-12-01 naddy err = run_blame(view);
7077 1c5e5faa 2022-06-23 thomas case 'C': {
7078 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
7080 07b0611c 2022-06-23 thomas view->count = 0;
7081 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
7082 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
7084 1e37a5c2 2019-05-12 jcs s->done = 1;
7085 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
7086 1e37a5c2 2019-05-12 jcs s->done = 0;
7087 1e37a5c2 2019-05-12 jcs if (thread_err)
7089 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
7090 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
7091 1e37a5c2 2019-05-12 jcs s->blamed_commit =
7092 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
7093 a5388363 2020-12-01 naddy err = run_blame(view);
7098 2a31b33b 2022-07-23 thomas case 'L':
7099 eaeaa612 2022-07-20 thomas view->count = 0;
7100 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
7101 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
7102 2a31b33b 2022-07-23 thomas if (s->id_to_log)
7103 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7105 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7106 1e37a5c2 2019-05-12 jcs case '\r': {
7107 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
7108 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
7109 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
7111 07b0611c 2022-06-23 thomas view->count = 0;
7112 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7113 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
7114 1e37a5c2 2019-05-12 jcs if (id == NULL)
7116 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
7119 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7120 4fc71f3b 2022-07-12 thomas if (*new_view) {
7121 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
7122 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
7123 4fc71f3b 2022-07-12 thomas if (err)
7125 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
7126 4fc71f3b 2022-07-12 thomas } else {
7127 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
7128 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
7130 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
7131 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
7132 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
7133 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
7134 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
7138 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
7139 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
7140 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7141 c68571e2 2023-07-17 thomas if (err)
7143 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
7144 4fc71f3b 2022-07-12 thomas s->selected_line;
7145 4fc71f3b 2022-07-12 thomas if (*new_view)
7146 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
7147 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7148 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7149 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7150 a5d43cac 2022-07-01 thomas if (err)
7154 e78dc838 2020-12-04 stsp view->focussed = 0;
7155 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
7156 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
7157 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
7158 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
7159 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
7160 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
7163 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
7164 40236d76 2022-06-23 thomas if (err)
7166 e78dc838 2020-12-04 stsp view->focus_child = 1;
7168 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
7173 70f17a53 2022-06-13 thomas case CTRL('d'):
7174 23427b14 2022-06-23 thomas case 'd':
7175 70f17a53 2022-06-13 thomas nscroll /= 2;
7176 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7177 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7178 ea025d1d 2020-02-22 naddy case CTRL('f'):
7179 1c5e5faa 2022-06-23 thomas case 'f':
7181 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7182 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
7183 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
7184 07b0611c 2022-06-23 thomas view->count = 0;
7187 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7188 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
7189 70f17a53 2022-06-13 thomas s->selected_line +=
7190 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
7191 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
7193 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
7194 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
7196 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
7197 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
7199 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7200 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
7201 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
7202 1e37a5c2 2019-05-12 jcs view->nlines - 2);
7206 07b0611c 2022-06-23 thomas view->count = 0;
7209 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
7212 adf4c9e0 2022-07-03 thomas static const struct got_error *
7213 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
7215 adf4c9e0 2022-07-03 thomas const struct got_error *err;
7216 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
7218 adf4c9e0 2022-07-03 thomas view->count = 0;
7219 adf4c9e0 2022-07-03 thomas s->done = 1;
7220 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
7221 adf4c9e0 2022-07-03 thomas s->done = 0;
7222 adf4c9e0 2022-07-03 thomas if (err)
7223 adf4c9e0 2022-07-03 thomas return err;
7224 adf4c9e0 2022-07-03 thomas return run_blame(view);
7227 a70480e0 2018-06-23 stsp static const struct got_error *
7228 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
7230 1d98034b 2023-04-14 thomas const struct got_error *error;
7231 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
7232 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
7233 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7234 0587e10c 2020-07-23 stsp char *link_target = NULL;
7235 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7236 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7237 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *commit_id_str = NULL;
7239 51b7e1c3 2023-04-14 thomas struct tog_view *view = NULL;
7240 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7242 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7243 a70480e0 2018-06-23 stsp switch (ch) {
7245 a70480e0 2018-06-23 stsp commit_id_str = optarg;
7248 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
7249 69069811 2018-08-02 stsp if (repo_path == NULL)
7250 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7254 17020d27 2019-03-07 stsp usage_blame();
7255 a70480e0 2018-06-23 stsp /* NOTREACHED */
7259 a70480e0 2018-06-23 stsp argc -= optind;
7260 a70480e0 2018-06-23 stsp argv += optind;
7262 f135c941 2020-02-20 stsp if (argc != 1)
7263 a70480e0 2018-06-23 stsp usage_blame();
7265 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7266 7cd52833 2022-06-23 thomas if (error != NULL)
7267 7cd52833 2022-06-23 thomas goto done;
7269 69069811 2018-08-02 stsp if (repo_path == NULL) {
7270 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7271 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7272 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7273 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
7274 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7275 c156c7a4 2020-12-18 stsp goto done;
7276 f135c941 2020-02-20 stsp if (worktree)
7277 eb41ed75 2019-02-05 stsp repo_path =
7278 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7280 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
7281 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7282 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7283 c156c7a4 2020-12-18 stsp goto done;
7287 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7288 c02c541e 2019-03-29 stsp if (error != NULL)
7289 8e94dd5b 2019-01-04 stsp goto done;
7291 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7292 f135c941 2020-02-20 stsp worktree);
7293 c02c541e 2019-03-29 stsp if (error)
7294 92205607 2019-01-04 stsp goto done;
7296 557d3365 2023-04-14 thomas init_curses();
7298 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7299 51a10b52 2020-12-26 stsp if (error)
7300 51a10b52 2020-12-26 stsp goto done;
7302 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7303 eb41ed75 2019-02-05 stsp if (error)
7304 69069811 2018-08-02 stsp goto done;
7306 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
7307 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
7308 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
7309 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7310 a70480e0 2018-06-23 stsp if (error != NULL)
7311 66b4983c 2018-06-23 stsp goto done;
7312 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7313 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7315 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7316 66b04f8f 2023-07-19 thomas repo, worktree);
7317 66b04f8f 2023-07-19 thomas if (error != NULL)
7318 66b04f8f 2023-07-19 thomas goto done;
7319 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
7320 66b04f8f 2023-07-19 thomas commit_id_str = keyword_idstr;
7322 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7323 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7325 a19e88aa 2018-06-23 stsp if (error != NULL)
7326 e1cd8fed 2018-08-01 stsp goto done;
7328 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7329 945f9229 2022-04-16 thomas if (error)
7330 945f9229 2022-04-16 thomas goto done;
7332 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7333 945f9229 2022-04-16 thomas commit, repo);
7334 7cbe629d 2018-08-04 stsp if (error)
7335 7cbe629d 2018-08-04 stsp goto done;
7337 4334634c 2023-04-22 thomas view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7338 4334634c 2023-04-22 thomas if (view == NULL) {
7339 4334634c 2023-04-22 thomas error = got_error_from_errno("view_open");
7340 4334634c 2023-04-22 thomas goto done;
7342 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7343 78756c87 2020-11-24 stsp commit_id, repo);
7344 4334634c 2023-04-22 thomas if (error != NULL) {
7345 4334634c 2023-04-22 thomas if (view->close == NULL)
7346 4334634c 2023-04-22 thomas close_blame_view(view);
7347 4334634c 2023-04-22 thomas view_close(view);
7348 0587e10c 2020-07-23 stsp goto done;
7351 12314ad4 2019-08-31 stsp if (worktree) {
7352 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
7353 349dfd1e 2023-07-23 thomas if (error != NULL)
7354 349dfd1e 2023-07-23 thomas goto done;
7356 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7357 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7358 12314ad4 2019-08-31 stsp worktree = NULL;
7361 557d3365 2023-04-14 thomas error = view_loop(view);
7364 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
7365 69069811 2018-08-02 stsp free(repo_path);
7366 f135c941 2020-02-20 stsp free(in_repo_path);
7367 0587e10c 2020-07-23 stsp free(link_target);
7368 69069811 2018-08-02 stsp free(cwd);
7369 a70480e0 2018-06-23 stsp free(commit_id);
7370 66b04f8f 2023-07-19 thomas free(keyword_idstr);
7371 945f9229 2022-04-16 thomas if (commit)
7372 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7373 eb41ed75 2019-02-05 stsp if (worktree)
7374 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7375 1d0f4054 2021-06-17 stsp if (repo) {
7376 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7377 1d0f4054 2021-06-17 stsp if (error == NULL)
7378 1d0f4054 2021-06-17 stsp error = close_err;
7380 7cd52833 2022-06-23 thomas if (pack_fds) {
7381 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7382 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7383 7cd52833 2022-06-23 thomas if (error == NULL)
7384 7cd52833 2022-06-23 thomas error = pack_err;
7386 51a10b52 2020-12-26 stsp tog_free_refs();
7387 a70480e0 2018-06-23 stsp return error;
7390 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7391 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7393 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7394 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7395 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7396 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7397 86f4aab9 2022-09-09 thomas char *index = NULL;
7398 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7399 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
7400 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7402 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7403 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7404 a5d43cac 2022-07-01 thomas --limit; /* border */
7406 f7d12f7e 2018-08-01 stsp werase(view->window);
7408 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7409 ffd1d5e5 2018-06-23 stsp return NULL;
7411 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7414 ffd1d5e5 2018-06-23 stsp return err;
7415 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7416 a3404814 2018-09-02 stsp wstandout(view->window);
7417 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7419 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7420 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7421 86f4aab9 2022-09-09 thomas free(wline);
7422 86f4aab9 2022-09-09 thomas wline = NULL;
7423 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7424 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7426 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7427 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7428 a3404814 2018-09-02 stsp wstandend(view->window);
7429 86f4aab9 2022-09-09 thomas if (--limit <= 0)
7430 86f4aab9 2022-09-09 thomas return NULL;
7432 6f5f393a 2022-08-12 thomas i += s->selected;
7433 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
7434 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
7435 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
7436 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
7438 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
7439 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
7440 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7441 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
7442 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7443 86f4aab9 2022-09-09 thomas free(index);
7445 ce52c690 2018-06-23 stsp return err;
7446 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7447 2550e4c3 2018-07-13 stsp free(wline);
7448 2550e4c3 2018-07-13 stsp wline = NULL;
7449 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7450 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7451 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7452 ffd1d5e5 2018-06-23 stsp return NULL;
7453 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7454 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7455 a1eca9bb 2018-06-23 stsp return NULL;
7457 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7458 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7459 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7460 0cf4efb1 2018-09-29 stsp if (view->focussed)
7461 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7462 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7464 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7465 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7466 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7467 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7468 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7469 ffd1d5e5 2018-06-23 stsp return NULL;
7473 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7476 c72de8ab 2023-02-03 thomas view->maxx = 0;
7477 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7478 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7479 848d6979 2019-08-12 stsp const char *modestr = "";
7480 56e0773d 2019-11-28 stsp mode_t mode;
7482 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7483 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7485 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7486 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7487 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7489 638f9024 2019-05-13 stsp return got_error_from_errno(
7490 230a42bd 2019-05-11 jcs "got_object_id_str");
7492 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7493 63c5ca5d 2019-08-24 stsp modestr = "$";
7494 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7497 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7498 d86d3b18 2020-12-01 naddy te, s->repo);
7499 0d6c6ee3 2020-05-20 stsp if (err) {
7500 0d6c6ee3 2020-05-20 stsp free(id_str);
7501 0d6c6ee3 2020-05-20 stsp return err;
7503 cbb35fac 2023-06-25 thomas for (i = 0; link_target[i] != '\0'; i++) {
7504 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7505 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7507 848d6979 2019-08-12 stsp modestr = "@";
7509 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7510 848d6979 2019-08-12 stsp modestr = "/";
7511 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7512 848d6979 2019-08-12 stsp modestr = "*";
7513 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7514 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7515 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7516 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7517 1d13200f 2018-07-12 stsp free(id_str);
7518 0d6c6ee3 2020-05-20 stsp free(link_target);
7519 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7521 1d13200f 2018-07-12 stsp free(id_str);
7522 0d6c6ee3 2020-05-20 stsp free(link_target);
7524 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
7525 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7526 ffd1d5e5 2018-06-23 stsp if (err) {
7527 ffd1d5e5 2018-06-23 stsp free(line);
7530 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
7531 c72de8ab 2023-02-03 thomas free(wline);
7532 c72de8ab 2023-02-03 thomas wline = NULL;
7534 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
7535 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
7536 c72de8ab 2023-02-03 thomas if (err) {
7537 c72de8ab 2023-02-03 thomas free(line);
7540 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7541 0cf4efb1 2018-09-29 stsp if (view->focussed)
7542 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7543 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7545 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7547 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7548 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7549 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
7551 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7552 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7553 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
7554 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7555 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7556 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7557 ffd1d5e5 2018-06-23 stsp free(line);
7558 2550e4c3 2018-07-13 stsp free(wline);
7559 2550e4c3 2018-07-13 stsp wline = NULL;
7561 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7562 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7563 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7567 ffd1d5e5 2018-06-23 stsp return err;
7570 ffd1d5e5 2018-06-23 stsp static void
7571 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7573 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7574 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7575 fa86c4bf 2020-11-29 stsp int i = 0;
7577 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7580 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7581 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7582 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7583 fa86c4bf 2020-11-29 stsp if (!isroot)
7584 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7587 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7588 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7592 a5d43cac 2022-07-01 thomas static const struct got_error *
7593 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
7595 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7596 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7597 ffd1d5e5 2018-06-23 stsp int n = 0;
7599 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7600 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7601 694d3271 2020-12-01 naddy s->first_displayed_entry);
7603 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7605 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7606 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7607 07dd3ed3 2022-08-06 thomas if (last) {
7608 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7609 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7611 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7612 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7613 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7617 a5d43cac 2022-07-01 thomas return NULL;
7620 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7621 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7622 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7624 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7625 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7626 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7628 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7629 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7630 56e0773d 2019-11-28 stsp + 1 /* slash */;
7632 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7634 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7635 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7636 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7638 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7639 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7640 d9765a41 2018-06-23 stsp while (pt) {
7641 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7642 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7643 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7644 cb2ebc8a 2018-06-23 stsp goto done;
7646 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7647 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7648 cb2ebc8a 2018-06-23 stsp goto done;
7650 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7653 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7654 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7655 ce52c690 2018-06-23 stsp goto done;
7659 ce52c690 2018-06-23 stsp if (err) {
7660 ce52c690 2018-06-23 stsp free(*path);
7661 ce52c690 2018-06-23 stsp *path = NULL;
7663 ce52c690 2018-06-23 stsp return err;
7666 ce52c690 2018-06-23 stsp static const struct got_error *
7667 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7668 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7669 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7671 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7672 ce52c690 2018-06-23 stsp char *path;
7673 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7675 a0de39f3 2019-08-09 stsp *new_view = NULL;
7677 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7679 ce52c690 2018-06-23 stsp return err;
7681 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7682 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7683 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7684 83ce39e3 2019-08-12 stsp goto done;
7687 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7688 e5a0f69f 2018-08-18 stsp if (err) {
7689 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7690 fc06ba56 2019-08-22 stsp err = NULL;
7691 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7693 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7695 83ce39e3 2019-08-12 stsp free(path);
7696 69efd4c4 2018-07-18 stsp return err;
7699 69efd4c4 2018-07-18 stsp static const struct got_error *
7700 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7701 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7703 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7704 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7705 69efd4c4 2018-07-18 stsp char *path;
7707 a0de39f3 2019-08-09 stsp *new_view = NULL;
7709 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7710 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7711 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7713 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7715 69efd4c4 2018-07-18 stsp return err;
7717 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7718 92845f09 2023-07-26 thomas path, 0, NULL);
7720 e5a0f69f 2018-08-18 stsp view_close(log_view);
7722 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7723 cb2ebc8a 2018-06-23 stsp free(path);
7724 cb2ebc8a 2018-06-23 stsp return err;
7727 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7728 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7729 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7731 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7732 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7733 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7734 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7736 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7737 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7739 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7740 51b7e1c3 2023-04-14 thomas if (s->commit_id == NULL) {
7741 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
7742 51b7e1c3 2023-04-14 thomas goto done;
7745 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7747 bc573f3b 2021-07-10 stsp goto done;
7750 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7751 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7752 bc573f3b 2021-07-10 stsp * closed on demand.
7754 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7755 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7757 bc573f3b 2021-07-10 stsp goto done;
7758 bc573f3b 2021-07-10 stsp s->tree = s->root;
7760 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7761 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7762 ffd1d5e5 2018-06-23 stsp goto done;
7764 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7765 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7766 ffd1d5e5 2018-06-23 stsp goto done;
7769 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7770 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7771 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7772 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7773 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7774 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7775 9cd7cbd1 2020-12-07 stsp goto done;
7778 fb2756b9 2018-08-04 stsp s->repo = repo;
7780 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7781 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7782 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7783 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7785 c0b01bdb 2019-11-08 stsp goto done;
7786 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7787 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7789 c0b01bdb 2019-11-08 stsp goto done;
7790 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7791 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7792 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7794 c0b01bdb 2019-11-08 stsp goto done;
7796 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7797 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7798 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7800 11b20872 2019-11-08 stsp goto done;
7802 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7803 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7805 c0b01bdb 2019-11-08 stsp goto done;
7808 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7809 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7810 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7811 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7812 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7814 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7815 bc573f3b 2021-07-10 stsp if (commit)
7816 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7817 51b7e1c3 2023-04-14 thomas if (err) {
7818 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
7819 51b7e1c3 2023-04-14 thomas close_tree_view(view);
7820 51b7e1c3 2023-04-14 thomas view_close(view);
7822 ad80ab7b 2018-08-04 stsp return err;
7825 e5a0f69f 2018-08-18 stsp static const struct got_error *
7826 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7828 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7830 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7831 fb2756b9 2018-08-04 stsp free(s->tree_label);
7832 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7833 6484ec90 2018-09-29 stsp free(s->commit_id);
7834 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7835 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7836 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7837 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7838 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7839 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7840 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7841 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7842 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7843 ad80ab7b 2018-08-04 stsp free(parent);
7846 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7847 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7848 bc573f3b 2021-07-10 stsp if (s->root)
7849 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7850 7c32bd05 2019-06-22 stsp return NULL;
7853 7c32bd05 2019-06-22 stsp static const struct got_error *
7854 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7856 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7858 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7859 7c32bd05 2019-06-22 stsp return NULL;
7862 7c32bd05 2019-06-22 stsp static int
7863 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7865 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7867 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7871 7c32bd05 2019-06-22 stsp static const struct got_error *
7872 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7874 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7875 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7877 7c32bd05 2019-06-22 stsp if (!view->searching) {
7878 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7879 7c32bd05 2019-06-22 stsp return NULL;
7882 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7883 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7884 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7885 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7886 56e0773d 2019-11-28 stsp s->selected_entry);
7888 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7890 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7891 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7893 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7894 56e0773d 2019-11-28 stsp s->selected_entry);
7897 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7898 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7899 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7900 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7902 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7905 7c32bd05 2019-06-22 stsp while (1) {
7906 56e0773d 2019-11-28 stsp if (te == NULL) {
7907 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7908 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7909 ac66afb8 2019-06-24 stsp return NULL;
7911 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7912 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7914 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7917 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7918 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7919 56e0773d 2019-11-28 stsp s->matched_entry = te;
7923 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7924 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7926 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7929 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7930 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7931 7c32bd05 2019-06-22 stsp s->selected = 0;
7934 e5a0f69f 2018-08-18 stsp return NULL;
7937 ad80ab7b 2018-08-04 stsp static const struct got_error *
7938 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7940 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7941 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7942 e5a0f69f 2018-08-18 stsp char *parent_path;
7944 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7946 e5a0f69f 2018-08-18 stsp return err;
7948 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7949 e5a0f69f 2018-08-18 stsp free(parent_path);
7951 a5d43cac 2022-07-01 thomas view_border(view);
7952 e5a0f69f 2018-08-18 stsp return err;
7955 07dd3ed3 2022-08-06 thomas static const struct got_error *
7956 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7958 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7959 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7960 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7961 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7962 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7963 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7965 07dd3ed3 2022-08-06 thomas g = view->gline;
7966 07dd3ed3 2022-08-06 thomas view->gline = 0;
7968 07dd3ed3 2022-08-06 thomas if (g == 0)
7970 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7971 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7973 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7974 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7975 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7977 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7978 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7979 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7981 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7982 07dd3ed3 2022-08-06 thomas last += off;
7984 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7985 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7986 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7989 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7990 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7991 07dd3ed3 2022-08-06 thomas i += off;
7994 07dd3ed3 2022-08-06 thomas if (i < g) {
7995 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7996 07dd3ed3 2022-08-06 thomas if (err)
7997 07dd3ed3 2022-08-06 thomas return err;
7998 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7999 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
8000 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
8001 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
8002 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
8003 07dd3ed3 2022-08-06 thomas first += off;
8004 07dd3ed3 2022-08-06 thomas s->selected = g - first;
8006 07dd3ed3 2022-08-06 thomas } else if (i > g)
8007 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
8009 07dd3ed3 2022-08-06 thomas if (g < nlines &&
8010 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
8011 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8013 07dd3ed3 2022-08-06 thomas return NULL;
8016 e5a0f69f 2018-08-18 stsp static const struct got_error *
8017 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
8019 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
8020 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
8021 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
8022 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
8024 07dd3ed3 2022-08-06 thomas if (view->gline)
8025 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
8027 e5a0f69f 2018-08-18 stsp switch (ch) {
8028 c72de8ab 2023-02-03 thomas case '0':
8029 c72de8ab 2023-02-03 thomas case '$':
8030 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
8031 c72de8ab 2023-02-03 thomas case 'l':
8032 c72de8ab 2023-02-03 thomas case KEY_LEFT:
8033 c72de8ab 2023-02-03 thomas case 'h':
8034 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8037 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
8038 07b0611c 2022-06-23 thomas view->count = 0;
8040 1be4947a 2022-07-22 thomas case 'L':
8041 07b0611c 2022-06-23 thomas view->count = 0;
8042 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
8044 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8046 1be4947a 2022-07-22 thomas case 'R':
8047 07b0611c 2022-06-23 thomas view->count = 0;
8048 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
8050 e4526bf5 2021-09-03 naddy case 'g':
8051 aa7a1117 2023-01-09 thomas case '=':
8052 e4526bf5 2021-09-03 naddy case KEY_HOME:
8053 e4526bf5 2021-09-03 naddy s->selected = 0;
8054 07b0611c 2022-06-23 thomas view->count = 0;
8055 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
8056 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
8057 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
8059 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
8061 e4526bf5 2021-09-03 naddy case 'G':
8062 aa7a1117 2023-01-09 thomas case '*':
8063 a5d43cac 2022-07-01 thomas case KEY_END: {
8064 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
8066 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8067 a5d43cac 2022-07-01 thomas --eos; /* border */
8068 e4526bf5 2021-09-03 naddy s->selected = 0;
8069 07b0611c 2022-06-23 thomas view->count = 0;
8070 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
8071 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8072 e4526bf5 2021-09-03 naddy if (te == NULL) {
8073 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
8074 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
8079 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
8080 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
8082 e4526bf5 2021-09-03 naddy if (n > 0)
8083 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8087 1e37a5c2 2019-05-12 jcs case KEY_UP:
8088 f7140bf5 2021-10-17 thomas case CTRL('p'):
8089 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
8090 1e37a5c2 2019-05-12 jcs s->selected--;
8093 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
8094 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
8095 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
8096 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
8097 07b0611c 2022-06-23 thomas view->count = 0;
8099 70f17a53 2022-06-13 thomas case CTRL('u'):
8100 23427b14 2022-06-23 thomas case 'u':
8101 70f17a53 2022-06-13 thomas nscroll /= 2;
8102 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8103 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
8104 ea025d1d 2020-02-22 naddy case CTRL('b'):
8105 1c5e5faa 2022-06-23 thomas case 'b':
8106 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
8107 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
8108 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
8109 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
8111 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
8112 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
8114 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
8115 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
8116 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
8117 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
8118 07b0611c 2022-06-23 thomas view->count = 0;
8121 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
8122 f7140bf5 2021-10-17 thomas case CTRL('n'):
8123 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
8124 1e37a5c2 2019-05-12 jcs s->selected++;
8127 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8128 07b0611c 2022-06-23 thomas == NULL) {
8129 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
8130 07b0611c 2022-06-23 thomas view->count = 0;
8133 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
8135 70f17a53 2022-06-13 thomas case CTRL('d'):
8136 23427b14 2022-06-23 thomas case 'd':
8137 70f17a53 2022-06-13 thomas nscroll /= 2;
8138 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8139 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
8140 ea025d1d 2020-02-22 naddy case CTRL('f'):
8141 1c5e5faa 2022-06-23 thomas case 'f':
8142 4c2d69cb 2022-06-23 thomas case ' ':
8143 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8145 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
8146 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
8147 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8148 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8150 07b0611c 2022-06-23 thomas view->count = 0;
8153 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
8155 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
8157 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
8158 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
8159 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
8160 1e37a5c2 2019-05-12 jcs /* user selected '..' */
8161 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
8162 07b0611c 2022-06-23 thomas view->count = 0;
8165 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
8166 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
8168 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
8169 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
8170 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
8171 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
8172 1e37a5c2 2019-05-12 jcs s->selected_entry =
8173 1e37a5c2 2019-05-12 jcs parent->selected_entry;
8174 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
8175 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
8176 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
8177 a5d43cac 2022-07-01 thomas if (err)
8180 1e37a5c2 2019-05-12 jcs free(parent);
8181 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
8182 56e0773d 2019-11-28 stsp s->selected_entry))) {
8183 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
8184 07b0611c 2022-06-23 thomas view->count = 0;
8185 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
8186 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
8189 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
8190 941e9f74 2019-05-21 stsp if (err) {
8191 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
8194 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8195 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8197 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
8198 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8199 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
8200 07b0611c 2022-06-23 thomas view->count = 0;
8203 07b0611c 2022-06-23 thomas view->count = 0;
8207 ffd1d5e5 2018-06-23 stsp return err;
8210 ffd1d5e5 2018-06-23 stsp __dead static void
8211 ffd1d5e5 2018-06-23 stsp usage_tree(void)
8214 91198554 2022-06-23 thomas fprintf(stderr,
8215 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8216 ffd1d5e5 2018-06-23 stsp getprogname());
8220 ffd1d5e5 2018-06-23 stsp static const struct got_error *
8221 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
8223 1d98034b 2023-04-14 thomas const struct got_error *error;
8224 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
8225 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
8226 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8227 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
8228 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8229 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
8230 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *label = NULL;
8231 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
8232 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
8234 5221c383 2018-08-01 stsp struct tog_view *view;
8235 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8237 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8238 ffd1d5e5 2018-06-23 stsp switch (ch) {
8240 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
8243 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
8244 74283ab8 2020-02-07 stsp if (repo_path == NULL)
8245 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
8249 e99e2d15 2020-11-24 naddy usage_tree();
8250 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
8254 ffd1d5e5 2018-06-23 stsp argc -= optind;
8255 ffd1d5e5 2018-06-23 stsp argv += optind;
8257 55cccc34 2020-02-20 stsp if (argc > 1)
8258 e99e2d15 2020-11-24 naddy usage_tree();
8260 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8261 7cd52833 2022-06-23 thomas if (error != NULL)
8262 7cd52833 2022-06-23 thomas goto done;
8264 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
8265 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8266 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8267 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8268 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
8269 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8270 c156c7a4 2020-12-18 stsp goto done;
8271 55cccc34 2020-02-20 stsp if (worktree)
8272 52185f70 2019-02-05 stsp repo_path =
8273 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
8275 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
8276 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8277 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8278 c156c7a4 2020-12-18 stsp goto done;
8282 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8283 c02c541e 2019-03-29 stsp if (error != NULL)
8284 52185f70 2019-02-05 stsp goto done;
8286 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8287 55cccc34 2020-02-20 stsp repo, worktree);
8288 55cccc34 2020-02-20 stsp if (error)
8289 55cccc34 2020-02-20 stsp goto done;
8291 557d3365 2023-04-14 thomas init_curses();
8293 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8294 c02c541e 2019-03-29 stsp if (error)
8295 52185f70 2019-02-05 stsp goto done;
8297 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8298 51a10b52 2020-12-26 stsp if (error)
8299 51a10b52 2020-12-26 stsp goto done;
8301 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
8302 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
8303 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
8304 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8305 4e97c21c 2020-12-06 stsp if (error)
8306 4e97c21c 2020-12-06 stsp goto done;
8307 4e97c21c 2020-12-06 stsp head_ref_name = label;
8309 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8310 66b04f8f 2023-07-19 thomas repo, worktree);
8311 66b04f8f 2023-07-19 thomas if (error != NULL)
8312 66b04f8f 2023-07-19 thomas goto done;
8313 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
8314 66b04f8f 2023-07-19 thomas commit_id_arg = keyword_idstr;
8316 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
8317 4e97c21c 2020-12-06 stsp if (error == NULL)
8318 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
8319 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
8320 4e97c21c 2020-12-06 stsp goto done;
8321 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
8322 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8323 4e97c21c 2020-12-06 stsp if (error)
8324 4e97c21c 2020-12-06 stsp goto done;
8327 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8328 945f9229 2022-04-16 thomas if (error)
8329 945f9229 2022-04-16 thomas goto done;
8331 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8332 5221c383 2018-08-01 stsp if (view == NULL) {
8333 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
8334 5221c383 2018-08-01 stsp goto done;
8336 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8337 ad80ab7b 2018-08-04 stsp if (error)
8338 ad80ab7b 2018-08-04 stsp goto done;
8339 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8340 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
8341 d91faf3b 2020-12-01 naddy in_repo_path);
8342 55cccc34 2020-02-20 stsp if (error)
8343 55cccc34 2020-02-20 stsp goto done;
8346 55cccc34 2020-02-20 stsp if (worktree) {
8347 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
8348 349dfd1e 2023-07-23 thomas if (error != NULL)
8349 349dfd1e 2023-07-23 thomas goto done;
8351 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8352 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8353 55cccc34 2020-02-20 stsp worktree = NULL;
8356 557d3365 2023-04-14 thomas error = view_loop(view);
8359 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
8360 66b04f8f 2023-07-19 thomas free(keyword_idstr);
8361 52185f70 2019-02-05 stsp free(repo_path);
8362 e4a0e26d 2020-02-20 stsp free(cwd);
8363 ffd1d5e5 2018-06-23 stsp free(commit_id);
8364 4e97c21c 2020-12-06 stsp free(label);
8366 486cd271 2020-12-06 stsp got_ref_close(ref);
8367 349dfd1e 2023-07-23 thomas if (worktree != NULL)
8368 349dfd1e 2023-07-23 thomas got_worktree_close(worktree);
8369 1d0f4054 2021-06-17 stsp if (repo) {
8370 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8371 1d0f4054 2021-06-17 stsp if (error == NULL)
8372 1d0f4054 2021-06-17 stsp error = close_err;
8374 7cd52833 2022-06-23 thomas if (pack_fds) {
8375 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8376 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8377 7cd52833 2022-06-23 thomas if (error == NULL)
8378 7cd52833 2022-06-23 thomas error = pack_err;
8380 51a10b52 2020-12-26 stsp tog_free_refs();
8381 ffd1d5e5 2018-06-23 stsp return error;
8384 6458efa5 2020-11-24 stsp static const struct got_error *
8385 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8387 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8388 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8390 6458efa5 2020-11-24 stsp s->nrefs = 0;
8391 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8392 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
8393 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
8394 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
8395 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
8398 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8399 6458efa5 2020-11-24 stsp if (re == NULL)
8400 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8402 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8403 8924d611 2020-12-26 stsp if (re->ref == NULL)
8404 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8405 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8406 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8409 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8410 6458efa5 2020-11-24 stsp return NULL;
8413 ef20f542 2022-06-26 thomas static void
8414 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8416 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8418 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8419 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8420 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8421 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8426 6458efa5 2020-11-24 stsp static const struct got_error *
8427 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8429 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8430 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8432 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8433 6458efa5 2020-11-24 stsp s->repo = repo;
8435 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8436 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8438 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8440 51b7e1c3 2023-04-14 thomas goto done;
8442 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8443 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8444 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8445 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8447 6458efa5 2020-11-24 stsp goto done;
8449 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8450 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8451 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8453 6458efa5 2020-11-24 stsp goto done;
8455 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8456 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8457 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8458 2183bbf6 2022-01-23 thomas if (err)
8459 2183bbf6 2022-01-23 thomas goto done;
8461 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
8462 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
8463 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
8465 6458efa5 2020-11-24 stsp goto done;
8468 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8469 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8470 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8471 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8472 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8474 51b7e1c3 2023-04-14 thomas if (err) {
8475 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
8476 51b7e1c3 2023-04-14 thomas close_ref_view(view);
8477 51b7e1c3 2023-04-14 thomas view_close(view);
8479 6458efa5 2020-11-24 stsp return err;
8482 6458efa5 2020-11-24 stsp static const struct got_error *
8483 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8485 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8487 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8488 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8490 6458efa5 2020-11-24 stsp return NULL;
8493 6458efa5 2020-11-24 stsp static const struct got_error *
8494 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8495 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8497 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8498 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8499 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8500 6458efa5 2020-11-24 stsp int obj_type;
8502 c42c9805 2020-11-24 stsp *commit_id = NULL;
8504 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8506 6458efa5 2020-11-24 stsp return err;
8508 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8510 6458efa5 2020-11-24 stsp goto done;
8512 6458efa5 2020-11-24 stsp switch (obj_type) {
8513 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8514 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8516 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8517 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8519 6458efa5 2020-11-24 stsp goto done;
8520 c42c9805 2020-11-24 stsp free(obj_id);
8521 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8522 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8524 6458efa5 2020-11-24 stsp goto done;
8525 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8526 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8527 c42c9805 2020-11-24 stsp goto done;
8529 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8530 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8531 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8532 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8533 c42c9805 2020-11-24 stsp goto done;
8537 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8543 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8544 c42c9805 2020-11-24 stsp if (err) {
8545 c42c9805 2020-11-24 stsp free(*commit_id);
8546 c42c9805 2020-11-24 stsp *commit_id = NULL;
8548 c42c9805 2020-11-24 stsp return err;
8551 c42c9805 2020-11-24 stsp static const struct got_error *
8552 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8553 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8555 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8556 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8557 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8559 c42c9805 2020-11-24 stsp *new_view = NULL;
8561 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8562 c42c9805 2020-11-24 stsp if (err) {
8563 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8564 c42c9805 2020-11-24 stsp return err;
8566 c42c9805 2020-11-24 stsp return NULL;
8569 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8570 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8571 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8572 6458efa5 2020-11-24 stsp goto done;
8575 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8576 92845f09 2023-07-26 thomas got_ref_get_name(re->ref), "", 0, NULL);
8579 6458efa5 2020-11-24 stsp view_close(log_view);
8581 6458efa5 2020-11-24 stsp *new_view = log_view;
8582 c42c9805 2020-11-24 stsp free(commit_id);
8583 6458efa5 2020-11-24 stsp return err;
8586 ce5b7c56 2019-07-09 stsp static void
8587 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8589 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8590 34ba6917 2020-11-30 stsp int i = 0;
8592 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8595 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8596 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8597 34ba6917 2020-11-30 stsp if (re == NULL)
8599 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8600 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8604 a5d43cac 2022-07-01 thomas static const struct got_error *
8605 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
8607 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8608 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8609 6458efa5 2020-11-24 stsp int n = 0;
8611 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8612 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8614 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8616 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8617 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
8618 07dd3ed3 2022-08-06 thomas if (last) {
8619 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
8620 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
8622 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8623 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8624 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8628 a5d43cac 2022-07-01 thomas return NULL;
8631 6458efa5 2020-11-24 stsp static const struct got_error *
8632 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8634 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8636 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8637 6458efa5 2020-11-24 stsp return NULL;
8640 6458efa5 2020-11-24 stsp static int
8641 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8643 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8645 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8649 6458efa5 2020-11-24 stsp static const struct got_error *
8650 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8652 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8653 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8655 6458efa5 2020-11-24 stsp if (!view->searching) {
8656 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8657 6458efa5 2020-11-24 stsp return NULL;
8660 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8661 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8662 6458efa5 2020-11-24 stsp if (s->selected_entry)
8663 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8665 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8666 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8668 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8669 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8671 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8672 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8675 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8676 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8677 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8678 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8680 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8683 6458efa5 2020-11-24 stsp while (1) {
8684 6458efa5 2020-11-24 stsp if (re == NULL) {
8685 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8686 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8687 6458efa5 2020-11-24 stsp return NULL;
8689 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8690 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8692 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8695 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8696 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8697 6458efa5 2020-11-24 stsp s->matched_entry = re;
8701 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8702 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8704 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8707 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8708 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8709 6458efa5 2020-11-24 stsp s->selected = 0;
8712 6458efa5 2020-11-24 stsp return NULL;
8715 6458efa5 2020-11-24 stsp static const struct got_error *
8716 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8718 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8719 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8720 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8721 6458efa5 2020-11-24 stsp char *line = NULL;
8722 6458efa5 2020-11-24 stsp wchar_t *wline;
8723 6458efa5 2020-11-24 stsp struct tog_color *tc;
8724 66a70b97 2023-02-03 thomas int width, n, scrollx;
8725 6458efa5 2020-11-24 stsp int limit = view->nlines;
8727 6458efa5 2020-11-24 stsp werase(view->window);
8729 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8730 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8731 a5d43cac 2022-07-01 thomas --limit; /* border */
8733 6458efa5 2020-11-24 stsp if (limit == 0)
8734 6458efa5 2020-11-24 stsp return NULL;
8736 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8738 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8739 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8740 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8742 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8743 6458efa5 2020-11-24 stsp if (err) {
8744 6458efa5 2020-11-24 stsp free(line);
8745 6458efa5 2020-11-24 stsp return err;
8747 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8748 6458efa5 2020-11-24 stsp wstandout(view->window);
8749 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8750 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8751 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8752 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8753 6458efa5 2020-11-24 stsp wstandend(view->window);
8754 6458efa5 2020-11-24 stsp free(wline);
8755 6458efa5 2020-11-24 stsp wline = NULL;
8756 6458efa5 2020-11-24 stsp free(line);
8757 6458efa5 2020-11-24 stsp line = NULL;
8758 6458efa5 2020-11-24 stsp if (--limit <= 0)
8759 6458efa5 2020-11-24 stsp return NULL;
8762 66a70b97 2023-02-03 thomas view->maxx = 0;
8763 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8764 6458efa5 2020-11-24 stsp char *line = NULL;
8765 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8767 84227eb1 2022-06-23 thomas if (s->show_date) {
8768 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8769 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8770 84227eb1 2022-06-23 thomas struct got_object_id *id;
8771 84227eb1 2022-06-23 thomas struct tm tm;
8772 84227eb1 2022-06-23 thomas time_t t;
8774 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8775 84227eb1 2022-06-23 thomas if (err)
8776 84227eb1 2022-06-23 thomas return err;
8777 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8778 84227eb1 2022-06-23 thomas if (err) {
8779 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8780 84227eb1 2022-06-23 thomas free(id);
8781 84227eb1 2022-06-23 thomas return err;
8783 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8785 84227eb1 2022-06-23 thomas if (err) {
8786 84227eb1 2022-06-23 thomas free(id);
8787 84227eb1 2022-06-23 thomas return err;
8789 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8790 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8791 84227eb1 2022-06-23 thomas } else {
8792 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8793 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8795 84227eb1 2022-06-23 thomas free(id);
8796 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8797 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8798 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8799 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8801 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8802 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8803 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8804 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8805 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8806 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8807 6458efa5 2020-11-24 stsp struct got_object_id *id;
8808 6458efa5 2020-11-24 stsp char *id_str;
8809 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8811 6458efa5 2020-11-24 stsp return err;
8812 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8813 6458efa5 2020-11-24 stsp if (err) {
8815 6458efa5 2020-11-24 stsp return err;
8817 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8818 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8819 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8821 6458efa5 2020-11-24 stsp free(id_str);
8822 6458efa5 2020-11-24 stsp return err;
8825 6458efa5 2020-11-24 stsp free(id_str);
8826 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8827 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8828 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8830 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8831 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8832 6458efa5 2020-11-24 stsp if (err) {
8833 6458efa5 2020-11-24 stsp free(line);
8834 6458efa5 2020-11-24 stsp return err;
8836 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8837 66a70b97 2023-02-03 thomas free(wline);
8838 66a70b97 2023-02-03 thomas wline = NULL;
8840 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8841 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8842 66a70b97 2023-02-03 thomas if (err) {
8843 66a70b97 2023-02-03 thomas free(line);
8844 66a70b97 2023-02-03 thomas return err;
8846 6458efa5 2020-11-24 stsp if (n == s->selected) {
8847 6458efa5 2020-11-24 stsp if (view->focussed)
8848 6458efa5 2020-11-24 stsp wstandout(view->window);
8849 6458efa5 2020-11-24 stsp s->selected_entry = re;
8851 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8853 6458efa5 2020-11-24 stsp wattr_on(view->window,
8854 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8855 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8857 6458efa5 2020-11-24 stsp wattr_off(view->window,
8858 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8859 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8860 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8861 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8862 6458efa5 2020-11-24 stsp wstandend(view->window);
8863 6458efa5 2020-11-24 stsp free(line);
8864 6458efa5 2020-11-24 stsp free(wline);
8865 6458efa5 2020-11-24 stsp wline = NULL;
8867 6458efa5 2020-11-24 stsp s->ndisplayed++;
8868 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8871 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8874 a5d43cac 2022-07-01 thomas view_border(view);
8875 6458efa5 2020-11-24 stsp return err;
8878 6458efa5 2020-11-24 stsp static const struct got_error *
8879 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8880 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8882 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8883 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8884 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8886 c42c9805 2020-11-24 stsp *new_view = NULL;
8888 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8889 c42c9805 2020-11-24 stsp if (err) {
8890 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8891 c42c9805 2020-11-24 stsp return err;
8893 c42c9805 2020-11-24 stsp return NULL;
8897 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8898 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8899 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8900 c42c9805 2020-11-24 stsp goto done;
8903 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8904 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8906 c42c9805 2020-11-24 stsp goto done;
8908 c42c9805 2020-11-24 stsp *new_view = tree_view;
8910 c42c9805 2020-11-24 stsp free(commit_id);
8911 c42c9805 2020-11-24 stsp return err;
8914 07dd3ed3 2022-08-06 thomas static const struct got_error *
8915 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8917 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8918 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8919 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8921 07dd3ed3 2022-08-06 thomas g = view->gline;
8922 07dd3ed3 2022-08-06 thomas view->gline = 0;
8924 07dd3ed3 2022-08-06 thomas if (g == 0)
8926 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8927 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8929 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8930 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8931 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8932 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8933 07dd3ed3 2022-08-06 thomas return NULL;
8936 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8937 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8938 07dd3ed3 2022-08-06 thomas if (err)
8939 07dd3ed3 2022-08-06 thomas return err;
8940 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8941 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8942 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8943 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8944 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8945 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8947 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8948 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8950 07dd3ed3 2022-08-06 thomas return NULL;
8954 c42c9805 2020-11-24 stsp static const struct got_error *
8955 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8957 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8958 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8959 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8960 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8962 07dd3ed3 2022-08-06 thomas if (view->gline)
8963 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8965 6458efa5 2020-11-24 stsp switch (ch) {
8966 66a70b97 2023-02-03 thomas case '0':
8967 66a70b97 2023-02-03 thomas case '$':
8968 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8969 66a70b97 2023-02-03 thomas case 'l':
8970 66a70b97 2023-02-03 thomas case KEY_LEFT:
8971 66a70b97 2023-02-03 thomas case 'h':
8972 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8975 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8976 07b0611c 2022-06-23 thomas view->count = 0;
8978 84227eb1 2022-06-23 thomas case 'm':
8979 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8980 07b0611c 2022-06-23 thomas view->count = 0;
8982 98182bd0 2021-11-20 thomas case 'o':
8983 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8984 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8985 07b0611c 2022-06-23 thomas view->count = 0;
8986 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8987 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8988 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8989 c591440f 2021-11-20 thomas if (err)
8991 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8992 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8993 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8994 3bfadbd4 2021-11-20 thomas if (err)
8996 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8997 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8999 6458efa5 2020-11-24 stsp case KEY_ENTER:
9000 6458efa5 2020-11-24 stsp case '\r':
9001 07b0611c 2022-06-23 thomas view->count = 0;
9002 6458efa5 2020-11-24 stsp if (!s->selected_entry)
9004 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
9006 1be4947a 2022-07-22 thomas case 'T':
9007 07b0611c 2022-06-23 thomas view->count = 0;
9008 c42c9805 2020-11-24 stsp if (!s->selected_entry)
9010 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
9012 e4526bf5 2021-09-03 naddy case 'g':
9013 aa7a1117 2023-01-09 thomas case '=':
9014 e4526bf5 2021-09-03 naddy case KEY_HOME:
9015 e4526bf5 2021-09-03 naddy s->selected = 0;
9016 07b0611c 2022-06-23 thomas view->count = 0;
9017 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
9019 e4526bf5 2021-09-03 naddy case 'G':
9020 aa7a1117 2023-01-09 thomas case '*':
9021 a5d43cac 2022-07-01 thomas case KEY_END: {
9022 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
9024 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
9025 a5d43cac 2022-07-01 thomas --eos; /* border */
9026 e4526bf5 2021-09-03 naddy s->selected = 0;
9027 07b0611c 2022-06-23 thomas view->count = 0;
9028 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
9029 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
9030 e4526bf5 2021-09-03 naddy if (re == NULL)
9032 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
9033 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
9035 e4526bf5 2021-09-03 naddy if (n > 0)
9036 e4526bf5 2021-09-03 naddy s->selected = n - 1;
9040 6458efa5 2020-11-24 stsp case KEY_UP:
9041 f7140bf5 2021-10-17 thomas case CTRL('p'):
9042 6458efa5 2020-11-24 stsp if (s->selected > 0) {
9043 6458efa5 2020-11-24 stsp s->selected--;
9046 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
9047 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
9048 07b0611c 2022-06-23 thomas view->count = 0;
9050 70f17a53 2022-06-13 thomas case CTRL('u'):
9051 23427b14 2022-06-23 thomas case 'u':
9052 70f17a53 2022-06-13 thomas nscroll /= 2;
9053 70f17a53 2022-06-13 thomas /* FALL THROUGH */
9054 6458efa5 2020-11-24 stsp case KEY_PPAGE:
9055 6458efa5 2020-11-24 stsp case CTRL('b'):
9056 1c5e5faa 2022-06-23 thomas case 'b':
9057 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
9058 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
9059 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
9060 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
9061 07b0611c 2022-06-23 thomas view->count = 0;
9064 6458efa5 2020-11-24 stsp case KEY_DOWN:
9065 f7140bf5 2021-10-17 thomas case CTRL('n'):
9066 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
9067 6458efa5 2020-11-24 stsp s->selected++;
9070 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9071 6458efa5 2020-11-24 stsp /* can't scroll any further */
9072 07b0611c 2022-06-23 thomas view->count = 0;
9075 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
9077 70f17a53 2022-06-13 thomas case CTRL('d'):
9078 23427b14 2022-06-23 thomas case 'd':
9079 70f17a53 2022-06-13 thomas nscroll /= 2;
9080 70f17a53 2022-06-13 thomas /* FALL THROUGH */
9081 6458efa5 2020-11-24 stsp case KEY_NPAGE:
9082 6458efa5 2020-11-24 stsp case CTRL('f'):
9083 1c5e5faa 2022-06-23 thomas case 'f':
9084 4c2d69cb 2022-06-23 thomas case ' ':
9085 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9086 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
9087 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
9088 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
9089 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
9090 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
9091 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
9092 07b0611c 2022-06-23 thomas view->count = 0;
9095 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
9097 6458efa5 2020-11-24 stsp case CTRL('l'):
9098 07b0611c 2022-06-23 thomas view->count = 0;
9099 8924d611 2020-12-26 stsp tog_free_refs();
9100 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
9103 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
9104 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
9106 6458efa5 2020-11-24 stsp case KEY_RESIZE:
9107 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
9108 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
9111 07b0611c 2022-06-23 thomas view->count = 0;
9115 6458efa5 2020-11-24 stsp return err;
9118 6458efa5 2020-11-24 stsp __dead static void
9119 6458efa5 2020-11-24 stsp usage_ref(void)
9122 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
9123 6458efa5 2020-11-24 stsp getprogname());
9127 6458efa5 2020-11-24 stsp static const struct got_error *
9128 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
9130 1d98034b 2023-04-14 thomas const struct got_error *error;
9131 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
9132 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
9133 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
9135 6458efa5 2020-11-24 stsp struct tog_view *view;
9136 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9138 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
9139 6458efa5 2020-11-24 stsp switch (ch) {
9141 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
9142 6458efa5 2020-11-24 stsp if (repo_path == NULL)
9143 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
9147 e99e2d15 2020-11-24 naddy usage_ref();
9148 6458efa5 2020-11-24 stsp /* NOTREACHED */
9152 6458efa5 2020-11-24 stsp argc -= optind;
9153 6458efa5 2020-11-24 stsp argv += optind;
9155 6458efa5 2020-11-24 stsp if (argc > 1)
9156 e99e2d15 2020-11-24 naddy usage_ref();
9158 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9159 7cd52833 2022-06-23 thomas if (error != NULL)
9160 7cd52833 2022-06-23 thomas goto done;
9162 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
9163 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
9164 c156c7a4 2020-12-18 stsp if (cwd == NULL)
9165 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
9166 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
9167 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9168 c156c7a4 2020-12-18 stsp goto done;
9169 6458efa5 2020-11-24 stsp if (worktree)
9170 6458efa5 2020-11-24 stsp repo_path =
9171 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
9173 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
9174 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
9175 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
9176 c156c7a4 2020-12-18 stsp goto done;
9180 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9181 6458efa5 2020-11-24 stsp if (error != NULL)
9182 6458efa5 2020-11-24 stsp goto done;
9184 557d3365 2023-04-14 thomas init_curses();
9186 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
9187 51a10b52 2020-12-26 stsp if (error)
9188 51a10b52 2020-12-26 stsp goto done;
9190 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9191 6458efa5 2020-11-24 stsp if (error)
9192 6458efa5 2020-11-24 stsp goto done;
9194 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9195 6458efa5 2020-11-24 stsp if (view == NULL) {
9196 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
9197 6458efa5 2020-11-24 stsp goto done;
9200 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
9201 6458efa5 2020-11-24 stsp if (error)
9202 6458efa5 2020-11-24 stsp goto done;
9204 6458efa5 2020-11-24 stsp if (worktree) {
9205 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
9206 349dfd1e 2023-07-23 thomas if (error != NULL)
9207 349dfd1e 2023-07-23 thomas goto done;
9209 6458efa5 2020-11-24 stsp /* Release work tree lock. */
9210 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
9211 6458efa5 2020-11-24 stsp worktree = NULL;
9214 557d3365 2023-04-14 thomas error = view_loop(view);
9217 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
9218 6458efa5 2020-11-24 stsp free(repo_path);
9219 6458efa5 2020-11-24 stsp free(cwd);
9220 349dfd1e 2023-07-23 thomas if (worktree != NULL)
9221 349dfd1e 2023-07-23 thomas got_worktree_close(worktree);
9222 1d0f4054 2021-06-17 stsp if (repo) {
9223 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9224 1d0f4054 2021-06-17 stsp if (close_err)
9225 1d0f4054 2021-06-17 stsp error = close_err;
9227 7cd52833 2022-06-23 thomas if (pack_fds) {
9228 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9229 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9230 7cd52833 2022-06-23 thomas if (error == NULL)
9231 7cd52833 2022-06-23 thomas error = pack_err;
9233 51a10b52 2020-12-26 stsp tog_free_refs();
9234 6458efa5 2020-11-24 stsp return error;
9237 fc2737d5 2022-09-15 thomas static const struct got_error*
9238 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9239 fc2737d5 2022-09-15 thomas const char *str)
9241 fc2737d5 2022-09-15 thomas size_t len;
9243 fc2737d5 2022-09-15 thomas if (win == NULL)
9244 fc2737d5 2022-09-15 thomas win = stdscr;
9246 fc2737d5 2022-09-15 thomas len = strlen(str);
9247 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9249 fc2737d5 2022-09-15 thomas if (focus)
9250 fc2737d5 2022-09-15 thomas wstandout(win);
9251 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
9252 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9253 fc2737d5 2022-09-15 thomas if (focus)
9254 fc2737d5 2022-09-15 thomas wstandend(win);
9256 fc2737d5 2022-09-15 thomas return NULL;
9259 2a31b33b 2022-07-23 thomas static const struct got_error *
9260 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9262 fc2737d5 2022-09-15 thomas off_t *p;
9264 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9265 fc2737d5 2022-09-15 thomas if (p == NULL) {
9266 fc2737d5 2022-09-15 thomas free(*line_offsets);
9267 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
9268 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
9271 fc2737d5 2022-09-15 thomas *line_offsets = p;
9272 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
9273 fc2737d5 2022-09-15 thomas ++(*nlines);
9274 fc2737d5 2022-09-15 thomas return NULL;
9277 fc2737d5 2022-09-15 thomas static const struct got_error *
9278 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9280 fc2737d5 2022-09-15 thomas *ret = 0;
9282 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
9283 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9284 fc2737d5 2022-09-15 thomas size_t len = 1;
9286 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
9287 fc2737d5 2022-09-15 thomas continue;
9289 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9290 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9291 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9293 fc2737d5 2022-09-15 thomas len += strlen(t);
9294 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
9295 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
9296 fc2737d5 2022-09-15 thomas free(t0);
9297 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
9300 fc2737d5 2022-09-15 thomas return NULL;
9304 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
9305 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
9306 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
9308 fc2737d5 2022-09-15 thomas static const struct got_error *
9309 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9311 fc2737d5 2022-09-15 thomas int n, len = width;
9313 fc2737d5 2022-09-15 thomas if (km->keys) {
9314 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
9315 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9316 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
9318 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9319 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
9321 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
9323 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9324 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9325 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9327 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
9328 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
9329 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
9330 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9331 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
9332 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9333 fc2737d5 2022-09-15 thomas if (n < 0) {
9334 fc2737d5 2022-09-15 thomas free(t0);
9335 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9337 fc2737d5 2022-09-15 thomas first = 0;
9338 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
9339 fc2737d5 2022-09-15 thomas *off += n;
9341 fc2737d5 2022-09-15 thomas free(t0);
9343 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9344 fc2737d5 2022-09-15 thomas if (n < 0)
9345 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9346 fc2737d5 2022-09-15 thomas *off += n;
9348 fc2737d5 2022-09-15 thomas return NULL;
9351 fc2737d5 2022-09-15 thomas static const struct got_error *
9352 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
9354 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9355 fc2737d5 2022-09-15 thomas off_t off = 0;
9356 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
9357 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
9358 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
9359 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9360 fc2737d5 2022-09-15 thomas GENERATE_HELP
9361 fc2737d5 2022-09-15 thomas #undef KEYMAP_
9362 fc2737d5 2022-09-15 thomas #undef KEY_
9365 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9366 fc2737d5 2022-09-15 thomas if (err)
9367 fc2737d5 2022-09-15 thomas return err;
9369 fc2737d5 2022-09-15 thomas n = nitems(km);
9370 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
9371 fc2737d5 2022-09-15 thomas if (err)
9372 fc2737d5 2022-09-15 thomas return err;
9374 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
9375 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
9376 fc2737d5 2022-09-15 thomas show = s->all;
9377 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
9378 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
9379 fc2737d5 2022-09-15 thomas show = 1;
9381 fc2737d5 2022-09-15 thomas if (show) {
9382 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
9383 fc2737d5 2022-09-15 thomas if (err)
9384 fc2737d5 2022-09-15 thomas return err;
9385 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9386 fc2737d5 2022-09-15 thomas if (err)
9387 fc2737d5 2022-09-15 thomas return err;
9390 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
9392 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9393 fc2737d5 2022-09-15 thomas return err;
9396 fc2737d5 2022-09-15 thomas static const struct got_error *
9397 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
9399 fc2737d5 2022-09-15 thomas FILE *f;
9400 fc2737d5 2022-09-15 thomas const struct got_error *err;
9402 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9403 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9404 fc2737d5 2022-09-15 thomas s->nlines = 0;
9406 fc2737d5 2022-09-15 thomas f = got_opentemp();
9407 fc2737d5 2022-09-15 thomas if (f == NULL)
9408 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
9409 fc2737d5 2022-09-15 thomas s->f = f;
9411 fc2737d5 2022-09-15 thomas err = format_help(s);
9412 fc2737d5 2022-09-15 thomas if (err)
9413 fc2737d5 2022-09-15 thomas return err;
9415 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
9416 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
9418 fc2737d5 2022-09-15 thomas return NULL;
9421 fc2737d5 2022-09-15 thomas static const struct got_error *
9422 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
9424 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
9425 fc2737d5 2022-09-15 thomas return NULL;
9428 2a7d3cd7 2022-09-17 thomas static void
9429 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9430 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
9432 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
9434 2a7d3cd7 2022-09-17 thomas *f = s->f;
9435 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
9436 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
9437 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
9438 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
9439 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
9440 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
9443 fc2737d5 2022-09-15 thomas static const struct got_error *
9444 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
9446 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9447 fc2737d5 2022-09-15 thomas const struct got_error *err;
9448 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
9449 fc2737d5 2022-09-15 thomas wchar_t *wline;
9450 fc2737d5 2022-09-15 thomas char *line;
9451 fc2737d5 2022-09-15 thomas ssize_t linelen;
9452 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9453 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
9454 fc2737d5 2022-09-15 thomas int eos = view->nlines;
9456 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9457 fc2737d5 2022-09-15 thomas --eos; /* account for border */
9459 fc2737d5 2022-09-15 thomas s->lineno = 0;
9460 fc2737d5 2022-09-15 thomas rewind(s->f);
9461 fc2737d5 2022-09-15 thomas werase(view->window);
9463 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
9464 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
9466 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
9467 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
9468 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
9469 fc2737d5 2022-09-15 thomas if (err)
9470 fc2737d5 2022-09-15 thomas return err;
9471 fc2737d5 2022-09-15 thomas if (eos <= 1)
9472 fc2737d5 2022-09-15 thomas return NULL;
9473 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
9474 fc2737d5 2022-09-15 thomas eos -= 2;
9476 fc2737d5 2022-09-15 thomas s->eof = 0;
9477 fc2737d5 2022-09-15 thomas view->maxx = 0;
9478 fc2737d5 2022-09-15 thomas line = NULL;
9479 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
9480 fc2737d5 2022-09-15 thomas attr_t attr = 0;
9482 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9483 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9484 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
9485 fc2737d5 2022-09-15 thomas free(line);
9486 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
9488 fc2737d5 2022-09-15 thomas s->eof = 1;
9491 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
9492 fc2737d5 2022-09-15 thomas continue;
9493 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9494 fc2737d5 2022-09-15 thomas continue;
9495 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
9496 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
9498 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9499 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
9500 fc2737d5 2022-09-15 thomas if (err) {
9501 fc2737d5 2022-09-15 thomas free(line);
9502 fc2737d5 2022-09-15 thomas return err;
9504 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
9505 fc2737d5 2022-09-15 thomas free(wline);
9506 fc2737d5 2022-09-15 thomas wline = NULL;
9508 fc2737d5 2022-09-15 thomas if (attr)
9509 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
9510 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
9511 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9512 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
9513 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
9514 fc2737d5 2022-09-15 thomas if (err) {
9515 fc2737d5 2022-09-15 thomas free(line);
9516 fc2737d5 2022-09-15 thomas return err;
9518 fc2737d5 2022-09-15 thomas } else {
9519 fc2737d5 2022-09-15 thomas int skip;
9521 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
9522 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
9523 fc2737d5 2022-09-15 thomas if (err) {
9524 fc2737d5 2022-09-15 thomas free(line);
9525 fc2737d5 2022-09-15 thomas return err;
9527 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
9528 fc2737d5 2022-09-15 thomas free(wline);
9529 fc2737d5 2022-09-15 thomas wline = NULL;
9531 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
9532 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
9533 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
9534 fc2737d5 2022-09-15 thomas } else {
9535 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
9536 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
9538 fc2737d5 2022-09-15 thomas if (attr)
9539 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
9540 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
9541 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
9543 fc2737d5 2022-09-15 thomas free(line);
9544 fc2737d5 2022-09-15 thomas if (nprinted > 0)
9545 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9547 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
9549 fc2737d5 2022-09-15 thomas view_border(view);
9551 fc2737d5 2022-09-15 thomas if (s->eof) {
9552 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
9553 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
9554 fc2737d5 2022-09-15 thomas view->ncols - 1);
9555 fc2737d5 2022-09-15 thomas if (rc == ERR)
9556 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9557 fc2737d5 2022-09-15 thomas } else {
9558 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
9559 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
9560 fc2737d5 2022-09-15 thomas wstandout(view->window);
9561 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
9562 fc2737d5 2022-09-15 thomas view->ncols - 1);
9563 fc2737d5 2022-09-15 thomas if (rc == ERR)
9564 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9565 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
9566 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
9567 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
9568 fc2737d5 2022-09-15 thomas if (rc == ERR)
9569 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
9571 fc2737d5 2022-09-15 thomas wstandend(view->window);
9574 fc2737d5 2022-09-15 thomas return NULL;
9577 fc2737d5 2022-09-15 thomas static const struct got_error *
9578 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9580 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9581 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9582 fc2737d5 2022-09-15 thomas char *line = NULL;
9583 fc2737d5 2022-09-15 thomas ssize_t linelen;
9584 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9585 fc2737d5 2022-09-15 thomas int eos, nscroll;
9587 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
9588 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9589 fc2737d5 2022-09-15 thomas --eos; /* border */
9591 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
9593 fc2737d5 2022-09-15 thomas switch (ch) {
9594 fc2737d5 2022-09-15 thomas case '0':
9595 fc2737d5 2022-09-15 thomas case '$':
9596 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
9597 fc2737d5 2022-09-15 thomas case 'l':
9598 fc2737d5 2022-09-15 thomas case KEY_LEFT:
9599 fc2737d5 2022-09-15 thomas case 'h':
9600 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
9602 fc2737d5 2022-09-15 thomas case 'g':
9603 fc2737d5 2022-09-15 thomas case KEY_HOME:
9604 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9605 fc2737d5 2022-09-15 thomas view->count = 0;
9607 fc2737d5 2022-09-15 thomas case 'G':
9608 fc2737d5 2022-09-15 thomas case KEY_END:
9609 fc2737d5 2022-09-15 thomas view->count = 0;
9610 fc2737d5 2022-09-15 thomas if (s->eof)
9612 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
9613 fc2737d5 2022-09-15 thomas s->eof = 1;
9615 fc2737d5 2022-09-15 thomas case 'k':
9616 fc2737d5 2022-09-15 thomas case KEY_UP:
9617 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
9618 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
9620 fc2737d5 2022-09-15 thomas view->count = 0;
9622 fc2737d5 2022-09-15 thomas case CTRL('u'):
9623 fc2737d5 2022-09-15 thomas case 'u':
9624 fc2737d5 2022-09-15 thomas nscroll /= 2;
9625 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9626 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
9627 fc2737d5 2022-09-15 thomas case CTRL('b'):
9628 fc2737d5 2022-09-15 thomas case 'b':
9629 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
9630 fc2737d5 2022-09-15 thomas view->count = 0;
9633 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
9634 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
9636 fc2737d5 2022-09-15 thomas case 'j':
9637 fc2737d5 2022-09-15 thomas case KEY_DOWN:
9638 fc2737d5 2022-09-15 thomas case CTRL('n'):
9639 fc2737d5 2022-09-15 thomas if (!s->eof)
9640 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9642 fc2737d5 2022-09-15 thomas view->count = 0;
9644 fc2737d5 2022-09-15 thomas case CTRL('d'):
9645 fc2737d5 2022-09-15 thomas case 'd':
9646 fc2737d5 2022-09-15 thomas nscroll /= 2;
9647 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9648 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9649 fc2737d5 2022-09-15 thomas case CTRL('f'):
9650 fc2737d5 2022-09-15 thomas case 'f':
9651 fc2737d5 2022-09-15 thomas case ' ':
9652 fc2737d5 2022-09-15 thomas if (s->eof) {
9653 fc2737d5 2022-09-15 thomas view->count = 0;
9656 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9657 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9658 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9659 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9660 fc2737d5 2022-09-15 thomas if (feof(s->f))
9661 fc2737d5 2022-09-15 thomas s->eof = 1;
9663 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9667 fc2737d5 2022-09-15 thomas free(line);
9669 fc2737d5 2022-09-15 thomas default:
9670 fc2737d5 2022-09-15 thomas view->count = 0;
9674 fc2737d5 2022-09-15 thomas return err;
9677 fc2737d5 2022-09-15 thomas static const struct got_error *
9678 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9680 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9682 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9683 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9684 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9685 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9687 fc2737d5 2022-09-15 thomas return NULL;
9690 fc2737d5 2022-09-15 thomas static const struct got_error *
9691 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9693 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9696 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9697 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9699 fc2737d5 2022-09-15 thomas wclear(view->window);
9700 fc2737d5 2022-09-15 thomas view->count = 0;
9701 fc2737d5 2022-09-15 thomas view->x = 0;
9702 fc2737d5 2022-09-15 thomas s->all = !s->all;
9703 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9704 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9705 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9707 fc2737d5 2022-09-15 thomas return create_help(s);
9710 fc2737d5 2022-09-15 thomas static const struct got_error *
9711 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9713 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9714 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9716 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9717 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9718 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9719 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9721 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9722 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9723 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9724 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9725 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9726 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9727 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9729 fc2737d5 2022-09-15 thomas err = create_help(s);
9730 fc2737d5 2022-09-15 thomas return err;
9733 fc2737d5 2022-09-15 thomas static const struct got_error *
9734 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9735 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9737 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9739 2a31b33b 2022-07-23 thomas *new_view = NULL;
9741 2a31b33b 2022-07-23 thomas switch (request) {
9742 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9743 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9744 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9746 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9747 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9748 2a31b33b 2022-07-23 thomas view, s->repo);
9750 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9751 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9753 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9754 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9755 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9757 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9758 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9759 2a31b33b 2022-07-23 thomas s->repo);
9761 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9762 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9764 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9765 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9766 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9767 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9768 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9769 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9770 2a31b33b 2022-07-23 thomas &view->state.tree);
9771 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9772 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9773 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9774 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9776 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9777 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9779 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9780 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9781 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9782 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9783 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9784 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9785 2a31b33b 2022-07-23 thomas view->state.log.repo);
9786 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9787 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9788 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9789 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9791 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9792 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9794 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9795 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9796 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9797 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9798 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9799 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9800 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9801 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9803 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9804 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9805 2a31b33b 2022-07-23 thomas if (err)
9806 2a31b33b 2022-07-23 thomas view_close(*new_view);
9808 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9809 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9810 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9811 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9812 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9813 fc2737d5 2022-09-15 thomas if (err)
9814 fc2737d5 2022-09-15 thomas view_close(*new_view);
9816 2a31b33b 2022-07-23 thomas default:
9817 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9820 2a31b33b 2022-07-23 thomas return err;
9824 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9825 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9827 6458efa5 2020-11-24 stsp static void
9828 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9830 a5d43cac 2022-07-01 thomas switch (view->type) {
9831 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9832 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9833 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9834 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9838 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9839 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9841 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9842 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9845 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9846 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9847 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9849 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9850 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9851 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9853 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9854 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9855 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9857 a5d43cac 2022-07-01 thomas default:
9861 a5d43cac 2022-07-01 thomas view->offset = 0;
9865 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9866 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9868 a5d43cac 2022-07-01 thomas static const struct got_error *
9869 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9871 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9872 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9873 a5d43cac 2022-07-01 thomas int *selected = NULL;
9874 a5d43cac 2022-07-01 thomas int header, offset;
9876 a5d43cac 2022-07-01 thomas switch (view->type) {
9877 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9878 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9879 a5d43cac 2022-07-01 thomas header = 3;
9880 a5d43cac 2022-07-01 thomas scrolld = NULL;
9881 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9882 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9883 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9884 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9885 a5d43cac 2022-07-01 thomas view->offset = offset;
9889 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9890 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9891 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9892 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9893 a5d43cac 2022-07-01 thomas selected = &s->selected;
9896 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9897 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9898 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9899 a5d43cac 2022-07-01 thomas header = 3;
9900 a5d43cac 2022-07-01 thomas selected = &s->selected;
9903 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9904 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9905 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9906 a5d43cac 2022-07-01 thomas header = 5;
9907 a5d43cac 2022-07-01 thomas selected = &s->selected;
9910 a5d43cac 2022-07-01 thomas default:
9911 a5d43cac 2022-07-01 thomas selected = NULL;
9912 a5d43cac 2022-07-01 thomas scrolld = NULL;
9913 a5d43cac 2022-07-01 thomas header = 0;
9917 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9918 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9919 a5d43cac 2022-07-01 thomas view->offset = offset;
9920 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9921 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9922 a5d43cac 2022-07-01 thomas *selected -= offset;
9926 a5d43cac 2022-07-01 thomas return err;
9929 a5d43cac 2022-07-01 thomas static void
9930 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9934 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9935 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9936 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9937 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9939 6879ba42 2020-10-01 naddy fputc('\n', fp);
9942 4ed7e80c 2018-05-20 stsp __dead static void
9943 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9945 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9947 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9948 6879ba42 2020-10-01 naddy getprogname());
9949 6879ba42 2020-10-01 naddy if (hflag) {
9950 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9951 6879ba42 2020-10-01 naddy list_commands(fp);
9953 6879ba42 2020-10-01 naddy exit(status);
9956 c2301be8 2018-04-30 stsp static char **
9957 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9959 ee85c5e8 2020-02-29 stsp va_list ap;
9960 c2301be8 2018-04-30 stsp char **argv;
9963 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9965 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9966 c2301be8 2018-04-30 stsp if (argv == NULL)
9967 c2301be8 2018-04-30 stsp err(1, "calloc");
9968 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9969 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9970 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9971 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9974 ee85c5e8 2020-02-29 stsp va_end(ap);
9975 c2301be8 2018-04-30 stsp return argv;
9979 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9980 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9981 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9982 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9983 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9985 ee85c5e8 2020-02-29 stsp static const struct got_error *
9986 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9988 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9989 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9990 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9991 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9992 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9993 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9994 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9995 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9996 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9998 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9999 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
10000 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
10002 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
10003 7cd52833 2022-06-23 thomas if (error != NULL)
10004 7cd52833 2022-06-23 thomas goto done;
10006 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
10007 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
10008 ee85c5e8 2020-02-29 stsp goto done;
10010 ee85c5e8 2020-02-29 stsp if (worktree)
10011 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
10013 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
10014 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
10015 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
10016 ee85c5e8 2020-02-29 stsp goto done;
10019 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
10020 ee85c5e8 2020-02-29 stsp if (error != NULL)
10021 ee85c5e8 2020-02-29 stsp goto done;
10023 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
10024 ee85c5e8 2020-02-29 stsp repo, worktree);
10025 ee85c5e8 2020-02-29 stsp if (error)
10026 ee85c5e8 2020-02-29 stsp goto done;
10028 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
10029 84de9106 2020-12-26 stsp if (error)
10030 84de9106 2020-12-26 stsp goto done;
10031 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
10032 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
10033 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
10034 ee85c5e8 2020-02-29 stsp if (error)
10035 ee85c5e8 2020-02-29 stsp goto done;
10037 ee85c5e8 2020-02-29 stsp if (worktree) {
10038 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
10039 ee85c5e8 2020-02-29 stsp worktree = NULL;
10042 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
10043 945f9229 2022-04-16 thomas if (error)
10044 945f9229 2022-04-16 thomas goto done;
10046 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
10047 ee85c5e8 2020-02-29 stsp if (error) {
10048 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
10049 ee85c5e8 2020-02-29 stsp goto done;
10050 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
10051 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
10052 6879ba42 2020-10-01 naddy usage(1, 1);
10053 ee85c5e8 2020-02-29 stsp /* not reached */
10056 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
10057 ee85c5e8 2020-02-29 stsp if (error)
10058 ee85c5e8 2020-02-29 stsp goto done;
10060 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
10061 ee85c5e8 2020-02-29 stsp argc = 4;
10062 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
10063 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
10065 1d0f4054 2021-06-17 stsp if (repo) {
10066 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
10067 1d0f4054 2021-06-17 stsp if (error == NULL)
10068 1d0f4054 2021-06-17 stsp error = close_err;
10070 945f9229 2022-04-16 thomas if (commit)
10071 945f9229 2022-04-16 thomas got_object_commit_close(commit);
10072 ee85c5e8 2020-02-29 stsp if (worktree)
10073 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
10074 7cd52833 2022-06-23 thomas if (pack_fds) {
10075 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
10076 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
10077 7cd52833 2022-06-23 thomas if (error == NULL)
10078 7cd52833 2022-06-23 thomas error = pack_err;
10080 ee85c5e8 2020-02-29 stsp free(id);
10081 ee85c5e8 2020-02-29 stsp free(commit_id_str);
10082 ee85c5e8 2020-02-29 stsp free(commit_id);
10083 ee85c5e8 2020-02-29 stsp free(cwd);
10084 ee85c5e8 2020-02-29 stsp free(repo_path);
10085 ee85c5e8 2020-02-29 stsp free(in_repo_path);
10086 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
10088 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
10089 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
10090 ee85c5e8 2020-02-29 stsp free(cmd_argv);
10092 87670572 2020-12-26 naddy tog_free_refs();
10093 ee85c5e8 2020-02-29 stsp return error;
10097 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
10099 1d98034b 2023-04-14 thomas const struct got_error *io_err, *error = NULL;
10100 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
10101 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
10102 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
10103 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
10104 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
10105 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
10107 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
10108 557d3365 2023-04-14 thomas const char *test_script_path;
10110 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
10113 e2a79cf9 2023-07-26 thomas * Override default signal handlers before starting ncurses.
10114 e2a79cf9 2023-07-26 thomas * This should prevent ncurses from installing its own
10115 e2a79cf9 2023-07-26 thomas * broken cleanup() signal handler.
10117 e2a79cf9 2023-07-26 thomas signal(SIGWINCH, tog_sigwinch);
10118 e2a79cf9 2023-07-26 thomas signal(SIGPIPE, tog_sigpipe);
10119 e2a79cf9 2023-07-26 thomas signal(SIGCONT, tog_sigcont);
10120 e2a79cf9 2023-07-26 thomas signal(SIGINT, tog_sigint);
10121 e2a79cf9 2023-07-26 thomas signal(SIGTERM, tog_sigterm);
10124 557d3365 2023-04-14 thomas * Test mode init must happen before pledge() because "tty" will
10125 557d3365 2023-04-14 thomas * not allow TTY-related ioctls to occur via regular files.
10127 fa9bb690 2023-04-22 thomas test_script_path = getenv("TOG_TEST_SCRIPT");
10128 557d3365 2023-04-14 thomas if (test_script_path != NULL) {
10129 557d3365 2023-04-14 thomas error = init_mock_term(test_script_path);
10130 557d3365 2023-04-14 thomas if (error) {
10131 557d3365 2023-04-14 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10132 557d3365 2023-04-14 thomas return 1;
10134 5ffbe221 2023-04-22 thomas } else if (!isatty(STDIN_FILENO))
10135 5ffbe221 2023-04-22 thomas errx(1, "standard input is not a tty");
10137 557d3365 2023-04-14 thomas #if !defined(PROFILE)
10138 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
10139 a0abeae5 2023-02-17 thomas NULL) == -1)
10140 a0abeae5 2023-02-17 thomas err(1, "pledge");
10143 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
10144 9f7d7167 2018-04-29 stsp switch (ch) {
10145 9f7d7167 2018-04-29 stsp case 'h':
10146 9f7d7167 2018-04-29 stsp hflag = 1;
10148 53ccebc2 2019-07-30 stsp case 'V':
10149 53ccebc2 2019-07-30 stsp Vflag = 1;
10152 6879ba42 2020-10-01 naddy usage(hflag, 1);
10153 9f7d7167 2018-04-29 stsp /* NOTREACHED */
10157 9f7d7167 2018-04-29 stsp argc -= optind;
10158 9f7d7167 2018-04-29 stsp argv += optind;
10159 9814e6a3 2020-09-27 naddy optind = 1;
10160 c2301be8 2018-04-30 stsp optreset = 1;
10162 53ccebc2 2019-07-30 stsp if (Vflag) {
10163 53ccebc2 2019-07-30 stsp got_version_print_str();
10164 6879ba42 2020-10-01 naddy return 0;
10167 c2301be8 2018-04-30 stsp if (argc == 0) {
10168 f29d3e89 2018-06-23 stsp if (hflag)
10169 6879ba42 2020-10-01 naddy usage(hflag, 0);
10170 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
10171 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
10172 c2301be8 2018-04-30 stsp argc = 1;
10173 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
10175 6059809a 2020-12-17 stsp size_t i;
10177 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
10178 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
10179 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
10180 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
10181 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
10187 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
10188 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
10189 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
10190 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10191 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
10192 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10195 92845f09 2023-07-26 thomas tog_base_commit.idx = -1;
10196 92845f09 2023-07-26 thomas tog_base_commit.marker = GOT_WORKTREE_STATE_UNKNOWN;
10198 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
10199 ee85c5e8 2020-02-29 stsp if (argc != 1)
10200 6879ba42 2020-10-01 naddy usage(0, 1);
10201 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
10202 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
10204 ee85c5e8 2020-02-29 stsp if (hflag)
10205 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
10207 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10210 1d98034b 2023-04-14 thomas if (using_mock_io) {
10211 1d98034b 2023-04-14 thomas io_err = tog_io_close();
10212 1d98034b 2023-04-14 thomas if (error == NULL)
10213 1d98034b 2023-04-14 thomas error = io_err;
10215 9f7d7167 2018-04-29 stsp endwin();
10216 a2f4a359 2020-02-28 stsp if (cmd_argv) {
10218 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
10219 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
10220 a2f4a359 2020-02-28 stsp free(cmd_argv);
10223 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
10224 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
10225 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
10226 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
10227 66b04f8f 2023-07-19 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10228 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10229 66b04f8f 2023-07-19 thomas return 1;
10231 9f7d7167 2018-04-29 stsp return 0;