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"
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 fc2737d5 2022-09-15 thomas TOG_VIEW_HELP
114 fc2737d5 2022-09-15 thomas /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 fc2737d5 2022-09-15 thomas enum tog_keymap_type {
116 fc2737d5 2022-09-15 thomas TOG_KEYMAP_KEYS = -2,
117 fc2737d5 2022-09-15 thomas TOG_KEYMAP_GLOBAL,
118 fc2737d5 2022-09-15 thomas TOG_KEYMAP_DIFF,
119 fc2737d5 2022-09-15 thomas TOG_KEYMAP_LOG,
120 fc2737d5 2022-09-15 thomas TOG_KEYMAP_BLAME,
121 fc2737d5 2022-09-15 thomas TOG_KEYMAP_TREE,
122 fc2737d5 2022-09-15 thomas TOG_KEYMAP_REF,
123 fc2737d5 2022-09-15 thomas TOG_KEYMAP_HELP
126 a5d43cac 2022-07-01 thomas enum tog_view_mode {
127 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
128 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
129 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
132 87a675e0 2023-04-22 thomas #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
134 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
136 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
137 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
138 ba4f502b 2018-08-04 stsp struct got_object_id *id;
139 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
142 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 ba4f502b 2018-08-04 stsp struct commit_queue {
144 ba4f502b 2018-08-04 stsp int ncommits;
145 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
148 f26dddb7 2019-11-08 stsp struct tog_color {
149 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
150 6d17833f 2019-11-08 stsp regex_t regex;
151 6d17833f 2019-11-08 stsp short colorpair;
153 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
155 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
157 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
159 2183bbf6 2022-01-23 thomas static const struct got_error *
160 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 2183bbf6 2022-01-23 thomas struct got_reference* re2)
163 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
164 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
165 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
167 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
168 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
171 2183bbf6 2022-01-23 thomas *cmp = -1;
172 2183bbf6 2022-01-23 thomas return NULL;
173 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
174 2183bbf6 2022-01-23 thomas *cmp = 1;
175 2183bbf6 2022-01-23 thomas return NULL;
178 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
179 2183bbf6 2022-01-23 thomas return NULL;
182 11b20872 2019-11-08 stsp static const struct got_error *
183 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
185 51a10b52 2020-12-26 stsp const struct got_error *err;
187 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
191 51a10b52 2020-12-26 stsp return err;
193 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
197 51a10b52 2020-12-26 stsp static void
198 51a10b52 2020-12-26 stsp tog_free_refs(void)
200 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
201 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
202 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
204 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
207 51a10b52 2020-12-26 stsp static const struct got_error *
208 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
209 11b20872 2019-11-08 stsp int idx, short color)
211 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
212 11b20872 2019-11-08 stsp struct tog_color *tc;
213 11b20872 2019-11-08 stsp int regerr = 0;
215 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
216 11b20872 2019-11-08 stsp return NULL;
218 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
220 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
221 11b20872 2019-11-08 stsp if (tc == NULL)
222 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
223 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
224 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
225 11b20872 2019-11-08 stsp if (regerr) {
226 11b20872 2019-11-08 stsp static char regerr_msg[512];
227 11b20872 2019-11-08 stsp static char err_msg[512];
228 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
229 11b20872 2019-11-08 stsp sizeof(regerr_msg));
230 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
231 11b20872 2019-11-08 stsp regerr_msg);
232 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
234 11b20872 2019-11-08 stsp return err;
236 11b20872 2019-11-08 stsp tc->colorpair = idx;
237 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
238 11b20872 2019-11-08 stsp return NULL;
241 11b20872 2019-11-08 stsp static void
242 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
244 11b20872 2019-11-08 stsp struct tog_color *tc;
246 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
247 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
248 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
249 11b20872 2019-11-08 stsp regfree(&tc->regex);
254 ef20f542 2022-06-26 thomas static struct tog_color *
255 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
257 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
259 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
260 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
264 11b20872 2019-11-08 stsp return NULL;
268 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
273 11b20872 2019-11-08 stsp return COLOR_CYAN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
275 11b20872 2019-11-08 stsp return COLOR_YELLOW;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
277 11b20872 2019-11-08 stsp return COLOR_GREEN;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
283 91b8c405 2020-01-25 stsp return COLOR_CYAN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
285 11b20872 2019-11-08 stsp return COLOR_GREEN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
287 11b20872 2019-11-08 stsp return COLOR_GREEN;
288 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
289 11b20872 2019-11-08 stsp return COLOR_CYAN;
290 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
291 11b20872 2019-11-08 stsp return COLOR_YELLOW;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_GREEN;
294 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
296 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
297 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
298 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
299 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
305 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
307 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
309 11b20872 2019-11-08 stsp if (val == NULL)
310 11b20872 2019-11-08 stsp return default_color_value(envvar);
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
313 11b20872 2019-11-08 stsp return COLOR_BLACK;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
315 11b20872 2019-11-08 stsp return COLOR_RED;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
317 11b20872 2019-11-08 stsp return COLOR_GREEN;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
319 11b20872 2019-11-08 stsp return COLOR_YELLOW;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
321 11b20872 2019-11-08 stsp return COLOR_BLUE;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
323 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
325 11b20872 2019-11-08 stsp return COLOR_CYAN;
326 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
327 11b20872 2019-11-08 stsp return COLOR_WHITE;
328 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
331 11b20872 2019-11-08 stsp return default_color_value(envvar);
334 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
335 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
336 3dbaef42 2020-11-24 stsp const char *label1, *label2;
337 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
338 19a6a6b5 2022-07-01 thomas int fd1, fd2;
339 82c78e96 2022-08-06 thomas int lineno;
340 15a087fe 2019-02-21 stsp int first_displayed_line;
341 15a087fe 2019-02-21 stsp int last_displayed_line;
343 15a087fe 2019-02-21 stsp int diff_context;
344 3dbaef42 2020-11-24 stsp int ignore_whitespace;
345 64453f7e 2020-11-21 stsp int force_text_diff;
346 15a087fe 2019-02-21 stsp struct got_repository *repo;
347 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
348 fe621944 2020-11-10 stsp size_t nlines;
349 f44b1f58 2020-02-02 tracey int matched_line;
350 f44b1f58 2020-02-02 tracey int selected_line;
352 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
353 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
356 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
359 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
360 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
361 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
362 1a76625f 2018-10-22 stsp int commits_needed;
363 fb280deb 2021-08-30 stsp int load_all;
364 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
365 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
366 1a76625f 2018-10-22 stsp const char *in_repo_path;
367 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp struct got_repository *repo;
369 1af5eddf 2022-06-23 thomas int *pack_fds;
370 1a76625f 2018-10-22 stsp int log_complete;
371 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
372 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
373 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
374 13add988 2019-10-15 stsp int *searching;
375 13add988 2019-10-15 stsp int *search_next_done;
376 13add988 2019-10-15 stsp regex_t *regex;
377 7e8004ba 2022-09-11 thomas int *limiting;
378 7e8004ba 2022-09-11 thomas int limit_match;
379 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
380 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
383 1a76625f 2018-10-22 stsp struct tog_log_view_state {
384 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
385 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
386 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
387 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
388 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
389 b01e7d3b 2018-08-04 stsp int selected;
390 b01e7d3b 2018-08-04 stsp char *in_repo_path;
391 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
392 b672a97a 2020-01-27 stsp int log_branches;
393 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
394 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
395 1a76625f 2018-10-22 stsp sig_atomic_t quit;
396 1a76625f 2018-10-22 stsp pthread_t thread;
397 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
398 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
399 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
400 11b20872 2019-11-08 stsp struct tog_colors colors;
401 f69c5a46 2022-07-19 thomas int use_committer;
402 7e8004ba 2022-09-11 thomas int limit_view;
403 7e8004ba 2022-09-11 thomas regex_t limit_regex;
404 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
407 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
408 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
409 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
411 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
412 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
413 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
414 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
415 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
416 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
417 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
418 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
419 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
420 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
421 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
425 e9424729 2018-08-04 stsp int nlines;
427 e9424729 2018-08-04 stsp struct tog_view *view;
428 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
432 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
433 e9424729 2018-08-04 stsp const char *path;
434 e9424729 2018-08-04 stsp struct got_repository *repo;
435 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
436 e9424729 2018-08-04 stsp int *complete;
437 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
438 fc06ba56 2019-08-22 stsp void *cancel_arg;
439 1134ebde 2023-04-22 thomas pthread_cond_t blame_complete;
442 e9424729 2018-08-04 stsp struct tog_blame {
444 be659d10 2020-11-18 stsp off_t filesize;
445 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
446 6fcac457 2018-11-19 stsp int nlines;
447 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
448 e9424729 2018-08-04 stsp pthread_t thread;
449 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
450 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
451 e9424729 2018-08-04 stsp const char *path;
452 7cd52833 2022-06-23 thomas int *pack_fds;
455 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
456 7cbe629d 2018-08-04 stsp int first_displayed_line;
457 7cbe629d 2018-08-04 stsp int last_displayed_line;
458 7cbe629d 2018-08-04 stsp int selected_line;
459 4fc71f3b 2022-07-12 thomas int last_diffed_line;
460 7cbe629d 2018-08-04 stsp int blame_complete;
463 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
464 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
465 e5a0f69f 2018-08-18 stsp char *path;
466 7cbe629d 2018-08-04 stsp struct got_repository *repo;
467 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
468 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
469 e9424729 2018-08-04 stsp struct tog_blame blame;
470 6c4c42e0 2019-06-24 stsp int matched_line;
471 11b20872 2019-11-08 stsp struct tog_colors colors;
474 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
475 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
476 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
478 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
479 ad80ab7b 2018-08-04 stsp int selected;
482 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
484 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
485 ad80ab7b 2018-08-04 stsp char *tree_label;
486 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
487 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
488 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
489 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
490 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
491 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
492 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
493 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
494 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
495 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
496 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
497 6458efa5 2020-11-24 stsp struct tog_colors colors;
500 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
501 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
502 6458efa5 2020-11-24 stsp struct got_reference *ref;
506 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
508 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
509 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
512 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
513 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
514 6458efa5 2020-11-24 stsp struct got_repository *repo;
515 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
516 bddb1296 2019-11-08 stsp struct tog_colors colors;
519 fc2737d5 2022-09-15 thomas struct tog_help_view_state {
521 fc2737d5 2022-09-15 thomas off_t *line_offsets;
522 fc2737d5 2022-09-15 thomas size_t nlines;
523 fc2737d5 2022-09-15 thomas int lineno;
524 fc2737d5 2022-09-15 thomas int first_displayed_line;
525 fc2737d5 2022-09-15 thomas int last_displayed_line;
527 fc2737d5 2022-09-15 thomas int matched_line;
528 fc2737d5 2022-09-15 thomas int selected_line;
530 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
533 fc2737d5 2022-09-15 thomas #define GENERATE_HELP \
534 fc2737d5 2022-09-15 thomas KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
535 fc2737d5 2022-09-15 thomas KEY_("H F1", "Open view-specific help (double tap for all help)"), \
536 fc2737d5 2022-09-15 thomas KEY_("k C-p Up", "Move cursor or page up one line"), \
537 fc2737d5 2022-09-15 thomas KEY_("j C-n Down", "Move cursor or page down one line"), \
538 fc2737d5 2022-09-15 thomas KEY_("C-b b PgUp", "Scroll the view up one page"), \
539 fc2737d5 2022-09-15 thomas KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
540 fc2737d5 2022-09-15 thomas KEY_("C-u u", "Scroll the view up one half page"), \
541 fc2737d5 2022-09-15 thomas KEY_("C-d d", "Scroll the view down one half page"), \
542 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
543 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
544 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
545 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
546 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
547 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
548 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
549 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
550 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
551 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
552 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
553 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
554 c282f787 2023-04-22 thomas KEY_("S", "Switch split-screen layout"), \
555 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
556 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
557 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
558 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
559 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
561 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
562 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
563 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
564 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
565 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
566 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
567 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
568 fc2737d5 2022-09-15 thomas " commit"), \
569 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
570 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
571 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
572 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
574 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
575 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
576 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
577 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
578 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
579 fc2737d5 2022-09-15 thomas " data"), \
580 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
581 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
582 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
583 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
584 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
585 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
586 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
588 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
589 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
590 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
591 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
592 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
593 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
594 fc2737d5 2022-09-15 thomas " selected line's commit"), \
595 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
596 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
598 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
599 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
600 fc2737d5 2022-09-15 thomas " selected file"), \
601 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
602 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
603 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
604 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
606 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
607 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
608 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
609 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
610 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
611 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
612 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
614 fc2737d5 2022-09-15 thomas struct tog_key_map {
615 fc2737d5 2022-09-15 thomas const char *keys;
616 fc2737d5 2022-09-15 thomas const char *info;
617 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
620 b85a3496 2023-04-14 thomas /* curses io for tog regress */
621 b85a3496 2023-04-14 thomas struct tog_io {
622 b85a3496 2023-04-14 thomas FILE *cin;
623 b85a3496 2023-04-14 thomas FILE *cout;
625 5b3a801d 2023-04-22 thomas FILE *sdump;
626 8e778ade 2023-04-22 thomas int wait_for_ui;
627 557d3365 2023-04-14 thomas } tog_io;
628 557d3365 2023-04-14 thomas static int using_mock_io;
630 13bc6832 2023-04-22 thomas #define TOG_KEY_SCRDUMP SHRT_MIN
633 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
635 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
636 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
637 669b5ffa 2018-10-07 stsp * there is enough screen estate.
639 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
640 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
642 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
643 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
644 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
646 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
647 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
649 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
651 cc3c9aac 2018-08-01 stsp struct tog_view {
652 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
653 26ed57b2 2018-05-19 stsp WINDOW *window;
654 26ed57b2 2018-05-19 stsp PANEL *panel;
655 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
656 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
657 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
658 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
659 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
660 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
661 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
662 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
663 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
665 669b5ffa 2018-10-07 stsp struct tog_view *parent;
666 669b5ffa 2018-10-07 stsp struct tog_view *child;
669 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
670 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
671 e78dc838 2020-12-04 stsp * between parent and child.
672 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
673 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
674 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
675 e78dc838 2020-12-04 stsp * situations.
677 e78dc838 2020-12-04 stsp int focus_child;
679 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
680 5dc9f4bc 2018-08-04 stsp /* type-specific state */
681 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
683 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
684 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
685 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
686 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
687 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
688 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
691 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
692 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
693 e78dc838 2020-12-04 stsp struct tog_view *, int);
694 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
695 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
696 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
698 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
699 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
700 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
701 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
702 c0c4acc8 2021-01-24 stsp int search_started;
703 60493ae3 2019-06-20 stsp int searching;
704 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
705 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
706 60493ae3 2019-06-20 stsp int search_next_done;
707 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
708 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
709 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
710 1803e47f 2019-06-22 stsp regex_t regex;
711 41605754 2020-11-12 stsp regmatch_t regmatch;
712 f2d06bef 2023-01-23 thomas const char *action;
715 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
716 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
717 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
718 78756c87 2020-11-24 stsp struct got_repository *);
719 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
720 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
721 e78dc838 2020-12-04 stsp struct tog_view *, int);
722 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
723 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
724 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
725 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
726 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
727 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
729 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
730 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
731 78756c87 2020-11-24 stsp const char *, const char *, int);
732 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
733 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
734 e78dc838 2020-12-04 stsp struct tog_view *, int);
735 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
736 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
737 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
738 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
740 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
741 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
742 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
743 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
744 e78dc838 2020-12-04 stsp struct tog_view *, int);
745 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
746 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
747 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
748 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
749 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
751 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
752 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
753 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
754 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
755 e78dc838 2020-12-04 stsp struct tog_view *, int);
756 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
757 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
758 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
760 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
761 6458efa5 2020-11-24 stsp struct got_repository *);
762 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
763 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
764 e78dc838 2020-12-04 stsp struct tog_view *, int);
765 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
766 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
767 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
769 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
770 2a7d3cd7 2022-09-17 thomas struct tog_view *);
771 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
772 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
773 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
774 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
775 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
776 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
777 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
778 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
780 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
781 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
782 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
783 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
784 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
786 25791caa 2018-10-24 stsp static void
787 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
789 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
792 83baff54 2019-08-12 stsp static void
793 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
795 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
798 61266923 2020-01-14 stsp static void
799 61266923 2020-01-14 stsp tog_sigcont(int signo)
801 61266923 2020-01-14 stsp tog_sigcont_received = 1;
804 296152d1 2022-05-31 thomas static void
805 296152d1 2022-05-31 thomas tog_sigint(int signo)
807 296152d1 2022-05-31 thomas tog_sigint_received = 1;
810 296152d1 2022-05-31 thomas static void
811 296152d1 2022-05-31 thomas tog_sigterm(int signo)
813 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
816 296152d1 2022-05-31 thomas static int
817 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
819 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
820 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
823 e5a0f69f 2018-08-18 stsp static const struct got_error *
824 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
826 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
828 669b5ffa 2018-10-07 stsp if (view->child) {
829 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
830 669b5ffa 2018-10-07 stsp view->child = NULL;
832 e5a0f69f 2018-08-18 stsp if (view->close)
833 e5a0f69f 2018-08-18 stsp err = view->close(view);
834 ea5e7bb5 2018-08-01 stsp if (view->panel)
835 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
836 ea5e7bb5 2018-08-01 stsp if (view->window)
837 ea5e7bb5 2018-08-01 stsp delwin(view->window);
838 ea5e7bb5 2018-08-01 stsp free(view);
839 f2d749db 2022-07-12 thomas return err ? err : child_err;
842 ea5e7bb5 2018-08-01 stsp static struct tog_view *
843 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
844 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
846 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
848 ea5e7bb5 2018-08-01 stsp if (view == NULL)
849 ea5e7bb5 2018-08-01 stsp return NULL;
851 d6b05b5b 2018-08-04 stsp view->type = type;
852 f7d12f7e 2018-08-01 stsp view->lines = LINES;
853 f7d12f7e 2018-08-01 stsp view->cols = COLS;
854 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
855 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
856 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
857 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
858 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
859 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
860 96a765a8 2018-08-04 stsp view_close(view);
861 ea5e7bb5 2018-08-01 stsp return NULL;
863 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
864 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
865 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
866 96a765a8 2018-08-04 stsp view_close(view);
867 ea5e7bb5 2018-08-01 stsp return NULL;
870 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
871 ea5e7bb5 2018-08-01 stsp return view;
875 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
877 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
879 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
882 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
883 a5d43cac 2022-07-01 thomas static int
884 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
886 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
889 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
891 5c60c32a 2018-10-18 stsp static const struct got_error *
892 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
894 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
896 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
897 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
898 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
900 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
901 a5d43cac 2022-07-01 thomas view->begin_x = 0;
902 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
903 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
904 53d2bdd3 2022-07-10 thomas view->cols > 119)
905 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
907 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
908 a5d43cac 2022-07-01 thomas view->begin_y = 0;
910 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
911 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
912 5c60c32a 2018-10-18 stsp view->lines = LINES;
913 5c60c32a 2018-10-18 stsp view->cols = COLS;
914 5c60c32a 2018-10-18 stsp err = view_resize(view);
916 5c60c32a 2018-10-18 stsp return err;
918 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
919 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
921 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
922 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
924 5c60c32a 2018-10-18 stsp return NULL;
927 5c60c32a 2018-10-18 stsp static const struct got_error *
928 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
930 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
932 5c60c32a 2018-10-18 stsp view->begin_x = 0;
933 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
934 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
935 5c60c32a 2018-10-18 stsp view->ncols = COLS;
936 5c60c32a 2018-10-18 stsp view->lines = LINES;
937 5c60c32a 2018-10-18 stsp view->cols = COLS;
938 5c60c32a 2018-10-18 stsp err = view_resize(view);
940 5c60c32a 2018-10-18 stsp return err;
942 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
943 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
945 5c60c32a 2018-10-18 stsp return NULL;
949 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
951 5c60c32a 2018-10-18 stsp return view->parent == NULL;
954 b65b3ea0 2022-06-23 thomas static int
955 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
957 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
960 e44940c3 2022-07-01 thomas static int
961 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
963 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
966 444d5325 2022-07-03 thomas static int
967 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
969 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
970 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
973 a5d43cac 2022-07-01 thomas static void
974 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
976 a5d43cac 2022-07-01 thomas PANEL *panel;
977 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
979 a5d43cac 2022-07-01 thomas if (view->parent)
980 a5d43cac 2022-07-01 thomas return view_border(view->parent);
982 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
983 a5d43cac 2022-07-01 thomas if (panel == NULL)
986 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
987 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
988 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
989 88e8b64f 2023-04-22 thomas view->begin_x, ACS_HLINE, view->ncols);
991 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
992 88e8b64f 2023-04-22 thomas ACS_VLINE, view->nlines);
995 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
996 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
997 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
998 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
999 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
1001 4d8c2215 2018-08-19 stsp static const struct got_error *
1002 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
1004 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
1005 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
1007 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
1009 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1010 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1012 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1013 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1014 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1016 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1018 4918811f 2022-07-01 thomas if (view->child) {
1019 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1021 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1022 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1023 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1024 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1025 40236d76 2022-06-23 thomas ncols = COLS;
1027 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1028 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1029 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1031 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1033 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1035 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1036 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1039 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1040 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1041 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1043 a5d43cac 2022-07-01 thomas if (hs) {
1044 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1045 a5d43cac 2022-07-01 thomas if (err)
1046 a5d43cac 2022-07-01 thomas return err;
1047 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1048 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1049 a5d43cac 2022-07-01 thomas if (err)
1050 a5d43cac 2022-07-01 thomas return err;
1052 a5d43cac 2022-07-01 thomas view_border(view);
1053 a5d43cac 2022-07-01 thomas update_panels();
1054 a5d43cac 2022-07-01 thomas doupdate();
1055 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1056 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1058 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1059 40236d76 2022-06-23 thomas ncols = COLS;
1061 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1062 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1063 ea0bff04 2022-07-19 thomas if (err)
1064 ea0bff04 2022-07-19 thomas return err;
1067 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1068 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1069 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1070 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1071 40236d76 2022-06-23 thomas wclear(view->window);
1073 40236d76 2022-06-23 thomas view->nlines = nlines;
1074 40236d76 2022-06-23 thomas view->ncols = ncols;
1075 40236d76 2022-06-23 thomas view->lines = LINES;
1076 40236d76 2022-06-23 thomas view->cols = COLS;
1078 5c60c32a 2018-10-18 stsp return NULL;
1081 ea0bff04 2022-07-19 thomas static const struct got_error *
1082 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1084 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1085 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1086 3a0139e8 2022-07-22 thomas int n = 0;
1088 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1089 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1092 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1093 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1095 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1096 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1097 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1100 ea0bff04 2022-07-19 thomas return err;
1103 97cb21cd 2022-07-12 thomas static void
1104 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1106 97cb21cd 2022-07-12 thomas if (n == 0)
1109 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1110 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1111 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1113 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1114 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1115 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1116 97cb21cd 2022-07-12 thomas view->offset -= n;
1118 97cb21cd 2022-07-12 thomas view->offset = 0;
1122 53d2bdd3 2022-07-10 thomas static const struct got_error *
1123 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1125 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1126 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1128 53d2bdd3 2022-07-10 thomas if (view->parent)
1129 53d2bdd3 2022-07-10 thomas v = view->parent;
1131 53d2bdd3 2022-07-10 thomas v = view;
1133 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1134 53d2bdd3 2022-07-10 thomas return NULL;
1136 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1138 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1139 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1140 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1141 53d2bdd3 2022-07-10 thomas if (view->parent)
1142 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1144 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1145 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1146 53d2bdd3 2022-07-10 thomas view->count = 0;
1147 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1148 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1149 53d2bdd3 2022-07-10 thomas view->count = 0;
1150 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1152 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1153 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1154 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1155 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1156 53d2bdd3 2022-07-10 thomas if (err)
1157 53d2bdd3 2022-07-10 thomas return err;
1158 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1159 53d2bdd3 2022-07-10 thomas } else {
1160 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1161 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1162 53d2bdd3 2022-07-10 thomas if (view->parent)
1163 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1165 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1166 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1167 53d2bdd3 2022-07-10 thomas view->count = 0;
1168 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1169 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1170 53d2bdd3 2022-07-10 thomas view->count = 0;
1171 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1173 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1176 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1177 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1178 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1179 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1181 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1182 53d2bdd3 2022-07-10 thomas if (err)
1183 53d2bdd3 2022-07-10 thomas return err;
1184 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1185 53d2bdd3 2022-07-10 thomas if (err)
1186 53d2bdd3 2022-07-10 thomas return err;
1188 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1189 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1190 53d2bdd3 2022-07-10 thomas if (err)
1191 53d2bdd3 2022-07-10 thomas return err;
1194 3a0139e8 2022-07-22 thomas if (v->resize)
1195 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1196 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1197 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1199 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1201 53d2bdd3 2022-07-10 thomas return err;
1204 53d2bdd3 2022-07-10 thomas static void
1205 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1207 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1209 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1210 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1213 669b5ffa 2018-10-07 stsp static const struct got_error *
1214 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1216 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1218 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1219 669b5ffa 2018-10-07 stsp return NULL;
1221 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1222 669b5ffa 2018-10-07 stsp view->child = NULL;
1223 669b5ffa 2018-10-07 stsp return err;
1226 40236d76 2022-06-23 thomas static const struct got_error *
1227 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1229 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1231 669b5ffa 2018-10-07 stsp view->child = child;
1232 669b5ffa 2018-10-07 stsp child->parent = view;
1234 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1235 53d2bdd3 2022-07-10 thomas if (err)
1236 53d2bdd3 2022-07-10 thomas return err;
1238 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1239 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1241 53d2bdd3 2022-07-10 thomas return err;
1244 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1245 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1247 2a31b33b 2022-07-23 thomas static const struct got_error *
1248 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1249 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1251 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1252 2a31b33b 2022-07-23 thomas const struct got_error *err;
1253 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1255 2a31b33b 2022-07-23 thomas *requested = NULL;
1257 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1258 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1260 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1261 2a31b33b 2022-07-23 thomas if (err)
1262 2a31b33b 2022-07-23 thomas return err;
1264 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1265 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1266 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1267 2a31b33b 2022-07-23 thomas if (err)
1268 2a31b33b 2022-07-23 thomas return err;
1271 2a31b33b 2022-07-23 thomas view->focussed = 0;
1272 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1273 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1274 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1275 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1277 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1278 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1279 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1280 2a31b33b 2022-07-23 thomas if (err)
1281 2a31b33b 2022-07-23 thomas return err;
1282 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1283 2a31b33b 2022-07-23 thomas if (err)
1284 2a31b33b 2022-07-23 thomas return err;
1285 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1287 2a31b33b 2022-07-23 thomas *requested = new_view;
1289 2a31b33b 2022-07-23 thomas return NULL;
1292 34bc9ec9 2019-02-22 stsp static void
1293 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1295 25791caa 2018-10-24 stsp int cols, lines;
1296 25791caa 2018-10-24 stsp struct winsize size;
1298 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1299 25791caa 2018-10-24 stsp cols = 80; /* Default */
1300 25791caa 2018-10-24 stsp lines = 24;
1302 25791caa 2018-10-24 stsp cols = size.ws_col;
1303 25791caa 2018-10-24 stsp lines = size.ws_row;
1305 25791caa 2018-10-24 stsp resize_term(lines, cols);
1308 2b49a8ae 2019-06-22 stsp static const struct got_error *
1309 f54d892e 2023-02-17 thomas view_search_start(struct tog_view *view, int fast_refresh)
1311 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1312 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1313 2b49a8ae 2019-06-22 stsp char pattern[1024];
1316 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1317 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1318 c0c4acc8 2021-01-24 stsp view->searching = 0;
1319 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1321 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1323 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1324 2b49a8ae 2019-06-22 stsp return NULL;
1326 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1327 a5d43cac 2022-07-01 thomas v = view->child;
1328 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1329 d07291c6 2022-12-30 thomas v = view->parent;
1331 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1332 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1334 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1335 2b49a8ae 2019-06-22 stsp nocbreak();
1337 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1338 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1341 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1342 557d3365 2023-04-14 thomas if (!fast_refresh && !using_mock_io)
1343 f54d892e 2023-02-17 thomas halfdelay(10);
1344 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1345 2b49a8ae 2019-06-22 stsp return NULL;
1347 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1348 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1349 7c32bd05 2019-06-22 stsp if (err) {
1350 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1351 7c32bd05 2019-06-22 stsp return err;
1353 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1354 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1355 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1356 2b49a8ae 2019-06-22 stsp view->search_next(view);
1359 2b49a8ae 2019-06-22 stsp return NULL;
1362 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1363 64486692 2022-07-07 thomas static const struct got_error *
1364 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1366 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1367 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1369 64486692 2022-07-07 thomas if (view->parent)
1370 64486692 2022-07-07 thomas v = view->parent;
1372 64486692 2022-07-07 thomas v = view;
1374 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1375 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1377 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1379 ddbc4d37 2022-07-12 thomas if (!v->child)
1380 ddbc4d37 2022-07-12 thomas return NULL;
1381 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1382 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1384 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1385 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1386 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1387 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1388 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1391 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1392 64486692 2022-07-07 thomas v->ncols = COLS;
1393 64486692 2022-07-07 thomas v->child->ncols = COLS;
1394 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1396 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1397 64486692 2022-07-07 thomas if (err)
1398 64486692 2022-07-07 thomas return err;
1400 64486692 2022-07-07 thomas v->child->mode = v->mode;
1401 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1402 64486692 2022-07-07 thomas v->focus_child = 1;
1404 64486692 2022-07-07 thomas err = view_fullscreen(v);
1405 64486692 2022-07-07 thomas if (err)
1406 64486692 2022-07-07 thomas return err;
1407 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1408 64486692 2022-07-07 thomas if (err)
1409 64486692 2022-07-07 thomas return err;
1411 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1412 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1413 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1414 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1415 cf1fe301 2022-07-29 thomas if (err)
1416 cf1fe301 2022-07-29 thomas return err;
1417 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1418 cf1fe301 2022-07-29 thomas if (err)
1419 cf1fe301 2022-07-29 thomas return err;
1420 ddbc4d37 2022-07-12 thomas } else {
1421 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1422 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1424 f4e6231a 2022-07-22 thomas if (v->resize)
1425 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1426 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1427 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1429 64486692 2022-07-07 thomas return err;
1433 b85a3496 2023-04-14 thomas * Strip trailing whitespace from str starting at byte *n;
1434 b85a3496 2023-04-14 thomas * if *n < 0, use strlen(str). Return new str length in *n.
1436 b85a3496 2023-04-14 thomas static void
1437 b85a3496 2023-04-14 thomas strip_trailing_ws(char *str, int *n)
1439 b85a3496 2023-04-14 thomas size_t x = *n;
1441 b85a3496 2023-04-14 thomas if (str == NULL || *str == '\0')
1444 b85a3496 2023-04-14 thomas if (x < 0)
1445 b85a3496 2023-04-14 thomas x = strlen(str);
1447 b85a3496 2023-04-14 thomas while (x-- > 0 && isspace((unsigned char)str[x]))
1448 b85a3496 2023-04-14 thomas str[x] = '\0';
1450 b85a3496 2023-04-14 thomas *n = x + 1;
1454 b85a3496 2023-04-14 thomas * Extract visible substring of line y from the curses screen
1455 13bc6832 2023-04-22 thomas * and strip trailing whitespace. If vline is set, overwrite
1456 13bc6832 2023-04-22 thomas * line[vline] with '|' because the ACS_VLINE character is
1457 13bc6832 2023-04-22 thomas * written out as 'x'. Write the line to file f.
1459 b85a3496 2023-04-14 thomas static const struct got_error *
1460 b85a3496 2023-04-14 thomas view_write_line(FILE *f, int y, int vline)
1462 b85a3496 2023-04-14 thomas char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1463 b85a3496 2023-04-14 thomas int r, w;
1465 b85a3496 2023-04-14 thomas r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1466 b85a3496 2023-04-14 thomas if (r == ERR)
1467 b85a3496 2023-04-14 thomas return got_error_fmt(GOT_ERR_RANGE,
1468 b85a3496 2023-04-14 thomas "failed to extract line %d", y);
1471 b85a3496 2023-04-14 thomas * In some views, lines are padded with blanks to COLS width.
1472 b85a3496 2023-04-14 thomas * Strip them so we can diff without the -b flag when testing.
1474 b85a3496 2023-04-14 thomas strip_trailing_ws(line, &r);
1476 13bc6832 2023-04-22 thomas if (vline > 0)
1477 b85a3496 2023-04-14 thomas line[vline] = '|';
1479 b85a3496 2023-04-14 thomas w = fprintf(f, "%s\n", line);
1480 b85a3496 2023-04-14 thomas if (w != r + 1) /* \n */
1481 b85a3496 2023-04-14 thomas return got_ferror(f, GOT_ERR_IO);
1483 b85a3496 2023-04-14 thomas return NULL;
1487 b85a3496 2023-04-14 thomas * Capture the visible curses screen by writing each line to the
1488 b85a3496 2023-04-14 thomas * file at the path set via the TOG_SCR_DUMP environment variable.
1490 b85a3496 2023-04-14 thomas static const struct got_error *
1491 b85a3496 2023-04-14 thomas screendump(struct tog_view *view)
1493 b85a3496 2023-04-14 thomas const struct got_error *err;
1496 5b3a801d 2023-04-22 thomas err = got_opentemp_truncate(tog_io.sdump);
1497 5b3a801d 2023-04-22 thomas if (err)
1498 5b3a801d 2023-04-22 thomas return err;
1500 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_x) ||
1501 b85a3496 2023-04-14 thomas (view->parent && view->begin_x)) {
1502 b85a3496 2023-04-14 thomas int ncols = view->child ? view->ncols : view->parent->ncols;
1504 b85a3496 2023-04-14 thomas /* vertical splitscreen */
1505 b85a3496 2023-04-14 thomas for (i = 0; i < view->nlines; ++i) {
1506 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, ncols - 1);
1507 b85a3496 2023-04-14 thomas if (err)
1508 b85a3496 2023-04-14 thomas goto done;
1510 b85a3496 2023-04-14 thomas } else {
1511 b85a3496 2023-04-14 thomas int hline = 0;
1513 b85a3496 2023-04-14 thomas /* fullscreen or horizontal splitscreen */
1514 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_y) ||
1515 b85a3496 2023-04-14 thomas (view->parent && view->begin_y)) /* hsplit */
1516 b85a3496 2023-04-14 thomas hline = view->child ?
1517 b85a3496 2023-04-14 thomas view->child->begin_y : view->begin_y;
1519 b85a3496 2023-04-14 thomas for (i = 0; i < view->lines; i++) {
1520 13bc6832 2023-04-22 thomas if (hline && i == hline - 1) {
1523 b85a3496 2023-04-14 thomas /* ACS_HLINE writes out as 'q', overwrite it */
1524 b85a3496 2023-04-14 thomas for (c = 0; c < view->cols; ++c)
1525 5b3a801d 2023-04-22 thomas fputc('-', tog_io.sdump);
1526 5b3a801d 2023-04-22 thomas fputc('\n', tog_io.sdump);
1527 b85a3496 2023-04-14 thomas continue;
1530 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, 0);
1531 b85a3496 2023-04-14 thomas if (err)
1532 b85a3496 2023-04-14 thomas goto done;
1537 b85a3496 2023-04-14 thomas return err;
1541 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1542 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1544 07b0611c 2022-06-23 thomas static int
1545 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1547 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1548 a5d43cac 2022-07-01 thomas int x, n = 0;
1550 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1551 a5d43cac 2022-07-01 thomas v = view->child;
1552 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1553 a5d43cac 2022-07-01 thomas v = view->parent;
1555 07b0611c 2022-06-23 thomas view->count = 0;
1556 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1557 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1558 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1559 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1560 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1563 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1564 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1565 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1566 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1570 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1571 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1573 07b0611c 2022-06-23 thomas if (n >= 9999999)
1574 07b0611c 2022-06-23 thomas n = 9999999;
1576 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1577 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1579 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1580 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1585 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1586 07b0611c 2022-06-23 thomas view->count = n;
1588 07b0611c 2022-06-23 thomas return c;
1591 f2d06bef 2023-01-23 thomas static void
1592 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1594 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1596 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1597 f2d06bef 2023-01-23 thomas v = view->child;
1598 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1599 f2d06bef 2023-01-23 thomas v = view->parent;
1601 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1602 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1603 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1604 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1607 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1608 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1610 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1611 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1612 f2d06bef 2023-01-23 thomas view->action = NULL;
1614 f2d06bef 2023-01-23 thomas view->action = NULL;
1618 b85a3496 2023-04-14 thomas * Read the next line from the test script and assign
1619 b85a3496 2023-04-14 thomas * key instruction to *ch. If at EOF, set the *done flag.
1621 0cf4efb1 2018-09-29 stsp static const struct got_error *
1622 a184c764 2023-04-22 thomas tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1624 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1625 b85a3496 2023-04-14 thomas char *line = NULL;
1626 b85a3496 2023-04-14 thomas size_t linesz = 0;
1628 a184c764 2023-04-22 thomas if (view->count && --view->count) {
1629 a184c764 2023-04-22 thomas *ch = view->ch;
1630 a184c764 2023-04-22 thomas return NULL;
1632 a184c764 2023-04-22 thomas *ch = -1;
1634 b85a3496 2023-04-14 thomas if (getline(&line, &linesz, script) == -1) {
1635 b85a3496 2023-04-14 thomas if (feof(script)) {
1636 b85a3496 2023-04-14 thomas *done = 1;
1637 b85a3496 2023-04-14 thomas goto done;
1638 b85a3496 2023-04-14 thomas } else {
1639 b85a3496 2023-04-14 thomas err = got_ferror(script, GOT_ERR_IO);
1640 b85a3496 2023-04-14 thomas goto done;
1644 8e778ade 2023-04-22 thomas if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1645 8e778ade 2023-04-22 thomas tog_io.wait_for_ui = 1;
1646 8e778ade 2023-04-22 thomas else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1647 b85a3496 2023-04-14 thomas *ch = KEY_ENTER;
1648 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1649 b85a3496 2023-04-14 thomas *ch = KEY_RIGHT;
1650 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1651 b85a3496 2023-04-14 thomas *ch = KEY_LEFT;
1652 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1653 b85a3496 2023-04-14 thomas *ch = KEY_DOWN;
1654 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_UP", 6) == 0)
1655 b85a3496 2023-04-14 thomas *ch = KEY_UP;
1656 52c5094b 2023-04-28 thomas else if (strncasecmp(line, "TAB", 3) == 0)
1657 52c5094b 2023-04-28 thomas *ch = '\t';
1658 13bc6832 2023-04-22 thomas else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1659 b85a3496 2023-04-14 thomas *ch = TOG_KEY_SCRDUMP;
1660 a184c764 2023-04-22 thomas else if (isdigit((unsigned char)*line)) {
1661 a184c764 2023-04-22 thomas char *t = line;
1663 a184c764 2023-04-22 thomas while (isdigit((unsigned char)*t))
1665 a184c764 2023-04-22 thomas view->ch = *ch = *t;
1666 a184c764 2023-04-22 thomas *t = '\0';
1667 a184c764 2023-04-22 thomas /* ignore error, view->count is 0 if instruction is invalid */
1668 a184c764 2023-04-22 thomas view->count = strtonum(line, 0, INT_MAX, NULL);
1670 b85a3496 2023-04-14 thomas *ch = *line;
1673 b85a3496 2023-04-14 thomas free(line);
1674 b85a3496 2023-04-14 thomas return err;
1677 b85a3496 2023-04-14 thomas static const struct got_error *
1678 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1679 557d3365 2023-04-14 thomas struct tog_view_list_head *views, int fast_refresh)
1681 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1682 669b5ffa 2018-10-07 stsp struct tog_view *v;
1683 1a76625f 2018-10-22 stsp int ch, errcode;
1685 e5a0f69f 2018-08-18 stsp *new = NULL;
1687 f2d06bef 2023-01-23 thomas if (view->action)
1688 f2d06bef 2023-01-23 thomas action_report(view);
1690 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1691 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1692 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1693 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1694 07b0611c 2022-06-23 thomas view->count = 0;
1697 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1698 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1699 82954512 2020-02-03 stsp if (errcode)
1700 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1701 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1702 3da8ef85 2021-09-21 stsp sched_yield();
1703 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1704 82954512 2020-02-03 stsp if (errcode)
1705 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1706 82954512 2020-02-03 stsp "pthread_mutex_lock");
1707 60493ae3 2019-06-20 stsp view->search_next(view);
1708 60493ae3 2019-06-20 stsp return NULL;
1711 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1712 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1713 1a76625f 2018-10-22 stsp if (errcode)
1714 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1716 557d3365 2023-04-14 thomas if (using_mock_io) {
1717 a184c764 2023-04-22 thomas err = tog_read_script_key(tog_io.f, view, &ch, done);
1718 9d9cab5e 2023-04-22 thomas if (err) {
1719 9d9cab5e 2023-04-22 thomas errcode = pthread_mutex_lock(&tog_mutex);
1720 b85a3496 2023-04-14 thomas return err;
1722 b85a3496 2023-04-14 thomas } else if (view->count && --view->count) {
1723 634cb454 2022-07-03 thomas cbreak();
1724 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1725 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1726 b85a3496 2023-04-14 thomas /* let C-g or backspace abort unfinished count */
1727 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1728 634cb454 2022-07-03 thomas view->count = 0;
1730 634cb454 2022-07-03 thomas ch = view->ch;
1731 634cb454 2022-07-03 thomas } else {
1732 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1733 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1734 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1736 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1737 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1738 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1739 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1740 1a76625f 2018-10-22 stsp if (errcode)
1741 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1743 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1744 25791caa 2018-10-24 stsp tog_resizeterm();
1745 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1746 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1747 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1748 25791caa 2018-10-24 stsp err = view_resize(v);
1750 25791caa 2018-10-24 stsp return err;
1751 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1753 25791caa 2018-10-24 stsp return err;
1754 cdfcfb03 2020-12-06 stsp if (v->child) {
1755 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1757 cdfcfb03 2020-12-06 stsp return err;
1758 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1759 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1761 cdfcfb03 2020-12-06 stsp return err;
1762 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1763 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1764 53d2bdd3 2022-07-10 thomas if (err)
1765 53d2bdd3 2022-07-10 thomas return err;
1771 e5a0f69f 2018-08-18 stsp switch (ch) {
1772 fc2737d5 2022-09-15 thomas case '?':
1773 fc2737d5 2022-09-15 thomas case 'H':
1774 fc2737d5 2022-09-15 thomas case KEY_F(1):
1775 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1776 fc2737d5 2022-09-15 thomas err = view->reset(view);
1778 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1781 07b0611c 2022-06-23 thomas view->count = 0;
1782 1e37a5c2 2019-05-12 jcs if (view->child) {
1783 e78dc838 2020-12-04 stsp view->focussed = 0;
1784 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1785 e78dc838 2020-12-04 stsp view->focus_child = 1;
1786 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1787 e78dc838 2020-12-04 stsp view->focussed = 0;
1788 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1789 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1790 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1791 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1792 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1794 a5d43cac 2022-07-01 thomas if (err)
1795 a5d43cac 2022-07-01 thomas return err;
1797 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1798 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1799 a5d43cac 2022-07-01 thomas if (err)
1800 a5d43cac 2022-07-01 thomas return err;
1805 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1806 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1807 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1808 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1809 a5d43cac 2022-07-01 thomas if (err)
1812 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1814 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1815 9970f7fc 2020-12-03 stsp view->dying = 1;
1820 1c5e5faa 2022-06-23 thomas case 'F':
1821 07b0611c 2022-06-23 thomas view->count = 0;
1822 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1823 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1825 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1826 e78dc838 2020-12-04 stsp view->focussed = 0;
1827 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1828 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1829 53d2bdd3 2022-07-10 thomas } else {
1830 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1831 53d2bdd3 2022-07-10 thomas if (!err)
1832 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1836 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1837 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1839 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1840 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1841 e78dc838 2020-12-04 stsp view->focussed = 1;
1842 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1844 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1845 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1846 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1847 53d2bdd3 2022-07-10 thomas if (!err)
1848 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1852 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1854 a5d43cac 2022-07-01 thomas if (err)
1856 3a0139e8 2022-07-22 thomas if (view->resize) {
1857 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1858 a5d43cac 2022-07-01 thomas if (err)
1861 e0bcabc5 2023-04-28 thomas if (view->parent) {
1862 e0bcabc5 2023-04-28 thomas if (view->parent->resize) {
1863 e0bcabc5 2023-04-28 thomas err = view->parent->resize(view->parent, 0);
1864 e0bcabc5 2023-04-28 thomas if (err != NULL)
1867 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1868 e0bcabc5 2023-04-28 thomas if (err != NULL)
1871 e0bcabc5 2023-04-28 thomas err = offset_selection_down(view);
1873 64486692 2022-07-07 thomas case 'S':
1874 53d2bdd3 2022-07-10 thomas view->count = 0;
1875 64486692 2022-07-07 thomas err = switch_split(view);
1877 53d2bdd3 2022-07-10 thomas case '-':
1878 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1880 53d2bdd3 2022-07-10 thomas case '+':
1881 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1883 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1886 07b0611c 2022-06-23 thomas view->count = 0;
1887 60493ae3 2019-06-20 stsp if (view->search_start)
1888 f54d892e 2023-02-17 thomas view_search_start(view, fast_refresh);
1890 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1894 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1895 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1896 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1897 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1898 60493ae3 2019-06-20 stsp view->search_next(view);
1900 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1902 adf4c9e0 2022-07-03 thomas case 'A':
1903 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1904 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1905 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1906 f2d06bef 2023-01-23 thomas } else {
1907 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1908 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1910 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1911 adf4c9e0 2022-07-03 thomas if (v->reset) {
1912 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1913 adf4c9e0 2022-07-03 thomas if (err)
1914 adf4c9e0 2022-07-03 thomas return err;
1916 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1917 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1918 adf4c9e0 2022-07-03 thomas if (err)
1919 adf4c9e0 2022-07-03 thomas return err;
1923 b85a3496 2023-04-14 thomas case TOG_KEY_SCRDUMP:
1924 b85a3496 2023-04-14 thomas err = screendump(view);
1927 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1931 e5a0f69f 2018-08-18 stsp return err;
1934 ef20f542 2022-06-26 thomas static int
1935 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1937 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1938 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1940 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1942 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1945 669b5ffa 2018-10-07 stsp return view->focussed;
1948 bcbd79e2 2018-08-19 stsp static const struct got_error *
1949 557d3365 2023-04-14 thomas tog_io_close(void)
1951 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1953 557d3365 2023-04-14 thomas if (tog_io.cin && fclose(tog_io.cin) == EOF)
1954 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cin, GOT_ERR_IO);
1955 557d3365 2023-04-14 thomas if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1956 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cout, GOT_ERR_IO);
1957 557d3365 2023-04-14 thomas if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1958 557d3365 2023-04-14 thomas err = got_ferror(tog_io.f, GOT_ERR_IO);
1959 5b3a801d 2023-04-22 thomas if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1960 5b3a801d 2023-04-22 thomas err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1962 b85a3496 2023-04-14 thomas return err;
1965 b85a3496 2023-04-14 thomas static const struct got_error *
1966 557d3365 2023-04-14 thomas view_loop(struct tog_view *view)
1968 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1969 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1970 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1971 64486692 2022-07-07 thomas char *mode;
1972 fd823528 2018-10-22 stsp int fast_refresh = 10;
1973 1a76625f 2018-10-22 stsp int done = 0, errcode;
1975 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1976 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1977 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1979 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1981 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1982 1a76625f 2018-10-22 stsp if (errcode)
1983 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1985 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1986 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1988 1004088d 2018-09-29 stsp view->focussed = 1;
1989 878940b7 2018-09-29 stsp err = view->show(view);
1991 0cf4efb1 2018-09-29 stsp return err;
1992 0cf4efb1 2018-09-29 stsp update_panels();
1993 0cf4efb1 2018-09-29 stsp doupdate();
1994 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1995 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1996 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1997 557d3365 2023-04-14 thomas if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1998 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
2000 557d3365 2023-04-14 thomas err = view_input(&new_view, &done, view, &views, fast_refresh);
2004 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
2005 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
2006 6ec3d39c 2023-01-23 thomas done = 1;
2007 6ec3d39c 2023-01-23 thomas if (done) {
2008 6ec3d39c 2023-01-23 thomas struct tog_view *v;
2011 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
2012 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
2014 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
2015 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
2016 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
2017 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
2018 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
2019 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
2020 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
2021 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
2024 6ec3d39c 2023-01-23 thomas doupdate();
2027 9970f7fc 2020-12-03 stsp if (view->dying) {
2028 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2030 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2031 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2033 e78dc838 2020-12-04 stsp else if (view->parent)
2034 669b5ffa 2018-10-07 stsp prev = view->parent;
2036 e78dc838 2020-12-04 stsp if (view->parent) {
2037 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2038 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2039 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
2040 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
2041 40236d76 2022-06-23 thomas err = view_resize(view->parent);
2042 40236d76 2022-06-23 thomas if (err)
2044 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
2045 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
2047 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2049 9970f7fc 2020-12-03 stsp err = view_close(view);
2051 e5a0f69f 2018-08-18 stsp goto done;
2053 e78dc838 2020-12-04 stsp view = NULL;
2054 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2055 e78dc838 2020-12-04 stsp if (v->focussed)
2058 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2059 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2061 e78dc838 2020-12-04 stsp view = prev;
2062 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2063 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2064 e78dc838 2020-12-04 stsp tog_view_list_head);
2066 e78dc838 2020-12-04 stsp if (view) {
2067 e78dc838 2020-12-04 stsp if (view->focus_child) {
2068 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2069 e78dc838 2020-12-04 stsp view = view->child;
2071 e78dc838 2020-12-04 stsp view->focussed = 1;
2075 bcbd79e2 2018-08-19 stsp if (new_view) {
2076 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2077 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2078 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2079 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2081 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2082 86c66b02 2018-10-18 stsp err = view_close(v);
2084 86c66b02 2018-10-18 stsp goto done;
2087 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2088 fed7eaa8 2018-10-24 stsp view = new_view;
2090 6ec3d39c 2023-01-23 thomas if (view && !done) {
2091 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2092 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2093 e78dc838 2020-12-04 stsp view = view->child;
2095 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2096 e78dc838 2020-12-04 stsp view = view->parent;
2098 e78dc838 2020-12-04 stsp show_panel(view->panel);
2099 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2100 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2101 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2102 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2104 1a76625f 2018-10-22 stsp goto done;
2106 669b5ffa 2018-10-07 stsp err = view->show(view);
2108 1a76625f 2018-10-22 stsp goto done;
2109 669b5ffa 2018-10-07 stsp if (view->child) {
2110 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2112 1a76625f 2018-10-22 stsp goto done;
2114 1a76625f 2018-10-22 stsp update_panels();
2115 1a76625f 2018-10-22 stsp doupdate();
2119 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2120 f2d749db 2022-07-12 thomas const struct got_error *close_err;
2121 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2122 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2123 f2d749db 2022-07-12 thomas close_err = view_close(view);
2124 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
2125 f2d749db 2022-07-12 thomas err = close_err;
2128 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2129 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2130 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2132 e5a0f69f 2018-08-18 stsp return err;
2135 4ed7e80c 2018-05-20 stsp __dead static void
2136 9f7d7167 2018-04-29 stsp usage_log(void)
2139 c70c5802 2018-08-01 stsp fprintf(stderr,
2140 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2141 9f7d7167 2018-04-29 stsp getprogname());
2145 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2146 80ddbec8 2018-04-29 stsp static const struct got_error *
2147 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2149 00dfcb92 2018-06-11 stsp char *vis = NULL;
2150 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2152 963b370f 2018-05-20 stsp *ws = NULL;
2153 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2154 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2155 00dfcb92 2018-06-11 stsp int vislen;
2156 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2157 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2159 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2160 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2162 00dfcb92 2018-06-11 stsp return err;
2163 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2164 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2165 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2166 a7f50699 2018-06-11 stsp goto done;
2170 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2171 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2172 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2173 a7f50699 2018-06-11 stsp goto done;
2176 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2177 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2179 00dfcb92 2018-06-11 stsp free(vis);
2180 963b370f 2018-05-20 stsp if (err) {
2181 963b370f 2018-05-20 stsp free(*ws);
2182 963b370f 2018-05-20 stsp *ws = NULL;
2183 963b370f 2018-05-20 stsp *wlen = 0;
2185 963b370f 2018-05-20 stsp return err;
2188 05171be4 2022-06-23 thomas static const struct got_error *
2189 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
2191 05171be4 2022-06-23 thomas char *dst;
2192 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
2194 05171be4 2022-06-23 thomas *ptr = NULL;
2195 05171be4 2022-06-23 thomas n = len = strlen(src);
2196 83316834 2022-06-23 thomas dst = malloc(n + 1);
2197 05171be4 2022-06-23 thomas if (dst == NULL)
2198 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
2200 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
2201 05171be4 2022-06-23 thomas const char c = src[idx];
2203 05171be4 2022-06-23 thomas if (c == '\t') {
2204 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
2205 b235ad5d 2022-06-23 thomas char *p;
2207 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
2208 83316834 2022-06-23 thomas if (p == NULL) {
2209 83316834 2022-06-23 thomas free(dst);
2210 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
2213 83316834 2022-06-23 thomas dst = p;
2214 05171be4 2022-06-23 thomas n += nb;
2215 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
2216 05171be4 2022-06-23 thomas sz += nb;
2218 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2222 05171be4 2022-06-23 thomas dst[sz] = '\0';
2223 05171be4 2022-06-23 thomas *ptr = dst;
2224 05171be4 2022-06-23 thomas return NULL;
2228 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2229 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2230 20181212 2023-06-01 thomas * Return the combined column width of all spanned wide characters in
2231 8d208d34 2022-06-23 thomas * *rcol.
2233 8d208d34 2022-06-23 thomas static int
2234 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2236 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2238 8d208d34 2022-06-23 thomas if (n == 0) {
2239 8d208d34 2022-06-23 thomas *rcol = cols;
2240 8d208d34 2022-06-23 thomas return off;
2243 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2244 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2245 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2247 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2249 8d208d34 2022-06-23 thomas if (width == -1) {
2250 8d208d34 2022-06-23 thomas width = 1;
2251 8d208d34 2022-06-23 thomas wline[i] = L'.';
2254 8d208d34 2022-06-23 thomas if (cols + width > n)
2256 8d208d34 2022-06-23 thomas cols += width;
2259 8d208d34 2022-06-23 thomas *rcol = cols;
2260 8d208d34 2022-06-23 thomas return i;
2264 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2265 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2266 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2268 f91a2b48 2022-06-23 thomas static const struct got_error *
2269 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2270 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2272 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2273 8d208d34 2022-06-23 thomas int cols;
2274 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2275 05171be4 2022-06-23 thomas char *exstr = NULL;
2276 963b370f 2018-05-20 stsp size_t wlen;
2277 8d208d34 2022-06-23 thomas int i, scrollx;
2279 963b370f 2018-05-20 stsp *wlinep = NULL;
2280 b700b5d6 2018-07-10 stsp *widthp = 0;
2282 05171be4 2022-06-23 thomas if (expand) {
2283 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2284 05171be4 2022-06-23 thomas if (err)
2285 05171be4 2022-06-23 thomas return err;
2288 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2289 05171be4 2022-06-23 thomas free(exstr);
2291 963b370f 2018-05-20 stsp return err;
2293 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2295 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2296 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2299 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2300 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2304 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2305 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2307 b700b5d6 2018-07-10 stsp if (widthp)
2308 b700b5d6 2018-07-10 stsp *widthp = cols;
2309 f91a2b48 2022-06-23 thomas if (scrollxp)
2310 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2312 963b370f 2018-05-20 stsp free(wline);
2314 963b370f 2018-05-20 stsp *wlinep = wline;
2315 963b370f 2018-05-20 stsp return err;
2318 8b473291 2019-02-21 stsp static const struct got_error*
2319 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2320 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2322 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2323 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2325 8b473291 2019-02-21 stsp const char *name;
2327 8b473291 2019-02-21 stsp *refs_str = NULL;
2329 00ddec2f 2023-05-17 thomas if (refs == NULL)
2330 00ddec2f 2023-05-17 thomas return NULL;
2332 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2333 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2334 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2337 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2338 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2340 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2341 8b473291 2019-02-21 stsp name += 5;
2342 0d095295 2023-06-01 thomas if (strncmp(name, "got/", 4) == 0)
2344 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2345 8b473291 2019-02-21 stsp name += 6;
2346 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2347 8b473291 2019-02-21 stsp name += 8;
2348 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2349 081e3dc2 2023-06-15 thomas if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2352 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2355 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2356 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2357 5d844a1e 2019-08-13 stsp if (err) {
2358 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2359 48cae60d 2020-09-22 stsp free(ref_id);
2362 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2363 5d844a1e 2019-08-13 stsp err = NULL;
2364 5d844a1e 2019-08-13 stsp tag = NULL;
2367 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2368 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2369 48cae60d 2020-09-22 stsp free(ref_id);
2371 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2372 52b5abe1 2019-08-13 stsp if (cmp != 0)
2374 8b473291 2019-02-21 stsp s = *refs_str;
2375 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2376 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2377 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2379 8b473291 2019-02-21 stsp *refs_str = NULL;
2385 8b473291 2019-02-21 stsp return err;
2388 963b370f 2018-05-20 stsp static const struct got_error *
2389 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2390 27a741e5 2019-09-11 stsp int col_tab_align)
2392 e6b8b890 2020-12-29 naddy char *smallerthan;
2394 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2395 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2396 5813d178 2019-03-09 stsp author = smallerthan + 1;
2397 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2398 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2399 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2402 5813d178 2019-03-09 stsp static const struct got_error *
2403 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2404 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2405 8fdc79fe 2020-12-01 naddy int author_display_cols)
2407 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2408 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2409 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2410 61dc16bb 2023-05-17 thomas char *refs_str = NULL;
2411 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2412 5813d178 2019-03-09 stsp char *author = NULL;
2413 cabb4cfd 2023-06-01 thomas wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2414 cabb4cfd 2023-06-01 thomas int author_width, refstr_width, logmsg_width;
2415 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2416 cabb4cfd 2023-06-01 thomas int col, limit, scrollx, logmsg_x;
2417 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2418 ccb26ccd 2018-11-05 stsp struct tm tm;
2419 45d799e2 2018-12-23 stsp time_t committer_time;
2420 11b20872 2019-11-08 stsp struct tog_color *tc;
2421 9472af95 2023-05-15 thomas struct got_reflist_head *refs;
2423 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2424 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2425 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2426 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2427 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2429 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2430 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2432 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2433 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2435 11b20872 2019-11-08 stsp wattr_on(view->window,
2436 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2437 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2439 11b20872 2019-11-08 stsp wattr_off(view->window,
2440 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2441 27a741e5 2019-09-11 stsp col = limit;
2442 b39d25c7 2018-07-10 stsp if (col > avail)
2443 b39d25c7 2018-07-10 stsp goto done;
2445 6570a66d 2019-11-08 stsp if (avail >= 120) {
2446 6570a66d 2019-11-08 stsp char *id_str;
2447 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2449 6570a66d 2019-11-08 stsp goto done;
2450 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2452 11b20872 2019-11-08 stsp wattr_on(view->window,
2453 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2454 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2456 11b20872 2019-11-08 stsp wattr_off(view->window,
2457 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2458 6570a66d 2019-11-08 stsp free(id_str);
2460 6570a66d 2019-11-08 stsp if (col > avail)
2461 6570a66d 2019-11-08 stsp goto done;
2464 f69c5a46 2022-07-19 thomas if (s->use_committer)
2465 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2467 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2468 5813d178 2019-03-09 stsp if (author == NULL) {
2469 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2470 80ddbec8 2018-04-29 stsp goto done;
2472 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2474 bb737323 2018-05-20 stsp goto done;
2475 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2477 11b20872 2019-11-08 stsp wattr_on(view->window,
2478 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2479 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2480 bb737323 2018-05-20 stsp col += author_width;
2481 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2482 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2484 bb737323 2018-05-20 stsp author_width++;
2487 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2488 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2489 9c2eaf34 2018-05-20 stsp if (col > avail)
2490 9c2eaf34 2018-05-20 stsp goto done;
2492 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2494 6d9fbc00 2018-04-29 stsp goto done;
2495 bb737323 2018-05-20 stsp logmsg = logmsg0;
2496 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2498 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2499 bb737323 2018-05-20 stsp if (newline)
2500 bb737323 2018-05-20 stsp *newline = '\0';
2502 cabb4cfd 2023-06-01 thomas limit = avail - col;
2503 cabb4cfd 2023-06-01 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2504 cabb4cfd 2023-06-01 thomas limit--; /* for the border */
2506 9472af95 2023-05-15 thomas /* Prepend reference labels to log message if possible .*/
2507 9472af95 2023-05-15 thomas refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2508 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, id, s->repo);
2509 61dc16bb 2023-05-17 thomas if (err)
2510 61dc16bb 2023-05-17 thomas goto done;
2511 61dc16bb 2023-05-17 thomas if (refs_str) {
2512 cabb4cfd 2023-06-01 thomas char *rs;
2514 cabb4cfd 2023-06-01 thomas if (asprintf(&rs, "[%s]", refs_str) == -1) {
2515 9472af95 2023-05-15 thomas err = got_error_from_errno("asprintf");
2516 9472af95 2023-05-15 thomas goto done;
2518 cabb4cfd 2023-06-01 thomas err = format_line(&wrefstr, &refstr_width,
2519 cabb4cfd 2023-06-01 thomas &scrollx, rs, view->x, limit, col, 1);
2520 cabb4cfd 2023-06-01 thomas free(rs);
2521 cabb4cfd 2023-06-01 thomas if (err)
2522 cabb4cfd 2023-06-01 thomas goto done;
2523 9472af95 2023-05-15 thomas tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2525 9472af95 2023-05-15 thomas wattr_on(view->window,
2526 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2527 cabb4cfd 2023-06-01 thomas waddwstr(view->window, &wrefstr[scrollx]);
2529 9472af95 2023-05-15 thomas wattr_off(view->window,
2530 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2531 cabb4cfd 2023-06-01 thomas col += MAX(refstr_width, 0);
2532 cabb4cfd 2023-06-01 thomas if (col > avail)
2533 cabb4cfd 2023-06-01 thomas goto done;
2535 cabb4cfd 2023-06-01 thomas if (col < avail) {
2536 cabb4cfd 2023-06-01 thomas waddch(view->window, ' ');
2540 cabb4cfd 2023-06-01 thomas if (refstr_width > 0)
2541 cabb4cfd 2023-06-01 thomas logmsg_x = 0;
2543 cabb4cfd 2023-06-01 thomas int unscrolled_refstr_width;
2544 cabb4cfd 2023-06-01 thomas size_t len = wcslen(wrefstr);
2547 cabb4cfd 2023-06-01 thomas * No need to check for -1 return value here since
2548 cabb4cfd 2023-06-01 thomas * unprintables have been replaced by span_wline().
2550 cabb4cfd 2023-06-01 thomas unscrolled_refstr_width = wcswidth(wrefstr, len);
2551 cabb4cfd 2023-06-01 thomas unscrolled_refstr_width += 1; /* trailing space */
2552 cabb4cfd 2023-06-01 thomas logmsg_x = view->x - unscrolled_refstr_width;
2555 cabb4cfd 2023-06-01 thomas limit = avail - col;
2556 cabb4cfd 2023-06-01 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2557 cabb4cfd 2023-06-01 thomas limit--; /* for the border */
2559 cabb4cfd 2023-06-01 thomas logmsg_x = view->x;
2561 cabb4cfd 2023-06-01 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2562 cabb4cfd 2023-06-01 thomas limit, col, 1);
2563 cabb4cfd 2023-06-01 thomas if (err)
2564 cabb4cfd 2023-06-01 thomas goto done;
2565 cabb4cfd 2023-06-01 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2566 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2567 27a741e5 2019-09-11 stsp while (col < avail) {
2568 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2572 80ddbec8 2018-04-29 stsp free(logmsg0);
2573 bb737323 2018-05-20 stsp free(wlogmsg);
2574 cabb4cfd 2023-06-01 thomas free(wrefstr);
2575 61dc16bb 2023-05-17 thomas free(refs_str);
2576 5813d178 2019-03-09 stsp free(author);
2577 bb737323 2018-05-20 stsp free(wauthor);
2578 80ddbec8 2018-04-29 stsp free(line);
2579 80ddbec8 2018-04-29 stsp return err;
2582 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2583 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2584 899d86c2 2018-05-10 stsp struct got_object_id *id)
2586 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2587 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2589 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2590 80ddbec8 2018-04-29 stsp if (entry == NULL)
2591 899d86c2 2018-05-10 stsp return NULL;
2593 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2594 d68b9737 2022-09-05 thomas if (dup == NULL) {
2595 d68b9737 2022-09-05 thomas free(entry);
2596 d68b9737 2022-09-05 thomas return NULL;
2599 d68b9737 2022-09-05 thomas entry->id = dup;
2600 99db9666 2018-05-07 stsp entry->commit = commit;
2601 899d86c2 2018-05-10 stsp return entry;
2604 99db9666 2018-05-07 stsp static void
2605 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2607 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2609 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2610 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2611 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2612 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2613 d68b9737 2022-09-05 thomas free(entry->id);
2614 99db9666 2018-05-07 stsp free(entry);
2617 99db9666 2018-05-07 stsp static void
2618 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2620 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2621 99db9666 2018-05-07 stsp pop_commit(commits);
2624 c4972b91 2018-05-07 stsp static const struct got_error *
2625 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2626 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2628 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2629 13add988 2019-10-15 stsp regmatch_t regmatch;
2630 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2632 13add988 2019-10-15 stsp *have_match = 0;
2634 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2636 13add988 2019-10-15 stsp return err;
2638 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2640 13add988 2019-10-15 stsp goto done;
2642 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2643 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2644 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2645 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2646 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2647 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2648 13add988 2019-10-15 stsp *have_match = 1;
2650 13add988 2019-10-15 stsp free(id_str);
2651 13add988 2019-10-15 stsp free(logmsg);
2652 13add988 2019-10-15 stsp return err;
2655 13add988 2019-10-15 stsp static const struct got_error *
2656 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2658 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2661 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2662 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2663 1a76625f 2018-10-22 stsp * while updating the display.
2666 7210b715 2022-09-11 thomas struct got_object_id id;
2667 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2668 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2669 7e8004ba 2022-09-11 thomas int limit_match = 0;
2670 1a76625f 2018-10-22 stsp int errcode;
2672 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2673 4e0d2870 2020-12-07 naddy NULL, NULL);
2674 7210b715 2022-09-11 thomas if (err)
2677 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2680 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2681 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2682 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2686 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2687 1a76625f 2018-10-22 stsp if (errcode) {
2688 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2689 13add988 2019-10-15 stsp "pthread_mutex_lock");
2693 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2694 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2695 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2697 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2698 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2699 7e8004ba 2022-09-11 thomas a->limit_regex);
2700 7e8004ba 2022-09-11 thomas if (err)
2703 7e8004ba 2022-09-11 thomas if (limit_match) {
2704 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2706 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2707 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2708 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2709 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2710 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2713 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2714 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2716 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2717 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2718 7e8004ba 2022-09-11 thomas matched, entry);
2719 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2723 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2724 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2725 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2727 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2730 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2731 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2732 7c1452c1 2020-03-26 stsp int have_match;
2733 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2737 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2738 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2739 7e8004ba 2022-09-11 thomas *a->search_next_done =
2740 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2741 7e8004ba 2022-09-11 thomas } else if (have_match)
2742 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2745 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2746 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2747 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2748 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2751 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2753 9ba79e04 2018-06-11 stsp return err;
2756 2b779855 2020-12-05 naddy static void
2757 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2759 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2760 2b779855 2020-12-05 naddy int ncommits = 0;
2762 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2763 2b779855 2020-12-05 naddy while (entry) {
2764 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2765 2b779855 2020-12-05 naddy s->selected_entry = entry;
2768 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2769 2b779855 2020-12-05 naddy ncommits++;
2773 0553a4e3 2018-04-30 stsp static const struct got_error *
2774 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2776 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2777 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2778 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2779 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2780 60493ae3 2019-06-20 stsp int width;
2781 cabb4cfd 2023-06-01 thomas int ncommits, author_cols = 4, refstr_cols;
2782 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2783 8b473291 2019-02-21 stsp char *refs_str = NULL;
2784 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2785 11b20872 2019-11-08 stsp struct tog_color *tc;
2786 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2787 cabb4cfd 2023-06-01 thomas struct got_reflist_head *refs;
2789 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2790 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2792 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2793 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2794 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2796 ecb28ae0 2018-07-16 stsp return err;
2797 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2798 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2799 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2800 00ddec2f 2023-05-17 thomas s->repo);
2801 00ddec2f 2023-05-17 thomas if (err)
2802 00ddec2f 2023-05-17 thomas goto done;
2805 557d3365 2023-04-14 thomas if (s->thread_args.commits_needed == 0 && !using_mock_io)
2806 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2808 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2809 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2810 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2811 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2812 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2813 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2814 8f4ed634 2020-03-26 stsp goto done;
2817 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2818 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2820 f9686aa5 2020-03-27 stsp if (view->searching) {
2821 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2822 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2823 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2824 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2825 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2826 f9686aa5 2020-03-27 stsp search_str = "searching...";
2829 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2830 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2832 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2833 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2834 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2835 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2836 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2837 8f4ed634 2020-03-26 stsp goto done;
2841 00580e07 2023-06-01 thomas free(refs_str);
2842 00580e07 2023-06-01 thomas refs_str = NULL;
2844 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2845 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2846 91198554 2022-06-23 thomas "........................................",
2847 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2848 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2849 1a76625f 2018-10-22 stsp header = NULL;
2850 1a76625f 2018-10-22 stsp goto done;
2852 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2853 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2854 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2855 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2856 1a76625f 2018-10-22 stsp header = NULL;
2857 1a76625f 2018-10-22 stsp goto done;
2859 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2861 1a76625f 2018-10-22 stsp goto done;
2863 2814baeb 2018-08-01 stsp werase(view->window);
2865 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2866 a3404814 2018-09-02 stsp wstandout(view->window);
2867 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2869 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2870 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2871 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2872 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2876 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2877 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2878 a3404814 2018-09-02 stsp wstandend(view->window);
2879 ecb28ae0 2018-07-16 stsp free(wline);
2880 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2881 1a76625f 2018-10-22 stsp goto done;
2883 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2884 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2885 5813d178 2019-03-09 stsp ncommits = 0;
2886 05171be4 2022-06-23 thomas view->maxx = 0;
2887 5813d178 2019-03-09 stsp while (entry) {
2888 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2889 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2890 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2891 5813d178 2019-03-09 stsp int width;
2892 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2894 f69c5a46 2022-07-19 thomas if (s->use_committer)
2895 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2897 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2898 5813d178 2019-03-09 stsp if (author == NULL) {
2899 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2900 5813d178 2019-03-09 stsp goto done;
2902 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2903 27a741e5 2019-09-11 stsp date_display_cols);
2904 5813d178 2019-03-09 stsp if (author_cols < width)
2905 5813d178 2019-03-09 stsp author_cols = width;
2906 5813d178 2019-03-09 stsp free(wauthor);
2907 5813d178 2019-03-09 stsp free(author);
2908 ef944b8b 2022-07-21 thomas if (err)
2909 ef944b8b 2022-07-21 thomas goto done;
2910 cabb4cfd 2023-06-01 thomas refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2911 cabb4cfd 2023-06-01 thomas entry->id);
2912 cabb4cfd 2023-06-01 thomas err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2913 cabb4cfd 2023-06-01 thomas if (err)
2914 cabb4cfd 2023-06-01 thomas goto done;
2915 cabb4cfd 2023-06-01 thomas if (refs_str) {
2916 cabb4cfd 2023-06-01 thomas wchar_t *ws;
2917 cabb4cfd 2023-06-01 thomas err = format_line(&ws, &width, NULL, refs_str,
2918 cabb4cfd 2023-06-01 thomas 0, INT_MAX, date_display_cols + author_cols, 0);
2919 cabb4cfd 2023-06-01 thomas free(ws);
2920 00580e07 2023-06-01 thomas free(refs_str);
2921 00580e07 2023-06-01 thomas refs_str = NULL;
2922 cabb4cfd 2023-06-01 thomas if (err)
2923 cabb4cfd 2023-06-01 thomas goto done;
2924 cabb4cfd 2023-06-01 thomas refstr_cols = width + 3; /* account for [ ] + space */
2926 cabb4cfd 2023-06-01 thomas refstr_cols = 0;
2927 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2928 05171be4 2022-06-23 thomas if (err)
2929 05171be4 2022-06-23 thomas goto done;
2930 05171be4 2022-06-23 thomas msg = msg0;
2931 05171be4 2022-06-23 thomas while (*msg == '\n')
2933 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2934 331b1a16 2022-06-23 thomas *eol = '\0';
2935 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2936 cabb4cfd 2023-06-01 thomas date_display_cols + author_cols + refstr_cols, 0);
2937 331b1a16 2022-06-23 thomas if (err)
2938 331b1a16 2022-06-23 thomas goto done;
2939 cabb4cfd 2023-06-01 thomas view->maxx = MAX(view->maxx, width + refstr_cols);
2940 05171be4 2022-06-23 thomas free(msg0);
2941 331b1a16 2022-06-23 thomas free(wmsg);
2942 7ca04879 2019-10-19 stsp ncommits++;
2943 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2946 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2947 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2948 867c6645 2018-07-10 stsp ncommits = 0;
2949 899d86c2 2018-05-10 stsp while (entry) {
2950 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2952 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2953 2814baeb 2018-08-01 stsp wstandout(view->window);
2954 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2955 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2956 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2957 2814baeb 2018-08-01 stsp wstandend(view->window);
2959 60493ae3 2019-06-20 stsp goto done;
2960 0553a4e3 2018-04-30 stsp ncommits++;
2961 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2962 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2965 a5d43cac 2022-07-01 thomas view_border(view);
2967 1a76625f 2018-10-22 stsp free(id_str);
2968 8b473291 2019-02-21 stsp free(refs_str);
2969 1a76625f 2018-10-22 stsp free(ncommits_str);
2970 1a76625f 2018-10-22 stsp free(header);
2971 80ddbec8 2018-04-29 stsp return err;
2974 07b55e75 2018-05-10 stsp static void
2975 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2977 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2978 07b55e75 2018-05-10 stsp int nscrolled = 0;
2980 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2981 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2984 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2985 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2986 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2987 07b55e75 2018-05-10 stsp if (entry) {
2988 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2989 07b55e75 2018-05-10 stsp nscrolled++;
2994 aa075928 2018-05-10 stsp static const struct got_error *
2995 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2997 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2998 5e224a3e 2019-02-22 stsp int errcode;
3000 557d3365 2023-04-14 thomas if (!using_mock_io)
3001 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while loading commits */
3003 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
3004 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
3005 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
3006 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
3007 7aafa0d1 2019-02-22 stsp if (errcode)
3008 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3009 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3012 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
3013 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
3015 7c1452c1 2020-03-26 stsp if (!wait)
3018 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
3019 ffe38506 2020-12-01 naddy show_log_view(view);
3020 7c1452c1 2020-03-26 stsp update_panels();
3021 7c1452c1 2020-03-26 stsp doupdate();
3023 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
3024 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3025 82954512 2020-02-03 stsp if (errcode)
3026 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
3027 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3029 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
3030 ffe38506 2020-12-01 naddy show_log_view(view);
3031 7c1452c1 2020-03-26 stsp update_panels();
3032 7c1452c1 2020-03-26 stsp doupdate();
3035 5e224a3e 2019-02-22 stsp return NULL;
3038 a5d43cac 2022-07-01 thomas static const struct got_error *
3039 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
3041 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
3042 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3044 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
3045 fe731b51 2022-07-14 thomas return NULL;
3047 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
3048 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
3049 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3051 a5d43cac 2022-07-01 thomas return err;
3054 5e224a3e 2019-02-22 stsp static const struct got_error *
3055 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
3057 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
3058 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
3059 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
3060 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
3062 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
3063 5e224a3e 2019-02-22 stsp return NULL;
3065 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3066 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
3067 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
3069 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
3071 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
3072 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
3073 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3075 5e224a3e 2019-02-22 stsp return err;
3079 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3080 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3083 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
3084 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
3086 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3087 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
3089 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
3090 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
3092 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3093 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
3095 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3097 dd0a52c1 2018-05-20 stsp return err;
3100 cd0acaa7 2018-05-20 stsp static const struct got_error *
3101 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3102 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
3103 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3105 cd0acaa7 2018-05-20 stsp const struct got_error *err;
3106 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
3107 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3109 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3110 15a94983 2018-12-23 stsp if (diff_view == NULL)
3111 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3113 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3114 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3115 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3116 e5a0f69f 2018-08-18 stsp if (err == NULL)
3117 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3118 cd0acaa7 2018-05-20 stsp return err;
3121 80ddbec8 2018-04-29 stsp static const struct got_error *
3122 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3123 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3125 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3127 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3128 941e9f74 2019-05-21 stsp if (parent == NULL)
3129 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3131 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3132 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3133 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3134 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3135 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3136 941e9f74 2019-05-21 stsp s->tree = subtree;
3137 941e9f74 2019-05-21 stsp s->selected = 0;
3138 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3139 941e9f74 2019-05-21 stsp return NULL;
3142 941e9f74 2019-05-21 stsp static const struct got_error *
3143 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3144 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
3146 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3147 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3148 941e9f74 2019-05-21 stsp const char *p;
3149 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3151 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3153 941e9f74 2019-05-21 stsp while (*p) {
3154 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3155 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3156 56e0773d 2019-11-28 stsp char *te_name;
3158 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3161 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3162 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3163 941e9f74 2019-05-21 stsp if (slash == NULL)
3164 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3166 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3167 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3168 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3171 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3172 56e0773d 2019-11-28 stsp if (te == NULL) {
3173 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3174 56e0773d 2019-11-28 stsp free(te_name);
3177 56e0773d 2019-11-28 stsp free(te_name);
3178 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3180 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3181 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3183 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3184 941e9f74 2019-05-21 stsp if (slash)
3185 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3187 941e9f74 2019-05-21 stsp subpath = strdup(path);
3188 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3189 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3193 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
3198 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3199 941e9f74 2019-05-21 stsp free(tree_id);
3203 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3204 941e9f74 2019-05-21 stsp if (err) {
3205 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3208 941e9f74 2019-05-21 stsp if (slash == NULL)
3210 941e9f74 2019-05-21 stsp free(subpath);
3211 941e9f74 2019-05-21 stsp subpath = NULL;
3212 941e9f74 2019-05-21 stsp p = slash;
3215 941e9f74 2019-05-21 stsp free(subpath);
3216 1a76625f 2018-10-22 stsp return err;
3219 61266923 2020-01-14 stsp static const struct got_error *
3220 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3221 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3222 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3224 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3225 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3226 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3228 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3229 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3230 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3232 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3234 55cccc34 2020-02-20 stsp return err;
3235 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3237 55cccc34 2020-02-20 stsp *new_view = tree_view;
3239 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3240 55cccc34 2020-02-20 stsp return NULL;
3242 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
3245 55cccc34 2020-02-20 stsp static const struct got_error *
3246 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3248 61266923 2020-01-14 stsp sigset_t sigset;
3249 61266923 2020-01-14 stsp int errcode;
3251 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3252 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3254 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3255 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3256 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3257 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3258 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3259 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
3260 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3261 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
3262 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3264 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3265 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3266 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3268 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3269 61266923 2020-01-14 stsp if (errcode)
3270 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3272 61266923 2020-01-14 stsp return NULL;
3275 1a76625f 2018-10-22 stsp static void *
3276 1a76625f 2018-10-22 stsp log_thread(void *arg)
3278 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3279 1a76625f 2018-10-22 stsp int errcode = 0;
3280 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3281 1a76625f 2018-10-22 stsp int done = 0;
3284 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
3285 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
3287 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
3288 f2d749db 2022-07-12 thomas if (errcode) {
3289 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
3290 61266923 2020-01-14 stsp return (void *)err;
3293 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
3294 f2d749db 2022-07-12 thomas if (err) {
3295 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3296 f2d749db 2022-07-12 thomas goto done;
3299 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3300 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3301 f2d749db 2022-07-12 thomas if (errcode) {
3302 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3303 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3304 f2d749db 2022-07-12 thomas goto done;
3306 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3307 1a76625f 2018-10-22 stsp if (err) {
3308 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3309 f2d749db 2022-07-12 thomas goto done;
3310 1a76625f 2018-10-22 stsp err = NULL;
3312 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3313 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3314 7e8004ba 2022-09-11 thomas if (a->limit_match)
3315 7e8004ba 2022-09-11 thomas a->commits_needed--;
3317 7e8004ba 2022-09-11 thomas a->commits_needed--;
3320 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3321 3abe8080 2019-04-10 stsp if (errcode) {
3322 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3323 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3324 f2d749db 2022-07-12 thomas goto done;
3325 3abe8080 2019-04-10 stsp } else if (*a->quit)
3327 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3328 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3329 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3330 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3331 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3332 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3333 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3334 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3337 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3338 7c1452c1 2020-03-26 stsp if (errcode) {
3339 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3340 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3341 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3342 f2d749db 2022-07-12 thomas goto done;
3346 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3348 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3349 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3350 7c1452c1 2020-03-26 stsp &tog_mutex);
3351 f2d749db 2022-07-12 thomas if (errcode) {
3352 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3353 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3354 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3355 f2d749db 2022-07-12 thomas goto done;
3357 21355643 2020-12-06 stsp if (*a->quit)
3362 3abe8080 2019-04-10 stsp a->log_complete = 1;
3363 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3364 f2d749db 2022-07-12 thomas if (errcode)
3365 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3367 f2d749db 2022-07-12 thomas if (err) {
3368 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3369 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3371 1a76625f 2018-10-22 stsp return (void *)err;
3374 1a76625f 2018-10-22 stsp static const struct got_error *
3375 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3377 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3378 1a76625f 2018-10-22 stsp int errcode;
3380 1a76625f 2018-10-22 stsp if (s->thread) {
3381 1a76625f 2018-10-22 stsp s->quit = 1;
3382 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3383 1a76625f 2018-10-22 stsp if (errcode)
3384 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3385 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3386 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3387 1a76625f 2018-10-22 stsp if (errcode)
3388 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3389 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3390 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3391 1a76625f 2018-10-22 stsp if (errcode)
3392 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3393 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3394 1a76625f 2018-10-22 stsp if (errcode)
3395 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3396 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3397 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3400 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3401 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3402 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3405 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3406 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3407 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3408 1af5eddf 2022-06-23 thomas if (err == NULL)
3409 1af5eddf 2022-06-23 thomas err = pack_err;
3410 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3413 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3414 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3415 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3418 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3421 9343a5fb 2018-06-23 stsp static const struct got_error *
3422 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3424 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3425 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3426 276b94a1 2020-11-13 naddy int errcode;
3428 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3430 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3431 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3432 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3434 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3435 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3436 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3438 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3439 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3440 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3441 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3442 1a76625f 2018-10-22 stsp free(s->start_id);
3443 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3444 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3445 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3446 1a76625f 2018-10-22 stsp return err;
3450 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3451 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3452 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3453 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3454 7e8004ba 2022-09-11 thomas * works with very slight change.
3456 1a76625f 2018-10-22 stsp static const struct got_error *
3457 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3459 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3460 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3461 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3462 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3463 7e8004ba 2022-09-11 thomas char pattern[1024];
3464 7e8004ba 2022-09-11 thomas int ret;
3466 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3467 7e8004ba 2022-09-11 thomas v = view->child;
3468 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3469 7e8004ba 2022-09-11 thomas v = view->parent;
3471 7e8004ba 2022-09-11 thomas /* Get the pattern */
3472 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3473 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3474 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3475 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3476 7e8004ba 2022-09-11 thomas nocbreak();
3478 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3479 7e8004ba 2022-09-11 thomas cbreak();
3480 7e8004ba 2022-09-11 thomas noecho();
3481 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3482 7e8004ba 2022-09-11 thomas if (ret == ERR)
3483 7e8004ba 2022-09-11 thomas return NULL;
3485 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3487 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3488 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3490 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3491 7e8004ba 2022-09-11 thomas return NULL;
3494 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3495 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3496 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3497 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3498 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3499 7e8004ba 2022-09-11 thomas * the queue.
3501 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3502 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3503 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3504 7e8004ba 2022-09-11 thomas s->selected = 0;
3505 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3507 7e8004ba 2022-09-11 thomas return NULL;
3510 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3511 7e8004ba 2022-09-11 thomas return NULL;
3513 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3515 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3516 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3517 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3518 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3519 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3521 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3522 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3523 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3525 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3526 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3527 7e8004ba 2022-09-11 thomas int have_match = 0;
3529 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3530 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3531 7e8004ba 2022-09-11 thomas if (err)
3532 7e8004ba 2022-09-11 thomas return err;
3534 7e8004ba 2022-09-11 thomas if (have_match) {
3535 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3537 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3538 7e8004ba 2022-09-11 thomas entry->id);
3539 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3540 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3541 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3544 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3545 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3547 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3548 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3549 7e8004ba 2022-09-11 thomas matched, entry);
3550 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3554 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3555 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3556 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3557 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3558 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3559 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3560 7e8004ba 2022-09-11 thomas if (err)
3561 7e8004ba 2022-09-11 thomas return err;
3564 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3565 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3566 7e8004ba 2022-09-11 thomas s->selected = 0;
3568 7e8004ba 2022-09-11 thomas return NULL;
3571 7e8004ba 2022-09-11 thomas static const struct got_error *
3572 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3574 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3576 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3577 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3578 60493ae3 2019-06-20 stsp return NULL;
3581 60493ae3 2019-06-20 stsp static const struct got_error *
3582 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3584 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3585 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3586 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3588 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3589 f9686aa5 2020-03-27 stsp show_log_view(view);
3590 f9686aa5 2020-03-27 stsp update_panels();
3591 f9686aa5 2020-03-27 stsp doupdate();
3593 96e2b566 2019-07-08 stsp if (s->search_entry) {
3594 13add988 2019-10-15 stsp int errcode, ch;
3595 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3596 13add988 2019-10-15 stsp if (errcode)
3597 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3598 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3599 13add988 2019-10-15 stsp ch = wgetch(view->window);
3600 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3601 13add988 2019-10-15 stsp if (errcode)
3602 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3603 13add988 2019-10-15 stsp "pthread_mutex_lock");
3604 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3605 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3606 678cbce5 2019-07-28 stsp return NULL;
3608 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3609 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3611 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3612 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3613 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3615 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3616 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3617 1f4d5162 2022-06-23 thomas * might have changed.
3619 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3620 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3622 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3625 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3628 60493ae3 2019-06-20 stsp while (1) {
3629 13add988 2019-10-15 stsp int have_match = 0;
3631 60493ae3 2019-06-20 stsp if (entry == NULL) {
3632 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3633 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3634 f9967bca 2020-03-27 stsp view->search_next_done =
3635 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3636 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3637 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3638 f801134a 2019-06-25 stsp return NULL;
3641 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3642 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3643 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3645 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3646 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3649 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3650 13add988 2019-10-15 stsp &view->regex);
3653 13add988 2019-10-15 stsp if (have_match) {
3654 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3655 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3659 96e2b566 2019-07-08 stsp s->search_entry = entry;
3660 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3661 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3663 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3666 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3667 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3668 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3669 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3671 60493ae3 2019-06-20 stsp return err;
3674 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3675 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3677 60493ae3 2019-06-20 stsp return err;
3682 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3684 60493ae3 2019-06-20 stsp return NULL;
3687 60493ae3 2019-06-20 stsp static const struct got_error *
3688 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3689 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3690 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3692 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3693 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3694 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3695 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3696 1a76625f 2018-10-22 stsp int errcode;
3698 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3699 f135c941 2020-02-20 stsp free(s->in_repo_path);
3700 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3701 51b7e1c3 2023-04-14 thomas if (s->in_repo_path == NULL) {
3702 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("strdup");
3703 51b7e1c3 2023-04-14 thomas goto done;
3707 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3708 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3709 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3710 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3712 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3713 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3714 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3716 fb2756b9 2018-08-04 stsp s->repo = repo;
3717 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3718 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3719 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3720 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3721 9cd7cbd1 2020-12-07 stsp goto done;
3724 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3725 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3726 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3727 5036bf37 2018-09-24 stsp goto done;
3729 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3730 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3732 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3733 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3734 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3735 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3737 11b20872 2019-11-08 stsp goto done;
3738 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3739 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3740 11b20872 2019-11-08 stsp if (err) {
3741 11b20872 2019-11-08 stsp free_colors(&s->colors);
3742 11b20872 2019-11-08 stsp goto done;
3744 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3745 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3746 11b20872 2019-11-08 stsp if (err) {
3747 11b20872 2019-11-08 stsp free_colors(&s->colors);
3748 11b20872 2019-11-08 stsp goto done;
3752 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3753 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3754 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3755 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3756 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3757 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3759 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3760 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3761 1af5eddf 2022-06-23 thomas if (err)
3762 1af5eddf 2022-06-23 thomas goto done;
3764 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3765 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3766 7cd52833 2022-06-23 thomas if (err)
3767 7cd52833 2022-06-23 thomas goto done;
3768 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3769 b672a97a 2020-01-27 stsp !s->log_branches);
3771 1a76625f 2018-10-22 stsp goto done;
3772 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3773 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3775 c5b78334 2020-01-12 stsp goto done;
3777 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3778 1a76625f 2018-10-22 stsp if (errcode) {
3779 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3780 1a76625f 2018-10-22 stsp goto done;
3782 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3783 7c1452c1 2020-03-26 stsp if (errcode) {
3784 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3785 7c1452c1 2020-03-26 stsp goto done;
3788 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3789 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3790 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3791 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3792 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3793 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3794 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3795 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3796 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3797 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3798 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3799 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3800 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3801 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3802 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3803 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3804 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3806 51b7e1c3 2023-04-14 thomas if (err) {
3807 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
3808 51b7e1c3 2023-04-14 thomas close_log_view(view);
3809 51b7e1c3 2023-04-14 thomas view_close(view);
3811 ba4f502b 2018-08-04 stsp return err;
3814 e5a0f69f 2018-08-18 stsp static const struct got_error *
3815 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3817 f2f6d207 2020-11-24 stsp const struct got_error *err;
3818 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3820 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3821 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3822 2b380cc8 2018-10-24 stsp &s->thread_args);
3823 2b380cc8 2018-10-24 stsp if (errcode)
3824 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3825 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3826 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3828 f2f6d207 2020-11-24 stsp return err;
3832 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3835 b31f89ff 2022-07-01 thomas static void
3836 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3838 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3840 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3842 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3843 7e8004ba 2022-09-11 thomas view->count = 0;
3845 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3846 b31f89ff 2022-07-01 thomas || home)
3847 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3849 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3850 b31f89ff 2022-07-01 thomas --s->selected;
3852 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3854 b31f89ff 2022-07-01 thomas select_commit(s);
3858 b31f89ff 2022-07-01 thomas static const struct got_error *
3859 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3861 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3862 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3863 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3865 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3866 7e8004ba 2022-09-11 thomas return NULL;
3868 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3869 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3870 b31f89ff 2022-07-01 thomas return NULL;
3872 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3873 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3875 7ed048bd 2022-08-12 thomas if (!page) {
3876 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3877 b31f89ff 2022-07-01 thomas ++s->selected;
3879 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3880 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3881 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3884 7ed048bd 2022-08-12 thomas s->selected = 0;
3885 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3886 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3887 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3888 7ed048bd 2022-08-12 thomas if (entry == NULL)
3890 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3891 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3893 7ed048bd 2022-08-12 thomas if (n > 0)
3894 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3895 b31f89ff 2022-07-01 thomas } else {
3896 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3897 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3898 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3899 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3901 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3903 b31f89ff 2022-07-01 thomas if (err)
3904 b31f89ff 2022-07-01 thomas return err;
3907 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3908 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3910 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3911 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3912 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3913 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3916 b31f89ff 2022-07-01 thomas select_commit(s);
3918 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3919 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3920 b31f89ff 2022-07-01 thomas view->count = 0;
3922 b31f89ff 2022-07-01 thomas return NULL;
3925 a5d43cac 2022-07-01 thomas static void
3926 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3931 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3932 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3933 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3934 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3935 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3937 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3938 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3939 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3940 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3941 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3942 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3944 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3948 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3949 e5a0f69f 2018-08-18 stsp static const struct got_error *
3950 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3952 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3954 a5d43cac 2022-07-01 thomas view->nlines = y;
3955 64486692 2022-07-07 thomas view->ncols = COLS;
3956 a5d43cac 2022-07-01 thomas err = view_resize(view);
3957 a5d43cac 2022-07-01 thomas if (err)
3958 a5d43cac 2022-07-01 thomas return err;
3960 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3962 a5d43cac 2022-07-01 thomas return err;
3965 07dd3ed3 2022-08-06 thomas static const struct got_error *
3966 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3968 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3969 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3970 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3972 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3973 25100026 2022-08-13 thomas return NULL;
3975 07dd3ed3 2022-08-06 thomas g = view->gline;
3976 07dd3ed3 2022-08-06 thomas view->gline = 0;
3978 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3979 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3980 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3981 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3982 07dd3ed3 2022-08-06 thomas select_commit(s);
3983 07dd3ed3 2022-08-06 thomas return NULL;
3986 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3987 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3988 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3989 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3990 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3991 07dd3ed3 2022-08-06 thomas if (err)
3992 07dd3ed3 2022-08-06 thomas return err;
3993 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3994 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3996 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3997 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3999 07dd3ed3 2022-08-06 thomas select_commit(s);
4000 07dd3ed3 2022-08-06 thomas return NULL;
4004 c72de8ab 2023-02-03 thomas static void
4005 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
4008 c72de8ab 2023-02-03 thomas switch (ch) {
4009 c72de8ab 2023-02-03 thomas case KEY_LEFT:
4010 c72de8ab 2023-02-03 thomas case 'h':
4011 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
4012 c72de8ab 2023-02-03 thomas if (view->x <= 0)
4013 c72de8ab 2023-02-03 thomas view->count = 0;
4015 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
4016 c72de8ab 2023-02-03 thomas case 'l':
4017 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
4018 c72de8ab 2023-02-03 thomas view->x += 2;
4020 c72de8ab 2023-02-03 thomas view->count = 0;
4022 c72de8ab 2023-02-03 thomas case '0':
4023 c72de8ab 2023-02-03 thomas view->x = 0;
4025 c72de8ab 2023-02-03 thomas case '$':
4026 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
4027 c72de8ab 2023-02-03 thomas view->count = 0;
4029 c72de8ab 2023-02-03 thomas default:
4034 a5d43cac 2022-07-01 thomas static const struct got_error *
4035 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4037 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4038 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
4039 7ed048bd 2022-08-12 thomas int eos, nscroll;
4041 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
4042 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4043 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
4044 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
4045 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4046 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
4048 c0be8933 2022-08-12 thomas if (err)
4049 c0be8933 2022-08-12 thomas return err;
4052 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
4053 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
4054 068ab281 2022-07-01 thomas --eos; /* border */
4056 07dd3ed3 2022-08-06 thomas if (view->gline)
4057 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
4059 528dedf3 2021-08-30 stsp switch (ch) {
4060 7e8004ba 2022-09-11 thomas case '&':
4061 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
4064 1e37a5c2 2019-05-12 jcs s->quit = 1;
4066 05171be4 2022-06-23 thomas case '0':
4067 05171be4 2022-06-23 thomas case '$':
4068 05171be4 2022-06-23 thomas case KEY_RIGHT:
4069 05171be4 2022-06-23 thomas case 'l':
4070 05171be4 2022-06-23 thomas case KEY_LEFT:
4071 05171be4 2022-06-23 thomas case 'h':
4072 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
4075 1e37a5c2 2019-05-12 jcs case KEY_UP:
4078 f7140bf5 2021-10-17 thomas case CTRL('p'):
4079 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
4082 aa7a1117 2023-01-09 thomas case '=':
4083 912a3f79 2021-08-30 j case KEY_HOME:
4084 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
4085 07b0611c 2022-06-23 thomas view->count = 0;
4087 70f17a53 2022-06-13 thomas case CTRL('u'):
4088 23427b14 2022-06-23 thomas case 'u':
4089 70f17a53 2022-06-13 thomas nscroll /= 2;
4090 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4091 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4092 a4292ac5 2019-05-12 jcs case CTRL('b'):
4093 1c5e5faa 2022-06-23 thomas case 'b':
4094 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
4097 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4100 f7140bf5 2021-10-17 thomas case CTRL('n'):
4101 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
4103 f69c5a46 2022-07-19 thomas case '@':
4104 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
4105 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
4106 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
4109 aa7a1117 2023-01-09 thomas case '*':
4110 912a3f79 2021-08-30 j case KEY_END: {
4111 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4112 912a3f79 2021-08-30 j * traverse them all. */
4113 07b0611c 2022-06-23 thomas view->count = 0;
4114 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
4115 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
4116 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4117 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4118 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
4121 bccd1d5d 2022-06-13 thomas case CTRL('d'):
4122 23427b14 2022-06-23 thomas case 'd':
4123 70f17a53 2022-06-13 thomas nscroll /= 2;
4124 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4125 70f17a53 2022-06-13 thomas case KEY_NPAGE:
4126 1c5e5faa 2022-06-23 thomas case CTRL('f'):
4127 4c2d69cb 2022-06-23 thomas case 'f':
4128 b31f89ff 2022-07-01 thomas case ' ':
4129 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
4131 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4132 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4133 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4134 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
4135 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
4136 2b779855 2020-12-05 naddy select_commit(s);
4137 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
4138 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4139 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4140 7e8004ba 2022-09-11 thomas s->commits->ncommits;
4141 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4144 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4145 444d5325 2022-07-03 thomas case '\r':
4146 07b0611c 2022-06-23 thomas view->count = 0;
4147 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4149 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4151 1be4947a 2022-07-22 thomas case 'T':
4152 07b0611c 2022-06-23 thomas view->count = 0;
4153 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4155 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
4157 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4158 21355643 2020-12-06 stsp case CTRL('l'):
4160 07b0611c 2022-06-23 thomas view->count = 0;
4161 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4162 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4164 21355643 2020-12-06 stsp err = stop_log_thread(s);
4166 74cfe85e 2020-10-20 stsp return err;
4167 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4168 21355643 2020-12-06 stsp char *parent_path;
4169 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4171 21355643 2020-12-06 stsp return err;
4172 21355643 2020-12-06 stsp free(s->in_repo_path);
4173 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4174 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4175 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4176 21355643 2020-12-06 stsp struct got_object_id *start_id;
4177 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4178 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4179 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4180 466429a1 2022-11-17 thomas if (err) {
4181 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
4182 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
4183 466429a1 2022-11-17 thomas return err;
4184 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
4185 466429a1 2022-11-17 thomas free(s->head_ref_name);
4186 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
4187 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
4188 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4189 466429a1 2022-11-17 thomas &tog_refs, s->repo);
4190 466429a1 2022-11-17 thomas if (err)
4191 466429a1 2022-11-17 thomas return err;
4193 21355643 2020-12-06 stsp free(s->start_id);
4194 21355643 2020-12-06 stsp s->start_id = start_id;
4195 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4196 21355643 2020-12-06 stsp } else /* 'B' */
4197 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4199 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
4200 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4201 bf7e79b3 2022-06-23 thomas if (err)
4202 bf7e79b3 2022-06-23 thomas return err;
4204 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
4205 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
4206 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
4208 21355643 2020-12-06 stsp return err;
4209 51a10b52 2020-12-26 stsp tog_free_refs();
4210 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
4212 ca51c541 2020-12-07 stsp return err;
4213 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4214 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4216 d01904d4 2019-06-25 stsp return err;
4217 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4218 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4220 b672a97a 2020-01-27 stsp return err;
4221 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
4222 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
4223 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4224 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4225 21355643 2020-12-06 stsp s->selected_entry = NULL;
4226 21355643 2020-12-06 stsp s->selected = 0;
4227 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4228 21355643 2020-12-06 stsp s->quit = 0;
4229 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
4230 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
4231 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
4232 94ecf40d 2022-07-22 thomas view->offset = 0;
4234 1be4947a 2022-07-22 thomas case 'R':
4235 07b0611c 2022-06-23 thomas view->count = 0;
4236 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
4239 07b0611c 2022-06-23 thomas view->count = 0;
4243 80ddbec8 2018-04-29 stsp return err;
4246 4ed7e80c 2018-05-20 stsp static const struct got_error *
4247 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4249 c2db6724 2019-01-04 stsp const struct got_error *error;
4251 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4252 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4253 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4255 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4256 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4258 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4259 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4261 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4262 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4264 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4265 c2db6724 2019-01-04 stsp if (error != NULL)
4266 c2db6724 2019-01-04 stsp return error;
4268 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4269 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4271 c2db6724 2019-01-04 stsp return NULL;
4274 b85a3496 2023-04-14 thomas static const struct got_error *
4275 557d3365 2023-04-14 thomas init_mock_term(const char *test_script_path)
4277 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4278 5b3a801d 2023-04-22 thomas const char *screen_dump_path;
4281 b85a3496 2023-04-14 thomas if (test_script_path == NULL || *test_script_path == '\0')
4282 fa9bb690 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4284 557d3365 2023-04-14 thomas tog_io.f = fopen(test_script_path, "re");
4285 557d3365 2023-04-14 thomas if (tog_io.f == NULL) {
4286 b85a3496 2023-04-14 thomas err = got_error_from_errno_fmt("fopen: %s",
4287 b85a3496 2023-04-14 thomas test_script_path);
4288 b85a3496 2023-04-14 thomas goto done;
4291 b85a3496 2023-04-14 thomas /* test mode, we don't want any output */
4292 557d3365 2023-04-14 thomas tog_io.cout = fopen("/dev/null", "w+");
4293 557d3365 2023-04-14 thomas if (tog_io.cout == NULL) {
4294 f4086c71 2023-04-22 thomas err = got_error_from_errno2("fopen", "/dev/null");
4295 b85a3496 2023-04-14 thomas goto done;
4298 f4086c71 2023-04-22 thomas in = dup(fileno(tog_io.cout));
4299 f4086c71 2023-04-22 thomas if (in == -1) {
4300 f4086c71 2023-04-22 thomas err = got_error_from_errno("dup");
4301 f4086c71 2023-04-22 thomas goto done;
4303 f4086c71 2023-04-22 thomas tog_io.cin = fdopen(in, "r");
4304 557d3365 2023-04-14 thomas if (tog_io.cin == NULL) {
4305 f4086c71 2023-04-22 thomas err = got_error_from_errno("fdopen");
4306 f4086c71 2023-04-22 thomas close(in);
4307 b85a3496 2023-04-14 thomas goto done;
4310 5b3a801d 2023-04-22 thomas screen_dump_path = getenv("TOG_SCR_DUMP");
4311 5b3a801d 2023-04-22 thomas if (screen_dump_path == NULL || *screen_dump_path == '\0')
4312 5b3a801d 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4313 5b3a801d 2023-04-22 thomas tog_io.sdump = fopen(screen_dump_path, "wex");
4314 5b3a801d 2023-04-22 thomas if (tog_io.sdump == NULL) {
4315 5b3a801d 2023-04-22 thomas err = got_error_from_errno2("fopen", screen_dump_path);
4316 5b3a801d 2023-04-22 thomas goto done;
4319 557d3365 2023-04-14 thomas if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4320 b85a3496 2023-04-14 thomas err = got_error_from_errno("fseeko");
4321 b85a3496 2023-04-14 thomas goto done;
4324 557d3365 2023-04-14 thomas if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4325 b85a3496 2023-04-14 thomas err = got_error_msg(GOT_ERR_IO,
4326 b85a3496 2023-04-14 thomas "newterm: failed to initialise curses");
4328 557d3365 2023-04-14 thomas using_mock_io = 1;
4331 b85a3496 2023-04-14 thomas if (err)
4332 557d3365 2023-04-14 thomas tog_io_close();
4333 b85a3496 2023-04-14 thomas return err;
4336 557d3365 2023-04-14 thomas static void
4337 557d3365 2023-04-14 thomas init_curses(void)
4340 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
4341 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
4342 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
4344 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
4345 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
4346 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
4347 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
4348 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
4350 557d3365 2023-04-14 thomas if (using_mock_io) /* In test mode we use a fake terminal */
4353 557d3365 2023-04-14 thomas initscr();
4356 557d3365 2023-04-14 thomas halfdelay(1); /* Fast refresh while initial view is loading. */
4359 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4360 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4361 a915003a 2019-02-05 stsp curs_set(0);
4362 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4363 6d17833f 2019-11-08 stsp start_color();
4364 6d17833f 2019-11-08 stsp use_default_colors();
4370 c2db6724 2019-01-04 stsp static const struct got_error *
4371 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4372 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4374 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4376 f135c941 2020-02-20 stsp if (argc == 0) {
4377 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4378 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4379 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4380 f135c941 2020-02-20 stsp return NULL;
4383 f135c941 2020-02-20 stsp if (worktree) {
4384 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4387 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4389 f135c941 2020-02-20 stsp return err;
4390 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4391 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4392 bfd61697 2020-11-14 stsp p) == -1) {
4393 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4394 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4398 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4400 f135c941 2020-02-20 stsp return err;
4403 f135c941 2020-02-20 stsp static const struct got_error *
4404 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4406 1d98034b 2023-04-14 thomas const struct got_error *error;
4407 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4408 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4409 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4410 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4411 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4412 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4413 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4414 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4415 04cc582a 2018-08-01 stsp struct tog_view *view;
4416 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4418 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4419 80ddbec8 2018-04-29 stsp switch (ch) {
4421 b672a97a 2020-01-27 stsp log_branches = 1;
4424 80ddbec8 2018-04-29 stsp start_commit = optarg;
4427 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4428 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4429 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4433 17020d27 2019-03-07 stsp usage_log();
4434 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4438 80ddbec8 2018-04-29 stsp argc -= optind;
4439 80ddbec8 2018-04-29 stsp argv += optind;
4441 f135c941 2020-02-20 stsp if (argc > 1)
4442 f135c941 2020-02-20 stsp usage_log();
4444 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4445 7cd52833 2022-06-23 thomas if (error != NULL)
4446 7cd52833 2022-06-23 thomas goto done;
4448 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4449 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4450 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4451 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4452 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4453 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4454 c156c7a4 2020-12-18 stsp goto done;
4455 a1fbf39a 2019-08-11 stsp if (worktree)
4456 6962eb72 2020-02-20 stsp repo_path =
4457 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4459 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4460 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4461 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4462 c156c7a4 2020-12-18 stsp goto done;
4466 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4467 80ddbec8 2018-04-29 stsp if (error != NULL)
4468 ecb28ae0 2018-07-16 stsp goto done;
4470 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4471 f135c941 2020-02-20 stsp repo, worktree);
4472 f135c941 2020-02-20 stsp if (error)
4473 f135c941 2020-02-20 stsp goto done;
4475 557d3365 2023-04-14 thomas init_curses();
4477 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4478 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4479 c02c541e 2019-03-29 stsp if (error)
4480 c02c541e 2019-03-29 stsp goto done;
4482 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4483 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4484 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4485 87670572 2020-12-26 naddy if (error)
4486 87670572 2020-12-26 naddy goto done;
4489 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4490 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4491 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4492 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4493 d8f38dc4 2020-12-05 stsp if (error)
4494 d8f38dc4 2020-12-05 stsp goto done;
4495 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4497 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4498 d8f38dc4 2020-12-05 stsp if (error == NULL)
4499 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4500 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4501 d8f38dc4 2020-12-05 stsp goto done;
4502 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4503 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4504 d8f38dc4 2020-12-05 stsp if (error)
4505 d8f38dc4 2020-12-05 stsp goto done;
4508 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4509 04cc582a 2018-08-01 stsp if (view == NULL) {
4510 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4511 04cc582a 2018-08-01 stsp goto done;
4513 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4514 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4515 ba4f502b 2018-08-04 stsp if (error)
4516 ba4f502b 2018-08-04 stsp goto done;
4517 2fc00ff4 2019-08-31 stsp if (worktree) {
4518 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4519 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4520 2fc00ff4 2019-08-31 stsp worktree = NULL;
4522 557d3365 2023-04-14 thomas error = view_loop(view);
4524 f135c941 2020-02-20 stsp free(in_repo_path);
4525 ecb28ae0 2018-07-16 stsp free(repo_path);
4526 ecb28ae0 2018-07-16 stsp free(cwd);
4527 899d86c2 2018-05-10 stsp free(start_id);
4528 d8f38dc4 2020-12-05 stsp free(label);
4530 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4531 1d0f4054 2021-06-17 stsp if (repo) {
4532 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4533 1d0f4054 2021-06-17 stsp if (error == NULL)
4534 1d0f4054 2021-06-17 stsp error = close_err;
4536 ec142235 2019-03-07 stsp if (worktree)
4537 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4538 7cd52833 2022-06-23 thomas if (pack_fds) {
4539 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4540 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4541 7cd52833 2022-06-23 thomas if (error == NULL)
4542 7cd52833 2022-06-23 thomas error = pack_err;
4544 51a10b52 2020-12-26 stsp tog_free_refs();
4545 80ddbec8 2018-04-29 stsp return error;
4548 4ed7e80c 2018-05-20 stsp __dead static void
4549 9f7d7167 2018-04-29 stsp usage_diff(void)
4552 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4553 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4557 6d17833f 2019-11-08 stsp static int
4558 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4559 41605754 2020-11-12 stsp regmatch_t *regmatch)
4561 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4564 ef20f542 2022-06-26 thomas static struct tog_color *
4565 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4567 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4569 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4570 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4571 6d17833f 2019-11-08 stsp return tc;
4574 6d17833f 2019-11-08 stsp return NULL;
4577 4ed7e80c 2018-05-20 stsp static const struct got_error *
4578 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4579 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4581 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4582 666f7b10 2022-06-23 thomas char *exstr = NULL;
4583 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4584 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4585 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4586 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4588 41605754 2020-11-12 stsp *wtotal = 0;
4590 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4591 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4593 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4594 666f7b10 2022-06-23 thomas if (err)
4595 666f7b10 2022-06-23 thomas return err;
4597 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4598 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4599 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4600 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4601 666f7b10 2022-06-23 thomas goto done;
4603 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4604 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4605 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4606 cbea3800 2022-06-23 thomas goto done;
4608 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4609 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4610 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4611 cbea3800 2022-06-23 thomas goto done;
4614 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4615 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4616 cbea3800 2022-06-23 thomas col_tab_align, 1);
4617 cbea3800 2022-06-23 thomas if (err)
4618 cbea3800 2022-06-23 thomas goto done;
4619 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4620 05171be4 2022-06-23 thomas if (n) {
4621 cbea3800 2022-06-23 thomas free(wline);
4622 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4623 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4624 cbea3800 2022-06-23 thomas if (err)
4625 cbea3800 2022-06-23 thomas goto done;
4626 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4627 cbea3800 2022-06-23 thomas wlimit -= width;
4628 cbea3800 2022-06-23 thomas *wtotal += width;
4631 41605754 2020-11-12 stsp if (wlimit > 0) {
4632 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4633 cbea3800 2022-06-23 thomas size_t wlen;
4635 cbea3800 2022-06-23 thomas free(wline);
4636 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4637 cbea3800 2022-06-23 thomas col_tab_align, 1);
4638 cbea3800 2022-06-23 thomas if (err)
4639 cbea3800 2022-06-23 thomas goto done;
4640 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4641 cbea3800 2022-06-23 thomas while (i < wlen) {
4642 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4643 cbea3800 2022-06-23 thomas if (width == -1) {
4644 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4645 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4646 cbea3800 2022-06-23 thomas goto done;
4648 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4650 1c5e5faa 2022-06-23 thomas w += width;
4653 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4654 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4655 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4656 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4657 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4658 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4659 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4663 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4664 cbea3800 2022-06-23 thomas free(wline);
4665 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4666 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4667 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4668 cbea3800 2022-06-23 thomas col_tab_align, 1);
4669 cbea3800 2022-06-23 thomas if (err)
4670 cbea3800 2022-06-23 thomas goto done;
4671 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4672 cbea3800 2022-06-23 thomas } else {
4673 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4674 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4675 cbea3800 2022-06-23 thomas if (err)
4676 cbea3800 2022-06-23 thomas goto done;
4677 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4679 cbea3800 2022-06-23 thomas *wtotal += width2;
4682 05171be4 2022-06-23 thomas free(wline);
4683 666f7b10 2022-06-23 thomas free(exstr);
4684 cbea3800 2022-06-23 thomas free(seg0);
4685 cbea3800 2022-06-23 thomas free(seg1);
4686 cbea3800 2022-06-23 thomas free(seg2);
4687 cbea3800 2022-06-23 thomas return err;
4690 07dd3ed3 2022-08-06 thomas static int
4691 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4693 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4694 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4696 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4697 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4699 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4700 fc2737d5 2022-09-15 thomas selected = first;
4701 fc2737d5 2022-09-15 thomas eof = &s->eof;
4702 fc2737d5 2022-09-15 thomas f = s->f;
4703 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4704 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4706 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4707 07dd3ed3 2022-08-06 thomas selected = first;
4708 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4709 07dd3ed3 2022-08-06 thomas f = s->f;
4710 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4711 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4713 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4714 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4715 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4716 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4718 07dd3ed3 2022-08-06 thomas return 0;
4720 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4721 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4722 07dd3ed3 2022-08-06 thomas return 0;
4723 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4724 07dd3ed3 2022-08-06 thomas rewind(f);
4725 07dd3ed3 2022-08-06 thomas *eof = 0;
4726 07dd3ed3 2022-08-06 thomas *first = 1;
4727 07dd3ed3 2022-08-06 thomas *lineno = 0;
4728 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4729 07dd3ed3 2022-08-06 thomas return 0;
4732 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4733 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4734 07dd3ed3 2022-08-06 thomas view->gline = 0;
4736 07dd3ed3 2022-08-06 thomas return 1;
4739 41605754 2020-11-12 stsp static const struct got_error *
4740 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4742 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4743 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4744 61e69b96 2018-05-20 stsp const struct got_error *err;
4745 82c78e96 2022-08-06 thomas int nprinted = 0;
4746 b304db33 2018-05-20 stsp char *line;
4747 826082fe 2020-12-10 stsp size_t linesize = 0;
4748 826082fe 2020-12-10 stsp ssize_t linelen;
4749 61e69b96 2018-05-20 stsp wchar_t *wline;
4750 e0b650dd 2018-05-20 stsp int width;
4751 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4752 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4753 fe621944 2020-11-10 stsp off_t line_offset;
4755 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4756 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4757 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4758 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4760 f7d12f7e 2018-08-01 stsp werase(view->window);
4762 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4763 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4765 a3404814 2018-09-02 stsp if (header) {
4766 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4767 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4768 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4770 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4771 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4772 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4774 135a2da0 2020-11-11 stsp free(line);
4776 a3404814 2018-09-02 stsp return err;
4778 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4779 a3404814 2018-09-02 stsp wstandout(view->window);
4780 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4781 e54cc94a 2020-11-11 stsp free(wline);
4782 e54cc94a 2020-11-11 stsp wline = NULL;
4783 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4784 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4785 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4786 a3404814 2018-09-02 stsp wstandend(view->window);
4788 a3404814 2018-09-02 stsp if (max_lines <= 1)
4789 a3404814 2018-09-02 stsp return NULL;
4790 a3404814 2018-09-02 stsp max_lines--;
4793 89f1a395 2020-12-01 naddy s->eof = 0;
4794 05171be4 2022-06-23 thomas view->maxx = 0;
4795 826082fe 2020-12-10 stsp line = NULL;
4796 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4797 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4798 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4800 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4801 826082fe 2020-12-10 stsp if (linelen == -1) {
4802 826082fe 2020-12-10 stsp if (feof(s->f)) {
4803 826082fe 2020-12-10 stsp s->eof = 1;
4806 826082fe 2020-12-10 stsp free(line);
4807 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4810 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4811 07dd3ed3 2022-08-06 thomas continue;
4812 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4813 07dd3ed3 2022-08-06 thomas continue;
4814 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4815 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4817 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4818 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4819 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4820 cbea3800 2022-06-23 thomas if (err) {
4821 cbea3800 2022-06-23 thomas free(line);
4822 cbea3800 2022-06-23 thomas return err;
4824 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4825 cbea3800 2022-06-23 thomas free(wline);
4826 cbea3800 2022-06-23 thomas wline = NULL;
4828 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4829 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4830 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4831 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4832 07dd3ed3 2022-08-06 thomas if (attr)
4833 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4834 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4835 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4836 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4837 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4838 41605754 2020-11-12 stsp if (err) {
4839 41605754 2020-11-12 stsp free(line);
4840 41605754 2020-11-12 stsp return err;
4843 f1c0ec19 2022-06-23 thomas int skip;
4844 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4845 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4846 f1c0ec19 2022-06-23 thomas if (err) {
4847 f1c0ec19 2022-06-23 thomas free(line);
4848 f1c0ec19 2022-06-23 thomas return err;
4850 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4851 f1c0ec19 2022-06-23 thomas free(wline);
4852 41605754 2020-11-12 stsp wline = NULL;
4854 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4855 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4856 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4857 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4858 07dd3ed3 2022-08-06 thomas } else {
4859 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4860 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4862 07dd3ed3 2022-08-06 thomas if (attr)
4863 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4864 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4865 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4867 826082fe 2020-12-10 stsp free(line);
4868 fe621944 2020-11-10 stsp if (nprinted >= 1)
4869 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4870 89f1a395 2020-12-01 naddy (nprinted - 1);
4872 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4874 a5d43cac 2022-07-01 thomas view_border(view);
4876 89f1a395 2020-12-01 naddy if (s->eof) {
4877 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4878 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4879 c3e9aa98 2019-05-13 jcs nprinted++;
4882 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4883 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4885 c3e9aa98 2019-05-13 jcs return err;
4888 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4889 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4890 e54cc94a 2020-11-11 stsp free(wline);
4891 e54cc94a 2020-11-11 stsp wline = NULL;
4892 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4895 26ed57b2 2018-05-19 stsp return NULL;
4898 abd2672a 2018-12-23 stsp static char *
4899 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4901 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4902 09867e48 2019-08-13 stsp char *p, *s;
4904 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4905 09867e48 2019-08-13 stsp if (tm == NULL)
4906 09867e48 2019-08-13 stsp return NULL;
4907 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4908 09867e48 2019-08-13 stsp if (s == NULL)
4909 09867e48 2019-08-13 stsp return NULL;
4910 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4912 abd2672a 2018-12-23 stsp *p = '\0';
4916 4ed7e80c 2018-05-20 stsp static const struct got_error *
4917 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4918 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4920 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4922 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4923 fe621944 2020-11-10 stsp if (p == NULL)
4924 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4925 82c78e96 2022-08-06 thomas *lines = p;
4926 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4927 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4928 fe621944 2020-11-10 stsp (*nlines)++;
4930 fe621944 2020-11-10 stsp return NULL;
4933 fe621944 2020-11-10 stsp static const struct got_error *
4934 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4935 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
4937 be97ab03 2023-01-19 thomas struct got_diff_line *p;
4938 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
4939 be97ab03 2023-01-19 thomas size_t i, r;
4941 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
4942 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
4944 be97ab03 2023-01-19 thomas for (;;) {
4945 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
4946 be97ab03 2023-01-19 thomas if (r == 0) {
4947 be97ab03 2023-01-19 thomas if (ferror(src))
4948 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
4949 be97ab03 2023-01-19 thomas if (feof(src))
4952 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
4953 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
4956 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
4957 9382c10a 2023-02-23 thomas return NULL;
4960 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
4961 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
4962 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
4964 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
4965 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
4966 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4968 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
4969 9382c10a 2023-02-23 thomas --s_nlines;
4970 9382c10a 2023-02-23 thomas ++s_lines;
4974 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4975 be97ab03 2023-01-19 thomas if (p == NULL) {
4976 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
4977 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
4980 be97ab03 2023-01-19 thomas *d_lines = p;
4982 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4983 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
4985 be97ab03 2023-01-19 thomas return NULL;
4988 be97ab03 2023-01-19 thomas static const struct got_error *
4989 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4990 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4991 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4992 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
4994 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4995 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4996 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4997 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4998 45d799e2 2018-12-23 stsp time_t committer_time;
4999 45d799e2 2018-12-23 stsp const char *author, *committer;
5000 8b473291 2019-02-21 stsp char *refs_str = NULL;
5001 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
5002 fe621944 2020-11-10 stsp off_t outoff = 0;
5005 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, commit_id, repo);
5006 00ddec2f 2023-05-17 thomas if (err)
5007 00ddec2f 2023-05-17 thomas return err;
5009 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5011 abd2672a 2018-12-23 stsp return err;
5013 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
5014 15a94983 2018-12-23 stsp if (err) {
5015 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
5016 15a94983 2018-12-23 stsp goto done;
5019 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5021 fe621944 2020-11-10 stsp goto done;
5023 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5024 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
5025 fe621944 2020-11-10 stsp if (n < 0) {
5026 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5027 abd2672a 2018-12-23 stsp goto done;
5029 fe621944 2020-11-10 stsp outoff += n;
5030 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5032 fe621944 2020-11-10 stsp goto done;
5034 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
5035 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
5036 fe621944 2020-11-10 stsp if (n < 0) {
5037 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5038 abd2672a 2018-12-23 stsp goto done;
5040 fe621944 2020-11-10 stsp outoff += n;
5041 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5043 fe621944 2020-11-10 stsp goto done;
5045 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
5046 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
5047 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
5048 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
5049 fe621944 2020-11-10 stsp if (n < 0) {
5050 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5051 fe621944 2020-11-10 stsp goto done;
5053 fe621944 2020-11-10 stsp outoff += n;
5054 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5055 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
5057 fe621944 2020-11-10 stsp goto done;
5059 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
5060 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
5061 48830929 2023-01-07 thomas if (datestr) {
5062 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
5063 fe621944 2020-11-10 stsp if (n < 0) {
5064 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5065 fe621944 2020-11-10 stsp goto done;
5067 fe621944 2020-11-10 stsp outoff += n;
5068 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5069 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
5071 fe621944 2020-11-10 stsp goto done;
5073 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
5074 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
5075 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
5076 ac4dc263 2021-09-24 thomas int pn = 1;
5077 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
5078 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
5079 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
5080 ac4dc263 2021-09-24 thomas if (err)
5081 ac4dc263 2021-09-24 thomas goto done;
5082 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5083 ac4dc263 2021-09-24 thomas if (n < 0) {
5084 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
5085 ac4dc263 2021-09-24 thomas goto done;
5087 ac4dc263 2021-09-24 thomas outoff += n;
5088 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5089 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
5090 ac4dc263 2021-09-24 thomas if (err)
5091 ac4dc263 2021-09-24 thomas goto done;
5092 ac4dc263 2021-09-24 thomas free(id_str);
5093 ac4dc263 2021-09-24 thomas id_str = NULL;
5097 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
5099 5943eee2 2019-08-13 stsp goto done;
5100 fe621944 2020-11-10 stsp s = logmsg;
5101 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
5102 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
5103 fe621944 2020-11-10 stsp if (n < 0) {
5104 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5105 fe621944 2020-11-10 stsp goto done;
5107 fe621944 2020-11-10 stsp outoff += n;
5108 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5109 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
5111 fe621944 2020-11-10 stsp goto done;
5114 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
5115 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5116 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
5118 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5119 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5120 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
5121 fe621944 2020-11-10 stsp if (n < 0) {
5122 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5123 fe621944 2020-11-10 stsp goto done;
5125 fe621944 2020-11-10 stsp outoff += n;
5126 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5127 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
5129 fe621944 2020-11-10 stsp goto done;
5132 0208f208 2020-05-05 stsp fputc('\n', outfile);
5134 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5135 772fcad5 2023-01-07 thomas if (err)
5136 772fcad5 2023-01-07 thomas goto done;
5138 772fcad5 2023-01-07 thomas n = fprintf(outfile,
5139 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5140 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5141 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5142 772fcad5 2023-01-07 thomas if (n < 0) {
5143 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
5144 772fcad5 2023-01-07 thomas goto done;
5146 772fcad5 2023-01-07 thomas outoff += n;
5147 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5148 772fcad5 2023-01-07 thomas if (err)
5149 772fcad5 2023-01-07 thomas goto done;
5151 772fcad5 2023-01-07 thomas fputc('\n', outfile);
5152 772fcad5 2023-01-07 thomas outoff++;
5153 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5155 abd2672a 2018-12-23 stsp free(id_str);
5156 5943eee2 2019-08-13 stsp free(logmsg);
5157 8b473291 2019-02-21 stsp free(refs_str);
5158 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5159 7510f233 2020-08-09 stsp if (err) {
5160 82c78e96 2022-08-06 thomas free(*lines);
5161 82c78e96 2022-08-06 thomas *lines = NULL;
5162 ae6a6978 2020-08-09 stsp *nlines = 0;
5164 fe621944 2020-11-10 stsp return err;
5167 abd2672a 2018-12-23 stsp static const struct got_error *
5168 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5170 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5171 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
5172 15a94983 2018-12-23 stsp int obj_type;
5173 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
5174 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
5176 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
5178 82c78e96 2022-08-06 thomas free(s->lines);
5179 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
5180 82c78e96 2022-08-06 thomas if (s->lines == NULL)
5181 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5182 fe621944 2020-11-10 stsp s->nlines = 0;
5184 511a516b 2018-05-19 stsp f = got_opentemp();
5185 48ae06ee 2018-10-18 stsp if (f == NULL) {
5186 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5187 48ae06ee 2018-10-18 stsp goto done;
5189 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
5190 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
5191 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
5192 be97ab03 2023-01-19 thomas goto done;
5194 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5195 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5196 fb43ecf1 2019-02-11 stsp goto done;
5200 15a94983 2018-12-23 stsp if (s->id1)
5201 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5203 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5205 15a94983 2018-12-23 stsp goto done;
5207 15a94983 2018-12-23 stsp switch (obj_type) {
5208 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5209 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5210 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5211 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
5212 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5215 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5216 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5217 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5218 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5219 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
5221 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5222 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5223 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5224 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5225 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5226 be97ab03 2023-01-19 thomas size_t nlines = 0;
5227 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
5228 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
5229 be97ab03 2023-01-19 thomas &changed_paths,
5230 be97ab03 2023-01-19 thomas s->ignore_whitespace,
5231 be97ab03 2023-01-19 thomas s->force_text_diff,
5232 be97ab03 2023-01-19 thomas tog_diff_algo
5235 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
5236 be97ab03 2023-01-19 thomas if (lines == NULL) {
5237 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
5238 be97ab03 2023-01-19 thomas goto done;
5241 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
5242 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
5243 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5244 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5245 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5246 be97ab03 2023-01-19 thomas if (err)
5249 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5251 3ffacbe1 2020-02-02 tracey goto done;
5252 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5253 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5254 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5255 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
5256 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
5257 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5258 f44b1f58 2020-02-02 tracey if (err)
5259 f44b1f58 2020-02-02 tracey goto done;
5260 f44b1f58 2020-02-02 tracey } else {
5261 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5262 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5263 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5264 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
5265 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
5266 772fcad5 2023-01-07 thomas s->ignore_whitespace,
5267 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5268 f44b1f58 2020-02-02 tracey if (err)
5269 f44b1f58 2020-02-02 tracey goto done;
5274 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5276 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5277 be97ab03 2023-01-19 thomas lines, nlines);
5281 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5285 be97ab03 2023-01-19 thomas free(lines);
5286 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5287 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5288 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5289 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5290 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
5291 48ae06ee 2018-10-18 stsp return err;
5294 f5215bb9 2019-02-22 stsp static void
5295 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5297 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5298 f5215bb9 2019-02-22 stsp update_panels();
5299 f5215bb9 2019-02-22 stsp doupdate();
5302 f44b1f58 2020-02-02 tracey static const struct got_error *
5303 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5305 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5307 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5308 f44b1f58 2020-02-02 tracey return NULL;
5311 2a7d3cd7 2022-09-17 thomas static void
5312 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5313 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
5315 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
5317 2a7d3cd7 2022-09-17 thomas *f = s->f;
5318 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
5319 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
5320 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
5321 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
5322 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
5323 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
5326 fc2737d5 2022-09-15 thomas static const struct got_error *
5327 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
5329 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5330 fc2737d5 2022-09-15 thomas FILE *f;
5331 f44b1f58 2020-02-02 tracey int lineno;
5332 78eecffc 2022-06-23 thomas char *line = NULL;
5333 826082fe 2020-12-10 stsp size_t linesize = 0;
5334 826082fe 2020-12-10 stsp ssize_t linelen;
5335 fc2737d5 2022-09-15 thomas off_t *line_offsets;
5336 fc2737d5 2022-09-15 thomas size_t nlines = 0;
5337 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
5339 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
5340 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
5341 2a7d3cd7 2022-09-17 thomas "view search not supported");
5342 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5343 fc2737d5 2022-09-15 thomas &match, &selected);
5345 f44b1f58 2020-02-02 tracey if (!view->searching) {
5346 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5347 f44b1f58 2020-02-02 tracey return NULL;
5350 fc2737d5 2022-09-15 thomas if (*match) {
5351 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5352 7303e8c8 2023-04-04 thomas lineno = *first + 1;
5354 7303e8c8 2023-04-04 thomas lineno = *first - 1;
5356 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
5358 f44b1f58 2020-02-02 tracey while (1) {
5359 f44b1f58 2020-02-02 tracey off_t offset;
5361 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
5362 fc2737d5 2022-09-15 thomas if (*match == 0) {
5363 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5367 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5368 f44b1f58 2020-02-02 tracey lineno = 1;
5370 fc2737d5 2022-09-15 thomas lineno = nlines;
5373 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5374 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5375 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5376 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5377 f44b1f58 2020-02-02 tracey free(line);
5378 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5380 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5381 78eecffc 2022-06-23 thomas if (linelen != -1) {
5382 78eecffc 2022-06-23 thomas char *exstr;
5383 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5384 78eecffc 2022-06-23 thomas if (err)
5386 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5387 78eecffc 2022-06-23 thomas &view->regmatch)) {
5388 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5389 fc2737d5 2022-09-15 thomas *match = lineno;
5390 78eecffc 2022-06-23 thomas free(exstr);
5393 78eecffc 2022-06-23 thomas free(exstr);
5395 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5396 f44b1f58 2020-02-02 tracey lineno++;
5398 f44b1f58 2020-02-02 tracey lineno--;
5400 826082fe 2020-12-10 stsp free(line);
5402 fc2737d5 2022-09-15 thomas if (*match) {
5403 fc2737d5 2022-09-15 thomas *first = *match;
5404 fc2737d5 2022-09-15 thomas *selected = 1;
5407 05171be4 2022-06-23 thomas return err;
5410 48ae06ee 2018-10-18 stsp static const struct got_error *
5411 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5413 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5414 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5416 a0f32f33 2022-06-13 thomas free(s->id1);
5417 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5418 a0f32f33 2022-06-13 thomas free(s->id2);
5419 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5420 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5421 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5422 a0f32f33 2022-06-13 thomas s->f = NULL;
5423 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5424 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5425 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5426 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5427 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5428 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5429 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5430 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5431 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5432 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5433 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5434 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5435 82c78e96 2022-08-06 thomas free(s->lines);
5436 82c78e96 2022-08-06 thomas s->lines = NULL;
5437 a0f32f33 2022-06-13 thomas s->nlines = 0;
5438 a0f32f33 2022-06-13 thomas return err;
5441 a0f32f33 2022-06-13 thomas static const struct got_error *
5442 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5443 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5444 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5445 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5447 48ae06ee 2018-10-18 stsp const struct got_error *err;
5448 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5450 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5451 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5452 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5454 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5455 51b7e1c3 2023-04-14 thomas int type1, type2;
5457 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type1, repo, id1);
5458 51b7e1c3 2023-04-14 thomas if (err)
5459 51b7e1c3 2023-04-14 thomas goto done;
5460 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type2, repo, id2);
5461 51b7e1c3 2023-04-14 thomas if (err)
5462 51b7e1c3 2023-04-14 thomas goto done;
5464 51b7e1c3 2023-04-14 thomas if (type1 != type2) {
5465 51b7e1c3 2023-04-14 thomas err = got_error(GOT_ERR_OBJ_TYPE);
5466 51b7e1c3 2023-04-14 thomas goto done;
5469 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5470 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5471 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5472 f44b1f58 2020-02-02 tracey s->repo = repo;
5473 f44b1f58 2020-02-02 tracey s->id1 = id1;
5474 f44b1f58 2020-02-02 tracey s->id2 = id2;
5475 3dbaef42 2020-11-24 stsp s->label1 = label1;
5476 3dbaef42 2020-11-24 stsp s->label2 = label2;
5478 15a94983 2018-12-23 stsp if (id1) {
5479 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5480 51b7e1c3 2023-04-14 thomas if (s->id1 == NULL) {
5481 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
5482 51b7e1c3 2023-04-14 thomas goto done;
5485 5465d566 2020-02-01 tracey s->id1 = NULL;
5487 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5488 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5489 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5490 a0f32f33 2022-06-13 thomas goto done;
5493 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5494 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5495 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5496 9a1d7435 2022-06-23 thomas goto done;
5499 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5500 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5501 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5502 a0f32f33 2022-06-13 thomas goto done;
5505 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5506 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5507 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5508 19a6a6b5 2022-07-01 thomas goto done;
5511 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5512 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5513 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5514 19a6a6b5 2022-07-01 thomas goto done;
5517 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5518 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5519 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5520 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5521 5465d566 2020-02-01 tracey s->repo = repo;
5523 557d3365 2023-04-14 thomas if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5526 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5527 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5528 6568f0aa 2022-08-06 thomas if (rc != ERR)
5529 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5530 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5531 6568f0aa 2022-08-06 thomas if (rc != ERR)
5532 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5533 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5534 6568f0aa 2022-08-06 thomas if (rc != ERR)
5535 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5536 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5537 6568f0aa 2022-08-06 thomas if (rc != ERR)
5538 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5539 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5540 6568f0aa 2022-08-06 thomas if (rc != ERR)
5541 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5542 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5543 6568f0aa 2022-08-06 thomas if (rc != ERR)
5544 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5545 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5546 6568f0aa 2022-08-06 thomas if (rc != ERR)
5547 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5548 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5549 6568f0aa 2022-08-06 thomas if (rc != ERR)
5550 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5551 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5552 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5553 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5554 a0f32f33 2022-06-13 thomas goto done;
5558 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5559 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5560 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5561 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5563 5465d566 2020-02-01 tracey err = create_diff(s);
5565 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5566 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5567 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5568 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5569 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5570 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5571 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5573 51b7e1c3 2023-04-14 thomas if (err) {
5574 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
5575 51b7e1c3 2023-04-14 thomas close_diff_view(view);
5576 51b7e1c3 2023-04-14 thomas view_close(view);
5578 e5a0f69f 2018-08-18 stsp return err;
5581 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5582 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5584 a3404814 2018-09-02 stsp const struct got_error *err;
5585 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5586 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5587 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5589 a3404814 2018-09-02 stsp if (s->id1) {
5590 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5592 a3404814 2018-09-02 stsp return err;
5593 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5595 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5597 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5599 a3404814 2018-09-02 stsp return err;
5600 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5602 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5603 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5604 a3404814 2018-09-02 stsp free(id_str1);
5605 a3404814 2018-09-02 stsp free(id_str2);
5606 a3404814 2018-09-02 stsp return err;
5608 a3404814 2018-09-02 stsp free(id_str1);
5609 a3404814 2018-09-02 stsp free(id_str2);
5611 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5612 267bb3b8 2021-08-01 stsp free(header);
5613 267bb3b8 2021-08-01 stsp return err;
5616 15a087fe 2019-02-21 stsp static const struct got_error *
5617 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5618 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5620 d7a04538 2019-02-21 stsp const struct got_error *err;
5621 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5622 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5623 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5625 15a087fe 2019-02-21 stsp free(s->id2);
5626 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5627 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5628 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5630 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5632 d7a04538 2019-02-21 stsp return err;
5633 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5634 15a087fe 2019-02-21 stsp free(s->id1);
5635 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5636 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5637 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5638 15a087fe 2019-02-21 stsp return NULL;
5641 0cf4efb1 2018-09-29 stsp static const struct got_error *
5642 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5644 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5646 adf4c9e0 2022-07-03 thomas view->count = 0;
5647 adf4c9e0 2022-07-03 thomas wclear(view->window);
5648 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5649 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5650 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5651 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5652 adf4c9e0 2022-07-03 thomas return create_diff(s);
5655 82c78e96 2022-08-06 thomas static void
5656 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5658 82c78e96 2022-08-06 thomas int start, i;
5660 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5662 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5663 82c78e96 2022-08-06 thomas if (i == 0)
5664 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5665 82c78e96 2022-08-06 thomas if (--i == start)
5666 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5669 82c78e96 2022-08-06 thomas s->selected_line = 1;
5670 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5673 82c78e96 2022-08-06 thomas static void
5674 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5676 82c78e96 2022-08-06 thomas int start, i;
5678 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5680 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5681 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5683 82c78e96 2022-08-06 thomas if (++i == start)
5684 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5687 82c78e96 2022-08-06 thomas s->selected_line = 1;
5688 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5691 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5692 4fc71f3b 2022-07-12 thomas int, int, int);
5693 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5694 4fc71f3b 2022-07-12 thomas int, int);
5696 adf4c9e0 2022-07-03 thomas static const struct got_error *
5697 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5699 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5700 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5701 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5702 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5703 826082fe 2020-12-10 stsp char *line = NULL;
5704 826082fe 2020-12-10 stsp size_t linesize = 0;
5705 826082fe 2020-12-10 stsp ssize_t linelen;
5706 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5708 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5710 e5a0f69f 2018-08-18 stsp switch (ch) {
5711 05171be4 2022-06-23 thomas case '0':
5712 05171be4 2022-06-23 thomas case '$':
5713 05171be4 2022-06-23 thomas case KEY_RIGHT:
5714 05171be4 2022-06-23 thomas case 'l':
5715 05171be4 2022-06-23 thomas case KEY_LEFT:
5716 05171be4 2022-06-23 thomas case 'h':
5717 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5721 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5722 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5723 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5724 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5725 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5727 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5728 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5729 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5730 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5731 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5733 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5736 912a3f79 2021-08-30 j case KEY_HOME:
5737 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5738 07b0611c 2022-06-23 thomas view->count = 0;
5741 912a3f79 2021-08-30 j case KEY_END:
5742 07b0611c 2022-06-23 thomas view->count = 0;
5746 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5750 1e37a5c2 2019-05-12 jcs case KEY_UP:
5751 f7140bf5 2021-10-17 thomas case CTRL('p'):
5752 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5753 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5755 07b0611c 2022-06-23 thomas view->count = 0;
5757 70f17a53 2022-06-13 thomas case CTRL('u'):
5758 23427b14 2022-06-23 thomas case 'u':
5759 70f17a53 2022-06-13 thomas nscroll /= 2;
5760 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5761 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5762 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5763 1c5e5faa 2022-06-23 thomas case 'b':
5764 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5765 07b0611c 2022-06-23 thomas view->count = 0;
5769 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5770 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5773 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5774 f7140bf5 2021-10-17 thomas case CTRL('n'):
5775 1e37a5c2 2019-05-12 jcs if (!s->eof)
5776 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5778 07b0611c 2022-06-23 thomas view->count = 0;
5780 70f17a53 2022-06-13 thomas case CTRL('d'):
5781 23427b14 2022-06-23 thomas case 'd':
5782 70f17a53 2022-06-13 thomas nscroll /= 2;
5783 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5784 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5785 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5786 1c5e5faa 2022-06-23 thomas case 'f':
5788 07b0611c 2022-06-23 thomas if (s->eof) {
5789 07b0611c 2022-06-23 thomas view->count = 0;
5793 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5794 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5795 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5796 826082fe 2020-12-10 stsp if (linelen == -1) {
5797 826082fe 2020-12-10 stsp if (feof(s->f)) {
5798 826082fe 2020-12-10 stsp s->eof = 1;
5800 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5804 826082fe 2020-12-10 stsp free(line);
5806 82c78e96 2022-08-06 thomas case '(':
5807 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5809 82c78e96 2022-08-06 thomas case ')':
5810 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5812 82c78e96 2022-08-06 thomas case '{':
5813 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5815 82c78e96 2022-08-06 thomas case '}':
5816 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5819 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5820 1e37a5c2 2019-05-12 jcs s->diff_context--;
5821 aa61903a 2021-12-31 thomas s->matched_line = 0;
5822 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5823 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5824 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5825 27829c9e 2020-11-21 stsp s->nlines) {
5826 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5827 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5830 07b0611c 2022-06-23 thomas view->count = 0;
5833 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5834 1e37a5c2 2019-05-12 jcs s->diff_context++;
5835 aa61903a 2021-12-31 thomas s->matched_line = 0;
5836 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5837 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5839 07b0611c 2022-06-23 thomas view->count = 0;
5843 777aae21 2022-07-20 thomas case 'K':
5845 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5846 4fc71f3b 2022-07-12 thomas case '>':
5847 4fc71f3b 2022-07-12 thomas case '.':
5848 777aae21 2022-07-20 thomas case 'J':
5849 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5850 07b0611c 2022-06-23 thomas view->count = 0;
5853 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5855 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5856 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5857 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5859 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5860 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5861 4fc71f3b 2022-07-12 thomas if (err)
5863 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5865 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5868 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5869 4fc71f3b 2022-07-12 thomas if (err)
5871 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5872 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5873 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5875 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5876 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5877 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5879 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5880 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5881 4fc71f3b 2022-07-12 thomas if (err)
5883 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5885 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5887 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5888 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5889 4fc71f3b 2022-07-12 thomas bs->selected_line);
5890 4fc71f3b 2022-07-12 thomas if (id == NULL)
5893 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5896 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5897 4fc71f3b 2022-07-12 thomas if (err)
5900 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5901 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5902 aa61903a 2021-12-31 thomas s->matched_line = 0;
5903 05171be4 2022-06-23 thomas view->x = 0;
5905 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5906 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5909 07b0611c 2022-06-23 thomas view->count = 0;
5913 bcbd79e2 2018-08-19 stsp return err;
5916 4ed7e80c 2018-05-20 stsp static const struct got_error *
5917 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5919 1d98034b 2023-04-14 thomas const struct got_error *error;
5920 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5921 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5922 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5923 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5924 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5925 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5926 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5927 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5928 3dbaef42 2020-11-24 stsp const char *errstr;
5929 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5930 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5932 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5933 26ed57b2 2018-05-19 stsp switch (ch) {
5935 64453f7e 2020-11-21 stsp force_text_diff = 1;
5938 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5940 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5941 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5942 8f666e67 2022-02-12 thomas errstr, errstr);
5944 09b5bff8 2020-02-23 naddy case 'r':
5945 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5946 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5947 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5949 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5952 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5955 17020d27 2019-03-07 stsp usage_diff();
5956 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5960 26ed57b2 2018-05-19 stsp argc -= optind;
5961 26ed57b2 2018-05-19 stsp argv += optind;
5963 26ed57b2 2018-05-19 stsp if (argc == 0) {
5964 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5965 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5966 15a94983 2018-12-23 stsp id_str1 = argv[0];
5967 15a94983 2018-12-23 stsp id_str2 = argv[1];
5969 26ed57b2 2018-05-19 stsp usage_diff();
5971 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5972 7cd52833 2022-06-23 thomas if (error)
5973 7cd52833 2022-06-23 thomas goto done;
5975 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5976 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5977 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5978 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5979 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5980 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5981 c156c7a4 2020-12-18 stsp goto done;
5982 a273ac94 2020-02-23 naddy if (worktree)
5983 a273ac94 2020-02-23 naddy repo_path =
5984 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5986 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5987 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5988 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5989 c156c7a4 2020-12-18 stsp goto done;
5993 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5994 eb6600df 2019-01-04 stsp if (error)
5995 eb6600df 2019-01-04 stsp goto done;
5997 557d3365 2023-04-14 thomas init_curses();
5999 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6000 51a10b52 2020-12-26 stsp if (error)
6001 51a10b52 2020-12-26 stsp goto done;
6003 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6004 26ed57b2 2018-05-19 stsp if (error)
6005 26ed57b2 2018-05-19 stsp goto done;
6007 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
6008 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6009 26ed57b2 2018-05-19 stsp if (error)
6010 26ed57b2 2018-05-19 stsp goto done;
6012 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
6013 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6014 26ed57b2 2018-05-19 stsp if (error)
6015 26ed57b2 2018-05-19 stsp goto done;
6017 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6018 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
6019 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6020 ea5e7bb5 2018-08-01 stsp goto done;
6022 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6023 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
6024 5dc9f4bc 2018-08-04 stsp if (error)
6025 5dc9f4bc 2018-08-04 stsp goto done;
6026 557d3365 2023-04-14 thomas error = view_loop(view);
6028 3dbaef42 2020-11-24 stsp free(label1);
6029 3dbaef42 2020-11-24 stsp free(label2);
6030 c02c541e 2019-03-29 stsp free(repo_path);
6031 a273ac94 2020-02-23 naddy free(cwd);
6032 1d0f4054 2021-06-17 stsp if (repo) {
6033 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6034 1d0f4054 2021-06-17 stsp if (error == NULL)
6035 1d0f4054 2021-06-17 stsp error = close_err;
6037 a273ac94 2020-02-23 naddy if (worktree)
6038 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
6039 7cd52833 2022-06-23 thomas if (pack_fds) {
6040 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6041 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6042 7cd52833 2022-06-23 thomas if (error == NULL)
6043 7cd52833 2022-06-23 thomas error = pack_err;
6045 51a10b52 2020-12-26 stsp tog_free_refs();
6046 26ed57b2 2018-05-19 stsp return error;
6049 4ed7e80c 2018-05-20 stsp __dead static void
6050 9f7d7167 2018-04-29 stsp usage_blame(void)
6053 91198554 2022-06-23 thomas fprintf(stderr,
6054 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
6055 9f7d7167 2018-04-29 stsp getprogname());
6059 84451b3e 2018-07-10 stsp struct tog_blame_line {
6060 84451b3e 2018-07-10 stsp int annotated;
6061 84451b3e 2018-07-10 stsp struct got_object_id *id;
6064 4ed7e80c 2018-05-20 stsp static const struct got_error *
6065 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
6067 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6068 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6069 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
6070 84451b3e 2018-07-10 stsp const struct got_error *err;
6071 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
6072 826082fe 2020-12-10 stsp char *line = NULL;
6073 826082fe 2020-12-10 stsp size_t linesize = 0;
6074 826082fe 2020-12-10 stsp ssize_t linelen;
6075 84451b3e 2018-07-10 stsp wchar_t *wline;
6076 27a741e5 2019-09-11 stsp int width;
6077 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
6078 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
6079 ab089a2a 2018-07-12 stsp char *id_str;
6080 11b20872 2019-11-08 stsp struct tog_color *tc;
6082 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
6084 ab089a2a 2018-07-12 stsp return err;
6086 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
6087 f7d12f7e 2018-08-01 stsp werase(view->window);
6089 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
6090 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6091 ab089a2a 2018-07-12 stsp free(id_str);
6092 ab089a2a 2018-07-12 stsp return err;
6095 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6096 ab089a2a 2018-07-12 stsp free(line);
6097 2550e4c3 2018-07-13 stsp line = NULL;
6099 1cae65b4 2019-09-22 stsp return err;
6100 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6101 a3404814 2018-09-02 stsp wstandout(view->window);
6102 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6104 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6105 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6106 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6107 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6109 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6110 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6111 a3404814 2018-09-02 stsp wstandend(view->window);
6112 2550e4c3 2018-07-13 stsp free(wline);
6113 2550e4c3 2018-07-13 stsp wline = NULL;
6115 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
6116 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
6118 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6119 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6122 1134ebde 2023-04-22 thomas rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6124 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_wait");
6125 1134ebde 2023-04-22 thomas tog_io.wait_for_ui = 0;
6128 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6129 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6130 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6131 ab089a2a 2018-07-12 stsp free(id_str);
6132 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6134 ab089a2a 2018-07-12 stsp free(id_str);
6135 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6136 3f60a8ef 2018-07-10 stsp free(line);
6137 2550e4c3 2018-07-13 stsp line = NULL;
6139 3f60a8ef 2018-07-10 stsp return err;
6140 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6141 2550e4c3 2018-07-13 stsp free(wline);
6142 2550e4c3 2018-07-13 stsp wline = NULL;
6143 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6144 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6146 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6147 05171be4 2022-06-23 thomas view->maxx = 0;
6148 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6149 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6150 826082fe 2020-12-10 stsp if (linelen == -1) {
6151 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6152 826082fe 2020-12-10 stsp s->eof = 1;
6155 84451b3e 2018-07-10 stsp free(line);
6156 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6158 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6159 07dd3ed3 2022-08-06 thomas continue;
6160 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
6163 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
6164 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6165 cbea3800 2022-06-23 thomas if (err) {
6166 cbea3800 2022-06-23 thomas free(line);
6167 cbea3800 2022-06-23 thomas return err;
6169 cbea3800 2022-06-23 thomas free(wline);
6170 cbea3800 2022-06-23 thomas wline = NULL;
6171 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
6173 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6174 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6176 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6177 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6178 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6179 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6180 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
6181 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6182 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6183 8d0fe45a 2019-08-12 stsp char *id_str;
6184 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
6185 91198554 2022-06-23 thomas blame_line->id);
6186 8d0fe45a 2019-08-12 stsp if (err) {
6187 8d0fe45a 2019-08-12 stsp free(line);
6188 8d0fe45a 2019-08-12 stsp return err;
6190 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6192 11b20872 2019-11-08 stsp wattr_on(view->window,
6193 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6194 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6196 11b20872 2019-11-08 stsp wattr_off(view->window,
6197 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6198 8d0fe45a 2019-08-12 stsp free(id_str);
6199 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6201 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6202 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6205 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6206 ee41ec32 2018-07-10 stsp prev_id = NULL;
6209 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6210 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6211 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6213 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6214 41605754 2020-11-12 stsp width = 9;
6215 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6216 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6217 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6218 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6219 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
6220 41605754 2020-11-12 stsp if (err) {
6221 41605754 2020-11-12 stsp free(line);
6222 41605754 2020-11-12 stsp return err;
6224 41605754 2020-11-12 stsp width += 9;
6226 923086fd 2022-06-23 thomas int skip;
6227 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
6228 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
6229 923086fd 2022-06-23 thomas if (err) {
6230 923086fd 2022-06-23 thomas free(line);
6231 923086fd 2022-06-23 thomas return err;
6233 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
6234 02bce7e2 2022-06-23 thomas width += 9;
6235 41605754 2020-11-12 stsp free(wline);
6236 41605754 2020-11-12 stsp wline = NULL;
6239 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6240 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6241 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6242 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6244 826082fe 2020-12-10 stsp free(line);
6245 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6247 a5d43cac 2022-07-01 thomas view_border(view);
6249 84451b3e 2018-07-10 stsp return NULL;
6252 84451b3e 2018-07-10 stsp static const struct got_error *
6253 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
6254 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
6256 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6257 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6258 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6259 1a76625f 2018-10-22 stsp int errcode;
6261 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6262 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6263 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6265 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6266 1a76625f 2018-10-22 stsp if (errcode)
6267 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6269 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6270 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6271 d68a0a7d 2018-07-10 stsp goto done;
6274 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6275 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6277 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6278 d68a0a7d 2018-07-10 stsp if (line->annotated)
6279 d68a0a7d 2018-07-10 stsp goto done;
6281 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6282 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6283 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6284 84451b3e 2018-07-10 stsp goto done;
6286 84451b3e 2018-07-10 stsp line->annotated = 1;
6288 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6289 1a76625f 2018-10-22 stsp if (errcode)
6290 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6291 84451b3e 2018-07-10 stsp return err;
6294 84451b3e 2018-07-10 stsp static void *
6295 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6297 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6298 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6299 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6300 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
6301 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
6303 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
6304 9117a7b7 2022-07-01 thomas if (fd1 == -1)
6305 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
6307 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
6308 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
6309 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6310 9117a7b7 2022-07-01 thomas goto done;
6313 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
6314 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
6315 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6316 9117a7b7 2022-07-01 thomas goto done;
6318 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
6319 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
6320 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6321 9117a7b7 2022-07-01 thomas goto done;
6324 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6326 9117a7b7 2022-07-01 thomas goto done;
6328 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6329 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
6330 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6331 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6332 fc06ba56 2019-08-22 stsp err = NULL;
6334 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6335 9117a7b7 2022-07-01 thomas if (errcode) {
6336 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
6337 9117a7b7 2022-07-01 thomas goto done;
6340 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6341 1d0f4054 2021-06-17 stsp if (err == NULL)
6342 1d0f4054 2021-06-17 stsp err = close_err;
6343 c9beca56 2018-07-22 stsp ta->repo = NULL;
6344 c9beca56 2018-07-22 stsp *ta->complete = 1;
6346 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6347 1134ebde 2023-04-22 thomas errcode = pthread_cond_signal(&ta->blame_complete);
6348 1134ebde 2023-04-22 thomas if (errcode && err == NULL)
6349 1134ebde 2023-04-22 thomas err = got_error_set_errno(errcode,
6350 1134ebde 2023-04-22 thomas "pthread_cond_signal");
6353 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6354 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6355 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6358 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6359 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
6360 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6361 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
6362 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
6363 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6364 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
6365 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6367 18430de3 2018-07-10 stsp return (void *)err;
6370 245d91c1 2018-07-12 stsp static struct got_object_id *
6371 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6372 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6374 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6376 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6377 8d0fe45a 2019-08-12 stsp return NULL;
6379 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6380 4fc71f3b 2022-07-12 thomas if (!line->annotated)
6381 4fc71f3b 2022-07-12 thomas return NULL;
6383 4fc71f3b 2022-07-12 thomas return line->id;
6386 4fc71f3b 2022-07-12 thomas static struct got_object_id *
6387 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6388 4fc71f3b 2022-07-12 thomas int lineno)
6390 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
6392 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6393 4fc71f3b 2022-07-12 thomas return NULL;
6395 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6396 245d91c1 2018-07-12 stsp if (!line->annotated)
6397 245d91c1 2018-07-12 stsp return NULL;
6399 245d91c1 2018-07-12 stsp return line->id;
6402 b880a918 2018-07-10 stsp static const struct got_error *
6403 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6405 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6408 245d91c1 2018-07-12 stsp if (blame->thread) {
6409 1a76625f 2018-10-22 stsp int errcode;
6410 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6411 1a76625f 2018-10-22 stsp if (errcode)
6412 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6413 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6414 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6415 1a76625f 2018-10-22 stsp if (errcode)
6416 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6417 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6418 1a76625f 2018-10-22 stsp if (errcode)
6419 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6420 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6421 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6422 245d91c1 2018-07-12 stsp err = NULL;
6423 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6425 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6426 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6427 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6428 1d0f4054 2021-06-17 stsp if (err == NULL)
6429 1d0f4054 2021-06-17 stsp err = close_err;
6430 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6432 245d91c1 2018-07-12 stsp if (blame->f) {
6433 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6434 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6435 245d91c1 2018-07-12 stsp blame->f = NULL;
6437 57670559 2018-12-23 stsp if (blame->lines) {
6438 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6439 57670559 2018-12-23 stsp free(blame->lines[i].id);
6440 57670559 2018-12-23 stsp free(blame->lines);
6441 57670559 2018-12-23 stsp blame->lines = NULL;
6443 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6444 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6445 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6446 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6447 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6448 7cd52833 2022-06-23 thomas if (err == NULL)
6449 7cd52833 2022-06-23 thomas err = pack_err;
6450 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6452 245d91c1 2018-07-12 stsp return err;
6455 245d91c1 2018-07-12 stsp static const struct got_error *
6456 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6458 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6459 fc06ba56 2019-08-22 stsp int *done = arg;
6460 fc06ba56 2019-08-22 stsp int errcode;
6462 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6463 fc06ba56 2019-08-22 stsp if (errcode)
6464 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6465 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6467 fc06ba56 2019-08-22 stsp if (*done)
6468 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6470 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6471 fc06ba56 2019-08-22 stsp if (errcode)
6472 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6473 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6475 fc06ba56 2019-08-22 stsp return err;
6478 fc06ba56 2019-08-22 stsp static const struct got_error *
6479 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6481 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6482 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6483 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6484 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6485 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6486 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6487 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6488 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6489 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6491 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6492 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6494 15a94983 2018-12-23 stsp return err;
6496 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6497 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6498 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6499 f4ae6ddb 2022-07-01 thomas goto done;
6502 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6503 945f9229 2022-04-16 thomas if (err)
6504 945f9229 2022-04-16 thomas goto done;
6506 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6508 84451b3e 2018-07-10 stsp goto done;
6510 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6511 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6512 84451b3e 2018-07-10 stsp goto done;
6515 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6517 a70480e0 2018-06-23 stsp goto done;
6518 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6519 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6520 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6521 84451b3e 2018-07-10 stsp goto done;
6523 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6524 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6526 1fddf795 2021-01-20 stsp goto done;
6527 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6528 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6529 84451b3e 2018-07-10 stsp goto done;
6532 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6533 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6534 b02560ec 2019-08-19 stsp blame->nlines--;
6536 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6537 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6538 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6539 84451b3e 2018-07-10 stsp goto done;
6542 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6544 bd24772e 2018-07-11 stsp goto done;
6545 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6546 7cd52833 2022-06-23 thomas pack_fds);
6547 7cd52833 2022-06-23 thomas if (err)
6548 7cd52833 2022-06-23 thomas goto done;
6550 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6551 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6552 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6553 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6554 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6555 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6556 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6557 245d91c1 2018-07-12 stsp goto done;
6559 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6561 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6562 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6563 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6564 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6565 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6566 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6567 a5388363 2020-12-01 naddy s->blame_complete = 0;
6569 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6570 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6571 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6572 f5a09613 2020-12-13 naddy s->selected_line = 1;
6574 aa61903a 2021-12-31 thomas s->matched_line = 0;
6577 945f9229 2022-04-16 thomas if (commit)
6578 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6579 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6580 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6582 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6583 27d434c2 2018-09-15 stsp free(obj_id);
6585 245d91c1 2018-07-12 stsp stop_blame(blame);
6586 245d91c1 2018-07-12 stsp return err;
6589 245d91c1 2018-07-12 stsp static const struct got_error *
6590 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6591 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6593 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6594 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6596 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6598 c4843652 2019-08-12 stsp s->path = strdup(path);
6599 c4843652 2019-08-12 stsp if (s->path == NULL)
6600 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6602 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6603 c4843652 2019-08-12 stsp if (err) {
6604 c4843652 2019-08-12 stsp free(s->path);
6605 7cbe629d 2018-08-04 stsp return err;
6608 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6609 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6610 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6611 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6612 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6613 fb2756b9 2018-08-04 stsp s->repo = repo;
6614 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6615 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6617 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6618 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6619 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6620 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6622 11b20872 2019-11-08 stsp return err;
6625 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6626 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6627 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6628 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6629 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6630 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6631 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6633 1134ebde 2023-04-22 thomas if (using_mock_io) {
6634 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6637 1134ebde 2023-04-22 thomas rc = pthread_cond_init(&bta->blame_complete, NULL);
6639 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_init");
6642 a5388363 2020-12-01 naddy return run_blame(view);
6645 e5a0f69f 2018-08-18 stsp static const struct got_error *
6646 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6648 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6649 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6651 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6652 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6654 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6655 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6656 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6657 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6658 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6661 1134ebde 2023-04-22 thomas if (using_mock_io) {
6662 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6665 1134ebde 2023-04-22 thomas rc = pthread_cond_destroy(&bta->blame_complete);
6666 1134ebde 2023-04-22 thomas if (rc && err == NULL)
6667 1134ebde 2023-04-22 thomas err = got_error_set_errno(rc, "pthread_cond_destroy");
6670 e5a0f69f 2018-08-18 stsp free(s->path);
6671 11b20872 2019-11-08 stsp free_colors(&s->colors);
6672 e5a0f69f 2018-08-18 stsp return err;
6675 7cbe629d 2018-08-04 stsp static const struct got_error *
6676 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6678 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6680 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6681 6c4c42e0 2019-06-24 stsp return NULL;
6684 2a7d3cd7 2022-09-17 thomas static void
6685 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6686 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6688 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6690 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6691 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6692 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6693 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6694 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6695 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6696 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6699 6c4c42e0 2019-06-24 stsp static const struct got_error *
6700 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6702 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6703 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6704 2b380cc8 2018-10-24 stsp int errcode;
6706 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6707 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6708 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6709 2b380cc8 2018-10-24 stsp if (errcode)
6710 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6712 557d3365 2023-04-14 thomas if (!using_mock_io)
6713 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while annotating */
6716 557d3365 2023-04-14 thomas if (s->blame_complete && !using_mock_io)
6717 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6719 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6721 a5d43cac 2022-07-01 thomas view_border(view);
6722 e5a0f69f 2018-08-18 stsp return err;
6725 e5a0f69f 2018-08-18 stsp static const struct got_error *
6726 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6727 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6729 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6730 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6732 eaeaa612 2022-07-20 thomas *new_view = NULL;
6734 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6735 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6736 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6738 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6739 eaeaa612 2022-07-20 thomas if (err)
6740 eaeaa612 2022-07-20 thomas view_close(log_view);
6742 eaeaa612 2022-07-20 thomas *new_view = log_view;
6744 eaeaa612 2022-07-20 thomas return err;
6747 eaeaa612 2022-07-20 thomas static const struct got_error *
6748 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6750 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6751 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6752 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6753 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6755 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6756 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6757 a5d43cac 2022-07-01 thomas --eos; /* border */
6759 e5a0f69f 2018-08-18 stsp switch (ch) {
6760 05171be4 2022-06-23 thomas case '0':
6761 05171be4 2022-06-23 thomas case '$':
6762 05171be4 2022-06-23 thomas case KEY_RIGHT:
6763 05171be4 2022-06-23 thomas case 'l':
6764 05171be4 2022-06-23 thomas case KEY_LEFT:
6765 05171be4 2022-06-23 thomas case 'h':
6766 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6769 1e37a5c2 2019-05-12 jcs s->done = 1;
6771 4deef56f 2021-09-02 naddy case 'g':
6772 4deef56f 2021-09-02 naddy case KEY_HOME:
6773 4deef56f 2021-09-02 naddy s->selected_line = 1;
6774 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6775 07b0611c 2022-06-23 thomas view->count = 0;
6777 4deef56f 2021-09-02 naddy case 'G':
6778 4deef56f 2021-09-02 naddy case KEY_END:
6779 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6780 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6781 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6783 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6784 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6786 07b0611c 2022-06-23 thomas view->count = 0;
6789 1e37a5c2 2019-05-12 jcs case KEY_UP:
6790 f7140bf5 2021-10-17 thomas case CTRL('p'):
6791 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6792 1e37a5c2 2019-05-12 jcs s->selected_line--;
6793 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6794 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6795 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6797 07b0611c 2022-06-23 thomas view->count = 0;
6799 70f17a53 2022-06-13 thomas case CTRL('u'):
6800 23427b14 2022-06-23 thomas case 'u':
6801 70f17a53 2022-06-13 thomas nscroll /= 2;
6802 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6803 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6804 ea025d1d 2020-02-22 naddy case CTRL('b'):
6805 1c5e5faa 2022-06-23 thomas case 'b':
6806 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6807 07b0611c 2022-06-23 thomas if (view->count > 1)
6808 07b0611c 2022-06-23 thomas nscroll += nscroll;
6809 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6810 07b0611c 2022-06-23 thomas view->count = 0;
6813 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6814 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6816 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6819 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6820 f7140bf5 2021-10-17 thomas case CTRL('n'):
6821 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6822 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6823 1e37a5c2 2019-05-12 jcs s->selected_line++;
6824 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6825 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6827 07b0611c 2022-06-23 thomas view->count = 0;
6829 1c5e5faa 2022-06-23 thomas case 'c':
6830 1e37a5c2 2019-05-12 jcs case 'p': {
6831 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6833 07b0611c 2022-06-23 thomas view->count = 0;
6834 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6835 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6836 1e37a5c2 2019-05-12 jcs if (id == NULL)
6838 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6839 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6840 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6841 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6842 1e37a5c2 2019-05-12 jcs int obj_type;
6843 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6844 1e37a5c2 2019-05-12 jcs s->repo, id);
6847 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6848 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6849 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6850 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6853 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6854 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6855 ec242592 2022-04-22 thomas s->repo, &pid->id);
6856 945f9229 2022-04-16 thomas if (err)
6858 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6859 945f9229 2022-04-16 thomas pcommit, s->path);
6860 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6861 e5a0f69f 2018-08-18 stsp if (err) {
6862 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6863 1e37a5c2 2019-05-12 jcs err = NULL;
6864 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6867 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6869 1e37a5c2 2019-05-12 jcs free(blob_id);
6870 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6871 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6872 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6875 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6876 ec242592 2022-04-22 thomas &pid->id);
6877 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6879 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6880 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6882 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6887 1e37a5c2 2019-05-12 jcs s->done = 1;
6888 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6889 1e37a5c2 2019-05-12 jcs s->done = 0;
6890 1e37a5c2 2019-05-12 jcs if (thread_err)
6892 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6893 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6894 a5388363 2020-12-01 naddy err = run_blame(view);
6899 1c5e5faa 2022-06-23 thomas case 'C': {
6900 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6902 07b0611c 2022-06-23 thomas view->count = 0;
6903 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6904 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6906 1e37a5c2 2019-05-12 jcs s->done = 1;
6907 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6908 1e37a5c2 2019-05-12 jcs s->done = 0;
6909 1e37a5c2 2019-05-12 jcs if (thread_err)
6911 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6912 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6913 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6914 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6915 a5388363 2020-12-01 naddy err = run_blame(view);
6920 2a31b33b 2022-07-23 thomas case 'L':
6921 eaeaa612 2022-07-20 thomas view->count = 0;
6922 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6923 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6924 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6925 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6927 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6928 1e37a5c2 2019-05-12 jcs case '\r': {
6929 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6930 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6931 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6933 07b0611c 2022-06-23 thomas view->count = 0;
6934 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6935 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6936 1e37a5c2 2019-05-12 jcs if (id == NULL)
6938 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6941 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6942 4fc71f3b 2022-07-12 thomas if (*new_view) {
6943 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6944 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6945 4fc71f3b 2022-07-12 thomas if (err)
6947 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6948 4fc71f3b 2022-07-12 thomas } else {
6949 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6950 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6952 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6953 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6954 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6955 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6956 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6960 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6961 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6962 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6964 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6967 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6968 4fc71f3b 2022-07-12 thomas s->selected_line;
6969 4fc71f3b 2022-07-12 thomas if (*new_view)
6970 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6971 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6972 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6973 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6974 a5d43cac 2022-07-01 thomas if (err)
6978 e78dc838 2020-12-04 stsp view->focussed = 0;
6979 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6980 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6981 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6982 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6983 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6984 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6987 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6988 40236d76 2022-06-23 thomas if (err)
6990 e78dc838 2020-12-04 stsp view->focus_child = 1;
6992 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6997 70f17a53 2022-06-13 thomas case CTRL('d'):
6998 23427b14 2022-06-23 thomas case 'd':
6999 70f17a53 2022-06-13 thomas nscroll /= 2;
7000 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7001 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7002 ea025d1d 2020-02-22 naddy case CTRL('f'):
7003 1c5e5faa 2022-06-23 thomas case 'f':
7005 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7006 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
7007 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
7008 07b0611c 2022-06-23 thomas view->count = 0;
7011 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7012 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
7013 70f17a53 2022-06-13 thomas s->selected_line +=
7014 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
7015 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
7017 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
7018 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
7020 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
7021 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
7023 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7024 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
7025 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
7026 1e37a5c2 2019-05-12 jcs view->nlines - 2);
7030 07b0611c 2022-06-23 thomas view->count = 0;
7033 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
7036 adf4c9e0 2022-07-03 thomas static const struct got_error *
7037 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
7039 adf4c9e0 2022-07-03 thomas const struct got_error *err;
7040 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
7042 adf4c9e0 2022-07-03 thomas view->count = 0;
7043 adf4c9e0 2022-07-03 thomas s->done = 1;
7044 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
7045 adf4c9e0 2022-07-03 thomas s->done = 0;
7046 adf4c9e0 2022-07-03 thomas if (err)
7047 adf4c9e0 2022-07-03 thomas return err;
7048 adf4c9e0 2022-07-03 thomas return run_blame(view);
7051 a70480e0 2018-06-23 stsp static const struct got_error *
7052 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
7054 1d98034b 2023-04-14 thomas const struct got_error *error;
7055 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
7056 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
7057 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7058 0587e10c 2020-07-23 stsp char *link_target = NULL;
7059 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7060 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7061 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
7063 51b7e1c3 2023-04-14 thomas struct tog_view *view = NULL;
7064 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7066 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7067 a70480e0 2018-06-23 stsp switch (ch) {
7069 a70480e0 2018-06-23 stsp commit_id_str = optarg;
7072 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
7073 69069811 2018-08-02 stsp if (repo_path == NULL)
7074 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7078 17020d27 2019-03-07 stsp usage_blame();
7079 a70480e0 2018-06-23 stsp /* NOTREACHED */
7083 a70480e0 2018-06-23 stsp argc -= optind;
7084 a70480e0 2018-06-23 stsp argv += optind;
7086 f135c941 2020-02-20 stsp if (argc != 1)
7087 a70480e0 2018-06-23 stsp usage_blame();
7089 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7090 7cd52833 2022-06-23 thomas if (error != NULL)
7091 7cd52833 2022-06-23 thomas goto done;
7093 69069811 2018-08-02 stsp if (repo_path == NULL) {
7094 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7095 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7096 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7097 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7098 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7099 c156c7a4 2020-12-18 stsp goto done;
7100 f135c941 2020-02-20 stsp if (worktree)
7101 eb41ed75 2019-02-05 stsp repo_path =
7102 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7104 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
7105 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7106 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7107 c156c7a4 2020-12-18 stsp goto done;
7111 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7112 c02c541e 2019-03-29 stsp if (error != NULL)
7113 8e94dd5b 2019-01-04 stsp goto done;
7115 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7116 f135c941 2020-02-20 stsp worktree);
7117 c02c541e 2019-03-29 stsp if (error)
7118 92205607 2019-01-04 stsp goto done;
7120 557d3365 2023-04-14 thomas init_curses();
7122 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7123 51a10b52 2020-12-26 stsp if (error)
7124 51a10b52 2020-12-26 stsp goto done;
7126 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7127 eb41ed75 2019-02-05 stsp if (error)
7128 69069811 2018-08-02 stsp goto done;
7130 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
7131 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
7132 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
7133 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7134 a70480e0 2018-06-23 stsp if (error != NULL)
7135 66b4983c 2018-06-23 stsp goto done;
7136 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7137 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7139 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7140 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7142 a19e88aa 2018-06-23 stsp if (error != NULL)
7143 e1cd8fed 2018-08-01 stsp goto done;
7145 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7146 945f9229 2022-04-16 thomas if (error)
7147 945f9229 2022-04-16 thomas goto done;
7149 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7150 945f9229 2022-04-16 thomas commit, repo);
7151 7cbe629d 2018-08-04 stsp if (error)
7152 7cbe629d 2018-08-04 stsp goto done;
7154 4334634c 2023-04-22 thomas view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7155 4334634c 2023-04-22 thomas if (view == NULL) {
7156 4334634c 2023-04-22 thomas error = got_error_from_errno("view_open");
7157 4334634c 2023-04-22 thomas goto done;
7159 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7160 78756c87 2020-11-24 stsp commit_id, repo);
7161 4334634c 2023-04-22 thomas if (error != NULL) {
7162 4334634c 2023-04-22 thomas if (view->close == NULL)
7163 4334634c 2023-04-22 thomas close_blame_view(view);
7164 4334634c 2023-04-22 thomas view_close(view);
7165 0587e10c 2020-07-23 stsp goto done;
7167 12314ad4 2019-08-31 stsp if (worktree) {
7168 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7169 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7170 12314ad4 2019-08-31 stsp worktree = NULL;
7172 557d3365 2023-04-14 thomas error = view_loop(view);
7174 69069811 2018-08-02 stsp free(repo_path);
7175 f135c941 2020-02-20 stsp free(in_repo_path);
7176 0587e10c 2020-07-23 stsp free(link_target);
7177 69069811 2018-08-02 stsp free(cwd);
7178 a70480e0 2018-06-23 stsp free(commit_id);
7179 945f9229 2022-04-16 thomas if (commit)
7180 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7181 eb41ed75 2019-02-05 stsp if (worktree)
7182 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7183 1d0f4054 2021-06-17 stsp if (repo) {
7184 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7185 1d0f4054 2021-06-17 stsp if (error == NULL)
7186 1d0f4054 2021-06-17 stsp error = close_err;
7188 7cd52833 2022-06-23 thomas if (pack_fds) {
7189 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7190 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7191 7cd52833 2022-06-23 thomas if (error == NULL)
7192 7cd52833 2022-06-23 thomas error = pack_err;
7194 51a10b52 2020-12-26 stsp tog_free_refs();
7195 a70480e0 2018-06-23 stsp return error;
7198 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7199 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7201 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7202 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7203 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7204 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7205 86f4aab9 2022-09-09 thomas char *index = NULL;
7206 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7207 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
7208 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7210 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7211 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7212 a5d43cac 2022-07-01 thomas --limit; /* border */
7214 f7d12f7e 2018-08-01 stsp werase(view->window);
7216 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7217 ffd1d5e5 2018-06-23 stsp return NULL;
7219 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7222 ffd1d5e5 2018-06-23 stsp return err;
7223 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7224 a3404814 2018-09-02 stsp wstandout(view->window);
7225 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7227 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7228 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7229 86f4aab9 2022-09-09 thomas free(wline);
7230 86f4aab9 2022-09-09 thomas wline = NULL;
7231 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7232 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7234 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7235 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7236 a3404814 2018-09-02 stsp wstandend(view->window);
7237 86f4aab9 2022-09-09 thomas if (--limit <= 0)
7238 86f4aab9 2022-09-09 thomas return NULL;
7240 6f5f393a 2022-08-12 thomas i += s->selected;
7241 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
7242 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
7243 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
7244 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
7246 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
7247 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
7248 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7249 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
7250 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7251 86f4aab9 2022-09-09 thomas free(index);
7253 ce52c690 2018-06-23 stsp return err;
7254 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7255 2550e4c3 2018-07-13 stsp free(wline);
7256 2550e4c3 2018-07-13 stsp wline = NULL;
7257 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7258 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7259 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7260 ffd1d5e5 2018-06-23 stsp return NULL;
7261 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7262 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7263 a1eca9bb 2018-06-23 stsp return NULL;
7265 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7266 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7267 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7268 0cf4efb1 2018-09-29 stsp if (view->focussed)
7269 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7270 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7272 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7273 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7274 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7275 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7276 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7277 ffd1d5e5 2018-06-23 stsp return NULL;
7281 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7284 c72de8ab 2023-02-03 thomas view->maxx = 0;
7285 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7286 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7287 848d6979 2019-08-12 stsp const char *modestr = "";
7288 56e0773d 2019-11-28 stsp mode_t mode;
7290 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7291 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7293 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7294 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7295 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7297 638f9024 2019-05-13 stsp return got_error_from_errno(
7298 230a42bd 2019-05-11 jcs "got_object_id_str");
7300 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7301 63c5ca5d 2019-08-24 stsp modestr = "$";
7302 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7305 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7306 d86d3b18 2020-12-01 naddy te, s->repo);
7307 0d6c6ee3 2020-05-20 stsp if (err) {
7308 0d6c6ee3 2020-05-20 stsp free(id_str);
7309 0d6c6ee3 2020-05-20 stsp return err;
7311 cbb35fac 2023-06-25 thomas for (i = 0; link_target[i] != '\0'; i++) {
7312 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7313 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7315 848d6979 2019-08-12 stsp modestr = "@";
7317 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7318 848d6979 2019-08-12 stsp modestr = "/";
7319 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7320 848d6979 2019-08-12 stsp modestr = "*";
7321 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7322 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7323 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7324 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7325 1d13200f 2018-07-12 stsp free(id_str);
7326 0d6c6ee3 2020-05-20 stsp free(link_target);
7327 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7329 1d13200f 2018-07-12 stsp free(id_str);
7330 0d6c6ee3 2020-05-20 stsp free(link_target);
7332 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
7333 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7334 ffd1d5e5 2018-06-23 stsp if (err) {
7335 ffd1d5e5 2018-06-23 stsp free(line);
7338 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
7339 c72de8ab 2023-02-03 thomas free(wline);
7340 c72de8ab 2023-02-03 thomas wline = NULL;
7342 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
7343 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
7344 c72de8ab 2023-02-03 thomas if (err) {
7345 c72de8ab 2023-02-03 thomas free(line);
7348 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7349 0cf4efb1 2018-09-29 stsp if (view->focussed)
7350 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7351 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7353 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7355 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7356 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7357 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
7359 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7360 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7361 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
7362 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7363 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7364 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7365 ffd1d5e5 2018-06-23 stsp free(line);
7366 2550e4c3 2018-07-13 stsp free(wline);
7367 2550e4c3 2018-07-13 stsp wline = NULL;
7369 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7370 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7371 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7375 ffd1d5e5 2018-06-23 stsp return err;
7378 ffd1d5e5 2018-06-23 stsp static void
7379 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7381 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7382 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7383 fa86c4bf 2020-11-29 stsp int i = 0;
7385 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7388 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7389 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7390 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7391 fa86c4bf 2020-11-29 stsp if (!isroot)
7392 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7395 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7396 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7400 a5d43cac 2022-07-01 thomas static const struct got_error *
7401 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
7403 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7404 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7405 ffd1d5e5 2018-06-23 stsp int n = 0;
7407 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7408 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7409 694d3271 2020-12-01 naddy s->first_displayed_entry);
7411 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7413 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7414 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7415 07dd3ed3 2022-08-06 thomas if (last) {
7416 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7417 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7419 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7420 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7421 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7425 a5d43cac 2022-07-01 thomas return NULL;
7428 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7429 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7430 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7432 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7433 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7434 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7436 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7437 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7438 56e0773d 2019-11-28 stsp + 1 /* slash */;
7440 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7442 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7443 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7444 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7446 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7447 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7448 d9765a41 2018-06-23 stsp while (pt) {
7449 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7450 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7451 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7452 cb2ebc8a 2018-06-23 stsp goto done;
7454 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7455 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7456 cb2ebc8a 2018-06-23 stsp goto done;
7458 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7461 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7462 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7463 ce52c690 2018-06-23 stsp goto done;
7467 ce52c690 2018-06-23 stsp if (err) {
7468 ce52c690 2018-06-23 stsp free(*path);
7469 ce52c690 2018-06-23 stsp *path = NULL;
7471 ce52c690 2018-06-23 stsp return err;
7474 ce52c690 2018-06-23 stsp static const struct got_error *
7475 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7476 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7477 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7479 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7480 ce52c690 2018-06-23 stsp char *path;
7481 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7483 a0de39f3 2019-08-09 stsp *new_view = NULL;
7485 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7487 ce52c690 2018-06-23 stsp return err;
7489 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7490 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7491 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7492 83ce39e3 2019-08-12 stsp goto done;
7495 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7496 e5a0f69f 2018-08-18 stsp if (err) {
7497 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7498 fc06ba56 2019-08-22 stsp err = NULL;
7499 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7501 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7503 83ce39e3 2019-08-12 stsp free(path);
7504 69efd4c4 2018-07-18 stsp return err;
7507 69efd4c4 2018-07-18 stsp static const struct got_error *
7508 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7509 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7511 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7512 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7513 69efd4c4 2018-07-18 stsp char *path;
7515 a0de39f3 2019-08-09 stsp *new_view = NULL;
7517 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7518 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7519 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7521 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7523 69efd4c4 2018-07-18 stsp return err;
7525 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7528 e5a0f69f 2018-08-18 stsp view_close(log_view);
7530 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7531 cb2ebc8a 2018-06-23 stsp free(path);
7532 cb2ebc8a 2018-06-23 stsp return err;
7535 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7536 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7537 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7539 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7540 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7541 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7542 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7544 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7545 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7547 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7548 51b7e1c3 2023-04-14 thomas if (s->commit_id == NULL) {
7549 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
7550 51b7e1c3 2023-04-14 thomas goto done;
7553 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7555 bc573f3b 2021-07-10 stsp goto done;
7558 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7559 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7560 bc573f3b 2021-07-10 stsp * closed on demand.
7562 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7563 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7565 bc573f3b 2021-07-10 stsp goto done;
7566 bc573f3b 2021-07-10 stsp s->tree = s->root;
7568 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7569 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7570 ffd1d5e5 2018-06-23 stsp goto done;
7572 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7573 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7574 ffd1d5e5 2018-06-23 stsp goto done;
7577 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7578 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7579 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7580 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7581 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7582 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7583 9cd7cbd1 2020-12-07 stsp goto done;
7586 fb2756b9 2018-08-04 stsp s->repo = repo;
7588 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7589 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7590 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7591 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7593 c0b01bdb 2019-11-08 stsp goto done;
7594 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7595 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7597 c0b01bdb 2019-11-08 stsp goto done;
7598 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7599 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7600 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7602 c0b01bdb 2019-11-08 stsp goto done;
7604 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7605 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7606 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7608 11b20872 2019-11-08 stsp goto done;
7610 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7611 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7613 c0b01bdb 2019-11-08 stsp goto done;
7616 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7617 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7618 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7619 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7620 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7622 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7623 bc573f3b 2021-07-10 stsp if (commit)
7624 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7625 51b7e1c3 2023-04-14 thomas if (err) {
7626 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
7627 51b7e1c3 2023-04-14 thomas close_tree_view(view);
7628 51b7e1c3 2023-04-14 thomas view_close(view);
7630 ad80ab7b 2018-08-04 stsp return err;
7633 e5a0f69f 2018-08-18 stsp static const struct got_error *
7634 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7636 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7638 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7639 fb2756b9 2018-08-04 stsp free(s->tree_label);
7640 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7641 6484ec90 2018-09-29 stsp free(s->commit_id);
7642 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7643 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7644 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7645 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7646 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7647 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7648 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7649 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7650 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7651 ad80ab7b 2018-08-04 stsp free(parent);
7654 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7655 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7656 bc573f3b 2021-07-10 stsp if (s->root)
7657 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7658 7c32bd05 2019-06-22 stsp return NULL;
7661 7c32bd05 2019-06-22 stsp static const struct got_error *
7662 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7664 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7666 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7667 7c32bd05 2019-06-22 stsp return NULL;
7670 7c32bd05 2019-06-22 stsp static int
7671 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7673 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7675 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7679 7c32bd05 2019-06-22 stsp static const struct got_error *
7680 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7682 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7683 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7685 7c32bd05 2019-06-22 stsp if (!view->searching) {
7686 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7687 7c32bd05 2019-06-22 stsp return NULL;
7690 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7691 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7692 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7693 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7694 56e0773d 2019-11-28 stsp s->selected_entry);
7696 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7698 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7699 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7701 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7702 56e0773d 2019-11-28 stsp s->selected_entry);
7705 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7706 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7707 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7708 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7710 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7713 7c32bd05 2019-06-22 stsp while (1) {
7714 56e0773d 2019-11-28 stsp if (te == NULL) {
7715 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7716 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7717 ac66afb8 2019-06-24 stsp return NULL;
7719 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7720 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7722 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7725 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7726 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7727 56e0773d 2019-11-28 stsp s->matched_entry = te;
7731 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7732 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7734 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7737 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7738 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7739 7c32bd05 2019-06-22 stsp s->selected = 0;
7742 e5a0f69f 2018-08-18 stsp return NULL;
7745 ad80ab7b 2018-08-04 stsp static const struct got_error *
7746 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7748 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7749 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7750 e5a0f69f 2018-08-18 stsp char *parent_path;
7752 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7754 e5a0f69f 2018-08-18 stsp return err;
7756 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7757 e5a0f69f 2018-08-18 stsp free(parent_path);
7759 a5d43cac 2022-07-01 thomas view_border(view);
7760 e5a0f69f 2018-08-18 stsp return err;
7763 07dd3ed3 2022-08-06 thomas static const struct got_error *
7764 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7766 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7767 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7768 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7769 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7770 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7771 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7773 07dd3ed3 2022-08-06 thomas g = view->gline;
7774 07dd3ed3 2022-08-06 thomas view->gline = 0;
7776 07dd3ed3 2022-08-06 thomas if (g == 0)
7778 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7779 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7781 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7782 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7783 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7785 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7786 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7787 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7789 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7790 07dd3ed3 2022-08-06 thomas last += off;
7792 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7793 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7794 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7797 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7798 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7799 07dd3ed3 2022-08-06 thomas i += off;
7802 07dd3ed3 2022-08-06 thomas if (i < g) {
7803 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7804 07dd3ed3 2022-08-06 thomas if (err)
7805 07dd3ed3 2022-08-06 thomas return err;
7806 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7807 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7808 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7809 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7810 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7811 07dd3ed3 2022-08-06 thomas first += off;
7812 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7814 07dd3ed3 2022-08-06 thomas } else if (i > g)
7815 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7817 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7818 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7819 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7821 07dd3ed3 2022-08-06 thomas return NULL;
7824 e5a0f69f 2018-08-18 stsp static const struct got_error *
7825 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7827 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7828 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7829 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7830 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7832 07dd3ed3 2022-08-06 thomas if (view->gline)
7833 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7835 e5a0f69f 2018-08-18 stsp switch (ch) {
7836 c72de8ab 2023-02-03 thomas case '0':
7837 c72de8ab 2023-02-03 thomas case '$':
7838 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
7839 c72de8ab 2023-02-03 thomas case 'l':
7840 c72de8ab 2023-02-03 thomas case KEY_LEFT:
7841 c72de8ab 2023-02-03 thomas case 'h':
7842 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
7845 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7846 07b0611c 2022-06-23 thomas view->count = 0;
7848 1be4947a 2022-07-22 thomas case 'L':
7849 07b0611c 2022-06-23 thomas view->count = 0;
7850 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7852 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7854 1be4947a 2022-07-22 thomas case 'R':
7855 07b0611c 2022-06-23 thomas view->count = 0;
7856 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7858 e4526bf5 2021-09-03 naddy case 'g':
7859 aa7a1117 2023-01-09 thomas case '=':
7860 e4526bf5 2021-09-03 naddy case KEY_HOME:
7861 e4526bf5 2021-09-03 naddy s->selected = 0;
7862 07b0611c 2022-06-23 thomas view->count = 0;
7863 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7864 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7865 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7867 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7869 e4526bf5 2021-09-03 naddy case 'G':
7870 aa7a1117 2023-01-09 thomas case '*':
7871 a5d43cac 2022-07-01 thomas case KEY_END: {
7872 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7874 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7875 a5d43cac 2022-07-01 thomas --eos; /* border */
7876 e4526bf5 2021-09-03 naddy s->selected = 0;
7877 07b0611c 2022-06-23 thomas view->count = 0;
7878 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7879 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7880 e4526bf5 2021-09-03 naddy if (te == NULL) {
7881 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7882 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7887 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7888 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7890 e4526bf5 2021-09-03 naddy if (n > 0)
7891 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7895 1e37a5c2 2019-05-12 jcs case KEY_UP:
7896 f7140bf5 2021-10-17 thomas case CTRL('p'):
7897 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7898 1e37a5c2 2019-05-12 jcs s->selected--;
7901 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7902 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7903 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7904 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7905 07b0611c 2022-06-23 thomas view->count = 0;
7907 70f17a53 2022-06-13 thomas case CTRL('u'):
7908 23427b14 2022-06-23 thomas case 'u':
7909 70f17a53 2022-06-13 thomas nscroll /= 2;
7910 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7911 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7912 ea025d1d 2020-02-22 naddy case CTRL('b'):
7913 1c5e5faa 2022-06-23 thomas case 'b':
7914 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7915 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7916 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7917 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7919 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7920 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7922 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7923 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7924 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7925 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7926 07b0611c 2022-06-23 thomas view->count = 0;
7929 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7930 f7140bf5 2021-10-17 thomas case CTRL('n'):
7931 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7932 1e37a5c2 2019-05-12 jcs s->selected++;
7935 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7936 07b0611c 2022-06-23 thomas == NULL) {
7937 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7938 07b0611c 2022-06-23 thomas view->count = 0;
7941 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7943 70f17a53 2022-06-13 thomas case CTRL('d'):
7944 23427b14 2022-06-23 thomas case 'd':
7945 70f17a53 2022-06-13 thomas nscroll /= 2;
7946 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7947 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7948 ea025d1d 2020-02-22 naddy case CTRL('f'):
7949 1c5e5faa 2022-06-23 thomas case 'f':
7950 4c2d69cb 2022-06-23 thomas case ' ':
7951 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7953 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7954 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7955 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7956 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7958 07b0611c 2022-06-23 thomas view->count = 0;
7961 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7963 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7965 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7966 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7967 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7968 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7969 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7970 07b0611c 2022-06-23 thomas view->count = 0;
7973 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7974 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7976 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7977 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7978 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7979 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7980 1e37a5c2 2019-05-12 jcs s->selected_entry =
7981 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7982 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7983 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7984 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7985 a5d43cac 2022-07-01 thomas if (err)
7988 1e37a5c2 2019-05-12 jcs free(parent);
7989 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7990 56e0773d 2019-11-28 stsp s->selected_entry))) {
7991 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7992 07b0611c 2022-06-23 thomas view->count = 0;
7993 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7994 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7997 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7998 941e9f74 2019-05-21 stsp if (err) {
7999 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
8002 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8003 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8005 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
8006 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8007 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
8008 07b0611c 2022-06-23 thomas view->count = 0;
8011 07b0611c 2022-06-23 thomas view->count = 0;
8015 ffd1d5e5 2018-06-23 stsp return err;
8018 ffd1d5e5 2018-06-23 stsp __dead static void
8019 ffd1d5e5 2018-06-23 stsp usage_tree(void)
8022 91198554 2022-06-23 thomas fprintf(stderr,
8023 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8024 ffd1d5e5 2018-06-23 stsp getprogname());
8028 ffd1d5e5 2018-06-23 stsp static const struct got_error *
8029 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
8031 1d98034b 2023-04-14 thomas const struct got_error *error;
8032 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
8033 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
8034 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8035 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
8036 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8037 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
8038 4e97c21c 2020-12-06 stsp char *label = NULL;
8039 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
8040 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
8042 5221c383 2018-08-01 stsp struct tog_view *view;
8043 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8045 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8046 ffd1d5e5 2018-06-23 stsp switch (ch) {
8048 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
8051 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
8052 74283ab8 2020-02-07 stsp if (repo_path == NULL)
8053 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
8057 e99e2d15 2020-11-24 naddy usage_tree();
8058 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
8062 ffd1d5e5 2018-06-23 stsp argc -= optind;
8063 ffd1d5e5 2018-06-23 stsp argv += optind;
8065 55cccc34 2020-02-20 stsp if (argc > 1)
8066 e99e2d15 2020-11-24 naddy usage_tree();
8068 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8069 7cd52833 2022-06-23 thomas if (error != NULL)
8070 7cd52833 2022-06-23 thomas goto done;
8072 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
8073 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8074 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8075 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8076 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8077 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8078 c156c7a4 2020-12-18 stsp goto done;
8079 55cccc34 2020-02-20 stsp if (worktree)
8080 52185f70 2019-02-05 stsp repo_path =
8081 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
8083 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
8084 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8085 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8086 c156c7a4 2020-12-18 stsp goto done;
8090 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8091 c02c541e 2019-03-29 stsp if (error != NULL)
8092 52185f70 2019-02-05 stsp goto done;
8094 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8095 55cccc34 2020-02-20 stsp repo, worktree);
8096 55cccc34 2020-02-20 stsp if (error)
8097 55cccc34 2020-02-20 stsp goto done;
8099 557d3365 2023-04-14 thomas init_curses();
8101 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8102 c02c541e 2019-03-29 stsp if (error)
8103 52185f70 2019-02-05 stsp goto done;
8105 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8106 51a10b52 2020-12-26 stsp if (error)
8107 51a10b52 2020-12-26 stsp goto done;
8109 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
8110 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
8111 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
8112 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8113 4e97c21c 2020-12-06 stsp if (error)
8114 4e97c21c 2020-12-06 stsp goto done;
8115 4e97c21c 2020-12-06 stsp head_ref_name = label;
8117 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
8118 4e97c21c 2020-12-06 stsp if (error == NULL)
8119 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
8120 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
8121 4e97c21c 2020-12-06 stsp goto done;
8122 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
8123 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8124 4e97c21c 2020-12-06 stsp if (error)
8125 4e97c21c 2020-12-06 stsp goto done;
8128 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8129 945f9229 2022-04-16 thomas if (error)
8130 945f9229 2022-04-16 thomas goto done;
8132 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8133 5221c383 2018-08-01 stsp if (view == NULL) {
8134 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
8135 5221c383 2018-08-01 stsp goto done;
8137 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8138 ad80ab7b 2018-08-04 stsp if (error)
8139 ad80ab7b 2018-08-04 stsp goto done;
8140 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8141 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
8142 d91faf3b 2020-12-01 naddy in_repo_path);
8143 55cccc34 2020-02-20 stsp if (error)
8144 55cccc34 2020-02-20 stsp goto done;
8147 55cccc34 2020-02-20 stsp if (worktree) {
8148 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8149 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8150 55cccc34 2020-02-20 stsp worktree = NULL;
8152 557d3365 2023-04-14 thomas error = view_loop(view);
8154 52185f70 2019-02-05 stsp free(repo_path);
8155 e4a0e26d 2020-02-20 stsp free(cwd);
8156 ffd1d5e5 2018-06-23 stsp free(commit_id);
8157 4e97c21c 2020-12-06 stsp free(label);
8159 486cd271 2020-12-06 stsp got_ref_close(ref);
8160 1d0f4054 2021-06-17 stsp if (repo) {
8161 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8162 1d0f4054 2021-06-17 stsp if (error == NULL)
8163 1d0f4054 2021-06-17 stsp error = close_err;
8165 7cd52833 2022-06-23 thomas if (pack_fds) {
8166 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8167 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8168 7cd52833 2022-06-23 thomas if (error == NULL)
8169 7cd52833 2022-06-23 thomas error = pack_err;
8171 51a10b52 2020-12-26 stsp tog_free_refs();
8172 ffd1d5e5 2018-06-23 stsp return error;
8175 6458efa5 2020-11-24 stsp static const struct got_error *
8176 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8178 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8179 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8181 6458efa5 2020-11-24 stsp s->nrefs = 0;
8182 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8183 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
8184 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
8185 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
8186 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
8189 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8190 6458efa5 2020-11-24 stsp if (re == NULL)
8191 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8193 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8194 8924d611 2020-12-26 stsp if (re->ref == NULL)
8195 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8196 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8197 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8200 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8201 6458efa5 2020-11-24 stsp return NULL;
8204 ef20f542 2022-06-26 thomas static void
8205 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8207 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8209 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8210 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8211 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8212 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8217 6458efa5 2020-11-24 stsp static const struct got_error *
8218 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8220 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8221 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8223 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8224 6458efa5 2020-11-24 stsp s->repo = repo;
8226 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8227 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8229 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8231 51b7e1c3 2023-04-14 thomas goto done;
8233 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8234 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8235 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8236 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8238 6458efa5 2020-11-24 stsp goto done;
8240 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8241 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8242 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8244 6458efa5 2020-11-24 stsp goto done;
8246 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8247 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8248 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8249 2183bbf6 2022-01-23 thomas if (err)
8250 2183bbf6 2022-01-23 thomas goto done;
8252 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
8253 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
8254 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
8256 6458efa5 2020-11-24 stsp goto done;
8259 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8260 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8261 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8262 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8263 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8265 51b7e1c3 2023-04-14 thomas if (err) {
8266 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
8267 51b7e1c3 2023-04-14 thomas close_ref_view(view);
8268 51b7e1c3 2023-04-14 thomas view_close(view);
8270 6458efa5 2020-11-24 stsp return err;
8273 6458efa5 2020-11-24 stsp static const struct got_error *
8274 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8276 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8278 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8279 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8281 6458efa5 2020-11-24 stsp return NULL;
8284 6458efa5 2020-11-24 stsp static const struct got_error *
8285 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8286 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8288 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8289 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8290 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8291 6458efa5 2020-11-24 stsp int obj_type;
8293 c42c9805 2020-11-24 stsp *commit_id = NULL;
8295 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8297 6458efa5 2020-11-24 stsp return err;
8299 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8301 6458efa5 2020-11-24 stsp goto done;
8303 6458efa5 2020-11-24 stsp switch (obj_type) {
8304 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8305 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8307 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8308 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8310 6458efa5 2020-11-24 stsp goto done;
8311 c42c9805 2020-11-24 stsp free(obj_id);
8312 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8313 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8315 6458efa5 2020-11-24 stsp goto done;
8316 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8317 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8318 c42c9805 2020-11-24 stsp goto done;
8320 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8321 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8322 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8323 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8324 c42c9805 2020-11-24 stsp goto done;
8328 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8334 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8335 c42c9805 2020-11-24 stsp if (err) {
8336 c42c9805 2020-11-24 stsp free(*commit_id);
8337 c42c9805 2020-11-24 stsp *commit_id = NULL;
8339 c42c9805 2020-11-24 stsp return err;
8342 c42c9805 2020-11-24 stsp static const struct got_error *
8343 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8344 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8346 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8347 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8348 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8350 c42c9805 2020-11-24 stsp *new_view = NULL;
8352 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8353 c42c9805 2020-11-24 stsp if (err) {
8354 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8355 c42c9805 2020-11-24 stsp return err;
8357 c42c9805 2020-11-24 stsp return NULL;
8360 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8361 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8362 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8363 6458efa5 2020-11-24 stsp goto done;
8366 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8367 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
8370 6458efa5 2020-11-24 stsp view_close(log_view);
8372 6458efa5 2020-11-24 stsp *new_view = log_view;
8373 c42c9805 2020-11-24 stsp free(commit_id);
8374 6458efa5 2020-11-24 stsp return err;
8377 ce5b7c56 2019-07-09 stsp static void
8378 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8380 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8381 34ba6917 2020-11-30 stsp int i = 0;
8383 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8386 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8387 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8388 34ba6917 2020-11-30 stsp if (re == NULL)
8390 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8391 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8395 a5d43cac 2022-07-01 thomas static const struct got_error *
8396 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
8398 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8399 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8400 6458efa5 2020-11-24 stsp int n = 0;
8402 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8403 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8405 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8407 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8408 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
8409 07dd3ed3 2022-08-06 thomas if (last) {
8410 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
8411 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
8413 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8414 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8415 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8419 a5d43cac 2022-07-01 thomas return NULL;
8422 6458efa5 2020-11-24 stsp static const struct got_error *
8423 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8425 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8427 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8428 6458efa5 2020-11-24 stsp return NULL;
8431 6458efa5 2020-11-24 stsp static int
8432 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8434 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8436 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
8440 6458efa5 2020-11-24 stsp static const struct got_error *
8441 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8443 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8444 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8446 6458efa5 2020-11-24 stsp if (!view->searching) {
8447 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8448 6458efa5 2020-11-24 stsp return NULL;
8451 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8452 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8453 6458efa5 2020-11-24 stsp if (s->selected_entry)
8454 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8456 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8457 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8459 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8460 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8462 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8463 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8466 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8467 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8468 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8469 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8471 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8474 6458efa5 2020-11-24 stsp while (1) {
8475 6458efa5 2020-11-24 stsp if (re == NULL) {
8476 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8477 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8478 6458efa5 2020-11-24 stsp return NULL;
8480 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8481 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8483 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8486 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8487 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8488 6458efa5 2020-11-24 stsp s->matched_entry = re;
8492 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8493 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8495 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8498 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8499 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8500 6458efa5 2020-11-24 stsp s->selected = 0;
8503 6458efa5 2020-11-24 stsp return NULL;
8506 6458efa5 2020-11-24 stsp static const struct got_error *
8507 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8509 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8510 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8512 6458efa5 2020-11-24 stsp char *line = NULL;
8513 6458efa5 2020-11-24 stsp wchar_t *wline;
8514 6458efa5 2020-11-24 stsp struct tog_color *tc;
8515 66a70b97 2023-02-03 thomas int width, n, scrollx;
8516 6458efa5 2020-11-24 stsp int limit = view->nlines;
8518 6458efa5 2020-11-24 stsp werase(view->window);
8520 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8521 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8522 a5d43cac 2022-07-01 thomas --limit; /* border */
8524 6458efa5 2020-11-24 stsp if (limit == 0)
8525 6458efa5 2020-11-24 stsp return NULL;
8527 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8529 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8530 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8531 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8533 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8534 6458efa5 2020-11-24 stsp if (err) {
8535 6458efa5 2020-11-24 stsp free(line);
8536 6458efa5 2020-11-24 stsp return err;
8538 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8539 6458efa5 2020-11-24 stsp wstandout(view->window);
8540 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8541 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8542 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8543 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8544 6458efa5 2020-11-24 stsp wstandend(view->window);
8545 6458efa5 2020-11-24 stsp free(wline);
8546 6458efa5 2020-11-24 stsp wline = NULL;
8547 6458efa5 2020-11-24 stsp free(line);
8548 6458efa5 2020-11-24 stsp line = NULL;
8549 6458efa5 2020-11-24 stsp if (--limit <= 0)
8550 6458efa5 2020-11-24 stsp return NULL;
8553 66a70b97 2023-02-03 thomas view->maxx = 0;
8554 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8555 6458efa5 2020-11-24 stsp char *line = NULL;
8556 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8558 84227eb1 2022-06-23 thomas if (s->show_date) {
8559 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8560 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8561 84227eb1 2022-06-23 thomas struct got_object_id *id;
8562 84227eb1 2022-06-23 thomas struct tm tm;
8563 84227eb1 2022-06-23 thomas time_t t;
8565 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8566 84227eb1 2022-06-23 thomas if (err)
8567 84227eb1 2022-06-23 thomas return err;
8568 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8569 84227eb1 2022-06-23 thomas if (err) {
8570 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8571 84227eb1 2022-06-23 thomas free(id);
8572 84227eb1 2022-06-23 thomas return err;
8574 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8576 84227eb1 2022-06-23 thomas if (err) {
8577 84227eb1 2022-06-23 thomas free(id);
8578 84227eb1 2022-06-23 thomas return err;
8580 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8581 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8582 84227eb1 2022-06-23 thomas } else {
8583 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8584 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8586 84227eb1 2022-06-23 thomas free(id);
8587 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8588 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8589 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8590 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8592 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8593 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8594 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8595 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8596 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8597 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8598 6458efa5 2020-11-24 stsp struct got_object_id *id;
8599 6458efa5 2020-11-24 stsp char *id_str;
8600 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8602 6458efa5 2020-11-24 stsp return err;
8603 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8604 6458efa5 2020-11-24 stsp if (err) {
8606 6458efa5 2020-11-24 stsp return err;
8608 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8609 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8610 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8612 6458efa5 2020-11-24 stsp free(id_str);
8613 6458efa5 2020-11-24 stsp return err;
8616 6458efa5 2020-11-24 stsp free(id_str);
8617 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8618 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8619 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8621 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8622 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8623 6458efa5 2020-11-24 stsp if (err) {
8624 6458efa5 2020-11-24 stsp free(line);
8625 6458efa5 2020-11-24 stsp return err;
8627 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8628 66a70b97 2023-02-03 thomas free(wline);
8629 66a70b97 2023-02-03 thomas wline = NULL;
8631 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8632 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8633 66a70b97 2023-02-03 thomas if (err) {
8634 66a70b97 2023-02-03 thomas free(line);
8635 66a70b97 2023-02-03 thomas return err;
8637 6458efa5 2020-11-24 stsp if (n == s->selected) {
8638 6458efa5 2020-11-24 stsp if (view->focussed)
8639 6458efa5 2020-11-24 stsp wstandout(view->window);
8640 6458efa5 2020-11-24 stsp s->selected_entry = re;
8642 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8644 6458efa5 2020-11-24 stsp wattr_on(view->window,
8645 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8646 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8648 6458efa5 2020-11-24 stsp wattr_off(view->window,
8649 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8650 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8651 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8652 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8653 6458efa5 2020-11-24 stsp wstandend(view->window);
8654 6458efa5 2020-11-24 stsp free(line);
8655 6458efa5 2020-11-24 stsp free(wline);
8656 6458efa5 2020-11-24 stsp wline = NULL;
8658 6458efa5 2020-11-24 stsp s->ndisplayed++;
8659 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8662 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8665 a5d43cac 2022-07-01 thomas view_border(view);
8666 6458efa5 2020-11-24 stsp return err;
8669 6458efa5 2020-11-24 stsp static const struct got_error *
8670 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8671 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8673 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8674 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8675 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8677 c42c9805 2020-11-24 stsp *new_view = NULL;
8679 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8680 c42c9805 2020-11-24 stsp if (err) {
8681 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8682 c42c9805 2020-11-24 stsp return err;
8684 c42c9805 2020-11-24 stsp return NULL;
8688 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8689 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8690 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8691 c42c9805 2020-11-24 stsp goto done;
8694 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8695 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8697 c42c9805 2020-11-24 stsp goto done;
8699 c42c9805 2020-11-24 stsp *new_view = tree_view;
8701 c42c9805 2020-11-24 stsp free(commit_id);
8702 c42c9805 2020-11-24 stsp return err;
8705 07dd3ed3 2022-08-06 thomas static const struct got_error *
8706 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8708 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8709 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8710 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8712 07dd3ed3 2022-08-06 thomas g = view->gline;
8713 07dd3ed3 2022-08-06 thomas view->gline = 0;
8715 07dd3ed3 2022-08-06 thomas if (g == 0)
8717 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8718 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8720 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8721 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8722 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8723 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8724 07dd3ed3 2022-08-06 thomas return NULL;
8727 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8728 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8729 07dd3ed3 2022-08-06 thomas if (err)
8730 07dd3ed3 2022-08-06 thomas return err;
8731 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8732 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8733 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8734 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8735 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8736 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8738 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8739 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8741 07dd3ed3 2022-08-06 thomas return NULL;
8745 c42c9805 2020-11-24 stsp static const struct got_error *
8746 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8748 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8749 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8750 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8751 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8753 07dd3ed3 2022-08-06 thomas if (view->gline)
8754 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8756 6458efa5 2020-11-24 stsp switch (ch) {
8757 66a70b97 2023-02-03 thomas case '0':
8758 66a70b97 2023-02-03 thomas case '$':
8759 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8760 66a70b97 2023-02-03 thomas case 'l':
8761 66a70b97 2023-02-03 thomas case KEY_LEFT:
8762 66a70b97 2023-02-03 thomas case 'h':
8763 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8766 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8767 07b0611c 2022-06-23 thomas view->count = 0;
8769 84227eb1 2022-06-23 thomas case 'm':
8770 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8771 07b0611c 2022-06-23 thomas view->count = 0;
8773 98182bd0 2021-11-20 thomas case 'o':
8774 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8775 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8776 07b0611c 2022-06-23 thomas view->count = 0;
8777 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8778 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8779 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8780 c591440f 2021-11-20 thomas if (err)
8782 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8783 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8784 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8785 3bfadbd4 2021-11-20 thomas if (err)
8787 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8788 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8790 6458efa5 2020-11-24 stsp case KEY_ENTER:
8791 6458efa5 2020-11-24 stsp case '\r':
8792 07b0611c 2022-06-23 thomas view->count = 0;
8793 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8795 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8797 1be4947a 2022-07-22 thomas case 'T':
8798 07b0611c 2022-06-23 thomas view->count = 0;
8799 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8801 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8803 e4526bf5 2021-09-03 naddy case 'g':
8804 aa7a1117 2023-01-09 thomas case '=':
8805 e4526bf5 2021-09-03 naddy case KEY_HOME:
8806 e4526bf5 2021-09-03 naddy s->selected = 0;
8807 07b0611c 2022-06-23 thomas view->count = 0;
8808 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8810 e4526bf5 2021-09-03 naddy case 'G':
8811 aa7a1117 2023-01-09 thomas case '*':
8812 a5d43cac 2022-07-01 thomas case KEY_END: {
8813 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8815 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8816 a5d43cac 2022-07-01 thomas --eos; /* border */
8817 e4526bf5 2021-09-03 naddy s->selected = 0;
8818 07b0611c 2022-06-23 thomas view->count = 0;
8819 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8820 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8821 e4526bf5 2021-09-03 naddy if (re == NULL)
8823 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8824 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8826 e4526bf5 2021-09-03 naddy if (n > 0)
8827 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8831 6458efa5 2020-11-24 stsp case KEY_UP:
8832 f7140bf5 2021-10-17 thomas case CTRL('p'):
8833 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8834 6458efa5 2020-11-24 stsp s->selected--;
8837 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8838 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8839 07b0611c 2022-06-23 thomas view->count = 0;
8841 70f17a53 2022-06-13 thomas case CTRL('u'):
8842 23427b14 2022-06-23 thomas case 'u':
8843 70f17a53 2022-06-13 thomas nscroll /= 2;
8844 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8845 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8846 6458efa5 2020-11-24 stsp case CTRL('b'):
8847 1c5e5faa 2022-06-23 thomas case 'b':
8848 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8849 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8850 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8851 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8852 07b0611c 2022-06-23 thomas view->count = 0;
8855 6458efa5 2020-11-24 stsp case KEY_DOWN:
8856 f7140bf5 2021-10-17 thomas case CTRL('n'):
8857 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8858 6458efa5 2020-11-24 stsp s->selected++;
8861 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8862 6458efa5 2020-11-24 stsp /* can't scroll any further */
8863 07b0611c 2022-06-23 thomas view->count = 0;
8866 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8868 70f17a53 2022-06-13 thomas case CTRL('d'):
8869 23427b14 2022-06-23 thomas case 'd':
8870 70f17a53 2022-06-13 thomas nscroll /= 2;
8871 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8872 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8873 6458efa5 2020-11-24 stsp case CTRL('f'):
8874 1c5e5faa 2022-06-23 thomas case 'f':
8875 4c2d69cb 2022-06-23 thomas case ' ':
8876 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8877 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8878 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8879 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8880 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8881 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8882 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8883 07b0611c 2022-06-23 thomas view->count = 0;
8886 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8888 6458efa5 2020-11-24 stsp case CTRL('l'):
8889 07b0611c 2022-06-23 thomas view->count = 0;
8890 8924d611 2020-12-26 stsp tog_free_refs();
8891 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8894 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8895 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8897 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8898 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8899 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8902 07b0611c 2022-06-23 thomas view->count = 0;
8906 6458efa5 2020-11-24 stsp return err;
8909 6458efa5 2020-11-24 stsp __dead static void
8910 6458efa5 2020-11-24 stsp usage_ref(void)
8913 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8914 6458efa5 2020-11-24 stsp getprogname());
8918 6458efa5 2020-11-24 stsp static const struct got_error *
8919 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8921 1d98034b 2023-04-14 thomas const struct got_error *error;
8922 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8923 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8924 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8926 6458efa5 2020-11-24 stsp struct tog_view *view;
8927 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8929 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8930 6458efa5 2020-11-24 stsp switch (ch) {
8932 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8933 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8934 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8938 e99e2d15 2020-11-24 naddy usage_ref();
8939 6458efa5 2020-11-24 stsp /* NOTREACHED */
8943 6458efa5 2020-11-24 stsp argc -= optind;
8944 6458efa5 2020-11-24 stsp argv += optind;
8946 6458efa5 2020-11-24 stsp if (argc > 1)
8947 e99e2d15 2020-11-24 naddy usage_ref();
8949 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8950 7cd52833 2022-06-23 thomas if (error != NULL)
8951 7cd52833 2022-06-23 thomas goto done;
8953 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8954 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8955 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8956 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8957 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8958 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8959 c156c7a4 2020-12-18 stsp goto done;
8960 6458efa5 2020-11-24 stsp if (worktree)
8961 6458efa5 2020-11-24 stsp repo_path =
8962 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8964 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8965 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8966 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8967 c156c7a4 2020-12-18 stsp goto done;
8971 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8972 6458efa5 2020-11-24 stsp if (error != NULL)
8973 6458efa5 2020-11-24 stsp goto done;
8975 557d3365 2023-04-14 thomas init_curses();
8977 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8978 51a10b52 2020-12-26 stsp if (error)
8979 51a10b52 2020-12-26 stsp goto done;
8981 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8982 6458efa5 2020-11-24 stsp if (error)
8983 6458efa5 2020-11-24 stsp goto done;
8985 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8986 6458efa5 2020-11-24 stsp if (view == NULL) {
8987 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8988 6458efa5 2020-11-24 stsp goto done;
8991 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8992 6458efa5 2020-11-24 stsp if (error)
8993 6458efa5 2020-11-24 stsp goto done;
8995 6458efa5 2020-11-24 stsp if (worktree) {
8996 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8997 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8998 6458efa5 2020-11-24 stsp worktree = NULL;
9000 557d3365 2023-04-14 thomas error = view_loop(view);
9002 6458efa5 2020-11-24 stsp free(repo_path);
9003 6458efa5 2020-11-24 stsp free(cwd);
9004 1d0f4054 2021-06-17 stsp if (repo) {
9005 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9006 1d0f4054 2021-06-17 stsp if (close_err)
9007 1d0f4054 2021-06-17 stsp error = close_err;
9009 7cd52833 2022-06-23 thomas if (pack_fds) {
9010 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9011 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9012 7cd52833 2022-06-23 thomas if (error == NULL)
9013 7cd52833 2022-06-23 thomas error = pack_err;
9015 51a10b52 2020-12-26 stsp tog_free_refs();
9016 6458efa5 2020-11-24 stsp return error;
9019 fc2737d5 2022-09-15 thomas static const struct got_error*
9020 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9021 fc2737d5 2022-09-15 thomas const char *str)
9023 fc2737d5 2022-09-15 thomas size_t len;
9025 fc2737d5 2022-09-15 thomas if (win == NULL)
9026 fc2737d5 2022-09-15 thomas win = stdscr;
9028 fc2737d5 2022-09-15 thomas len = strlen(str);
9029 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9031 fc2737d5 2022-09-15 thomas if (focus)
9032 fc2737d5 2022-09-15 thomas wstandout(win);
9033 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
9034 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9035 fc2737d5 2022-09-15 thomas if (focus)
9036 fc2737d5 2022-09-15 thomas wstandend(win);
9038 fc2737d5 2022-09-15 thomas return NULL;
9041 2a31b33b 2022-07-23 thomas static const struct got_error *
9042 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9044 fc2737d5 2022-09-15 thomas off_t *p;
9046 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9047 fc2737d5 2022-09-15 thomas if (p == NULL) {
9048 fc2737d5 2022-09-15 thomas free(*line_offsets);
9049 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
9050 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
9053 fc2737d5 2022-09-15 thomas *line_offsets = p;
9054 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
9055 fc2737d5 2022-09-15 thomas ++(*nlines);
9056 fc2737d5 2022-09-15 thomas return NULL;
9059 fc2737d5 2022-09-15 thomas static const struct got_error *
9060 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9062 fc2737d5 2022-09-15 thomas *ret = 0;
9064 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
9065 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9066 fc2737d5 2022-09-15 thomas size_t len = 1;
9068 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
9069 fc2737d5 2022-09-15 thomas continue;
9071 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9072 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9073 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9075 fc2737d5 2022-09-15 thomas len += strlen(t);
9076 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
9077 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
9078 fc2737d5 2022-09-15 thomas free(t0);
9079 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
9082 fc2737d5 2022-09-15 thomas return NULL;
9086 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
9087 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
9088 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
9090 fc2737d5 2022-09-15 thomas static const struct got_error *
9091 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9093 fc2737d5 2022-09-15 thomas int n, len = width;
9095 fc2737d5 2022-09-15 thomas if (km->keys) {
9096 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
9097 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9098 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
9100 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9101 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
9103 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
9105 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9106 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9107 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9109 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
9110 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
9111 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
9112 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9113 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
9114 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9115 fc2737d5 2022-09-15 thomas if (n < 0) {
9116 fc2737d5 2022-09-15 thomas free(t0);
9117 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9119 fc2737d5 2022-09-15 thomas first = 0;
9120 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
9121 fc2737d5 2022-09-15 thomas *off += n;
9123 fc2737d5 2022-09-15 thomas free(t0);
9125 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9126 fc2737d5 2022-09-15 thomas if (n < 0)
9127 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9128 fc2737d5 2022-09-15 thomas *off += n;
9130 fc2737d5 2022-09-15 thomas return NULL;
9133 fc2737d5 2022-09-15 thomas static const struct got_error *
9134 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
9136 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9137 fc2737d5 2022-09-15 thomas off_t off = 0;
9138 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
9139 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
9140 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
9141 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9142 fc2737d5 2022-09-15 thomas GENERATE_HELP
9143 fc2737d5 2022-09-15 thomas #undef KEYMAP_
9144 fc2737d5 2022-09-15 thomas #undef KEY_
9147 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9148 fc2737d5 2022-09-15 thomas if (err)
9149 fc2737d5 2022-09-15 thomas return err;
9151 fc2737d5 2022-09-15 thomas n = nitems(km);
9152 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
9153 fc2737d5 2022-09-15 thomas if (err)
9154 fc2737d5 2022-09-15 thomas return err;
9156 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
9157 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
9158 fc2737d5 2022-09-15 thomas show = s->all;
9159 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
9160 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
9161 fc2737d5 2022-09-15 thomas show = 1;
9163 fc2737d5 2022-09-15 thomas if (show) {
9164 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
9165 fc2737d5 2022-09-15 thomas if (err)
9166 fc2737d5 2022-09-15 thomas return err;
9167 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9168 fc2737d5 2022-09-15 thomas if (err)
9169 fc2737d5 2022-09-15 thomas return err;
9172 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
9174 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9175 fc2737d5 2022-09-15 thomas return err;
9178 fc2737d5 2022-09-15 thomas static const struct got_error *
9179 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
9181 fc2737d5 2022-09-15 thomas FILE *f;
9182 fc2737d5 2022-09-15 thomas const struct got_error *err;
9184 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9185 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9186 fc2737d5 2022-09-15 thomas s->nlines = 0;
9188 fc2737d5 2022-09-15 thomas f = got_opentemp();
9189 fc2737d5 2022-09-15 thomas if (f == NULL)
9190 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
9191 fc2737d5 2022-09-15 thomas s->f = f;
9193 fc2737d5 2022-09-15 thomas err = format_help(s);
9194 fc2737d5 2022-09-15 thomas if (err)
9195 fc2737d5 2022-09-15 thomas return err;
9197 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
9198 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
9200 fc2737d5 2022-09-15 thomas return NULL;
9203 fc2737d5 2022-09-15 thomas static const struct got_error *
9204 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
9206 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
9207 fc2737d5 2022-09-15 thomas return NULL;
9210 2a7d3cd7 2022-09-17 thomas static void
9211 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9212 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
9214 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
9216 2a7d3cd7 2022-09-17 thomas *f = s->f;
9217 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
9218 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
9219 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
9220 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
9221 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
9222 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
9225 fc2737d5 2022-09-15 thomas static const struct got_error *
9226 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
9228 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9229 fc2737d5 2022-09-15 thomas const struct got_error *err;
9230 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
9231 fc2737d5 2022-09-15 thomas wchar_t *wline;
9232 fc2737d5 2022-09-15 thomas char *line;
9233 fc2737d5 2022-09-15 thomas ssize_t linelen;
9234 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9235 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
9236 fc2737d5 2022-09-15 thomas int eos = view->nlines;
9238 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9239 fc2737d5 2022-09-15 thomas --eos; /* account for border */
9241 fc2737d5 2022-09-15 thomas s->lineno = 0;
9242 fc2737d5 2022-09-15 thomas rewind(s->f);
9243 fc2737d5 2022-09-15 thomas werase(view->window);
9245 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
9246 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
9248 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
9249 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
9250 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
9251 fc2737d5 2022-09-15 thomas if (err)
9252 fc2737d5 2022-09-15 thomas return err;
9253 fc2737d5 2022-09-15 thomas if (eos <= 1)
9254 fc2737d5 2022-09-15 thomas return NULL;
9255 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
9256 fc2737d5 2022-09-15 thomas eos -= 2;
9258 fc2737d5 2022-09-15 thomas s->eof = 0;
9259 fc2737d5 2022-09-15 thomas view->maxx = 0;
9260 fc2737d5 2022-09-15 thomas line = NULL;
9261 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
9262 fc2737d5 2022-09-15 thomas attr_t attr = 0;
9264 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9265 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9266 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
9267 fc2737d5 2022-09-15 thomas free(line);
9268 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
9270 fc2737d5 2022-09-15 thomas s->eof = 1;
9273 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
9274 fc2737d5 2022-09-15 thomas continue;
9275 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9276 fc2737d5 2022-09-15 thomas continue;
9277 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
9278 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
9280 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9281 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
9282 fc2737d5 2022-09-15 thomas if (err) {
9283 fc2737d5 2022-09-15 thomas free(line);
9284 fc2737d5 2022-09-15 thomas return err;
9286 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
9287 fc2737d5 2022-09-15 thomas free(wline);
9288 fc2737d5 2022-09-15 thomas wline = NULL;
9290 fc2737d5 2022-09-15 thomas if (attr)
9291 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
9292 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
9293 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9294 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
9295 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
9296 fc2737d5 2022-09-15 thomas if (err) {
9297 fc2737d5 2022-09-15 thomas free(line);
9298 fc2737d5 2022-09-15 thomas return err;
9300 fc2737d5 2022-09-15 thomas } else {
9301 fc2737d5 2022-09-15 thomas int skip;
9303 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
9304 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
9305 fc2737d5 2022-09-15 thomas if (err) {
9306 fc2737d5 2022-09-15 thomas free(line);
9307 fc2737d5 2022-09-15 thomas return err;
9309 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
9310 fc2737d5 2022-09-15 thomas free(wline);
9311 fc2737d5 2022-09-15 thomas wline = NULL;
9313 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
9314 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
9315 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
9316 fc2737d5 2022-09-15 thomas } else {
9317 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
9318 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
9320 fc2737d5 2022-09-15 thomas if (attr)
9321 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
9322 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
9323 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
9325 fc2737d5 2022-09-15 thomas free(line);
9326 fc2737d5 2022-09-15 thomas if (nprinted > 0)
9327 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9329 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
9331 fc2737d5 2022-09-15 thomas view_border(view);
9333 fc2737d5 2022-09-15 thomas if (s->eof) {
9334 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
9335 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
9336 fc2737d5 2022-09-15 thomas view->ncols - 1);
9337 fc2737d5 2022-09-15 thomas if (rc == ERR)
9338 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9339 fc2737d5 2022-09-15 thomas } else {
9340 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
9341 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
9342 fc2737d5 2022-09-15 thomas wstandout(view->window);
9343 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
9344 fc2737d5 2022-09-15 thomas view->ncols - 1);
9345 fc2737d5 2022-09-15 thomas if (rc == ERR)
9346 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9347 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
9348 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
9349 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
9350 fc2737d5 2022-09-15 thomas if (rc == ERR)
9351 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
9353 fc2737d5 2022-09-15 thomas wstandend(view->window);
9356 fc2737d5 2022-09-15 thomas return NULL;
9359 fc2737d5 2022-09-15 thomas static const struct got_error *
9360 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9362 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9363 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9364 fc2737d5 2022-09-15 thomas char *line = NULL;
9365 fc2737d5 2022-09-15 thomas ssize_t linelen;
9366 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9367 fc2737d5 2022-09-15 thomas int eos, nscroll;
9369 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
9370 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9371 fc2737d5 2022-09-15 thomas --eos; /* border */
9373 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
9375 fc2737d5 2022-09-15 thomas switch (ch) {
9376 fc2737d5 2022-09-15 thomas case '0':
9377 fc2737d5 2022-09-15 thomas case '$':
9378 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
9379 fc2737d5 2022-09-15 thomas case 'l':
9380 fc2737d5 2022-09-15 thomas case KEY_LEFT:
9381 fc2737d5 2022-09-15 thomas case 'h':
9382 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
9384 fc2737d5 2022-09-15 thomas case 'g':
9385 fc2737d5 2022-09-15 thomas case KEY_HOME:
9386 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9387 fc2737d5 2022-09-15 thomas view->count = 0;
9389 fc2737d5 2022-09-15 thomas case 'G':
9390 fc2737d5 2022-09-15 thomas case KEY_END:
9391 fc2737d5 2022-09-15 thomas view->count = 0;
9392 fc2737d5 2022-09-15 thomas if (s->eof)
9394 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
9395 fc2737d5 2022-09-15 thomas s->eof = 1;
9397 fc2737d5 2022-09-15 thomas case 'k':
9398 fc2737d5 2022-09-15 thomas case KEY_UP:
9399 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
9400 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
9402 fc2737d5 2022-09-15 thomas view->count = 0;
9404 fc2737d5 2022-09-15 thomas case CTRL('u'):
9405 fc2737d5 2022-09-15 thomas case 'u':
9406 fc2737d5 2022-09-15 thomas nscroll /= 2;
9407 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9408 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
9409 fc2737d5 2022-09-15 thomas case CTRL('b'):
9410 fc2737d5 2022-09-15 thomas case 'b':
9411 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
9412 fc2737d5 2022-09-15 thomas view->count = 0;
9415 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
9416 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
9418 fc2737d5 2022-09-15 thomas case 'j':
9419 fc2737d5 2022-09-15 thomas case KEY_DOWN:
9420 fc2737d5 2022-09-15 thomas case CTRL('n'):
9421 fc2737d5 2022-09-15 thomas if (!s->eof)
9422 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9424 fc2737d5 2022-09-15 thomas view->count = 0;
9426 fc2737d5 2022-09-15 thomas case CTRL('d'):
9427 fc2737d5 2022-09-15 thomas case 'd':
9428 fc2737d5 2022-09-15 thomas nscroll /= 2;
9429 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9430 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9431 fc2737d5 2022-09-15 thomas case CTRL('f'):
9432 fc2737d5 2022-09-15 thomas case 'f':
9433 fc2737d5 2022-09-15 thomas case ' ':
9434 fc2737d5 2022-09-15 thomas if (s->eof) {
9435 fc2737d5 2022-09-15 thomas view->count = 0;
9438 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9439 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9440 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9441 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9442 fc2737d5 2022-09-15 thomas if (feof(s->f))
9443 fc2737d5 2022-09-15 thomas s->eof = 1;
9445 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9449 fc2737d5 2022-09-15 thomas free(line);
9451 fc2737d5 2022-09-15 thomas default:
9452 fc2737d5 2022-09-15 thomas view->count = 0;
9456 fc2737d5 2022-09-15 thomas return err;
9459 fc2737d5 2022-09-15 thomas static const struct got_error *
9460 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9462 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9464 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9465 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9466 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9467 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9469 fc2737d5 2022-09-15 thomas return NULL;
9472 fc2737d5 2022-09-15 thomas static const struct got_error *
9473 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9475 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9478 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9479 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9481 fc2737d5 2022-09-15 thomas wclear(view->window);
9482 fc2737d5 2022-09-15 thomas view->count = 0;
9483 fc2737d5 2022-09-15 thomas view->x = 0;
9484 fc2737d5 2022-09-15 thomas s->all = !s->all;
9485 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9486 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9487 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9489 fc2737d5 2022-09-15 thomas return create_help(s);
9492 fc2737d5 2022-09-15 thomas static const struct got_error *
9493 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9495 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9496 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9498 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9499 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9500 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9501 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9503 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9504 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9505 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9506 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9507 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9508 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9509 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9511 fc2737d5 2022-09-15 thomas err = create_help(s);
9512 fc2737d5 2022-09-15 thomas return err;
9515 fc2737d5 2022-09-15 thomas static const struct got_error *
9516 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9517 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9519 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9521 2a31b33b 2022-07-23 thomas *new_view = NULL;
9523 2a31b33b 2022-07-23 thomas switch (request) {
9524 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9525 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9526 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9528 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9529 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9530 2a31b33b 2022-07-23 thomas view, s->repo);
9532 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9533 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9535 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9536 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9537 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9539 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9540 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9541 2a31b33b 2022-07-23 thomas s->repo);
9543 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9544 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9546 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9547 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9548 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9549 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9550 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9551 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9552 2a31b33b 2022-07-23 thomas &view->state.tree);
9553 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9554 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9555 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9556 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9558 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9559 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9561 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9562 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9563 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9564 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9565 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9566 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9567 2a31b33b 2022-07-23 thomas view->state.log.repo);
9568 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9569 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9570 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9571 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9573 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9574 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9576 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9577 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9578 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9579 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9580 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9581 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9582 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9583 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9585 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9586 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9587 2a31b33b 2022-07-23 thomas if (err)
9588 2a31b33b 2022-07-23 thomas view_close(*new_view);
9590 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9591 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9592 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9593 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9594 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9595 fc2737d5 2022-09-15 thomas if (err)
9596 fc2737d5 2022-09-15 thomas view_close(*new_view);
9598 2a31b33b 2022-07-23 thomas default:
9599 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9602 2a31b33b 2022-07-23 thomas return err;
9606 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9607 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9609 6458efa5 2020-11-24 stsp static void
9610 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9612 a5d43cac 2022-07-01 thomas switch (view->type) {
9613 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9614 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9615 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9616 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9620 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9621 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9623 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9624 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9627 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9628 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9629 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9631 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9632 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9633 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9635 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9636 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9637 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9639 a5d43cac 2022-07-01 thomas default:
9643 a5d43cac 2022-07-01 thomas view->offset = 0;
9647 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9648 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9650 a5d43cac 2022-07-01 thomas static const struct got_error *
9651 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9653 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9654 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9655 a5d43cac 2022-07-01 thomas int *selected = NULL;
9656 a5d43cac 2022-07-01 thomas int header, offset;
9658 a5d43cac 2022-07-01 thomas switch (view->type) {
9659 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9660 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9661 a5d43cac 2022-07-01 thomas header = 3;
9662 a5d43cac 2022-07-01 thomas scrolld = NULL;
9663 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9664 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9665 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9666 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9667 a5d43cac 2022-07-01 thomas view->offset = offset;
9671 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9672 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9673 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9674 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9675 a5d43cac 2022-07-01 thomas selected = &s->selected;
9678 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9679 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9680 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9681 a5d43cac 2022-07-01 thomas header = 3;
9682 a5d43cac 2022-07-01 thomas selected = &s->selected;
9685 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9686 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9687 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9688 a5d43cac 2022-07-01 thomas header = 5;
9689 a5d43cac 2022-07-01 thomas selected = &s->selected;
9692 a5d43cac 2022-07-01 thomas default:
9693 a5d43cac 2022-07-01 thomas selected = NULL;
9694 a5d43cac 2022-07-01 thomas scrolld = NULL;
9695 a5d43cac 2022-07-01 thomas header = 0;
9699 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9700 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9701 a5d43cac 2022-07-01 thomas view->offset = offset;
9702 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9703 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9704 a5d43cac 2022-07-01 thomas *selected -= offset;
9708 a5d43cac 2022-07-01 thomas return err;
9711 a5d43cac 2022-07-01 thomas static void
9712 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9716 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9717 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9718 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9719 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9721 6879ba42 2020-10-01 naddy fputc('\n', fp);
9724 4ed7e80c 2018-05-20 stsp __dead static void
9725 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9727 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9729 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9730 6879ba42 2020-10-01 naddy getprogname());
9731 6879ba42 2020-10-01 naddy if (hflag) {
9732 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9733 6879ba42 2020-10-01 naddy list_commands(fp);
9735 6879ba42 2020-10-01 naddy exit(status);
9738 c2301be8 2018-04-30 stsp static char **
9739 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9741 ee85c5e8 2020-02-29 stsp va_list ap;
9742 c2301be8 2018-04-30 stsp char **argv;
9745 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9747 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9748 c2301be8 2018-04-30 stsp if (argv == NULL)
9749 c2301be8 2018-04-30 stsp err(1, "calloc");
9750 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9751 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9752 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9753 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9756 ee85c5e8 2020-02-29 stsp va_end(ap);
9757 c2301be8 2018-04-30 stsp return argv;
9761 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9762 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9763 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9764 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9765 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9767 ee85c5e8 2020-02-29 stsp static const struct got_error *
9768 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9770 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9771 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9772 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9773 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9774 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9775 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9776 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9777 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9778 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9780 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9781 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9782 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9784 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9785 7cd52833 2022-06-23 thomas if (error != NULL)
9786 7cd52833 2022-06-23 thomas goto done;
9788 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9789 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9790 ee85c5e8 2020-02-29 stsp goto done;
9792 ee85c5e8 2020-02-29 stsp if (worktree)
9793 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9795 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9796 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9797 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9798 ee85c5e8 2020-02-29 stsp goto done;
9801 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9802 ee85c5e8 2020-02-29 stsp if (error != NULL)
9803 ee85c5e8 2020-02-29 stsp goto done;
9805 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9806 ee85c5e8 2020-02-29 stsp repo, worktree);
9807 ee85c5e8 2020-02-29 stsp if (error)
9808 ee85c5e8 2020-02-29 stsp goto done;
9810 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9811 84de9106 2020-12-26 stsp if (error)
9812 84de9106 2020-12-26 stsp goto done;
9813 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9814 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9815 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9816 ee85c5e8 2020-02-29 stsp if (error)
9817 ee85c5e8 2020-02-29 stsp goto done;
9819 ee85c5e8 2020-02-29 stsp if (worktree) {
9820 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9821 ee85c5e8 2020-02-29 stsp worktree = NULL;
9824 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9825 945f9229 2022-04-16 thomas if (error)
9826 945f9229 2022-04-16 thomas goto done;
9828 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9829 ee85c5e8 2020-02-29 stsp if (error) {
9830 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9831 ee85c5e8 2020-02-29 stsp goto done;
9832 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9833 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9834 6879ba42 2020-10-01 naddy usage(1, 1);
9835 ee85c5e8 2020-02-29 stsp /* not reached */
9838 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9839 ee85c5e8 2020-02-29 stsp if (error)
9840 ee85c5e8 2020-02-29 stsp goto done;
9842 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9844 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9845 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9847 1d0f4054 2021-06-17 stsp if (repo) {
9848 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9849 1d0f4054 2021-06-17 stsp if (error == NULL)
9850 1d0f4054 2021-06-17 stsp error = close_err;
9852 945f9229 2022-04-16 thomas if (commit)
9853 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9854 ee85c5e8 2020-02-29 stsp if (worktree)
9855 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9856 7cd52833 2022-06-23 thomas if (pack_fds) {
9857 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9858 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9859 7cd52833 2022-06-23 thomas if (error == NULL)
9860 7cd52833 2022-06-23 thomas error = pack_err;
9863 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9864 ee85c5e8 2020-02-29 stsp free(commit_id);
9865 ee85c5e8 2020-02-29 stsp free(cwd);
9866 ee85c5e8 2020-02-29 stsp free(repo_path);
9867 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9868 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9870 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9871 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9872 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9874 87670572 2020-12-26 naddy tog_free_refs();
9875 ee85c5e8 2020-02-29 stsp return error;
9879 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9881 1d98034b 2023-04-14 thomas const struct got_error *io_err, *error = NULL;
9882 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9883 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9884 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9885 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9886 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9887 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9889 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9890 557d3365 2023-04-14 thomas const char *test_script_path;
9892 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
9895 557d3365 2023-04-14 thomas * Test mode init must happen before pledge() because "tty" will
9896 557d3365 2023-04-14 thomas * not allow TTY-related ioctls to occur via regular files.
9898 fa9bb690 2023-04-22 thomas test_script_path = getenv("TOG_TEST_SCRIPT");
9899 557d3365 2023-04-14 thomas if (test_script_path != NULL) {
9900 557d3365 2023-04-14 thomas error = init_mock_term(test_script_path);
9901 557d3365 2023-04-14 thomas if (error) {
9902 557d3365 2023-04-14 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9903 557d3365 2023-04-14 thomas return 1;
9905 5ffbe221 2023-04-22 thomas } else if (!isatty(STDIN_FILENO))
9906 5ffbe221 2023-04-22 thomas errx(1, "standard input is not a tty");
9908 557d3365 2023-04-14 thomas #if !defined(PROFILE)
9909 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9910 a0abeae5 2023-02-17 thomas NULL) == -1)
9911 a0abeae5 2023-02-17 thomas err(1, "pledge");
9914 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9915 9f7d7167 2018-04-29 stsp switch (ch) {
9917 9f7d7167 2018-04-29 stsp hflag = 1;
9920 53ccebc2 2019-07-30 stsp Vflag = 1;
9923 6879ba42 2020-10-01 naddy usage(hflag, 1);
9924 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9928 9f7d7167 2018-04-29 stsp argc -= optind;
9929 9f7d7167 2018-04-29 stsp argv += optind;
9930 9814e6a3 2020-09-27 naddy optind = 1;
9931 c2301be8 2018-04-30 stsp optreset = 1;
9933 53ccebc2 2019-07-30 stsp if (Vflag) {
9934 53ccebc2 2019-07-30 stsp got_version_print_str();
9935 6879ba42 2020-10-01 naddy return 0;
9938 c2301be8 2018-04-30 stsp if (argc == 0) {
9939 f29d3e89 2018-06-23 stsp if (hflag)
9940 6879ba42 2020-10-01 naddy usage(hflag, 0);
9941 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9942 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9944 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9948 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9949 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9950 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9951 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9952 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9958 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9959 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9960 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9961 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9962 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9963 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9966 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9967 ee85c5e8 2020-02-29 stsp if (argc != 1)
9968 6879ba42 2020-10-01 naddy usage(0, 1);
9969 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9970 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9972 ee85c5e8 2020-02-29 stsp if (hflag)
9973 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9975 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9978 1d98034b 2023-04-14 thomas if (using_mock_io) {
9979 1d98034b 2023-04-14 thomas io_err = tog_io_close();
9980 1d98034b 2023-04-14 thomas if (error == NULL)
9981 1d98034b 2023-04-14 thomas error = io_err;
9984 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9986 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9987 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9988 a2f4a359 2020-02-28 stsp free(cmd_argv);
9991 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9992 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9993 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9994 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9995 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9996 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);