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 1a080567 2023-01-14 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 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4469 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4471 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4473 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4474 fe621944 2020-11-10 stsp if (p == NULL)
4475 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4476 82c78e96 2022-08-06 thomas *lines = p;
4477 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4478 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4479 fe621944 2020-11-10 stsp (*nlines)++;
4481 fe621944 2020-11-10 stsp return NULL;
4484 fe621944 2020-11-10 stsp static const struct got_error *
4485 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4486 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
4488 be97ab03 2023-01-19 thomas struct got_diff_line *p;
4489 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
4490 be97ab03 2023-01-19 thomas size_t i, r;
4492 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
4493 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
4495 be97ab03 2023-01-19 thomas for (;;) {
4496 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
4497 be97ab03 2023-01-19 thomas if (r == 0) {
4498 be97ab03 2023-01-19 thomas if (ferror(src))
4499 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
4500 be97ab03 2023-01-19 thomas if (feof(src))
4503 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
4504 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
4508 be97ab03 2023-01-19 thomas * The diff driver initialises the first line at offset zero when the
4509 be97ab03 2023-01-19 thomas * array isn't prepopulated, skip it; we already have it in *d_lines.
4511 be97ab03 2023-01-19 thomas for (i = 1; i < s_nlines; ++i)
4512 be97ab03 2023-01-19 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4514 be97ab03 2023-01-19 thomas --s_nlines;
4516 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4517 be97ab03 2023-01-19 thomas if (p == NULL) {
4518 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
4519 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
4522 be97ab03 2023-01-19 thomas *d_lines = p;
4524 be97ab03 2023-01-19 thomas memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4525 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
4527 be97ab03 2023-01-19 thomas return NULL;
4530 be97ab03 2023-01-19 thomas static const struct got_error *
4531 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4532 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4533 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4534 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
4536 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4537 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4538 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4539 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4540 45d799e2 2018-12-23 stsp time_t committer_time;
4541 45d799e2 2018-12-23 stsp const char *author, *committer;
4542 8b473291 2019-02-21 stsp char *refs_str = NULL;
4543 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4544 fe621944 2020-11-10 stsp off_t outoff = 0;
4547 8b473291 2019-02-21 stsp if (refs) {
4548 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4550 8b473291 2019-02-21 stsp return err;
4553 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4555 abd2672a 2018-12-23 stsp return err;
4557 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4558 15a94983 2018-12-23 stsp if (err) {
4559 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4560 15a94983 2018-12-23 stsp goto done;
4563 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4565 fe621944 2020-11-10 stsp goto done;
4567 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4568 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4569 fe621944 2020-11-10 stsp if (n < 0) {
4570 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4571 abd2672a 2018-12-23 stsp goto done;
4573 fe621944 2020-11-10 stsp outoff += n;
4574 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4576 fe621944 2020-11-10 stsp goto done;
4578 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4579 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4580 fe621944 2020-11-10 stsp if (n < 0) {
4581 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4582 abd2672a 2018-12-23 stsp goto done;
4584 fe621944 2020-11-10 stsp outoff += n;
4585 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4587 fe621944 2020-11-10 stsp goto done;
4589 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
4590 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
4591 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
4592 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
4593 fe621944 2020-11-10 stsp if (n < 0) {
4594 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4595 fe621944 2020-11-10 stsp goto done;
4597 fe621944 2020-11-10 stsp outoff += n;
4598 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4599 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
4601 fe621944 2020-11-10 stsp goto done;
4603 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
4604 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
4605 48830929 2023-01-07 thomas if (datestr) {
4606 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
4607 fe621944 2020-11-10 stsp if (n < 0) {
4608 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4609 fe621944 2020-11-10 stsp goto done;
4611 fe621944 2020-11-10 stsp outoff += n;
4612 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4613 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
4615 fe621944 2020-11-10 stsp goto done;
4617 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4618 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4619 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4620 ac4dc263 2021-09-24 thomas int pn = 1;
4621 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4622 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4623 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4624 ac4dc263 2021-09-24 thomas if (err)
4625 ac4dc263 2021-09-24 thomas goto done;
4626 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4627 ac4dc263 2021-09-24 thomas if (n < 0) {
4628 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4629 ac4dc263 2021-09-24 thomas goto done;
4631 ac4dc263 2021-09-24 thomas outoff += n;
4632 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4633 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4634 ac4dc263 2021-09-24 thomas if (err)
4635 ac4dc263 2021-09-24 thomas goto done;
4636 ac4dc263 2021-09-24 thomas free(id_str);
4637 ac4dc263 2021-09-24 thomas id_str = NULL;
4641 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4643 5943eee2 2019-08-13 stsp goto done;
4644 fe621944 2020-11-10 stsp s = logmsg;
4645 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4646 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4647 fe621944 2020-11-10 stsp if (n < 0) {
4648 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4649 fe621944 2020-11-10 stsp goto done;
4651 fe621944 2020-11-10 stsp outoff += n;
4652 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4653 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4655 fe621944 2020-11-10 stsp goto done;
4658 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
4659 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4660 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
4662 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4663 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4664 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
4665 fe621944 2020-11-10 stsp if (n < 0) {
4666 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4667 fe621944 2020-11-10 stsp goto done;
4669 fe621944 2020-11-10 stsp outoff += n;
4670 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4671 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4673 fe621944 2020-11-10 stsp goto done;
4676 0208f208 2020-05-05 stsp fputc('\n', outfile);
4678 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4679 772fcad5 2023-01-07 thomas if (err)
4680 772fcad5 2023-01-07 thomas goto done;
4682 772fcad5 2023-01-07 thomas n = fprintf(outfile,
4683 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4684 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4685 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4686 772fcad5 2023-01-07 thomas if (n < 0) {
4687 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
4688 772fcad5 2023-01-07 thomas goto done;
4690 772fcad5 2023-01-07 thomas outoff += n;
4691 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4692 772fcad5 2023-01-07 thomas if (err)
4693 772fcad5 2023-01-07 thomas goto done;
4695 772fcad5 2023-01-07 thomas fputc('\n', outfile);
4696 772fcad5 2023-01-07 thomas outoff++;
4697 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4699 abd2672a 2018-12-23 stsp free(id_str);
4700 5943eee2 2019-08-13 stsp free(logmsg);
4701 8b473291 2019-02-21 stsp free(refs_str);
4702 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4703 7510f233 2020-08-09 stsp if (err) {
4704 82c78e96 2022-08-06 thomas free(*lines);
4705 82c78e96 2022-08-06 thomas *lines = NULL;
4706 ae6a6978 2020-08-09 stsp *nlines = 0;
4708 fe621944 2020-11-10 stsp return err;
4711 abd2672a 2018-12-23 stsp static const struct got_error *
4712 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4714 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4715 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
4716 15a94983 2018-12-23 stsp int obj_type;
4717 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
4718 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
4720 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
4722 82c78e96 2022-08-06 thomas free(s->lines);
4723 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4724 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4725 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4726 fe621944 2020-11-10 stsp s->nlines = 0;
4728 511a516b 2018-05-19 stsp f = got_opentemp();
4729 48ae06ee 2018-10-18 stsp if (f == NULL) {
4730 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4731 48ae06ee 2018-10-18 stsp goto done;
4733 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
4734 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
4735 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
4736 be97ab03 2023-01-19 thomas goto done;
4738 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4739 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4740 fb43ecf1 2019-02-11 stsp goto done;
4744 15a94983 2018-12-23 stsp if (s->id1)
4745 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4747 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4749 15a94983 2018-12-23 stsp goto done;
4751 15a94983 2018-12-23 stsp switch (obj_type) {
4752 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4753 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4754 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4755 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4756 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4759 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4760 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4761 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4762 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4763 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
4765 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4766 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4767 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4768 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4769 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4770 be97ab03 2023-01-19 thomas size_t nlines = 0;
4771 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
4772 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
4773 be97ab03 2023-01-19 thomas &changed_paths,
4774 be97ab03 2023-01-19 thomas s->ignore_whitespace,
4775 be97ab03 2023-01-19 thomas s->force_text_diff,
4776 be97ab03 2023-01-19 thomas tog_diff_algo
4779 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
4780 be97ab03 2023-01-19 thomas if (lines == NULL) {
4781 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
4782 be97ab03 2023-01-19 thomas goto done;
4785 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
4786 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
4787 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4788 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4789 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4790 be97ab03 2023-01-19 thomas if (err)
4793 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4795 3ffacbe1 2020-02-02 tracey goto done;
4796 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4797 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4798 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4799 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4800 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
4801 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
4802 f44b1f58 2020-02-02 tracey if (err)
4803 f44b1f58 2020-02-02 tracey goto done;
4804 f44b1f58 2020-02-02 tracey } else {
4805 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4806 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4807 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4808 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4809 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4810 772fcad5 2023-01-07 thomas s->ignore_whitespace,
4811 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
4812 f44b1f58 2020-02-02 tracey if (err)
4813 f44b1f58 2020-02-02 tracey goto done;
4818 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4820 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4821 be97ab03 2023-01-19 thomas lines, nlines);
4825 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4829 be97ab03 2023-01-19 thomas free(lines);
4830 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4831 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4832 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4833 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4834 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
4835 48ae06ee 2018-10-18 stsp return err;
4838 f5215bb9 2019-02-22 stsp static void
4839 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4841 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4842 f5215bb9 2019-02-22 stsp update_panels();
4843 f5215bb9 2019-02-22 stsp doupdate();
4846 f44b1f58 2020-02-02 tracey static const struct got_error *
4847 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4849 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4851 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4852 f44b1f58 2020-02-02 tracey return NULL;
4855 2a7d3cd7 2022-09-17 thomas static void
4856 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4857 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
4859 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
4861 2a7d3cd7 2022-09-17 thomas *f = s->f;
4862 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
4863 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
4864 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
4865 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
4866 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
4867 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
4870 fc2737d5 2022-09-15 thomas static const struct got_error *
4871 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
4873 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4874 fc2737d5 2022-09-15 thomas FILE *f;
4875 f44b1f58 2020-02-02 tracey int lineno;
4876 78eecffc 2022-06-23 thomas char *line = NULL;
4877 826082fe 2020-12-10 stsp size_t linesize = 0;
4878 826082fe 2020-12-10 stsp ssize_t linelen;
4879 fc2737d5 2022-09-15 thomas off_t *line_offsets;
4880 fc2737d5 2022-09-15 thomas size_t nlines = 0;
4881 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
4883 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
4884 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
4885 2a7d3cd7 2022-09-17 thomas "view search not supported");
4886 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4887 fc2737d5 2022-09-15 thomas &match, &selected);
4889 f44b1f58 2020-02-02 tracey if (!view->searching) {
4890 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4891 f44b1f58 2020-02-02 tracey return NULL;
4894 fc2737d5 2022-09-15 thomas if (*match) {
4895 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4896 fc2737d5 2022-09-15 thomas lineno = *match + 1;
4898 fc2737d5 2022-09-15 thomas lineno = *match - 1;
4900 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
4902 f44b1f58 2020-02-02 tracey while (1) {
4903 f44b1f58 2020-02-02 tracey off_t offset;
4905 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
4906 fc2737d5 2022-09-15 thomas if (*match == 0) {
4907 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4911 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4912 f44b1f58 2020-02-02 tracey lineno = 1;
4914 fc2737d5 2022-09-15 thomas lineno = nlines;
4917 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
4918 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
4919 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
4920 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
4921 f44b1f58 2020-02-02 tracey free(line);
4922 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4924 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
4925 78eecffc 2022-06-23 thomas if (linelen != -1) {
4926 78eecffc 2022-06-23 thomas char *exstr;
4927 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4928 78eecffc 2022-06-23 thomas if (err)
4930 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4931 78eecffc 2022-06-23 thomas &view->regmatch)) {
4932 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4933 fc2737d5 2022-09-15 thomas *match = lineno;
4934 78eecffc 2022-06-23 thomas free(exstr);
4937 78eecffc 2022-06-23 thomas free(exstr);
4939 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4940 f44b1f58 2020-02-02 tracey lineno++;
4942 f44b1f58 2020-02-02 tracey lineno--;
4944 826082fe 2020-12-10 stsp free(line);
4946 fc2737d5 2022-09-15 thomas if (*match) {
4947 fc2737d5 2022-09-15 thomas *first = *match;
4948 fc2737d5 2022-09-15 thomas *selected = 1;
4951 05171be4 2022-06-23 thomas return err;
4954 48ae06ee 2018-10-18 stsp static const struct got_error *
4955 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4957 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4958 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4960 a0f32f33 2022-06-13 thomas free(s->id1);
4961 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4962 a0f32f33 2022-06-13 thomas free(s->id2);
4963 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4964 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4965 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4966 a0f32f33 2022-06-13 thomas s->f = NULL;
4967 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4968 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4969 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4970 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4971 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4972 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4973 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4974 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4975 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4976 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4977 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4978 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4979 82c78e96 2022-08-06 thomas free(s->lines);
4980 82c78e96 2022-08-06 thomas s->lines = NULL;
4981 a0f32f33 2022-06-13 thomas s->nlines = 0;
4982 a0f32f33 2022-06-13 thomas return err;
4985 a0f32f33 2022-06-13 thomas static const struct got_error *
4986 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4987 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4988 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4989 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4991 48ae06ee 2018-10-18 stsp const struct got_error *err;
4992 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4994 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4995 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4996 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4998 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4999 15a94983 2018-12-23 stsp int type1, type2;
5000 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5002 15a94983 2018-12-23 stsp return err;
5003 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5005 15a94983 2018-12-23 stsp return err;
5007 15a94983 2018-12-23 stsp if (type1 != type2)
5008 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5010 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5011 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5012 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5013 f44b1f58 2020-02-02 tracey s->repo = repo;
5014 f44b1f58 2020-02-02 tracey s->id1 = id1;
5015 f44b1f58 2020-02-02 tracey s->id2 = id2;
5016 3dbaef42 2020-11-24 stsp s->label1 = label1;
5017 3dbaef42 2020-11-24 stsp s->label2 = label2;
5019 15a94983 2018-12-23 stsp if (id1) {
5020 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5021 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5022 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5024 5465d566 2020-02-01 tracey s->id1 = NULL;
5026 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5027 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5028 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5029 a0f32f33 2022-06-13 thomas goto done;
5032 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5033 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5034 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5035 9a1d7435 2022-06-23 thomas goto done;
5038 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5039 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5040 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5041 a0f32f33 2022-06-13 thomas goto done;
5044 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5045 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5046 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5047 19a6a6b5 2022-07-01 thomas goto done;
5050 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5051 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5052 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5053 19a6a6b5 2022-07-01 thomas goto done;
5056 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5057 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5058 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5059 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5060 5465d566 2020-02-01 tracey s->repo = repo;
5062 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5065 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5066 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5067 6568f0aa 2022-08-06 thomas if (rc != ERR)
5068 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5069 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5070 6568f0aa 2022-08-06 thomas if (rc != ERR)
5071 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5072 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5073 6568f0aa 2022-08-06 thomas if (rc != ERR)
5074 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5075 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5076 6568f0aa 2022-08-06 thomas if (rc != ERR)
5077 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5078 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5079 6568f0aa 2022-08-06 thomas if (rc != ERR)
5080 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5081 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5082 6568f0aa 2022-08-06 thomas if (rc != ERR)
5083 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5084 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5085 6568f0aa 2022-08-06 thomas if (rc != ERR)
5086 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5087 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5088 6568f0aa 2022-08-06 thomas if (rc != ERR)
5089 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5090 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5091 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5092 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5093 a0f32f33 2022-06-13 thomas goto done;
5097 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5098 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5099 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5100 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5102 5465d566 2020-02-01 tracey err = create_diff(s);
5104 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5105 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5106 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5107 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5108 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5109 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5110 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5112 a0f32f33 2022-06-13 thomas if (err)
5113 a0f32f33 2022-06-13 thomas close_diff_view(view);
5114 e5a0f69f 2018-08-18 stsp return err;
5117 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5118 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5120 a3404814 2018-09-02 stsp const struct got_error *err;
5121 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5122 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5123 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5125 a3404814 2018-09-02 stsp if (s->id1) {
5126 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5128 a3404814 2018-09-02 stsp return err;
5129 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5131 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5133 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5135 a3404814 2018-09-02 stsp return err;
5136 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5138 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5139 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5140 a3404814 2018-09-02 stsp free(id_str1);
5141 a3404814 2018-09-02 stsp free(id_str2);
5142 a3404814 2018-09-02 stsp return err;
5144 a3404814 2018-09-02 stsp free(id_str1);
5145 a3404814 2018-09-02 stsp free(id_str2);
5147 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5148 267bb3b8 2021-08-01 stsp free(header);
5149 267bb3b8 2021-08-01 stsp return err;
5152 15a087fe 2019-02-21 stsp static const struct got_error *
5153 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5154 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5156 d7a04538 2019-02-21 stsp const struct got_error *err;
5157 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5158 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5159 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5161 15a087fe 2019-02-21 stsp free(s->id2);
5162 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5163 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5164 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5166 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5168 d7a04538 2019-02-21 stsp return err;
5169 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5170 15a087fe 2019-02-21 stsp free(s->id1);
5171 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5172 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5173 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5174 15a087fe 2019-02-21 stsp return NULL;
5177 0cf4efb1 2018-09-29 stsp static const struct got_error *
5178 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5180 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5182 adf4c9e0 2022-07-03 thomas view->count = 0;
5183 adf4c9e0 2022-07-03 thomas wclear(view->window);
5184 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5185 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5186 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5187 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5188 adf4c9e0 2022-07-03 thomas return create_diff(s);
5191 82c78e96 2022-08-06 thomas static void
5192 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5194 82c78e96 2022-08-06 thomas int start, i;
5196 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5198 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5199 82c78e96 2022-08-06 thomas if (i == 0)
5200 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5201 82c78e96 2022-08-06 thomas if (--i == start)
5202 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5205 82c78e96 2022-08-06 thomas s->selected_line = 1;
5206 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5209 82c78e96 2022-08-06 thomas static void
5210 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5212 82c78e96 2022-08-06 thomas int start, i;
5214 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5216 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5217 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5219 82c78e96 2022-08-06 thomas if (++i == start)
5220 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5223 82c78e96 2022-08-06 thomas s->selected_line = 1;
5224 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5227 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5228 4fc71f3b 2022-07-12 thomas int, int, int);
5229 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5230 4fc71f3b 2022-07-12 thomas int, int);
5232 adf4c9e0 2022-07-03 thomas static const struct got_error *
5233 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5235 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5236 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5237 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5238 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5239 826082fe 2020-12-10 stsp char *line = NULL;
5240 826082fe 2020-12-10 stsp size_t linesize = 0;
5241 826082fe 2020-12-10 stsp ssize_t linelen;
5242 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5244 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5246 e5a0f69f 2018-08-18 stsp switch (ch) {
5247 05171be4 2022-06-23 thomas case '0':
5248 05171be4 2022-06-23 thomas view->x = 0;
5250 05171be4 2022-06-23 thomas case '$':
5251 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5252 07b0611c 2022-06-23 thomas view->count = 0;
5254 05171be4 2022-06-23 thomas case KEY_RIGHT:
5255 05171be4 2022-06-23 thomas case 'l':
5256 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5257 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5259 07b0611c 2022-06-23 thomas view->count = 0;
5261 05171be4 2022-06-23 thomas case KEY_LEFT:
5262 05171be4 2022-06-23 thomas case 'h':
5263 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5264 07b0611c 2022-06-23 thomas if (view->x <= 0)
5265 07b0611c 2022-06-23 thomas view->count = 0;
5269 3dbaef42 2020-11-24 stsp if (ch == 'a')
5270 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5271 772fcad5 2023-01-07 thomas else if (ch == 'w')
5272 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5273 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5276 912a3f79 2021-08-30 j case KEY_HOME:
5277 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5278 07b0611c 2022-06-23 thomas view->count = 0;
5281 912a3f79 2021-08-30 j case KEY_END:
5282 07b0611c 2022-06-23 thomas view->count = 0;
5286 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5290 1e37a5c2 2019-05-12 jcs case KEY_UP:
5291 f7140bf5 2021-10-17 thomas case CTRL('p'):
5292 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5293 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5295 07b0611c 2022-06-23 thomas view->count = 0;
5297 70f17a53 2022-06-13 thomas case CTRL('u'):
5298 23427b14 2022-06-23 thomas case 'u':
5299 70f17a53 2022-06-13 thomas nscroll /= 2;
5300 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5301 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5302 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5303 1c5e5faa 2022-06-23 thomas case 'b':
5304 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5305 07b0611c 2022-06-23 thomas view->count = 0;
5309 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5310 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5313 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5314 f7140bf5 2021-10-17 thomas case CTRL('n'):
5315 1e37a5c2 2019-05-12 jcs if (!s->eof)
5316 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5318 07b0611c 2022-06-23 thomas view->count = 0;
5320 70f17a53 2022-06-13 thomas case CTRL('d'):
5321 23427b14 2022-06-23 thomas case 'd':
5322 70f17a53 2022-06-13 thomas nscroll /= 2;
5323 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5324 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5325 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5326 1c5e5faa 2022-06-23 thomas case 'f':
5328 07b0611c 2022-06-23 thomas if (s->eof) {
5329 07b0611c 2022-06-23 thomas view->count = 0;
5333 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5334 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5335 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5336 826082fe 2020-12-10 stsp if (linelen == -1) {
5337 826082fe 2020-12-10 stsp if (feof(s->f)) {
5338 826082fe 2020-12-10 stsp s->eof = 1;
5340 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5344 826082fe 2020-12-10 stsp free(line);
5346 82c78e96 2022-08-06 thomas case '(':
5347 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5349 82c78e96 2022-08-06 thomas case ')':
5350 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5352 82c78e96 2022-08-06 thomas case '{':
5353 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5355 82c78e96 2022-08-06 thomas case '}':
5356 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5359 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5360 1e37a5c2 2019-05-12 jcs s->diff_context--;
5361 aa61903a 2021-12-31 thomas s->matched_line = 0;
5362 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5363 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5364 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5365 27829c9e 2020-11-21 stsp s->nlines) {
5366 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5367 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5370 07b0611c 2022-06-23 thomas view->count = 0;
5373 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
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);
5379 07b0611c 2022-06-23 thomas view->count = 0;
5383 777aae21 2022-07-20 thomas case 'K':
5385 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5386 4fc71f3b 2022-07-12 thomas case '>':
5387 4fc71f3b 2022-07-12 thomas case '.':
5388 777aae21 2022-07-20 thomas case 'J':
5389 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5390 07b0611c 2022-06-23 thomas view->count = 0;
5393 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5395 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5396 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5397 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5399 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5400 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5401 4fc71f3b 2022-07-12 thomas if (err)
5403 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5405 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5408 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5409 4fc71f3b 2022-07-12 thomas if (err)
5411 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5412 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5413 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5415 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5416 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5417 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5419 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5420 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5421 4fc71f3b 2022-07-12 thomas if (err)
5423 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5425 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5427 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5428 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5429 4fc71f3b 2022-07-12 thomas bs->selected_line);
5430 4fc71f3b 2022-07-12 thomas if (id == NULL)
5433 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5436 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5437 4fc71f3b 2022-07-12 thomas if (err)
5440 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5441 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5442 aa61903a 2021-12-31 thomas s->matched_line = 0;
5443 05171be4 2022-06-23 thomas view->x = 0;
5445 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5446 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5449 07b0611c 2022-06-23 thomas view->count = 0;
5453 bcbd79e2 2018-08-19 stsp return err;
5456 4ed7e80c 2018-05-20 stsp static const struct got_error *
5457 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5459 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5460 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5461 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5462 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5463 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5464 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5465 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5466 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5467 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5468 3dbaef42 2020-11-24 stsp const char *errstr;
5469 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5470 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5472 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5473 26ed57b2 2018-05-19 stsp switch (ch) {
5475 64453f7e 2020-11-21 stsp force_text_diff = 1;
5478 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5480 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5481 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5482 8f666e67 2022-02-12 thomas errstr, errstr);
5484 09b5bff8 2020-02-23 naddy case 'r':
5485 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5486 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5487 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5489 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5492 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5495 17020d27 2019-03-07 stsp usage_diff();
5496 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5500 26ed57b2 2018-05-19 stsp argc -= optind;
5501 26ed57b2 2018-05-19 stsp argv += optind;
5503 26ed57b2 2018-05-19 stsp if (argc == 0) {
5504 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5505 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5506 15a94983 2018-12-23 stsp id_str1 = argv[0];
5507 15a94983 2018-12-23 stsp id_str2 = argv[1];
5509 26ed57b2 2018-05-19 stsp usage_diff();
5511 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5512 7cd52833 2022-06-23 thomas if (error)
5513 7cd52833 2022-06-23 thomas goto done;
5515 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5516 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5517 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5518 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5519 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5520 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5521 c156c7a4 2020-12-18 stsp goto done;
5522 a273ac94 2020-02-23 naddy if (worktree)
5523 a273ac94 2020-02-23 naddy repo_path =
5524 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5526 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5527 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5528 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5529 c156c7a4 2020-12-18 stsp goto done;
5533 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5534 eb6600df 2019-01-04 stsp if (error)
5535 eb6600df 2019-01-04 stsp goto done;
5537 a273ac94 2020-02-23 naddy init_curses();
5539 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5540 51a10b52 2020-12-26 stsp if (error)
5541 51a10b52 2020-12-26 stsp goto done;
5543 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5544 26ed57b2 2018-05-19 stsp if (error)
5545 26ed57b2 2018-05-19 stsp goto done;
5547 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5548 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5549 26ed57b2 2018-05-19 stsp if (error)
5550 26ed57b2 2018-05-19 stsp goto done;
5552 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5553 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5554 26ed57b2 2018-05-19 stsp if (error)
5555 26ed57b2 2018-05-19 stsp goto done;
5557 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5558 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5559 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5560 ea5e7bb5 2018-08-01 stsp goto done;
5562 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5563 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5564 5dc9f4bc 2018-08-04 stsp if (error)
5565 5dc9f4bc 2018-08-04 stsp goto done;
5566 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5568 3dbaef42 2020-11-24 stsp free(label1);
5569 3dbaef42 2020-11-24 stsp free(label2);
5570 c02c541e 2019-03-29 stsp free(repo_path);
5571 a273ac94 2020-02-23 naddy free(cwd);
5572 1d0f4054 2021-06-17 stsp if (repo) {
5573 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5574 1d0f4054 2021-06-17 stsp if (error == NULL)
5575 1d0f4054 2021-06-17 stsp error = close_err;
5577 a273ac94 2020-02-23 naddy if (worktree)
5578 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5579 7cd52833 2022-06-23 thomas if (pack_fds) {
5580 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5581 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5582 7cd52833 2022-06-23 thomas if (error == NULL)
5583 7cd52833 2022-06-23 thomas error = pack_err;
5585 51a10b52 2020-12-26 stsp tog_free_refs();
5586 26ed57b2 2018-05-19 stsp return error;
5589 4ed7e80c 2018-05-20 stsp __dead static void
5590 9f7d7167 2018-04-29 stsp usage_blame(void)
5593 91198554 2022-06-23 thomas fprintf(stderr,
5594 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5595 9f7d7167 2018-04-29 stsp getprogname());
5599 84451b3e 2018-07-10 stsp struct tog_blame_line {
5600 84451b3e 2018-07-10 stsp int annotated;
5601 84451b3e 2018-07-10 stsp struct got_object_id *id;
5604 4ed7e80c 2018-05-20 stsp static const struct got_error *
5605 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5607 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5608 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5609 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5610 84451b3e 2018-07-10 stsp const struct got_error *err;
5611 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5612 826082fe 2020-12-10 stsp char *line = NULL;
5613 826082fe 2020-12-10 stsp size_t linesize = 0;
5614 826082fe 2020-12-10 stsp ssize_t linelen;
5615 84451b3e 2018-07-10 stsp wchar_t *wline;
5616 27a741e5 2019-09-11 stsp int width;
5617 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5618 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5619 ab089a2a 2018-07-12 stsp char *id_str;
5620 11b20872 2019-11-08 stsp struct tog_color *tc;
5622 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5624 ab089a2a 2018-07-12 stsp return err;
5626 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5627 f7d12f7e 2018-08-01 stsp werase(view->window);
5629 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5630 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5631 ab089a2a 2018-07-12 stsp free(id_str);
5632 ab089a2a 2018-07-12 stsp return err;
5635 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5636 ab089a2a 2018-07-12 stsp free(line);
5637 2550e4c3 2018-07-13 stsp line = NULL;
5639 1cae65b4 2019-09-22 stsp return err;
5640 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5641 a3404814 2018-09-02 stsp wstandout(view->window);
5642 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5644 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5645 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5646 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5647 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5649 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5650 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5651 a3404814 2018-09-02 stsp wstandend(view->window);
5652 2550e4c3 2018-07-13 stsp free(wline);
5653 2550e4c3 2018-07-13 stsp wline = NULL;
5655 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5656 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5658 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5659 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5660 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5661 ab089a2a 2018-07-12 stsp free(id_str);
5662 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5664 ab089a2a 2018-07-12 stsp free(id_str);
5665 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5666 3f60a8ef 2018-07-10 stsp free(line);
5667 2550e4c3 2018-07-13 stsp line = NULL;
5669 3f60a8ef 2018-07-10 stsp return err;
5670 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5671 2550e4c3 2018-07-13 stsp free(wline);
5672 2550e4c3 2018-07-13 stsp wline = NULL;
5673 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5674 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5676 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5677 05171be4 2022-06-23 thomas view->maxx = 0;
5678 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5679 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5680 826082fe 2020-12-10 stsp if (linelen == -1) {
5681 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5682 826082fe 2020-12-10 stsp s->eof = 1;
5685 84451b3e 2018-07-10 stsp free(line);
5686 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5688 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5689 07dd3ed3 2022-08-06 thomas continue;
5690 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5693 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5694 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5695 cbea3800 2022-06-23 thomas if (err) {
5696 cbea3800 2022-06-23 thomas free(line);
5697 cbea3800 2022-06-23 thomas return err;
5699 cbea3800 2022-06-23 thomas free(wline);
5700 cbea3800 2022-06-23 thomas wline = NULL;
5701 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5703 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5704 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5706 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5707 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5708 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5709 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5710 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5711 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5712 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5713 8d0fe45a 2019-08-12 stsp char *id_str;
5714 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5715 91198554 2022-06-23 thomas blame_line->id);
5716 8d0fe45a 2019-08-12 stsp if (err) {
5717 8d0fe45a 2019-08-12 stsp free(line);
5718 8d0fe45a 2019-08-12 stsp return err;
5720 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5722 11b20872 2019-11-08 stsp wattr_on(view->window,
5723 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5724 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5726 11b20872 2019-11-08 stsp wattr_off(view->window,
5727 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5728 8d0fe45a 2019-08-12 stsp free(id_str);
5729 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5731 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5732 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5735 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5736 ee41ec32 2018-07-10 stsp prev_id = NULL;
5739 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5740 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5741 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5743 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5744 41605754 2020-11-12 stsp width = 9;
5745 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5746 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5747 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5748 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5749 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5750 41605754 2020-11-12 stsp if (err) {
5751 41605754 2020-11-12 stsp free(line);
5752 41605754 2020-11-12 stsp return err;
5754 41605754 2020-11-12 stsp width += 9;
5756 923086fd 2022-06-23 thomas int skip;
5757 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5758 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5759 923086fd 2022-06-23 thomas if (err) {
5760 923086fd 2022-06-23 thomas free(line);
5761 923086fd 2022-06-23 thomas return err;
5763 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5764 02bce7e2 2022-06-23 thomas width += 9;
5765 41605754 2020-11-12 stsp free(wline);
5766 41605754 2020-11-12 stsp wline = NULL;
5769 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5770 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5771 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5772 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5774 826082fe 2020-12-10 stsp free(line);
5775 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5777 a5d43cac 2022-07-01 thomas view_border(view);
5779 84451b3e 2018-07-10 stsp return NULL;
5782 84451b3e 2018-07-10 stsp static const struct got_error *
5783 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5784 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5786 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5787 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5788 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5789 1a76625f 2018-10-22 stsp int errcode;
5791 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5792 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5793 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5795 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5796 1a76625f 2018-10-22 stsp if (errcode)
5797 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5799 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5800 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5801 d68a0a7d 2018-07-10 stsp goto done;
5804 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5805 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5807 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5808 d68a0a7d 2018-07-10 stsp if (line->annotated)
5809 d68a0a7d 2018-07-10 stsp goto done;
5811 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5812 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5813 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5814 84451b3e 2018-07-10 stsp goto done;
5816 84451b3e 2018-07-10 stsp line->annotated = 1;
5818 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5819 1a76625f 2018-10-22 stsp if (errcode)
5820 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5821 84451b3e 2018-07-10 stsp return err;
5824 84451b3e 2018-07-10 stsp static void *
5825 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5827 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5828 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5829 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5830 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5831 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5833 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5834 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5835 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5837 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5838 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5839 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5840 9117a7b7 2022-07-01 thomas goto done;
5843 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5844 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5845 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5846 9117a7b7 2022-07-01 thomas goto done;
5848 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5849 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5850 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5851 9117a7b7 2022-07-01 thomas goto done;
5854 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5856 9117a7b7 2022-07-01 thomas goto done;
5858 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5859 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5860 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5861 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5862 fc06ba56 2019-08-22 stsp err = NULL;
5864 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5865 9117a7b7 2022-07-01 thomas if (errcode) {
5866 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5867 9117a7b7 2022-07-01 thomas goto done;
5870 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5871 1d0f4054 2021-06-17 stsp if (err == NULL)
5872 1d0f4054 2021-06-17 stsp err = close_err;
5873 c9beca56 2018-07-22 stsp ta->repo = NULL;
5874 c9beca56 2018-07-22 stsp *ta->complete = 1;
5876 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5877 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5878 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5881 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5882 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5883 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5884 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5885 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5886 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5887 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5888 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5890 18430de3 2018-07-10 stsp return (void *)err;
5893 245d91c1 2018-07-12 stsp static struct got_object_id *
5894 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5895 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5897 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5899 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5900 8d0fe45a 2019-08-12 stsp return NULL;
5902 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5903 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5904 4fc71f3b 2022-07-12 thomas return NULL;
5906 4fc71f3b 2022-07-12 thomas return line->id;
5909 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5910 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5911 4fc71f3b 2022-07-12 thomas int lineno)
5913 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5915 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5916 4fc71f3b 2022-07-12 thomas return NULL;
5918 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5919 245d91c1 2018-07-12 stsp if (!line->annotated)
5920 245d91c1 2018-07-12 stsp return NULL;
5922 245d91c1 2018-07-12 stsp return line->id;
5925 b880a918 2018-07-10 stsp static const struct got_error *
5926 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5928 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5931 245d91c1 2018-07-12 stsp if (blame->thread) {
5932 1a76625f 2018-10-22 stsp int errcode;
5933 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5934 1a76625f 2018-10-22 stsp if (errcode)
5935 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5936 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5937 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5938 1a76625f 2018-10-22 stsp if (errcode)
5939 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5940 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5941 1a76625f 2018-10-22 stsp if (errcode)
5942 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5943 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5944 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5945 245d91c1 2018-07-12 stsp err = NULL;
5946 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5948 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5949 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5950 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5951 1d0f4054 2021-06-17 stsp if (err == NULL)
5952 1d0f4054 2021-06-17 stsp err = close_err;
5953 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5955 245d91c1 2018-07-12 stsp if (blame->f) {
5956 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5957 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5958 245d91c1 2018-07-12 stsp blame->f = NULL;
5960 57670559 2018-12-23 stsp if (blame->lines) {
5961 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5962 57670559 2018-12-23 stsp free(blame->lines[i].id);
5963 57670559 2018-12-23 stsp free(blame->lines);
5964 57670559 2018-12-23 stsp blame->lines = NULL;
5966 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5967 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5968 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5969 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5970 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5971 7cd52833 2022-06-23 thomas if (err == NULL)
5972 7cd52833 2022-06-23 thomas err = pack_err;
5973 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5975 245d91c1 2018-07-12 stsp return err;
5978 245d91c1 2018-07-12 stsp static const struct got_error *
5979 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5981 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5982 fc06ba56 2019-08-22 stsp int *done = arg;
5983 fc06ba56 2019-08-22 stsp int errcode;
5985 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5986 fc06ba56 2019-08-22 stsp if (errcode)
5987 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5988 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5990 fc06ba56 2019-08-22 stsp if (*done)
5991 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5993 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5994 fc06ba56 2019-08-22 stsp if (errcode)
5995 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5996 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5998 fc06ba56 2019-08-22 stsp return err;
6001 fc06ba56 2019-08-22 stsp static const struct got_error *
6002 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6004 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6005 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6006 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6007 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6008 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6009 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6010 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6011 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6012 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6014 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6015 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6017 15a94983 2018-12-23 stsp return err;
6019 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6020 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6021 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6022 f4ae6ddb 2022-07-01 thomas goto done;
6025 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6026 945f9229 2022-04-16 thomas if (err)
6027 945f9229 2022-04-16 thomas goto done;
6029 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6031 84451b3e 2018-07-10 stsp goto done;
6033 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6034 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6035 84451b3e 2018-07-10 stsp goto done;
6038 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6040 a70480e0 2018-06-23 stsp goto done;
6041 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6042 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6043 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6044 84451b3e 2018-07-10 stsp goto done;
6046 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6047 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6049 1fddf795 2021-01-20 stsp goto done;
6050 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6051 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6052 84451b3e 2018-07-10 stsp goto done;
6055 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6056 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6057 b02560ec 2019-08-19 stsp blame->nlines--;
6059 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6060 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6061 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6062 84451b3e 2018-07-10 stsp goto done;
6065 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6067 bd24772e 2018-07-11 stsp goto done;
6068 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6069 7cd52833 2022-06-23 thomas pack_fds);
6070 7cd52833 2022-06-23 thomas if (err)
6071 7cd52833 2022-06-23 thomas goto done;
6073 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6074 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6075 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6076 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6077 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6078 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6079 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6080 245d91c1 2018-07-12 stsp goto done;
6082 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6084 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6085 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6086 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6087 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6088 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6089 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6090 a5388363 2020-12-01 naddy s->blame_complete = 0;
6092 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6093 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6094 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6095 f5a09613 2020-12-13 naddy s->selected_line = 1;
6097 aa61903a 2021-12-31 thomas s->matched_line = 0;
6100 945f9229 2022-04-16 thomas if (commit)
6101 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6102 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6103 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6105 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6106 27d434c2 2018-09-15 stsp free(obj_id);
6108 245d91c1 2018-07-12 stsp stop_blame(blame);
6109 245d91c1 2018-07-12 stsp return err;
6112 245d91c1 2018-07-12 stsp static const struct got_error *
6113 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6114 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6116 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6117 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6119 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6121 c4843652 2019-08-12 stsp s->path = strdup(path);
6122 c4843652 2019-08-12 stsp if (s->path == NULL)
6123 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6125 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6126 c4843652 2019-08-12 stsp if (err) {
6127 c4843652 2019-08-12 stsp free(s->path);
6128 7cbe629d 2018-08-04 stsp return err;
6131 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6132 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6133 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6134 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6135 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6136 fb2756b9 2018-08-04 stsp s->repo = repo;
6137 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6138 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6140 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6141 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6142 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6143 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6145 11b20872 2019-11-08 stsp return err;
6148 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6149 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6150 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6151 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6152 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6153 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6154 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6156 a5388363 2020-12-01 naddy return run_blame(view);
6159 e5a0f69f 2018-08-18 stsp static const struct got_error *
6160 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6162 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6163 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6165 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6166 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6168 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6169 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6170 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6171 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6172 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6175 e5a0f69f 2018-08-18 stsp free(s->path);
6176 11b20872 2019-11-08 stsp free_colors(&s->colors);
6177 e5a0f69f 2018-08-18 stsp return err;
6180 7cbe629d 2018-08-04 stsp static const struct got_error *
6181 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6183 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6185 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6186 6c4c42e0 2019-06-24 stsp return NULL;
6189 2a7d3cd7 2022-09-17 thomas static void
6190 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6191 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6193 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6195 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6196 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6197 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6198 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6199 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6200 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6201 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6204 6c4c42e0 2019-06-24 stsp static const struct got_error *
6205 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6207 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6208 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6209 2b380cc8 2018-10-24 stsp int errcode;
6211 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6212 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6213 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6214 2b380cc8 2018-10-24 stsp if (errcode)
6215 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6217 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6220 51fe7530 2019-08-19 stsp if (s->blame_complete)
6221 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6223 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6225 a5d43cac 2022-07-01 thomas view_border(view);
6226 e5a0f69f 2018-08-18 stsp return err;
6229 e5a0f69f 2018-08-18 stsp static const struct got_error *
6230 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6231 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6233 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6234 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6236 eaeaa612 2022-07-20 thomas *new_view = NULL;
6238 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6239 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6240 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6242 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6243 eaeaa612 2022-07-20 thomas if (err)
6244 eaeaa612 2022-07-20 thomas view_close(log_view);
6246 eaeaa612 2022-07-20 thomas *new_view = log_view;
6248 eaeaa612 2022-07-20 thomas return err;
6251 eaeaa612 2022-07-20 thomas static const struct got_error *
6252 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6254 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6255 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6256 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6257 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6259 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6260 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6261 a5d43cac 2022-07-01 thomas --eos; /* border */
6263 e5a0f69f 2018-08-18 stsp switch (ch) {
6264 05171be4 2022-06-23 thomas case '0':
6265 05171be4 2022-06-23 thomas view->x = 0;
6267 05171be4 2022-06-23 thomas case '$':
6268 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
6269 07b0611c 2022-06-23 thomas view->count = 0;
6271 05171be4 2022-06-23 thomas case KEY_RIGHT:
6272 05171be4 2022-06-23 thomas case 'l':
6273 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
6274 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
6276 07b0611c 2022-06-23 thomas view->count = 0;
6278 05171be4 2022-06-23 thomas case KEY_LEFT:
6279 05171be4 2022-06-23 thomas case 'h':
6280 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
6281 07b0611c 2022-06-23 thomas if (view->x <= 0)
6282 07b0611c 2022-06-23 thomas view->count = 0;
6285 1e37a5c2 2019-05-12 jcs s->done = 1;
6287 4deef56f 2021-09-02 naddy case 'g':
6288 4deef56f 2021-09-02 naddy case KEY_HOME:
6289 4deef56f 2021-09-02 naddy s->selected_line = 1;
6290 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6291 07b0611c 2022-06-23 thomas view->count = 0;
6293 4deef56f 2021-09-02 naddy case 'G':
6294 4deef56f 2021-09-02 naddy case KEY_END:
6295 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6296 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6297 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6299 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6300 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6302 07b0611c 2022-06-23 thomas view->count = 0;
6305 1e37a5c2 2019-05-12 jcs case KEY_UP:
6306 f7140bf5 2021-10-17 thomas case CTRL('p'):
6307 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6308 1e37a5c2 2019-05-12 jcs s->selected_line--;
6309 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6310 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6311 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6313 07b0611c 2022-06-23 thomas view->count = 0;
6315 70f17a53 2022-06-13 thomas case CTRL('u'):
6316 23427b14 2022-06-23 thomas case 'u':
6317 70f17a53 2022-06-13 thomas nscroll /= 2;
6318 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6319 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6320 ea025d1d 2020-02-22 naddy case CTRL('b'):
6321 1c5e5faa 2022-06-23 thomas case 'b':
6322 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6323 07b0611c 2022-06-23 thomas if (view->count > 1)
6324 07b0611c 2022-06-23 thomas nscroll += nscroll;
6325 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6326 07b0611c 2022-06-23 thomas view->count = 0;
6329 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6330 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6332 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6335 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6336 f7140bf5 2021-10-17 thomas case CTRL('n'):
6337 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6338 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6339 1e37a5c2 2019-05-12 jcs s->selected_line++;
6340 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6341 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6343 07b0611c 2022-06-23 thomas view->count = 0;
6345 1c5e5faa 2022-06-23 thomas case 'c':
6346 1e37a5c2 2019-05-12 jcs case 'p': {
6347 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6349 07b0611c 2022-06-23 thomas view->count = 0;
6350 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6351 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6352 1e37a5c2 2019-05-12 jcs if (id == NULL)
6354 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6355 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6356 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6357 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6358 1e37a5c2 2019-05-12 jcs int obj_type;
6359 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6360 1e37a5c2 2019-05-12 jcs s->repo, id);
6363 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6364 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6365 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6366 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6369 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6370 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6371 ec242592 2022-04-22 thomas s->repo, &pid->id);
6372 945f9229 2022-04-16 thomas if (err)
6374 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6375 945f9229 2022-04-16 thomas pcommit, s->path);
6376 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6377 e5a0f69f 2018-08-18 stsp if (err) {
6378 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6379 1e37a5c2 2019-05-12 jcs err = NULL;
6380 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6383 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6385 1e37a5c2 2019-05-12 jcs free(blob_id);
6386 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6387 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6388 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6391 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6392 ec242592 2022-04-22 thomas &pid->id);
6393 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6395 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6396 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6398 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6403 1e37a5c2 2019-05-12 jcs s->done = 1;
6404 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6405 1e37a5c2 2019-05-12 jcs s->done = 0;
6406 1e37a5c2 2019-05-12 jcs if (thread_err)
6408 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6409 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6410 a5388363 2020-12-01 naddy err = run_blame(view);
6415 1c5e5faa 2022-06-23 thomas case 'C': {
6416 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6418 07b0611c 2022-06-23 thomas view->count = 0;
6419 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6420 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6422 1e37a5c2 2019-05-12 jcs s->done = 1;
6423 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6424 1e37a5c2 2019-05-12 jcs s->done = 0;
6425 1e37a5c2 2019-05-12 jcs if (thread_err)
6427 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6428 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6429 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6430 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6431 a5388363 2020-12-01 naddy err = run_blame(view);
6436 2a31b33b 2022-07-23 thomas case 'L':
6437 eaeaa612 2022-07-20 thomas view->count = 0;
6438 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6439 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6440 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6441 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6443 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6444 1e37a5c2 2019-05-12 jcs case '\r': {
6445 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6446 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6447 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6449 07b0611c 2022-06-23 thomas view->count = 0;
6450 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6451 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6452 1e37a5c2 2019-05-12 jcs if (id == NULL)
6454 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6457 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6458 4fc71f3b 2022-07-12 thomas if (*new_view) {
6459 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6460 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6461 4fc71f3b 2022-07-12 thomas if (err)
6463 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6464 4fc71f3b 2022-07-12 thomas } else {
6465 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6466 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6468 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6469 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6470 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6471 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6472 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6476 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6477 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6478 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6480 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6483 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6484 4fc71f3b 2022-07-12 thomas s->selected_line;
6485 4fc71f3b 2022-07-12 thomas if (*new_view)
6486 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6487 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6488 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6489 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6490 a5d43cac 2022-07-01 thomas if (err)
6494 e78dc838 2020-12-04 stsp view->focussed = 0;
6495 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6496 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6497 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6498 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6499 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6500 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6503 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6504 40236d76 2022-06-23 thomas if (err)
6506 e78dc838 2020-12-04 stsp view->focus_child = 1;
6508 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6513 70f17a53 2022-06-13 thomas case CTRL('d'):
6514 23427b14 2022-06-23 thomas case 'd':
6515 70f17a53 2022-06-13 thomas nscroll /= 2;
6516 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6517 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6518 ea025d1d 2020-02-22 naddy case CTRL('f'):
6519 1c5e5faa 2022-06-23 thomas case 'f':
6521 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6522 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6523 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6524 07b0611c 2022-06-23 thomas view->count = 0;
6527 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6528 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6529 70f17a53 2022-06-13 thomas s->selected_line +=
6530 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6531 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6533 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6534 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6536 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6537 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6539 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6540 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6541 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6542 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6546 07b0611c 2022-06-23 thomas view->count = 0;
6549 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6552 adf4c9e0 2022-07-03 thomas static const struct got_error *
6553 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6555 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6556 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6558 adf4c9e0 2022-07-03 thomas view->count = 0;
6559 adf4c9e0 2022-07-03 thomas s->done = 1;
6560 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6561 adf4c9e0 2022-07-03 thomas s->done = 0;
6562 adf4c9e0 2022-07-03 thomas if (err)
6563 adf4c9e0 2022-07-03 thomas return err;
6564 adf4c9e0 2022-07-03 thomas return run_blame(view);
6567 a70480e0 2018-06-23 stsp static const struct got_error *
6568 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6570 a70480e0 2018-06-23 stsp const struct got_error *error;
6571 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6572 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6573 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6574 0587e10c 2020-07-23 stsp char *link_target = NULL;
6575 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6576 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6577 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6579 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6580 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6582 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6583 a70480e0 2018-06-23 stsp switch (ch) {
6585 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6588 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6589 69069811 2018-08-02 stsp if (repo_path == NULL)
6590 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6594 17020d27 2019-03-07 stsp usage_blame();
6595 a70480e0 2018-06-23 stsp /* NOTREACHED */
6599 a70480e0 2018-06-23 stsp argc -= optind;
6600 a70480e0 2018-06-23 stsp argv += optind;
6602 f135c941 2020-02-20 stsp if (argc != 1)
6603 a70480e0 2018-06-23 stsp usage_blame();
6605 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6606 7cd52833 2022-06-23 thomas if (error != NULL)
6607 7cd52833 2022-06-23 thomas goto done;
6609 69069811 2018-08-02 stsp if (repo_path == NULL) {
6610 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6611 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6612 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6613 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6614 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6615 c156c7a4 2020-12-18 stsp goto done;
6616 f135c941 2020-02-20 stsp if (worktree)
6617 eb41ed75 2019-02-05 stsp repo_path =
6618 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6620 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6621 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6622 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6623 c156c7a4 2020-12-18 stsp goto done;
6627 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6628 c02c541e 2019-03-29 stsp if (error != NULL)
6629 8e94dd5b 2019-01-04 stsp goto done;
6631 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6632 f135c941 2020-02-20 stsp worktree);
6633 c02c541e 2019-03-29 stsp if (error)
6634 92205607 2019-01-04 stsp goto done;
6636 f135c941 2020-02-20 stsp init_curses();
6638 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6639 51a10b52 2020-12-26 stsp if (error)
6640 51a10b52 2020-12-26 stsp goto done;
6642 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6643 eb41ed75 2019-02-05 stsp if (error)
6644 69069811 2018-08-02 stsp goto done;
6646 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6647 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6648 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6649 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6650 a70480e0 2018-06-23 stsp if (error != NULL)
6651 66b4983c 2018-06-23 stsp goto done;
6652 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6653 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6655 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6656 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6658 a19e88aa 2018-06-23 stsp if (error != NULL)
6659 8b473291 2019-02-21 stsp goto done;
6661 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6662 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6663 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6664 e1cd8fed 2018-08-01 stsp goto done;
6667 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6668 945f9229 2022-04-16 thomas if (error)
6669 945f9229 2022-04-16 thomas goto done;
6671 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6672 945f9229 2022-04-16 thomas commit, repo);
6673 7cbe629d 2018-08-04 stsp if (error)
6674 7cbe629d 2018-08-04 stsp goto done;
6676 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6677 78756c87 2020-11-24 stsp commit_id, repo);
6678 0587e10c 2020-07-23 stsp if (error)
6679 0587e10c 2020-07-23 stsp goto done;
6680 12314ad4 2019-08-31 stsp if (worktree) {
6681 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6682 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6683 12314ad4 2019-08-31 stsp worktree = NULL;
6685 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6687 69069811 2018-08-02 stsp free(repo_path);
6688 f135c941 2020-02-20 stsp free(in_repo_path);
6689 0587e10c 2020-07-23 stsp free(link_target);
6690 69069811 2018-08-02 stsp free(cwd);
6691 a70480e0 2018-06-23 stsp free(commit_id);
6692 945f9229 2022-04-16 thomas if (commit)
6693 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6694 eb41ed75 2019-02-05 stsp if (worktree)
6695 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6696 1d0f4054 2021-06-17 stsp if (repo) {
6697 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6698 1d0f4054 2021-06-17 stsp if (error == NULL)
6699 1d0f4054 2021-06-17 stsp error = close_err;
6701 7cd52833 2022-06-23 thomas if (pack_fds) {
6702 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6703 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6704 7cd52833 2022-06-23 thomas if (error == NULL)
6705 7cd52833 2022-06-23 thomas error = pack_err;
6707 51a10b52 2020-12-26 stsp tog_free_refs();
6708 a70480e0 2018-06-23 stsp return error;
6711 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6712 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6714 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6715 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6716 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6717 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6718 86f4aab9 2022-09-09 thomas char *index = NULL;
6719 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6720 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6721 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6723 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6724 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6725 a5d43cac 2022-07-01 thomas --limit; /* border */
6727 f7d12f7e 2018-08-01 stsp werase(view->window);
6729 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6730 ffd1d5e5 2018-06-23 stsp return NULL;
6732 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6735 ffd1d5e5 2018-06-23 stsp return err;
6736 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6737 a3404814 2018-09-02 stsp wstandout(view->window);
6738 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6740 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6741 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6742 86f4aab9 2022-09-09 thomas free(wline);
6743 86f4aab9 2022-09-09 thomas wline = NULL;
6744 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6745 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6747 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6748 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6749 a3404814 2018-09-02 stsp wstandend(view->window);
6750 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6751 86f4aab9 2022-09-09 thomas return NULL;
6753 6f5f393a 2022-08-12 thomas i += s->selected;
6754 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6755 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6756 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6757 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6759 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6760 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6761 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6762 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6763 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6764 86f4aab9 2022-09-09 thomas free(index);
6766 ce52c690 2018-06-23 stsp return err;
6767 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6768 2550e4c3 2018-07-13 stsp free(wline);
6769 2550e4c3 2018-07-13 stsp wline = NULL;
6770 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6771 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6772 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6773 ffd1d5e5 2018-06-23 stsp return NULL;
6774 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6775 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6776 a1eca9bb 2018-06-23 stsp return NULL;
6778 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6779 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6780 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6781 0cf4efb1 2018-09-29 stsp if (view->focussed)
6782 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6783 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6785 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6786 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6787 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6788 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6789 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6790 ffd1d5e5 2018-06-23 stsp return NULL;
6794 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6797 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6798 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6799 848d6979 2019-08-12 stsp const char *modestr = "";
6800 56e0773d 2019-11-28 stsp mode_t mode;
6802 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6803 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6805 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6806 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6807 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6809 638f9024 2019-05-13 stsp return got_error_from_errno(
6810 230a42bd 2019-05-11 jcs "got_object_id_str");
6812 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6813 63c5ca5d 2019-08-24 stsp modestr = "$";
6814 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6817 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6818 d86d3b18 2020-12-01 naddy te, s->repo);
6819 0d6c6ee3 2020-05-20 stsp if (err) {
6820 0d6c6ee3 2020-05-20 stsp free(id_str);
6821 0d6c6ee3 2020-05-20 stsp return err;
6823 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6824 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6825 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6827 848d6979 2019-08-12 stsp modestr = "@";
6829 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6830 848d6979 2019-08-12 stsp modestr = "/";
6831 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6832 848d6979 2019-08-12 stsp modestr = "*";
6833 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6834 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6835 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6836 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6837 1d13200f 2018-07-12 stsp free(id_str);
6838 0d6c6ee3 2020-05-20 stsp free(link_target);
6839 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6841 1d13200f 2018-07-12 stsp free(id_str);
6842 0d6c6ee3 2020-05-20 stsp free(link_target);
6843 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6845 ffd1d5e5 2018-06-23 stsp if (err) {
6846 ffd1d5e5 2018-06-23 stsp free(line);
6849 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6850 0cf4efb1 2018-09-29 stsp if (view->focussed)
6851 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6852 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6854 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6856 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6857 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6858 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6860 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6861 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6862 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6863 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6864 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6865 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6866 ffd1d5e5 2018-06-23 stsp free(line);
6867 2550e4c3 2018-07-13 stsp free(wline);
6868 2550e4c3 2018-07-13 stsp wline = NULL;
6870 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6871 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6872 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6876 ffd1d5e5 2018-06-23 stsp return err;
6879 ffd1d5e5 2018-06-23 stsp static void
6880 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6882 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6883 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6884 fa86c4bf 2020-11-29 stsp int i = 0;
6886 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6889 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6890 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6891 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6892 fa86c4bf 2020-11-29 stsp if (!isroot)
6893 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6896 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6897 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6901 a5d43cac 2022-07-01 thomas static const struct got_error *
6902 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6904 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6905 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6906 ffd1d5e5 2018-06-23 stsp int n = 0;
6908 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6909 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6910 694d3271 2020-12-01 naddy s->first_displayed_entry);
6912 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6914 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6915 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6916 07dd3ed3 2022-08-06 thomas if (last) {
6917 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6918 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6920 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6921 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6922 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6926 a5d43cac 2022-07-01 thomas return NULL;
6929 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6930 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6931 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6933 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6934 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6935 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6937 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6938 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6939 56e0773d 2019-11-28 stsp + 1 /* slash */;
6941 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6943 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6944 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6945 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6947 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6948 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6949 d9765a41 2018-06-23 stsp while (pt) {
6950 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6951 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6952 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6953 cb2ebc8a 2018-06-23 stsp goto done;
6955 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6956 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6957 cb2ebc8a 2018-06-23 stsp goto done;
6959 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6962 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6963 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6964 ce52c690 2018-06-23 stsp goto done;
6968 ce52c690 2018-06-23 stsp if (err) {
6969 ce52c690 2018-06-23 stsp free(*path);
6970 ce52c690 2018-06-23 stsp *path = NULL;
6972 ce52c690 2018-06-23 stsp return err;
6975 ce52c690 2018-06-23 stsp static const struct got_error *
6976 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6977 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6978 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6980 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6981 ce52c690 2018-06-23 stsp char *path;
6982 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6984 a0de39f3 2019-08-09 stsp *new_view = NULL;
6986 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6988 ce52c690 2018-06-23 stsp return err;
6990 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6991 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6992 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6993 83ce39e3 2019-08-12 stsp goto done;
6996 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6997 e5a0f69f 2018-08-18 stsp if (err) {
6998 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6999 fc06ba56 2019-08-22 stsp err = NULL;
7000 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7002 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7004 83ce39e3 2019-08-12 stsp free(path);
7005 69efd4c4 2018-07-18 stsp return err;
7008 69efd4c4 2018-07-18 stsp static const struct got_error *
7009 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7010 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7012 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7013 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7014 69efd4c4 2018-07-18 stsp char *path;
7016 a0de39f3 2019-08-09 stsp *new_view = NULL;
7018 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7019 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7020 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7022 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7024 69efd4c4 2018-07-18 stsp return err;
7026 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7029 e5a0f69f 2018-08-18 stsp view_close(log_view);
7031 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7032 cb2ebc8a 2018-06-23 stsp free(path);
7033 cb2ebc8a 2018-06-23 stsp return err;
7036 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7037 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7038 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7040 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7041 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7042 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7043 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7045 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7046 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7048 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7049 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7050 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7052 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7054 bc573f3b 2021-07-10 stsp goto done;
7057 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7058 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7059 bc573f3b 2021-07-10 stsp * closed on demand.
7061 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7062 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7064 bc573f3b 2021-07-10 stsp goto done;
7065 bc573f3b 2021-07-10 stsp s->tree = s->root;
7067 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7068 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7069 ffd1d5e5 2018-06-23 stsp goto done;
7071 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7072 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7073 ffd1d5e5 2018-06-23 stsp goto done;
7076 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7077 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7078 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7079 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7080 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7081 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7082 9cd7cbd1 2020-12-07 stsp goto done;
7085 fb2756b9 2018-08-04 stsp s->repo = repo;
7087 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7088 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7089 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7090 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7092 c0b01bdb 2019-11-08 stsp goto done;
7093 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7094 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7096 c0b01bdb 2019-11-08 stsp goto done;
7097 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7098 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7099 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7101 c0b01bdb 2019-11-08 stsp goto done;
7103 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7104 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7105 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7107 11b20872 2019-11-08 stsp goto done;
7109 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7110 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7112 c0b01bdb 2019-11-08 stsp goto done;
7115 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7116 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7117 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7118 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7119 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7121 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7122 bc573f3b 2021-07-10 stsp if (commit)
7123 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7125 bc573f3b 2021-07-10 stsp close_tree_view(view);
7126 ad80ab7b 2018-08-04 stsp return err;
7129 e5a0f69f 2018-08-18 stsp static const struct got_error *
7130 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7132 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7134 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7135 fb2756b9 2018-08-04 stsp free(s->tree_label);
7136 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7137 6484ec90 2018-09-29 stsp free(s->commit_id);
7138 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7139 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7140 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7141 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7142 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7143 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7144 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7145 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7146 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7147 ad80ab7b 2018-08-04 stsp free(parent);
7150 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7151 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7152 bc573f3b 2021-07-10 stsp if (s->root)
7153 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7154 7c32bd05 2019-06-22 stsp return NULL;
7157 7c32bd05 2019-06-22 stsp static const struct got_error *
7158 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7160 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7162 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7163 7c32bd05 2019-06-22 stsp return NULL;
7166 7c32bd05 2019-06-22 stsp static int
7167 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7169 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7171 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7175 7c32bd05 2019-06-22 stsp static const struct got_error *
7176 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7178 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7179 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7181 7c32bd05 2019-06-22 stsp if (!view->searching) {
7182 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7183 7c32bd05 2019-06-22 stsp return NULL;
7186 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7187 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7188 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7189 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7190 56e0773d 2019-11-28 stsp s->selected_entry);
7192 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7194 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7195 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7197 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7198 56e0773d 2019-11-28 stsp s->selected_entry);
7201 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7202 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7203 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7204 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7206 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7209 7c32bd05 2019-06-22 stsp while (1) {
7210 56e0773d 2019-11-28 stsp if (te == NULL) {
7211 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7212 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7213 ac66afb8 2019-06-24 stsp return NULL;
7215 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7216 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7218 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7221 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7222 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7223 56e0773d 2019-11-28 stsp s->matched_entry = te;
7227 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7228 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7230 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7233 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7234 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7235 7c32bd05 2019-06-22 stsp s->selected = 0;
7238 e5a0f69f 2018-08-18 stsp return NULL;
7241 ad80ab7b 2018-08-04 stsp static const struct got_error *
7242 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7244 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7245 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7246 e5a0f69f 2018-08-18 stsp char *parent_path;
7248 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7250 e5a0f69f 2018-08-18 stsp return err;
7252 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7253 e5a0f69f 2018-08-18 stsp free(parent_path);
7255 a5d43cac 2022-07-01 thomas view_border(view);
7256 e5a0f69f 2018-08-18 stsp return err;
7259 07dd3ed3 2022-08-06 thomas static const struct got_error *
7260 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7262 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7263 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7264 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7265 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7266 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7267 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7269 07dd3ed3 2022-08-06 thomas g = view->gline;
7270 07dd3ed3 2022-08-06 thomas view->gline = 0;
7272 07dd3ed3 2022-08-06 thomas if (g == 0)
7274 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7275 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7277 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7278 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7279 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7281 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7282 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7283 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7285 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7286 07dd3ed3 2022-08-06 thomas last += off;
7288 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7289 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7290 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7293 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7294 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7295 07dd3ed3 2022-08-06 thomas i += off;
7298 07dd3ed3 2022-08-06 thomas if (i < g) {
7299 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7300 07dd3ed3 2022-08-06 thomas if (err)
7301 07dd3ed3 2022-08-06 thomas return err;
7302 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7303 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7304 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7305 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7306 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7307 07dd3ed3 2022-08-06 thomas first += off;
7308 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7310 07dd3ed3 2022-08-06 thomas } else if (i > g)
7311 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7313 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7314 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7315 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7317 07dd3ed3 2022-08-06 thomas return NULL;
7320 e5a0f69f 2018-08-18 stsp static const struct got_error *
7321 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7323 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7324 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7325 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7326 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7328 07dd3ed3 2022-08-06 thomas if (view->gline)
7329 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7331 e5a0f69f 2018-08-18 stsp switch (ch) {
7333 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7334 07b0611c 2022-06-23 thomas view->count = 0;
7336 1be4947a 2022-07-22 thomas case 'L':
7337 07b0611c 2022-06-23 thomas view->count = 0;
7338 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7340 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7342 1be4947a 2022-07-22 thomas case 'R':
7343 07b0611c 2022-06-23 thomas view->count = 0;
7344 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7346 e4526bf5 2021-09-03 naddy case 'g':
7347 aa7a1117 2023-01-09 thomas case '=':
7348 e4526bf5 2021-09-03 naddy case KEY_HOME:
7349 e4526bf5 2021-09-03 naddy s->selected = 0;
7350 07b0611c 2022-06-23 thomas view->count = 0;
7351 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7352 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7353 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7355 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7357 e4526bf5 2021-09-03 naddy case 'G':
7358 aa7a1117 2023-01-09 thomas case '*':
7359 a5d43cac 2022-07-01 thomas case KEY_END: {
7360 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7362 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7363 a5d43cac 2022-07-01 thomas --eos; /* border */
7364 e4526bf5 2021-09-03 naddy s->selected = 0;
7365 07b0611c 2022-06-23 thomas view->count = 0;
7366 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7367 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7368 e4526bf5 2021-09-03 naddy if (te == NULL) {
7369 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7370 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7375 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7376 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7378 e4526bf5 2021-09-03 naddy if (n > 0)
7379 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7383 1e37a5c2 2019-05-12 jcs case KEY_UP:
7384 f7140bf5 2021-10-17 thomas case CTRL('p'):
7385 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7386 1e37a5c2 2019-05-12 jcs s->selected--;
7389 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7390 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7391 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7392 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7393 07b0611c 2022-06-23 thomas view->count = 0;
7395 70f17a53 2022-06-13 thomas case CTRL('u'):
7396 23427b14 2022-06-23 thomas case 'u':
7397 70f17a53 2022-06-13 thomas nscroll /= 2;
7398 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7399 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7400 ea025d1d 2020-02-22 naddy case CTRL('b'):
7401 1c5e5faa 2022-06-23 thomas case 'b':
7402 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7403 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7404 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7405 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7407 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7408 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7410 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7411 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7412 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7413 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7414 07b0611c 2022-06-23 thomas view->count = 0;
7417 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7418 f7140bf5 2021-10-17 thomas case CTRL('n'):
7419 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7420 1e37a5c2 2019-05-12 jcs s->selected++;
7423 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7424 07b0611c 2022-06-23 thomas == NULL) {
7425 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7426 07b0611c 2022-06-23 thomas view->count = 0;
7429 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7431 70f17a53 2022-06-13 thomas case CTRL('d'):
7432 23427b14 2022-06-23 thomas case 'd':
7433 70f17a53 2022-06-13 thomas nscroll /= 2;
7434 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7435 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7436 ea025d1d 2020-02-22 naddy case CTRL('f'):
7437 1c5e5faa 2022-06-23 thomas case 'f':
7438 4c2d69cb 2022-06-23 thomas case ' ':
7439 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7441 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7442 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7443 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7444 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7446 07b0611c 2022-06-23 thomas view->count = 0;
7449 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7451 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7453 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7454 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7455 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7456 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7457 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7458 07b0611c 2022-06-23 thomas view->count = 0;
7461 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7462 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7464 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7465 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7466 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7467 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7468 1e37a5c2 2019-05-12 jcs s->selected_entry =
7469 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7470 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7471 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7472 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7473 a5d43cac 2022-07-01 thomas if (err)
7476 1e37a5c2 2019-05-12 jcs free(parent);
7477 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7478 56e0773d 2019-11-28 stsp s->selected_entry))) {
7479 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7480 07b0611c 2022-06-23 thomas view->count = 0;
7481 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7482 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7485 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7486 941e9f74 2019-05-21 stsp if (err) {
7487 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7490 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7491 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7493 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7494 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7495 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7496 07b0611c 2022-06-23 thomas view->count = 0;
7499 07b0611c 2022-06-23 thomas view->count = 0;
7503 ffd1d5e5 2018-06-23 stsp return err;
7506 ffd1d5e5 2018-06-23 stsp __dead static void
7507 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7510 91198554 2022-06-23 thomas fprintf(stderr,
7511 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7512 ffd1d5e5 2018-06-23 stsp getprogname());
7516 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7517 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7519 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7520 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7521 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7522 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7523 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7524 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7525 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7526 4e97c21c 2020-12-06 stsp char *label = NULL;
7527 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7528 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7530 5221c383 2018-08-01 stsp struct tog_view *view;
7531 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7533 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7534 ffd1d5e5 2018-06-23 stsp switch (ch) {
7536 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7539 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7540 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7541 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7545 e99e2d15 2020-11-24 naddy usage_tree();
7546 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7550 ffd1d5e5 2018-06-23 stsp argc -= optind;
7551 ffd1d5e5 2018-06-23 stsp argv += optind;
7553 55cccc34 2020-02-20 stsp if (argc > 1)
7554 e99e2d15 2020-11-24 naddy usage_tree();
7556 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7557 7cd52833 2022-06-23 thomas if (error != NULL)
7558 7cd52833 2022-06-23 thomas goto done;
7560 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7561 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7562 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7563 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7564 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7565 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7566 c156c7a4 2020-12-18 stsp goto done;
7567 55cccc34 2020-02-20 stsp if (worktree)
7568 52185f70 2019-02-05 stsp repo_path =
7569 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7571 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7572 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7573 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7574 c156c7a4 2020-12-18 stsp goto done;
7578 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7579 c02c541e 2019-03-29 stsp if (error != NULL)
7580 52185f70 2019-02-05 stsp goto done;
7582 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7583 55cccc34 2020-02-20 stsp repo, worktree);
7584 55cccc34 2020-02-20 stsp if (error)
7585 55cccc34 2020-02-20 stsp goto done;
7587 55cccc34 2020-02-20 stsp init_curses();
7589 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7590 c02c541e 2019-03-29 stsp if (error)
7591 52185f70 2019-02-05 stsp goto done;
7593 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7594 51a10b52 2020-12-26 stsp if (error)
7595 51a10b52 2020-12-26 stsp goto done;
7597 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7598 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7599 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7600 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7601 4e97c21c 2020-12-06 stsp if (error)
7602 4e97c21c 2020-12-06 stsp goto done;
7603 4e97c21c 2020-12-06 stsp head_ref_name = label;
7605 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7606 4e97c21c 2020-12-06 stsp if (error == NULL)
7607 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7608 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7609 4e97c21c 2020-12-06 stsp goto done;
7610 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7611 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7612 4e97c21c 2020-12-06 stsp if (error)
7613 4e97c21c 2020-12-06 stsp goto done;
7616 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7617 945f9229 2022-04-16 thomas if (error)
7618 945f9229 2022-04-16 thomas goto done;
7620 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7621 5221c383 2018-08-01 stsp if (view == NULL) {
7622 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7623 5221c383 2018-08-01 stsp goto done;
7625 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7626 ad80ab7b 2018-08-04 stsp if (error)
7627 ad80ab7b 2018-08-04 stsp goto done;
7628 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7629 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7630 d91faf3b 2020-12-01 naddy in_repo_path);
7631 55cccc34 2020-02-20 stsp if (error)
7632 55cccc34 2020-02-20 stsp goto done;
7635 55cccc34 2020-02-20 stsp if (worktree) {
7636 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7637 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7638 55cccc34 2020-02-20 stsp worktree = NULL;
7640 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7642 52185f70 2019-02-05 stsp free(repo_path);
7643 e4a0e26d 2020-02-20 stsp free(cwd);
7644 ffd1d5e5 2018-06-23 stsp free(commit_id);
7645 4e97c21c 2020-12-06 stsp free(label);
7647 486cd271 2020-12-06 stsp got_ref_close(ref);
7648 1d0f4054 2021-06-17 stsp if (repo) {
7649 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7650 1d0f4054 2021-06-17 stsp if (error == NULL)
7651 1d0f4054 2021-06-17 stsp error = close_err;
7653 7cd52833 2022-06-23 thomas if (pack_fds) {
7654 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7655 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7656 7cd52833 2022-06-23 thomas if (error == NULL)
7657 7cd52833 2022-06-23 thomas error = pack_err;
7659 51a10b52 2020-12-26 stsp tog_free_refs();
7660 ffd1d5e5 2018-06-23 stsp return error;
7663 6458efa5 2020-11-24 stsp static const struct got_error *
7664 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7666 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7667 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7669 6458efa5 2020-11-24 stsp s->nrefs = 0;
7670 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7671 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7672 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7673 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7674 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7677 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7678 6458efa5 2020-11-24 stsp if (re == NULL)
7679 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7681 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7682 8924d611 2020-12-26 stsp if (re->ref == NULL)
7683 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7684 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7685 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7688 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7689 6458efa5 2020-11-24 stsp return NULL;
7692 ef20f542 2022-06-26 thomas static void
7693 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7695 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7697 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7698 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7699 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7700 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7705 6458efa5 2020-11-24 stsp static const struct got_error *
7706 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7708 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7709 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7711 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7712 6458efa5 2020-11-24 stsp s->repo = repo;
7714 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7715 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7717 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7719 6458efa5 2020-11-24 stsp return err;
7721 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7722 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7723 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7724 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7726 6458efa5 2020-11-24 stsp goto done;
7728 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7729 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7730 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7732 6458efa5 2020-11-24 stsp goto done;
7734 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7735 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7736 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7737 2183bbf6 2022-01-23 thomas if (err)
7738 2183bbf6 2022-01-23 thomas goto done;
7740 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7741 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7742 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7744 6458efa5 2020-11-24 stsp goto done;
7747 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7748 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7749 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7750 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7751 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7754 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7755 6458efa5 2020-11-24 stsp return err;
7758 6458efa5 2020-11-24 stsp static const struct got_error *
7759 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7761 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7763 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7764 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7766 6458efa5 2020-11-24 stsp return NULL;
7769 6458efa5 2020-11-24 stsp static const struct got_error *
7770 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7771 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7773 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7774 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7775 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7776 6458efa5 2020-11-24 stsp int obj_type;
7778 c42c9805 2020-11-24 stsp *commit_id = NULL;
7780 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7782 6458efa5 2020-11-24 stsp return err;
7784 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7786 6458efa5 2020-11-24 stsp goto done;
7788 6458efa5 2020-11-24 stsp switch (obj_type) {
7789 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7790 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7792 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7793 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7795 6458efa5 2020-11-24 stsp goto done;
7796 c42c9805 2020-11-24 stsp free(obj_id);
7797 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7798 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7800 6458efa5 2020-11-24 stsp goto done;
7801 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7802 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7803 c42c9805 2020-11-24 stsp goto done;
7805 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7806 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7807 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7808 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7809 c42c9805 2020-11-24 stsp goto done;
7813 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7819 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7820 c42c9805 2020-11-24 stsp if (err) {
7821 c42c9805 2020-11-24 stsp free(*commit_id);
7822 c42c9805 2020-11-24 stsp *commit_id = NULL;
7824 c42c9805 2020-11-24 stsp return err;
7827 c42c9805 2020-11-24 stsp static const struct got_error *
7828 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7829 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7831 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7832 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7833 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7835 c42c9805 2020-11-24 stsp *new_view = NULL;
7837 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7838 c42c9805 2020-11-24 stsp if (err) {
7839 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7840 c42c9805 2020-11-24 stsp return err;
7842 c42c9805 2020-11-24 stsp return NULL;
7845 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7846 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7847 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7848 6458efa5 2020-11-24 stsp goto done;
7851 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7852 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7855 6458efa5 2020-11-24 stsp view_close(log_view);
7857 6458efa5 2020-11-24 stsp *new_view = log_view;
7858 c42c9805 2020-11-24 stsp free(commit_id);
7859 6458efa5 2020-11-24 stsp return err;
7862 ce5b7c56 2019-07-09 stsp static void
7863 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7865 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7866 34ba6917 2020-11-30 stsp int i = 0;
7868 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7871 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7872 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7873 34ba6917 2020-11-30 stsp if (re == NULL)
7875 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7876 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7880 a5d43cac 2022-07-01 thomas static const struct got_error *
7881 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7883 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7884 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7885 6458efa5 2020-11-24 stsp int n = 0;
7887 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7888 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7890 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7892 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7893 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7894 07dd3ed3 2022-08-06 thomas if (last) {
7895 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7896 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7898 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7899 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7900 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7904 a5d43cac 2022-07-01 thomas return NULL;
7907 6458efa5 2020-11-24 stsp static const struct got_error *
7908 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7910 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7912 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7913 6458efa5 2020-11-24 stsp return NULL;
7916 6458efa5 2020-11-24 stsp static int
7917 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7919 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7921 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7925 6458efa5 2020-11-24 stsp static const struct got_error *
7926 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7928 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7929 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7931 6458efa5 2020-11-24 stsp if (!view->searching) {
7932 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7933 6458efa5 2020-11-24 stsp return NULL;
7936 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7937 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7938 6458efa5 2020-11-24 stsp if (s->selected_entry)
7939 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7941 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7942 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7944 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7945 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7947 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7948 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7951 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7952 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7953 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7954 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7956 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7959 6458efa5 2020-11-24 stsp while (1) {
7960 6458efa5 2020-11-24 stsp if (re == NULL) {
7961 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7962 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7963 6458efa5 2020-11-24 stsp return NULL;
7965 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7966 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7968 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7971 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7972 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7973 6458efa5 2020-11-24 stsp s->matched_entry = re;
7977 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7978 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7980 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7983 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7984 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7985 6458efa5 2020-11-24 stsp s->selected = 0;
7988 6458efa5 2020-11-24 stsp return NULL;
7991 6458efa5 2020-11-24 stsp static const struct got_error *
7992 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7994 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7995 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7996 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7997 6458efa5 2020-11-24 stsp char *line = NULL;
7998 6458efa5 2020-11-24 stsp wchar_t *wline;
7999 6458efa5 2020-11-24 stsp struct tog_color *tc;
8000 6458efa5 2020-11-24 stsp int width, n;
8001 6458efa5 2020-11-24 stsp int limit = view->nlines;
8003 6458efa5 2020-11-24 stsp werase(view->window);
8005 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8006 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8007 a5d43cac 2022-07-01 thomas --limit; /* border */
8009 6458efa5 2020-11-24 stsp if (limit == 0)
8010 6458efa5 2020-11-24 stsp return NULL;
8012 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8014 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8015 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8016 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8018 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8019 6458efa5 2020-11-24 stsp if (err) {
8020 6458efa5 2020-11-24 stsp free(line);
8021 6458efa5 2020-11-24 stsp return err;
8023 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8024 6458efa5 2020-11-24 stsp wstandout(view->window);
8025 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8026 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8027 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8028 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8029 6458efa5 2020-11-24 stsp wstandend(view->window);
8030 6458efa5 2020-11-24 stsp free(wline);
8031 6458efa5 2020-11-24 stsp wline = NULL;
8032 6458efa5 2020-11-24 stsp free(line);
8033 6458efa5 2020-11-24 stsp line = NULL;
8034 6458efa5 2020-11-24 stsp if (--limit <= 0)
8035 6458efa5 2020-11-24 stsp return NULL;
8038 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8039 6458efa5 2020-11-24 stsp char *line = NULL;
8040 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8042 84227eb1 2022-06-23 thomas if (s->show_date) {
8043 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8044 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8045 84227eb1 2022-06-23 thomas struct got_object_id *id;
8046 84227eb1 2022-06-23 thomas struct tm tm;
8047 84227eb1 2022-06-23 thomas time_t t;
8049 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8050 84227eb1 2022-06-23 thomas if (err)
8051 84227eb1 2022-06-23 thomas return err;
8052 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8053 84227eb1 2022-06-23 thomas if (err) {
8054 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8055 84227eb1 2022-06-23 thomas free(id);
8056 84227eb1 2022-06-23 thomas return err;
8058 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8060 84227eb1 2022-06-23 thomas if (err) {
8061 84227eb1 2022-06-23 thomas free(id);
8062 84227eb1 2022-06-23 thomas return err;
8064 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8065 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8066 84227eb1 2022-06-23 thomas } else {
8067 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8068 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8070 84227eb1 2022-06-23 thomas free(id);
8071 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8072 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8073 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8074 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8076 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8077 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8078 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8079 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8080 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8081 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8082 6458efa5 2020-11-24 stsp struct got_object_id *id;
8083 6458efa5 2020-11-24 stsp char *id_str;
8084 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8086 6458efa5 2020-11-24 stsp return err;
8087 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8088 6458efa5 2020-11-24 stsp if (err) {
8090 6458efa5 2020-11-24 stsp return err;
8092 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8093 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8094 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8096 6458efa5 2020-11-24 stsp free(id_str);
8097 6458efa5 2020-11-24 stsp return err;
8100 6458efa5 2020-11-24 stsp free(id_str);
8101 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8102 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8103 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8105 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8107 6458efa5 2020-11-24 stsp if (err) {
8108 6458efa5 2020-11-24 stsp free(line);
8109 6458efa5 2020-11-24 stsp return err;
8111 6458efa5 2020-11-24 stsp if (n == s->selected) {
8112 6458efa5 2020-11-24 stsp if (view->focussed)
8113 6458efa5 2020-11-24 stsp wstandout(view->window);
8114 6458efa5 2020-11-24 stsp s->selected_entry = re;
8116 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8118 6458efa5 2020-11-24 stsp wattr_on(view->window,
8119 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8120 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8122 6458efa5 2020-11-24 stsp wattr_off(view->window,
8123 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8124 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8125 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8126 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8127 6458efa5 2020-11-24 stsp wstandend(view->window);
8128 6458efa5 2020-11-24 stsp free(line);
8129 6458efa5 2020-11-24 stsp free(wline);
8130 6458efa5 2020-11-24 stsp wline = NULL;
8132 6458efa5 2020-11-24 stsp s->ndisplayed++;
8133 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8136 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8139 a5d43cac 2022-07-01 thomas view_border(view);
8140 6458efa5 2020-11-24 stsp return err;
8143 6458efa5 2020-11-24 stsp static const struct got_error *
8144 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8145 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8147 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8148 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8149 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8151 c42c9805 2020-11-24 stsp *new_view = NULL;
8153 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8154 c42c9805 2020-11-24 stsp if (err) {
8155 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8156 c42c9805 2020-11-24 stsp return err;
8158 c42c9805 2020-11-24 stsp return NULL;
8162 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8163 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8164 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8165 c42c9805 2020-11-24 stsp goto done;
8168 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8169 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8171 c42c9805 2020-11-24 stsp goto done;
8173 c42c9805 2020-11-24 stsp *new_view = tree_view;
8175 c42c9805 2020-11-24 stsp free(commit_id);
8176 c42c9805 2020-11-24 stsp return err;
8179 07dd3ed3 2022-08-06 thomas static const struct got_error *
8180 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8182 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8183 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8184 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8186 07dd3ed3 2022-08-06 thomas g = view->gline;
8187 07dd3ed3 2022-08-06 thomas view->gline = 0;
8189 07dd3ed3 2022-08-06 thomas if (g == 0)
8191 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8192 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8194 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8195 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8196 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8197 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8198 07dd3ed3 2022-08-06 thomas return NULL;
8201 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8202 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8203 07dd3ed3 2022-08-06 thomas if (err)
8204 07dd3ed3 2022-08-06 thomas return err;
8205 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8206 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8207 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8208 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8209 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8210 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8212 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8213 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8215 07dd3ed3 2022-08-06 thomas return NULL;
8219 c42c9805 2020-11-24 stsp static const struct got_error *
8220 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8222 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8223 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8224 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8225 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8227 07dd3ed3 2022-08-06 thomas if (view->gline)
8228 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8230 6458efa5 2020-11-24 stsp switch (ch) {
8232 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8233 07b0611c 2022-06-23 thomas view->count = 0;
8235 84227eb1 2022-06-23 thomas case 'm':
8236 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8237 07b0611c 2022-06-23 thomas view->count = 0;
8239 98182bd0 2021-11-20 thomas case 'o':
8240 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8241 07b0611c 2022-06-23 thomas view->count = 0;
8242 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8243 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8244 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8245 c591440f 2021-11-20 thomas if (err)
8247 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8248 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8249 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8250 3bfadbd4 2021-11-20 thomas if (err)
8252 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8253 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8255 6458efa5 2020-11-24 stsp case KEY_ENTER:
8256 6458efa5 2020-11-24 stsp case '\r':
8257 07b0611c 2022-06-23 thomas view->count = 0;
8258 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8260 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8262 1be4947a 2022-07-22 thomas case 'T':
8263 07b0611c 2022-06-23 thomas view->count = 0;
8264 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8266 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8268 e4526bf5 2021-09-03 naddy case 'g':
8269 aa7a1117 2023-01-09 thomas case '=':
8270 e4526bf5 2021-09-03 naddy case KEY_HOME:
8271 e4526bf5 2021-09-03 naddy s->selected = 0;
8272 07b0611c 2022-06-23 thomas view->count = 0;
8273 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8275 e4526bf5 2021-09-03 naddy case 'G':
8276 aa7a1117 2023-01-09 thomas case '*':
8277 a5d43cac 2022-07-01 thomas case KEY_END: {
8278 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8280 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8281 a5d43cac 2022-07-01 thomas --eos; /* border */
8282 e4526bf5 2021-09-03 naddy s->selected = 0;
8283 07b0611c 2022-06-23 thomas view->count = 0;
8284 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8285 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8286 e4526bf5 2021-09-03 naddy if (re == NULL)
8288 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8289 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8291 e4526bf5 2021-09-03 naddy if (n > 0)
8292 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8296 6458efa5 2020-11-24 stsp case KEY_UP:
8297 f7140bf5 2021-10-17 thomas case CTRL('p'):
8298 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8299 6458efa5 2020-11-24 stsp s->selected--;
8302 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8303 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8304 07b0611c 2022-06-23 thomas view->count = 0;
8306 70f17a53 2022-06-13 thomas case CTRL('u'):
8307 23427b14 2022-06-23 thomas case 'u':
8308 70f17a53 2022-06-13 thomas nscroll /= 2;
8309 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8310 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8311 6458efa5 2020-11-24 stsp case CTRL('b'):
8312 1c5e5faa 2022-06-23 thomas case 'b':
8313 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8314 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8315 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8316 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8317 07b0611c 2022-06-23 thomas view->count = 0;
8320 6458efa5 2020-11-24 stsp case KEY_DOWN:
8321 f7140bf5 2021-10-17 thomas case CTRL('n'):
8322 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8323 6458efa5 2020-11-24 stsp s->selected++;
8326 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8327 6458efa5 2020-11-24 stsp /* can't scroll any further */
8328 07b0611c 2022-06-23 thomas view->count = 0;
8331 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8333 70f17a53 2022-06-13 thomas case CTRL('d'):
8334 23427b14 2022-06-23 thomas case 'd':
8335 70f17a53 2022-06-13 thomas nscroll /= 2;
8336 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8337 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8338 6458efa5 2020-11-24 stsp case CTRL('f'):
8339 1c5e5faa 2022-06-23 thomas case 'f':
8340 4c2d69cb 2022-06-23 thomas case ' ':
8341 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8342 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8343 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8344 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8345 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8346 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8347 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8348 07b0611c 2022-06-23 thomas view->count = 0;
8351 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8353 6458efa5 2020-11-24 stsp case CTRL('l'):
8354 07b0611c 2022-06-23 thomas view->count = 0;
8355 8924d611 2020-12-26 stsp tog_free_refs();
8356 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8359 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8360 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8362 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8363 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8364 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8367 07b0611c 2022-06-23 thomas view->count = 0;
8371 6458efa5 2020-11-24 stsp return err;
8374 6458efa5 2020-11-24 stsp __dead static void
8375 6458efa5 2020-11-24 stsp usage_ref(void)
8378 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8379 6458efa5 2020-11-24 stsp getprogname());
8383 6458efa5 2020-11-24 stsp static const struct got_error *
8384 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8386 6458efa5 2020-11-24 stsp const struct got_error *error;
8387 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8388 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8389 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8391 6458efa5 2020-11-24 stsp struct tog_view *view;
8392 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8394 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8395 6458efa5 2020-11-24 stsp switch (ch) {
8397 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8398 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8399 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8403 e99e2d15 2020-11-24 naddy usage_ref();
8404 6458efa5 2020-11-24 stsp /* NOTREACHED */
8408 6458efa5 2020-11-24 stsp argc -= optind;
8409 6458efa5 2020-11-24 stsp argv += optind;
8411 6458efa5 2020-11-24 stsp if (argc > 1)
8412 e99e2d15 2020-11-24 naddy usage_ref();
8414 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8415 7cd52833 2022-06-23 thomas if (error != NULL)
8416 7cd52833 2022-06-23 thomas goto done;
8418 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8419 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8420 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8421 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8422 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8423 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8424 c156c7a4 2020-12-18 stsp goto done;
8425 6458efa5 2020-11-24 stsp if (worktree)
8426 6458efa5 2020-11-24 stsp repo_path =
8427 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8429 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8430 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8431 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8432 c156c7a4 2020-12-18 stsp goto done;
8436 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8437 6458efa5 2020-11-24 stsp if (error != NULL)
8438 6458efa5 2020-11-24 stsp goto done;
8440 6458efa5 2020-11-24 stsp init_curses();
8442 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8443 51a10b52 2020-12-26 stsp if (error)
8444 51a10b52 2020-12-26 stsp goto done;
8446 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8447 6458efa5 2020-11-24 stsp if (error)
8448 6458efa5 2020-11-24 stsp goto done;
8450 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8451 6458efa5 2020-11-24 stsp if (view == NULL) {
8452 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8453 6458efa5 2020-11-24 stsp goto done;
8456 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8457 6458efa5 2020-11-24 stsp if (error)
8458 6458efa5 2020-11-24 stsp goto done;
8460 6458efa5 2020-11-24 stsp if (worktree) {
8461 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8462 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8463 6458efa5 2020-11-24 stsp worktree = NULL;
8465 6458efa5 2020-11-24 stsp error = view_loop(view);
8467 6458efa5 2020-11-24 stsp free(repo_path);
8468 6458efa5 2020-11-24 stsp free(cwd);
8469 1d0f4054 2021-06-17 stsp if (repo) {
8470 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8471 1d0f4054 2021-06-17 stsp if (close_err)
8472 1d0f4054 2021-06-17 stsp error = close_err;
8474 7cd52833 2022-06-23 thomas if (pack_fds) {
8475 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8476 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8477 7cd52833 2022-06-23 thomas if (error == NULL)
8478 7cd52833 2022-06-23 thomas error = pack_err;
8480 51a10b52 2020-12-26 stsp tog_free_refs();
8481 6458efa5 2020-11-24 stsp return error;
8484 fc2737d5 2022-09-15 thomas static const struct got_error*
8485 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8486 fc2737d5 2022-09-15 thomas const char *str)
8488 fc2737d5 2022-09-15 thomas size_t len;
8490 fc2737d5 2022-09-15 thomas if (win == NULL)
8491 fc2737d5 2022-09-15 thomas win = stdscr;
8493 fc2737d5 2022-09-15 thomas len = strlen(str);
8494 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8496 fc2737d5 2022-09-15 thomas if (focus)
8497 fc2737d5 2022-09-15 thomas wstandout(win);
8498 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8499 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8500 fc2737d5 2022-09-15 thomas if (focus)
8501 fc2737d5 2022-09-15 thomas wstandend(win);
8503 fc2737d5 2022-09-15 thomas return NULL;
8506 2a31b33b 2022-07-23 thomas static const struct got_error *
8507 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8509 fc2737d5 2022-09-15 thomas off_t *p;
8511 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8512 fc2737d5 2022-09-15 thomas if (p == NULL) {
8513 fc2737d5 2022-09-15 thomas free(*line_offsets);
8514 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8515 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8518 fc2737d5 2022-09-15 thomas *line_offsets = p;
8519 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8520 fc2737d5 2022-09-15 thomas ++(*nlines);
8521 fc2737d5 2022-09-15 thomas return NULL;
8524 fc2737d5 2022-09-15 thomas static const struct got_error *
8525 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8527 fc2737d5 2022-09-15 thomas *ret = 0;
8529 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8530 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8531 fc2737d5 2022-09-15 thomas size_t len = 1;
8533 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8534 fc2737d5 2022-09-15 thomas continue;
8536 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8537 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8538 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8540 fc2737d5 2022-09-15 thomas len += strlen(t);
8541 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8542 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8543 fc2737d5 2022-09-15 thomas free(t0);
8544 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8547 fc2737d5 2022-09-15 thomas return NULL;
8551 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8552 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8553 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8555 fc2737d5 2022-09-15 thomas static const struct got_error *
8556 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8558 fc2737d5 2022-09-15 thomas int n, len = width;
8560 fc2737d5 2022-09-15 thomas if (km->keys) {
8561 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8562 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8563 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8565 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8566 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8568 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8570 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8571 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8572 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8574 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8575 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8576 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8577 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8578 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8579 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8580 fc2737d5 2022-09-15 thomas if (n < 0) {
8581 fc2737d5 2022-09-15 thomas free(t0);
8582 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8584 fc2737d5 2022-09-15 thomas first = 0;
8585 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8586 fc2737d5 2022-09-15 thomas *off += n;
8588 fc2737d5 2022-09-15 thomas free(t0);
8590 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8591 fc2737d5 2022-09-15 thomas if (n < 0)
8592 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8593 fc2737d5 2022-09-15 thomas *off += n;
8595 fc2737d5 2022-09-15 thomas return NULL;
8598 fc2737d5 2022-09-15 thomas static const struct got_error *
8599 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
8601 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8602 fc2737d5 2022-09-15 thomas off_t off = 0;
8603 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
8604 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
8605 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
8606 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8607 fc2737d5 2022-09-15 thomas GENERATE_HELP
8608 fc2737d5 2022-09-15 thomas #undef KEYMAP_
8609 fc2737d5 2022-09-15 thomas #undef KEY_
8612 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8613 fc2737d5 2022-09-15 thomas if (err)
8614 fc2737d5 2022-09-15 thomas return err;
8616 fc2737d5 2022-09-15 thomas n = nitems(km);
8617 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
8618 fc2737d5 2022-09-15 thomas if (err)
8619 fc2737d5 2022-09-15 thomas return err;
8621 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
8622 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
8623 fc2737d5 2022-09-15 thomas show = s->all;
8624 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
8625 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
8626 fc2737d5 2022-09-15 thomas show = 1;
8628 fc2737d5 2022-09-15 thomas if (show) {
8629 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
8630 fc2737d5 2022-09-15 thomas if (err)
8631 fc2737d5 2022-09-15 thomas return err;
8632 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8633 fc2737d5 2022-09-15 thomas if (err)
8634 fc2737d5 2022-09-15 thomas return err;
8637 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
8639 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8640 fc2737d5 2022-09-15 thomas return err;
8643 fc2737d5 2022-09-15 thomas static const struct got_error *
8644 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
8646 fc2737d5 2022-09-15 thomas FILE *f;
8647 fc2737d5 2022-09-15 thomas const struct got_error *err;
8649 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8650 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8651 fc2737d5 2022-09-15 thomas s->nlines = 0;
8653 fc2737d5 2022-09-15 thomas f = got_opentemp();
8654 fc2737d5 2022-09-15 thomas if (f == NULL)
8655 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
8656 fc2737d5 2022-09-15 thomas s->f = f;
8658 fc2737d5 2022-09-15 thomas err = format_help(s);
8659 fc2737d5 2022-09-15 thomas if (err)
8660 fc2737d5 2022-09-15 thomas return err;
8662 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
8663 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
8665 fc2737d5 2022-09-15 thomas return NULL;
8668 fc2737d5 2022-09-15 thomas static const struct got_error *
8669 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
8671 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
8672 fc2737d5 2022-09-15 thomas return NULL;
8675 2a7d3cd7 2022-09-17 thomas static void
8676 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8677 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
8679 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
8681 2a7d3cd7 2022-09-17 thomas *f = s->f;
8682 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
8683 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
8684 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
8685 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
8686 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
8687 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
8690 fc2737d5 2022-09-15 thomas static const struct got_error *
8691 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
8693 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8694 fc2737d5 2022-09-15 thomas const struct got_error *err;
8695 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
8696 fc2737d5 2022-09-15 thomas wchar_t *wline;
8697 fc2737d5 2022-09-15 thomas char *line;
8698 fc2737d5 2022-09-15 thomas ssize_t linelen;
8699 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8700 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
8701 fc2737d5 2022-09-15 thomas int eos = view->nlines;
8703 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8704 fc2737d5 2022-09-15 thomas --eos; /* account for border */
8706 fc2737d5 2022-09-15 thomas s->lineno = 0;
8707 fc2737d5 2022-09-15 thomas rewind(s->f);
8708 fc2737d5 2022-09-15 thomas werase(view->window);
8710 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
8711 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
8713 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
8714 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
8715 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
8716 fc2737d5 2022-09-15 thomas if (err)
8717 fc2737d5 2022-09-15 thomas return err;
8718 fc2737d5 2022-09-15 thomas if (eos <= 1)
8719 fc2737d5 2022-09-15 thomas return NULL;
8720 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
8721 fc2737d5 2022-09-15 thomas eos -= 2;
8723 fc2737d5 2022-09-15 thomas s->eof = 0;
8724 fc2737d5 2022-09-15 thomas view->maxx = 0;
8725 fc2737d5 2022-09-15 thomas line = NULL;
8726 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
8727 fc2737d5 2022-09-15 thomas attr_t attr = 0;
8729 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8730 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8731 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
8732 fc2737d5 2022-09-15 thomas free(line);
8733 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
8735 fc2737d5 2022-09-15 thomas s->eof = 1;
8738 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
8739 fc2737d5 2022-09-15 thomas continue;
8740 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8741 fc2737d5 2022-09-15 thomas continue;
8742 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
8743 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
8745 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8746 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
8747 fc2737d5 2022-09-15 thomas if (err) {
8748 fc2737d5 2022-09-15 thomas free(line);
8749 fc2737d5 2022-09-15 thomas return err;
8751 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
8752 fc2737d5 2022-09-15 thomas free(wline);
8753 fc2737d5 2022-09-15 thomas wline = NULL;
8755 fc2737d5 2022-09-15 thomas if (attr)
8756 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
8757 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
8758 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8759 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
8760 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
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 } else {
8766 fc2737d5 2022-09-15 thomas int skip;
8768 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
8769 fc2737d5 2022-09-15 thomas view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8770 fc2737d5 2022-09-15 thomas if (err) {
8771 fc2737d5 2022-09-15 thomas free(line);
8772 fc2737d5 2022-09-15 thomas return err;
8774 fc2737d5 2022-09-15 thomas rc = waddwstr(view->window, &wline[skip]);
8775 fc2737d5 2022-09-15 thomas free(wline);
8776 fc2737d5 2022-09-15 thomas wline = NULL;
8777 fc2737d5 2022-09-15 thomas if (rc == ERR)
8778 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "waddwstr");
8780 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
8781 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
8782 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
8783 fc2737d5 2022-09-15 thomas } else {
8784 fc2737d5 2022-09-15 thomas if (width <= view->ncols)
8785 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
8787 fc2737d5 2022-09-15 thomas if (attr)
8788 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
8789 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
8790 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
8792 fc2737d5 2022-09-15 thomas free(line);
8793 fc2737d5 2022-09-15 thomas if (nprinted > 0)
8794 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8796 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
8798 fc2737d5 2022-09-15 thomas view_border(view);
8800 fc2737d5 2022-09-15 thomas if (s->eof) {
8801 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
8802 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
8803 fc2737d5 2022-09-15 thomas view->ncols - 1);
8804 fc2737d5 2022-09-15 thomas if (rc == ERR)
8805 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8806 fc2737d5 2022-09-15 thomas } else {
8807 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
8808 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
8809 fc2737d5 2022-09-15 thomas wstandout(view->window);
8810 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
8811 fc2737d5 2022-09-15 thomas view->ncols - 1);
8812 fc2737d5 2022-09-15 thomas if (rc == ERR)
8813 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8814 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
8815 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
8816 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
8817 fc2737d5 2022-09-15 thomas if (rc == ERR)
8818 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
8820 fc2737d5 2022-09-15 thomas wstandend(view->window);
8823 fc2737d5 2022-09-15 thomas return NULL;
8826 fc2737d5 2022-09-15 thomas static const struct got_error *
8827 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8829 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8830 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8831 fc2737d5 2022-09-15 thomas char *line = NULL;
8832 fc2737d5 2022-09-15 thomas ssize_t linelen;
8833 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8834 fc2737d5 2022-09-15 thomas int eos, nscroll;
8836 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
8837 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8838 fc2737d5 2022-09-15 thomas --eos; /* border */
8840 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
8842 fc2737d5 2022-09-15 thomas switch (ch) {
8843 fc2737d5 2022-09-15 thomas case '0':
8844 fc2737d5 2022-09-15 thomas view->x = 0;
8846 fc2737d5 2022-09-15 thomas case '$':
8847 fc2737d5 2022-09-15 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
8848 fc2737d5 2022-09-15 thomas view->count = 0;
8850 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
8851 fc2737d5 2022-09-15 thomas case 'l':
8852 fc2737d5 2022-09-15 thomas if (view->x + view->ncols / 3 < view->maxx)
8853 fc2737d5 2022-09-15 thomas view->x += 2;
8855 fc2737d5 2022-09-15 thomas view->count = 0;
8857 fc2737d5 2022-09-15 thomas case KEY_LEFT:
8858 fc2737d5 2022-09-15 thomas case 'h':
8859 fc2737d5 2022-09-15 thomas view->x -= MIN(view->x, 2);
8860 fc2737d5 2022-09-15 thomas if (view->x <= 0)
8861 fc2737d5 2022-09-15 thomas view->count = 0;
8863 fc2737d5 2022-09-15 thomas case 'g':
8864 fc2737d5 2022-09-15 thomas case KEY_HOME:
8865 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8866 fc2737d5 2022-09-15 thomas view->count = 0;
8868 fc2737d5 2022-09-15 thomas case 'G':
8869 fc2737d5 2022-09-15 thomas case KEY_END:
8870 fc2737d5 2022-09-15 thomas view->count = 0;
8871 fc2737d5 2022-09-15 thomas if (s->eof)
8873 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
8874 fc2737d5 2022-09-15 thomas s->eof = 1;
8876 fc2737d5 2022-09-15 thomas case 'k':
8877 fc2737d5 2022-09-15 thomas case KEY_UP:
8878 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
8879 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
8881 fc2737d5 2022-09-15 thomas view->count = 0;
8883 fc2737d5 2022-09-15 thomas case CTRL('u'):
8884 fc2737d5 2022-09-15 thomas case 'u':
8885 fc2737d5 2022-09-15 thomas nscroll /= 2;
8886 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8887 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
8888 fc2737d5 2022-09-15 thomas case CTRL('b'):
8889 fc2737d5 2022-09-15 thomas case 'b':
8890 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
8891 fc2737d5 2022-09-15 thomas view->count = 0;
8894 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
8895 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
8897 fc2737d5 2022-09-15 thomas case 'j':
8898 fc2737d5 2022-09-15 thomas case KEY_DOWN:
8899 fc2737d5 2022-09-15 thomas case CTRL('n'):
8900 fc2737d5 2022-09-15 thomas if (!s->eof)
8901 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
8903 fc2737d5 2022-09-15 thomas view->count = 0;
8905 fc2737d5 2022-09-15 thomas case CTRL('d'):
8906 fc2737d5 2022-09-15 thomas case 'd':
8907 fc2737d5 2022-09-15 thomas nscroll /= 2;
8908 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8909 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
8910 fc2737d5 2022-09-15 thomas case CTRL('f'):
8911 fc2737d5 2022-09-15 thomas case 'f':
8912 fc2737d5 2022-09-15 thomas case ' ':
8913 fc2737d5 2022-09-15 thomas if (s->eof) {
8914 fc2737d5 2022-09-15 thomas view->count = 0;
8917 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
8918 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8919 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
8920 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8921 fc2737d5 2022-09-15 thomas if (feof(s->f))
8922 fc2737d5 2022-09-15 thomas s->eof = 1;
8924 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
8928 fc2737d5 2022-09-15 thomas free(line);
8930 fc2737d5 2022-09-15 thomas default:
8931 fc2737d5 2022-09-15 thomas view->count = 0;
8935 fc2737d5 2022-09-15 thomas return err;
8938 fc2737d5 2022-09-15 thomas static const struct got_error *
8939 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
8941 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8943 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8944 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8945 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
8946 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8948 fc2737d5 2022-09-15 thomas return NULL;
8951 fc2737d5 2022-09-15 thomas static const struct got_error *
8952 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
8954 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8957 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
8958 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8960 fc2737d5 2022-09-15 thomas wclear(view->window);
8961 fc2737d5 2022-09-15 thomas view->count = 0;
8962 fc2737d5 2022-09-15 thomas view->x = 0;
8963 fc2737d5 2022-09-15 thomas s->all = !s->all;
8964 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8965 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
8966 fc2737d5 2022-09-15 thomas s->matched_line = 0;
8968 fc2737d5 2022-09-15 thomas return create_help(s);
8971 fc2737d5 2022-09-15 thomas static const struct got_error *
8972 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
8974 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8975 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8977 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
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->selected_line = 1;
8982 fc2737d5 2022-09-15 thomas view->show = show_help_view;
8983 fc2737d5 2022-09-15 thomas view->input = input_help_view;
8984 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
8985 fc2737d5 2022-09-15 thomas view->close = close_help_view;
8986 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
8987 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
8988 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
8990 fc2737d5 2022-09-15 thomas err = create_help(s);
8991 fc2737d5 2022-09-15 thomas return err;
8994 fc2737d5 2022-09-15 thomas static const struct got_error *
8995 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8996 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8998 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9000 2a31b33b 2022-07-23 thomas *new_view = NULL;
9002 2a31b33b 2022-07-23 thomas switch (request) {
9003 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9004 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9005 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9007 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9008 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9009 2a31b33b 2022-07-23 thomas view, s->repo);
9011 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9012 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9014 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9015 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9016 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9018 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9019 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9020 2a31b33b 2022-07-23 thomas s->repo);
9022 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9023 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9025 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9026 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9027 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9028 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9029 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9030 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9031 2a31b33b 2022-07-23 thomas &view->state.tree);
9032 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9033 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9034 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9035 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9037 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9038 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9040 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9041 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9042 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9043 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9044 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9045 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9046 2a31b33b 2022-07-23 thomas view->state.log.repo);
9047 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9048 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9049 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9050 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9052 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9053 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9055 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9056 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9057 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9058 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9059 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9060 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9061 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9062 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9064 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9065 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9066 2a31b33b 2022-07-23 thomas if (err)
9067 2a31b33b 2022-07-23 thomas view_close(*new_view);
9069 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9070 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9071 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9072 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9073 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9074 fc2737d5 2022-09-15 thomas if (err)
9075 fc2737d5 2022-09-15 thomas view_close(*new_view);
9077 2a31b33b 2022-07-23 thomas default:
9078 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9081 2a31b33b 2022-07-23 thomas return err;
9085 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9086 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9088 6458efa5 2020-11-24 stsp static void
9089 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9091 a5d43cac 2022-07-01 thomas switch (view->type) {
9092 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9093 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9094 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9095 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9099 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9100 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9102 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9103 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9106 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9107 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9108 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9110 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9111 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9112 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9114 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9115 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9116 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9118 a5d43cac 2022-07-01 thomas default:
9122 a5d43cac 2022-07-01 thomas view->offset = 0;
9126 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9127 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9129 a5d43cac 2022-07-01 thomas static const struct got_error *
9130 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9132 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9133 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9134 a5d43cac 2022-07-01 thomas int *selected = NULL;
9135 a5d43cac 2022-07-01 thomas int header, offset;
9137 a5d43cac 2022-07-01 thomas switch (view->type) {
9138 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9139 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9140 a5d43cac 2022-07-01 thomas header = 3;
9141 a5d43cac 2022-07-01 thomas scrolld = NULL;
9142 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9143 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9144 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9145 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9146 a5d43cac 2022-07-01 thomas view->offset = offset;
9150 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9151 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9152 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9153 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9154 a5d43cac 2022-07-01 thomas selected = &s->selected;
9157 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9158 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9159 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9160 a5d43cac 2022-07-01 thomas header = 3;
9161 a5d43cac 2022-07-01 thomas selected = &s->selected;
9164 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9165 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9166 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9167 a5d43cac 2022-07-01 thomas header = 5;
9168 a5d43cac 2022-07-01 thomas selected = &s->selected;
9171 a5d43cac 2022-07-01 thomas default:
9172 a5d43cac 2022-07-01 thomas selected = NULL;
9173 a5d43cac 2022-07-01 thomas scrolld = NULL;
9174 a5d43cac 2022-07-01 thomas header = 0;
9178 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9179 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9180 a5d43cac 2022-07-01 thomas view->offset = offset;
9181 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9182 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9183 a5d43cac 2022-07-01 thomas *selected -= offset;
9187 a5d43cac 2022-07-01 thomas return err;
9190 a5d43cac 2022-07-01 thomas static void
9191 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9195 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9196 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9197 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9198 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9200 6879ba42 2020-10-01 naddy fputc('\n', fp);
9203 4ed7e80c 2018-05-20 stsp __dead static void
9204 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9206 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9208 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9209 6879ba42 2020-10-01 naddy getprogname());
9210 6879ba42 2020-10-01 naddy if (hflag) {
9211 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9212 6879ba42 2020-10-01 naddy list_commands(fp);
9214 6879ba42 2020-10-01 naddy exit(status);
9217 c2301be8 2018-04-30 stsp static char **
9218 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9220 ee85c5e8 2020-02-29 stsp va_list ap;
9221 c2301be8 2018-04-30 stsp char **argv;
9224 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9226 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9227 c2301be8 2018-04-30 stsp if (argv == NULL)
9228 c2301be8 2018-04-30 stsp err(1, "calloc");
9229 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9230 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9231 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9232 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9235 ee85c5e8 2020-02-29 stsp va_end(ap);
9236 c2301be8 2018-04-30 stsp return argv;
9240 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9241 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9242 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9243 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9244 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9246 ee85c5e8 2020-02-29 stsp static const struct got_error *
9247 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9249 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9250 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9251 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9252 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9253 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9254 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9255 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9256 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9257 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9259 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9260 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9261 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9263 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9264 7cd52833 2022-06-23 thomas if (error != NULL)
9265 7cd52833 2022-06-23 thomas goto done;
9267 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9268 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9269 ee85c5e8 2020-02-29 stsp goto done;
9271 ee85c5e8 2020-02-29 stsp if (worktree)
9272 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9274 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9275 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9276 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9277 ee85c5e8 2020-02-29 stsp goto done;
9280 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9281 ee85c5e8 2020-02-29 stsp if (error != NULL)
9282 ee85c5e8 2020-02-29 stsp goto done;
9284 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9285 ee85c5e8 2020-02-29 stsp repo, worktree);
9286 ee85c5e8 2020-02-29 stsp if (error)
9287 ee85c5e8 2020-02-29 stsp goto done;
9289 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9290 84de9106 2020-12-26 stsp if (error)
9291 84de9106 2020-12-26 stsp goto done;
9292 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9293 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9294 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9295 ee85c5e8 2020-02-29 stsp if (error)
9296 ee85c5e8 2020-02-29 stsp goto done;
9298 ee85c5e8 2020-02-29 stsp if (worktree) {
9299 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9300 ee85c5e8 2020-02-29 stsp worktree = NULL;
9303 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9304 945f9229 2022-04-16 thomas if (error)
9305 945f9229 2022-04-16 thomas goto done;
9307 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9308 ee85c5e8 2020-02-29 stsp if (error) {
9309 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9310 ee85c5e8 2020-02-29 stsp goto done;
9311 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9312 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9313 6879ba42 2020-10-01 naddy usage(1, 1);
9314 ee85c5e8 2020-02-29 stsp /* not reached */
9317 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9318 ee85c5e8 2020-02-29 stsp if (error)
9319 ee85c5e8 2020-02-29 stsp goto done;
9321 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9323 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9324 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9326 1d0f4054 2021-06-17 stsp if (repo) {
9327 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9328 1d0f4054 2021-06-17 stsp if (error == NULL)
9329 1d0f4054 2021-06-17 stsp error = close_err;
9331 945f9229 2022-04-16 thomas if (commit)
9332 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9333 ee85c5e8 2020-02-29 stsp if (worktree)
9334 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9335 7cd52833 2022-06-23 thomas if (pack_fds) {
9336 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9337 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9338 7cd52833 2022-06-23 thomas if (error == NULL)
9339 7cd52833 2022-06-23 thomas error = pack_err;
9342 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9343 ee85c5e8 2020-02-29 stsp free(commit_id);
9344 ee85c5e8 2020-02-29 stsp free(cwd);
9345 ee85c5e8 2020-02-29 stsp free(repo_path);
9346 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9347 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9349 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9350 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9351 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9353 87670572 2020-12-26 naddy tog_free_refs();
9354 ee85c5e8 2020-02-29 stsp return error;
9358 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9360 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9361 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9362 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9363 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9364 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9365 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9366 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9368 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9370 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9371 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9373 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9375 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9376 9f7d7167 2018-04-29 stsp switch (ch) {
9378 9f7d7167 2018-04-29 stsp hflag = 1;
9381 53ccebc2 2019-07-30 stsp Vflag = 1;
9384 6879ba42 2020-10-01 naddy usage(hflag, 1);
9385 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9389 9f7d7167 2018-04-29 stsp argc -= optind;
9390 9f7d7167 2018-04-29 stsp argv += optind;
9391 9814e6a3 2020-09-27 naddy optind = 1;
9392 c2301be8 2018-04-30 stsp optreset = 1;
9394 53ccebc2 2019-07-30 stsp if (Vflag) {
9395 53ccebc2 2019-07-30 stsp got_version_print_str();
9396 6879ba42 2020-10-01 naddy return 0;
9399 4010e238 2020-12-04 stsp #ifndef PROFILE
9400 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9401 4010e238 2020-12-04 stsp NULL) == -1)
9402 4010e238 2020-12-04 stsp err(1, "pledge");
9405 c2301be8 2018-04-30 stsp if (argc == 0) {
9406 f29d3e89 2018-06-23 stsp if (hflag)
9407 6879ba42 2020-10-01 naddy usage(hflag, 0);
9408 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9409 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9411 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9415 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9416 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9417 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9418 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9419 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9425 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9426 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9427 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9428 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9429 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9430 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9433 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9434 ee85c5e8 2020-02-29 stsp if (argc != 1)
9435 6879ba42 2020-10-01 naddy usage(0, 1);
9436 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9437 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9439 ee85c5e8 2020-02-29 stsp if (hflag)
9440 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9442 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9446 b46c1e04 2020-09-20 naddy putchar('\n');
9447 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9449 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9450 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9451 a2f4a359 2020-02-28 stsp free(cmd_argv);
9454 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9455 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9456 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9457 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9458 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9459 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);