2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 fc2737d5 2022-09-15 thomas TOG_VIEW_HELP
114 fc2737d5 2022-09-15 thomas /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 fc2737d5 2022-09-15 thomas enum tog_keymap_type {
116 fc2737d5 2022-09-15 thomas TOG_KEYMAP_KEYS = -2,
117 fc2737d5 2022-09-15 thomas TOG_KEYMAP_GLOBAL,
118 fc2737d5 2022-09-15 thomas TOG_KEYMAP_DIFF,
119 fc2737d5 2022-09-15 thomas TOG_KEYMAP_LOG,
120 fc2737d5 2022-09-15 thomas TOG_KEYMAP_BLAME,
121 fc2737d5 2022-09-15 thomas TOG_KEYMAP_TREE,
122 fc2737d5 2022-09-15 thomas TOG_KEYMAP_REF,
123 fc2737d5 2022-09-15 thomas TOG_KEYMAP_HELP
126 a5d43cac 2022-07-01 thomas enum tog_view_mode {
127 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
128 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
129 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
132 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
134 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
136 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
137 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
138 ba4f502b 2018-08-04 stsp struct got_object_id *id;
139 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
142 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 ba4f502b 2018-08-04 stsp struct commit_queue {
144 ba4f502b 2018-08-04 stsp int ncommits;
145 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
148 f26dddb7 2019-11-08 stsp struct tog_color {
149 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
150 6d17833f 2019-11-08 stsp regex_t regex;
151 6d17833f 2019-11-08 stsp short colorpair;
153 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
155 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
157 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
159 2183bbf6 2022-01-23 thomas static const struct got_error *
160 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 2183bbf6 2022-01-23 thomas struct got_reference* re2)
163 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
164 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
165 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
167 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
168 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
171 2183bbf6 2022-01-23 thomas *cmp = -1;
172 2183bbf6 2022-01-23 thomas return NULL;
173 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
174 2183bbf6 2022-01-23 thomas *cmp = 1;
175 2183bbf6 2022-01-23 thomas return NULL;
178 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
179 2183bbf6 2022-01-23 thomas return NULL;
182 11b20872 2019-11-08 stsp static const struct got_error *
183 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
185 51a10b52 2020-12-26 stsp const struct got_error *err;
187 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
191 51a10b52 2020-12-26 stsp return err;
193 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
197 51a10b52 2020-12-26 stsp static void
198 51a10b52 2020-12-26 stsp tog_free_refs(void)
200 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
201 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
202 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
204 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
207 51a10b52 2020-12-26 stsp static const struct got_error *
208 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
209 11b20872 2019-11-08 stsp int idx, short color)
211 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
212 11b20872 2019-11-08 stsp struct tog_color *tc;
213 11b20872 2019-11-08 stsp int regerr = 0;
215 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
216 11b20872 2019-11-08 stsp return NULL;
218 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
220 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
221 11b20872 2019-11-08 stsp if (tc == NULL)
222 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
223 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
224 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
225 11b20872 2019-11-08 stsp if (regerr) {
226 11b20872 2019-11-08 stsp static char regerr_msg[512];
227 11b20872 2019-11-08 stsp static char err_msg[512];
228 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
229 11b20872 2019-11-08 stsp sizeof(regerr_msg));
230 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
231 11b20872 2019-11-08 stsp regerr_msg);
232 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
234 11b20872 2019-11-08 stsp return err;
236 11b20872 2019-11-08 stsp tc->colorpair = idx;
237 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
238 11b20872 2019-11-08 stsp return NULL;
241 11b20872 2019-11-08 stsp static void
242 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
244 11b20872 2019-11-08 stsp struct tog_color *tc;
246 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
247 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
248 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
249 11b20872 2019-11-08 stsp regfree(&tc->regex);
254 ef20f542 2022-06-26 thomas static struct tog_color *
255 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
257 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
259 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
260 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
264 11b20872 2019-11-08 stsp return NULL;
268 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
273 11b20872 2019-11-08 stsp return COLOR_CYAN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
275 11b20872 2019-11-08 stsp return COLOR_YELLOW;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
277 11b20872 2019-11-08 stsp return COLOR_GREEN;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
283 91b8c405 2020-01-25 stsp return COLOR_CYAN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
285 11b20872 2019-11-08 stsp return COLOR_GREEN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
287 11b20872 2019-11-08 stsp return COLOR_GREEN;
288 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
289 11b20872 2019-11-08 stsp return COLOR_CYAN;
290 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
291 11b20872 2019-11-08 stsp return COLOR_YELLOW;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_GREEN;
294 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
296 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
297 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
298 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
299 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
305 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
307 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
309 11b20872 2019-11-08 stsp if (val == NULL)
310 11b20872 2019-11-08 stsp return default_color_value(envvar);
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
313 11b20872 2019-11-08 stsp return COLOR_BLACK;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
315 11b20872 2019-11-08 stsp return COLOR_RED;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
317 11b20872 2019-11-08 stsp return COLOR_GREEN;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
319 11b20872 2019-11-08 stsp return COLOR_YELLOW;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
321 11b20872 2019-11-08 stsp return COLOR_BLUE;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
323 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
325 11b20872 2019-11-08 stsp return COLOR_CYAN;
326 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
327 11b20872 2019-11-08 stsp return COLOR_WHITE;
328 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
331 11b20872 2019-11-08 stsp return default_color_value(envvar);
334 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
335 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
336 3dbaef42 2020-11-24 stsp const char *label1, *label2;
337 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
338 19a6a6b5 2022-07-01 thomas int fd1, fd2;
339 82c78e96 2022-08-06 thomas int lineno;
340 15a087fe 2019-02-21 stsp int first_displayed_line;
341 15a087fe 2019-02-21 stsp int last_displayed_line;
343 15a087fe 2019-02-21 stsp int diff_context;
344 3dbaef42 2020-11-24 stsp int ignore_whitespace;
345 64453f7e 2020-11-21 stsp int force_text_diff;
346 15a087fe 2019-02-21 stsp struct got_repository *repo;
347 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
348 fe621944 2020-11-10 stsp size_t nlines;
349 f44b1f58 2020-02-02 tracey int matched_line;
350 f44b1f58 2020-02-02 tracey int selected_line;
352 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
353 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
356 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
359 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
360 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
361 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
362 1a76625f 2018-10-22 stsp int commits_needed;
363 fb280deb 2021-08-30 stsp int load_all;
364 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
365 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
366 1a76625f 2018-10-22 stsp const char *in_repo_path;
367 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp struct got_repository *repo;
369 1af5eddf 2022-06-23 thomas int *pack_fds;
370 1a76625f 2018-10-22 stsp int log_complete;
371 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
372 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
373 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
374 13add988 2019-10-15 stsp int *searching;
375 13add988 2019-10-15 stsp int *search_next_done;
376 13add988 2019-10-15 stsp regex_t *regex;
377 7e8004ba 2022-09-11 thomas int *limiting;
378 7e8004ba 2022-09-11 thomas int limit_match;
379 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
380 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
383 1a76625f 2018-10-22 stsp struct tog_log_view_state {
384 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
385 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
386 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
387 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
388 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
389 b01e7d3b 2018-08-04 stsp int selected;
390 b01e7d3b 2018-08-04 stsp char *in_repo_path;
391 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
392 b672a97a 2020-01-27 stsp int log_branches;
393 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
394 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
395 1a76625f 2018-10-22 stsp sig_atomic_t quit;
396 1a76625f 2018-10-22 stsp pthread_t thread;
397 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
398 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
399 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
400 11b20872 2019-11-08 stsp struct tog_colors colors;
401 f69c5a46 2022-07-19 thomas int use_committer;
402 7e8004ba 2022-09-11 thomas int limit_view;
403 7e8004ba 2022-09-11 thomas regex_t limit_regex;
404 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
407 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
408 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
409 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
411 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
412 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
413 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
414 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
415 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
416 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
417 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
418 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
419 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
420 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
421 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
425 e9424729 2018-08-04 stsp int nlines;
427 e9424729 2018-08-04 stsp struct tog_view *view;
428 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
432 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
433 e9424729 2018-08-04 stsp const char *path;
434 e9424729 2018-08-04 stsp struct got_repository *repo;
435 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
436 e9424729 2018-08-04 stsp int *complete;
437 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
438 fc06ba56 2019-08-22 stsp void *cancel_arg;
441 e9424729 2018-08-04 stsp struct tog_blame {
443 be659d10 2020-11-18 stsp off_t filesize;
444 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
445 6fcac457 2018-11-19 stsp int nlines;
446 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
447 e9424729 2018-08-04 stsp pthread_t thread;
448 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
449 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
450 e9424729 2018-08-04 stsp const char *path;
451 7cd52833 2022-06-23 thomas int *pack_fds;
454 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
455 7cbe629d 2018-08-04 stsp int first_displayed_line;
456 7cbe629d 2018-08-04 stsp int last_displayed_line;
457 7cbe629d 2018-08-04 stsp int selected_line;
458 4fc71f3b 2022-07-12 thomas int last_diffed_line;
459 7cbe629d 2018-08-04 stsp int blame_complete;
462 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
463 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
464 e5a0f69f 2018-08-18 stsp char *path;
465 7cbe629d 2018-08-04 stsp struct got_repository *repo;
466 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
467 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
468 e9424729 2018-08-04 stsp struct tog_blame blame;
469 6c4c42e0 2019-06-24 stsp int matched_line;
470 11b20872 2019-11-08 stsp struct tog_colors colors;
473 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
474 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
475 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
476 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
478 ad80ab7b 2018-08-04 stsp int selected;
481 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
484 ad80ab7b 2018-08-04 stsp char *tree_label;
485 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
486 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
487 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
489 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
490 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
491 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
492 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
493 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
494 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
495 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
496 6458efa5 2020-11-24 stsp struct tog_colors colors;
499 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
500 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
501 6458efa5 2020-11-24 stsp struct got_reference *ref;
505 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
508 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
512 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 6458efa5 2020-11-24 stsp struct got_repository *repo;
514 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
515 bddb1296 2019-11-08 stsp struct tog_colors colors;
518 fc2737d5 2022-09-15 thomas struct tog_help_view_state {
520 fc2737d5 2022-09-15 thomas off_t *line_offsets;
521 fc2737d5 2022-09-15 thomas size_t nlines;
522 fc2737d5 2022-09-15 thomas int lineno;
523 fc2737d5 2022-09-15 thomas int first_displayed_line;
524 fc2737d5 2022-09-15 thomas int last_displayed_line;
526 fc2737d5 2022-09-15 thomas int matched_line;
527 fc2737d5 2022-09-15 thomas int selected_line;
529 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
532 fc2737d5 2022-09-15 thomas #define GENERATE_HELP \
533 fc2737d5 2022-09-15 thomas KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 fc2737d5 2022-09-15 thomas KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 fc2737d5 2022-09-15 thomas KEY_("k C-p Up", "Move cursor or page up one line"), \
536 fc2737d5 2022-09-15 thomas KEY_("j C-n Down", "Move cursor or page down one line"), \
537 fc2737d5 2022-09-15 thomas KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 fc2737d5 2022-09-15 thomas KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 fc2737d5 2022-09-15 thomas KEY_("C-u u", "Scroll the view up one half page"), \
540 fc2737d5 2022-09-15 thomas KEY_("C-d d", "Scroll the view down one half page"), \
541 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
542 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
543 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
544 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
545 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
546 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
547 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
548 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
549 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
550 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
551 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
552 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
553 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
554 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
555 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
556 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
557 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
559 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
560 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
561 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
562 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
563 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
564 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
565 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
566 fc2737d5 2022-09-15 thomas " commit"), \
567 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
568 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
569 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
570 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
572 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
573 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
574 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
575 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
576 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
577 fc2737d5 2022-09-15 thomas " data"), \
578 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
579 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
580 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
581 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
582 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
583 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
584 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
586 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
587 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
588 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
589 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
590 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
591 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
592 fc2737d5 2022-09-15 thomas " selected line's commit"), \
593 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
594 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
596 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
597 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
598 fc2737d5 2022-09-15 thomas " selected file"), \
599 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
600 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
601 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
602 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
604 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
605 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
606 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
607 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
608 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
609 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
610 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
612 fc2737d5 2022-09-15 thomas struct tog_key_map {
613 fc2737d5 2022-09-15 thomas const char *keys;
614 fc2737d5 2022-09-15 thomas const char *info;
615 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
619 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
621 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
622 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
623 669b5ffa 2018-10-07 stsp * there is enough screen estate.
625 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
626 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
628 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
629 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
630 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
632 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
633 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
635 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
637 cc3c9aac 2018-08-01 stsp struct tog_view {
638 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
639 26ed57b2 2018-05-19 stsp WINDOW *window;
640 26ed57b2 2018-05-19 stsp PANEL *panel;
641 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
642 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
643 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
644 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
645 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
646 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
647 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
648 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
649 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
651 669b5ffa 2018-10-07 stsp struct tog_view *parent;
652 669b5ffa 2018-10-07 stsp struct tog_view *child;
655 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
656 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
657 e78dc838 2020-12-04 stsp * between parent and child.
658 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
659 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
660 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
661 e78dc838 2020-12-04 stsp * situations.
663 e78dc838 2020-12-04 stsp int focus_child;
665 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
666 5dc9f4bc 2018-08-04 stsp /* type-specific state */
667 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
669 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
670 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
671 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
672 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
673 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
674 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
677 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
678 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
679 e78dc838 2020-12-04 stsp struct tog_view *, int);
680 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
681 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
682 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
684 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
685 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
686 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
687 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
688 c0c4acc8 2021-01-24 stsp int search_started;
689 60493ae3 2019-06-20 stsp int searching;
690 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
691 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
692 60493ae3 2019-06-20 stsp int search_next_done;
693 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
694 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
695 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
696 1803e47f 2019-06-22 stsp regex_t regex;
697 41605754 2020-11-12 stsp regmatch_t regmatch;
700 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
701 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
702 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
703 78756c87 2020-11-24 stsp struct got_repository *);
704 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
705 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
706 e78dc838 2020-12-04 stsp struct tog_view *, int);
707 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
708 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
709 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
710 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
711 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
712 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
714 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
715 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
716 78756c87 2020-11-24 stsp const char *, const char *, int);
717 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
718 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
719 e78dc838 2020-12-04 stsp struct tog_view *, int);
720 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
721 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
722 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
723 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
725 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
726 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
727 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
728 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
729 e78dc838 2020-12-04 stsp struct tog_view *, int);
730 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
731 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
732 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
733 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
734 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
736 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
737 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
738 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
739 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
740 e78dc838 2020-12-04 stsp struct tog_view *, int);
741 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
742 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
743 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
745 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
746 6458efa5 2020-11-24 stsp struct got_repository *);
747 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
748 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
749 e78dc838 2020-12-04 stsp struct tog_view *, int);
750 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
751 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
752 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
754 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
755 2a7d3cd7 2022-09-17 thomas struct tog_view *);
756 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
757 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
758 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
759 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
760 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
761 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
762 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
763 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
765 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
766 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
767 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
768 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
769 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
771 25791caa 2018-10-24 stsp static void
772 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
774 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
777 83baff54 2019-08-12 stsp static void
778 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
780 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
783 61266923 2020-01-14 stsp static void
784 61266923 2020-01-14 stsp tog_sigcont(int signo)
786 61266923 2020-01-14 stsp tog_sigcont_received = 1;
789 296152d1 2022-05-31 thomas static void
790 296152d1 2022-05-31 thomas tog_sigint(int signo)
792 296152d1 2022-05-31 thomas tog_sigint_received = 1;
795 296152d1 2022-05-31 thomas static void
796 296152d1 2022-05-31 thomas tog_sigterm(int signo)
798 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
801 296152d1 2022-05-31 thomas static int
802 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
804 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
805 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
808 e5a0f69f 2018-08-18 stsp static const struct got_error *
809 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
811 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
813 669b5ffa 2018-10-07 stsp if (view->child) {
814 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
815 669b5ffa 2018-10-07 stsp view->child = NULL;
817 e5a0f69f 2018-08-18 stsp if (view->close)
818 e5a0f69f 2018-08-18 stsp err = view->close(view);
819 ea5e7bb5 2018-08-01 stsp if (view->panel)
820 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
821 ea5e7bb5 2018-08-01 stsp if (view->window)
822 ea5e7bb5 2018-08-01 stsp delwin(view->window);
823 ea5e7bb5 2018-08-01 stsp free(view);
824 f2d749db 2022-07-12 thomas return err ? err : child_err;
827 ea5e7bb5 2018-08-01 stsp static struct tog_view *
828 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
829 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
831 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
833 ea5e7bb5 2018-08-01 stsp if (view == NULL)
834 ea5e7bb5 2018-08-01 stsp return NULL;
836 d6b05b5b 2018-08-04 stsp view->type = type;
837 f7d12f7e 2018-08-01 stsp view->lines = LINES;
838 f7d12f7e 2018-08-01 stsp view->cols = COLS;
839 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
840 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
841 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
842 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
843 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
844 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
845 96a765a8 2018-08-04 stsp view_close(view);
846 ea5e7bb5 2018-08-01 stsp return NULL;
848 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
849 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
850 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
851 96a765a8 2018-08-04 stsp view_close(view);
852 ea5e7bb5 2018-08-01 stsp return NULL;
855 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
856 ea5e7bb5 2018-08-01 stsp return view;
860 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
862 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
864 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
867 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
868 a5d43cac 2022-07-01 thomas static int
869 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
871 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
874 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
876 5c60c32a 2018-10-18 stsp static const struct got_error *
877 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
879 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
881 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
882 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
883 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
885 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
886 a5d43cac 2022-07-01 thomas view->begin_x = 0;
887 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
888 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
889 53d2bdd3 2022-07-10 thomas view->cols > 119)
890 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
892 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
893 a5d43cac 2022-07-01 thomas view->begin_y = 0;
895 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
896 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
897 5c60c32a 2018-10-18 stsp view->lines = LINES;
898 5c60c32a 2018-10-18 stsp view->cols = COLS;
899 5c60c32a 2018-10-18 stsp err = view_resize(view);
901 5c60c32a 2018-10-18 stsp return err;
903 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
904 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
906 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
907 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
909 5c60c32a 2018-10-18 stsp return NULL;
912 5c60c32a 2018-10-18 stsp static const struct got_error *
913 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
915 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
917 5c60c32a 2018-10-18 stsp view->begin_x = 0;
918 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
919 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
920 5c60c32a 2018-10-18 stsp view->ncols = COLS;
921 5c60c32a 2018-10-18 stsp view->lines = LINES;
922 5c60c32a 2018-10-18 stsp view->cols = COLS;
923 5c60c32a 2018-10-18 stsp err = view_resize(view);
925 5c60c32a 2018-10-18 stsp return err;
927 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
928 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
930 5c60c32a 2018-10-18 stsp return NULL;
934 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
936 5c60c32a 2018-10-18 stsp return view->parent == NULL;
939 b65b3ea0 2022-06-23 thomas static int
940 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
942 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
945 e44940c3 2022-07-01 thomas static int
946 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
948 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
951 444d5325 2022-07-03 thomas static int
952 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
954 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
955 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
958 a5d43cac 2022-07-01 thomas static void
959 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
961 a5d43cac 2022-07-01 thomas PANEL *panel;
962 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
964 a5d43cac 2022-07-01 thomas if (view->parent)
965 a5d43cac 2022-07-01 thomas return view_border(view->parent);
967 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
968 a5d43cac 2022-07-01 thomas if (panel == NULL)
971 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
972 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
973 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
974 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
975 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
977 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
978 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
981 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
982 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
983 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
984 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
985 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
987 4d8c2215 2018-08-19 stsp static const struct got_error *
988 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
990 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
991 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
993 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
995 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
996 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
998 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
999 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1000 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1002 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1004 4918811f 2022-07-01 thomas if (view->child) {
1005 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1007 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1008 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1009 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1010 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1011 40236d76 2022-06-23 thomas ncols = COLS;
1013 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1014 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1015 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1017 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1019 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1021 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1022 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1025 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1026 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1027 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1029 a5d43cac 2022-07-01 thomas if (hs) {
1030 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1031 a5d43cac 2022-07-01 thomas if (err)
1032 a5d43cac 2022-07-01 thomas return err;
1033 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1034 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1035 a5d43cac 2022-07-01 thomas if (err)
1036 a5d43cac 2022-07-01 thomas return err;
1038 a5d43cac 2022-07-01 thomas view_border(view);
1039 a5d43cac 2022-07-01 thomas update_panels();
1040 a5d43cac 2022-07-01 thomas doupdate();
1041 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1042 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1044 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1045 40236d76 2022-06-23 thomas ncols = COLS;
1047 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1048 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1049 ea0bff04 2022-07-19 thomas if (err)
1050 ea0bff04 2022-07-19 thomas return err;
1053 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1054 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1055 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1056 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1057 40236d76 2022-06-23 thomas wclear(view->window);
1059 40236d76 2022-06-23 thomas view->nlines = nlines;
1060 40236d76 2022-06-23 thomas view->ncols = ncols;
1061 40236d76 2022-06-23 thomas view->lines = LINES;
1062 40236d76 2022-06-23 thomas view->cols = COLS;
1064 5c60c32a 2018-10-18 stsp return NULL;
1067 ea0bff04 2022-07-19 thomas static const struct got_error *
1068 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1070 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1071 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1072 3a0139e8 2022-07-22 thomas int n = 0;
1074 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1075 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1078 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1079 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1081 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1082 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1083 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1086 ea0bff04 2022-07-19 thomas return err;
1089 97cb21cd 2022-07-12 thomas static void
1090 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1092 97cb21cd 2022-07-12 thomas if (n == 0)
1095 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1096 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1097 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1099 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1100 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1101 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1102 97cb21cd 2022-07-12 thomas view->offset -= n;
1104 97cb21cd 2022-07-12 thomas view->offset = 0;
1108 53d2bdd3 2022-07-10 thomas static const struct got_error *
1109 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1111 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1112 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1114 53d2bdd3 2022-07-10 thomas if (view->parent)
1115 53d2bdd3 2022-07-10 thomas v = view->parent;
1117 53d2bdd3 2022-07-10 thomas v = view;
1119 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1120 53d2bdd3 2022-07-10 thomas return NULL;
1122 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1124 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1125 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1126 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1127 53d2bdd3 2022-07-10 thomas if (view->parent)
1128 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1130 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1131 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1132 53d2bdd3 2022-07-10 thomas view->count = 0;
1133 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1134 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1135 53d2bdd3 2022-07-10 thomas view->count = 0;
1136 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1138 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1139 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1140 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1141 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1142 53d2bdd3 2022-07-10 thomas if (err)
1143 53d2bdd3 2022-07-10 thomas return err;
1144 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1145 53d2bdd3 2022-07-10 thomas } else {
1146 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1147 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1148 53d2bdd3 2022-07-10 thomas if (view->parent)
1149 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1151 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1152 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1153 53d2bdd3 2022-07-10 thomas view->count = 0;
1154 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1155 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1156 53d2bdd3 2022-07-10 thomas view->count = 0;
1157 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1159 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1162 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1163 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1164 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1165 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1167 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1168 53d2bdd3 2022-07-10 thomas if (err)
1169 53d2bdd3 2022-07-10 thomas return err;
1170 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1171 53d2bdd3 2022-07-10 thomas if (err)
1172 53d2bdd3 2022-07-10 thomas return err;
1174 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1175 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1176 53d2bdd3 2022-07-10 thomas if (err)
1177 53d2bdd3 2022-07-10 thomas return err;
1180 3a0139e8 2022-07-22 thomas if (v->resize)
1181 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1182 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1183 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1185 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1187 53d2bdd3 2022-07-10 thomas return err;
1190 53d2bdd3 2022-07-10 thomas static void
1191 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1193 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1195 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1196 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1199 669b5ffa 2018-10-07 stsp static const struct got_error *
1200 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1202 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1204 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1205 669b5ffa 2018-10-07 stsp return NULL;
1207 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1208 669b5ffa 2018-10-07 stsp view->child = NULL;
1209 669b5ffa 2018-10-07 stsp return err;
1212 40236d76 2022-06-23 thomas static const struct got_error *
1213 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1215 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1217 669b5ffa 2018-10-07 stsp view->child = child;
1218 669b5ffa 2018-10-07 stsp child->parent = view;
1220 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1221 53d2bdd3 2022-07-10 thomas if (err)
1222 53d2bdd3 2022-07-10 thomas return err;
1224 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1225 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1227 53d2bdd3 2022-07-10 thomas return err;
1230 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1231 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1233 2a31b33b 2022-07-23 thomas static const struct got_error *
1234 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1235 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1237 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1238 2a31b33b 2022-07-23 thomas const struct got_error *err;
1239 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1241 2a31b33b 2022-07-23 thomas *requested = NULL;
1243 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1244 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1246 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1247 2a31b33b 2022-07-23 thomas if (err)
1248 2a31b33b 2022-07-23 thomas return err;
1250 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1251 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1252 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1253 2a31b33b 2022-07-23 thomas if (err)
1254 2a31b33b 2022-07-23 thomas return err;
1257 2a31b33b 2022-07-23 thomas view->focussed = 0;
1258 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1259 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1260 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1261 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1263 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1264 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1265 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1266 2a31b33b 2022-07-23 thomas if (err)
1267 2a31b33b 2022-07-23 thomas return err;
1268 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1269 2a31b33b 2022-07-23 thomas if (err)
1270 2a31b33b 2022-07-23 thomas return err;
1271 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1273 2a31b33b 2022-07-23 thomas *requested = new_view;
1275 2a31b33b 2022-07-23 thomas return NULL;
1278 34bc9ec9 2019-02-22 stsp static void
1279 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1281 25791caa 2018-10-24 stsp int cols, lines;
1282 25791caa 2018-10-24 stsp struct winsize size;
1284 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1285 25791caa 2018-10-24 stsp cols = 80; /* Default */
1286 25791caa 2018-10-24 stsp lines = 24;
1288 25791caa 2018-10-24 stsp cols = size.ws_col;
1289 25791caa 2018-10-24 stsp lines = size.ws_row;
1291 25791caa 2018-10-24 stsp resize_term(lines, cols);
1294 2b49a8ae 2019-06-22 stsp static const struct got_error *
1295 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1297 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1298 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1299 2b49a8ae 2019-06-22 stsp char pattern[1024];
1302 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1303 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1304 c0c4acc8 2021-01-24 stsp view->searching = 0;
1305 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1307 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1309 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1310 2b49a8ae 2019-06-22 stsp return NULL;
1312 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1313 a5d43cac 2022-07-01 thomas v = view->child;
1314 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1315 d07291c6 2022-12-30 thomas v = view->parent;
1317 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1318 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1320 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1321 2b49a8ae 2019-06-22 stsp nocbreak();
1323 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1324 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1327 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1328 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1329 2b49a8ae 2019-06-22 stsp return NULL;
1331 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1332 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1333 7c32bd05 2019-06-22 stsp if (err) {
1334 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1335 7c32bd05 2019-06-22 stsp return err;
1337 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1338 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1339 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1340 2b49a8ae 2019-06-22 stsp view->search_next(view);
1343 2b49a8ae 2019-06-22 stsp return NULL;
1346 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1347 64486692 2022-07-07 thomas static const struct got_error *
1348 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1350 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1351 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1353 64486692 2022-07-07 thomas if (view->parent)
1354 64486692 2022-07-07 thomas v = view->parent;
1356 64486692 2022-07-07 thomas v = view;
1358 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1359 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1361 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1363 ddbc4d37 2022-07-12 thomas if (!v->child)
1364 ddbc4d37 2022-07-12 thomas return NULL;
1365 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1366 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1368 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1369 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1370 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1371 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1372 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1375 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1376 64486692 2022-07-07 thomas v->ncols = COLS;
1377 64486692 2022-07-07 thomas v->child->ncols = COLS;
1378 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1380 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1381 64486692 2022-07-07 thomas if (err)
1382 64486692 2022-07-07 thomas return err;
1384 64486692 2022-07-07 thomas v->child->mode = v->mode;
1385 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1386 64486692 2022-07-07 thomas v->focus_child = 1;
1388 64486692 2022-07-07 thomas err = view_fullscreen(v);
1389 64486692 2022-07-07 thomas if (err)
1390 64486692 2022-07-07 thomas return err;
1391 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1392 64486692 2022-07-07 thomas if (err)
1393 64486692 2022-07-07 thomas return err;
1395 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1396 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1397 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1398 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1399 cf1fe301 2022-07-29 thomas if (err)
1400 cf1fe301 2022-07-29 thomas return err;
1401 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1402 cf1fe301 2022-07-29 thomas if (err)
1403 cf1fe301 2022-07-29 thomas return err;
1404 ddbc4d37 2022-07-12 thomas } else {
1405 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1406 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1408 f4e6231a 2022-07-22 thomas if (v->resize)
1409 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1410 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1411 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1413 64486692 2022-07-07 thomas return err;
1417 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1418 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1420 07b0611c 2022-06-23 thomas static int
1421 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1423 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1424 a5d43cac 2022-07-01 thomas int x, n = 0;
1426 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1427 a5d43cac 2022-07-01 thomas v = view->child;
1428 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1429 a5d43cac 2022-07-01 thomas v = view->parent;
1431 07b0611c 2022-06-23 thomas view->count = 0;
1432 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1433 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1434 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1435 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1436 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1439 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1440 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1441 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1442 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1446 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1447 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1449 07b0611c 2022-06-23 thomas if (n >= 9999999)
1450 07b0611c 2022-06-23 thomas n = 9999999;
1452 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1453 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1455 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1456 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1461 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1462 07b0611c 2022-06-23 thomas view->count = n;
1464 07b0611c 2022-06-23 thomas return c;
1467 0cf4efb1 2018-09-29 stsp static const struct got_error *
1468 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1469 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1471 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1472 669b5ffa 2018-10-07 stsp struct tog_view *v;
1473 1a76625f 2018-10-22 stsp int ch, errcode;
1475 e5a0f69f 2018-08-18 stsp *new = NULL;
1477 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1478 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1479 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1480 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1481 07b0611c 2022-06-23 thomas view->count = 0;
1484 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1485 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1486 82954512 2020-02-03 stsp if (errcode)
1487 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1488 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1489 3da8ef85 2021-09-21 stsp sched_yield();
1490 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1491 82954512 2020-02-03 stsp if (errcode)
1492 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1493 82954512 2020-02-03 stsp "pthread_mutex_lock");
1494 60493ae3 2019-06-20 stsp view->search_next(view);
1495 60493ae3 2019-06-20 stsp return NULL;
1498 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1499 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1500 1a76625f 2018-10-22 stsp if (errcode)
1501 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1502 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1503 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1504 634cb454 2022-07-03 thomas cbreak();
1505 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1506 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1507 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1508 634cb454 2022-07-03 thomas view->count = 0;
1510 634cb454 2022-07-03 thomas ch = view->ch;
1511 634cb454 2022-07-03 thomas } else {
1512 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1513 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1514 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1516 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1517 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1518 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1519 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1520 1a76625f 2018-10-22 stsp if (errcode)
1521 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1523 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1524 25791caa 2018-10-24 stsp tog_resizeterm();
1525 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1526 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1527 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1528 25791caa 2018-10-24 stsp err = view_resize(v);
1530 25791caa 2018-10-24 stsp return err;
1531 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1533 25791caa 2018-10-24 stsp return err;
1534 cdfcfb03 2020-12-06 stsp if (v->child) {
1535 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1537 cdfcfb03 2020-12-06 stsp return err;
1538 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1539 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1541 cdfcfb03 2020-12-06 stsp return err;
1542 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1543 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1544 53d2bdd3 2022-07-10 thomas if (err)
1545 53d2bdd3 2022-07-10 thomas return err;
1551 e5a0f69f 2018-08-18 stsp switch (ch) {
1552 fc2737d5 2022-09-15 thomas case '?':
1553 fc2737d5 2022-09-15 thomas case 'H':
1554 fc2737d5 2022-09-15 thomas case KEY_F(1):
1555 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1556 fc2737d5 2022-09-15 thomas err = view->reset(view);
1558 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1561 07b0611c 2022-06-23 thomas view->count = 0;
1562 1e37a5c2 2019-05-12 jcs if (view->child) {
1563 e78dc838 2020-12-04 stsp view->focussed = 0;
1564 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1565 e78dc838 2020-12-04 stsp view->focus_child = 1;
1566 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1567 e78dc838 2020-12-04 stsp view->focussed = 0;
1568 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1569 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1570 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1571 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1572 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1574 a5d43cac 2022-07-01 thomas if (err)
1575 a5d43cac 2022-07-01 thomas return err;
1577 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1578 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1579 a5d43cac 2022-07-01 thomas if (err)
1580 a5d43cac 2022-07-01 thomas return err;
1585 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1586 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1587 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1588 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1589 a5d43cac 2022-07-01 thomas if (err)
1592 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1594 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1595 9970f7fc 2020-12-03 stsp view->dying = 1;
1600 1c5e5faa 2022-06-23 thomas case 'F':
1601 07b0611c 2022-06-23 thomas view->count = 0;
1602 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1603 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1605 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1606 e78dc838 2020-12-04 stsp view->focussed = 0;
1607 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1608 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1609 53d2bdd3 2022-07-10 thomas } else {
1610 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1611 53d2bdd3 2022-07-10 thomas if (!err)
1612 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1616 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1617 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1619 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1620 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1621 e78dc838 2020-12-04 stsp view->focussed = 1;
1622 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1624 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1625 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1626 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1627 53d2bdd3 2022-07-10 thomas if (!err)
1628 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1632 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1634 a5d43cac 2022-07-01 thomas if (err)
1636 3a0139e8 2022-07-22 thomas if (view->resize) {
1637 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1638 a5d43cac 2022-07-01 thomas if (err)
1641 a5d43cac 2022-07-01 thomas if (view->parent)
1642 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1643 a5d43cac 2022-07-01 thomas if (!err)
1644 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1646 64486692 2022-07-07 thomas case 'S':
1647 53d2bdd3 2022-07-10 thomas view->count = 0;
1648 64486692 2022-07-07 thomas err = switch_split(view);
1650 53d2bdd3 2022-07-10 thomas case '-':
1651 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1653 53d2bdd3 2022-07-10 thomas case '+':
1654 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1656 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1659 07b0611c 2022-06-23 thomas view->count = 0;
1660 60493ae3 2019-06-20 stsp if (view->search_start)
1661 2b49a8ae 2019-06-22 stsp view_search_start(view);
1663 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1667 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1668 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1669 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1670 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1671 60493ae3 2019-06-20 stsp view->search_next(view);
1673 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1675 adf4c9e0 2022-07-03 thomas case 'A':
1676 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1677 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1679 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1680 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1681 adf4c9e0 2022-07-03 thomas if (v->reset) {
1682 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1683 adf4c9e0 2022-07-03 thomas if (err)
1684 adf4c9e0 2022-07-03 thomas return err;
1686 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1687 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1688 adf4c9e0 2022-07-03 thomas if (err)
1689 adf4c9e0 2022-07-03 thomas return err;
1694 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1698 e5a0f69f 2018-08-18 stsp return err;
1701 ef20f542 2022-06-26 thomas static int
1702 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1704 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1705 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1707 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1709 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1712 669b5ffa 2018-10-07 stsp return view->focussed;
1715 bcbd79e2 2018-08-19 stsp static const struct got_error *
1716 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1718 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1719 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1720 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1721 64486692 2022-07-07 thomas char *mode;
1722 fd823528 2018-10-22 stsp int fast_refresh = 10;
1723 1a76625f 2018-10-22 stsp int done = 0, errcode;
1725 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1726 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1727 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1729 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1731 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1732 1a76625f 2018-10-22 stsp if (errcode)
1733 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1735 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1736 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1738 1004088d 2018-09-29 stsp view->focussed = 1;
1739 878940b7 2018-09-29 stsp err = view->show(view);
1741 0cf4efb1 2018-09-29 stsp return err;
1742 0cf4efb1 2018-09-29 stsp update_panels();
1743 0cf4efb1 2018-09-29 stsp doupdate();
1744 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1745 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1746 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1747 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1748 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1750 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1753 9970f7fc 2020-12-03 stsp if (view->dying) {
1754 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1756 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1757 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1759 e78dc838 2020-12-04 stsp else if (view->parent)
1760 669b5ffa 2018-10-07 stsp prev = view->parent;
1762 e78dc838 2020-12-04 stsp if (view->parent) {
1763 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1764 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1765 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1766 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1767 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1768 40236d76 2022-06-23 thomas if (err)
1770 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1771 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1773 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1775 9970f7fc 2020-12-03 stsp err = view_close(view);
1777 e5a0f69f 2018-08-18 stsp goto done;
1779 e78dc838 2020-12-04 stsp view = NULL;
1780 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1781 e78dc838 2020-12-04 stsp if (v->focussed)
1784 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1785 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1787 e78dc838 2020-12-04 stsp view = prev;
1788 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1789 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1790 e78dc838 2020-12-04 stsp tog_view_list_head);
1792 e78dc838 2020-12-04 stsp if (view) {
1793 e78dc838 2020-12-04 stsp if (view->focus_child) {
1794 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1795 e78dc838 2020-12-04 stsp view = view->child;
1797 e78dc838 2020-12-04 stsp view->focussed = 1;
1801 bcbd79e2 2018-08-19 stsp if (new_view) {
1802 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1803 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1804 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1805 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1807 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1808 86c66b02 2018-10-18 stsp err = view_close(v);
1810 86c66b02 2018-10-18 stsp goto done;
1813 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1814 fed7eaa8 2018-10-24 stsp view = new_view;
1816 669b5ffa 2018-10-07 stsp if (view) {
1817 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1818 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1819 e78dc838 2020-12-04 stsp view = view->child;
1821 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1822 e78dc838 2020-12-04 stsp view = view->parent;
1824 e78dc838 2020-12-04 stsp show_panel(view->panel);
1825 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1826 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1827 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1828 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1830 1a76625f 2018-10-22 stsp goto done;
1832 669b5ffa 2018-10-07 stsp err = view->show(view);
1834 1a76625f 2018-10-22 stsp goto done;
1835 669b5ffa 2018-10-07 stsp if (view->child) {
1836 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1838 1a76625f 2018-10-22 stsp goto done;
1840 1a76625f 2018-10-22 stsp update_panels();
1841 1a76625f 2018-10-22 stsp doupdate();
1845 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1846 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1847 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1848 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1849 f2d749db 2022-07-12 thomas close_err = view_close(view);
1850 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1851 f2d749db 2022-07-12 thomas err = close_err;
1854 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1855 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1856 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1858 e5a0f69f 2018-08-18 stsp return err;
1861 4ed7e80c 2018-05-20 stsp __dead static void
1862 9f7d7167 2018-04-29 stsp usage_log(void)
1865 c70c5802 2018-08-01 stsp fprintf(stderr,
1866 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1867 9f7d7167 2018-04-29 stsp getprogname());
1871 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1872 80ddbec8 2018-04-29 stsp static const struct got_error *
1873 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1875 00dfcb92 2018-06-11 stsp char *vis = NULL;
1876 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1878 963b370f 2018-05-20 stsp *ws = NULL;
1879 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1880 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1881 00dfcb92 2018-06-11 stsp int vislen;
1882 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1883 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1885 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1886 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1888 00dfcb92 2018-06-11 stsp return err;
1889 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1890 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1891 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1892 a7f50699 2018-06-11 stsp goto done;
1896 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1897 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1898 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1899 a7f50699 2018-06-11 stsp goto done;
1902 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1903 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1905 00dfcb92 2018-06-11 stsp free(vis);
1906 963b370f 2018-05-20 stsp if (err) {
1907 963b370f 2018-05-20 stsp free(*ws);
1908 963b370f 2018-05-20 stsp *ws = NULL;
1909 963b370f 2018-05-20 stsp *wlen = 0;
1911 963b370f 2018-05-20 stsp return err;
1914 05171be4 2022-06-23 thomas static const struct got_error *
1915 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1917 05171be4 2022-06-23 thomas char *dst;
1918 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1920 05171be4 2022-06-23 thomas *ptr = NULL;
1921 05171be4 2022-06-23 thomas n = len = strlen(src);
1922 83316834 2022-06-23 thomas dst = malloc(n + 1);
1923 05171be4 2022-06-23 thomas if (dst == NULL)
1924 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1926 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1927 05171be4 2022-06-23 thomas const char c = src[idx];
1929 05171be4 2022-06-23 thomas if (c == '\t') {
1930 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1931 b235ad5d 2022-06-23 thomas char *p;
1933 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1934 83316834 2022-06-23 thomas if (p == NULL) {
1935 83316834 2022-06-23 thomas free(dst);
1936 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1939 83316834 2022-06-23 thomas dst = p;
1940 05171be4 2022-06-23 thomas n += nb;
1941 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1942 05171be4 2022-06-23 thomas sz += nb;
1944 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1948 05171be4 2022-06-23 thomas dst[sz] = '\0';
1949 05171be4 2022-06-23 thomas *ptr = dst;
1950 05171be4 2022-06-23 thomas return NULL;
1954 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1955 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1956 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1957 8d208d34 2022-06-23 thomas * *rcol.
1959 8d208d34 2022-06-23 thomas static int
1960 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1962 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1964 8d208d34 2022-06-23 thomas if (n == 0) {
1965 8d208d34 2022-06-23 thomas *rcol = cols;
1966 8d208d34 2022-06-23 thomas return off;
1969 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1970 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1971 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1973 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1975 8d208d34 2022-06-23 thomas if (width == -1) {
1976 8d208d34 2022-06-23 thomas width = 1;
1977 8d208d34 2022-06-23 thomas wline[i] = L'.';
1980 8d208d34 2022-06-23 thomas if (cols + width > n)
1982 8d208d34 2022-06-23 thomas cols += width;
1985 8d208d34 2022-06-23 thomas *rcol = cols;
1986 8d208d34 2022-06-23 thomas return i;
1990 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1991 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1992 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1994 f91a2b48 2022-06-23 thomas static const struct got_error *
1995 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1996 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1998 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1999 8d208d34 2022-06-23 thomas int cols;
2000 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2001 05171be4 2022-06-23 thomas char *exstr = NULL;
2002 963b370f 2018-05-20 stsp size_t wlen;
2003 8d208d34 2022-06-23 thomas int i, scrollx;
2005 963b370f 2018-05-20 stsp *wlinep = NULL;
2006 b700b5d6 2018-07-10 stsp *widthp = 0;
2008 05171be4 2022-06-23 thomas if (expand) {
2009 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2010 05171be4 2022-06-23 thomas if (err)
2011 05171be4 2022-06-23 thomas return err;
2014 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2015 05171be4 2022-06-23 thomas free(exstr);
2017 963b370f 2018-05-20 stsp return err;
2019 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2021 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2022 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2025 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2026 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2030 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2031 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2033 b700b5d6 2018-07-10 stsp if (widthp)
2034 b700b5d6 2018-07-10 stsp *widthp = cols;
2035 f91a2b48 2022-06-23 thomas if (scrollxp)
2036 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2038 963b370f 2018-05-20 stsp free(wline);
2040 963b370f 2018-05-20 stsp *wlinep = wline;
2041 963b370f 2018-05-20 stsp return err;
2044 8b473291 2019-02-21 stsp static const struct got_error*
2045 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2046 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2048 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2049 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2051 8b473291 2019-02-21 stsp const char *name;
2053 8b473291 2019-02-21 stsp *refs_str = NULL;
2055 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2056 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2057 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2060 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2061 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2063 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2064 8b473291 2019-02-21 stsp name += 5;
2065 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2066 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2068 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2069 8b473291 2019-02-21 stsp name += 6;
2070 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2071 8b473291 2019-02-21 stsp name += 8;
2072 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2073 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2076 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2079 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2080 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2081 5d844a1e 2019-08-13 stsp if (err) {
2082 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2083 48cae60d 2020-09-22 stsp free(ref_id);
2086 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2087 5d844a1e 2019-08-13 stsp err = NULL;
2088 5d844a1e 2019-08-13 stsp tag = NULL;
2091 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2092 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2093 48cae60d 2020-09-22 stsp free(ref_id);
2095 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2096 52b5abe1 2019-08-13 stsp if (cmp != 0)
2098 8b473291 2019-02-21 stsp s = *refs_str;
2099 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2100 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2101 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2103 8b473291 2019-02-21 stsp *refs_str = NULL;
2109 8b473291 2019-02-21 stsp return err;
2112 963b370f 2018-05-20 stsp static const struct got_error *
2113 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2114 27a741e5 2019-09-11 stsp int col_tab_align)
2116 e6b8b890 2020-12-29 naddy char *smallerthan;
2118 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2119 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2120 5813d178 2019-03-09 stsp author = smallerthan + 1;
2121 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2122 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2123 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2126 5813d178 2019-03-09 stsp static const struct got_error *
2127 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2128 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2129 8fdc79fe 2020-12-01 naddy int author_display_cols)
2131 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2132 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2133 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2134 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2135 5813d178 2019-03-09 stsp char *author = NULL;
2136 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2137 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2138 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2139 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2140 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2141 ccb26ccd 2018-11-05 stsp struct tm tm;
2142 45d799e2 2018-12-23 stsp time_t committer_time;
2143 11b20872 2019-11-08 stsp struct tog_color *tc;
2145 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2146 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2147 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2148 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2149 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2151 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2152 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2154 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2155 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2157 11b20872 2019-11-08 stsp wattr_on(view->window,
2158 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2159 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2161 11b20872 2019-11-08 stsp wattr_off(view->window,
2162 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2163 27a741e5 2019-09-11 stsp col = limit;
2164 b39d25c7 2018-07-10 stsp if (col > avail)
2165 b39d25c7 2018-07-10 stsp goto done;
2167 6570a66d 2019-11-08 stsp if (avail >= 120) {
2168 6570a66d 2019-11-08 stsp char *id_str;
2169 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2171 6570a66d 2019-11-08 stsp goto done;
2172 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2174 11b20872 2019-11-08 stsp wattr_on(view->window,
2175 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2176 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2178 11b20872 2019-11-08 stsp wattr_off(view->window,
2179 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2180 6570a66d 2019-11-08 stsp free(id_str);
2182 6570a66d 2019-11-08 stsp if (col > avail)
2183 6570a66d 2019-11-08 stsp goto done;
2186 f69c5a46 2022-07-19 thomas if (s->use_committer)
2187 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2189 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2190 5813d178 2019-03-09 stsp if (author == NULL) {
2191 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2192 80ddbec8 2018-04-29 stsp goto done;
2194 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2196 bb737323 2018-05-20 stsp goto done;
2197 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2199 11b20872 2019-11-08 stsp wattr_on(view->window,
2200 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2201 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2202 bb737323 2018-05-20 stsp col += author_width;
2203 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2204 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2206 bb737323 2018-05-20 stsp author_width++;
2209 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2210 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2211 9c2eaf34 2018-05-20 stsp if (col > avail)
2212 9c2eaf34 2018-05-20 stsp goto done;
2214 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2216 6d9fbc00 2018-04-29 stsp goto done;
2217 bb737323 2018-05-20 stsp logmsg = logmsg0;
2218 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2220 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2221 bb737323 2018-05-20 stsp if (newline)
2222 bb737323 2018-05-20 stsp *newline = '\0';
2223 f91a2b48 2022-06-23 thomas limit = avail - col;
2224 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2225 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2226 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2227 f91a2b48 2022-06-23 thomas limit, col, 1);
2228 331b1a16 2022-06-23 thomas if (err)
2229 331b1a16 2022-06-23 thomas goto done;
2230 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2231 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2232 27a741e5 2019-09-11 stsp while (col < avail) {
2233 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2237 80ddbec8 2018-04-29 stsp free(logmsg0);
2238 bb737323 2018-05-20 stsp free(wlogmsg);
2239 5813d178 2019-03-09 stsp free(author);
2240 bb737323 2018-05-20 stsp free(wauthor);
2241 80ddbec8 2018-04-29 stsp free(line);
2242 80ddbec8 2018-04-29 stsp return err;
2245 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2246 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2247 899d86c2 2018-05-10 stsp struct got_object_id *id)
2249 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2250 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2252 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2253 80ddbec8 2018-04-29 stsp if (entry == NULL)
2254 899d86c2 2018-05-10 stsp return NULL;
2256 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2257 d68b9737 2022-09-05 thomas if (dup == NULL) {
2258 d68b9737 2022-09-05 thomas free(entry);
2259 d68b9737 2022-09-05 thomas return NULL;
2262 d68b9737 2022-09-05 thomas entry->id = dup;
2263 99db9666 2018-05-07 stsp entry->commit = commit;
2264 899d86c2 2018-05-10 stsp return entry;
2267 99db9666 2018-05-07 stsp static void
2268 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2270 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2272 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2273 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2274 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2275 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2276 d68b9737 2022-09-05 thomas free(entry->id);
2277 99db9666 2018-05-07 stsp free(entry);
2280 99db9666 2018-05-07 stsp static void
2281 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2283 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2284 99db9666 2018-05-07 stsp pop_commit(commits);
2287 c4972b91 2018-05-07 stsp static const struct got_error *
2288 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2289 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2291 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2292 13add988 2019-10-15 stsp regmatch_t regmatch;
2293 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2295 13add988 2019-10-15 stsp *have_match = 0;
2297 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2299 13add988 2019-10-15 stsp return err;
2301 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2303 13add988 2019-10-15 stsp goto done;
2305 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2306 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2307 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2308 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2309 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2310 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2311 13add988 2019-10-15 stsp *have_match = 1;
2313 13add988 2019-10-15 stsp free(id_str);
2314 13add988 2019-10-15 stsp free(logmsg);
2315 13add988 2019-10-15 stsp return err;
2318 13add988 2019-10-15 stsp static const struct got_error *
2319 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2321 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2324 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2325 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2326 1a76625f 2018-10-22 stsp * while updating the display.
2329 7210b715 2022-09-11 thomas struct got_object_id id;
2330 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2331 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2332 7e8004ba 2022-09-11 thomas int limit_match = 0;
2333 1a76625f 2018-10-22 stsp int errcode;
2335 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2336 4e0d2870 2020-12-07 naddy NULL, NULL);
2337 7210b715 2022-09-11 thomas if (err)
2340 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2343 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2344 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2345 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2349 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2350 1a76625f 2018-10-22 stsp if (errcode) {
2351 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2352 13add988 2019-10-15 stsp "pthread_mutex_lock");
2356 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2357 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2358 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2360 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2361 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2362 7e8004ba 2022-09-11 thomas a->limit_regex);
2363 7e8004ba 2022-09-11 thomas if (err)
2366 7e8004ba 2022-09-11 thomas if (limit_match) {
2367 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2369 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2370 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2371 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2372 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2373 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2376 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2377 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2379 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2380 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2381 7e8004ba 2022-09-11 thomas matched, entry);
2382 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2386 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2387 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2388 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2390 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2393 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2394 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2395 7c1452c1 2020-03-26 stsp int have_match;
2396 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2400 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2401 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2402 7e8004ba 2022-09-11 thomas *a->search_next_done =
2403 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2404 7e8004ba 2022-09-11 thomas } else if (have_match)
2405 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2408 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2409 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2410 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2411 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2414 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2416 9ba79e04 2018-06-11 stsp return err;
2419 2b779855 2020-12-05 naddy static void
2420 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2422 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2423 2b779855 2020-12-05 naddy int ncommits = 0;
2425 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2426 2b779855 2020-12-05 naddy while (entry) {
2427 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2428 2b779855 2020-12-05 naddy s->selected_entry = entry;
2431 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2432 2b779855 2020-12-05 naddy ncommits++;
2436 0553a4e3 2018-04-30 stsp static const struct got_error *
2437 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2439 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2440 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2441 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2442 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2443 60493ae3 2019-06-20 stsp int width;
2444 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2445 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2446 8b473291 2019-02-21 stsp char *refs_str = NULL;
2447 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2448 11b20872 2019-11-08 stsp struct tog_color *tc;
2449 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2451 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2452 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2454 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2455 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2456 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2457 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2459 ecb28ae0 2018-07-16 stsp return err;
2460 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2461 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2462 d2075bf3 2020-12-25 stsp if (refs) {
2463 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2464 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2466 d2075bf3 2020-12-25 stsp goto done;
2470 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2471 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2473 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2474 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2475 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2476 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2477 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2478 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2479 8f4ed634 2020-03-26 stsp goto done;
2482 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2483 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2485 f9686aa5 2020-03-27 stsp if (view->searching) {
2486 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2487 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2488 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2489 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2490 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2491 f9686aa5 2020-03-27 stsp search_str = "searching...";
2494 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2495 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2497 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2498 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2499 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2500 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2501 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2502 8f4ed634 2020-03-26 stsp goto done;
2506 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2507 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2508 91198554 2022-06-23 thomas "........................................",
2509 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2510 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2511 1a76625f 2018-10-22 stsp header = NULL;
2512 1a76625f 2018-10-22 stsp goto done;
2514 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2515 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2516 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2517 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2518 1a76625f 2018-10-22 stsp header = NULL;
2519 1a76625f 2018-10-22 stsp goto done;
2521 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2523 1a76625f 2018-10-22 stsp goto done;
2525 2814baeb 2018-08-01 stsp werase(view->window);
2527 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2528 a3404814 2018-09-02 stsp wstandout(view->window);
2529 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2531 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2532 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2533 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2534 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2538 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2539 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2540 a3404814 2018-09-02 stsp wstandend(view->window);
2541 ecb28ae0 2018-07-16 stsp free(wline);
2542 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2543 1a76625f 2018-10-22 stsp goto done;
2545 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2546 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2547 5813d178 2019-03-09 stsp ncommits = 0;
2548 05171be4 2022-06-23 thomas view->maxx = 0;
2549 5813d178 2019-03-09 stsp while (entry) {
2550 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2551 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2552 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2553 5813d178 2019-03-09 stsp int width;
2554 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2556 f69c5a46 2022-07-19 thomas if (s->use_committer)
2557 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2559 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2560 5813d178 2019-03-09 stsp if (author == NULL) {
2561 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2562 5813d178 2019-03-09 stsp goto done;
2564 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2565 27a741e5 2019-09-11 stsp date_display_cols);
2566 5813d178 2019-03-09 stsp if (author_cols < width)
2567 5813d178 2019-03-09 stsp author_cols = width;
2568 5813d178 2019-03-09 stsp free(wauthor);
2569 5813d178 2019-03-09 stsp free(author);
2570 ef944b8b 2022-07-21 thomas if (err)
2571 ef944b8b 2022-07-21 thomas goto done;
2572 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2573 05171be4 2022-06-23 thomas if (err)
2574 05171be4 2022-06-23 thomas goto done;
2575 05171be4 2022-06-23 thomas msg = msg0;
2576 05171be4 2022-06-23 thomas while (*msg == '\n')
2578 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2579 331b1a16 2022-06-23 thomas *eol = '\0';
2580 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2581 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2582 331b1a16 2022-06-23 thomas if (err)
2583 331b1a16 2022-06-23 thomas goto done;
2584 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2585 05171be4 2022-06-23 thomas free(msg0);
2586 331b1a16 2022-06-23 thomas free(wmsg);
2587 7ca04879 2019-10-19 stsp ncommits++;
2588 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2591 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2592 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2593 867c6645 2018-07-10 stsp ncommits = 0;
2594 899d86c2 2018-05-10 stsp while (entry) {
2595 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2597 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2598 2814baeb 2018-08-01 stsp wstandout(view->window);
2599 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2600 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2601 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2602 2814baeb 2018-08-01 stsp wstandend(view->window);
2604 60493ae3 2019-06-20 stsp goto done;
2605 0553a4e3 2018-04-30 stsp ncommits++;
2606 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2607 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2610 a5d43cac 2022-07-01 thomas view_border(view);
2612 1a76625f 2018-10-22 stsp free(id_str);
2613 8b473291 2019-02-21 stsp free(refs_str);
2614 1a76625f 2018-10-22 stsp free(ncommits_str);
2615 1a76625f 2018-10-22 stsp free(header);
2616 80ddbec8 2018-04-29 stsp return err;
2619 07b55e75 2018-05-10 stsp static void
2620 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2622 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2623 07b55e75 2018-05-10 stsp int nscrolled = 0;
2625 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2626 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2629 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2630 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2631 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2632 07b55e75 2018-05-10 stsp if (entry) {
2633 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2634 07b55e75 2018-05-10 stsp nscrolled++;
2639 aa075928 2018-05-10 stsp static const struct got_error *
2640 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2642 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2643 5e224a3e 2019-02-22 stsp int errcode;
2645 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2647 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2648 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2649 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2650 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2651 7aafa0d1 2019-02-22 stsp if (errcode)
2652 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2653 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2656 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2657 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2659 7c1452c1 2020-03-26 stsp if (!wait)
2662 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2663 ffe38506 2020-12-01 naddy show_log_view(view);
2664 7c1452c1 2020-03-26 stsp update_panels();
2665 7c1452c1 2020-03-26 stsp doupdate();
2667 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2668 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2669 82954512 2020-02-03 stsp if (errcode)
2670 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2671 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2673 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2674 ffe38506 2020-12-01 naddy show_log_view(view);
2675 7c1452c1 2020-03-26 stsp update_panels();
2676 7c1452c1 2020-03-26 stsp doupdate();
2679 5e224a3e 2019-02-22 stsp return NULL;
2682 a5d43cac 2022-07-01 thomas static const struct got_error *
2683 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2685 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2686 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2688 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2689 fe731b51 2022-07-14 thomas return NULL;
2691 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2692 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2693 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2695 a5d43cac 2022-07-01 thomas return err;
2698 5e224a3e 2019-02-22 stsp static const struct got_error *
2699 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2701 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2702 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2703 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2704 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2706 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2707 5e224a3e 2019-02-22 stsp return NULL;
2709 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2710 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2711 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2713 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2715 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2716 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2717 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2719 5e224a3e 2019-02-22 stsp return err;
2723 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2724 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2727 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2728 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2730 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2731 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2733 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2734 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2736 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2737 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2739 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2741 dd0a52c1 2018-05-20 stsp return err;
2744 cd0acaa7 2018-05-20 stsp static const struct got_error *
2745 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2746 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2747 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2749 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2750 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2751 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2753 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2754 15a94983 2018-12-23 stsp if (diff_view == NULL)
2755 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2757 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2758 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2759 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2760 e5a0f69f 2018-08-18 stsp if (err == NULL)
2761 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2762 cd0acaa7 2018-05-20 stsp return err;
2765 80ddbec8 2018-04-29 stsp static const struct got_error *
2766 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2767 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2769 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2771 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2772 941e9f74 2019-05-21 stsp if (parent == NULL)
2773 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2775 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2776 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2777 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2778 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2779 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2780 941e9f74 2019-05-21 stsp s->tree = subtree;
2781 941e9f74 2019-05-21 stsp s->selected = 0;
2782 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2783 941e9f74 2019-05-21 stsp return NULL;
2786 941e9f74 2019-05-21 stsp static const struct got_error *
2787 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2788 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2790 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2791 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2792 941e9f74 2019-05-21 stsp const char *p;
2793 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2795 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2797 941e9f74 2019-05-21 stsp while (*p) {
2798 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2799 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2800 56e0773d 2019-11-28 stsp char *te_name;
2802 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2805 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2806 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2807 941e9f74 2019-05-21 stsp if (slash == NULL)
2808 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2810 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2811 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2812 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2815 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2816 56e0773d 2019-11-28 stsp if (te == NULL) {
2817 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2818 56e0773d 2019-11-28 stsp free(te_name);
2821 56e0773d 2019-11-28 stsp free(te_name);
2822 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2824 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2825 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2827 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2828 941e9f74 2019-05-21 stsp if (slash)
2829 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2831 941e9f74 2019-05-21 stsp subpath = strdup(path);
2832 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2833 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2837 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2842 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2843 941e9f74 2019-05-21 stsp free(tree_id);
2847 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2848 941e9f74 2019-05-21 stsp if (err) {
2849 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2852 941e9f74 2019-05-21 stsp if (slash == NULL)
2854 941e9f74 2019-05-21 stsp free(subpath);
2855 941e9f74 2019-05-21 stsp subpath = NULL;
2856 941e9f74 2019-05-21 stsp p = slash;
2859 941e9f74 2019-05-21 stsp free(subpath);
2860 1a76625f 2018-10-22 stsp return err;
2863 61266923 2020-01-14 stsp static const struct got_error *
2864 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2865 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2866 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2868 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2869 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2870 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2872 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2873 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2874 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2876 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2878 55cccc34 2020-02-20 stsp return err;
2879 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2881 55cccc34 2020-02-20 stsp *new_view = tree_view;
2883 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2884 55cccc34 2020-02-20 stsp return NULL;
2886 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2889 55cccc34 2020-02-20 stsp static const struct got_error *
2890 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2892 61266923 2020-01-14 stsp sigset_t sigset;
2893 61266923 2020-01-14 stsp int errcode;
2895 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2896 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2898 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2899 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2900 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2901 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2902 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2903 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2904 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2905 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2906 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2908 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2909 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2910 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2912 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2913 61266923 2020-01-14 stsp if (errcode)
2914 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2916 61266923 2020-01-14 stsp return NULL;
2919 1a76625f 2018-10-22 stsp static void *
2920 1a76625f 2018-10-22 stsp log_thread(void *arg)
2922 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2923 1a76625f 2018-10-22 stsp int errcode = 0;
2924 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2925 1a76625f 2018-10-22 stsp int done = 0;
2928 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2929 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2931 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2932 f2d749db 2022-07-12 thomas if (errcode) {
2933 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2934 61266923 2020-01-14 stsp return (void *)err;
2937 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2938 f2d749db 2022-07-12 thomas if (err) {
2939 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2940 f2d749db 2022-07-12 thomas goto done;
2943 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2944 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2945 f2d749db 2022-07-12 thomas if (errcode) {
2946 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2947 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2948 f2d749db 2022-07-12 thomas goto done;
2950 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2951 1a76625f 2018-10-22 stsp if (err) {
2952 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2953 f2d749db 2022-07-12 thomas goto done;
2954 1a76625f 2018-10-22 stsp err = NULL;
2956 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
2957 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2958 7e8004ba 2022-09-11 thomas if (a->limit_match)
2959 7e8004ba 2022-09-11 thomas a->commits_needed--;
2961 7e8004ba 2022-09-11 thomas a->commits_needed--;
2964 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2965 3abe8080 2019-04-10 stsp if (errcode) {
2966 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2967 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2968 f2d749db 2022-07-12 thomas goto done;
2969 3abe8080 2019-04-10 stsp } else if (*a->quit)
2971 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
2972 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2973 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
2974 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2975 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
2976 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
2977 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
2978 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
2981 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2982 7c1452c1 2020-03-26 stsp if (errcode) {
2983 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2984 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2985 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2986 f2d749db 2022-07-12 thomas goto done;
2990 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2992 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2993 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2994 7c1452c1 2020-03-26 stsp &tog_mutex);
2995 f2d749db 2022-07-12 thomas if (errcode) {
2996 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2997 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2998 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2999 f2d749db 2022-07-12 thomas goto done;
3001 21355643 2020-12-06 stsp if (*a->quit)
3006 3abe8080 2019-04-10 stsp a->log_complete = 1;
3007 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3008 f2d749db 2022-07-12 thomas if (errcode)
3009 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3011 f2d749db 2022-07-12 thomas if (err) {
3012 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3013 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3015 1a76625f 2018-10-22 stsp return (void *)err;
3018 1a76625f 2018-10-22 stsp static const struct got_error *
3019 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3021 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3022 1a76625f 2018-10-22 stsp int errcode;
3024 1a76625f 2018-10-22 stsp if (s->thread) {
3025 1a76625f 2018-10-22 stsp s->quit = 1;
3026 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3027 1a76625f 2018-10-22 stsp if (errcode)
3028 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3029 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3030 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3031 1a76625f 2018-10-22 stsp if (errcode)
3032 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3033 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3034 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3035 1a76625f 2018-10-22 stsp if (errcode)
3036 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3037 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3038 1a76625f 2018-10-22 stsp if (errcode)
3039 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3040 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3041 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3044 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3045 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3046 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3049 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3050 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3051 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3052 1af5eddf 2022-06-23 thomas if (err == NULL)
3053 1af5eddf 2022-06-23 thomas err = pack_err;
3054 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3057 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3058 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3059 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3062 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3065 9343a5fb 2018-06-23 stsp static const struct got_error *
3066 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3068 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3069 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3070 276b94a1 2020-11-13 naddy int errcode;
3072 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3074 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3075 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3076 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3078 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3079 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3080 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3082 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3083 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3084 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3085 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3086 1a76625f 2018-10-22 stsp free(s->start_id);
3087 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3088 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3089 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3090 1a76625f 2018-10-22 stsp return err;
3094 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3095 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3096 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3097 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3098 7e8004ba 2022-09-11 thomas * works with very slight change.
3100 1a76625f 2018-10-22 stsp static const struct got_error *
3101 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3103 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3104 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3105 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3106 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3107 7e8004ba 2022-09-11 thomas char pattern[1024];
3108 7e8004ba 2022-09-11 thomas int ret;
3110 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3111 7e8004ba 2022-09-11 thomas v = view->child;
3112 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3113 7e8004ba 2022-09-11 thomas v = view->parent;
3115 7e8004ba 2022-09-11 thomas /* Get the pattern */
3116 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3117 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3118 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3119 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3120 7e8004ba 2022-09-11 thomas nocbreak();
3122 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3123 7e8004ba 2022-09-11 thomas cbreak();
3124 7e8004ba 2022-09-11 thomas noecho();
3125 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3126 7e8004ba 2022-09-11 thomas if (ret == ERR)
3127 7e8004ba 2022-09-11 thomas return NULL;
3129 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3131 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3132 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3134 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3135 7e8004ba 2022-09-11 thomas return NULL;
3138 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3139 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3140 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3141 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3142 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3143 7e8004ba 2022-09-11 thomas * the queue.
3145 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3146 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3147 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3148 7e8004ba 2022-09-11 thomas s->selected = 0;
3149 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3151 7e8004ba 2022-09-11 thomas return NULL;
3154 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3155 7e8004ba 2022-09-11 thomas return NULL;
3157 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3159 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3160 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3161 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3162 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3163 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3165 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3166 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3167 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3169 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3170 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3171 7e8004ba 2022-09-11 thomas int have_match = 0;
3173 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3174 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3175 7e8004ba 2022-09-11 thomas if (err)
3176 7e8004ba 2022-09-11 thomas return err;
3178 7e8004ba 2022-09-11 thomas if (have_match) {
3179 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3181 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3182 7e8004ba 2022-09-11 thomas entry->id);
3183 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3184 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3185 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3188 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3189 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3191 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3192 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3193 7e8004ba 2022-09-11 thomas matched, entry);
3194 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3198 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3199 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3200 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3201 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3202 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3203 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3204 7e8004ba 2022-09-11 thomas if (err)
3205 7e8004ba 2022-09-11 thomas return err;
3208 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3209 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3210 7e8004ba 2022-09-11 thomas s->selected = 0;
3212 7e8004ba 2022-09-11 thomas return NULL;
3215 7e8004ba 2022-09-11 thomas static const struct got_error *
3216 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3218 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3220 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3221 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3222 60493ae3 2019-06-20 stsp return NULL;
3225 60493ae3 2019-06-20 stsp static const struct got_error *
3226 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3228 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3229 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3230 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3232 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3233 f9686aa5 2020-03-27 stsp show_log_view(view);
3234 f9686aa5 2020-03-27 stsp update_panels();
3235 f9686aa5 2020-03-27 stsp doupdate();
3237 96e2b566 2019-07-08 stsp if (s->search_entry) {
3238 13add988 2019-10-15 stsp int errcode, ch;
3239 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3240 13add988 2019-10-15 stsp if (errcode)
3241 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3242 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3243 13add988 2019-10-15 stsp ch = wgetch(view->window);
3244 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3245 13add988 2019-10-15 stsp if (errcode)
3246 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3247 13add988 2019-10-15 stsp "pthread_mutex_lock");
3248 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3249 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3250 678cbce5 2019-07-28 stsp return NULL;
3252 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3253 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3255 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3256 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3257 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3259 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3260 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3261 1f4d5162 2022-06-23 thomas * might have changed.
3263 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3264 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3266 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3269 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3272 60493ae3 2019-06-20 stsp while (1) {
3273 13add988 2019-10-15 stsp int have_match = 0;
3275 60493ae3 2019-06-20 stsp if (entry == NULL) {
3276 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3277 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3278 f9967bca 2020-03-27 stsp view->search_next_done =
3279 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3280 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3281 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3282 f801134a 2019-06-25 stsp return NULL;
3285 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3286 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3287 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3289 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3290 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3293 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3294 13add988 2019-10-15 stsp &view->regex);
3297 13add988 2019-10-15 stsp if (have_match) {
3298 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3299 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3303 96e2b566 2019-07-08 stsp s->search_entry = entry;
3304 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3305 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3307 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3310 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3311 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3312 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3313 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3315 60493ae3 2019-06-20 stsp return err;
3318 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3319 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3321 60493ae3 2019-06-20 stsp return err;
3326 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3328 60493ae3 2019-06-20 stsp return NULL;
3331 60493ae3 2019-06-20 stsp static const struct got_error *
3332 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3333 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3334 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3336 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3337 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3338 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3339 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3340 1a76625f 2018-10-22 stsp int errcode;
3342 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3343 f135c941 2020-02-20 stsp free(s->in_repo_path);
3344 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3345 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3346 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3349 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3350 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3351 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3352 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3354 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3355 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3356 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3358 fb2756b9 2018-08-04 stsp s->repo = repo;
3359 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3360 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3361 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3362 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3363 9cd7cbd1 2020-12-07 stsp goto done;
3366 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3367 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3368 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3369 5036bf37 2018-09-24 stsp goto done;
3371 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3372 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3374 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3375 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3376 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3377 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3379 11b20872 2019-11-08 stsp goto done;
3380 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3381 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3382 11b20872 2019-11-08 stsp if (err) {
3383 11b20872 2019-11-08 stsp free_colors(&s->colors);
3384 11b20872 2019-11-08 stsp goto done;
3386 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3387 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3388 11b20872 2019-11-08 stsp if (err) {
3389 11b20872 2019-11-08 stsp free_colors(&s->colors);
3390 11b20872 2019-11-08 stsp goto done;
3394 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3395 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3396 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3397 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3398 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3399 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3401 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3402 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3403 1af5eddf 2022-06-23 thomas if (err)
3404 1af5eddf 2022-06-23 thomas goto done;
3406 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3407 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3408 7cd52833 2022-06-23 thomas if (err)
3409 7cd52833 2022-06-23 thomas goto done;
3410 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3411 b672a97a 2020-01-27 stsp !s->log_branches);
3413 1a76625f 2018-10-22 stsp goto done;
3414 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3415 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3417 c5b78334 2020-01-12 stsp goto done;
3419 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3420 1a76625f 2018-10-22 stsp if (errcode) {
3421 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3422 1a76625f 2018-10-22 stsp goto done;
3424 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3425 7c1452c1 2020-03-26 stsp if (errcode) {
3426 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3427 7c1452c1 2020-03-26 stsp goto done;
3430 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3431 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3432 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3433 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3434 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3435 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3436 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3437 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3438 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3439 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3440 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3441 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3442 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3443 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3444 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3445 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3446 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3449 1a76625f 2018-10-22 stsp close_log_view(view);
3450 ba4f502b 2018-08-04 stsp return err;
3453 e5a0f69f 2018-08-18 stsp static const struct got_error *
3454 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3456 f2f6d207 2020-11-24 stsp const struct got_error *err;
3457 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3459 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3460 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3461 2b380cc8 2018-10-24 stsp &s->thread_args);
3462 2b380cc8 2018-10-24 stsp if (errcode)
3463 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3464 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3465 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3467 f2f6d207 2020-11-24 stsp return err;
3471 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3474 b31f89ff 2022-07-01 thomas static void
3475 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3477 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3479 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3481 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3482 7e8004ba 2022-09-11 thomas view->count = 0;
3484 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3485 b31f89ff 2022-07-01 thomas || home)
3486 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3488 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3489 b31f89ff 2022-07-01 thomas --s->selected;
3491 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3493 b31f89ff 2022-07-01 thomas select_commit(s);
3497 b31f89ff 2022-07-01 thomas static const struct got_error *
3498 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3500 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3501 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3502 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3504 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3505 7e8004ba 2022-09-11 thomas return NULL;
3507 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3508 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3509 b31f89ff 2022-07-01 thomas return NULL;
3511 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3512 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3514 7ed048bd 2022-08-12 thomas if (!page) {
3515 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3516 b31f89ff 2022-07-01 thomas ++s->selected;
3518 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3519 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3520 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3523 7ed048bd 2022-08-12 thomas s->selected = 0;
3524 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3525 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3526 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3527 7ed048bd 2022-08-12 thomas if (entry == NULL)
3529 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3530 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3532 7ed048bd 2022-08-12 thomas if (n > 0)
3533 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3534 b31f89ff 2022-07-01 thomas } else {
3535 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3536 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3537 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3538 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3540 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3542 b31f89ff 2022-07-01 thomas if (err)
3543 b31f89ff 2022-07-01 thomas return err;
3546 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3547 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3549 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3550 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3551 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3552 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3555 b31f89ff 2022-07-01 thomas select_commit(s);
3557 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3558 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3559 b31f89ff 2022-07-01 thomas view->count = 0;
3561 b31f89ff 2022-07-01 thomas return NULL;
3564 a5d43cac 2022-07-01 thomas static void
3565 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3570 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3571 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3572 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3573 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3574 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3576 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3577 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3578 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3579 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3580 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3581 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3583 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3587 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3588 e5a0f69f 2018-08-18 stsp static const struct got_error *
3589 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3591 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3593 a5d43cac 2022-07-01 thomas view->nlines = y;
3594 64486692 2022-07-07 thomas view->ncols = COLS;
3595 a5d43cac 2022-07-01 thomas err = view_resize(view);
3596 a5d43cac 2022-07-01 thomas if (err)
3597 a5d43cac 2022-07-01 thomas return err;
3599 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3601 a5d43cac 2022-07-01 thomas return err;
3604 07dd3ed3 2022-08-06 thomas static const struct got_error *
3605 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3607 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3608 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3609 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3611 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3612 25100026 2022-08-13 thomas return NULL;
3614 07dd3ed3 2022-08-06 thomas g = view->gline;
3615 07dd3ed3 2022-08-06 thomas view->gline = 0;
3617 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3618 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3619 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3620 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3621 07dd3ed3 2022-08-06 thomas select_commit(s);
3622 07dd3ed3 2022-08-06 thomas return NULL;
3625 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3626 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3627 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3628 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3629 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3630 07dd3ed3 2022-08-06 thomas if (err)
3631 07dd3ed3 2022-08-06 thomas return err;
3632 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3633 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3635 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3636 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3638 07dd3ed3 2022-08-06 thomas select_commit(s);
3639 07dd3ed3 2022-08-06 thomas return NULL;
3643 a5d43cac 2022-07-01 thomas static const struct got_error *
3644 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3646 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3647 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3648 7ed048bd 2022-08-12 thomas int eos, nscroll;
3650 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3651 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3652 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3653 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3654 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3655 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3657 c0be8933 2022-08-12 thomas if (err)
3658 c0be8933 2022-08-12 thomas return err;
3661 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3662 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3663 068ab281 2022-07-01 thomas --eos; /* border */
3665 07dd3ed3 2022-08-06 thomas if (view->gline)
3666 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3668 528dedf3 2021-08-30 stsp switch (ch) {
3669 7e8004ba 2022-09-11 thomas case '&':
3670 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3673 1e37a5c2 2019-05-12 jcs s->quit = 1;
3675 05171be4 2022-06-23 thomas case '0':
3676 05171be4 2022-06-23 thomas view->x = 0;
3678 05171be4 2022-06-23 thomas case '$':
3679 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3680 07b0611c 2022-06-23 thomas view->count = 0;
3682 05171be4 2022-06-23 thomas case KEY_RIGHT:
3683 05171be4 2022-06-23 thomas case 'l':
3684 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3685 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3687 07b0611c 2022-06-23 thomas view->count = 0;
3689 05171be4 2022-06-23 thomas case KEY_LEFT:
3690 05171be4 2022-06-23 thomas case 'h':
3691 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3692 07b0611c 2022-06-23 thomas if (view->x <= 0)
3693 07b0611c 2022-06-23 thomas view->count = 0;
3696 1e37a5c2 2019-05-12 jcs case KEY_UP:
3699 f7140bf5 2021-10-17 thomas case CTRL('p'):
3700 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3703 aa7a1117 2023-01-09 thomas case '=':
3704 912a3f79 2021-08-30 j case KEY_HOME:
3705 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3706 07b0611c 2022-06-23 thomas view->count = 0;
3708 70f17a53 2022-06-13 thomas case CTRL('u'):
3709 23427b14 2022-06-23 thomas case 'u':
3710 70f17a53 2022-06-13 thomas nscroll /= 2;
3711 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3712 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3713 a4292ac5 2019-05-12 jcs case CTRL('b'):
3714 1c5e5faa 2022-06-23 thomas case 'b':
3715 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3718 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3721 f7140bf5 2021-10-17 thomas case CTRL('n'):
3722 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3724 f69c5a46 2022-07-19 thomas case '@':
3725 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3728 aa7a1117 2023-01-09 thomas case '*':
3729 912a3f79 2021-08-30 j case KEY_END: {
3730 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3731 912a3f79 2021-08-30 j * traverse them all. */
3732 07b0611c 2022-06-23 thomas view->count = 0;
3733 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3734 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3735 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3736 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3737 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3740 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3741 23427b14 2022-06-23 thomas case 'd':
3742 70f17a53 2022-06-13 thomas nscroll /= 2;
3743 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3744 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3745 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3746 4c2d69cb 2022-06-23 thomas case 'f':
3747 b31f89ff 2022-07-01 thomas case ' ':
3748 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3750 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3751 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3752 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3753 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3754 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3755 2b779855 2020-12-05 naddy select_commit(s);
3756 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3757 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3758 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3759 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3760 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3763 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3764 444d5325 2022-07-03 thomas case '\r':
3765 07b0611c 2022-06-23 thomas view->count = 0;
3766 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3768 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3770 1be4947a 2022-07-22 thomas case 'T':
3771 07b0611c 2022-06-23 thomas view->count = 0;
3772 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3774 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3776 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3777 21355643 2020-12-06 stsp case CTRL('l'):
3779 07b0611c 2022-06-23 thomas view->count = 0;
3780 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3781 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3783 21355643 2020-12-06 stsp err = stop_log_thread(s);
3785 74cfe85e 2020-10-20 stsp return err;
3786 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3787 21355643 2020-12-06 stsp char *parent_path;
3788 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3790 21355643 2020-12-06 stsp return err;
3791 21355643 2020-12-06 stsp free(s->in_repo_path);
3792 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3793 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3794 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3795 21355643 2020-12-06 stsp struct got_object_id *start_id;
3796 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3797 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3798 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3799 466429a1 2022-11-17 thomas if (err) {
3800 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
3801 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
3802 466429a1 2022-11-17 thomas return err;
3803 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
3804 466429a1 2022-11-17 thomas free(s->head_ref_name);
3805 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
3806 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
3807 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3808 466429a1 2022-11-17 thomas &tog_refs, s->repo);
3809 466429a1 2022-11-17 thomas if (err)
3810 466429a1 2022-11-17 thomas return err;
3812 21355643 2020-12-06 stsp free(s->start_id);
3813 21355643 2020-12-06 stsp s->start_id = start_id;
3814 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3815 21355643 2020-12-06 stsp } else /* 'B' */
3816 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3818 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3819 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3820 bf7e79b3 2022-06-23 thomas if (err)
3821 bf7e79b3 2022-06-23 thomas return err;
3823 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3824 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3825 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3827 21355643 2020-12-06 stsp return err;
3828 51a10b52 2020-12-26 stsp tog_free_refs();
3829 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3831 ca51c541 2020-12-07 stsp return err;
3832 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3833 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3835 d01904d4 2019-06-25 stsp return err;
3836 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3837 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3839 b672a97a 2020-01-27 stsp return err;
3840 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3841 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3842 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3843 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3844 21355643 2020-12-06 stsp s->selected_entry = NULL;
3845 21355643 2020-12-06 stsp s->selected = 0;
3846 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3847 21355643 2020-12-06 stsp s->quit = 0;
3848 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3849 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3850 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3851 94ecf40d 2022-07-22 thomas view->offset = 0;
3853 1be4947a 2022-07-22 thomas case 'R':
3854 07b0611c 2022-06-23 thomas view->count = 0;
3855 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3858 07b0611c 2022-06-23 thomas view->count = 0;
3862 80ddbec8 2018-04-29 stsp return err;
3865 4ed7e80c 2018-05-20 stsp static const struct got_error *
3866 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3868 c2db6724 2019-01-04 stsp const struct got_error *error;
3870 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3871 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3872 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3874 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3875 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3877 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3878 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3880 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3881 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3883 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3884 c2db6724 2019-01-04 stsp if (error != NULL)
3885 c2db6724 2019-01-04 stsp return error;
3887 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3888 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3890 c2db6724 2019-01-04 stsp return NULL;
3893 a915003a 2019-02-05 stsp static void
3894 a915003a 2019-02-05 stsp init_curses(void)
3897 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3898 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3899 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3901 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3902 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3903 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3904 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3905 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3907 a915003a 2019-02-05 stsp initscr();
3909 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3912 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3913 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3914 a915003a 2019-02-05 stsp curs_set(0);
3915 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3916 6d17833f 2019-11-08 stsp start_color();
3917 6d17833f 2019-11-08 stsp use_default_colors();
3921 c2db6724 2019-01-04 stsp static const struct got_error *
3922 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3923 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3925 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3927 f135c941 2020-02-20 stsp if (argc == 0) {
3928 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3929 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3930 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3931 f135c941 2020-02-20 stsp return NULL;
3934 f135c941 2020-02-20 stsp if (worktree) {
3935 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3938 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3940 f135c941 2020-02-20 stsp return err;
3941 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3942 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3943 bfd61697 2020-11-14 stsp p) == -1) {
3944 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3945 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3949 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3951 f135c941 2020-02-20 stsp return err;
3954 f135c941 2020-02-20 stsp static const struct got_error *
3955 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3957 80ddbec8 2018-04-29 stsp const struct got_error *error;
3958 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3959 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3960 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3961 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3962 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3963 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3964 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3965 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3966 04cc582a 2018-08-01 stsp struct tog_view *view;
3967 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3969 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3970 80ddbec8 2018-04-29 stsp switch (ch) {
3972 b672a97a 2020-01-27 stsp log_branches = 1;
3975 80ddbec8 2018-04-29 stsp start_commit = optarg;
3978 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3979 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3980 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3984 17020d27 2019-03-07 stsp usage_log();
3985 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3989 80ddbec8 2018-04-29 stsp argc -= optind;
3990 80ddbec8 2018-04-29 stsp argv += optind;
3992 f135c941 2020-02-20 stsp if (argc > 1)
3993 f135c941 2020-02-20 stsp usage_log();
3995 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3996 7cd52833 2022-06-23 thomas if (error != NULL)
3997 7cd52833 2022-06-23 thomas goto done;
3999 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4000 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4001 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4002 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4003 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4004 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4005 c156c7a4 2020-12-18 stsp goto done;
4006 a1fbf39a 2019-08-11 stsp if (worktree)
4007 6962eb72 2020-02-20 stsp repo_path =
4008 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4010 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4011 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4012 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4013 c156c7a4 2020-12-18 stsp goto done;
4017 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4018 80ddbec8 2018-04-29 stsp if (error != NULL)
4019 ecb28ae0 2018-07-16 stsp goto done;
4021 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4022 f135c941 2020-02-20 stsp repo, worktree);
4023 f135c941 2020-02-20 stsp if (error)
4024 f135c941 2020-02-20 stsp goto done;
4026 f135c941 2020-02-20 stsp init_curses();
4028 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4029 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4030 c02c541e 2019-03-29 stsp if (error)
4031 c02c541e 2019-03-29 stsp goto done;
4033 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4034 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4035 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4036 87670572 2020-12-26 naddy if (error)
4037 87670572 2020-12-26 naddy goto done;
4040 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4041 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4042 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4043 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4044 d8f38dc4 2020-12-05 stsp if (error)
4045 d8f38dc4 2020-12-05 stsp goto done;
4046 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4048 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4049 d8f38dc4 2020-12-05 stsp if (error == NULL)
4050 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4051 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4052 d8f38dc4 2020-12-05 stsp goto done;
4053 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4054 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4055 d8f38dc4 2020-12-05 stsp if (error)
4056 d8f38dc4 2020-12-05 stsp goto done;
4059 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4060 04cc582a 2018-08-01 stsp if (view == NULL) {
4061 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4062 04cc582a 2018-08-01 stsp goto done;
4064 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4065 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4066 ba4f502b 2018-08-04 stsp if (error)
4067 ba4f502b 2018-08-04 stsp goto done;
4068 2fc00ff4 2019-08-31 stsp if (worktree) {
4069 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4070 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4071 2fc00ff4 2019-08-31 stsp worktree = NULL;
4073 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4075 f135c941 2020-02-20 stsp free(in_repo_path);
4076 ecb28ae0 2018-07-16 stsp free(repo_path);
4077 ecb28ae0 2018-07-16 stsp free(cwd);
4078 899d86c2 2018-05-10 stsp free(start_id);
4079 d8f38dc4 2020-12-05 stsp free(label);
4081 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4082 1d0f4054 2021-06-17 stsp if (repo) {
4083 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4084 1d0f4054 2021-06-17 stsp if (error == NULL)
4085 1d0f4054 2021-06-17 stsp error = close_err;
4087 ec142235 2019-03-07 stsp if (worktree)
4088 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4089 7cd52833 2022-06-23 thomas if (pack_fds) {
4090 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4091 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4092 7cd52833 2022-06-23 thomas if (error == NULL)
4093 7cd52833 2022-06-23 thomas error = pack_err;
4095 51a10b52 2020-12-26 stsp tog_free_refs();
4096 80ddbec8 2018-04-29 stsp return error;
4099 4ed7e80c 2018-05-20 stsp __dead static void
4100 9f7d7167 2018-04-29 stsp usage_diff(void)
4103 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4104 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4108 6d17833f 2019-11-08 stsp static int
4109 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4110 41605754 2020-11-12 stsp regmatch_t *regmatch)
4112 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4115 ef20f542 2022-06-26 thomas static struct tog_color *
4116 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4118 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4120 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4121 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4122 6d17833f 2019-11-08 stsp return tc;
4125 6d17833f 2019-11-08 stsp return NULL;
4128 4ed7e80c 2018-05-20 stsp static const struct got_error *
4129 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4130 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4132 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4133 666f7b10 2022-06-23 thomas char *exstr = NULL;
4134 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4135 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4136 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4137 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4139 41605754 2020-11-12 stsp *wtotal = 0;
4141 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4142 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4144 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4145 666f7b10 2022-06-23 thomas if (err)
4146 666f7b10 2022-06-23 thomas return err;
4148 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4149 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4150 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4151 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4152 666f7b10 2022-06-23 thomas goto done;
4154 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4155 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4156 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4157 cbea3800 2022-06-23 thomas goto done;
4159 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4160 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4161 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4162 cbea3800 2022-06-23 thomas goto done;
4165 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4166 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4167 cbea3800 2022-06-23 thomas col_tab_align, 1);
4168 cbea3800 2022-06-23 thomas if (err)
4169 cbea3800 2022-06-23 thomas goto done;
4170 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4171 05171be4 2022-06-23 thomas if (n) {
4172 cbea3800 2022-06-23 thomas free(wline);
4173 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4174 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4175 cbea3800 2022-06-23 thomas if (err)
4176 cbea3800 2022-06-23 thomas goto done;
4177 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4178 cbea3800 2022-06-23 thomas wlimit -= width;
4179 cbea3800 2022-06-23 thomas *wtotal += width;
4182 41605754 2020-11-12 stsp if (wlimit > 0) {
4183 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4184 cbea3800 2022-06-23 thomas size_t wlen;
4186 cbea3800 2022-06-23 thomas free(wline);
4187 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4188 cbea3800 2022-06-23 thomas col_tab_align, 1);
4189 cbea3800 2022-06-23 thomas if (err)
4190 cbea3800 2022-06-23 thomas goto done;
4191 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4192 cbea3800 2022-06-23 thomas while (i < wlen) {
4193 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4194 cbea3800 2022-06-23 thomas if (width == -1) {
4195 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4196 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4197 cbea3800 2022-06-23 thomas goto done;
4199 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4201 1c5e5faa 2022-06-23 thomas w += width;
4204 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4205 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4206 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4207 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4208 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4209 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4210 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4214 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4215 cbea3800 2022-06-23 thomas free(wline);
4216 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4217 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4218 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4219 cbea3800 2022-06-23 thomas col_tab_align, 1);
4220 cbea3800 2022-06-23 thomas if (err)
4221 cbea3800 2022-06-23 thomas goto done;
4222 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4223 cbea3800 2022-06-23 thomas } else {
4224 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4225 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4226 cbea3800 2022-06-23 thomas if (err)
4227 cbea3800 2022-06-23 thomas goto done;
4228 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4230 cbea3800 2022-06-23 thomas *wtotal += width2;
4233 05171be4 2022-06-23 thomas free(wline);
4234 666f7b10 2022-06-23 thomas free(exstr);
4235 cbea3800 2022-06-23 thomas free(seg0);
4236 cbea3800 2022-06-23 thomas free(seg1);
4237 cbea3800 2022-06-23 thomas free(seg2);
4238 cbea3800 2022-06-23 thomas return err;
4241 07dd3ed3 2022-08-06 thomas static int
4242 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4244 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4245 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4247 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4248 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4250 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4251 fc2737d5 2022-09-15 thomas selected = first;
4252 fc2737d5 2022-09-15 thomas eof = &s->eof;
4253 fc2737d5 2022-09-15 thomas f = s->f;
4254 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4255 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4257 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4258 07dd3ed3 2022-08-06 thomas selected = first;
4259 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4260 07dd3ed3 2022-08-06 thomas f = s->f;
4261 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4262 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4264 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4265 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4266 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4267 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4269 07dd3ed3 2022-08-06 thomas return 0;
4271 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4272 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4273 07dd3ed3 2022-08-06 thomas return 0;
4274 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4275 07dd3ed3 2022-08-06 thomas rewind(f);
4276 07dd3ed3 2022-08-06 thomas *eof = 0;
4277 07dd3ed3 2022-08-06 thomas *first = 1;
4278 07dd3ed3 2022-08-06 thomas *lineno = 0;
4279 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4280 07dd3ed3 2022-08-06 thomas return 0;
4283 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4284 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4285 07dd3ed3 2022-08-06 thomas view->gline = 0;
4287 07dd3ed3 2022-08-06 thomas return 1;
4290 41605754 2020-11-12 stsp static const struct got_error *
4291 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4293 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4294 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4295 61e69b96 2018-05-20 stsp const struct got_error *err;
4296 82c78e96 2022-08-06 thomas int nprinted = 0;
4297 b304db33 2018-05-20 stsp char *line;
4298 826082fe 2020-12-10 stsp size_t linesize = 0;
4299 826082fe 2020-12-10 stsp ssize_t linelen;
4300 61e69b96 2018-05-20 stsp wchar_t *wline;
4301 e0b650dd 2018-05-20 stsp int width;
4302 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4303 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4304 fe621944 2020-11-10 stsp off_t line_offset;
4306 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4307 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4308 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4309 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4311 f7d12f7e 2018-08-01 stsp werase(view->window);
4313 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4314 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4316 a3404814 2018-09-02 stsp if (header) {
4317 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4318 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4319 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4321 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4322 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4323 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4325 135a2da0 2020-11-11 stsp free(line);
4327 a3404814 2018-09-02 stsp return err;
4329 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4330 a3404814 2018-09-02 stsp wstandout(view->window);
4331 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4332 e54cc94a 2020-11-11 stsp free(wline);
4333 e54cc94a 2020-11-11 stsp wline = NULL;
4334 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4335 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4336 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4337 a3404814 2018-09-02 stsp wstandend(view->window);
4339 a3404814 2018-09-02 stsp if (max_lines <= 1)
4340 a3404814 2018-09-02 stsp return NULL;
4341 a3404814 2018-09-02 stsp max_lines--;
4344 89f1a395 2020-12-01 naddy s->eof = 0;
4345 05171be4 2022-06-23 thomas view->maxx = 0;
4346 826082fe 2020-12-10 stsp line = NULL;
4347 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4348 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4349 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4351 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4352 826082fe 2020-12-10 stsp if (linelen == -1) {
4353 826082fe 2020-12-10 stsp if (feof(s->f)) {
4354 826082fe 2020-12-10 stsp s->eof = 1;
4357 826082fe 2020-12-10 stsp free(line);
4358 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4361 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4362 07dd3ed3 2022-08-06 thomas continue;
4363 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4364 07dd3ed3 2022-08-06 thomas continue;
4365 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4366 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4368 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4369 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4370 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4371 cbea3800 2022-06-23 thomas if (err) {
4372 cbea3800 2022-06-23 thomas free(line);
4373 cbea3800 2022-06-23 thomas return err;
4375 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4376 cbea3800 2022-06-23 thomas free(wline);
4377 cbea3800 2022-06-23 thomas wline = NULL;
4379 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4380 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4381 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4382 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4383 07dd3ed3 2022-08-06 thomas if (attr)
4384 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4385 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4386 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4387 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4388 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4389 41605754 2020-11-12 stsp if (err) {
4390 41605754 2020-11-12 stsp free(line);
4391 41605754 2020-11-12 stsp return err;
4394 f1c0ec19 2022-06-23 thomas int skip;
4395 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4396 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4397 f1c0ec19 2022-06-23 thomas if (err) {
4398 f1c0ec19 2022-06-23 thomas free(line);
4399 f1c0ec19 2022-06-23 thomas return err;
4401 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4402 f1c0ec19 2022-06-23 thomas free(wline);
4403 41605754 2020-11-12 stsp wline = NULL;
4405 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4406 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4407 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4408 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4409 07dd3ed3 2022-08-06 thomas } else {
4410 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4411 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4413 07dd3ed3 2022-08-06 thomas if (attr)
4414 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4415 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4416 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4418 826082fe 2020-12-10 stsp free(line);
4419 fe621944 2020-11-10 stsp if (nprinted >= 1)
4420 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4421 89f1a395 2020-12-01 naddy (nprinted - 1);
4423 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4425 a5d43cac 2022-07-01 thomas view_border(view);
4427 89f1a395 2020-12-01 naddy if (s->eof) {
4428 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4429 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4430 c3e9aa98 2019-05-13 jcs nprinted++;
4433 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4434 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4436 c3e9aa98 2019-05-13 jcs return err;
4439 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4440 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4441 e54cc94a 2020-11-11 stsp free(wline);
4442 e54cc94a 2020-11-11 stsp wline = NULL;
4443 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4446 26ed57b2 2018-05-19 stsp return NULL;
4449 abd2672a 2018-12-23 stsp static char *
4450 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4452 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4453 09867e48 2019-08-13 stsp char *p, *s;
4455 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4456 09867e48 2019-08-13 stsp if (tm == NULL)
4457 09867e48 2019-08-13 stsp return NULL;
4458 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4459 09867e48 2019-08-13 stsp if (s == NULL)
4460 09867e48 2019-08-13 stsp return NULL;
4461 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4463 abd2672a 2018-12-23 stsp *p = '\0';
4467 4ed7e80c 2018-05-20 stsp static const struct got_error *
4468 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4469 772fcad5 2023-01-07 thomas struct got_commit_object *commit, struct got_repository *repo,
4470 772fcad5 2023-01-07 thomas struct got_diffstat_cb_arg *dsa)
4472 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4473 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4474 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4475 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4476 772fcad5 2023-01-07 thomas FILE *f1 = NULL, *f2 = NULL;
4477 772fcad5 2023-01-07 thomas int fd1 = -1, fd2 = -1;
4479 772fcad5 2023-01-07 thomas f1 = got_opentemp();
4480 772fcad5 2023-01-07 thomas if (f1 == NULL) {
4481 772fcad5 2023-01-07 thomas err = got_error_from_errno("got_opentemp");
4482 772fcad5 2023-01-07 thomas goto done;
4484 772fcad5 2023-01-07 thomas f2 = got_opentemp();
4485 772fcad5 2023-01-07 thomas if (f2 == NULL) {
4486 772fcad5 2023-01-07 thomas err = got_error_from_errno("got_opentemp");
4487 772fcad5 2023-01-07 thomas goto done;
4490 772fcad5 2023-01-07 thomas fd1 = got_opentempfd();
4491 772fcad5 2023-01-07 thomas if (fd1 == -1) {
4492 772fcad5 2023-01-07 thomas err = got_error_from_errno("got_opentempfd");
4493 772fcad5 2023-01-07 thomas goto done;
4495 772fcad5 2023-01-07 thomas fd2 = got_opentempfd();
4496 772fcad5 2023-01-07 thomas if (fd2 == -1) {
4497 772fcad5 2023-01-07 thomas err = got_error_from_errno("got_opentempfd");
4498 772fcad5 2023-01-07 thomas goto done;
4501 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4502 0208f208 2020-05-05 stsp if (qid != NULL) {
4503 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4504 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4505 ec242592 2022-04-22 thomas &qid->id);
4507 0208f208 2020-05-05 stsp return err;
4509 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4510 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4511 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4512 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4513 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4515 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4519 0208f208 2020-05-05 stsp if (tree_id1) {
4520 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4522 0208f208 2020-05-05 stsp goto done;
4525 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4526 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4528 0208f208 2020-05-05 stsp goto done;
4530 772fcad5 2023-01-07 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
4531 772fcad5 2023-01-07 thomas got_diff_tree_compute_diffstat, dsa, 1);
4533 0208f208 2020-05-05 stsp if (tree1)
4534 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4535 0208f208 2020-05-05 stsp if (tree2)
4536 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4537 772fcad5 2023-01-07 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4538 772fcad5 2023-01-07 thomas err = got_error_from_errno("close");
4539 772fcad5 2023-01-07 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4540 772fcad5 2023-01-07 thomas err = got_error_from_errno("close");
4541 772fcad5 2023-01-07 thomas if (f1 && fclose(f1) == EOF && err == NULL)
4542 772fcad5 2023-01-07 thomas err = got_error_from_errno("fclose");
4543 772fcad5 2023-01-07 thomas if (f2 && fclose(f2) == EOF && err == NULL)
4544 772fcad5 2023-01-07 thomas err = got_error_from_errno("fclose");
4545 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4546 0208f208 2020-05-05 stsp return err;
4549 0208f208 2020-05-05 stsp static const struct got_error *
4550 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4551 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4553 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4555 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4556 fe621944 2020-11-10 stsp if (p == NULL)
4557 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4558 82c78e96 2022-08-06 thomas *lines = p;
4559 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4560 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4561 fe621944 2020-11-10 stsp (*nlines)++;
4563 fe621944 2020-11-10 stsp return NULL;
4566 fe621944 2020-11-10 stsp static const struct got_error *
4567 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4568 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4569 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4570 772fcad5 2023-01-07 thomas FILE *outfile)
4572 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4573 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4574 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4575 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4576 45d799e2 2018-12-23 stsp time_t committer_time;
4577 45d799e2 2018-12-23 stsp const char *author, *committer;
4578 8b473291 2019-02-21 stsp char *refs_str = NULL;
4579 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4580 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4581 772fcad5 2023-01-07 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0, &changed_paths,
4582 772fcad5 2023-01-07 thomas ignore_ws, force_text_diff, tog_diff_algo };
4583 fe621944 2020-11-10 stsp off_t outoff = 0;
4586 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4588 8b473291 2019-02-21 stsp if (refs) {
4589 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4591 8b473291 2019-02-21 stsp return err;
4594 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4596 abd2672a 2018-12-23 stsp return err;
4598 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4599 15a94983 2018-12-23 stsp if (err) {
4600 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4601 15a94983 2018-12-23 stsp goto done;
4604 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4606 fe621944 2020-11-10 stsp goto done;
4608 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4609 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4610 fe621944 2020-11-10 stsp if (n < 0) {
4611 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4612 abd2672a 2018-12-23 stsp goto done;
4614 fe621944 2020-11-10 stsp outoff += n;
4615 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4617 fe621944 2020-11-10 stsp goto done;
4619 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4620 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4621 fe621944 2020-11-10 stsp if (n < 0) {
4622 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4623 abd2672a 2018-12-23 stsp goto done;
4625 fe621944 2020-11-10 stsp outoff += n;
4626 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4628 fe621944 2020-11-10 stsp goto done;
4630 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
4631 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
4632 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
4633 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
4634 fe621944 2020-11-10 stsp if (n < 0) {
4635 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4636 fe621944 2020-11-10 stsp goto done;
4638 fe621944 2020-11-10 stsp outoff += n;
4639 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4640 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
4642 fe621944 2020-11-10 stsp goto done;
4644 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
4645 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
4646 48830929 2023-01-07 thomas if (datestr) {
4647 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
4648 fe621944 2020-11-10 stsp if (n < 0) {
4649 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4650 fe621944 2020-11-10 stsp goto done;
4652 fe621944 2020-11-10 stsp outoff += n;
4653 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4654 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
4656 fe621944 2020-11-10 stsp goto done;
4658 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4659 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4660 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4661 ac4dc263 2021-09-24 thomas int pn = 1;
4662 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4663 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4664 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4665 ac4dc263 2021-09-24 thomas if (err)
4666 ac4dc263 2021-09-24 thomas goto done;
4667 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4668 ac4dc263 2021-09-24 thomas if (n < 0) {
4669 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4670 ac4dc263 2021-09-24 thomas goto done;
4672 ac4dc263 2021-09-24 thomas outoff += n;
4673 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4674 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4675 ac4dc263 2021-09-24 thomas if (err)
4676 ac4dc263 2021-09-24 thomas goto done;
4677 ac4dc263 2021-09-24 thomas free(id_str);
4678 ac4dc263 2021-09-24 thomas id_str = NULL;
4682 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4684 5943eee2 2019-08-13 stsp goto done;
4685 fe621944 2020-11-10 stsp s = logmsg;
4686 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4687 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4688 fe621944 2020-11-10 stsp if (n < 0) {
4689 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4690 fe621944 2020-11-10 stsp goto done;
4692 fe621944 2020-11-10 stsp outoff += n;
4693 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4694 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4696 fe621944 2020-11-10 stsp goto done;
4699 772fcad5 2023-01-07 thomas err = get_changed_paths(&changed_paths, commit, repo, &dsa);
4701 0208f208 2020-05-05 stsp goto done;
4703 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4704 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4705 772fcad5 2023-01-07 thomas int pad = dsa.max_path_len - pe->path_len + 1;
4707 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4708 772fcad5 2023-01-07 thomas pe->path, pad, ' ', dsa.add_cols + 1, cp->add,
4709 772fcad5 2023-01-07 thomas dsa.rm_cols + 1, cp->rm);
4710 fe621944 2020-11-10 stsp if (n < 0) {
4711 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4712 fe621944 2020-11-10 stsp goto done;
4714 fe621944 2020-11-10 stsp outoff += n;
4715 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4716 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4718 fe621944 2020-11-10 stsp goto done;
4721 0208f208 2020-05-05 stsp fputc('\n', outfile);
4723 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4724 772fcad5 2023-01-07 thomas if (err)
4725 772fcad5 2023-01-07 thomas goto done;
4727 772fcad5 2023-01-07 thomas n = fprintf(outfile,
4728 772fcad5 2023-01-07 thomas "%d file%s changed, %d insertions(+), %d deletions(-)\n",
4729 772fcad5 2023-01-07 thomas dsa.nfiles, dsa.nfiles > 1 ? "s" : "", dsa.ins, dsa.del);
4730 772fcad5 2023-01-07 thomas if (n < 0) {
4731 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
4732 772fcad5 2023-01-07 thomas goto done;
4734 772fcad5 2023-01-07 thomas outoff += n;
4735 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4736 772fcad5 2023-01-07 thomas if (err)
4737 772fcad5 2023-01-07 thomas goto done;
4739 772fcad5 2023-01-07 thomas fputc('\n', outfile);
4740 772fcad5 2023-01-07 thomas outoff++;
4741 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4743 21c2d8be 2023-01-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4744 abd2672a 2018-12-23 stsp free(id_str);
4745 5943eee2 2019-08-13 stsp free(logmsg);
4746 8b473291 2019-02-21 stsp free(refs_str);
4747 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4748 7510f233 2020-08-09 stsp if (err) {
4749 82c78e96 2022-08-06 thomas free(*lines);
4750 82c78e96 2022-08-06 thomas *lines = NULL;
4751 ae6a6978 2020-08-09 stsp *nlines = 0;
4753 fe621944 2020-11-10 stsp return err;
4756 abd2672a 2018-12-23 stsp static const struct got_error *
4757 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4759 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4760 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4761 15a94983 2018-12-23 stsp int obj_type;
4763 82c78e96 2022-08-06 thomas free(s->lines);
4764 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4765 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4766 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4767 fe621944 2020-11-10 stsp s->nlines = 0;
4769 511a516b 2018-05-19 stsp f = got_opentemp();
4770 48ae06ee 2018-10-18 stsp if (f == NULL) {
4771 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4772 48ae06ee 2018-10-18 stsp goto done;
4774 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4775 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4776 fb43ecf1 2019-02-11 stsp goto done;
4780 15a94983 2018-12-23 stsp if (s->id1)
4781 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4783 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4785 15a94983 2018-12-23 stsp goto done;
4787 15a94983 2018-12-23 stsp switch (obj_type) {
4788 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4789 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4790 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4791 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4792 53d03f97 2023-01-10 thomas s->ignore_whitespace, s->force_text_diff, 0, NULL, s->repo,
4795 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4796 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4797 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4798 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4799 53d03f97 2023-01-10 thomas s->force_text_diff, 0, NULL, s->repo, s->f);
4801 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4802 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4803 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4804 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4805 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4807 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4809 3ffacbe1 2020-02-02 tracey goto done;
4810 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4811 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4812 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4813 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4814 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
4815 772fcad5 2023-01-07 thomas s->force_text_diff, s->f);
4816 f44b1f58 2020-02-02 tracey if (err)
4817 f44b1f58 2020-02-02 tracey goto done;
4818 f44b1f58 2020-02-02 tracey } else {
4819 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4820 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4821 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4822 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4823 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4824 772fcad5 2023-01-07 thomas s->ignore_whitespace,
4825 772fcad5 2023-01-07 thomas s->force_text_diff, s->f);
4826 f44b1f58 2020-02-02 tracey if (err)
4827 f44b1f58 2020-02-02 tracey goto done;
4832 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4834 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4835 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4836 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4837 53d03f97 2023-01-10 thomas s->force_text_diff, 0, NULL, s->repo, s->f);
4841 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4845 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4846 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4847 48ae06ee 2018-10-18 stsp return err;
4850 f5215bb9 2019-02-22 stsp static void
4851 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4853 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4854 f5215bb9 2019-02-22 stsp update_panels();
4855 f5215bb9 2019-02-22 stsp doupdate();
4858 f44b1f58 2020-02-02 tracey static const struct got_error *
4859 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4861 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4863 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4864 f44b1f58 2020-02-02 tracey return NULL;
4867 2a7d3cd7 2022-09-17 thomas static void
4868 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4869 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
4871 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
4873 2a7d3cd7 2022-09-17 thomas *f = s->f;
4874 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
4875 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
4876 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
4877 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
4878 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
4879 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
4882 fc2737d5 2022-09-15 thomas static const struct got_error *
4883 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
4885 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4886 fc2737d5 2022-09-15 thomas FILE *f;
4887 f44b1f58 2020-02-02 tracey int lineno;
4888 78eecffc 2022-06-23 thomas char *line = NULL;
4889 826082fe 2020-12-10 stsp size_t linesize = 0;
4890 826082fe 2020-12-10 stsp ssize_t linelen;
4891 fc2737d5 2022-09-15 thomas off_t *line_offsets;
4892 fc2737d5 2022-09-15 thomas size_t nlines = 0;
4893 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
4895 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
4896 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
4897 2a7d3cd7 2022-09-17 thomas "view search not supported");
4898 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4899 fc2737d5 2022-09-15 thomas &match, &selected);
4901 f44b1f58 2020-02-02 tracey if (!view->searching) {
4902 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4903 f44b1f58 2020-02-02 tracey return NULL;
4906 fc2737d5 2022-09-15 thomas if (*match) {
4907 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4908 fc2737d5 2022-09-15 thomas lineno = *match + 1;
4910 fc2737d5 2022-09-15 thomas lineno = *match - 1;
4912 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
4914 f44b1f58 2020-02-02 tracey while (1) {
4915 f44b1f58 2020-02-02 tracey off_t offset;
4917 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
4918 fc2737d5 2022-09-15 thomas if (*match == 0) {
4919 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4923 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4924 f44b1f58 2020-02-02 tracey lineno = 1;
4926 fc2737d5 2022-09-15 thomas lineno = nlines;
4929 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
4930 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
4931 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
4932 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
4933 f44b1f58 2020-02-02 tracey free(line);
4934 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4936 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
4937 78eecffc 2022-06-23 thomas if (linelen != -1) {
4938 78eecffc 2022-06-23 thomas char *exstr;
4939 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4940 78eecffc 2022-06-23 thomas if (err)
4942 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4943 78eecffc 2022-06-23 thomas &view->regmatch)) {
4944 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4945 fc2737d5 2022-09-15 thomas *match = lineno;
4946 78eecffc 2022-06-23 thomas free(exstr);
4949 78eecffc 2022-06-23 thomas free(exstr);
4951 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4952 f44b1f58 2020-02-02 tracey lineno++;
4954 f44b1f58 2020-02-02 tracey lineno--;
4956 826082fe 2020-12-10 stsp free(line);
4958 fc2737d5 2022-09-15 thomas if (*match) {
4959 fc2737d5 2022-09-15 thomas *first = *match;
4960 fc2737d5 2022-09-15 thomas *selected = 1;
4963 05171be4 2022-06-23 thomas return err;
4966 48ae06ee 2018-10-18 stsp static const struct got_error *
4967 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4969 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4970 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4972 a0f32f33 2022-06-13 thomas free(s->id1);
4973 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4974 a0f32f33 2022-06-13 thomas free(s->id2);
4975 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4976 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4977 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4978 a0f32f33 2022-06-13 thomas s->f = NULL;
4979 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4980 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4981 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4982 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4983 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4984 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4985 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4986 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4987 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4988 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4989 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4990 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4991 82c78e96 2022-08-06 thomas free(s->lines);
4992 82c78e96 2022-08-06 thomas s->lines = NULL;
4993 a0f32f33 2022-06-13 thomas s->nlines = 0;
4994 a0f32f33 2022-06-13 thomas return err;
4997 a0f32f33 2022-06-13 thomas static const struct got_error *
4998 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4999 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5000 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5001 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5003 48ae06ee 2018-10-18 stsp const struct got_error *err;
5004 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5006 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5007 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5008 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5010 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5011 15a94983 2018-12-23 stsp int type1, type2;
5012 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5014 15a94983 2018-12-23 stsp return err;
5015 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5017 15a94983 2018-12-23 stsp return err;
5019 15a94983 2018-12-23 stsp if (type1 != type2)
5020 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5022 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5023 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5024 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5025 f44b1f58 2020-02-02 tracey s->repo = repo;
5026 f44b1f58 2020-02-02 tracey s->id1 = id1;
5027 f44b1f58 2020-02-02 tracey s->id2 = id2;
5028 3dbaef42 2020-11-24 stsp s->label1 = label1;
5029 3dbaef42 2020-11-24 stsp s->label2 = label2;
5031 15a94983 2018-12-23 stsp if (id1) {
5032 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5033 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5034 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5036 5465d566 2020-02-01 tracey s->id1 = NULL;
5038 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5039 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5040 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5041 a0f32f33 2022-06-13 thomas goto done;
5044 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5045 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5046 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5047 9a1d7435 2022-06-23 thomas goto done;
5050 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5051 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5052 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5053 a0f32f33 2022-06-13 thomas goto done;
5056 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5057 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5058 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5059 19a6a6b5 2022-07-01 thomas goto done;
5062 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5063 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5064 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5065 19a6a6b5 2022-07-01 thomas goto done;
5068 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
5069 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
5070 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5071 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5072 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5073 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5074 5465d566 2020-02-01 tracey s->repo = repo;
5076 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5079 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5080 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5081 6568f0aa 2022-08-06 thomas if (rc != ERR)
5082 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5083 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5084 6568f0aa 2022-08-06 thomas if (rc != ERR)
5085 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5086 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5087 6568f0aa 2022-08-06 thomas if (rc != ERR)
5088 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5089 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5090 6568f0aa 2022-08-06 thomas if (rc != ERR)
5091 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5092 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5093 6568f0aa 2022-08-06 thomas if (rc != ERR)
5094 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5095 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5096 6568f0aa 2022-08-06 thomas if (rc != ERR)
5097 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5098 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5099 6568f0aa 2022-08-06 thomas if (rc != ERR)
5100 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5101 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5102 6568f0aa 2022-08-06 thomas if (rc != ERR)
5103 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5104 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5105 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5106 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5107 a0f32f33 2022-06-13 thomas goto done;
5111 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5112 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5113 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5114 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5116 5465d566 2020-02-01 tracey err = create_diff(s);
5118 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5119 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5120 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5121 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5122 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5123 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5124 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5126 a0f32f33 2022-06-13 thomas if (err)
5127 a0f32f33 2022-06-13 thomas close_diff_view(view);
5128 e5a0f69f 2018-08-18 stsp return err;
5131 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5132 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5134 a3404814 2018-09-02 stsp const struct got_error *err;
5135 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5136 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5137 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5139 a3404814 2018-09-02 stsp if (s->id1) {
5140 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5142 a3404814 2018-09-02 stsp return err;
5143 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5145 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5147 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5149 a3404814 2018-09-02 stsp return err;
5150 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5152 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5153 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5154 a3404814 2018-09-02 stsp free(id_str1);
5155 a3404814 2018-09-02 stsp free(id_str2);
5156 a3404814 2018-09-02 stsp return err;
5158 a3404814 2018-09-02 stsp free(id_str1);
5159 a3404814 2018-09-02 stsp free(id_str2);
5161 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5162 267bb3b8 2021-08-01 stsp free(header);
5163 267bb3b8 2021-08-01 stsp return err;
5166 15a087fe 2019-02-21 stsp static const struct got_error *
5167 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5168 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5170 d7a04538 2019-02-21 stsp const struct got_error *err;
5171 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5172 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5173 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5175 15a087fe 2019-02-21 stsp free(s->id2);
5176 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5177 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5178 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5180 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5182 d7a04538 2019-02-21 stsp return err;
5183 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5184 15a087fe 2019-02-21 stsp free(s->id1);
5185 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5186 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5187 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5188 15a087fe 2019-02-21 stsp return NULL;
5191 0cf4efb1 2018-09-29 stsp static const struct got_error *
5192 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5194 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5196 adf4c9e0 2022-07-03 thomas view->count = 0;
5197 adf4c9e0 2022-07-03 thomas wclear(view->window);
5198 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5199 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5200 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5201 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5202 adf4c9e0 2022-07-03 thomas return create_diff(s);
5205 82c78e96 2022-08-06 thomas static void
5206 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5208 82c78e96 2022-08-06 thomas int start, i;
5210 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5212 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5213 82c78e96 2022-08-06 thomas if (i == 0)
5214 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5215 82c78e96 2022-08-06 thomas if (--i == start)
5216 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5219 82c78e96 2022-08-06 thomas s->selected_line = 1;
5220 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5223 82c78e96 2022-08-06 thomas static void
5224 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5226 82c78e96 2022-08-06 thomas int start, i;
5228 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5230 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5231 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5233 82c78e96 2022-08-06 thomas if (++i == start)
5234 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5237 82c78e96 2022-08-06 thomas s->selected_line = 1;
5238 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5241 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5242 4fc71f3b 2022-07-12 thomas int, int, int);
5243 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5244 4fc71f3b 2022-07-12 thomas int, int);
5246 adf4c9e0 2022-07-03 thomas static const struct got_error *
5247 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5249 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5250 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5251 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5252 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5253 826082fe 2020-12-10 stsp char *line = NULL;
5254 826082fe 2020-12-10 stsp size_t linesize = 0;
5255 826082fe 2020-12-10 stsp ssize_t linelen;
5256 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5258 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5260 e5a0f69f 2018-08-18 stsp switch (ch) {
5261 05171be4 2022-06-23 thomas case '0':
5262 05171be4 2022-06-23 thomas view->x = 0;
5264 05171be4 2022-06-23 thomas case '$':
5265 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5266 07b0611c 2022-06-23 thomas view->count = 0;
5268 05171be4 2022-06-23 thomas case KEY_RIGHT:
5269 05171be4 2022-06-23 thomas case 'l':
5270 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5271 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5273 07b0611c 2022-06-23 thomas view->count = 0;
5275 05171be4 2022-06-23 thomas case KEY_LEFT:
5276 05171be4 2022-06-23 thomas case 'h':
5277 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5278 07b0611c 2022-06-23 thomas if (view->x <= 0)
5279 07b0611c 2022-06-23 thomas view->count = 0;
5283 3dbaef42 2020-11-24 stsp if (ch == 'a')
5284 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5285 772fcad5 2023-01-07 thomas else if (ch == 'w')
5286 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5287 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5290 912a3f79 2021-08-30 j case KEY_HOME:
5291 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5292 07b0611c 2022-06-23 thomas view->count = 0;
5295 912a3f79 2021-08-30 j case KEY_END:
5296 07b0611c 2022-06-23 thomas view->count = 0;
5300 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5304 1e37a5c2 2019-05-12 jcs case KEY_UP:
5305 f7140bf5 2021-10-17 thomas case CTRL('p'):
5306 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5307 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5309 07b0611c 2022-06-23 thomas view->count = 0;
5311 70f17a53 2022-06-13 thomas case CTRL('u'):
5312 23427b14 2022-06-23 thomas case 'u':
5313 70f17a53 2022-06-13 thomas nscroll /= 2;
5314 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5315 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5316 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5317 1c5e5faa 2022-06-23 thomas case 'b':
5318 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5319 07b0611c 2022-06-23 thomas view->count = 0;
5323 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5324 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5327 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5328 f7140bf5 2021-10-17 thomas case CTRL('n'):
5329 1e37a5c2 2019-05-12 jcs if (!s->eof)
5330 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5332 07b0611c 2022-06-23 thomas view->count = 0;
5334 70f17a53 2022-06-13 thomas case CTRL('d'):
5335 23427b14 2022-06-23 thomas case 'd':
5336 70f17a53 2022-06-13 thomas nscroll /= 2;
5337 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5338 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5339 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5340 1c5e5faa 2022-06-23 thomas case 'f':
5342 07b0611c 2022-06-23 thomas if (s->eof) {
5343 07b0611c 2022-06-23 thomas view->count = 0;
5347 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5348 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5349 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5350 826082fe 2020-12-10 stsp if (linelen == -1) {
5351 826082fe 2020-12-10 stsp if (feof(s->f)) {
5352 826082fe 2020-12-10 stsp s->eof = 1;
5354 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5358 826082fe 2020-12-10 stsp free(line);
5360 82c78e96 2022-08-06 thomas case '(':
5361 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5363 82c78e96 2022-08-06 thomas case ')':
5364 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5366 82c78e96 2022-08-06 thomas case '{':
5367 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5369 82c78e96 2022-08-06 thomas case '}':
5370 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5373 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5374 1e37a5c2 2019-05-12 jcs s->diff_context--;
5375 aa61903a 2021-12-31 thomas s->matched_line = 0;
5376 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5377 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5378 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5379 27829c9e 2020-11-21 stsp s->nlines) {
5380 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5381 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5384 07b0611c 2022-06-23 thomas view->count = 0;
5387 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5388 1e37a5c2 2019-05-12 jcs s->diff_context++;
5389 aa61903a 2021-12-31 thomas s->matched_line = 0;
5390 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5391 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5393 07b0611c 2022-06-23 thomas view->count = 0;
5397 777aae21 2022-07-20 thomas case 'K':
5399 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5400 4fc71f3b 2022-07-12 thomas case '>':
5401 4fc71f3b 2022-07-12 thomas case '.':
5402 777aae21 2022-07-20 thomas case 'J':
5403 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5404 07b0611c 2022-06-23 thomas view->count = 0;
5407 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5409 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5410 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5411 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5413 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5414 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5415 4fc71f3b 2022-07-12 thomas if (err)
5417 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5419 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5422 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5423 4fc71f3b 2022-07-12 thomas if (err)
5425 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5426 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5427 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5429 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5430 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5431 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5433 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5434 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5435 4fc71f3b 2022-07-12 thomas if (err)
5437 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5439 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5441 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5442 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5443 4fc71f3b 2022-07-12 thomas bs->selected_line);
5444 4fc71f3b 2022-07-12 thomas if (id == NULL)
5447 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5450 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5451 4fc71f3b 2022-07-12 thomas if (err)
5454 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5455 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5456 aa61903a 2021-12-31 thomas s->matched_line = 0;
5457 05171be4 2022-06-23 thomas view->x = 0;
5459 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5460 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5463 07b0611c 2022-06-23 thomas view->count = 0;
5467 bcbd79e2 2018-08-19 stsp return err;
5470 4ed7e80c 2018-05-20 stsp static const struct got_error *
5471 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5473 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5474 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5475 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5476 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5477 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5478 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5479 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5480 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5481 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5482 3dbaef42 2020-11-24 stsp const char *errstr;
5483 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5484 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5486 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5487 26ed57b2 2018-05-19 stsp switch (ch) {
5489 64453f7e 2020-11-21 stsp force_text_diff = 1;
5492 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5494 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5495 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5496 8f666e67 2022-02-12 thomas errstr, errstr);
5498 09b5bff8 2020-02-23 naddy case 'r':
5499 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5500 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5501 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5503 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5506 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5509 17020d27 2019-03-07 stsp usage_diff();
5510 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5514 26ed57b2 2018-05-19 stsp argc -= optind;
5515 26ed57b2 2018-05-19 stsp argv += optind;
5517 26ed57b2 2018-05-19 stsp if (argc == 0) {
5518 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5519 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5520 15a94983 2018-12-23 stsp id_str1 = argv[0];
5521 15a94983 2018-12-23 stsp id_str2 = argv[1];
5523 26ed57b2 2018-05-19 stsp usage_diff();
5525 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5526 7cd52833 2022-06-23 thomas if (error)
5527 7cd52833 2022-06-23 thomas goto done;
5529 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5530 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5531 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5532 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5533 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5534 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5535 c156c7a4 2020-12-18 stsp goto done;
5536 a273ac94 2020-02-23 naddy if (worktree)
5537 a273ac94 2020-02-23 naddy repo_path =
5538 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5540 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5541 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5542 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5543 c156c7a4 2020-12-18 stsp goto done;
5547 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5548 eb6600df 2019-01-04 stsp if (error)
5549 eb6600df 2019-01-04 stsp goto done;
5551 a273ac94 2020-02-23 naddy init_curses();
5553 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5554 51a10b52 2020-12-26 stsp if (error)
5555 51a10b52 2020-12-26 stsp goto done;
5557 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5558 26ed57b2 2018-05-19 stsp if (error)
5559 26ed57b2 2018-05-19 stsp goto done;
5561 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5562 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5563 26ed57b2 2018-05-19 stsp if (error)
5564 26ed57b2 2018-05-19 stsp goto done;
5566 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5567 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5568 26ed57b2 2018-05-19 stsp if (error)
5569 26ed57b2 2018-05-19 stsp goto done;
5571 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5572 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5573 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5574 ea5e7bb5 2018-08-01 stsp goto done;
5576 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5577 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5578 5dc9f4bc 2018-08-04 stsp if (error)
5579 5dc9f4bc 2018-08-04 stsp goto done;
5580 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5582 3dbaef42 2020-11-24 stsp free(label1);
5583 3dbaef42 2020-11-24 stsp free(label2);
5584 c02c541e 2019-03-29 stsp free(repo_path);
5585 a273ac94 2020-02-23 naddy free(cwd);
5586 1d0f4054 2021-06-17 stsp if (repo) {
5587 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5588 1d0f4054 2021-06-17 stsp if (error == NULL)
5589 1d0f4054 2021-06-17 stsp error = close_err;
5591 a273ac94 2020-02-23 naddy if (worktree)
5592 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5593 7cd52833 2022-06-23 thomas if (pack_fds) {
5594 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5595 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5596 7cd52833 2022-06-23 thomas if (error == NULL)
5597 7cd52833 2022-06-23 thomas error = pack_err;
5599 51a10b52 2020-12-26 stsp tog_free_refs();
5600 26ed57b2 2018-05-19 stsp return error;
5603 4ed7e80c 2018-05-20 stsp __dead static void
5604 9f7d7167 2018-04-29 stsp usage_blame(void)
5607 91198554 2022-06-23 thomas fprintf(stderr,
5608 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5609 9f7d7167 2018-04-29 stsp getprogname());
5613 84451b3e 2018-07-10 stsp struct tog_blame_line {
5614 84451b3e 2018-07-10 stsp int annotated;
5615 84451b3e 2018-07-10 stsp struct got_object_id *id;
5618 4ed7e80c 2018-05-20 stsp static const struct got_error *
5619 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5621 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5622 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5623 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5624 84451b3e 2018-07-10 stsp const struct got_error *err;
5625 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5626 826082fe 2020-12-10 stsp char *line = NULL;
5627 826082fe 2020-12-10 stsp size_t linesize = 0;
5628 826082fe 2020-12-10 stsp ssize_t linelen;
5629 84451b3e 2018-07-10 stsp wchar_t *wline;
5630 27a741e5 2019-09-11 stsp int width;
5631 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5632 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5633 ab089a2a 2018-07-12 stsp char *id_str;
5634 11b20872 2019-11-08 stsp struct tog_color *tc;
5636 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5638 ab089a2a 2018-07-12 stsp return err;
5640 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5641 f7d12f7e 2018-08-01 stsp werase(view->window);
5643 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5644 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5645 ab089a2a 2018-07-12 stsp free(id_str);
5646 ab089a2a 2018-07-12 stsp return err;
5649 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5650 ab089a2a 2018-07-12 stsp free(line);
5651 2550e4c3 2018-07-13 stsp line = NULL;
5653 1cae65b4 2019-09-22 stsp return err;
5654 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5655 a3404814 2018-09-02 stsp wstandout(view->window);
5656 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5658 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5659 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5660 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5661 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5663 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5664 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5665 a3404814 2018-09-02 stsp wstandend(view->window);
5666 2550e4c3 2018-07-13 stsp free(wline);
5667 2550e4c3 2018-07-13 stsp wline = NULL;
5669 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5670 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5672 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5673 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5674 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5675 ab089a2a 2018-07-12 stsp free(id_str);
5676 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5678 ab089a2a 2018-07-12 stsp free(id_str);
5679 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5680 3f60a8ef 2018-07-10 stsp free(line);
5681 2550e4c3 2018-07-13 stsp line = NULL;
5683 3f60a8ef 2018-07-10 stsp return err;
5684 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5685 2550e4c3 2018-07-13 stsp free(wline);
5686 2550e4c3 2018-07-13 stsp wline = NULL;
5687 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5688 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5690 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5691 05171be4 2022-06-23 thomas view->maxx = 0;
5692 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5693 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5694 826082fe 2020-12-10 stsp if (linelen == -1) {
5695 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5696 826082fe 2020-12-10 stsp s->eof = 1;
5699 84451b3e 2018-07-10 stsp free(line);
5700 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5702 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5703 07dd3ed3 2022-08-06 thomas continue;
5704 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5707 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5708 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5709 cbea3800 2022-06-23 thomas if (err) {
5710 cbea3800 2022-06-23 thomas free(line);
5711 cbea3800 2022-06-23 thomas return err;
5713 cbea3800 2022-06-23 thomas free(wline);
5714 cbea3800 2022-06-23 thomas wline = NULL;
5715 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5717 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5718 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5720 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5721 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5722 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5723 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5724 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5725 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5726 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5727 8d0fe45a 2019-08-12 stsp char *id_str;
5728 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5729 91198554 2022-06-23 thomas blame_line->id);
5730 8d0fe45a 2019-08-12 stsp if (err) {
5731 8d0fe45a 2019-08-12 stsp free(line);
5732 8d0fe45a 2019-08-12 stsp return err;
5734 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5736 11b20872 2019-11-08 stsp wattr_on(view->window,
5737 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5738 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5740 11b20872 2019-11-08 stsp wattr_off(view->window,
5741 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5742 8d0fe45a 2019-08-12 stsp free(id_str);
5743 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5745 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5746 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5749 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5750 ee41ec32 2018-07-10 stsp prev_id = NULL;
5753 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5754 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5755 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5757 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5758 41605754 2020-11-12 stsp width = 9;
5759 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5760 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5761 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5762 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5763 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5764 41605754 2020-11-12 stsp if (err) {
5765 41605754 2020-11-12 stsp free(line);
5766 41605754 2020-11-12 stsp return err;
5768 41605754 2020-11-12 stsp width += 9;
5770 923086fd 2022-06-23 thomas int skip;
5771 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5772 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5773 923086fd 2022-06-23 thomas if (err) {
5774 923086fd 2022-06-23 thomas free(line);
5775 923086fd 2022-06-23 thomas return err;
5777 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5778 02bce7e2 2022-06-23 thomas width += 9;
5779 41605754 2020-11-12 stsp free(wline);
5780 41605754 2020-11-12 stsp wline = NULL;
5783 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5784 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5785 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5786 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5788 826082fe 2020-12-10 stsp free(line);
5789 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5791 a5d43cac 2022-07-01 thomas view_border(view);
5793 84451b3e 2018-07-10 stsp return NULL;
5796 84451b3e 2018-07-10 stsp static const struct got_error *
5797 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5798 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5800 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5801 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5802 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5803 1a76625f 2018-10-22 stsp int errcode;
5805 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5806 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5807 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5809 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5810 1a76625f 2018-10-22 stsp if (errcode)
5811 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5813 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5814 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5815 d68a0a7d 2018-07-10 stsp goto done;
5818 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5819 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5821 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5822 d68a0a7d 2018-07-10 stsp if (line->annotated)
5823 d68a0a7d 2018-07-10 stsp goto done;
5825 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5826 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5827 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5828 84451b3e 2018-07-10 stsp goto done;
5830 84451b3e 2018-07-10 stsp line->annotated = 1;
5832 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5833 1a76625f 2018-10-22 stsp if (errcode)
5834 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5835 84451b3e 2018-07-10 stsp return err;
5838 84451b3e 2018-07-10 stsp static void *
5839 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5841 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5842 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5843 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5844 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5845 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5847 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5848 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5849 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5851 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5852 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5853 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5854 9117a7b7 2022-07-01 thomas goto done;
5857 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5858 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5859 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5860 9117a7b7 2022-07-01 thomas goto done;
5862 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5863 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5864 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5865 9117a7b7 2022-07-01 thomas goto done;
5868 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5870 9117a7b7 2022-07-01 thomas goto done;
5872 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5873 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5874 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5875 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5876 fc06ba56 2019-08-22 stsp err = NULL;
5878 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5879 9117a7b7 2022-07-01 thomas if (errcode) {
5880 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5881 9117a7b7 2022-07-01 thomas goto done;
5884 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5885 1d0f4054 2021-06-17 stsp if (err == NULL)
5886 1d0f4054 2021-06-17 stsp err = close_err;
5887 c9beca56 2018-07-22 stsp ta->repo = NULL;
5888 c9beca56 2018-07-22 stsp *ta->complete = 1;
5890 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5891 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5892 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5895 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5896 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5897 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5898 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5899 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5900 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5901 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5902 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5904 18430de3 2018-07-10 stsp return (void *)err;
5907 245d91c1 2018-07-12 stsp static struct got_object_id *
5908 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5909 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5911 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5913 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5914 8d0fe45a 2019-08-12 stsp return NULL;
5916 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5917 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5918 4fc71f3b 2022-07-12 thomas return NULL;
5920 4fc71f3b 2022-07-12 thomas return line->id;
5923 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5924 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5925 4fc71f3b 2022-07-12 thomas int lineno)
5927 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5929 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5930 4fc71f3b 2022-07-12 thomas return NULL;
5932 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5933 245d91c1 2018-07-12 stsp if (!line->annotated)
5934 245d91c1 2018-07-12 stsp return NULL;
5936 245d91c1 2018-07-12 stsp return line->id;
5939 b880a918 2018-07-10 stsp static const struct got_error *
5940 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5942 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5945 245d91c1 2018-07-12 stsp if (blame->thread) {
5946 1a76625f 2018-10-22 stsp int errcode;
5947 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5948 1a76625f 2018-10-22 stsp if (errcode)
5949 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5950 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5951 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5952 1a76625f 2018-10-22 stsp if (errcode)
5953 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5954 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5955 1a76625f 2018-10-22 stsp if (errcode)
5956 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5957 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5958 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5959 245d91c1 2018-07-12 stsp err = NULL;
5960 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5962 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5963 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5964 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5965 1d0f4054 2021-06-17 stsp if (err == NULL)
5966 1d0f4054 2021-06-17 stsp err = close_err;
5967 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5969 245d91c1 2018-07-12 stsp if (blame->f) {
5970 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5971 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5972 245d91c1 2018-07-12 stsp blame->f = NULL;
5974 57670559 2018-12-23 stsp if (blame->lines) {
5975 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5976 57670559 2018-12-23 stsp free(blame->lines[i].id);
5977 57670559 2018-12-23 stsp free(blame->lines);
5978 57670559 2018-12-23 stsp blame->lines = NULL;
5980 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5981 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5982 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5983 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5984 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5985 7cd52833 2022-06-23 thomas if (err == NULL)
5986 7cd52833 2022-06-23 thomas err = pack_err;
5987 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5989 245d91c1 2018-07-12 stsp return err;
5992 245d91c1 2018-07-12 stsp static const struct got_error *
5993 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5995 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5996 fc06ba56 2019-08-22 stsp int *done = arg;
5997 fc06ba56 2019-08-22 stsp int errcode;
5999 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6000 fc06ba56 2019-08-22 stsp if (errcode)
6001 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6002 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6004 fc06ba56 2019-08-22 stsp if (*done)
6005 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6007 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6008 fc06ba56 2019-08-22 stsp if (errcode)
6009 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6010 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6012 fc06ba56 2019-08-22 stsp return err;
6015 fc06ba56 2019-08-22 stsp static const struct got_error *
6016 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6018 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6019 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6020 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6021 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6022 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6023 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6024 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6025 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6026 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6028 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6029 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6031 15a94983 2018-12-23 stsp return err;
6033 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6034 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6035 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6036 f4ae6ddb 2022-07-01 thomas goto done;
6039 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6040 945f9229 2022-04-16 thomas if (err)
6041 945f9229 2022-04-16 thomas goto done;
6043 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6045 84451b3e 2018-07-10 stsp goto done;
6047 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6048 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6049 84451b3e 2018-07-10 stsp goto done;
6052 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6054 a70480e0 2018-06-23 stsp goto done;
6055 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6056 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6057 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6058 84451b3e 2018-07-10 stsp goto done;
6060 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6061 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6063 1fddf795 2021-01-20 stsp goto done;
6064 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6065 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6066 84451b3e 2018-07-10 stsp goto done;
6069 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6070 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6071 b02560ec 2019-08-19 stsp blame->nlines--;
6073 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6074 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6075 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6076 84451b3e 2018-07-10 stsp goto done;
6079 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6081 bd24772e 2018-07-11 stsp goto done;
6082 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6083 7cd52833 2022-06-23 thomas pack_fds);
6084 7cd52833 2022-06-23 thomas if (err)
6085 7cd52833 2022-06-23 thomas goto done;
6087 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6088 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6089 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6090 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6091 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6092 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6093 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6094 245d91c1 2018-07-12 stsp goto done;
6096 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6098 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6099 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6100 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6101 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6102 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6103 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6104 a5388363 2020-12-01 naddy s->blame_complete = 0;
6106 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6107 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6108 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6109 f5a09613 2020-12-13 naddy s->selected_line = 1;
6111 aa61903a 2021-12-31 thomas s->matched_line = 0;
6114 945f9229 2022-04-16 thomas if (commit)
6115 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6116 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6117 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6119 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6120 27d434c2 2018-09-15 stsp free(obj_id);
6122 245d91c1 2018-07-12 stsp stop_blame(blame);
6123 245d91c1 2018-07-12 stsp return err;
6126 245d91c1 2018-07-12 stsp static const struct got_error *
6127 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6128 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6130 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6131 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6133 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6135 c4843652 2019-08-12 stsp s->path = strdup(path);
6136 c4843652 2019-08-12 stsp if (s->path == NULL)
6137 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6139 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6140 c4843652 2019-08-12 stsp if (err) {
6141 c4843652 2019-08-12 stsp free(s->path);
6142 7cbe629d 2018-08-04 stsp return err;
6145 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6146 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6147 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6148 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6149 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6150 fb2756b9 2018-08-04 stsp s->repo = repo;
6151 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6152 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6154 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6155 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6156 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6157 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6159 11b20872 2019-11-08 stsp return err;
6162 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6163 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6164 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6165 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6166 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6167 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6168 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6170 a5388363 2020-12-01 naddy return run_blame(view);
6173 e5a0f69f 2018-08-18 stsp static const struct got_error *
6174 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6176 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6177 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6179 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6180 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6182 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6183 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6184 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6185 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6186 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6189 e5a0f69f 2018-08-18 stsp free(s->path);
6190 11b20872 2019-11-08 stsp free_colors(&s->colors);
6191 e5a0f69f 2018-08-18 stsp return err;
6194 7cbe629d 2018-08-04 stsp static const struct got_error *
6195 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6197 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6199 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6200 6c4c42e0 2019-06-24 stsp return NULL;
6203 2a7d3cd7 2022-09-17 thomas static void
6204 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6205 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6207 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6209 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6210 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6211 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6212 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6213 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6214 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6215 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6218 6c4c42e0 2019-06-24 stsp static const struct got_error *
6219 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6221 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6222 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6223 2b380cc8 2018-10-24 stsp int errcode;
6225 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6226 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6227 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6228 2b380cc8 2018-10-24 stsp if (errcode)
6229 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6231 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6234 51fe7530 2019-08-19 stsp if (s->blame_complete)
6235 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6237 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6239 a5d43cac 2022-07-01 thomas view_border(view);
6240 e5a0f69f 2018-08-18 stsp return err;
6243 e5a0f69f 2018-08-18 stsp static const struct got_error *
6244 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6245 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6247 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6248 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6250 eaeaa612 2022-07-20 thomas *new_view = NULL;
6252 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6253 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6254 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6256 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6257 eaeaa612 2022-07-20 thomas if (err)
6258 eaeaa612 2022-07-20 thomas view_close(log_view);
6260 eaeaa612 2022-07-20 thomas *new_view = log_view;
6262 eaeaa612 2022-07-20 thomas return err;
6265 eaeaa612 2022-07-20 thomas static const struct got_error *
6266 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6268 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6269 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6270 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6271 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6273 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6274 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6275 a5d43cac 2022-07-01 thomas --eos; /* border */
6277 e5a0f69f 2018-08-18 stsp switch (ch) {
6278 05171be4 2022-06-23 thomas case '0':
6279 05171be4 2022-06-23 thomas view->x = 0;
6281 05171be4 2022-06-23 thomas case '$':
6282 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
6283 07b0611c 2022-06-23 thomas view->count = 0;
6285 05171be4 2022-06-23 thomas case KEY_RIGHT:
6286 05171be4 2022-06-23 thomas case 'l':
6287 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
6288 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
6290 07b0611c 2022-06-23 thomas view->count = 0;
6292 05171be4 2022-06-23 thomas case KEY_LEFT:
6293 05171be4 2022-06-23 thomas case 'h':
6294 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
6295 07b0611c 2022-06-23 thomas if (view->x <= 0)
6296 07b0611c 2022-06-23 thomas view->count = 0;
6299 1e37a5c2 2019-05-12 jcs s->done = 1;
6301 4deef56f 2021-09-02 naddy case 'g':
6302 4deef56f 2021-09-02 naddy case KEY_HOME:
6303 4deef56f 2021-09-02 naddy s->selected_line = 1;
6304 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6305 07b0611c 2022-06-23 thomas view->count = 0;
6307 4deef56f 2021-09-02 naddy case 'G':
6308 4deef56f 2021-09-02 naddy case KEY_END:
6309 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6310 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6311 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6313 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6314 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6316 07b0611c 2022-06-23 thomas view->count = 0;
6319 1e37a5c2 2019-05-12 jcs case KEY_UP:
6320 f7140bf5 2021-10-17 thomas case CTRL('p'):
6321 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6322 1e37a5c2 2019-05-12 jcs s->selected_line--;
6323 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6324 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6325 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6327 07b0611c 2022-06-23 thomas view->count = 0;
6329 70f17a53 2022-06-13 thomas case CTRL('u'):
6330 23427b14 2022-06-23 thomas case 'u':
6331 70f17a53 2022-06-13 thomas nscroll /= 2;
6332 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6333 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6334 ea025d1d 2020-02-22 naddy case CTRL('b'):
6335 1c5e5faa 2022-06-23 thomas case 'b':
6336 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6337 07b0611c 2022-06-23 thomas if (view->count > 1)
6338 07b0611c 2022-06-23 thomas nscroll += nscroll;
6339 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6340 07b0611c 2022-06-23 thomas view->count = 0;
6343 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6344 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6346 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6349 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6350 f7140bf5 2021-10-17 thomas case CTRL('n'):
6351 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6352 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6353 1e37a5c2 2019-05-12 jcs s->selected_line++;
6354 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6355 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6357 07b0611c 2022-06-23 thomas view->count = 0;
6359 1c5e5faa 2022-06-23 thomas case 'c':
6360 1e37a5c2 2019-05-12 jcs case 'p': {
6361 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6363 07b0611c 2022-06-23 thomas view->count = 0;
6364 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6365 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6366 1e37a5c2 2019-05-12 jcs if (id == NULL)
6368 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6369 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6370 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6371 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6372 1e37a5c2 2019-05-12 jcs int obj_type;
6373 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6374 1e37a5c2 2019-05-12 jcs s->repo, id);
6377 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6378 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6379 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6380 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6383 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6384 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6385 ec242592 2022-04-22 thomas s->repo, &pid->id);
6386 945f9229 2022-04-16 thomas if (err)
6388 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6389 945f9229 2022-04-16 thomas pcommit, s->path);
6390 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6391 e5a0f69f 2018-08-18 stsp if (err) {
6392 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6393 1e37a5c2 2019-05-12 jcs err = NULL;
6394 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6397 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6399 1e37a5c2 2019-05-12 jcs free(blob_id);
6400 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6401 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6402 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6405 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6406 ec242592 2022-04-22 thomas &pid->id);
6407 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6409 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6410 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6412 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6417 1e37a5c2 2019-05-12 jcs s->done = 1;
6418 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6419 1e37a5c2 2019-05-12 jcs s->done = 0;
6420 1e37a5c2 2019-05-12 jcs if (thread_err)
6422 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6423 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6424 a5388363 2020-12-01 naddy err = run_blame(view);
6429 1c5e5faa 2022-06-23 thomas case 'C': {
6430 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6432 07b0611c 2022-06-23 thomas view->count = 0;
6433 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6434 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6436 1e37a5c2 2019-05-12 jcs s->done = 1;
6437 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6438 1e37a5c2 2019-05-12 jcs s->done = 0;
6439 1e37a5c2 2019-05-12 jcs if (thread_err)
6441 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6442 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6443 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6444 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6445 a5388363 2020-12-01 naddy err = run_blame(view);
6450 2a31b33b 2022-07-23 thomas case 'L':
6451 eaeaa612 2022-07-20 thomas view->count = 0;
6452 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6453 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6454 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6455 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6457 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6458 1e37a5c2 2019-05-12 jcs case '\r': {
6459 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6460 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6461 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6463 07b0611c 2022-06-23 thomas view->count = 0;
6464 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6465 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6466 1e37a5c2 2019-05-12 jcs if (id == NULL)
6468 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6471 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6472 4fc71f3b 2022-07-12 thomas if (*new_view) {
6473 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6474 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6475 4fc71f3b 2022-07-12 thomas if (err)
6477 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6478 4fc71f3b 2022-07-12 thomas } else {
6479 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6480 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6482 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6483 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6484 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6485 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6486 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6490 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6491 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6492 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6494 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6497 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6498 4fc71f3b 2022-07-12 thomas s->selected_line;
6499 4fc71f3b 2022-07-12 thomas if (*new_view)
6500 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6501 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6502 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6503 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6504 a5d43cac 2022-07-01 thomas if (err)
6508 e78dc838 2020-12-04 stsp view->focussed = 0;
6509 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6510 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6511 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6512 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6513 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6514 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6517 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6518 40236d76 2022-06-23 thomas if (err)
6520 e78dc838 2020-12-04 stsp view->focus_child = 1;
6522 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6527 70f17a53 2022-06-13 thomas case CTRL('d'):
6528 23427b14 2022-06-23 thomas case 'd':
6529 70f17a53 2022-06-13 thomas nscroll /= 2;
6530 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6531 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6532 ea025d1d 2020-02-22 naddy case CTRL('f'):
6533 1c5e5faa 2022-06-23 thomas case 'f':
6535 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6536 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6537 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6538 07b0611c 2022-06-23 thomas view->count = 0;
6541 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6542 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6543 70f17a53 2022-06-13 thomas s->selected_line +=
6544 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6545 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6547 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6548 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6550 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6551 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6553 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6554 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6555 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6556 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6560 07b0611c 2022-06-23 thomas view->count = 0;
6563 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6566 adf4c9e0 2022-07-03 thomas static const struct got_error *
6567 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6569 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6570 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6572 adf4c9e0 2022-07-03 thomas view->count = 0;
6573 adf4c9e0 2022-07-03 thomas s->done = 1;
6574 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6575 adf4c9e0 2022-07-03 thomas s->done = 0;
6576 adf4c9e0 2022-07-03 thomas if (err)
6577 adf4c9e0 2022-07-03 thomas return err;
6578 adf4c9e0 2022-07-03 thomas return run_blame(view);
6581 a70480e0 2018-06-23 stsp static const struct got_error *
6582 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6584 a70480e0 2018-06-23 stsp const struct got_error *error;
6585 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6586 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6587 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6588 0587e10c 2020-07-23 stsp char *link_target = NULL;
6589 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6590 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6591 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6593 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6594 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6596 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6597 a70480e0 2018-06-23 stsp switch (ch) {
6599 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6602 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6603 69069811 2018-08-02 stsp if (repo_path == NULL)
6604 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6608 17020d27 2019-03-07 stsp usage_blame();
6609 a70480e0 2018-06-23 stsp /* NOTREACHED */
6613 a70480e0 2018-06-23 stsp argc -= optind;
6614 a70480e0 2018-06-23 stsp argv += optind;
6616 f135c941 2020-02-20 stsp if (argc != 1)
6617 a70480e0 2018-06-23 stsp usage_blame();
6619 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6620 7cd52833 2022-06-23 thomas if (error != NULL)
6621 7cd52833 2022-06-23 thomas goto done;
6623 69069811 2018-08-02 stsp if (repo_path == NULL) {
6624 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6625 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6626 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6627 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6628 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6629 c156c7a4 2020-12-18 stsp goto done;
6630 f135c941 2020-02-20 stsp if (worktree)
6631 eb41ed75 2019-02-05 stsp repo_path =
6632 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6634 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6635 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6636 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6637 c156c7a4 2020-12-18 stsp goto done;
6641 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6642 c02c541e 2019-03-29 stsp if (error != NULL)
6643 8e94dd5b 2019-01-04 stsp goto done;
6645 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6646 f135c941 2020-02-20 stsp worktree);
6647 c02c541e 2019-03-29 stsp if (error)
6648 92205607 2019-01-04 stsp goto done;
6650 f135c941 2020-02-20 stsp init_curses();
6652 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6653 51a10b52 2020-12-26 stsp if (error)
6654 51a10b52 2020-12-26 stsp goto done;
6656 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6657 eb41ed75 2019-02-05 stsp if (error)
6658 69069811 2018-08-02 stsp goto done;
6660 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6661 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6662 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6663 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6664 a70480e0 2018-06-23 stsp if (error != NULL)
6665 66b4983c 2018-06-23 stsp goto done;
6666 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6667 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6669 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6670 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6672 a19e88aa 2018-06-23 stsp if (error != NULL)
6673 8b473291 2019-02-21 stsp goto done;
6675 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6676 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6677 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6678 e1cd8fed 2018-08-01 stsp goto done;
6681 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6682 945f9229 2022-04-16 thomas if (error)
6683 945f9229 2022-04-16 thomas goto done;
6685 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6686 945f9229 2022-04-16 thomas commit, repo);
6687 7cbe629d 2018-08-04 stsp if (error)
6688 7cbe629d 2018-08-04 stsp goto done;
6690 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6691 78756c87 2020-11-24 stsp commit_id, repo);
6692 0587e10c 2020-07-23 stsp if (error)
6693 0587e10c 2020-07-23 stsp goto done;
6694 12314ad4 2019-08-31 stsp if (worktree) {
6695 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6696 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6697 12314ad4 2019-08-31 stsp worktree = NULL;
6699 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6701 69069811 2018-08-02 stsp free(repo_path);
6702 f135c941 2020-02-20 stsp free(in_repo_path);
6703 0587e10c 2020-07-23 stsp free(link_target);
6704 69069811 2018-08-02 stsp free(cwd);
6705 a70480e0 2018-06-23 stsp free(commit_id);
6706 945f9229 2022-04-16 thomas if (commit)
6707 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6708 eb41ed75 2019-02-05 stsp if (worktree)
6709 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6710 1d0f4054 2021-06-17 stsp if (repo) {
6711 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6712 1d0f4054 2021-06-17 stsp if (error == NULL)
6713 1d0f4054 2021-06-17 stsp error = close_err;
6715 7cd52833 2022-06-23 thomas if (pack_fds) {
6716 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6717 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6718 7cd52833 2022-06-23 thomas if (error == NULL)
6719 7cd52833 2022-06-23 thomas error = pack_err;
6721 51a10b52 2020-12-26 stsp tog_free_refs();
6722 a70480e0 2018-06-23 stsp return error;
6725 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6726 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6728 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6729 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6730 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6731 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6732 86f4aab9 2022-09-09 thomas char *index = NULL;
6733 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6734 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6735 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6737 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6738 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6739 a5d43cac 2022-07-01 thomas --limit; /* border */
6741 f7d12f7e 2018-08-01 stsp werase(view->window);
6743 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6744 ffd1d5e5 2018-06-23 stsp return NULL;
6746 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6749 ffd1d5e5 2018-06-23 stsp return err;
6750 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6751 a3404814 2018-09-02 stsp wstandout(view->window);
6752 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6754 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6755 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6756 86f4aab9 2022-09-09 thomas free(wline);
6757 86f4aab9 2022-09-09 thomas wline = NULL;
6758 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6759 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6761 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6762 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6763 a3404814 2018-09-02 stsp wstandend(view->window);
6764 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6765 86f4aab9 2022-09-09 thomas return NULL;
6767 6f5f393a 2022-08-12 thomas i += s->selected;
6768 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6769 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6770 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6771 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6773 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6774 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6775 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6776 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6777 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6778 86f4aab9 2022-09-09 thomas free(index);
6780 ce52c690 2018-06-23 stsp return err;
6781 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6782 2550e4c3 2018-07-13 stsp free(wline);
6783 2550e4c3 2018-07-13 stsp wline = NULL;
6784 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6785 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6786 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6787 ffd1d5e5 2018-06-23 stsp return NULL;
6788 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6789 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6790 a1eca9bb 2018-06-23 stsp return NULL;
6792 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6793 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6794 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6795 0cf4efb1 2018-09-29 stsp if (view->focussed)
6796 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6797 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6799 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6800 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6801 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6802 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6803 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6804 ffd1d5e5 2018-06-23 stsp return NULL;
6808 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6811 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6812 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6813 848d6979 2019-08-12 stsp const char *modestr = "";
6814 56e0773d 2019-11-28 stsp mode_t mode;
6816 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6817 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6819 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6820 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6821 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6823 638f9024 2019-05-13 stsp return got_error_from_errno(
6824 230a42bd 2019-05-11 jcs "got_object_id_str");
6826 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6827 63c5ca5d 2019-08-24 stsp modestr = "$";
6828 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6831 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6832 d86d3b18 2020-12-01 naddy te, s->repo);
6833 0d6c6ee3 2020-05-20 stsp if (err) {
6834 0d6c6ee3 2020-05-20 stsp free(id_str);
6835 0d6c6ee3 2020-05-20 stsp return err;
6837 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6838 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6839 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6841 848d6979 2019-08-12 stsp modestr = "@";
6843 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6844 848d6979 2019-08-12 stsp modestr = "/";
6845 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6846 848d6979 2019-08-12 stsp modestr = "*";
6847 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6848 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6849 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6850 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6851 1d13200f 2018-07-12 stsp free(id_str);
6852 0d6c6ee3 2020-05-20 stsp free(link_target);
6853 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6855 1d13200f 2018-07-12 stsp free(id_str);
6856 0d6c6ee3 2020-05-20 stsp free(link_target);
6857 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6859 ffd1d5e5 2018-06-23 stsp if (err) {
6860 ffd1d5e5 2018-06-23 stsp free(line);
6863 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6864 0cf4efb1 2018-09-29 stsp if (view->focussed)
6865 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6866 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6868 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6870 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6871 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6872 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6874 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6875 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6876 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6877 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6878 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6879 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6880 ffd1d5e5 2018-06-23 stsp free(line);
6881 2550e4c3 2018-07-13 stsp free(wline);
6882 2550e4c3 2018-07-13 stsp wline = NULL;
6884 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6885 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6886 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6890 ffd1d5e5 2018-06-23 stsp return err;
6893 ffd1d5e5 2018-06-23 stsp static void
6894 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6896 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6897 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6898 fa86c4bf 2020-11-29 stsp int i = 0;
6900 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6903 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6904 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6905 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6906 fa86c4bf 2020-11-29 stsp if (!isroot)
6907 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6910 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6911 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6915 a5d43cac 2022-07-01 thomas static const struct got_error *
6916 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6918 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6919 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6920 ffd1d5e5 2018-06-23 stsp int n = 0;
6922 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6923 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6924 694d3271 2020-12-01 naddy s->first_displayed_entry);
6926 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6928 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6929 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6930 07dd3ed3 2022-08-06 thomas if (last) {
6931 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6932 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6934 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6935 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6936 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6940 a5d43cac 2022-07-01 thomas return NULL;
6943 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6944 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6945 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6947 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6948 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6949 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6951 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6952 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6953 56e0773d 2019-11-28 stsp + 1 /* slash */;
6955 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6957 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6958 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6959 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6961 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6962 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6963 d9765a41 2018-06-23 stsp while (pt) {
6964 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6965 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6966 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6967 cb2ebc8a 2018-06-23 stsp goto done;
6969 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6970 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6971 cb2ebc8a 2018-06-23 stsp goto done;
6973 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6976 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6977 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6978 ce52c690 2018-06-23 stsp goto done;
6982 ce52c690 2018-06-23 stsp if (err) {
6983 ce52c690 2018-06-23 stsp free(*path);
6984 ce52c690 2018-06-23 stsp *path = NULL;
6986 ce52c690 2018-06-23 stsp return err;
6989 ce52c690 2018-06-23 stsp static const struct got_error *
6990 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6991 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6992 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6994 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6995 ce52c690 2018-06-23 stsp char *path;
6996 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6998 a0de39f3 2019-08-09 stsp *new_view = NULL;
7000 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7002 ce52c690 2018-06-23 stsp return err;
7004 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7005 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7006 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7007 83ce39e3 2019-08-12 stsp goto done;
7010 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7011 e5a0f69f 2018-08-18 stsp if (err) {
7012 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7013 fc06ba56 2019-08-22 stsp err = NULL;
7014 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7016 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7018 83ce39e3 2019-08-12 stsp free(path);
7019 69efd4c4 2018-07-18 stsp return err;
7022 69efd4c4 2018-07-18 stsp static const struct got_error *
7023 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7024 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7026 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7027 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7028 69efd4c4 2018-07-18 stsp char *path;
7030 a0de39f3 2019-08-09 stsp *new_view = NULL;
7032 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7033 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7034 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7036 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7038 69efd4c4 2018-07-18 stsp return err;
7040 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7043 e5a0f69f 2018-08-18 stsp view_close(log_view);
7045 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7046 cb2ebc8a 2018-06-23 stsp free(path);
7047 cb2ebc8a 2018-06-23 stsp return err;
7050 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7051 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7052 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7054 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7055 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7056 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7057 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7059 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7060 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7062 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7063 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7064 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7066 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7068 bc573f3b 2021-07-10 stsp goto done;
7071 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7072 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7073 bc573f3b 2021-07-10 stsp * closed on demand.
7075 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7076 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7078 bc573f3b 2021-07-10 stsp goto done;
7079 bc573f3b 2021-07-10 stsp s->tree = s->root;
7081 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7082 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7083 ffd1d5e5 2018-06-23 stsp goto done;
7085 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7086 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7087 ffd1d5e5 2018-06-23 stsp goto done;
7090 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7091 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7092 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7093 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7094 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7095 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7096 9cd7cbd1 2020-12-07 stsp goto done;
7099 fb2756b9 2018-08-04 stsp s->repo = repo;
7101 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7102 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7103 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7104 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7106 c0b01bdb 2019-11-08 stsp goto done;
7107 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7108 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7110 c0b01bdb 2019-11-08 stsp goto done;
7111 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7112 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7113 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7115 c0b01bdb 2019-11-08 stsp goto done;
7117 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7118 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7119 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7121 11b20872 2019-11-08 stsp goto done;
7123 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7124 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7126 c0b01bdb 2019-11-08 stsp goto done;
7129 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7130 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7131 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7132 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7133 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7135 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7136 bc573f3b 2021-07-10 stsp if (commit)
7137 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7139 bc573f3b 2021-07-10 stsp close_tree_view(view);
7140 ad80ab7b 2018-08-04 stsp return err;
7143 e5a0f69f 2018-08-18 stsp static const struct got_error *
7144 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7146 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7148 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7149 fb2756b9 2018-08-04 stsp free(s->tree_label);
7150 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7151 6484ec90 2018-09-29 stsp free(s->commit_id);
7152 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7153 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7154 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7155 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7156 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7157 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7158 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7159 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7160 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7161 ad80ab7b 2018-08-04 stsp free(parent);
7164 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7165 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7166 bc573f3b 2021-07-10 stsp if (s->root)
7167 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7168 7c32bd05 2019-06-22 stsp return NULL;
7171 7c32bd05 2019-06-22 stsp static const struct got_error *
7172 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7174 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7176 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7177 7c32bd05 2019-06-22 stsp return NULL;
7180 7c32bd05 2019-06-22 stsp static int
7181 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7183 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7185 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7189 7c32bd05 2019-06-22 stsp static const struct got_error *
7190 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7192 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7193 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7195 7c32bd05 2019-06-22 stsp if (!view->searching) {
7196 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7197 7c32bd05 2019-06-22 stsp return NULL;
7200 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7201 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7202 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7203 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7204 56e0773d 2019-11-28 stsp s->selected_entry);
7206 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7208 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7209 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7211 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7212 56e0773d 2019-11-28 stsp s->selected_entry);
7215 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7216 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7217 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7218 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7220 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7223 7c32bd05 2019-06-22 stsp while (1) {
7224 56e0773d 2019-11-28 stsp if (te == NULL) {
7225 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7226 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7227 ac66afb8 2019-06-24 stsp return NULL;
7229 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7230 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7232 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7235 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7236 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7237 56e0773d 2019-11-28 stsp s->matched_entry = te;
7241 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7242 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7244 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7247 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7248 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7249 7c32bd05 2019-06-22 stsp s->selected = 0;
7252 e5a0f69f 2018-08-18 stsp return NULL;
7255 ad80ab7b 2018-08-04 stsp static const struct got_error *
7256 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7258 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7259 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7260 e5a0f69f 2018-08-18 stsp char *parent_path;
7262 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7264 e5a0f69f 2018-08-18 stsp return err;
7266 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7267 e5a0f69f 2018-08-18 stsp free(parent_path);
7269 a5d43cac 2022-07-01 thomas view_border(view);
7270 e5a0f69f 2018-08-18 stsp return err;
7273 07dd3ed3 2022-08-06 thomas static const struct got_error *
7274 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7276 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7277 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7278 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7279 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7280 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7281 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7283 07dd3ed3 2022-08-06 thomas g = view->gline;
7284 07dd3ed3 2022-08-06 thomas view->gline = 0;
7286 07dd3ed3 2022-08-06 thomas if (g == 0)
7288 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7289 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7291 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7292 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7293 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7295 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7296 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7297 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7299 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7300 07dd3ed3 2022-08-06 thomas last += off;
7302 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7303 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7304 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7307 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7308 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7309 07dd3ed3 2022-08-06 thomas i += off;
7312 07dd3ed3 2022-08-06 thomas if (i < g) {
7313 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7314 07dd3ed3 2022-08-06 thomas if (err)
7315 07dd3ed3 2022-08-06 thomas return err;
7316 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7317 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7318 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7319 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7320 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7321 07dd3ed3 2022-08-06 thomas first += off;
7322 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7324 07dd3ed3 2022-08-06 thomas } else if (i > g)
7325 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7327 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7328 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7329 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7331 07dd3ed3 2022-08-06 thomas return NULL;
7334 e5a0f69f 2018-08-18 stsp static const struct got_error *
7335 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7337 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7338 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7339 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7340 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7342 07dd3ed3 2022-08-06 thomas if (view->gline)
7343 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7345 e5a0f69f 2018-08-18 stsp switch (ch) {
7347 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7348 07b0611c 2022-06-23 thomas view->count = 0;
7350 1be4947a 2022-07-22 thomas case 'L':
7351 07b0611c 2022-06-23 thomas view->count = 0;
7352 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7354 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7356 1be4947a 2022-07-22 thomas case 'R':
7357 07b0611c 2022-06-23 thomas view->count = 0;
7358 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7360 e4526bf5 2021-09-03 naddy case 'g':
7361 aa7a1117 2023-01-09 thomas case '=':
7362 e4526bf5 2021-09-03 naddy case KEY_HOME:
7363 e4526bf5 2021-09-03 naddy s->selected = 0;
7364 07b0611c 2022-06-23 thomas view->count = 0;
7365 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7366 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7367 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7369 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7371 e4526bf5 2021-09-03 naddy case 'G':
7372 aa7a1117 2023-01-09 thomas case '*':
7373 a5d43cac 2022-07-01 thomas case KEY_END: {
7374 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7376 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7377 a5d43cac 2022-07-01 thomas --eos; /* border */
7378 e4526bf5 2021-09-03 naddy s->selected = 0;
7379 07b0611c 2022-06-23 thomas view->count = 0;
7380 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7381 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7382 e4526bf5 2021-09-03 naddy if (te == NULL) {
7383 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7384 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7389 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7390 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7392 e4526bf5 2021-09-03 naddy if (n > 0)
7393 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7397 1e37a5c2 2019-05-12 jcs case KEY_UP:
7398 f7140bf5 2021-10-17 thomas case CTRL('p'):
7399 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7400 1e37a5c2 2019-05-12 jcs s->selected--;
7403 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7404 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7405 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7406 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7407 07b0611c 2022-06-23 thomas view->count = 0;
7409 70f17a53 2022-06-13 thomas case CTRL('u'):
7410 23427b14 2022-06-23 thomas case 'u':
7411 70f17a53 2022-06-13 thomas nscroll /= 2;
7412 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7413 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7414 ea025d1d 2020-02-22 naddy case CTRL('b'):
7415 1c5e5faa 2022-06-23 thomas case 'b':
7416 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7417 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7418 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7419 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7421 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7422 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7424 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7425 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7426 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7427 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7428 07b0611c 2022-06-23 thomas view->count = 0;
7431 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7432 f7140bf5 2021-10-17 thomas case CTRL('n'):
7433 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7434 1e37a5c2 2019-05-12 jcs s->selected++;
7437 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7438 07b0611c 2022-06-23 thomas == NULL) {
7439 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7440 07b0611c 2022-06-23 thomas view->count = 0;
7443 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7445 70f17a53 2022-06-13 thomas case CTRL('d'):
7446 23427b14 2022-06-23 thomas case 'd':
7447 70f17a53 2022-06-13 thomas nscroll /= 2;
7448 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7449 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7450 ea025d1d 2020-02-22 naddy case CTRL('f'):
7451 1c5e5faa 2022-06-23 thomas case 'f':
7452 4c2d69cb 2022-06-23 thomas case ' ':
7453 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7455 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7456 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7457 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7458 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7460 07b0611c 2022-06-23 thomas view->count = 0;
7463 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7465 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7467 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7468 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7469 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7470 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7471 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7472 07b0611c 2022-06-23 thomas view->count = 0;
7475 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7476 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7478 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7479 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7480 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7481 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7482 1e37a5c2 2019-05-12 jcs s->selected_entry =
7483 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7484 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7485 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7486 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7487 a5d43cac 2022-07-01 thomas if (err)
7490 1e37a5c2 2019-05-12 jcs free(parent);
7491 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7492 56e0773d 2019-11-28 stsp s->selected_entry))) {
7493 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7494 07b0611c 2022-06-23 thomas view->count = 0;
7495 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7496 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7499 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7500 941e9f74 2019-05-21 stsp if (err) {
7501 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7504 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7505 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7507 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7508 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7509 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7510 07b0611c 2022-06-23 thomas view->count = 0;
7513 07b0611c 2022-06-23 thomas view->count = 0;
7517 ffd1d5e5 2018-06-23 stsp return err;
7520 ffd1d5e5 2018-06-23 stsp __dead static void
7521 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7524 91198554 2022-06-23 thomas fprintf(stderr,
7525 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7526 ffd1d5e5 2018-06-23 stsp getprogname());
7530 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7531 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7533 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7534 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7535 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7536 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7537 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7538 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7539 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7540 4e97c21c 2020-12-06 stsp char *label = NULL;
7541 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7542 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7544 5221c383 2018-08-01 stsp struct tog_view *view;
7545 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7547 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7548 ffd1d5e5 2018-06-23 stsp switch (ch) {
7550 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7553 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7554 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7555 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7559 e99e2d15 2020-11-24 naddy usage_tree();
7560 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7564 ffd1d5e5 2018-06-23 stsp argc -= optind;
7565 ffd1d5e5 2018-06-23 stsp argv += optind;
7567 55cccc34 2020-02-20 stsp if (argc > 1)
7568 e99e2d15 2020-11-24 naddy usage_tree();
7570 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7571 7cd52833 2022-06-23 thomas if (error != NULL)
7572 7cd52833 2022-06-23 thomas goto done;
7574 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7575 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7576 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7577 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7578 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7579 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7580 c156c7a4 2020-12-18 stsp goto done;
7581 55cccc34 2020-02-20 stsp if (worktree)
7582 52185f70 2019-02-05 stsp repo_path =
7583 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7585 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7586 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7587 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7588 c156c7a4 2020-12-18 stsp goto done;
7592 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7593 c02c541e 2019-03-29 stsp if (error != NULL)
7594 52185f70 2019-02-05 stsp goto done;
7596 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7597 55cccc34 2020-02-20 stsp repo, worktree);
7598 55cccc34 2020-02-20 stsp if (error)
7599 55cccc34 2020-02-20 stsp goto done;
7601 55cccc34 2020-02-20 stsp init_curses();
7603 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7604 c02c541e 2019-03-29 stsp if (error)
7605 52185f70 2019-02-05 stsp goto done;
7607 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7608 51a10b52 2020-12-26 stsp if (error)
7609 51a10b52 2020-12-26 stsp goto done;
7611 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7612 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7613 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7614 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7615 4e97c21c 2020-12-06 stsp if (error)
7616 4e97c21c 2020-12-06 stsp goto done;
7617 4e97c21c 2020-12-06 stsp head_ref_name = label;
7619 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7620 4e97c21c 2020-12-06 stsp if (error == NULL)
7621 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7622 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7623 4e97c21c 2020-12-06 stsp goto done;
7624 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7625 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7626 4e97c21c 2020-12-06 stsp if (error)
7627 4e97c21c 2020-12-06 stsp goto done;
7630 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7631 945f9229 2022-04-16 thomas if (error)
7632 945f9229 2022-04-16 thomas goto done;
7634 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7635 5221c383 2018-08-01 stsp if (view == NULL) {
7636 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7637 5221c383 2018-08-01 stsp goto done;
7639 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7640 ad80ab7b 2018-08-04 stsp if (error)
7641 ad80ab7b 2018-08-04 stsp goto done;
7642 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7643 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7644 d91faf3b 2020-12-01 naddy in_repo_path);
7645 55cccc34 2020-02-20 stsp if (error)
7646 55cccc34 2020-02-20 stsp goto done;
7649 55cccc34 2020-02-20 stsp if (worktree) {
7650 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7651 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7652 55cccc34 2020-02-20 stsp worktree = NULL;
7654 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7656 52185f70 2019-02-05 stsp free(repo_path);
7657 e4a0e26d 2020-02-20 stsp free(cwd);
7658 ffd1d5e5 2018-06-23 stsp free(commit_id);
7659 4e97c21c 2020-12-06 stsp free(label);
7661 486cd271 2020-12-06 stsp got_ref_close(ref);
7662 1d0f4054 2021-06-17 stsp if (repo) {
7663 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7664 1d0f4054 2021-06-17 stsp if (error == NULL)
7665 1d0f4054 2021-06-17 stsp error = close_err;
7667 7cd52833 2022-06-23 thomas if (pack_fds) {
7668 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7669 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7670 7cd52833 2022-06-23 thomas if (error == NULL)
7671 7cd52833 2022-06-23 thomas error = pack_err;
7673 51a10b52 2020-12-26 stsp tog_free_refs();
7674 ffd1d5e5 2018-06-23 stsp return error;
7677 6458efa5 2020-11-24 stsp static const struct got_error *
7678 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7680 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7681 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7683 6458efa5 2020-11-24 stsp s->nrefs = 0;
7684 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7685 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7686 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7687 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7688 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7691 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7692 6458efa5 2020-11-24 stsp if (re == NULL)
7693 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7695 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7696 8924d611 2020-12-26 stsp if (re->ref == NULL)
7697 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7698 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7699 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7702 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7703 6458efa5 2020-11-24 stsp return NULL;
7706 ef20f542 2022-06-26 thomas static void
7707 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7709 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7711 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7712 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7713 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7714 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7719 6458efa5 2020-11-24 stsp static const struct got_error *
7720 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7722 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7723 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7725 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7726 6458efa5 2020-11-24 stsp s->repo = repo;
7728 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7729 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7731 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7733 6458efa5 2020-11-24 stsp return err;
7735 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7736 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7737 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7738 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7740 6458efa5 2020-11-24 stsp goto done;
7742 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7743 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7744 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7746 6458efa5 2020-11-24 stsp goto done;
7748 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7749 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7750 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7751 2183bbf6 2022-01-23 thomas if (err)
7752 2183bbf6 2022-01-23 thomas goto done;
7754 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7755 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7756 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7758 6458efa5 2020-11-24 stsp goto done;
7761 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7762 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7763 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7764 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7765 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7768 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7769 6458efa5 2020-11-24 stsp return err;
7772 6458efa5 2020-11-24 stsp static const struct got_error *
7773 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7775 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7777 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7778 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7780 6458efa5 2020-11-24 stsp return NULL;
7783 6458efa5 2020-11-24 stsp static const struct got_error *
7784 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7785 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7787 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7788 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7789 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7790 6458efa5 2020-11-24 stsp int obj_type;
7792 c42c9805 2020-11-24 stsp *commit_id = NULL;
7794 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7796 6458efa5 2020-11-24 stsp return err;
7798 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7800 6458efa5 2020-11-24 stsp goto done;
7802 6458efa5 2020-11-24 stsp switch (obj_type) {
7803 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7804 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7806 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7807 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7809 6458efa5 2020-11-24 stsp goto done;
7810 c42c9805 2020-11-24 stsp free(obj_id);
7811 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7812 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7814 6458efa5 2020-11-24 stsp goto done;
7815 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7816 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7817 c42c9805 2020-11-24 stsp goto done;
7819 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7820 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7821 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7822 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7823 c42c9805 2020-11-24 stsp goto done;
7827 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7833 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7834 c42c9805 2020-11-24 stsp if (err) {
7835 c42c9805 2020-11-24 stsp free(*commit_id);
7836 c42c9805 2020-11-24 stsp *commit_id = NULL;
7838 c42c9805 2020-11-24 stsp return err;
7841 c42c9805 2020-11-24 stsp static const struct got_error *
7842 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7843 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7845 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7846 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7847 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7849 c42c9805 2020-11-24 stsp *new_view = NULL;
7851 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7852 c42c9805 2020-11-24 stsp if (err) {
7853 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7854 c42c9805 2020-11-24 stsp return err;
7856 c42c9805 2020-11-24 stsp return NULL;
7859 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7860 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7861 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7862 6458efa5 2020-11-24 stsp goto done;
7865 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7866 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7869 6458efa5 2020-11-24 stsp view_close(log_view);
7871 6458efa5 2020-11-24 stsp *new_view = log_view;
7872 c42c9805 2020-11-24 stsp free(commit_id);
7873 6458efa5 2020-11-24 stsp return err;
7876 ce5b7c56 2019-07-09 stsp static void
7877 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7879 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7880 34ba6917 2020-11-30 stsp int i = 0;
7882 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7885 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7886 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7887 34ba6917 2020-11-30 stsp if (re == NULL)
7889 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7890 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7894 a5d43cac 2022-07-01 thomas static const struct got_error *
7895 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7897 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7898 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7899 6458efa5 2020-11-24 stsp int n = 0;
7901 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7902 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7904 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7906 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7907 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7908 07dd3ed3 2022-08-06 thomas if (last) {
7909 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7910 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7912 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7913 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7914 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7918 a5d43cac 2022-07-01 thomas return NULL;
7921 6458efa5 2020-11-24 stsp static const struct got_error *
7922 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7924 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7926 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7927 6458efa5 2020-11-24 stsp return NULL;
7930 6458efa5 2020-11-24 stsp static int
7931 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7933 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7935 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7939 6458efa5 2020-11-24 stsp static const struct got_error *
7940 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7942 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7943 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7945 6458efa5 2020-11-24 stsp if (!view->searching) {
7946 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7947 6458efa5 2020-11-24 stsp return NULL;
7950 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7951 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7952 6458efa5 2020-11-24 stsp if (s->selected_entry)
7953 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7955 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7956 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7958 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7959 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7961 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7962 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7965 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7966 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7967 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7968 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7970 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7973 6458efa5 2020-11-24 stsp while (1) {
7974 6458efa5 2020-11-24 stsp if (re == NULL) {
7975 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7976 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7977 6458efa5 2020-11-24 stsp return NULL;
7979 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7980 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7982 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7985 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7986 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7987 6458efa5 2020-11-24 stsp s->matched_entry = re;
7991 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7992 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7994 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7997 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7998 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7999 6458efa5 2020-11-24 stsp s->selected = 0;
8002 6458efa5 2020-11-24 stsp return NULL;
8005 6458efa5 2020-11-24 stsp static const struct got_error *
8006 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8008 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8009 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8010 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8011 6458efa5 2020-11-24 stsp char *line = NULL;
8012 6458efa5 2020-11-24 stsp wchar_t *wline;
8013 6458efa5 2020-11-24 stsp struct tog_color *tc;
8014 6458efa5 2020-11-24 stsp int width, n;
8015 6458efa5 2020-11-24 stsp int limit = view->nlines;
8017 6458efa5 2020-11-24 stsp werase(view->window);
8019 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8020 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8021 a5d43cac 2022-07-01 thomas --limit; /* border */
8023 6458efa5 2020-11-24 stsp if (limit == 0)
8024 6458efa5 2020-11-24 stsp return NULL;
8026 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8028 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8029 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8030 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8032 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8033 6458efa5 2020-11-24 stsp if (err) {
8034 6458efa5 2020-11-24 stsp free(line);
8035 6458efa5 2020-11-24 stsp return err;
8037 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8038 6458efa5 2020-11-24 stsp wstandout(view->window);
8039 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8040 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8041 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8042 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8043 6458efa5 2020-11-24 stsp wstandend(view->window);
8044 6458efa5 2020-11-24 stsp free(wline);
8045 6458efa5 2020-11-24 stsp wline = NULL;
8046 6458efa5 2020-11-24 stsp free(line);
8047 6458efa5 2020-11-24 stsp line = NULL;
8048 6458efa5 2020-11-24 stsp if (--limit <= 0)
8049 6458efa5 2020-11-24 stsp return NULL;
8052 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8053 6458efa5 2020-11-24 stsp char *line = NULL;
8054 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8056 84227eb1 2022-06-23 thomas if (s->show_date) {
8057 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8058 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8059 84227eb1 2022-06-23 thomas struct got_object_id *id;
8060 84227eb1 2022-06-23 thomas struct tm tm;
8061 84227eb1 2022-06-23 thomas time_t t;
8063 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8064 84227eb1 2022-06-23 thomas if (err)
8065 84227eb1 2022-06-23 thomas return err;
8066 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8067 84227eb1 2022-06-23 thomas if (err) {
8068 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8069 84227eb1 2022-06-23 thomas free(id);
8070 84227eb1 2022-06-23 thomas return err;
8072 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8074 84227eb1 2022-06-23 thomas if (err) {
8075 84227eb1 2022-06-23 thomas free(id);
8076 84227eb1 2022-06-23 thomas return err;
8078 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8079 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8080 84227eb1 2022-06-23 thomas } else {
8081 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8082 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8084 84227eb1 2022-06-23 thomas free(id);
8085 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8086 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8087 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8088 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8090 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8091 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8092 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8093 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8094 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8095 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8096 6458efa5 2020-11-24 stsp struct got_object_id *id;
8097 6458efa5 2020-11-24 stsp char *id_str;
8098 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8100 6458efa5 2020-11-24 stsp return err;
8101 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8102 6458efa5 2020-11-24 stsp if (err) {
8104 6458efa5 2020-11-24 stsp return err;
8106 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8107 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8108 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8110 6458efa5 2020-11-24 stsp free(id_str);
8111 6458efa5 2020-11-24 stsp return err;
8114 6458efa5 2020-11-24 stsp free(id_str);
8115 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8116 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8117 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8119 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8121 6458efa5 2020-11-24 stsp if (err) {
8122 6458efa5 2020-11-24 stsp free(line);
8123 6458efa5 2020-11-24 stsp return err;
8125 6458efa5 2020-11-24 stsp if (n == s->selected) {
8126 6458efa5 2020-11-24 stsp if (view->focussed)
8127 6458efa5 2020-11-24 stsp wstandout(view->window);
8128 6458efa5 2020-11-24 stsp s->selected_entry = re;
8130 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8132 6458efa5 2020-11-24 stsp wattr_on(view->window,
8133 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8134 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8136 6458efa5 2020-11-24 stsp wattr_off(view->window,
8137 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8138 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8139 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8140 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8141 6458efa5 2020-11-24 stsp wstandend(view->window);
8142 6458efa5 2020-11-24 stsp free(line);
8143 6458efa5 2020-11-24 stsp free(wline);
8144 6458efa5 2020-11-24 stsp wline = NULL;
8146 6458efa5 2020-11-24 stsp s->ndisplayed++;
8147 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8150 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8153 a5d43cac 2022-07-01 thomas view_border(view);
8154 6458efa5 2020-11-24 stsp return err;
8157 6458efa5 2020-11-24 stsp static const struct got_error *
8158 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8159 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8161 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8162 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8163 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8165 c42c9805 2020-11-24 stsp *new_view = NULL;
8167 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8168 c42c9805 2020-11-24 stsp if (err) {
8169 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8170 c42c9805 2020-11-24 stsp return err;
8172 c42c9805 2020-11-24 stsp return NULL;
8176 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8177 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8178 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8179 c42c9805 2020-11-24 stsp goto done;
8182 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8183 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8185 c42c9805 2020-11-24 stsp goto done;
8187 c42c9805 2020-11-24 stsp *new_view = tree_view;
8189 c42c9805 2020-11-24 stsp free(commit_id);
8190 c42c9805 2020-11-24 stsp return err;
8193 07dd3ed3 2022-08-06 thomas static const struct got_error *
8194 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8196 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8197 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8198 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8200 07dd3ed3 2022-08-06 thomas g = view->gline;
8201 07dd3ed3 2022-08-06 thomas view->gline = 0;
8203 07dd3ed3 2022-08-06 thomas if (g == 0)
8205 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8206 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8208 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8209 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8210 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8211 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8212 07dd3ed3 2022-08-06 thomas return NULL;
8215 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8216 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8217 07dd3ed3 2022-08-06 thomas if (err)
8218 07dd3ed3 2022-08-06 thomas return err;
8219 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8220 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8221 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8222 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8223 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8224 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8226 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8227 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8229 07dd3ed3 2022-08-06 thomas return NULL;
8233 c42c9805 2020-11-24 stsp static const struct got_error *
8234 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8236 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8237 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8238 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8239 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8241 07dd3ed3 2022-08-06 thomas if (view->gline)
8242 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8244 6458efa5 2020-11-24 stsp switch (ch) {
8246 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8247 07b0611c 2022-06-23 thomas view->count = 0;
8249 84227eb1 2022-06-23 thomas case 'm':
8250 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8251 07b0611c 2022-06-23 thomas view->count = 0;
8253 98182bd0 2021-11-20 thomas case 'o':
8254 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8255 07b0611c 2022-06-23 thomas view->count = 0;
8256 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8257 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8258 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8259 c591440f 2021-11-20 thomas if (err)
8261 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8262 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8263 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8264 3bfadbd4 2021-11-20 thomas if (err)
8266 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8267 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8269 6458efa5 2020-11-24 stsp case KEY_ENTER:
8270 6458efa5 2020-11-24 stsp case '\r':
8271 07b0611c 2022-06-23 thomas view->count = 0;
8272 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8274 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8276 1be4947a 2022-07-22 thomas case 'T':
8277 07b0611c 2022-06-23 thomas view->count = 0;
8278 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8280 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8282 e4526bf5 2021-09-03 naddy case 'g':
8283 aa7a1117 2023-01-09 thomas case '=':
8284 e4526bf5 2021-09-03 naddy case KEY_HOME:
8285 e4526bf5 2021-09-03 naddy s->selected = 0;
8286 07b0611c 2022-06-23 thomas view->count = 0;
8287 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8289 e4526bf5 2021-09-03 naddy case 'G':
8290 aa7a1117 2023-01-09 thomas case '*':
8291 a5d43cac 2022-07-01 thomas case KEY_END: {
8292 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8294 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8295 a5d43cac 2022-07-01 thomas --eos; /* border */
8296 e4526bf5 2021-09-03 naddy s->selected = 0;
8297 07b0611c 2022-06-23 thomas view->count = 0;
8298 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8299 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8300 e4526bf5 2021-09-03 naddy if (re == NULL)
8302 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8303 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8305 e4526bf5 2021-09-03 naddy if (n > 0)
8306 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8310 6458efa5 2020-11-24 stsp case KEY_UP:
8311 f7140bf5 2021-10-17 thomas case CTRL('p'):
8312 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8313 6458efa5 2020-11-24 stsp s->selected--;
8316 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8317 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8318 07b0611c 2022-06-23 thomas view->count = 0;
8320 70f17a53 2022-06-13 thomas case CTRL('u'):
8321 23427b14 2022-06-23 thomas case 'u':
8322 70f17a53 2022-06-13 thomas nscroll /= 2;
8323 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8324 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8325 6458efa5 2020-11-24 stsp case CTRL('b'):
8326 1c5e5faa 2022-06-23 thomas case 'b':
8327 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8328 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8329 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8330 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8331 07b0611c 2022-06-23 thomas view->count = 0;
8334 6458efa5 2020-11-24 stsp case KEY_DOWN:
8335 f7140bf5 2021-10-17 thomas case CTRL('n'):
8336 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8337 6458efa5 2020-11-24 stsp s->selected++;
8340 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8341 6458efa5 2020-11-24 stsp /* can't scroll any further */
8342 07b0611c 2022-06-23 thomas view->count = 0;
8345 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8347 70f17a53 2022-06-13 thomas case CTRL('d'):
8348 23427b14 2022-06-23 thomas case 'd':
8349 70f17a53 2022-06-13 thomas nscroll /= 2;
8350 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8351 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8352 6458efa5 2020-11-24 stsp case CTRL('f'):
8353 1c5e5faa 2022-06-23 thomas case 'f':
8354 4c2d69cb 2022-06-23 thomas case ' ':
8355 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8356 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8357 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8358 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8359 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8360 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8361 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8362 07b0611c 2022-06-23 thomas view->count = 0;
8365 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8367 6458efa5 2020-11-24 stsp case CTRL('l'):
8368 07b0611c 2022-06-23 thomas view->count = 0;
8369 8924d611 2020-12-26 stsp tog_free_refs();
8370 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8373 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8374 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8376 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8377 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8378 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8381 07b0611c 2022-06-23 thomas view->count = 0;
8385 6458efa5 2020-11-24 stsp return err;
8388 6458efa5 2020-11-24 stsp __dead static void
8389 6458efa5 2020-11-24 stsp usage_ref(void)
8392 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8393 6458efa5 2020-11-24 stsp getprogname());
8397 6458efa5 2020-11-24 stsp static const struct got_error *
8398 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8400 6458efa5 2020-11-24 stsp const struct got_error *error;
8401 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8402 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8403 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8405 6458efa5 2020-11-24 stsp struct tog_view *view;
8406 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8408 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8409 6458efa5 2020-11-24 stsp switch (ch) {
8411 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8412 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8413 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8417 e99e2d15 2020-11-24 naddy usage_ref();
8418 6458efa5 2020-11-24 stsp /* NOTREACHED */
8422 6458efa5 2020-11-24 stsp argc -= optind;
8423 6458efa5 2020-11-24 stsp argv += optind;
8425 6458efa5 2020-11-24 stsp if (argc > 1)
8426 e99e2d15 2020-11-24 naddy usage_ref();
8428 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8429 7cd52833 2022-06-23 thomas if (error != NULL)
8430 7cd52833 2022-06-23 thomas goto done;
8432 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8433 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8434 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8435 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8436 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8437 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8438 c156c7a4 2020-12-18 stsp goto done;
8439 6458efa5 2020-11-24 stsp if (worktree)
8440 6458efa5 2020-11-24 stsp repo_path =
8441 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8443 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8444 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8445 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8446 c156c7a4 2020-12-18 stsp goto done;
8450 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8451 6458efa5 2020-11-24 stsp if (error != NULL)
8452 6458efa5 2020-11-24 stsp goto done;
8454 6458efa5 2020-11-24 stsp init_curses();
8456 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8457 51a10b52 2020-12-26 stsp if (error)
8458 51a10b52 2020-12-26 stsp goto done;
8460 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8461 6458efa5 2020-11-24 stsp if (error)
8462 6458efa5 2020-11-24 stsp goto done;
8464 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8465 6458efa5 2020-11-24 stsp if (view == NULL) {
8466 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8467 6458efa5 2020-11-24 stsp goto done;
8470 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8471 6458efa5 2020-11-24 stsp if (error)
8472 6458efa5 2020-11-24 stsp goto done;
8474 6458efa5 2020-11-24 stsp if (worktree) {
8475 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8476 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8477 6458efa5 2020-11-24 stsp worktree = NULL;
8479 6458efa5 2020-11-24 stsp error = view_loop(view);
8481 6458efa5 2020-11-24 stsp free(repo_path);
8482 6458efa5 2020-11-24 stsp free(cwd);
8483 1d0f4054 2021-06-17 stsp if (repo) {
8484 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8485 1d0f4054 2021-06-17 stsp if (close_err)
8486 1d0f4054 2021-06-17 stsp error = close_err;
8488 7cd52833 2022-06-23 thomas if (pack_fds) {
8489 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8490 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8491 7cd52833 2022-06-23 thomas if (error == NULL)
8492 7cd52833 2022-06-23 thomas error = pack_err;
8494 51a10b52 2020-12-26 stsp tog_free_refs();
8495 6458efa5 2020-11-24 stsp return error;
8498 fc2737d5 2022-09-15 thomas static const struct got_error*
8499 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8500 fc2737d5 2022-09-15 thomas const char *str)
8502 fc2737d5 2022-09-15 thomas size_t len;
8504 fc2737d5 2022-09-15 thomas if (win == NULL)
8505 fc2737d5 2022-09-15 thomas win = stdscr;
8507 fc2737d5 2022-09-15 thomas len = strlen(str);
8508 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8510 fc2737d5 2022-09-15 thomas if (focus)
8511 fc2737d5 2022-09-15 thomas wstandout(win);
8512 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8513 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8514 fc2737d5 2022-09-15 thomas if (focus)
8515 fc2737d5 2022-09-15 thomas wstandend(win);
8517 fc2737d5 2022-09-15 thomas return NULL;
8520 2a31b33b 2022-07-23 thomas static const struct got_error *
8521 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8523 fc2737d5 2022-09-15 thomas off_t *p;
8525 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8526 fc2737d5 2022-09-15 thomas if (p == NULL) {
8527 fc2737d5 2022-09-15 thomas free(*line_offsets);
8528 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8529 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8532 fc2737d5 2022-09-15 thomas *line_offsets = p;
8533 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8534 fc2737d5 2022-09-15 thomas ++(*nlines);
8535 fc2737d5 2022-09-15 thomas return NULL;
8538 fc2737d5 2022-09-15 thomas static const struct got_error *
8539 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8541 fc2737d5 2022-09-15 thomas *ret = 0;
8543 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8544 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8545 fc2737d5 2022-09-15 thomas size_t len = 1;
8547 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8548 fc2737d5 2022-09-15 thomas continue;
8550 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8551 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8552 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8554 fc2737d5 2022-09-15 thomas len += strlen(t);
8555 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8556 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8557 fc2737d5 2022-09-15 thomas free(t0);
8558 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8561 fc2737d5 2022-09-15 thomas return NULL;
8565 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8566 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8567 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8569 fc2737d5 2022-09-15 thomas static const struct got_error *
8570 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8572 fc2737d5 2022-09-15 thomas int n, len = width;
8574 fc2737d5 2022-09-15 thomas if (km->keys) {
8575 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8576 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8577 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8579 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8580 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8582 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8584 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8585 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8586 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8588 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8589 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8590 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8591 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8592 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8593 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8594 fc2737d5 2022-09-15 thomas if (n < 0) {
8595 fc2737d5 2022-09-15 thomas free(t0);
8596 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8598 fc2737d5 2022-09-15 thomas first = 0;
8599 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8600 fc2737d5 2022-09-15 thomas *off += n;
8602 fc2737d5 2022-09-15 thomas free(t0);
8604 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8605 fc2737d5 2022-09-15 thomas if (n < 0)
8606 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8607 fc2737d5 2022-09-15 thomas *off += n;
8609 fc2737d5 2022-09-15 thomas return NULL;
8612 fc2737d5 2022-09-15 thomas static const struct got_error *
8613 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
8615 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8616 fc2737d5 2022-09-15 thomas off_t off = 0;
8617 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
8618 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
8619 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
8620 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8621 fc2737d5 2022-09-15 thomas GENERATE_HELP
8622 fc2737d5 2022-09-15 thomas #undef KEYMAP_
8623 fc2737d5 2022-09-15 thomas #undef KEY_
8626 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8627 fc2737d5 2022-09-15 thomas if (err)
8628 fc2737d5 2022-09-15 thomas return err;
8630 fc2737d5 2022-09-15 thomas n = nitems(km);
8631 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
8632 fc2737d5 2022-09-15 thomas if (err)
8633 fc2737d5 2022-09-15 thomas return err;
8635 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
8636 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
8637 fc2737d5 2022-09-15 thomas show = s->all;
8638 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
8639 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
8640 fc2737d5 2022-09-15 thomas show = 1;
8642 fc2737d5 2022-09-15 thomas if (show) {
8643 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
8644 fc2737d5 2022-09-15 thomas if (err)
8645 fc2737d5 2022-09-15 thomas return err;
8646 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8647 fc2737d5 2022-09-15 thomas if (err)
8648 fc2737d5 2022-09-15 thomas return err;
8651 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
8653 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8654 fc2737d5 2022-09-15 thomas return err;
8657 fc2737d5 2022-09-15 thomas static const struct got_error *
8658 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
8660 fc2737d5 2022-09-15 thomas FILE *f;
8661 fc2737d5 2022-09-15 thomas const struct got_error *err;
8663 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8664 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8665 fc2737d5 2022-09-15 thomas s->nlines = 0;
8667 fc2737d5 2022-09-15 thomas f = got_opentemp();
8668 fc2737d5 2022-09-15 thomas if (f == NULL)
8669 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
8670 fc2737d5 2022-09-15 thomas s->f = f;
8672 fc2737d5 2022-09-15 thomas err = format_help(s);
8673 fc2737d5 2022-09-15 thomas if (err)
8674 fc2737d5 2022-09-15 thomas return err;
8676 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
8677 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
8679 fc2737d5 2022-09-15 thomas return NULL;
8682 fc2737d5 2022-09-15 thomas static const struct got_error *
8683 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
8685 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
8686 fc2737d5 2022-09-15 thomas return NULL;
8689 2a7d3cd7 2022-09-17 thomas static void
8690 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8691 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
8693 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
8695 2a7d3cd7 2022-09-17 thomas *f = s->f;
8696 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
8697 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
8698 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
8699 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
8700 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
8701 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
8704 fc2737d5 2022-09-15 thomas static const struct got_error *
8705 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
8707 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8708 fc2737d5 2022-09-15 thomas const struct got_error *err;
8709 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
8710 fc2737d5 2022-09-15 thomas wchar_t *wline;
8711 fc2737d5 2022-09-15 thomas char *line;
8712 fc2737d5 2022-09-15 thomas ssize_t linelen;
8713 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8714 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
8715 fc2737d5 2022-09-15 thomas int eos = view->nlines;
8717 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8718 fc2737d5 2022-09-15 thomas --eos; /* account for border */
8720 fc2737d5 2022-09-15 thomas s->lineno = 0;
8721 fc2737d5 2022-09-15 thomas rewind(s->f);
8722 fc2737d5 2022-09-15 thomas werase(view->window);
8724 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
8725 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
8727 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
8728 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
8729 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
8730 fc2737d5 2022-09-15 thomas if (err)
8731 fc2737d5 2022-09-15 thomas return err;
8732 fc2737d5 2022-09-15 thomas if (eos <= 1)
8733 fc2737d5 2022-09-15 thomas return NULL;
8734 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
8735 fc2737d5 2022-09-15 thomas eos -= 2;
8737 fc2737d5 2022-09-15 thomas s->eof = 0;
8738 fc2737d5 2022-09-15 thomas view->maxx = 0;
8739 fc2737d5 2022-09-15 thomas line = NULL;
8740 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
8741 fc2737d5 2022-09-15 thomas attr_t attr = 0;
8743 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8744 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8745 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
8746 fc2737d5 2022-09-15 thomas free(line);
8747 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
8749 fc2737d5 2022-09-15 thomas s->eof = 1;
8752 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
8753 fc2737d5 2022-09-15 thomas continue;
8754 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8755 fc2737d5 2022-09-15 thomas continue;
8756 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
8757 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
8759 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8760 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
8761 fc2737d5 2022-09-15 thomas if (err) {
8762 fc2737d5 2022-09-15 thomas free(line);
8763 fc2737d5 2022-09-15 thomas return err;
8765 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
8766 fc2737d5 2022-09-15 thomas free(wline);
8767 fc2737d5 2022-09-15 thomas wline = NULL;
8769 fc2737d5 2022-09-15 thomas if (attr)
8770 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
8771 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
8772 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8773 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
8774 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
8775 fc2737d5 2022-09-15 thomas if (err) {
8776 fc2737d5 2022-09-15 thomas free(line);
8777 fc2737d5 2022-09-15 thomas return err;
8779 fc2737d5 2022-09-15 thomas } else {
8780 fc2737d5 2022-09-15 thomas int skip;
8782 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
8783 fc2737d5 2022-09-15 thomas view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8784 fc2737d5 2022-09-15 thomas if (err) {
8785 fc2737d5 2022-09-15 thomas free(line);
8786 fc2737d5 2022-09-15 thomas return err;
8788 fc2737d5 2022-09-15 thomas rc = waddwstr(view->window, &wline[skip]);
8789 fc2737d5 2022-09-15 thomas free(wline);
8790 fc2737d5 2022-09-15 thomas wline = NULL;
8791 fc2737d5 2022-09-15 thomas if (rc == ERR)
8792 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "waddwstr");
8794 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
8795 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
8796 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
8797 fc2737d5 2022-09-15 thomas } else {
8798 fc2737d5 2022-09-15 thomas if (width <= view->ncols)
8799 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
8801 fc2737d5 2022-09-15 thomas if (attr)
8802 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
8803 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
8804 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
8806 fc2737d5 2022-09-15 thomas free(line);
8807 fc2737d5 2022-09-15 thomas if (nprinted > 0)
8808 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8810 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
8812 fc2737d5 2022-09-15 thomas view_border(view);
8814 fc2737d5 2022-09-15 thomas if (s->eof) {
8815 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
8816 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
8817 fc2737d5 2022-09-15 thomas view->ncols - 1);
8818 fc2737d5 2022-09-15 thomas if (rc == ERR)
8819 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8820 fc2737d5 2022-09-15 thomas } else {
8821 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
8822 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
8823 fc2737d5 2022-09-15 thomas wstandout(view->window);
8824 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
8825 fc2737d5 2022-09-15 thomas view->ncols - 1);
8826 fc2737d5 2022-09-15 thomas if (rc == ERR)
8827 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8828 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
8829 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
8830 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
8831 fc2737d5 2022-09-15 thomas if (rc == ERR)
8832 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
8834 fc2737d5 2022-09-15 thomas wstandend(view->window);
8837 fc2737d5 2022-09-15 thomas return NULL;
8840 fc2737d5 2022-09-15 thomas static const struct got_error *
8841 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8843 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8844 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8845 fc2737d5 2022-09-15 thomas char *line = NULL;
8846 fc2737d5 2022-09-15 thomas ssize_t linelen;
8847 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8848 fc2737d5 2022-09-15 thomas int eos, nscroll;
8850 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
8851 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8852 fc2737d5 2022-09-15 thomas --eos; /* border */
8854 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
8856 fc2737d5 2022-09-15 thomas switch (ch) {
8857 fc2737d5 2022-09-15 thomas case '0':
8858 fc2737d5 2022-09-15 thomas view->x = 0;
8860 fc2737d5 2022-09-15 thomas case '$':
8861 fc2737d5 2022-09-15 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
8862 fc2737d5 2022-09-15 thomas view->count = 0;
8864 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
8865 fc2737d5 2022-09-15 thomas case 'l':
8866 fc2737d5 2022-09-15 thomas if (view->x + view->ncols / 3 < view->maxx)
8867 fc2737d5 2022-09-15 thomas view->x += 2;
8869 fc2737d5 2022-09-15 thomas view->count = 0;
8871 fc2737d5 2022-09-15 thomas case KEY_LEFT:
8872 fc2737d5 2022-09-15 thomas case 'h':
8873 fc2737d5 2022-09-15 thomas view->x -= MIN(view->x, 2);
8874 fc2737d5 2022-09-15 thomas if (view->x <= 0)
8875 fc2737d5 2022-09-15 thomas view->count = 0;
8877 fc2737d5 2022-09-15 thomas case 'g':
8878 fc2737d5 2022-09-15 thomas case KEY_HOME:
8879 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8880 fc2737d5 2022-09-15 thomas view->count = 0;
8882 fc2737d5 2022-09-15 thomas case 'G':
8883 fc2737d5 2022-09-15 thomas case KEY_END:
8884 fc2737d5 2022-09-15 thomas view->count = 0;
8885 fc2737d5 2022-09-15 thomas if (s->eof)
8887 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
8888 fc2737d5 2022-09-15 thomas s->eof = 1;
8890 fc2737d5 2022-09-15 thomas case 'k':
8891 fc2737d5 2022-09-15 thomas case KEY_UP:
8892 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
8893 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
8895 fc2737d5 2022-09-15 thomas view->count = 0;
8897 fc2737d5 2022-09-15 thomas case CTRL('u'):
8898 fc2737d5 2022-09-15 thomas case 'u':
8899 fc2737d5 2022-09-15 thomas nscroll /= 2;
8900 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8901 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
8902 fc2737d5 2022-09-15 thomas case CTRL('b'):
8903 fc2737d5 2022-09-15 thomas case 'b':
8904 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
8905 fc2737d5 2022-09-15 thomas view->count = 0;
8908 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
8909 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
8911 fc2737d5 2022-09-15 thomas case 'j':
8912 fc2737d5 2022-09-15 thomas case KEY_DOWN:
8913 fc2737d5 2022-09-15 thomas case CTRL('n'):
8914 fc2737d5 2022-09-15 thomas if (!s->eof)
8915 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
8917 fc2737d5 2022-09-15 thomas view->count = 0;
8919 fc2737d5 2022-09-15 thomas case CTRL('d'):
8920 fc2737d5 2022-09-15 thomas case 'd':
8921 fc2737d5 2022-09-15 thomas nscroll /= 2;
8922 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8923 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
8924 fc2737d5 2022-09-15 thomas case CTRL('f'):
8925 fc2737d5 2022-09-15 thomas case 'f':
8926 fc2737d5 2022-09-15 thomas case ' ':
8927 fc2737d5 2022-09-15 thomas if (s->eof) {
8928 fc2737d5 2022-09-15 thomas view->count = 0;
8931 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
8932 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8933 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
8934 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8935 fc2737d5 2022-09-15 thomas if (feof(s->f))
8936 fc2737d5 2022-09-15 thomas s->eof = 1;
8938 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
8942 fc2737d5 2022-09-15 thomas free(line);
8944 fc2737d5 2022-09-15 thomas default:
8945 fc2737d5 2022-09-15 thomas view->count = 0;
8949 fc2737d5 2022-09-15 thomas return err;
8952 fc2737d5 2022-09-15 thomas static const struct got_error *
8953 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
8955 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8957 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8958 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8959 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
8960 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8962 fc2737d5 2022-09-15 thomas return NULL;
8965 fc2737d5 2022-09-15 thomas static const struct got_error *
8966 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
8968 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8971 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
8972 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8974 fc2737d5 2022-09-15 thomas wclear(view->window);
8975 fc2737d5 2022-09-15 thomas view->count = 0;
8976 fc2737d5 2022-09-15 thomas view->x = 0;
8977 fc2737d5 2022-09-15 thomas s->all = !s->all;
8978 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8979 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
8980 fc2737d5 2022-09-15 thomas s->matched_line = 0;
8982 fc2737d5 2022-09-15 thomas return create_help(s);
8985 fc2737d5 2022-09-15 thomas static const struct got_error *
8986 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
8988 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8989 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8991 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
8992 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8993 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
8994 fc2737d5 2022-09-15 thomas s->selected_line = 1;
8996 fc2737d5 2022-09-15 thomas view->show = show_help_view;
8997 fc2737d5 2022-09-15 thomas view->input = input_help_view;
8998 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
8999 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9000 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9001 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9002 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9004 fc2737d5 2022-09-15 thomas err = create_help(s);
9005 fc2737d5 2022-09-15 thomas return err;
9008 fc2737d5 2022-09-15 thomas static const struct got_error *
9009 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9010 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9012 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9014 2a31b33b 2022-07-23 thomas *new_view = NULL;
9016 2a31b33b 2022-07-23 thomas switch (request) {
9017 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9018 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9019 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9021 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9022 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9023 2a31b33b 2022-07-23 thomas view, s->repo);
9025 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9026 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9028 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9029 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9030 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9032 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9033 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9034 2a31b33b 2022-07-23 thomas s->repo);
9036 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9037 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9039 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9040 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9041 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9042 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9043 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9044 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9045 2a31b33b 2022-07-23 thomas &view->state.tree);
9046 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9047 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9048 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9049 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9051 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9052 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9054 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9055 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9056 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9057 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9058 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9059 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9060 2a31b33b 2022-07-23 thomas view->state.log.repo);
9061 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9062 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9063 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9064 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9066 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9067 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9069 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9070 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9071 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9072 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9073 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9074 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9075 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9076 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9078 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9079 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9080 2a31b33b 2022-07-23 thomas if (err)
9081 2a31b33b 2022-07-23 thomas view_close(*new_view);
9083 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9084 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9085 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9086 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9087 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9088 fc2737d5 2022-09-15 thomas if (err)
9089 fc2737d5 2022-09-15 thomas view_close(*new_view);
9091 2a31b33b 2022-07-23 thomas default:
9092 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9095 2a31b33b 2022-07-23 thomas return err;
9099 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9100 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9102 6458efa5 2020-11-24 stsp static void
9103 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9105 a5d43cac 2022-07-01 thomas switch (view->type) {
9106 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9107 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9108 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9109 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9113 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9114 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9116 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9117 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9120 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9121 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9122 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9124 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9125 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9126 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9128 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9129 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9130 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9132 a5d43cac 2022-07-01 thomas default:
9136 a5d43cac 2022-07-01 thomas view->offset = 0;
9140 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9141 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9143 a5d43cac 2022-07-01 thomas static const struct got_error *
9144 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9146 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9147 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9148 a5d43cac 2022-07-01 thomas int *selected = NULL;
9149 a5d43cac 2022-07-01 thomas int header, offset;
9151 a5d43cac 2022-07-01 thomas switch (view->type) {
9152 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9153 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9154 a5d43cac 2022-07-01 thomas header = 3;
9155 a5d43cac 2022-07-01 thomas scrolld = NULL;
9156 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9157 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9158 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9159 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9160 a5d43cac 2022-07-01 thomas view->offset = offset;
9164 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9165 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9166 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9167 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9168 a5d43cac 2022-07-01 thomas selected = &s->selected;
9171 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9172 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9173 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9174 a5d43cac 2022-07-01 thomas header = 3;
9175 a5d43cac 2022-07-01 thomas selected = &s->selected;
9178 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9179 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9180 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9181 a5d43cac 2022-07-01 thomas header = 5;
9182 a5d43cac 2022-07-01 thomas selected = &s->selected;
9185 a5d43cac 2022-07-01 thomas default:
9186 a5d43cac 2022-07-01 thomas selected = NULL;
9187 a5d43cac 2022-07-01 thomas scrolld = NULL;
9188 a5d43cac 2022-07-01 thomas header = 0;
9192 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9193 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9194 a5d43cac 2022-07-01 thomas view->offset = offset;
9195 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9196 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9197 a5d43cac 2022-07-01 thomas *selected -= offset;
9201 a5d43cac 2022-07-01 thomas return err;
9204 a5d43cac 2022-07-01 thomas static void
9205 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9209 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9210 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9211 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9212 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9214 6879ba42 2020-10-01 naddy fputc('\n', fp);
9217 4ed7e80c 2018-05-20 stsp __dead static void
9218 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9220 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9222 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9223 6879ba42 2020-10-01 naddy getprogname());
9224 6879ba42 2020-10-01 naddy if (hflag) {
9225 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9226 6879ba42 2020-10-01 naddy list_commands(fp);
9228 6879ba42 2020-10-01 naddy exit(status);
9231 c2301be8 2018-04-30 stsp static char **
9232 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9234 ee85c5e8 2020-02-29 stsp va_list ap;
9235 c2301be8 2018-04-30 stsp char **argv;
9238 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9240 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9241 c2301be8 2018-04-30 stsp if (argv == NULL)
9242 c2301be8 2018-04-30 stsp err(1, "calloc");
9243 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9244 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9245 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9246 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9249 ee85c5e8 2020-02-29 stsp va_end(ap);
9250 c2301be8 2018-04-30 stsp return argv;
9254 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9255 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9256 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9257 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9258 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9260 ee85c5e8 2020-02-29 stsp static const struct got_error *
9261 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9263 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9264 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9265 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9266 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9267 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9268 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9269 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9270 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9271 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9273 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9274 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9275 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9277 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9278 7cd52833 2022-06-23 thomas if (error != NULL)
9279 7cd52833 2022-06-23 thomas goto done;
9281 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9282 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9283 ee85c5e8 2020-02-29 stsp goto done;
9285 ee85c5e8 2020-02-29 stsp if (worktree)
9286 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9288 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9289 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9290 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9291 ee85c5e8 2020-02-29 stsp goto done;
9294 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9295 ee85c5e8 2020-02-29 stsp if (error != NULL)
9296 ee85c5e8 2020-02-29 stsp goto done;
9298 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9299 ee85c5e8 2020-02-29 stsp repo, worktree);
9300 ee85c5e8 2020-02-29 stsp if (error)
9301 ee85c5e8 2020-02-29 stsp goto done;
9303 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9304 84de9106 2020-12-26 stsp if (error)
9305 84de9106 2020-12-26 stsp goto done;
9306 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9307 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9308 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9309 ee85c5e8 2020-02-29 stsp if (error)
9310 ee85c5e8 2020-02-29 stsp goto done;
9312 ee85c5e8 2020-02-29 stsp if (worktree) {
9313 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9314 ee85c5e8 2020-02-29 stsp worktree = NULL;
9317 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9318 945f9229 2022-04-16 thomas if (error)
9319 945f9229 2022-04-16 thomas goto done;
9321 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9322 ee85c5e8 2020-02-29 stsp if (error) {
9323 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9324 ee85c5e8 2020-02-29 stsp goto done;
9325 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9326 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9327 6879ba42 2020-10-01 naddy usage(1, 1);
9328 ee85c5e8 2020-02-29 stsp /* not reached */
9331 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9332 ee85c5e8 2020-02-29 stsp if (error)
9333 ee85c5e8 2020-02-29 stsp goto done;
9335 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9337 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9338 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9340 1d0f4054 2021-06-17 stsp if (repo) {
9341 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9342 1d0f4054 2021-06-17 stsp if (error == NULL)
9343 1d0f4054 2021-06-17 stsp error = close_err;
9345 945f9229 2022-04-16 thomas if (commit)
9346 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9347 ee85c5e8 2020-02-29 stsp if (worktree)
9348 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9349 7cd52833 2022-06-23 thomas if (pack_fds) {
9350 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9351 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9352 7cd52833 2022-06-23 thomas if (error == NULL)
9353 7cd52833 2022-06-23 thomas error = pack_err;
9356 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9357 ee85c5e8 2020-02-29 stsp free(commit_id);
9358 ee85c5e8 2020-02-29 stsp free(cwd);
9359 ee85c5e8 2020-02-29 stsp free(repo_path);
9360 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9361 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9363 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9364 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9365 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9367 87670572 2020-12-26 naddy tog_free_refs();
9368 ee85c5e8 2020-02-29 stsp return error;
9372 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9374 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9375 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9376 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9377 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9378 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9379 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9380 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9382 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9384 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9385 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9387 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9389 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9390 9f7d7167 2018-04-29 stsp switch (ch) {
9392 9f7d7167 2018-04-29 stsp hflag = 1;
9395 53ccebc2 2019-07-30 stsp Vflag = 1;
9398 6879ba42 2020-10-01 naddy usage(hflag, 1);
9399 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9403 9f7d7167 2018-04-29 stsp argc -= optind;
9404 9f7d7167 2018-04-29 stsp argv += optind;
9405 9814e6a3 2020-09-27 naddy optind = 1;
9406 c2301be8 2018-04-30 stsp optreset = 1;
9408 53ccebc2 2019-07-30 stsp if (Vflag) {
9409 53ccebc2 2019-07-30 stsp got_version_print_str();
9410 6879ba42 2020-10-01 naddy return 0;
9413 4010e238 2020-12-04 stsp #ifndef PROFILE
9414 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9415 4010e238 2020-12-04 stsp NULL) == -1)
9416 4010e238 2020-12-04 stsp err(1, "pledge");
9419 c2301be8 2018-04-30 stsp if (argc == 0) {
9420 f29d3e89 2018-06-23 stsp if (hflag)
9421 6879ba42 2020-10-01 naddy usage(hflag, 0);
9422 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9423 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9425 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9429 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9430 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9431 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9432 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9433 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9439 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9440 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9441 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9442 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9443 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9444 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9447 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9448 ee85c5e8 2020-02-29 stsp if (argc != 1)
9449 6879ba42 2020-10-01 naddy usage(0, 1);
9450 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9451 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9453 ee85c5e8 2020-02-29 stsp if (hflag)
9454 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9456 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9460 b46c1e04 2020-09-20 naddy putchar('\n');
9461 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9463 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9464 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9465 a2f4a359 2020-02-28 stsp free(cmd_argv);
9468 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9469 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9470 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9471 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9472 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9473 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);