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 80ddbec8 2018-04-29 stsp #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 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
106 ec2a9698 2022-09-15 mark TOG_VIEW_HELP
109 ec2a9698 2022-09-15 mark /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
110 ec2a9698 2022-09-15 mark enum tog_keymap_type {
111 ec2a9698 2022-09-15 mark TOG_KEYMAP_KEYS = -2,
112 ec2a9698 2022-09-15 mark TOG_KEYMAP_GLOBAL,
113 ec2a9698 2022-09-15 mark TOG_KEYMAP_DIFF,
114 ec2a9698 2022-09-15 mark TOG_KEYMAP_LOG,
115 ec2a9698 2022-09-15 mark TOG_KEYMAP_BLAME,
116 ec2a9698 2022-09-15 mark TOG_KEYMAP_TREE,
117 ec2a9698 2022-09-15 mark TOG_KEYMAP_REF,
118 ec2a9698 2022-09-15 mark TOG_KEYMAP_HELP
121 9b058f45 2022-06-30 mark enum tog_view_mode {
122 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
123 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
124 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
127 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
129 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
131 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
132 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
133 ba4f502b 2018-08-04 stsp struct got_object_id *id;
134 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
137 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
138 ba4f502b 2018-08-04 stsp struct commit_queue {
139 ba4f502b 2018-08-04 stsp int ncommits;
140 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
143 f26dddb7 2019-11-08 stsp struct tog_color {
144 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
145 6d17833f 2019-11-08 stsp regex_t regex;
146 6d17833f 2019-11-08 stsp short colorpair;
148 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
150 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
151 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
152 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
154 cc488aa7 2022-01-23 stsp static const struct got_error *
155 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
156 cc488aa7 2022-01-23 stsp struct got_reference* re2)
158 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
159 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
160 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
162 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
163 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
164 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
165 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
167 cc488aa7 2022-01-23 stsp return NULL;
168 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
170 cc488aa7 2022-01-23 stsp return NULL;
173 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
174 cc488aa7 2022-01-23 stsp return NULL;
177 11b20872 2019-11-08 stsp static const struct got_error *
178 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
180 51a10b52 2020-12-26 stsp const struct got_error *err;
182 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
183 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
186 51a10b52 2020-12-26 stsp return err;
188 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
192 51a10b52 2020-12-26 stsp static void
193 51a10b52 2020-12-26 stsp tog_free_refs(void)
195 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
196 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
197 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
199 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
202 51a10b52 2020-12-26 stsp static const struct got_error *
203 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
204 11b20872 2019-11-08 stsp int idx, short color)
206 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
207 11b20872 2019-11-08 stsp struct tog_color *tc;
208 11b20872 2019-11-08 stsp int regerr = 0;
210 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
211 11b20872 2019-11-08 stsp return NULL;
213 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
215 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
216 11b20872 2019-11-08 stsp if (tc == NULL)
217 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
218 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
219 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
220 11b20872 2019-11-08 stsp if (regerr) {
221 11b20872 2019-11-08 stsp static char regerr_msg[512];
222 11b20872 2019-11-08 stsp static char err_msg[512];
223 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
224 11b20872 2019-11-08 stsp sizeof(regerr_msg));
225 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
226 11b20872 2019-11-08 stsp regerr_msg);
227 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
229 11b20872 2019-11-08 stsp return err;
231 11b20872 2019-11-08 stsp tc->colorpair = idx;
232 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
233 11b20872 2019-11-08 stsp return NULL;
236 11b20872 2019-11-08 stsp static void
237 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
239 11b20872 2019-11-08 stsp struct tog_color *tc;
241 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
242 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
243 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
244 11b20872 2019-11-08 stsp regfree(&tc->regex);
249 336075a4 2022-06-25 op static struct tog_color *
250 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
252 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
254 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
255 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
259 11b20872 2019-11-08 stsp return NULL;
263 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
268 11b20872 2019-11-08 stsp return COLOR_CYAN;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
270 11b20872 2019-11-08 stsp return COLOR_YELLOW;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
274 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
276 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
278 91b8c405 2020-01-25 stsp return COLOR_CYAN;
279 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
280 11b20872 2019-11-08 stsp return COLOR_GREEN;
281 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
282 11b20872 2019-11-08 stsp return COLOR_GREEN;
283 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
284 11b20872 2019-11-08 stsp return COLOR_CYAN;
285 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
286 11b20872 2019-11-08 stsp return COLOR_YELLOW;
287 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
288 6458efa5 2020-11-24 stsp return COLOR_GREEN;
289 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
290 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
291 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
292 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
293 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
294 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
300 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
302 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
304 11b20872 2019-11-08 stsp if (val == NULL)
305 11b20872 2019-11-08 stsp return default_color_value(envvar);
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLACK;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
310 11b20872 2019-11-08 stsp return COLOR_RED;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
312 11b20872 2019-11-08 stsp return COLOR_GREEN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
314 11b20872 2019-11-08 stsp return COLOR_YELLOW;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
316 11b20872 2019-11-08 stsp return COLOR_BLUE;
317 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
318 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
319 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
320 11b20872 2019-11-08 stsp return COLOR_CYAN;
321 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
322 11b20872 2019-11-08 stsp return COLOR_WHITE;
323 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
326 11b20872 2019-11-08 stsp return default_color_value(envvar);
329 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
330 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
331 3dbaef42 2020-11-24 stsp const char *label1, *label2;
332 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
333 f9d37699 2022-06-28 stsp int fd1, fd2;
334 c7d5c43c 2022-08-04 mark int lineno;
335 15a087fe 2019-02-21 stsp int first_displayed_line;
336 15a087fe 2019-02-21 stsp int last_displayed_line;
338 15a087fe 2019-02-21 stsp int diff_context;
339 3dbaef42 2020-11-24 stsp int ignore_whitespace;
340 64453f7e 2020-11-21 stsp int force_text_diff;
341 15a087fe 2019-02-21 stsp struct got_repository *repo;
342 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
343 fe621944 2020-11-10 stsp size_t nlines;
344 f44b1f58 2020-02-02 tracey int matched_line;
345 f44b1f58 2020-02-02 tracey int selected_line;
347 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
348 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
351 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
352 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
354 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
355 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
356 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
357 1a76625f 2018-10-22 stsp int commits_needed;
358 fb280deb 2021-08-30 stsp int load_all;
359 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
360 568eae95 2022-09-11 mark struct commit_queue *real_commits;
361 1a76625f 2018-10-22 stsp const char *in_repo_path;
362 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
363 1a76625f 2018-10-22 stsp struct got_repository *repo;
364 74467cc8 2022-06-15 stsp int *pack_fds;
365 1a76625f 2018-10-22 stsp int log_complete;
366 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
367 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
368 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
369 13add988 2019-10-15 stsp int *searching;
370 13add988 2019-10-15 stsp int *search_next_done;
371 13add988 2019-10-15 stsp regex_t *regex;
372 568eae95 2022-09-11 mark int *limiting;
373 568eae95 2022-09-11 mark int limit_match;
374 568eae95 2022-09-11 mark regex_t *limit_regex;
375 568eae95 2022-09-11 mark struct commit_queue *limit_commits;
378 1a76625f 2018-10-22 stsp struct tog_log_view_state {
379 568eae95 2022-09-11 mark struct commit_queue *commits;
380 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
381 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
382 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
383 568eae95 2022-09-11 mark struct commit_queue real_commits;
384 b01e7d3b 2018-08-04 stsp int selected;
385 b01e7d3b 2018-08-04 stsp char *in_repo_path;
386 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
387 b672a97a 2020-01-27 stsp int log_branches;
388 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
389 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
390 1a76625f 2018-10-22 stsp sig_atomic_t quit;
391 1a76625f 2018-10-22 stsp pthread_t thread;
392 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
393 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
394 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
395 11b20872 2019-11-08 stsp struct tog_colors colors;
396 10aab77f 2022-07-19 op int use_committer;
397 568eae95 2022-09-11 mark int limit_view;
398 568eae95 2022-09-11 mark regex_t limit_regex;
399 568eae95 2022-09-11 mark struct commit_queue limit_commits;
402 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
403 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
404 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
405 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
406 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
407 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
408 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
409 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
410 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
411 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
412 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
413 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
414 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
415 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
416 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
418 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
419 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
420 e9424729 2018-08-04 stsp int nlines;
422 e9424729 2018-08-04 stsp struct tog_view *view;
423 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
427 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
428 e9424729 2018-08-04 stsp const char *path;
429 e9424729 2018-08-04 stsp struct got_repository *repo;
430 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
431 e9424729 2018-08-04 stsp int *complete;
432 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
433 fc06ba56 2019-08-22 stsp void *cancel_arg;
436 e9424729 2018-08-04 stsp struct tog_blame {
438 be659d10 2020-11-18 stsp off_t filesize;
439 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
440 6fcac457 2018-11-19 stsp int nlines;
441 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
442 e9424729 2018-08-04 stsp pthread_t thread;
443 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
444 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
445 e9424729 2018-08-04 stsp const char *path;
446 0ae84acc 2022-06-15 tracey int *pack_fds;
449 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
450 7cbe629d 2018-08-04 stsp int first_displayed_line;
451 7cbe629d 2018-08-04 stsp int last_displayed_line;
452 7cbe629d 2018-08-04 stsp int selected_line;
453 c0f61fa4 2022-07-11 mark int last_diffed_line;
454 7cbe629d 2018-08-04 stsp int blame_complete;
457 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
458 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
459 e5a0f69f 2018-08-18 stsp char *path;
460 7cbe629d 2018-08-04 stsp struct got_repository *repo;
461 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
462 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
463 e9424729 2018-08-04 stsp struct tog_blame blame;
464 6c4c42e0 2019-06-24 stsp int matched_line;
465 11b20872 2019-11-08 stsp struct tog_colors colors;
468 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
469 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
470 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
471 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
472 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
473 ad80ab7b 2018-08-04 stsp int selected;
476 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
478 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
479 ad80ab7b 2018-08-04 stsp char *tree_label;
480 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
481 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
482 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
483 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
484 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
486 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
487 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
488 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
489 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
490 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
491 6458efa5 2020-11-24 stsp struct tog_colors colors;
494 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
495 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
496 6458efa5 2020-11-24 stsp struct got_reference *ref;
500 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
502 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
503 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
504 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
506 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
507 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
508 6458efa5 2020-11-24 stsp struct got_repository *repo;
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
510 bddb1296 2019-11-08 stsp struct tog_colors colors;
513 ec2a9698 2022-09-15 mark struct tog_help_view_state {
515 ec2a9698 2022-09-15 mark off_t *line_offsets;
516 ec2a9698 2022-09-15 mark size_t nlines;
517 ec2a9698 2022-09-15 mark int lineno;
518 ec2a9698 2022-09-15 mark int first_displayed_line;
519 ec2a9698 2022-09-15 mark int last_displayed_line;
521 ec2a9698 2022-09-15 mark int matched_line;
522 ec2a9698 2022-09-15 mark int selected_line;
524 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
527 ec2a9698 2022-09-15 mark #define GENERATE_HELP \
528 ec2a9698 2022-09-15 mark KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
529 ec2a9698 2022-09-15 mark KEY_("H F1", "Open view-specific help (double tap for all help)"), \
530 ec2a9698 2022-09-15 mark KEY_("k C-p Up", "Move cursor or page up one line"), \
531 ec2a9698 2022-09-15 mark KEY_("j C-n Down", "Move cursor or page down one line"), \
532 ec2a9698 2022-09-15 mark KEY_("C-b b PgUp", "Scroll the view up one page"), \
533 ec2a9698 2022-09-15 mark KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
534 ec2a9698 2022-09-15 mark KEY_("C-u u", "Scroll the view up one half page"), \
535 ec2a9698 2022-09-15 mark KEY_("C-d d", "Scroll the view down one half page"), \
536 ec2a9698 2022-09-15 mark KEY_("g Home", "Go to line N (default: first line)"), \
537 ec2a9698 2022-09-15 mark KEY_("G End", "Go to line N (default: last line)"), \
538 ec2a9698 2022-09-15 mark KEY_("l Right", "Scroll the view right"), \
539 ec2a9698 2022-09-15 mark KEY_("h Left", "Scroll the view left"), \
540 ec2a9698 2022-09-15 mark KEY_("$", "Scroll view to the rightmost position"), \
541 ec2a9698 2022-09-15 mark KEY_("0", "Scroll view to the leftmost position"), \
542 ec2a9698 2022-09-15 mark KEY_("-", "Decrease size of the focussed split"), \
543 ec2a9698 2022-09-15 mark KEY_("+", "Increase size of the focussed split"), \
544 ec2a9698 2022-09-15 mark KEY_("Tab", "Switch focus between views"), \
545 ec2a9698 2022-09-15 mark KEY_("F", "Toggle fullscreen mode"), \
546 ec2a9698 2022-09-15 mark KEY_("/", "Open prompt to enter search term"), \
547 ec2a9698 2022-09-15 mark KEY_("n", "Find next line/token matching the current search term"), \
548 ec2a9698 2022-09-15 mark KEY_("N", "Find previous line/token matching the current search term"),\
549 16a2d4f0 2022-09-19 stsp KEY_("q", "Quit the focussed view; Quit help screen"), \
550 ec2a9698 2022-09-15 mark KEY_("Q", "Quit tog"), \
552 fd6badaa 2022-09-23 stsp KEYMAP_("Log view", TOG_KEYMAP_LOG), \
553 ec2a9698 2022-09-15 mark KEY_("< ,", "Move cursor up one commit"), \
554 ec2a9698 2022-09-15 mark KEY_("> .", "Move cursor down one commit"), \
555 ec2a9698 2022-09-15 mark KEY_("Enter", "Open diff view of the selected commit"), \
556 ec2a9698 2022-09-15 mark KEY_("B", "Reload the log view and toggle display of merged commits"), \
557 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
558 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the repository from the selected" \
559 ec2a9698 2022-09-15 mark " commit"), \
560 ec2a9698 2022-09-15 mark KEY_("@", "Toggle between displaying author and committer name"), \
561 ec2a9698 2022-09-15 mark KEY_("&", "Open prompt to enter term to limit commits displayed"), \
562 ec2a9698 2022-09-15 mark KEY_("C-g Backspace", "Cancel current search or log operation"), \
563 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload the log view with new commits in the repository"), \
565 fd6badaa 2022-09-23 stsp KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
566 ec2a9698 2022-09-15 mark KEY_("K < ,", "Display diff of next line in the file/log entry"), \
567 ec2a9698 2022-09-15 mark KEY_("J > .", "Display diff of previous line in the file/log entry"), \
568 ec2a9698 2022-09-15 mark KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
569 ec2a9698 2022-09-15 mark KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
570 ec2a9698 2022-09-15 mark " data"), \
571 ec2a9698 2022-09-15 mark KEY_("(", "Go to the previous file in the diff"), \
572 ec2a9698 2022-09-15 mark KEY_(")", "Go to the next file in the diff"), \
573 ec2a9698 2022-09-15 mark KEY_("{", "Go to the previous hunk in the diff"), \
574 ec2a9698 2022-09-15 mark KEY_("}", "Go to the next hunk in the diff"), \
575 ec2a9698 2022-09-15 mark KEY_("[", "Decrease the number of context lines"), \
576 ec2a9698 2022-09-15 mark KEY_("]", "Increase the number of context lines"), \
577 ec2a9698 2022-09-15 mark KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
579 fd6badaa 2022-09-23 stsp KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
580 ec2a9698 2022-09-15 mark KEY_("Enter", "Display diff view of the selected line's commit"), \
581 ec2a9698 2022-09-15 mark KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
582 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the currently selected annotated line"), \
583 ec2a9698 2022-09-15 mark KEY_("C", "Reload view with the previously blamed commit"), \
584 ec2a9698 2022-09-15 mark KEY_("c", "Reload view with the version of the file found in the" \
585 ec2a9698 2022-09-15 mark " selected line's commit"), \
586 ec2a9698 2022-09-15 mark KEY_("p", "Reload view with the version of the file found in the" \
587 ec2a9698 2022-09-15 mark " selected line's parent commit"), \
589 fd6badaa 2022-09-23 stsp KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
590 ec2a9698 2022-09-15 mark KEY_("Enter", "Enter selected directory or open blame view of the" \
591 ec2a9698 2022-09-15 mark " selected file"), \
592 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the selected entry"), \
593 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
594 ec2a9698 2022-09-15 mark KEY_("i", "Show object IDs for all tree entries"), \
595 ec2a9698 2022-09-15 mark KEY_("Backspace", "Return to the parent directory"), \
597 fd6badaa 2022-09-23 stsp KEYMAP_("Ref view", TOG_KEYMAP_REF), \
598 ec2a9698 2022-09-15 mark KEY_("Enter", "Display log view of the selected reference"), \
599 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the selected reference"), \
600 ec2a9698 2022-09-15 mark KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
601 ec2a9698 2022-09-15 mark KEY_("m", "Toggle display of last modified date for each reference"), \
602 ec2a9698 2022-09-15 mark KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
603 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload view with all repository references")
605 ec2a9698 2022-09-15 mark struct tog_key_map {
606 ec2a9698 2022-09-15 mark const char *keys;
607 ec2a9698 2022-09-15 mark const char *info;
608 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
612 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
614 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
615 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
616 669b5ffa 2018-10-07 stsp * there is enough screen estate.
618 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
619 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
621 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
622 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
623 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
625 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
626 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
628 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
630 cc3c9aac 2018-08-01 stsp struct tog_view {
631 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
632 26ed57b2 2018-05-19 stsp WINDOW *window;
633 26ed57b2 2018-05-19 stsp PANEL *panel;
634 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
635 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
636 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
637 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
638 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
639 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
640 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
641 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
642 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
644 669b5ffa 2018-10-07 stsp struct tog_view *parent;
645 669b5ffa 2018-10-07 stsp struct tog_view *child;
648 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
649 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
650 e78dc838 2020-12-04 stsp * between parent and child.
651 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
652 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
653 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
654 e78dc838 2020-12-04 stsp * situations.
656 e78dc838 2020-12-04 stsp int focus_child;
658 9b058f45 2022-06-30 mark enum tog_view_mode mode;
659 5dc9f4bc 2018-08-04 stsp /* type-specific state */
660 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
662 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
663 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
664 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
665 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
666 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
667 ec2a9698 2022-09-15 mark struct tog_help_view_state help;
670 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
671 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
672 e78dc838 2020-12-04 stsp struct tog_view *, int);
673 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
674 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
675 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
677 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
678 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
679 eaf2c13e 2022-09-17 mark void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
680 eaf2c13e 2022-09-17 mark int **, int **, int **, int **);
681 c0c4acc8 2021-01-24 stsp int search_started;
682 60493ae3 2019-06-20 stsp int searching;
683 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
684 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
685 60493ae3 2019-06-20 stsp int search_next_done;
686 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
687 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
688 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
689 1803e47f 2019-06-22 stsp regex_t regex;
690 41605754 2020-11-12 stsp regmatch_t regmatch;
693 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
694 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
695 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
696 78756c87 2020-11-24 stsp struct got_repository *);
697 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
698 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
699 e78dc838 2020-12-04 stsp struct tog_view *, int);
700 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
701 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
702 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
703 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
704 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
705 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
707 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
708 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
709 78756c87 2020-11-24 stsp const char *, const char *, int);
710 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
711 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
712 e78dc838 2020-12-04 stsp struct tog_view *, int);
713 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
714 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
715 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
716 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
718 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
719 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
720 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
721 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
722 e78dc838 2020-12-04 stsp struct tog_view *, int);
723 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
724 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
725 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
726 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
727 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
729 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
730 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
731 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
732 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
733 e78dc838 2020-12-04 stsp struct tog_view *, int);
734 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
735 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
736 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
738 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
739 6458efa5 2020-11-24 stsp struct got_repository *);
740 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
741 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
742 e78dc838 2020-12-04 stsp struct tog_view *, int);
743 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
744 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
745 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
747 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
748 eaf2c13e 2022-09-17 mark struct tog_view *);
749 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
750 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
751 eaf2c13e 2022-09-17 mark struct tog_view *, int);
752 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
753 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
754 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
755 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
756 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
758 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
759 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
760 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
761 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
762 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
764 25791caa 2018-10-24 stsp static void
765 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
767 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
770 83baff54 2019-08-12 stsp static void
771 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
773 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
776 61266923 2020-01-14 stsp static void
777 61266923 2020-01-14 stsp tog_sigcont(int signo)
779 61266923 2020-01-14 stsp tog_sigcont_received = 1;
782 2497f032 2022-05-31 stsp static void
783 2497f032 2022-05-31 stsp tog_sigint(int signo)
785 2497f032 2022-05-31 stsp tog_sigint_received = 1;
788 2497f032 2022-05-31 stsp static void
789 2497f032 2022-05-31 stsp tog_sigterm(int signo)
791 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
795 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
797 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
798 f044c841 2022-10-24 stsp tog_sigint_received || tog_sigterm_received);
801 e5a0f69f 2018-08-18 stsp static const struct got_error *
802 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
804 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
806 669b5ffa 2018-10-07 stsp if (view->child) {
807 5629093a 2022-07-11 stsp child_err = view_close(view->child);
808 669b5ffa 2018-10-07 stsp view->child = NULL;
810 e5a0f69f 2018-08-18 stsp if (view->close)
811 e5a0f69f 2018-08-18 stsp err = view->close(view);
812 ea5e7bb5 2018-08-01 stsp if (view->panel)
813 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
814 ea5e7bb5 2018-08-01 stsp if (view->window)
815 ea5e7bb5 2018-08-01 stsp delwin(view->window);
816 ea5e7bb5 2018-08-01 stsp free(view);
817 5629093a 2022-07-11 stsp return err ? err : child_err;
820 ea5e7bb5 2018-08-01 stsp static struct tog_view *
821 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
822 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
824 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
826 ea5e7bb5 2018-08-01 stsp if (view == NULL)
827 ea5e7bb5 2018-08-01 stsp return NULL;
829 d6b05b5b 2018-08-04 stsp view->type = type;
830 f7d12f7e 2018-08-01 stsp view->lines = LINES;
831 f7d12f7e 2018-08-01 stsp view->cols = COLS;
832 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
833 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
834 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
835 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
836 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
837 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
838 96a765a8 2018-08-04 stsp view_close(view);
839 ea5e7bb5 2018-08-01 stsp return NULL;
841 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
842 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
843 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
844 96a765a8 2018-08-04 stsp view_close(view);
845 ea5e7bb5 2018-08-01 stsp return NULL;
848 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
849 ea5e7bb5 2018-08-01 stsp return view;
853 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
855 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
857 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
860 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
862 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
864 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
867 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
869 5c60c32a 2018-10-18 stsp static const struct got_error *
870 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
872 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
874 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
875 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
876 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
878 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
879 9b058f45 2022-06-30 mark view->begin_x = 0;
880 571ccd73 2022-07-19 mark } else if (!view->resized) {
881 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
882 3c1dfe12 2022-07-08 mark view->cols > 119)
883 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
885 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
886 9b058f45 2022-06-30 mark view->begin_y = 0;
888 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
889 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
890 5c60c32a 2018-10-18 stsp view->lines = LINES;
891 5c60c32a 2018-10-18 stsp view->cols = COLS;
892 5c60c32a 2018-10-18 stsp err = view_resize(view);
894 5c60c32a 2018-10-18 stsp return err;
896 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
897 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
899 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
900 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
902 5c60c32a 2018-10-18 stsp return NULL;
905 5c60c32a 2018-10-18 stsp static const struct got_error *
906 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
908 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
910 5c60c32a 2018-10-18 stsp view->begin_x = 0;
911 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
912 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
913 5c60c32a 2018-10-18 stsp view->ncols = COLS;
914 5c60c32a 2018-10-18 stsp view->lines = LINES;
915 5c60c32a 2018-10-18 stsp view->cols = COLS;
916 5c60c32a 2018-10-18 stsp err = view_resize(view);
918 5c60c32a 2018-10-18 stsp return err;
920 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
921 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
923 5c60c32a 2018-10-18 stsp return NULL;
927 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
929 5c60c32a 2018-10-18 stsp return view->parent == NULL;
933 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
935 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
939 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
941 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
945 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
947 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
948 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
951 9b058f45 2022-06-30 mark static void
952 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
954 9b058f45 2022-06-30 mark PANEL *panel;
955 9b058f45 2022-06-30 mark const struct tog_view *view_above;
957 9b058f45 2022-06-30 mark if (view->parent)
958 9b058f45 2022-06-30 mark return view_border(view->parent);
960 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
961 9b058f45 2022-06-30 mark if (panel == NULL)
964 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
965 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
966 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
967 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
968 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
970 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
971 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
974 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
975 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
976 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
977 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
978 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
980 4d8c2215 2018-08-19 stsp static const struct got_error *
981 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
983 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
984 9b058f45 2022-06-30 mark int dif, nlines, ncols;
986 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
988 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
989 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
991 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
992 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
993 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
995 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
997 4dd27a72 2022-06-29 stsp if (view->child) {
998 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
1000 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1001 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1002 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1003 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1004 0dbbbe90 2022-06-17 op ncols = COLS;
1006 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1007 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1008 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1010 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1012 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1014 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1015 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1018 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1019 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1020 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1023 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1025 9b058f45 2022-06-30 mark return err;
1026 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1027 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1029 9b058f45 2022-06-30 mark return err;
1031 9b058f45 2022-06-30 mark view_border(view);
1032 9b058f45 2022-06-30 mark update_panels();
1033 9b058f45 2022-06-30 mark doupdate();
1034 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1035 9b058f45 2022-06-30 mark nlines = view->nlines;
1037 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1038 0dbbbe90 2022-06-17 op ncols = COLS;
1040 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1041 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1043 571ccd73 2022-07-19 mark return err;
1046 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1047 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1048 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1049 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1050 0dbbbe90 2022-06-17 op wclear(view->window);
1052 0dbbbe90 2022-06-17 op view->nlines = nlines;
1053 0dbbbe90 2022-06-17 op view->ncols = ncols;
1054 0dbbbe90 2022-06-17 op view->lines = LINES;
1055 0dbbbe90 2022-06-17 op view->cols = COLS;
1057 5c60c32a 2018-10-18 stsp return NULL;
1060 571ccd73 2022-07-19 mark static const struct got_error *
1061 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1063 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1064 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1065 6fe51fee 2022-07-22 mark int n = 0;
1067 6fe51fee 2022-07-22 mark if (s->selected_entry)
1068 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1071 571ccd73 2022-07-19 mark * Request commits to account for the increased
1072 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1074 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1075 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1076 571ccd73 2022-07-19 mark err = request_log_commits(view);
1079 571ccd73 2022-07-19 mark return err;
1082 d9a7ab53 2022-07-11 mark static void
1083 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1085 d9a7ab53 2022-07-11 mark if (n == 0)
1088 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1089 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1090 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1092 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1093 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1094 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1095 d9a7ab53 2022-07-11 mark view->offset -= n;
1097 d9a7ab53 2022-07-11 mark view->offset = 0;
1101 3c1dfe12 2022-07-08 mark static const struct got_error *
1102 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1104 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1105 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1107 3c1dfe12 2022-07-08 mark if (view->parent)
1108 3c1dfe12 2022-07-08 mark v = view->parent;
1112 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1113 3c1dfe12 2022-07-08 mark return NULL;
1115 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1117 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1118 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1119 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1120 3c1dfe12 2022-07-08 mark if (view->parent)
1121 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1123 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1124 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1125 3c1dfe12 2022-07-08 mark view->count = 0;
1126 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1127 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1128 3c1dfe12 2022-07-08 mark view->count = 0;
1129 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1131 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1132 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1133 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1134 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1136 3c1dfe12 2022-07-08 mark return err;
1137 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1139 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1140 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1141 3c1dfe12 2022-07-08 mark if (view->parent)
1142 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1144 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1145 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1146 3c1dfe12 2022-07-08 mark view->count = 0;
1147 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1148 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1149 3c1dfe12 2022-07-08 mark view->count = 0;
1150 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1152 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1155 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1156 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1157 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1158 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1160 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1162 3c1dfe12 2022-07-08 mark return err;
1163 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1165 3c1dfe12 2022-07-08 mark return err;
1167 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1168 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1170 3c1dfe12 2022-07-08 mark return err;
1173 6fe51fee 2022-07-22 mark if (v->resize)
1174 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1175 6fe51fee 2022-07-22 mark else if (v->child->resize)
1176 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1178 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1180 3c1dfe12 2022-07-08 mark return err;
1183 3c1dfe12 2022-07-08 mark static void
1184 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1186 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1188 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1189 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1192 669b5ffa 2018-10-07 stsp static const struct got_error *
1193 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1195 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1197 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1198 669b5ffa 2018-10-07 stsp return NULL;
1200 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1201 669b5ffa 2018-10-07 stsp view->child = NULL;
1202 669b5ffa 2018-10-07 stsp return err;
1205 0dbbbe90 2022-06-17 op static const struct got_error *
1206 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1208 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1210 669b5ffa 2018-10-07 stsp view->child = child;
1211 669b5ffa 2018-10-07 stsp child->parent = view;
1213 3c1dfe12 2022-07-08 mark err = view_resize(view);
1215 3c1dfe12 2022-07-08 mark return err;
1217 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1218 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1220 3c1dfe12 2022-07-08 mark return err;
1223 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1224 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1226 136e2bd4 2022-07-23 mark static const struct got_error *
1227 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1228 136e2bd4 2022-07-23 mark enum tog_view_type request)
1230 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1231 136e2bd4 2022-07-23 mark const struct got_error *err;
1232 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1234 136e2bd4 2022-07-23 mark *requested = NULL;
1236 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1237 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1239 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1241 136e2bd4 2022-07-23 mark return err;
1243 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1244 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1245 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1247 136e2bd4 2022-07-23 mark return err;
1250 136e2bd4 2022-07-23 mark view->focussed = 0;
1251 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1252 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1253 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1254 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1256 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1257 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1258 136e2bd4 2022-07-23 mark err = view_close_child(view);
1260 136e2bd4 2022-07-23 mark return err;
1261 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1263 136e2bd4 2022-07-23 mark return err;
1264 136e2bd4 2022-07-23 mark view->focus_child = 1;
1266 136e2bd4 2022-07-23 mark *requested = new_view;
1268 136e2bd4 2022-07-23 mark return NULL;
1271 34bc9ec9 2019-02-22 stsp static void
1272 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1274 25791caa 2018-10-24 stsp int cols, lines;
1275 25791caa 2018-10-24 stsp struct winsize size;
1277 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1278 25791caa 2018-10-24 stsp cols = 80; /* Default */
1279 25791caa 2018-10-24 stsp lines = 24;
1281 25791caa 2018-10-24 stsp cols = size.ws_col;
1282 25791caa 2018-10-24 stsp lines = size.ws_row;
1284 25791caa 2018-10-24 stsp resize_term(lines, cols);
1287 2b49a8ae 2019-06-22 stsp static const struct got_error *
1288 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1290 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1291 9b058f45 2022-06-30 mark struct tog_view *v = view;
1292 2b49a8ae 2019-06-22 stsp char pattern[1024];
1295 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1296 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1297 c0c4acc8 2021-01-24 stsp view->searching = 0;
1298 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1300 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1302 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1303 2b49a8ae 2019-06-22 stsp return NULL;
1305 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1306 9b058f45 2022-06-30 mark v = view->child;
1307 9d0feb8b 2022-12-27 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1308 9d0feb8b 2022-12-27 mark v = view->parent;
1310 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1311 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1313 092a9f9c 2022-12-27 mark nodelay(v->window, FALSE); /* block for search term input */
1314 2b49a8ae 2019-06-22 stsp nocbreak();
1316 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1317 9b058f45 2022-06-30 mark wrefresh(v->window);
1320 092a9f9c 2022-12-27 mark nodelay(v->window, TRUE);
1321 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1322 2b49a8ae 2019-06-22 stsp return NULL;
1324 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1325 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1326 7c32bd05 2019-06-22 stsp if (err) {
1327 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1328 7c32bd05 2019-06-22 stsp return err;
1330 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1331 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1332 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1333 2b49a8ae 2019-06-22 stsp view->search_next(view);
1336 2b49a8ae 2019-06-22 stsp return NULL;
1339 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1340 d2366e29 2022-07-07 mark static const struct got_error *
1341 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1343 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1344 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1346 d2366e29 2022-07-07 mark if (view->parent)
1347 d2366e29 2022-07-07 mark v = view->parent;
1351 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1352 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1354 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1356 7532ccda 2022-07-11 mark if (!v->child)
1357 7532ccda 2022-07-11 mark return NULL;
1358 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1359 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1361 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1362 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1363 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1364 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1365 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1368 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1369 d2366e29 2022-07-07 mark v->ncols = COLS;
1370 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1371 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1373 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1375 d2366e29 2022-07-07 mark return err;
1377 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1378 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1379 d2366e29 2022-07-07 mark v->focus_child = 1;
1381 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1383 d2366e29 2022-07-07 mark return err;
1384 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1386 d2366e29 2022-07-07 mark return err;
1388 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1389 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1390 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1391 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1393 279d2047 2022-07-29 stsp return err;
1394 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1396 279d2047 2022-07-29 stsp return err;
1398 7532ccda 2022-07-11 mark offset_selection_up(v);
1399 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1401 dff91825 2022-07-22 mark if (v->resize)
1402 dff91825 2022-07-22 mark err = v->resize(v, 0);
1403 dff91825 2022-07-22 mark else if (v->child->resize)
1404 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1406 d2366e29 2022-07-07 mark return err;
1410 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1411 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1413 640cd7ff 2022-06-22 mark static int
1414 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1416 9b058f45 2022-06-30 mark struct tog_view *v = view;
1417 9b058f45 2022-06-30 mark int x, n = 0;
1419 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1420 9b058f45 2022-06-30 mark v = view->child;
1421 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1422 9b058f45 2022-06-30 mark v = view->parent;
1424 640cd7ff 2022-06-22 mark view->count = 0;
1425 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1426 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1427 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1428 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1429 9b058f45 2022-06-30 mark waddch(v->window, ':');
1432 9b058f45 2022-06-30 mark x = getcurx(v->window);
1433 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1434 9b058f45 2022-06-30 mark waddch(v->window, c);
1435 9b058f45 2022-06-30 mark wrefresh(v->window);
1439 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1440 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1442 640cd7ff 2022-06-22 mark if (n >= 9999999)
1443 640cd7ff 2022-06-22 mark n = 9999999;
1445 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1446 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1448 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1449 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1454 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1455 640cd7ff 2022-06-22 mark view->count = n;
1460 0cf4efb1 2018-09-29 stsp static const struct got_error *
1461 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1462 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1464 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1465 669b5ffa 2018-10-07 stsp struct tog_view *v;
1466 1a76625f 2018-10-22 stsp int ch, errcode;
1468 e5a0f69f 2018-08-18 stsp *new = NULL;
1470 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1471 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1472 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1473 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1474 640cd7ff 2022-06-22 mark view->count = 0;
1477 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1478 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1479 82954512 2020-02-03 stsp if (errcode)
1480 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1481 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1482 3da8ef85 2021-09-21 stsp sched_yield();
1483 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1484 82954512 2020-02-03 stsp if (errcode)
1485 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1486 82954512 2020-02-03 stsp "pthread_mutex_lock");
1487 60493ae3 2019-06-20 stsp view->search_next(view);
1488 60493ae3 2019-06-20 stsp return NULL;
1491 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1492 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1493 1a76625f 2018-10-22 stsp if (errcode)
1494 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1495 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1496 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1498 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1499 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1500 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1501 a6d37fac 2022-07-03 mark view->count = 0;
1503 a6d37fac 2022-07-03 mark ch = view->ch;
1505 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1506 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1507 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1509 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1510 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1511 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1512 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1513 1a76625f 2018-10-22 stsp if (errcode)
1514 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1516 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1517 25791caa 2018-10-24 stsp tog_resizeterm();
1518 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1519 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1520 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1521 25791caa 2018-10-24 stsp err = view_resize(v);
1523 25791caa 2018-10-24 stsp return err;
1524 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1526 25791caa 2018-10-24 stsp return err;
1527 cdfcfb03 2020-12-06 stsp if (v->child) {
1528 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1530 cdfcfb03 2020-12-06 stsp return err;
1531 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1532 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1534 cdfcfb03 2020-12-06 stsp return err;
1535 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1536 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1538 3c1dfe12 2022-07-08 mark return err;
1544 e5a0f69f 2018-08-18 stsp switch (ch) {
1547 ec2a9698 2022-09-15 mark case KEY_F(1):
1548 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1549 ec2a9698 2022-09-15 mark err = view->reset(view);
1551 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1554 640cd7ff 2022-06-22 mark view->count = 0;
1555 1e37a5c2 2019-05-12 jcs if (view->child) {
1556 e78dc838 2020-12-04 stsp view->focussed = 0;
1557 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1558 e78dc838 2020-12-04 stsp view->focus_child = 1;
1559 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1560 e78dc838 2020-12-04 stsp view->focussed = 0;
1561 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1562 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1563 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1564 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1565 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1568 9b058f45 2022-06-30 mark return err;
1570 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1571 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1573 9b058f45 2022-06-30 mark return err;
1578 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1579 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1580 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1581 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1585 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1587 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1588 9970f7fc 2020-12-03 stsp view->dying = 1;
1594 640cd7ff 2022-06-22 mark view->count = 0;
1595 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1596 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1598 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1599 e78dc838 2020-12-04 stsp view->focussed = 0;
1600 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1601 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1603 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1605 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1609 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1610 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1612 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1613 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1614 e78dc838 2020-12-04 stsp view->focussed = 1;
1615 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1617 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1618 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1619 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1621 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1625 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1629 6fe51fee 2022-07-22 mark if (view->resize) {
1630 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1634 9b058f45 2022-06-30 mark if (view->parent)
1635 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1637 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1640 3c1dfe12 2022-07-08 mark view->count = 0;
1641 d2366e29 2022-07-07 mark err = switch_split(view);
1644 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1647 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1649 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1652 640cd7ff 2022-06-22 mark view->count = 0;
1653 60493ae3 2019-06-20 stsp if (view->search_start)
1654 2b49a8ae 2019-06-22 stsp view_search_start(view);
1656 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1660 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1661 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1662 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1663 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1664 60493ae3 2019-06-20 stsp view->search_next(view);
1666 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1669 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1670 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1672 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1673 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1674 917d79a7 2022-07-01 stsp if (v->reset) {
1675 917d79a7 2022-07-01 stsp err = v->reset(v);
1677 917d79a7 2022-07-01 stsp return err;
1679 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1680 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1682 917d79a7 2022-07-01 stsp return err;
1687 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1691 e5a0f69f 2018-08-18 stsp return err;
1695 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1697 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1698 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1700 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1702 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1705 669b5ffa 2018-10-07 stsp return view->focussed;
1708 bcbd79e2 2018-08-19 stsp static const struct got_error *
1709 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1711 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1712 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1713 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1714 d2366e29 2022-07-07 mark char *mode;
1715 fd823528 2018-10-22 stsp int fast_refresh = 10;
1716 1a76625f 2018-10-22 stsp int done = 0, errcode;
1718 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1719 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1720 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1722 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1724 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1725 1a76625f 2018-10-22 stsp if (errcode)
1726 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1728 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1729 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1731 1004088d 2018-09-29 stsp view->focussed = 1;
1732 878940b7 2018-09-29 stsp err = view->show(view);
1734 0cf4efb1 2018-09-29 stsp return err;
1735 0cf4efb1 2018-09-29 stsp update_panels();
1736 0cf4efb1 2018-09-29 stsp doupdate();
1737 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1738 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1739 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1740 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1741 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1743 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1746 9970f7fc 2020-12-03 stsp if (view->dying) {
1747 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1749 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1750 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1752 e78dc838 2020-12-04 stsp else if (view->parent)
1753 669b5ffa 2018-10-07 stsp prev = view->parent;
1755 e78dc838 2020-12-04 stsp if (view->parent) {
1756 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1757 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1758 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1759 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1760 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1763 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1764 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1766 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1768 9970f7fc 2020-12-03 stsp err = view_close(view);
1770 e5a0f69f 2018-08-18 stsp goto done;
1772 e78dc838 2020-12-04 stsp view = NULL;
1773 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1774 e78dc838 2020-12-04 stsp if (v->focussed)
1777 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1778 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1780 e78dc838 2020-12-04 stsp view = prev;
1781 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1782 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1783 e78dc838 2020-12-04 stsp tog_view_list_head);
1785 e78dc838 2020-12-04 stsp if (view) {
1786 e78dc838 2020-12-04 stsp if (view->focus_child) {
1787 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1788 e78dc838 2020-12-04 stsp view = view->child;
1790 e78dc838 2020-12-04 stsp view->focussed = 1;
1794 bcbd79e2 2018-08-19 stsp if (new_view) {
1795 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1796 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1797 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1798 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1800 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1801 86c66b02 2018-10-18 stsp err = view_close(v);
1803 86c66b02 2018-10-18 stsp goto done;
1806 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1807 fed7eaa8 2018-10-24 stsp view = new_view;
1809 669b5ffa 2018-10-07 stsp if (view) {
1810 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1811 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1812 e78dc838 2020-12-04 stsp view = view->child;
1814 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1815 e78dc838 2020-12-04 stsp view = view->parent;
1817 e78dc838 2020-12-04 stsp show_panel(view->panel);
1818 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1819 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1820 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1821 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1823 1a76625f 2018-10-22 stsp goto done;
1825 669b5ffa 2018-10-07 stsp err = view->show(view);
1827 1a76625f 2018-10-22 stsp goto done;
1828 669b5ffa 2018-10-07 stsp if (view->child) {
1829 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1831 1a76625f 2018-10-22 stsp goto done;
1833 1a76625f 2018-10-22 stsp update_panels();
1834 1a76625f 2018-10-22 stsp doupdate();
1838 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1839 5629093a 2022-07-11 stsp const struct got_error *close_err;
1840 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1841 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1842 5629093a 2022-07-11 stsp close_err = view_close(view);
1843 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1844 5629093a 2022-07-11 stsp err = close_err;
1847 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1848 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1849 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1851 e5a0f69f 2018-08-18 stsp return err;
1854 4ed7e80c 2018-05-20 stsp __dead static void
1855 9f7d7167 2018-04-29 stsp usage_log(void)
1858 c70c5802 2018-08-01 stsp fprintf(stderr,
1859 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1860 9f7d7167 2018-04-29 stsp getprogname());
1864 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1865 80ddbec8 2018-04-29 stsp static const struct got_error *
1866 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1868 00dfcb92 2018-06-11 stsp char *vis = NULL;
1869 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1871 963b370f 2018-05-20 stsp *ws = NULL;
1872 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1873 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1874 00dfcb92 2018-06-11 stsp int vislen;
1875 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1876 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1878 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1879 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1881 00dfcb92 2018-06-11 stsp return err;
1882 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1883 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1884 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1885 a7f50699 2018-06-11 stsp goto done;
1889 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1890 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1891 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1892 a7f50699 2018-06-11 stsp goto done;
1895 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1896 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1898 00dfcb92 2018-06-11 stsp free(vis);
1899 963b370f 2018-05-20 stsp if (err) {
1900 963b370f 2018-05-20 stsp free(*ws);
1901 963b370f 2018-05-20 stsp *ws = NULL;
1902 963b370f 2018-05-20 stsp *wlen = 0;
1904 963b370f 2018-05-20 stsp return err;
1907 145b6838 2022-06-16 stsp static const struct got_error *
1908 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1910 145b6838 2022-06-16 stsp char *dst;
1911 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1913 145b6838 2022-06-16 stsp *ptr = NULL;
1914 145b6838 2022-06-16 stsp n = len = strlen(src);
1915 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1916 145b6838 2022-06-16 stsp if (dst == NULL)
1917 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1919 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1920 145b6838 2022-06-16 stsp const char c = src[idx];
1922 145b6838 2022-06-16 stsp if (c == '\t') {
1923 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1926 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1927 6e1c41ad 2022-06-16 mark if (p == NULL) {
1928 6e1c41ad 2022-06-16 mark free(dst);
1929 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1934 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1937 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1941 145b6838 2022-06-16 stsp dst[sz] = '\0';
1942 145b6838 2022-06-16 stsp *ptr = dst;
1943 145b6838 2022-06-16 stsp return NULL;
1947 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1948 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1949 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1953 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1955 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1957 4e4a9ac8 2022-06-17 op if (n == 0) {
1958 4e4a9ac8 2022-06-17 op *rcol = cols;
1962 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1963 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1964 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1966 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1968 4e4a9ac8 2022-06-17 op if (width == -1) {
1970 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1973 4e4a9ac8 2022-06-17 op if (cols + width > n)
1975 4e4a9ac8 2022-06-17 op cols += width;
1978 4e4a9ac8 2022-06-17 op *rcol = cols;
1983 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1984 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1985 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1987 ccda2f4d 2022-06-16 stsp static const struct got_error *
1988 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1989 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1991 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1993 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1994 145b6838 2022-06-16 stsp char *exstr = NULL;
1995 963b370f 2018-05-20 stsp size_t wlen;
1996 4e4a9ac8 2022-06-17 op int i, scrollx;
1998 963b370f 2018-05-20 stsp *wlinep = NULL;
1999 b700b5d6 2018-07-10 stsp *widthp = 0;
2001 145b6838 2022-06-16 stsp if (expand) {
2002 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2004 145b6838 2022-06-16 stsp return err;
2007 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2008 145b6838 2022-06-16 stsp free(exstr);
2010 963b370f 2018-05-20 stsp return err;
2012 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2014 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2015 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2018 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2019 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2023 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2024 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2026 b700b5d6 2018-07-10 stsp if (widthp)
2027 b700b5d6 2018-07-10 stsp *widthp = cols;
2028 ccda2f4d 2022-06-16 stsp if (scrollxp)
2029 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2031 963b370f 2018-05-20 stsp free(wline);
2033 963b370f 2018-05-20 stsp *wlinep = wline;
2034 963b370f 2018-05-20 stsp return err;
2037 8b473291 2019-02-21 stsp static const struct got_error*
2038 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2039 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2041 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2042 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2044 8b473291 2019-02-21 stsp const char *name;
2046 8b473291 2019-02-21 stsp *refs_str = NULL;
2048 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2049 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2050 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2053 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2054 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2056 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2057 8b473291 2019-02-21 stsp name += 5;
2058 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2059 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2061 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2062 8b473291 2019-02-21 stsp name += 6;
2063 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2064 8b473291 2019-02-21 stsp name += 8;
2065 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2066 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2069 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2072 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2073 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2074 5d844a1e 2019-08-13 stsp if (err) {
2075 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2076 48cae60d 2020-09-22 stsp free(ref_id);
2079 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2080 5d844a1e 2019-08-13 stsp err = NULL;
2081 5d844a1e 2019-08-13 stsp tag = NULL;
2084 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2085 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2086 48cae60d 2020-09-22 stsp free(ref_id);
2088 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2089 52b5abe1 2019-08-13 stsp if (cmp != 0)
2091 8b473291 2019-02-21 stsp s = *refs_str;
2092 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2093 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2094 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2096 8b473291 2019-02-21 stsp *refs_str = NULL;
2102 8b473291 2019-02-21 stsp return err;
2105 963b370f 2018-05-20 stsp static const struct got_error *
2106 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2107 27a741e5 2019-09-11 stsp int col_tab_align)
2109 e6b8b890 2020-12-29 naddy char *smallerthan;
2111 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2112 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2113 5813d178 2019-03-09 stsp author = smallerthan + 1;
2114 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2115 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2116 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2119 5813d178 2019-03-09 stsp static const struct got_error *
2120 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2121 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2122 8fdc79fe 2020-12-01 naddy int author_display_cols)
2124 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2125 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2126 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2127 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2128 5813d178 2019-03-09 stsp char *author = NULL;
2129 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2130 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2131 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2132 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2133 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2134 ccb26ccd 2018-11-05 stsp struct tm tm;
2135 45d799e2 2018-12-23 stsp time_t committer_time;
2136 11b20872 2019-11-08 stsp struct tog_color *tc;
2138 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2139 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2140 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2141 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2142 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2144 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2145 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2147 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2148 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2150 11b20872 2019-11-08 stsp wattr_on(view->window,
2151 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2152 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2154 11b20872 2019-11-08 stsp wattr_off(view->window,
2155 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2156 27a741e5 2019-09-11 stsp col = limit;
2157 b39d25c7 2018-07-10 stsp if (col > avail)
2158 b39d25c7 2018-07-10 stsp goto done;
2160 6570a66d 2019-11-08 stsp if (avail >= 120) {
2161 6570a66d 2019-11-08 stsp char *id_str;
2162 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2164 6570a66d 2019-11-08 stsp goto done;
2165 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2167 11b20872 2019-11-08 stsp wattr_on(view->window,
2168 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2169 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2171 11b20872 2019-11-08 stsp wattr_off(view->window,
2172 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2173 6570a66d 2019-11-08 stsp free(id_str);
2175 6570a66d 2019-11-08 stsp if (col > avail)
2176 6570a66d 2019-11-08 stsp goto done;
2179 10aab77f 2022-07-19 op if (s->use_committer)
2180 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2182 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2183 5813d178 2019-03-09 stsp if (author == NULL) {
2184 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2185 80ddbec8 2018-04-29 stsp goto done;
2187 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2189 bb737323 2018-05-20 stsp goto done;
2190 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2192 11b20872 2019-11-08 stsp wattr_on(view->window,
2193 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2194 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2195 bb737323 2018-05-20 stsp col += author_width;
2196 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2197 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2199 bb737323 2018-05-20 stsp author_width++;
2202 f0f62654 2022-09-08 mark wattr_off(view->window,
2203 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2204 9c2eaf34 2018-05-20 stsp if (col > avail)
2205 9c2eaf34 2018-05-20 stsp goto done;
2207 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2209 6d9fbc00 2018-04-29 stsp goto done;
2210 bb737323 2018-05-20 stsp logmsg = logmsg0;
2211 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2213 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2214 bb737323 2018-05-20 stsp if (newline)
2215 bb737323 2018-05-20 stsp *newline = '\0';
2216 ccda2f4d 2022-06-16 stsp limit = avail - col;
2217 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2218 4d1f6af3 2022-06-17 op limit--; /* for the border */
2219 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2220 ccda2f4d 2022-06-16 stsp limit, col, 1);
2222 29688b02 2022-06-16 stsp goto done;
2223 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2224 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2225 27a741e5 2019-09-11 stsp while (col < avail) {
2226 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2230 80ddbec8 2018-04-29 stsp free(logmsg0);
2231 bb737323 2018-05-20 stsp free(wlogmsg);
2232 5813d178 2019-03-09 stsp free(author);
2233 bb737323 2018-05-20 stsp free(wauthor);
2234 80ddbec8 2018-04-29 stsp free(line);
2235 80ddbec8 2018-04-29 stsp return err;
2238 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2239 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2240 899d86c2 2018-05-10 stsp struct got_object_id *id)
2242 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2243 e15c42de 2022-09-05 op struct got_object_id *dup;
2245 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2246 80ddbec8 2018-04-29 stsp if (entry == NULL)
2247 899d86c2 2018-05-10 stsp return NULL;
2249 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2250 e15c42de 2022-09-05 op if (dup == NULL) {
2251 e15c42de 2022-09-05 op free(entry);
2252 e15c42de 2022-09-05 op return NULL;
2255 e15c42de 2022-09-05 op entry->id = dup;
2256 99db9666 2018-05-07 stsp entry->commit = commit;
2257 899d86c2 2018-05-10 stsp return entry;
2260 99db9666 2018-05-07 stsp static void
2261 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2263 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2265 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2266 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2267 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2268 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2269 e15c42de 2022-09-05 op free(entry->id);
2270 99db9666 2018-05-07 stsp free(entry);
2273 99db9666 2018-05-07 stsp static void
2274 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2276 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2277 99db9666 2018-05-07 stsp pop_commit(commits);
2280 c4972b91 2018-05-07 stsp static const struct got_error *
2281 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2282 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2284 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2285 13add988 2019-10-15 stsp regmatch_t regmatch;
2286 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2288 13add988 2019-10-15 stsp *have_match = 0;
2290 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2292 13add988 2019-10-15 stsp return err;
2294 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2296 13add988 2019-10-15 stsp goto done;
2298 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2299 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2300 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2301 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2302 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2303 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2304 13add988 2019-10-15 stsp *have_match = 1;
2306 13add988 2019-10-15 stsp free(id_str);
2307 13add988 2019-10-15 stsp free(logmsg);
2308 13add988 2019-10-15 stsp return err;
2311 13add988 2019-10-15 stsp static const struct got_error *
2312 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2314 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2317 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2318 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2319 1a76625f 2018-10-22 stsp * while updating the display.
2322 d9787ed8 2022-09-10 op struct got_object_id id;
2323 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2324 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2325 568eae95 2022-09-11 mark int limit_match = 0;
2326 1a76625f 2018-10-22 stsp int errcode;
2328 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2329 4e0d2870 2020-12-07 naddy NULL, NULL);
2333 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2336 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2337 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2338 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2342 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2343 1a76625f 2018-10-22 stsp if (errcode) {
2344 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2345 13add988 2019-10-15 stsp "pthread_mutex_lock");
2349 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2350 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2351 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2353 568eae95 2022-09-11 mark if (*a->limiting) {
2354 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2355 568eae95 2022-09-11 mark a->limit_regex);
2359 568eae95 2022-09-11 mark if (limit_match) {
2360 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2362 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2363 568eae95 2022-09-11 mark entry->commit, entry->id);
2364 568eae95 2022-09-11 mark if (matched == NULL) {
2365 568eae95 2022-09-11 mark err = got_error_from_errno(
2366 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2369 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2370 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2372 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2373 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2374 568eae95 2022-09-11 mark matched, entry);
2375 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2379 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2380 568eae95 2022-09-11 mark * have found a match, and that it should be
2381 568eae95 2022-09-11 mark * counted as a new entry for the view.
2383 568eae95 2022-09-11 mark a->limit_match = limit_match;
2386 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2387 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2388 7c1452c1 2020-03-26 stsp int have_match;
2389 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2393 568eae95 2022-09-11 mark if (*a->limiting) {
2394 568eae95 2022-09-11 mark if (limit_match && have_match)
2395 568eae95 2022-09-11 mark *a->search_next_done =
2396 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2397 568eae95 2022-09-11 mark } else if (have_match)
2398 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2401 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2402 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2403 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2404 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2407 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2409 9ba79e04 2018-06-11 stsp return err;
2412 2b779855 2020-12-05 naddy static void
2413 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2415 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2416 2b779855 2020-12-05 naddy int ncommits = 0;
2418 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2419 2b779855 2020-12-05 naddy while (entry) {
2420 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2421 2b779855 2020-12-05 naddy s->selected_entry = entry;
2424 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2425 2b779855 2020-12-05 naddy ncommits++;
2429 0553a4e3 2018-04-30 stsp static const struct got_error *
2430 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2432 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2433 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2434 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2435 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2436 60493ae3 2019-06-20 stsp int width;
2437 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2438 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2439 8b473291 2019-02-21 stsp char *refs_str = NULL;
2440 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2441 11b20872 2019-11-08 stsp struct tog_color *tc;
2442 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2444 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2445 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2447 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2448 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2449 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2450 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2452 ecb28ae0 2018-07-16 stsp return err;
2453 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2454 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2455 d2075bf3 2020-12-25 stsp if (refs) {
2456 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2457 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2459 d2075bf3 2020-12-25 stsp goto done;
2463 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2464 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2466 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2467 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2468 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2469 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2470 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2471 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2472 8f4ed634 2020-03-26 stsp goto done;
2475 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2476 568eae95 2022-09-11 mark const char *limit_str = NULL;
2478 f9686aa5 2020-03-27 stsp if (view->searching) {
2479 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2480 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2481 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2482 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2483 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2484 f9686aa5 2020-03-27 stsp search_str = "searching...";
2487 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2488 568eae95 2022-09-11 mark limit_str = "no matches found";
2490 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2491 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2492 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2493 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2494 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2495 8f4ed634 2020-03-26 stsp goto done;
2499 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2500 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2501 87411fa9 2022-06-16 stsp "........................................",
2502 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2503 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2504 1a76625f 2018-10-22 stsp header = NULL;
2505 1a76625f 2018-10-22 stsp goto done;
2507 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2508 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2509 1a76625f 2018-10-22 stsp 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 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2516 1a76625f 2018-10-22 stsp goto done;
2518 2814baeb 2018-08-01 stsp werase(view->window);
2520 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2521 a3404814 2018-09-02 stsp wstandout(view->window);
2522 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2524 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2525 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2526 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2527 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2531 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2532 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2533 a3404814 2018-09-02 stsp wstandend(view->window);
2534 ecb28ae0 2018-07-16 stsp free(wline);
2535 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2536 1a76625f 2018-10-22 stsp goto done;
2538 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2539 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2540 5813d178 2019-03-09 stsp ncommits = 0;
2541 145b6838 2022-06-16 stsp view->maxx = 0;
2542 5813d178 2019-03-09 stsp while (entry) {
2543 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2544 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2545 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2546 5813d178 2019-03-09 stsp int width;
2547 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2549 10aab77f 2022-07-19 op if (s->use_committer)
2550 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2552 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2553 5813d178 2019-03-09 stsp if (author == NULL) {
2554 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2555 5813d178 2019-03-09 stsp goto done;
2557 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2558 27a741e5 2019-09-11 stsp date_display_cols);
2559 5813d178 2019-03-09 stsp if (author_cols < width)
2560 5813d178 2019-03-09 stsp author_cols = width;
2561 5813d178 2019-03-09 stsp free(wauthor);
2562 5813d178 2019-03-09 stsp free(author);
2563 a310d9c3 2022-07-21 florian if (err)
2564 a310d9c3 2022-07-21 florian goto done;
2565 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2567 145b6838 2022-06-16 stsp goto done;
2568 145b6838 2022-06-16 stsp msg = msg0;
2569 145b6838 2022-06-16 stsp while (*msg == '\n')
2571 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2572 29688b02 2022-06-16 stsp *eol = '\0';
2573 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2574 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2576 29688b02 2022-06-16 stsp goto done;
2577 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2578 145b6838 2022-06-16 stsp free(msg0);
2579 29688b02 2022-06-16 stsp free(wmsg);
2580 7ca04879 2019-10-19 stsp ncommits++;
2581 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2584 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2585 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2586 867c6645 2018-07-10 stsp ncommits = 0;
2587 899d86c2 2018-05-10 stsp while (entry) {
2588 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2590 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2591 2814baeb 2018-08-01 stsp wstandout(view->window);
2592 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2593 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2594 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2595 2814baeb 2018-08-01 stsp wstandend(view->window);
2597 60493ae3 2019-06-20 stsp goto done;
2598 0553a4e3 2018-04-30 stsp ncommits++;
2599 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2600 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2603 9b058f45 2022-06-30 mark view_border(view);
2605 1a76625f 2018-10-22 stsp free(id_str);
2606 8b473291 2019-02-21 stsp free(refs_str);
2607 1a76625f 2018-10-22 stsp free(ncommits_str);
2608 1a76625f 2018-10-22 stsp free(header);
2609 80ddbec8 2018-04-29 stsp return err;
2612 07b55e75 2018-05-10 stsp static void
2613 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2615 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2616 07b55e75 2018-05-10 stsp int nscrolled = 0;
2618 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2619 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2622 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2623 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2624 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2625 07b55e75 2018-05-10 stsp if (entry) {
2626 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2627 07b55e75 2018-05-10 stsp nscrolled++;
2632 aa075928 2018-05-10 stsp static const struct got_error *
2633 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2635 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2636 5e224a3e 2019-02-22 stsp int errcode;
2638 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2640 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2641 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2642 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2643 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2644 7aafa0d1 2019-02-22 stsp if (errcode)
2645 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2646 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2649 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2650 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2652 7c1452c1 2020-03-26 stsp if (!wait)
2655 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2656 ffe38506 2020-12-01 naddy show_log_view(view);
2657 7c1452c1 2020-03-26 stsp update_panels();
2658 7c1452c1 2020-03-26 stsp doupdate();
2660 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2661 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2662 82954512 2020-02-03 stsp if (errcode)
2663 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2664 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2666 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2667 ffe38506 2020-12-01 naddy show_log_view(view);
2668 7c1452c1 2020-03-26 stsp update_panels();
2669 7c1452c1 2020-03-26 stsp doupdate();
2672 5e224a3e 2019-02-22 stsp return NULL;
2675 5e224a3e 2019-02-22 stsp static const struct got_error *
2676 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2678 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2679 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2681 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2682 2525dccb 2022-07-13 mark return NULL;
2684 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2685 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2686 9b058f45 2022-06-30 mark view->nscrolled = 0;
2688 9b058f45 2022-06-30 mark return err;
2691 9b058f45 2022-06-30 mark static const struct got_error *
2692 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2694 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2695 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2696 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2697 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2699 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2700 5e224a3e 2019-02-22 stsp return NULL;
2702 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2703 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2704 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2706 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2708 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2709 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2710 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2712 5e224a3e 2019-02-22 stsp return err;
2716 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2717 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2720 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2721 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2723 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2724 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2726 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2727 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2729 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2730 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2732 9b058f45 2022-06-30 mark view->nscrolled = 0;
2734 dd0a52c1 2018-05-20 stsp return err;
2737 cd0acaa7 2018-05-20 stsp static const struct got_error *
2738 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2739 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2740 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2742 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2743 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2744 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2746 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2747 15a94983 2018-12-23 stsp if (diff_view == NULL)
2748 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2750 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2751 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2752 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2753 e5a0f69f 2018-08-18 stsp if (err == NULL)
2754 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2755 cd0acaa7 2018-05-20 stsp return err;
2758 80ddbec8 2018-04-29 stsp static const struct got_error *
2759 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2760 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2762 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2764 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2765 941e9f74 2019-05-21 stsp if (parent == NULL)
2766 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2768 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2769 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2770 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2771 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2772 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2773 941e9f74 2019-05-21 stsp s->tree = subtree;
2774 941e9f74 2019-05-21 stsp s->selected = 0;
2775 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2776 941e9f74 2019-05-21 stsp return NULL;
2779 941e9f74 2019-05-21 stsp static const struct got_error *
2780 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2781 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2783 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2784 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2785 941e9f74 2019-05-21 stsp const char *p;
2786 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2788 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2790 941e9f74 2019-05-21 stsp while (*p) {
2791 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2792 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2793 56e0773d 2019-11-28 stsp char *te_name;
2795 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2798 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2799 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2800 941e9f74 2019-05-21 stsp if (slash == NULL)
2801 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2803 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2804 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2805 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2808 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2809 56e0773d 2019-11-28 stsp if (te == NULL) {
2810 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2811 56e0773d 2019-11-28 stsp free(te_name);
2814 56e0773d 2019-11-28 stsp free(te_name);
2815 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2817 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2818 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2820 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2821 941e9f74 2019-05-21 stsp if (slash)
2822 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2824 941e9f74 2019-05-21 stsp subpath = strdup(path);
2825 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2826 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2830 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2835 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2836 941e9f74 2019-05-21 stsp free(tree_id);
2840 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2841 941e9f74 2019-05-21 stsp if (err) {
2842 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2845 941e9f74 2019-05-21 stsp if (slash == NULL)
2847 941e9f74 2019-05-21 stsp free(subpath);
2848 941e9f74 2019-05-21 stsp subpath = NULL;
2849 941e9f74 2019-05-21 stsp p = slash;
2852 941e9f74 2019-05-21 stsp free(subpath);
2853 1a76625f 2018-10-22 stsp return err;
2856 61266923 2020-01-14 stsp static const struct got_error *
2857 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2858 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2859 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2861 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2862 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2863 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2865 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2866 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2867 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2869 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2871 55cccc34 2020-02-20 stsp return err;
2872 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2874 55cccc34 2020-02-20 stsp *new_view = tree_view;
2876 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2877 55cccc34 2020-02-20 stsp return NULL;
2879 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2882 55cccc34 2020-02-20 stsp static const struct got_error *
2883 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2885 61266923 2020-01-14 stsp sigset_t sigset;
2886 61266923 2020-01-14 stsp int errcode;
2888 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2889 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2891 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2892 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2893 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2894 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2895 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2896 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2897 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2898 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2899 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2901 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2902 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2903 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2905 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2906 61266923 2020-01-14 stsp if (errcode)
2907 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2909 61266923 2020-01-14 stsp return NULL;
2912 1a76625f 2018-10-22 stsp static void *
2913 1a76625f 2018-10-22 stsp log_thread(void *arg)
2915 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2916 1a76625f 2018-10-22 stsp int errcode = 0;
2917 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2918 1a76625f 2018-10-22 stsp int done = 0;
2921 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2922 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2924 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2925 5629093a 2022-07-11 stsp if (errcode) {
2926 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2927 61266923 2020-01-14 stsp return (void *)err;
2930 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2931 5629093a 2022-07-11 stsp if (err) {
2932 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2933 5629093a 2022-07-11 stsp goto done;
2936 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2937 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2938 5629093a 2022-07-11 stsp if (errcode) {
2939 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2940 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2941 5629093a 2022-07-11 stsp goto done;
2943 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2944 1a76625f 2018-10-22 stsp if (err) {
2945 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2946 5629093a 2022-07-11 stsp goto done;
2947 1a76625f 2018-10-22 stsp err = NULL;
2949 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
2950 568eae95 2022-09-11 mark if (*a->limiting) {
2951 568eae95 2022-09-11 mark if (a->limit_match)
2952 568eae95 2022-09-11 mark a->commits_needed--;
2954 568eae95 2022-09-11 mark a->commits_needed--;
2957 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2958 3abe8080 2019-04-10 stsp if (errcode) {
2959 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2960 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2961 5629093a 2022-07-11 stsp goto done;
2962 3abe8080 2019-04-10 stsp } else if (*a->quit)
2964 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
2965 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2966 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
2967 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2968 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
2969 568eae95 2022-09-11 mark *a->first_displayed_entry =
2970 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
2971 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
2974 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2975 7c1452c1 2020-03-26 stsp if (errcode) {
2976 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2977 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2978 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2979 5629093a 2022-07-11 stsp goto done;
2983 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2985 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2986 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2987 7c1452c1 2020-03-26 stsp &tog_mutex);
2988 5629093a 2022-07-11 stsp if (errcode) {
2989 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2990 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2991 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2992 5629093a 2022-07-11 stsp goto done;
2994 21355643 2020-12-06 stsp if (*a->quit)
2999 3abe8080 2019-04-10 stsp a->log_complete = 1;
3000 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3001 5629093a 2022-07-11 stsp if (errcode)
3002 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3004 5629093a 2022-07-11 stsp if (err) {
3005 5629093a 2022-07-11 stsp tog_thread_error = 1;
3006 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3008 1a76625f 2018-10-22 stsp return (void *)err;
3011 1a76625f 2018-10-22 stsp static const struct got_error *
3012 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3014 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3015 1a76625f 2018-10-22 stsp int errcode;
3017 1a76625f 2018-10-22 stsp if (s->thread) {
3018 1a76625f 2018-10-22 stsp s->quit = 1;
3019 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3020 1a76625f 2018-10-22 stsp if (errcode)
3021 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3022 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3023 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3024 1a76625f 2018-10-22 stsp if (errcode)
3025 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3026 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3027 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3028 1a76625f 2018-10-22 stsp if (errcode)
3029 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3030 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&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_lock");
3034 1a76625f 2018-10-22 stsp s->thread = NULL;
3037 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3038 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3039 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3042 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3043 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3044 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3045 74467cc8 2022-06-15 stsp if (err == NULL)
3046 74467cc8 2022-06-15 stsp err = pack_err;
3047 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3050 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3051 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3052 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3055 5629093a 2022-07-11 stsp return err ? err : thread_err;
3058 9343a5fb 2018-06-23 stsp static const struct got_error *
3059 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3061 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3062 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3063 276b94a1 2020-11-13 naddy int errcode;
3065 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3067 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3068 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3069 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3071 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3072 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3073 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3075 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3076 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3077 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3078 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3079 1a76625f 2018-10-22 stsp free(s->start_id);
3080 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3081 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3082 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3083 1a76625f 2018-10-22 stsp return err;
3087 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3088 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3089 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3090 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3091 568eae95 2022-09-11 mark * works with very slight change.
3093 1a76625f 2018-10-22 stsp static const struct got_error *
3094 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3096 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3097 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3098 568eae95 2022-09-11 mark struct tog_view *v = view;
3099 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3100 568eae95 2022-09-11 mark char pattern[1024];
3103 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3104 568eae95 2022-09-11 mark v = view->child;
3105 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3106 568eae95 2022-09-11 mark v = view->parent;
3108 568eae95 2022-09-11 mark /* Get the pattern */
3109 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3110 568eae95 2022-09-11 mark wclrtoeol(v->window);
3111 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3112 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3113 568eae95 2022-09-11 mark nocbreak();
3115 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3118 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3119 568eae95 2022-09-11 mark if (ret == ERR)
3120 568eae95 2022-09-11 mark return NULL;
3122 568eae95 2022-09-11 mark if (*pattern == '\0') {
3124 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3125 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3127 568eae95 2022-09-11 mark if (!s->limit_view)
3128 568eae95 2022-09-11 mark return NULL;
3131 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3132 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3133 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3134 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3135 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3136 568eae95 2022-09-11 mark * the queue.
3138 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3139 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3140 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3141 568eae95 2022-09-11 mark s->selected = 0;
3142 568eae95 2022-09-11 mark s->limit_view = 0;
3144 568eae95 2022-09-11 mark return NULL;
3147 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3148 568eae95 2022-09-11 mark return NULL;
3150 568eae95 2022-09-11 mark s->limit_view = 1;
3152 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3153 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3154 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3155 568eae95 2022-09-11 mark s->selected_entry = NULL;
3156 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3158 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3159 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3160 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3162 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3163 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3164 568eae95 2022-09-11 mark int have_match = 0;
3166 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3167 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3169 568eae95 2022-09-11 mark return err;
3171 568eae95 2022-09-11 mark if (have_match) {
3172 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3174 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3175 568eae95 2022-09-11 mark entry->id);
3176 568eae95 2022-09-11 mark if (matched == NULL) {
3177 568eae95 2022-09-11 mark err = got_error_from_errno(
3178 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3181 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3182 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3184 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3185 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3186 568eae95 2022-09-11 mark matched, entry);
3187 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3191 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3192 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3193 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3194 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3195 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3196 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3198 568eae95 2022-09-11 mark return err;
3201 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3202 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3203 568eae95 2022-09-11 mark s->selected = 0;
3205 568eae95 2022-09-11 mark return NULL;
3208 568eae95 2022-09-11 mark static const struct got_error *
3209 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3211 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3213 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3214 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3215 60493ae3 2019-06-20 stsp return NULL;
3218 60493ae3 2019-06-20 stsp static const struct got_error *
3219 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3221 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3222 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3223 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3225 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3226 f9686aa5 2020-03-27 stsp show_log_view(view);
3227 f9686aa5 2020-03-27 stsp update_panels();
3228 f9686aa5 2020-03-27 stsp doupdate();
3230 96e2b566 2019-07-08 stsp if (s->search_entry) {
3231 13add988 2019-10-15 stsp int errcode, ch;
3232 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3233 13add988 2019-10-15 stsp if (errcode)
3234 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3235 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3236 13add988 2019-10-15 stsp ch = wgetch(view->window);
3237 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3238 13add988 2019-10-15 stsp if (errcode)
3239 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3240 13add988 2019-10-15 stsp "pthread_mutex_lock");
3241 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3242 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3243 678cbce5 2019-07-28 stsp return NULL;
3245 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3246 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3248 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3249 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3250 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3252 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3253 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3254 f704b35c 2022-06-18 stsp * might have changed.
3256 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3257 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3259 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3262 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3265 60493ae3 2019-06-20 stsp while (1) {
3266 13add988 2019-10-15 stsp int have_match = 0;
3268 60493ae3 2019-06-20 stsp if (entry == NULL) {
3269 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3270 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3271 f9967bca 2020-03-27 stsp view->search_next_done =
3272 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3273 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3274 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3275 f801134a 2019-06-25 stsp return NULL;
3278 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3279 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3280 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3282 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3283 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3286 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3287 13add988 2019-10-15 stsp &view->regex);
3290 13add988 2019-10-15 stsp if (have_match) {
3291 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3292 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3296 96e2b566 2019-07-08 stsp s->search_entry = entry;
3297 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3298 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3300 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3303 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3304 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3305 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3306 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3308 60493ae3 2019-06-20 stsp return err;
3311 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3312 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3314 60493ae3 2019-06-20 stsp return err;
3319 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3321 60493ae3 2019-06-20 stsp return NULL;
3324 60493ae3 2019-06-20 stsp static const struct got_error *
3325 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3326 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3327 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3329 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3330 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3331 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3332 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3333 1a76625f 2018-10-22 stsp int errcode;
3335 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3336 f135c941 2020-02-20 stsp free(s->in_repo_path);
3337 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3338 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3339 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3342 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3343 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3344 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3345 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3347 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3348 568eae95 2022-09-11 mark s->limit_view = 0;
3349 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3351 fb2756b9 2018-08-04 stsp s->repo = repo;
3352 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3353 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3354 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3355 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3356 9cd7cbd1 2020-12-07 stsp goto done;
3359 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3360 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3361 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3362 5036bf37 2018-09-24 stsp goto done;
3364 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3365 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3367 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3368 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3369 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3370 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3372 11b20872 2019-11-08 stsp goto done;
3373 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3374 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3375 11b20872 2019-11-08 stsp if (err) {
3376 11b20872 2019-11-08 stsp free_colors(&s->colors);
3377 11b20872 2019-11-08 stsp goto done;
3379 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3380 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3381 11b20872 2019-11-08 stsp if (err) {
3382 11b20872 2019-11-08 stsp free_colors(&s->colors);
3383 11b20872 2019-11-08 stsp goto done;
3387 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3388 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3389 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3390 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3391 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3392 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3394 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3395 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3397 74467cc8 2022-06-15 stsp goto done;
3399 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3400 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3401 0ae84acc 2022-06-15 tracey if (err)
3402 0ae84acc 2022-06-15 tracey goto done;
3403 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3404 b672a97a 2020-01-27 stsp !s->log_branches);
3406 1a76625f 2018-10-22 stsp goto done;
3407 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3408 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3410 c5b78334 2020-01-12 stsp goto done;
3412 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3413 1a76625f 2018-10-22 stsp if (errcode) {
3414 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3415 1a76625f 2018-10-22 stsp goto done;
3417 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3418 7c1452c1 2020-03-26 stsp if (errcode) {
3419 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3420 7c1452c1 2020-03-26 stsp goto done;
3423 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3424 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3425 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3426 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3427 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3428 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3429 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3430 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3431 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3432 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3433 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3434 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3435 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3436 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3437 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3438 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3439 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3442 1a76625f 2018-10-22 stsp close_log_view(view);
3443 ba4f502b 2018-08-04 stsp return err;
3446 e5a0f69f 2018-08-18 stsp static const struct got_error *
3447 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3449 f2f6d207 2020-11-24 stsp const struct got_error *err;
3450 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3452 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3453 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3454 2b380cc8 2018-10-24 stsp &s->thread_args);
3455 2b380cc8 2018-10-24 stsp if (errcode)
3456 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3457 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3458 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3460 f2f6d207 2020-11-24 stsp return err;
3464 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3467 b880cc75 2022-06-30 mark static void
3468 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3470 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3472 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3474 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3475 568eae95 2022-09-11 mark view->count = 0;
3477 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3479 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3481 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3482 b880cc75 2022-06-30 mark --s->selected;
3484 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3486 b880cc75 2022-06-30 mark select_commit(s);
3490 b880cc75 2022-06-30 mark static const struct got_error *
3491 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3493 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3494 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3495 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3497 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3498 568eae95 2022-09-11 mark return NULL;
3500 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3501 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3502 b880cc75 2022-06-30 mark return NULL;
3504 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3505 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3507 631e7531 2022-08-12 mark if (!page) {
3508 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3509 b880cc75 2022-06-30 mark ++s->selected;
3511 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3512 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3513 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3516 631e7531 2022-08-12 mark s->selected = 0;
3517 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3518 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3519 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3520 631e7531 2022-08-12 mark if (entry == NULL)
3522 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3523 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3525 631e7531 2022-08-12 mark if (n > 0)
3526 631e7531 2022-08-12 mark s->selected = n - 1;
3528 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3529 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3530 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3531 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3533 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3536 b880cc75 2022-06-30 mark return err;
3539 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3540 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3542 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3543 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3544 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3545 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3548 b880cc75 2022-06-30 mark select_commit(s);
3550 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3551 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3552 b880cc75 2022-06-30 mark view->count = 0;
3554 b880cc75 2022-06-30 mark return NULL;
3557 9b058f45 2022-06-30 mark static void
3558 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3563 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3564 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3565 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3566 7532ccda 2022-07-11 mark else if (view->resized_y)
3567 7532ccda 2022-07-11 mark *y = view->resized_y;
3569 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3570 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3571 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3572 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3573 7532ccda 2022-07-11 mark else if (view->resized_x)
3574 7532ccda 2022-07-11 mark *x = view->resized_x;
3576 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3580 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3581 e5a0f69f 2018-08-18 stsp static const struct got_error *
3582 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3584 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3586 9b058f45 2022-06-30 mark view->nlines = y;
3587 d2366e29 2022-07-07 mark view->ncols = COLS;
3588 9b058f45 2022-06-30 mark err = view_resize(view);
3590 9b058f45 2022-06-30 mark return err;
3592 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3594 9b058f45 2022-06-30 mark return err;
3597 94b80cfa 2022-08-01 mark static const struct got_error *
3598 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3600 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3601 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3602 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3604 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3605 374f69dd 2022-08-13 stsp return NULL;
3607 94b80cfa 2022-08-01 mark g = view->gline;
3608 94b80cfa 2022-08-01 mark view->gline = 0;
3610 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3611 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3612 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3613 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3614 94b80cfa 2022-08-01 mark select_commit(s);
3615 94b80cfa 2022-08-01 mark return NULL;
3618 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3619 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3620 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3621 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3622 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3624 94b80cfa 2022-08-01 mark return err;
3625 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3626 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3628 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3629 94b80cfa 2022-08-01 mark s->selected = g - 1;
3631 94b80cfa 2022-08-01 mark select_commit(s);
3632 94b80cfa 2022-08-01 mark return NULL;
3636 9b058f45 2022-06-30 mark static const struct got_error *
3637 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3639 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3640 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3641 631e7531 2022-08-12 mark int eos, nscroll;
3643 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3644 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3645 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3646 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3647 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3648 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3651 11edf34c 2022-08-12 mark return err;
3654 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3655 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3656 0dca135e 2022-06-30 mark --eos; /* border */
3658 94b80cfa 2022-08-01 mark if (view->gline)
3659 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3661 528dedf3 2021-08-30 stsp switch (ch) {
3663 568eae95 2022-09-11 mark err = limit_log_view(view);
3666 1e37a5c2 2019-05-12 jcs s->quit = 1;
3669 145b6838 2022-06-16 stsp view->x = 0;
3672 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3673 640cd7ff 2022-06-22 mark view->count = 0;
3675 145b6838 2022-06-16 stsp case KEY_RIGHT:
3677 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3678 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3680 640cd7ff 2022-06-22 mark view->count = 0;
3682 145b6838 2022-06-16 stsp case KEY_LEFT:
3684 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3685 640cd7ff 2022-06-22 mark if (view->x <= 0)
3686 640cd7ff 2022-06-22 mark view->count = 0;
3689 1e37a5c2 2019-05-12 jcs case KEY_UP:
3692 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3693 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3696 912a3f79 2021-08-30 j case KEY_HOME:
3697 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3698 640cd7ff 2022-06-22 mark view->count = 0;
3700 83cc4199 2022-06-13 stsp case CTRL('u'):
3702 83cc4199 2022-06-13 stsp nscroll /= 2;
3703 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3704 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3705 a4292ac5 2019-05-12 jcs case CTRL('b'):
3707 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3710 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3713 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3714 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3717 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3720 912a3f79 2021-08-30 j case KEY_END: {
3721 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3722 912a3f79 2021-08-30 j * traverse them all. */
3723 640cd7ff 2022-06-22 mark view->count = 0;
3724 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3725 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3726 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3727 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3728 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3731 80b7e8da 2022-06-11 stsp case CTRL('d'):
3733 83cc4199 2022-06-13 stsp nscroll /= 2;
3734 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3735 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3736 61417565 2022-06-20 mark case CTRL('f'):
3737 48bb96f0 2022-06-20 naddy case 'f':
3739 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3741 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3742 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3743 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3744 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3745 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3746 2b779855 2020-12-05 naddy select_commit(s);
3747 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3748 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3749 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3750 568eae95 2022-09-11 mark s->commits->ncommits;
3751 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3754 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3755 49b24ee5 2022-07-03 mark case '\r':
3756 640cd7ff 2022-06-22 mark view->count = 0;
3757 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3759 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3762 640cd7ff 2022-06-22 mark view->count = 0;
3763 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3765 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3767 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3768 21355643 2020-12-06 stsp case CTRL('l'):
3770 640cd7ff 2022-06-22 mark view->count = 0;
3771 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3772 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3774 21355643 2020-12-06 stsp err = stop_log_thread(s);
3776 74cfe85e 2020-10-20 stsp return err;
3777 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3778 21355643 2020-12-06 stsp char *parent_path;
3779 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3781 21355643 2020-12-06 stsp return err;
3782 21355643 2020-12-06 stsp free(s->in_repo_path);
3783 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3784 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3785 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3786 21355643 2020-12-06 stsp struct got_object_id *start_id;
3787 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3788 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3789 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3790 ea371198 2022-11-17 stsp if (err) {
3791 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
3792 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
3793 ea371198 2022-11-17 stsp return err;
3794 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
3795 ea371198 2022-11-17 stsp free(s->head_ref_name);
3796 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
3797 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
3798 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3799 ea371198 2022-11-17 stsp &tog_refs, s->repo);
3801 ea371198 2022-11-17 stsp return err;
3803 21355643 2020-12-06 stsp free(s->start_id);
3804 21355643 2020-12-06 stsp s->start_id = start_id;
3805 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3806 21355643 2020-12-06 stsp } else /* 'B' */
3807 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3809 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3810 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3812 b0dd8d27 2022-06-16 stsp return err;
3814 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3815 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3816 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3818 21355643 2020-12-06 stsp return err;
3819 51a10b52 2020-12-26 stsp tog_free_refs();
3820 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3822 ca51c541 2020-12-07 stsp return err;
3823 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3824 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3826 d01904d4 2019-06-25 stsp return err;
3827 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3828 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3830 b672a97a 2020-01-27 stsp return err;
3831 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3832 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3833 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3834 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3835 21355643 2020-12-06 stsp s->selected_entry = NULL;
3836 21355643 2020-12-06 stsp s->selected = 0;
3837 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3838 21355643 2020-12-06 stsp s->quit = 0;
3839 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3840 dfee752e 2022-06-18 op s->matched_entry = NULL;
3841 dfee752e 2022-06-18 op s->search_entry = NULL;
3842 01a7bcaf 2022-07-22 mark view->offset = 0;
3845 640cd7ff 2022-06-22 mark view->count = 0;
3846 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3849 640cd7ff 2022-06-22 mark view->count = 0;
3853 80ddbec8 2018-04-29 stsp return err;
3856 4ed7e80c 2018-05-20 stsp static const struct got_error *
3857 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3859 c2db6724 2019-01-04 stsp const struct got_error *error;
3861 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3862 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3863 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3865 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3866 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3868 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3869 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3871 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3872 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3874 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3875 c2db6724 2019-01-04 stsp if (error != NULL)
3876 c2db6724 2019-01-04 stsp return error;
3878 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3879 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3881 c2db6724 2019-01-04 stsp return NULL;
3884 a915003a 2019-02-05 stsp static void
3885 a915003a 2019-02-05 stsp init_curses(void)
3888 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3889 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3890 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3892 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3893 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3894 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3895 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3896 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3898 a915003a 2019-02-05 stsp initscr();
3900 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3903 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3904 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3905 a915003a 2019-02-05 stsp curs_set(0);
3906 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3907 6d17833f 2019-11-08 stsp start_color();
3908 6d17833f 2019-11-08 stsp use_default_colors();
3912 c2db6724 2019-01-04 stsp static const struct got_error *
3913 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3914 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3916 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3918 f135c941 2020-02-20 stsp if (argc == 0) {
3919 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3920 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3921 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3922 f135c941 2020-02-20 stsp return NULL;
3925 f135c941 2020-02-20 stsp if (worktree) {
3926 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3929 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3931 f135c941 2020-02-20 stsp return err;
3932 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3933 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3934 bfd61697 2020-11-14 stsp p) == -1) {
3935 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3936 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3940 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3942 f135c941 2020-02-20 stsp return err;
3945 f135c941 2020-02-20 stsp static const struct got_error *
3946 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3948 80ddbec8 2018-04-29 stsp const struct got_error *error;
3949 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3950 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3951 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3952 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3953 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3954 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3955 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3956 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3957 04cc582a 2018-08-01 stsp struct tog_view *view;
3958 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3960 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3961 80ddbec8 2018-04-29 stsp switch (ch) {
3963 b672a97a 2020-01-27 stsp log_branches = 1;
3966 80ddbec8 2018-04-29 stsp start_commit = optarg;
3969 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3970 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3971 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3975 17020d27 2019-03-07 stsp usage_log();
3976 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3980 80ddbec8 2018-04-29 stsp argc -= optind;
3981 80ddbec8 2018-04-29 stsp argv += optind;
3983 f135c941 2020-02-20 stsp if (argc > 1)
3984 f135c941 2020-02-20 stsp usage_log();
3986 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3987 0ae84acc 2022-06-15 tracey if (error != NULL)
3988 0ae84acc 2022-06-15 tracey goto done;
3990 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3991 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3992 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3993 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3994 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3995 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3996 c156c7a4 2020-12-18 stsp goto done;
3997 a1fbf39a 2019-08-11 stsp if (worktree)
3998 6962eb72 2020-02-20 stsp repo_path =
3999 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4001 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4002 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4003 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4004 c156c7a4 2020-12-18 stsp goto done;
4008 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4009 80ddbec8 2018-04-29 stsp if (error != NULL)
4010 ecb28ae0 2018-07-16 stsp goto done;
4012 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4013 f135c941 2020-02-20 stsp repo, worktree);
4014 f135c941 2020-02-20 stsp if (error)
4015 f135c941 2020-02-20 stsp goto done;
4017 f135c941 2020-02-20 stsp init_curses();
4019 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4020 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4021 c02c541e 2019-03-29 stsp if (error)
4022 c02c541e 2019-03-29 stsp goto done;
4024 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4025 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4026 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4027 87670572 2020-12-26 naddy if (error)
4028 87670572 2020-12-26 naddy goto done;
4031 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4032 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4033 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4034 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4035 d8f38dc4 2020-12-05 stsp if (error)
4036 d8f38dc4 2020-12-05 stsp goto done;
4037 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4039 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4040 d8f38dc4 2020-12-05 stsp if (error == NULL)
4041 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4042 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4043 d8f38dc4 2020-12-05 stsp goto done;
4044 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4045 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4046 d8f38dc4 2020-12-05 stsp if (error)
4047 d8f38dc4 2020-12-05 stsp goto done;
4050 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4051 04cc582a 2018-08-01 stsp if (view == NULL) {
4052 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4053 04cc582a 2018-08-01 stsp goto done;
4055 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4056 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4057 ba4f502b 2018-08-04 stsp if (error)
4058 ba4f502b 2018-08-04 stsp goto done;
4059 2fc00ff4 2019-08-31 stsp if (worktree) {
4060 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4061 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4062 2fc00ff4 2019-08-31 stsp worktree = NULL;
4064 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4066 f135c941 2020-02-20 stsp free(in_repo_path);
4067 ecb28ae0 2018-07-16 stsp free(repo_path);
4068 ecb28ae0 2018-07-16 stsp free(cwd);
4069 899d86c2 2018-05-10 stsp free(start_id);
4070 d8f38dc4 2020-12-05 stsp free(label);
4072 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4073 1d0f4054 2021-06-17 stsp if (repo) {
4074 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4075 1d0f4054 2021-06-17 stsp if (error == NULL)
4076 1d0f4054 2021-06-17 stsp error = close_err;
4078 ec142235 2019-03-07 stsp if (worktree)
4079 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4080 0ae84acc 2022-06-15 tracey if (pack_fds) {
4081 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4082 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4083 0ae84acc 2022-06-15 tracey if (error == NULL)
4084 0ae84acc 2022-06-15 tracey error = pack_err;
4086 51a10b52 2020-12-26 stsp tog_free_refs();
4087 80ddbec8 2018-04-29 stsp return error;
4090 4ed7e80c 2018-05-20 stsp __dead static void
4091 9f7d7167 2018-04-29 stsp usage_diff(void)
4094 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4095 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4099 6d17833f 2019-11-08 stsp static int
4100 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4101 41605754 2020-11-12 stsp regmatch_t *regmatch)
4103 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4106 336075a4 2022-06-25 op static struct tog_color *
4107 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4109 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4111 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4112 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4113 6d17833f 2019-11-08 stsp return tc;
4116 6d17833f 2019-11-08 stsp return NULL;
4119 4ed7e80c 2018-05-20 stsp static const struct got_error *
4120 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4121 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4123 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4124 44a87665 2022-06-16 stsp char *exstr = NULL;
4125 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4126 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4127 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4128 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4130 41605754 2020-11-12 stsp *wtotal = 0;
4132 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4133 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4135 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4137 44a87665 2022-06-16 stsp return err;
4139 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4140 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4141 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4142 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4143 44a87665 2022-06-16 stsp goto done;
4145 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4146 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4147 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4148 1853e0f4 2022-06-16 stsp goto done;
4150 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4151 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4152 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4153 1853e0f4 2022-06-16 stsp goto done;
4156 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4157 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4158 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4160 1853e0f4 2022-06-16 stsp goto done;
4161 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4163 1853e0f4 2022-06-16 stsp free(wline);
4164 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4165 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4167 1853e0f4 2022-06-16 stsp goto done;
4168 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4169 1853e0f4 2022-06-16 stsp wlimit -= width;
4170 1853e0f4 2022-06-16 stsp *wtotal += width;
4173 41605754 2020-11-12 stsp if (wlimit > 0) {
4174 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4175 1853e0f4 2022-06-16 stsp size_t wlen;
4177 1853e0f4 2022-06-16 stsp free(wline);
4178 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4179 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4181 1853e0f4 2022-06-16 stsp goto done;
4182 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4183 1853e0f4 2022-06-16 stsp while (i < wlen) {
4184 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4185 1853e0f4 2022-06-16 stsp if (width == -1) {
4186 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4187 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4188 1853e0f4 2022-06-16 stsp goto done;
4190 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4192 61417565 2022-06-20 mark w += width;
4195 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4196 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4197 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4198 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4199 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4200 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4201 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4205 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4206 1853e0f4 2022-06-16 stsp free(wline);
4207 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4208 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4209 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4210 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4212 1853e0f4 2022-06-16 stsp goto done;
4213 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4215 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4216 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4218 1853e0f4 2022-06-16 stsp goto done;
4219 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4221 1853e0f4 2022-06-16 stsp *wtotal += width2;
4224 145b6838 2022-06-16 stsp free(wline);
4225 44a87665 2022-06-16 stsp free(exstr);
4226 1853e0f4 2022-06-16 stsp free(seg0);
4227 1853e0f4 2022-06-16 stsp free(seg1);
4228 1853e0f4 2022-06-16 stsp free(seg2);
4229 1853e0f4 2022-06-16 stsp return err;
4232 94b80cfa 2022-08-01 mark static int
4233 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4235 94b80cfa 2022-08-01 mark FILE *f = NULL;
4236 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4238 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4239 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4241 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4242 ec2a9698 2022-09-15 mark selected = first;
4243 ec2a9698 2022-09-15 mark eof = &s->eof;
4245 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4246 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4248 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4249 94b80cfa 2022-08-01 mark selected = first;
4250 94b80cfa 2022-08-01 mark eof = &s->eof;
4252 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4253 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4255 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4256 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4257 94b80cfa 2022-08-01 mark eof = &s->eof;
4258 94b80cfa 2022-08-01 mark f = s->blame.f;
4262 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4263 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4265 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4266 94b80cfa 2022-08-01 mark rewind(f);
4268 94b80cfa 2022-08-01 mark *first = 1;
4269 94b80cfa 2022-08-01 mark *lineno = 0;
4270 94b80cfa 2022-08-01 mark *nprinted = 0;
4274 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4275 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4276 94b80cfa 2022-08-01 mark view->gline = 0;
4281 41605754 2020-11-12 stsp static const struct got_error *
4282 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4284 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4285 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4286 61e69b96 2018-05-20 stsp const struct got_error *err;
4287 c7d5c43c 2022-08-04 mark int nprinted = 0;
4288 b304db33 2018-05-20 stsp char *line;
4289 826082fe 2020-12-10 stsp size_t linesize = 0;
4290 826082fe 2020-12-10 stsp ssize_t linelen;
4291 61e69b96 2018-05-20 stsp wchar_t *wline;
4292 e0b650dd 2018-05-20 stsp int width;
4293 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4294 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4295 fe621944 2020-11-10 stsp off_t line_offset;
4297 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4298 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4299 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4300 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4302 f7d12f7e 2018-08-01 stsp werase(view->window);
4304 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4305 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4307 a3404814 2018-09-02 stsp if (header) {
4308 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4309 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4310 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4312 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4313 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4314 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4316 135a2da0 2020-11-11 stsp free(line);
4318 a3404814 2018-09-02 stsp return err;
4320 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4321 a3404814 2018-09-02 stsp wstandout(view->window);
4322 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4323 e54cc94a 2020-11-11 stsp free(wline);
4324 e54cc94a 2020-11-11 stsp wline = NULL;
4325 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4326 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4327 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4328 a3404814 2018-09-02 stsp wstandend(view->window);
4330 a3404814 2018-09-02 stsp if (max_lines <= 1)
4331 a3404814 2018-09-02 stsp return NULL;
4332 a3404814 2018-09-02 stsp max_lines--;
4335 89f1a395 2020-12-01 naddy s->eof = 0;
4336 145b6838 2022-06-16 stsp view->maxx = 0;
4337 826082fe 2020-12-10 stsp line = NULL;
4338 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4339 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4340 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4342 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4343 826082fe 2020-12-10 stsp if (linelen == -1) {
4344 826082fe 2020-12-10 stsp if (feof(s->f)) {
4345 826082fe 2020-12-10 stsp s->eof = 1;
4348 826082fe 2020-12-10 stsp free(line);
4349 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4352 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4354 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4356 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4357 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4359 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4360 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4361 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4362 1853e0f4 2022-06-16 stsp if (err) {
4363 1853e0f4 2022-06-16 stsp free(line);
4364 1853e0f4 2022-06-16 stsp return err;
4366 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4367 1853e0f4 2022-06-16 stsp free(wline);
4368 1853e0f4 2022-06-16 stsp wline = NULL;
4370 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4371 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4372 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4373 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4375 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4376 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4377 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4378 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4379 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4380 41605754 2020-11-12 stsp if (err) {
4381 41605754 2020-11-12 stsp free(line);
4382 41605754 2020-11-12 stsp return err;
4386 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4387 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4388 969c159c 2022-06-16 stsp if (err) {
4389 969c159c 2022-06-16 stsp free(line);
4390 969c159c 2022-06-16 stsp return err;
4392 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4393 969c159c 2022-06-16 stsp free(wline);
4394 41605754 2020-11-12 stsp wline = NULL;
4396 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4397 94b80cfa 2022-08-01 mark /* highlight full gline length */
4398 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4399 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4401 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4402 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4405 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4406 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4407 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4409 826082fe 2020-12-10 stsp free(line);
4410 fe621944 2020-11-10 stsp if (nprinted >= 1)
4411 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4412 89f1a395 2020-12-01 naddy (nprinted - 1);
4414 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4416 9b058f45 2022-06-30 mark view_border(view);
4418 89f1a395 2020-12-01 naddy if (s->eof) {
4419 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4420 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4421 c3e9aa98 2019-05-13 jcs nprinted++;
4424 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4425 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4427 c3e9aa98 2019-05-13 jcs return err;
4430 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4431 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4432 e54cc94a 2020-11-11 stsp free(wline);
4433 e54cc94a 2020-11-11 stsp wline = NULL;
4434 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4437 26ed57b2 2018-05-19 stsp return NULL;
4440 abd2672a 2018-12-23 stsp static char *
4441 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4443 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4444 09867e48 2019-08-13 stsp char *p, *s;
4446 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4447 09867e48 2019-08-13 stsp if (tm == NULL)
4448 09867e48 2019-08-13 stsp return NULL;
4449 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4450 09867e48 2019-08-13 stsp if (s == NULL)
4451 09867e48 2019-08-13 stsp return NULL;
4452 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4454 abd2672a 2018-12-23 stsp *p = '\0';
4458 4ed7e80c 2018-05-20 stsp static const struct got_error *
4459 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4460 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4462 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4463 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4464 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4465 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4467 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4468 0208f208 2020-05-05 stsp if (qid != NULL) {
4469 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4470 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4471 d7b5a0e8 2022-04-20 stsp &qid->id);
4473 0208f208 2020-05-05 stsp return err;
4475 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4476 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4477 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4478 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4479 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4481 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4485 0208f208 2020-05-05 stsp if (tree_id1) {
4486 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4488 0208f208 2020-05-05 stsp goto done;
4491 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4492 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4494 0208f208 2020-05-05 stsp goto done;
4496 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4497 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4499 0208f208 2020-05-05 stsp if (tree1)
4500 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4501 0208f208 2020-05-05 stsp if (tree2)
4502 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4503 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4504 0208f208 2020-05-05 stsp return err;
4507 0208f208 2020-05-05 stsp static const struct got_error *
4508 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4509 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4511 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4513 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4514 fe621944 2020-11-10 stsp if (p == NULL)
4515 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4516 c7d5c43c 2022-08-04 mark *lines = p;
4517 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4518 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4519 fe621944 2020-11-10 stsp (*nlines)++;
4521 fe621944 2020-11-10 stsp return NULL;
4524 fe621944 2020-11-10 stsp static const struct got_error *
4525 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4526 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4527 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4529 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4530 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4531 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4532 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4533 45d799e2 2018-12-23 stsp time_t committer_time;
4534 45d799e2 2018-12-23 stsp const char *author, *committer;
4535 8b473291 2019-02-21 stsp char *refs_str = NULL;
4536 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4537 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4538 fe621944 2020-11-10 stsp off_t outoff = 0;
4541 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4543 8b473291 2019-02-21 stsp if (refs) {
4544 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4546 8b473291 2019-02-21 stsp return err;
4549 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4551 abd2672a 2018-12-23 stsp return err;
4553 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4554 15a94983 2018-12-23 stsp if (err) {
4555 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4556 15a94983 2018-12-23 stsp goto done;
4559 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4561 fe621944 2020-11-10 stsp goto done;
4563 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4564 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4565 fe621944 2020-11-10 stsp if (n < 0) {
4566 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4567 abd2672a 2018-12-23 stsp goto done;
4569 fe621944 2020-11-10 stsp outoff += n;
4570 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4572 fe621944 2020-11-10 stsp goto done;
4574 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4575 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4576 fe621944 2020-11-10 stsp if (n < 0) {
4577 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4578 abd2672a 2018-12-23 stsp goto done;
4580 fe621944 2020-11-10 stsp outoff += n;
4581 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4583 fe621944 2020-11-10 stsp goto done;
4585 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4586 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4587 fe621944 2020-11-10 stsp if (datestr) {
4588 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4589 fe621944 2020-11-10 stsp if (n < 0) {
4590 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4591 fe621944 2020-11-10 stsp goto done;
4593 fe621944 2020-11-10 stsp outoff += n;
4594 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4595 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_DATE);
4597 fe621944 2020-11-10 stsp goto done;
4599 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4600 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4601 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4602 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4603 fe621944 2020-11-10 stsp if (n < 0) {
4604 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4605 fe621944 2020-11-10 stsp goto done;
4607 fe621944 2020-11-10 stsp outoff += n;
4608 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4609 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_AUTHOR);
4611 fe621944 2020-11-10 stsp goto done;
4613 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4614 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4615 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4616 9f98ca05 2021-09-24 stsp int pn = 1;
4617 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4618 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4619 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4621 9f98ca05 2021-09-24 stsp goto done;
4622 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4623 9f98ca05 2021-09-24 stsp if (n < 0) {
4624 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4625 9f98ca05 2021-09-24 stsp goto done;
4627 9f98ca05 2021-09-24 stsp outoff += n;
4628 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4629 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4631 9f98ca05 2021-09-24 stsp goto done;
4632 9f98ca05 2021-09-24 stsp free(id_str);
4633 9f98ca05 2021-09-24 stsp id_str = NULL;
4637 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4639 5943eee2 2019-08-13 stsp goto done;
4640 fe621944 2020-11-10 stsp s = logmsg;
4641 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4642 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4643 fe621944 2020-11-10 stsp if (n < 0) {
4644 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4645 fe621944 2020-11-10 stsp goto done;
4647 fe621944 2020-11-10 stsp outoff += n;
4648 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4649 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4651 fe621944 2020-11-10 stsp goto done;
4654 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4656 0208f208 2020-05-05 stsp goto done;
4657 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4658 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4659 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4660 fe621944 2020-11-10 stsp if (n < 0) {
4661 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4662 fe621944 2020-11-10 stsp goto done;
4664 fe621944 2020-11-10 stsp outoff += n;
4665 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4666 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4668 fe621944 2020-11-10 stsp goto done;
4669 0208f208 2020-05-05 stsp free((char *)pe->path);
4670 0208f208 2020-05-05 stsp free(pe->data);
4673 0208f208 2020-05-05 stsp fputc('\n', outfile);
4675 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4677 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4678 abd2672a 2018-12-23 stsp free(id_str);
4679 5943eee2 2019-08-13 stsp free(logmsg);
4680 8b473291 2019-02-21 stsp free(refs_str);
4681 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4682 7510f233 2020-08-09 stsp if (err) {
4683 c7d5c43c 2022-08-04 mark free(*lines);
4684 c7d5c43c 2022-08-04 mark *lines = NULL;
4685 ae6a6978 2020-08-09 stsp *nlines = 0;
4687 fe621944 2020-11-10 stsp return err;
4690 abd2672a 2018-12-23 stsp static const struct got_error *
4691 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4693 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4694 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4695 15a94983 2018-12-23 stsp int obj_type;
4697 c7d5c43c 2022-08-04 mark free(s->lines);
4698 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4699 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4700 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4701 fe621944 2020-11-10 stsp s->nlines = 0;
4703 511a516b 2018-05-19 stsp f = got_opentemp();
4704 48ae06ee 2018-10-18 stsp if (f == NULL) {
4705 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4706 48ae06ee 2018-10-18 stsp goto done;
4708 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4709 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4710 fb43ecf1 2019-02-11 stsp goto done;
4714 15a94983 2018-12-23 stsp if (s->id1)
4715 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4717 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4719 15a94983 2018-12-23 stsp goto done;
4721 15a94983 2018-12-23 stsp switch (obj_type) {
4722 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4723 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4724 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4725 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4726 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4728 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4729 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4730 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4731 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4732 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4734 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4735 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4736 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4737 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4738 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4740 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4742 3ffacbe1 2020-02-02 tracey goto done;
4743 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4744 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4745 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4746 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4747 c7d5c43c 2022-08-04 mark refs, s->repo, s->f);
4748 f44b1f58 2020-02-02 tracey if (err)
4749 f44b1f58 2020-02-02 tracey goto done;
4750 f44b1f58 2020-02-02 tracey } else {
4751 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4752 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4753 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4754 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4755 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4757 f44b1f58 2020-02-02 tracey if (err)
4758 f44b1f58 2020-02-02 tracey goto done;
4763 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4765 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4766 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4767 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4768 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4772 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4776 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4777 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4778 48ae06ee 2018-10-18 stsp return err;
4781 f5215bb9 2019-02-22 stsp static void
4782 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4784 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4785 f5215bb9 2019-02-22 stsp update_panels();
4786 f5215bb9 2019-02-22 stsp doupdate();
4789 f44b1f58 2020-02-02 tracey static const struct got_error *
4790 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4792 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4794 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4795 f44b1f58 2020-02-02 tracey return NULL;
4798 eaf2c13e 2022-09-17 mark static void
4799 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4800 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4802 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4804 eaf2c13e 2022-09-17 mark *f = s->f;
4805 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4806 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4807 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4808 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4809 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4810 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4813 ec2a9698 2022-09-15 mark static const struct got_error *
4814 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4816 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4818 f44b1f58 2020-02-02 tracey int lineno;
4819 cb713507 2022-06-16 stsp char *line = NULL;
4820 826082fe 2020-12-10 stsp size_t linesize = 0;
4821 826082fe 2020-12-10 stsp ssize_t linelen;
4822 ec2a9698 2022-09-15 mark off_t *line_offsets;
4823 ec2a9698 2022-09-15 mark size_t nlines = 0;
4824 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4826 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4827 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4828 eaf2c13e 2022-09-17 mark "view search not supported");
4829 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4830 ec2a9698 2022-09-15 mark &match, &selected);
4832 f44b1f58 2020-02-02 tracey if (!view->searching) {
4833 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4834 f44b1f58 2020-02-02 tracey return NULL;
4837 ec2a9698 2022-09-15 mark if (*match) {
4838 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4839 ec2a9698 2022-09-15 mark lineno = *match + 1;
4841 ec2a9698 2022-09-15 mark lineno = *match - 1;
4843 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4845 f44b1f58 2020-02-02 tracey while (1) {
4846 f44b1f58 2020-02-02 tracey off_t offset;
4848 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4849 ec2a9698 2022-09-15 mark if (*match == 0) {
4850 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4854 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4855 f44b1f58 2020-02-02 tracey lineno = 1;
4857 ec2a9698 2022-09-15 mark lineno = nlines;
4860 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
4861 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
4862 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
4863 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
4864 f44b1f58 2020-02-02 tracey free(line);
4865 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4867 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
4868 cb713507 2022-06-16 stsp if (linelen != -1) {
4869 cb713507 2022-06-16 stsp char *exstr;
4870 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4873 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4874 cb713507 2022-06-16 stsp &view->regmatch)) {
4875 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4876 ec2a9698 2022-09-15 mark *match = lineno;
4877 cb713507 2022-06-16 stsp free(exstr);
4880 cb713507 2022-06-16 stsp free(exstr);
4882 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4883 f44b1f58 2020-02-02 tracey lineno++;
4885 f44b1f58 2020-02-02 tracey lineno--;
4887 826082fe 2020-12-10 stsp free(line);
4889 ec2a9698 2022-09-15 mark if (*match) {
4890 ec2a9698 2022-09-15 mark *first = *match;
4891 ec2a9698 2022-09-15 mark *selected = 1;
4894 145b6838 2022-06-16 stsp return err;
4897 48ae06ee 2018-10-18 stsp static const struct got_error *
4898 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4900 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4901 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4903 b72706c3 2022-06-01 stsp free(s->id1);
4904 b72706c3 2022-06-01 stsp s->id1 = NULL;
4905 b72706c3 2022-06-01 stsp free(s->id2);
4906 b72706c3 2022-06-01 stsp s->id2 = NULL;
4907 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4908 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4909 b72706c3 2022-06-01 stsp s->f = NULL;
4910 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4911 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4912 b72706c3 2022-06-01 stsp s->f1 = NULL;
4913 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4914 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4915 b72706c3 2022-06-01 stsp s->f2 = NULL;
4916 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4917 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4918 f9d37699 2022-06-28 stsp s->fd1 = -1;
4919 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4920 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4921 f9d37699 2022-06-28 stsp s->fd2 = -1;
4922 c7d5c43c 2022-08-04 mark free(s->lines);
4923 c7d5c43c 2022-08-04 mark s->lines = NULL;
4924 b72706c3 2022-06-01 stsp s->nlines = 0;
4925 b72706c3 2022-06-01 stsp return err;
4928 b72706c3 2022-06-01 stsp static const struct got_error *
4929 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4930 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4931 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4932 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4934 48ae06ee 2018-10-18 stsp const struct got_error *err;
4935 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4937 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4938 f9d37699 2022-06-28 stsp s->fd1 = -1;
4939 f9d37699 2022-06-28 stsp s->fd2 = -1;
4941 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4942 15a94983 2018-12-23 stsp int type1, type2;
4943 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4945 15a94983 2018-12-23 stsp return err;
4946 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4948 15a94983 2018-12-23 stsp return err;
4950 15a94983 2018-12-23 stsp if (type1 != type2)
4951 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4953 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4954 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4955 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4956 f44b1f58 2020-02-02 tracey s->repo = repo;
4957 f44b1f58 2020-02-02 tracey s->id1 = id1;
4958 f44b1f58 2020-02-02 tracey s->id2 = id2;
4959 3dbaef42 2020-11-24 stsp s->label1 = label1;
4960 3dbaef42 2020-11-24 stsp s->label2 = label2;
4962 15a94983 2018-12-23 stsp if (id1) {
4963 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4964 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4965 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4967 5465d566 2020-02-01 tracey s->id1 = NULL;
4969 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4970 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4971 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4972 b72706c3 2022-06-01 stsp goto done;
4975 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4976 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4977 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4978 a00719e9 2022-06-17 stsp goto done;
4981 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4982 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4983 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4984 b72706c3 2022-06-01 stsp goto done;
4987 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4988 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4989 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4990 f9d37699 2022-06-28 stsp goto done;
4993 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4994 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4995 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4996 f9d37699 2022-06-28 stsp goto done;
4999 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
5000 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
5001 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5002 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5003 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5004 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5005 5465d566 2020-02-01 tracey s->repo = repo;
5007 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5010 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5011 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5012 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5013 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5014 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5015 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5016 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5017 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5018 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5019 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5020 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5021 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5022 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5023 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5024 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5025 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5026 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5027 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5028 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5029 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5030 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5031 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5032 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5033 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5034 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5035 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5036 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5037 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5038 b72706c3 2022-06-01 stsp goto done;
5042 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5043 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5044 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5045 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5047 5465d566 2020-02-01 tracey err = create_diff(s);
5049 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5050 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5051 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5052 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5053 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5054 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5055 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5058 b72706c3 2022-06-01 stsp close_diff_view(view);
5059 e5a0f69f 2018-08-18 stsp return err;
5062 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5063 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5065 a3404814 2018-09-02 stsp const struct got_error *err;
5066 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5067 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5068 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5070 a3404814 2018-09-02 stsp if (s->id1) {
5071 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5073 a3404814 2018-09-02 stsp return err;
5074 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5076 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5078 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5080 a3404814 2018-09-02 stsp return err;
5081 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5083 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5084 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5085 a3404814 2018-09-02 stsp free(id_str1);
5086 a3404814 2018-09-02 stsp free(id_str2);
5087 a3404814 2018-09-02 stsp return err;
5089 a3404814 2018-09-02 stsp free(id_str1);
5090 a3404814 2018-09-02 stsp free(id_str2);
5092 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5093 267bb3b8 2021-08-01 stsp free(header);
5094 267bb3b8 2021-08-01 stsp return err;
5097 15a087fe 2019-02-21 stsp static const struct got_error *
5098 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5099 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5101 d7a04538 2019-02-21 stsp const struct got_error *err;
5102 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5103 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5104 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5106 15a087fe 2019-02-21 stsp free(s->id2);
5107 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5108 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5109 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5111 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5113 d7a04538 2019-02-21 stsp return err;
5114 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5115 15a087fe 2019-02-21 stsp free(s->id1);
5116 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5117 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5118 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5119 15a087fe 2019-02-21 stsp return NULL;
5122 0cf4efb1 2018-09-29 stsp static const struct got_error *
5123 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5125 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5127 917d79a7 2022-07-01 stsp view->count = 0;
5128 917d79a7 2022-07-01 stsp wclear(view->window);
5129 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5130 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5131 917d79a7 2022-07-01 stsp s->matched_line = 0;
5132 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5133 917d79a7 2022-07-01 stsp return create_diff(s);
5136 c7d5c43c 2022-08-04 mark static void
5137 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5139 c7d5c43c 2022-08-04 mark int start, i;
5141 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5143 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5144 c7d5c43c 2022-08-04 mark if (i == 0)
5145 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5146 c7d5c43c 2022-08-04 mark if (--i == start)
5147 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5150 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5151 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5154 c7d5c43c 2022-08-04 mark static void
5155 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5157 c7d5c43c 2022-08-04 mark int start, i;
5159 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5161 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5162 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5164 c7d5c43c 2022-08-04 mark if (++i == start)
5165 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5168 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5169 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5172 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5173 c0f61fa4 2022-07-11 mark int, int, int);
5174 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5175 c0f61fa4 2022-07-11 mark int, int);
5177 917d79a7 2022-07-01 stsp static const struct got_error *
5178 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5180 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5181 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5182 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5183 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5184 826082fe 2020-12-10 stsp char *line = NULL;
5185 826082fe 2020-12-10 stsp size_t linesize = 0;
5186 826082fe 2020-12-10 stsp ssize_t linelen;
5187 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5189 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5191 e5a0f69f 2018-08-18 stsp switch (ch) {
5193 145b6838 2022-06-16 stsp view->x = 0;
5196 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5197 640cd7ff 2022-06-22 mark view->count = 0;
5199 145b6838 2022-06-16 stsp case KEY_RIGHT:
5201 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5202 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5204 640cd7ff 2022-06-22 mark view->count = 0;
5206 145b6838 2022-06-16 stsp case KEY_LEFT:
5208 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5209 640cd7ff 2022-06-22 mark if (view->x <= 0)
5210 640cd7ff 2022-06-22 mark view->count = 0;
5214 3dbaef42 2020-11-24 stsp if (ch == 'a')
5215 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5216 3dbaef42 2020-11-24 stsp if (ch == 'w')
5217 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5218 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5221 912a3f79 2021-08-30 j case KEY_HOME:
5222 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5223 640cd7ff 2022-06-22 mark view->count = 0;
5226 912a3f79 2021-08-30 j case KEY_END:
5227 640cd7ff 2022-06-22 mark view->count = 0;
5231 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5235 1e37a5c2 2019-05-12 jcs case KEY_UP:
5236 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5237 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5238 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5240 640cd7ff 2022-06-22 mark view->count = 0;
5242 83cc4199 2022-06-13 stsp case CTRL('u'):
5244 83cc4199 2022-06-13 stsp nscroll /= 2;
5245 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5246 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5247 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5249 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5250 640cd7ff 2022-06-22 mark view->count = 0;
5254 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5255 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5258 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5259 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5260 1e37a5c2 2019-05-12 jcs if (!s->eof)
5261 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5263 640cd7ff 2022-06-22 mark view->count = 0;
5265 83cc4199 2022-06-13 stsp case CTRL('d'):
5267 83cc4199 2022-06-13 stsp nscroll /= 2;
5268 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5269 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5270 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5273 640cd7ff 2022-06-22 mark if (s->eof) {
5274 640cd7ff 2022-06-22 mark view->count = 0;
5278 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5279 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5280 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5281 826082fe 2020-12-10 stsp if (linelen == -1) {
5282 826082fe 2020-12-10 stsp if (feof(s->f)) {
5283 826082fe 2020-12-10 stsp s->eof = 1;
5285 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5289 826082fe 2020-12-10 stsp free(line);
5292 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5295 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5298 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5301 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5304 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5305 1e37a5c2 2019-05-12 jcs s->diff_context--;
5306 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5307 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5308 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5309 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5310 27829c9e 2020-11-21 stsp s->nlines) {
5311 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5312 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5315 640cd7ff 2022-06-22 mark view->count = 0;
5318 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5319 1e37a5c2 2019-05-12 jcs s->diff_context++;
5320 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5321 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5322 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5324 640cd7ff 2022-06-22 mark view->count = 0;
5330 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5334 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5335 640cd7ff 2022-06-22 mark view->count = 0;
5338 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5340 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5341 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5342 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5344 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5345 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5348 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5350 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5353 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5356 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5357 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5358 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5360 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5361 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5362 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5364 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5365 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5368 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5370 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5372 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5373 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5374 c0f61fa4 2022-07-11 mark bs->selected_line);
5375 c0f61fa4 2022-07-11 mark if (id == NULL)
5378 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5381 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5385 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5386 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5387 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5388 145b6838 2022-06-16 stsp view->x = 0;
5390 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5391 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5394 640cd7ff 2022-06-22 mark view->count = 0;
5398 bcbd79e2 2018-08-19 stsp return err;
5401 4ed7e80c 2018-05-20 stsp static const struct got_error *
5402 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5404 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5405 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5406 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5407 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5408 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5409 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5410 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5411 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5412 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5413 3dbaef42 2020-11-24 stsp const char *errstr;
5414 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5415 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5417 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5418 26ed57b2 2018-05-19 stsp switch (ch) {
5420 64453f7e 2020-11-21 stsp force_text_diff = 1;
5423 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5425 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5426 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5427 5a20d08d 2022-02-09 op errstr, errstr);
5429 09b5bff8 2020-02-23 naddy case 'r':
5430 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5431 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5432 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5434 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5437 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5440 17020d27 2019-03-07 stsp usage_diff();
5441 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5445 26ed57b2 2018-05-19 stsp argc -= optind;
5446 26ed57b2 2018-05-19 stsp argv += optind;
5448 26ed57b2 2018-05-19 stsp if (argc == 0) {
5449 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5450 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5451 15a94983 2018-12-23 stsp id_str1 = argv[0];
5452 15a94983 2018-12-23 stsp id_str2 = argv[1];
5454 26ed57b2 2018-05-19 stsp usage_diff();
5456 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5457 0ae84acc 2022-06-15 tracey if (error)
5458 0ae84acc 2022-06-15 tracey goto done;
5460 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5461 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5462 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5463 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5464 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5465 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5466 c156c7a4 2020-12-18 stsp goto done;
5467 a273ac94 2020-02-23 naddy if (worktree)
5468 a273ac94 2020-02-23 naddy repo_path =
5469 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5471 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5472 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5473 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5474 c156c7a4 2020-12-18 stsp goto done;
5478 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5479 eb6600df 2019-01-04 stsp if (error)
5480 eb6600df 2019-01-04 stsp goto done;
5482 a273ac94 2020-02-23 naddy init_curses();
5484 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5485 51a10b52 2020-12-26 stsp if (error)
5486 51a10b52 2020-12-26 stsp goto done;
5488 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5489 26ed57b2 2018-05-19 stsp if (error)
5490 26ed57b2 2018-05-19 stsp goto done;
5492 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5493 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5494 26ed57b2 2018-05-19 stsp if (error)
5495 26ed57b2 2018-05-19 stsp goto done;
5497 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5498 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5499 26ed57b2 2018-05-19 stsp if (error)
5500 26ed57b2 2018-05-19 stsp goto done;
5502 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5503 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5504 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5505 ea5e7bb5 2018-08-01 stsp goto done;
5507 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5508 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5509 5dc9f4bc 2018-08-04 stsp if (error)
5510 5dc9f4bc 2018-08-04 stsp goto done;
5511 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5513 3dbaef42 2020-11-24 stsp free(label1);
5514 3dbaef42 2020-11-24 stsp free(label2);
5515 c02c541e 2019-03-29 stsp free(repo_path);
5516 a273ac94 2020-02-23 naddy free(cwd);
5517 1d0f4054 2021-06-17 stsp if (repo) {
5518 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5519 1d0f4054 2021-06-17 stsp if (error == NULL)
5520 1d0f4054 2021-06-17 stsp error = close_err;
5522 a273ac94 2020-02-23 naddy if (worktree)
5523 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5524 0ae84acc 2022-06-15 tracey if (pack_fds) {
5525 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5526 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5527 0ae84acc 2022-06-15 tracey if (error == NULL)
5528 0ae84acc 2022-06-15 tracey error = pack_err;
5530 51a10b52 2020-12-26 stsp tog_free_refs();
5531 26ed57b2 2018-05-19 stsp return error;
5534 4ed7e80c 2018-05-20 stsp __dead static void
5535 9f7d7167 2018-04-29 stsp usage_blame(void)
5538 87411fa9 2022-06-16 stsp fprintf(stderr,
5539 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5540 9f7d7167 2018-04-29 stsp getprogname());
5544 84451b3e 2018-07-10 stsp struct tog_blame_line {
5545 84451b3e 2018-07-10 stsp int annotated;
5546 84451b3e 2018-07-10 stsp struct got_object_id *id;
5549 4ed7e80c 2018-05-20 stsp static const struct got_error *
5550 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5552 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5553 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5554 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5555 84451b3e 2018-07-10 stsp const struct got_error *err;
5556 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5557 826082fe 2020-12-10 stsp char *line = NULL;
5558 826082fe 2020-12-10 stsp size_t linesize = 0;
5559 826082fe 2020-12-10 stsp ssize_t linelen;
5560 84451b3e 2018-07-10 stsp wchar_t *wline;
5561 27a741e5 2019-09-11 stsp int width;
5562 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5563 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5564 ab089a2a 2018-07-12 stsp char *id_str;
5565 11b20872 2019-11-08 stsp struct tog_color *tc;
5567 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5569 ab089a2a 2018-07-12 stsp return err;
5571 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5572 f7d12f7e 2018-08-01 stsp werase(view->window);
5574 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5575 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5576 ab089a2a 2018-07-12 stsp free(id_str);
5577 ab089a2a 2018-07-12 stsp return err;
5580 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5581 ab089a2a 2018-07-12 stsp free(line);
5582 2550e4c3 2018-07-13 stsp line = NULL;
5584 1cae65b4 2019-09-22 stsp return err;
5585 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5586 a3404814 2018-09-02 stsp wstandout(view->window);
5587 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5589 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5590 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5591 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5592 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5594 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5595 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5596 a3404814 2018-09-02 stsp wstandend(view->window);
5597 2550e4c3 2018-07-13 stsp free(wline);
5598 2550e4c3 2018-07-13 stsp wline = NULL;
5600 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5601 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5603 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5604 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5605 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5606 ab089a2a 2018-07-12 stsp free(id_str);
5607 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5609 ab089a2a 2018-07-12 stsp free(id_str);
5610 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5611 3f60a8ef 2018-07-10 stsp free(line);
5612 2550e4c3 2018-07-13 stsp line = NULL;
5614 3f60a8ef 2018-07-10 stsp return err;
5615 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5616 2550e4c3 2018-07-13 stsp free(wline);
5617 2550e4c3 2018-07-13 stsp wline = NULL;
5618 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5619 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5621 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5622 145b6838 2022-06-16 stsp view->maxx = 0;
5623 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5624 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5625 826082fe 2020-12-10 stsp if (linelen == -1) {
5626 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5627 826082fe 2020-12-10 stsp s->eof = 1;
5630 84451b3e 2018-07-10 stsp free(line);
5631 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5633 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5635 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5638 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5639 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5640 1853e0f4 2022-06-16 stsp if (err) {
5641 1853e0f4 2022-06-16 stsp free(line);
5642 1853e0f4 2022-06-16 stsp return err;
5644 1853e0f4 2022-06-16 stsp free(wline);
5645 1853e0f4 2022-06-16 stsp wline = NULL;
5646 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5648 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5649 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5651 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5652 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5653 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5654 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5655 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5656 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5657 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5658 8d0fe45a 2019-08-12 stsp char *id_str;
5659 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5660 87411fa9 2022-06-16 stsp blame_line->id);
5661 8d0fe45a 2019-08-12 stsp if (err) {
5662 8d0fe45a 2019-08-12 stsp free(line);
5663 8d0fe45a 2019-08-12 stsp return err;
5665 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5667 11b20872 2019-11-08 stsp wattr_on(view->window,
5668 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5669 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5671 11b20872 2019-11-08 stsp wattr_off(view->window,
5672 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5673 8d0fe45a 2019-08-12 stsp free(id_str);
5674 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5676 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5677 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5680 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5681 ee41ec32 2018-07-10 stsp prev_id = NULL;
5684 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5685 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5686 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5688 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5689 41605754 2020-11-12 stsp width = 9;
5690 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5691 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5692 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5693 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5694 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5695 41605754 2020-11-12 stsp if (err) {
5696 41605754 2020-11-12 stsp free(line);
5697 41605754 2020-11-12 stsp return err;
5699 41605754 2020-11-12 stsp width += 9;
5702 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5703 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5704 4cb9d869 2022-06-16 stsp if (err) {
5705 4cb9d869 2022-06-16 stsp free(line);
5706 4cb9d869 2022-06-16 stsp return err;
5708 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5709 a75b3e2d 2022-06-16 stsp width += 9;
5710 41605754 2020-11-12 stsp free(wline);
5711 41605754 2020-11-12 stsp wline = NULL;
5714 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5715 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5716 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5717 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5719 826082fe 2020-12-10 stsp free(line);
5720 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5722 9b058f45 2022-06-30 mark view_border(view);
5724 84451b3e 2018-07-10 stsp return NULL;
5727 84451b3e 2018-07-10 stsp static const struct got_error *
5728 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5729 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5731 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5732 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5733 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5734 1a76625f 2018-10-22 stsp int errcode;
5736 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5737 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5738 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5740 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5741 1a76625f 2018-10-22 stsp if (errcode)
5742 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5744 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5745 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5746 d68a0a7d 2018-07-10 stsp goto done;
5749 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5750 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5752 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5753 d68a0a7d 2018-07-10 stsp if (line->annotated)
5754 d68a0a7d 2018-07-10 stsp goto done;
5756 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5757 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5758 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5759 84451b3e 2018-07-10 stsp goto done;
5761 84451b3e 2018-07-10 stsp line->annotated = 1;
5763 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5764 1a76625f 2018-10-22 stsp if (errcode)
5765 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5766 84451b3e 2018-07-10 stsp return err;
5769 84451b3e 2018-07-10 stsp static void *
5770 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5772 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5773 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5774 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5775 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5776 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5778 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5779 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5780 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5782 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5783 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5784 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5785 e6e73e55 2022-06-30 tracey goto done;
5788 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5789 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5790 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5791 e6e73e55 2022-06-30 tracey goto done;
5793 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5794 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5795 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5796 e6e73e55 2022-06-30 tracey goto done;
5799 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5801 e6e73e55 2022-06-30 tracey goto done;
5803 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5804 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5805 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5806 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5807 fc06ba56 2019-08-22 stsp err = NULL;
5809 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5810 e6e73e55 2022-06-30 tracey if (errcode) {
5811 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5812 e6e73e55 2022-06-30 tracey goto done;
5815 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5816 1d0f4054 2021-06-17 stsp if (err == NULL)
5817 1d0f4054 2021-06-17 stsp err = close_err;
5818 c9beca56 2018-07-22 stsp ta->repo = NULL;
5819 c9beca56 2018-07-22 stsp *ta->complete = 1;
5821 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5822 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5823 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5826 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5827 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5828 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5829 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5830 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5831 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5832 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5833 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5835 18430de3 2018-07-10 stsp return (void *)err;
5838 245d91c1 2018-07-12 stsp static struct got_object_id *
5839 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5840 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5842 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5844 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5845 8d0fe45a 2019-08-12 stsp return NULL;
5847 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5848 c0f61fa4 2022-07-11 mark if (!line->annotated)
5849 c0f61fa4 2022-07-11 mark return NULL;
5851 c0f61fa4 2022-07-11 mark return line->id;
5854 c0f61fa4 2022-07-11 mark static struct got_object_id *
5855 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5856 c0f61fa4 2022-07-11 mark int lineno)
5858 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5860 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5861 c0f61fa4 2022-07-11 mark return NULL;
5863 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5864 245d91c1 2018-07-12 stsp if (!line->annotated)
5865 245d91c1 2018-07-12 stsp return NULL;
5867 245d91c1 2018-07-12 stsp return line->id;
5870 b880a918 2018-07-10 stsp static const struct got_error *
5871 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5873 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5876 245d91c1 2018-07-12 stsp if (blame->thread) {
5877 1a76625f 2018-10-22 stsp int errcode;
5878 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5879 1a76625f 2018-10-22 stsp if (errcode)
5880 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5881 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5882 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5883 1a76625f 2018-10-22 stsp if (errcode)
5884 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5885 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5886 1a76625f 2018-10-22 stsp if (errcode)
5887 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5888 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5889 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5890 245d91c1 2018-07-12 stsp err = NULL;
5891 245d91c1 2018-07-12 stsp blame->thread = NULL;
5893 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5894 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5895 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5896 1d0f4054 2021-06-17 stsp if (err == NULL)
5897 1d0f4054 2021-06-17 stsp err = close_err;
5898 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5900 245d91c1 2018-07-12 stsp if (blame->f) {
5901 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5902 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5903 245d91c1 2018-07-12 stsp blame->f = NULL;
5905 57670559 2018-12-23 stsp if (blame->lines) {
5906 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5907 57670559 2018-12-23 stsp free(blame->lines[i].id);
5908 57670559 2018-12-23 stsp free(blame->lines);
5909 57670559 2018-12-23 stsp blame->lines = NULL;
5911 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5912 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5913 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5914 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5915 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5916 0ae84acc 2022-06-15 tracey if (err == NULL)
5917 0ae84acc 2022-06-15 tracey err = pack_err;
5918 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5920 245d91c1 2018-07-12 stsp return err;
5923 245d91c1 2018-07-12 stsp static const struct got_error *
5924 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5926 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5927 fc06ba56 2019-08-22 stsp int *done = arg;
5928 fc06ba56 2019-08-22 stsp int errcode;
5930 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5931 fc06ba56 2019-08-22 stsp if (errcode)
5932 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5933 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5935 fc06ba56 2019-08-22 stsp if (*done)
5936 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5938 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5939 fc06ba56 2019-08-22 stsp if (errcode)
5940 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5941 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5943 fc06ba56 2019-08-22 stsp return err;
5946 fc06ba56 2019-08-22 stsp static const struct got_error *
5947 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5949 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5950 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5951 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5952 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5953 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5954 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5955 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5956 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5957 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5959 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5960 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5962 15a94983 2018-12-23 stsp return err;
5964 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5965 eb81bc23 2022-06-28 tracey if (fd == -1) {
5966 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5967 eb81bc23 2022-06-28 tracey goto done;
5970 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5972 a44927cc 2022-04-07 stsp goto done;
5974 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5976 84451b3e 2018-07-10 stsp goto done;
5978 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5979 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5980 84451b3e 2018-07-10 stsp goto done;
5983 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5985 a70480e0 2018-06-23 stsp goto done;
5986 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5987 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5988 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5989 84451b3e 2018-07-10 stsp goto done;
5991 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5992 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5994 1fddf795 2021-01-20 stsp goto done;
5995 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5996 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5997 84451b3e 2018-07-10 stsp goto done;
6000 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6001 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6002 b02560ec 2019-08-19 stsp blame->nlines--;
6004 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6005 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6006 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6007 84451b3e 2018-07-10 stsp goto done;
6010 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6012 bd24772e 2018-07-11 stsp goto done;
6013 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6014 0ae84acc 2022-06-15 tracey pack_fds);
6015 0ae84acc 2022-06-15 tracey if (err)
6016 0ae84acc 2022-06-15 tracey goto done;
6018 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6019 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6020 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6021 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6022 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6023 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6024 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6025 245d91c1 2018-07-12 stsp goto done;
6027 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6029 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6030 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6031 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6032 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6033 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6034 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6035 a5388363 2020-12-01 naddy s->blame_complete = 0;
6037 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6038 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6039 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6040 f5a09613 2020-12-13 naddy s->selected_line = 1;
6042 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6045 a44927cc 2022-04-07 stsp if (commit)
6046 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6047 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6048 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6050 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6051 27d434c2 2018-09-15 stsp free(obj_id);
6053 245d91c1 2018-07-12 stsp stop_blame(blame);
6054 245d91c1 2018-07-12 stsp return err;
6057 245d91c1 2018-07-12 stsp static const struct got_error *
6058 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6059 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6061 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6062 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6064 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6066 c4843652 2019-08-12 stsp s->path = strdup(path);
6067 c4843652 2019-08-12 stsp if (s->path == NULL)
6068 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6070 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6071 c4843652 2019-08-12 stsp if (err) {
6072 c4843652 2019-08-12 stsp free(s->path);
6073 7cbe629d 2018-08-04 stsp return err;
6076 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6077 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6078 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6079 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6080 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6081 fb2756b9 2018-08-04 stsp s->repo = repo;
6082 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6083 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6085 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6086 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6087 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6088 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6090 11b20872 2019-11-08 stsp return err;
6093 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6094 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6095 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6096 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6097 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6098 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6099 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6101 a5388363 2020-12-01 naddy return run_blame(view);
6104 e5a0f69f 2018-08-18 stsp static const struct got_error *
6105 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6107 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6108 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6110 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6111 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6113 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6114 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6115 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6116 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6117 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6120 e5a0f69f 2018-08-18 stsp free(s->path);
6121 11b20872 2019-11-08 stsp free_colors(&s->colors);
6122 e5a0f69f 2018-08-18 stsp return err;
6125 7cbe629d 2018-08-04 stsp static const struct got_error *
6126 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6128 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6130 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6131 6c4c42e0 2019-06-24 stsp return NULL;
6134 eaf2c13e 2022-09-17 mark static void
6135 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6136 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6138 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6140 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6141 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6142 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6143 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6144 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6145 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6146 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6149 6c4c42e0 2019-06-24 stsp static const struct got_error *
6150 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6152 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6153 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6154 2b380cc8 2018-10-24 stsp int errcode;
6156 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6157 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6158 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6159 2b380cc8 2018-10-24 stsp if (errcode)
6160 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6162 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6165 51fe7530 2019-08-19 stsp if (s->blame_complete)
6166 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6168 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6170 9b058f45 2022-06-30 mark view_border(view);
6171 e5a0f69f 2018-08-18 stsp return err;
6174 e5a0f69f 2018-08-18 stsp static const struct got_error *
6175 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6176 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6178 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6179 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6181 05f04cdf 2022-07-20 mark *new_view = NULL;
6183 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6184 05f04cdf 2022-07-20 mark if (log_view == NULL)
6185 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6187 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6189 05f04cdf 2022-07-20 mark view_close(log_view);
6191 05f04cdf 2022-07-20 mark *new_view = log_view;
6193 05f04cdf 2022-07-20 mark return err;
6196 05f04cdf 2022-07-20 mark static const struct got_error *
6197 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6199 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6200 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6201 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6202 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6204 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6205 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6206 9b058f45 2022-06-30 mark --eos; /* border */
6208 e5a0f69f 2018-08-18 stsp switch (ch) {
6210 145b6838 2022-06-16 stsp view->x = 0;
6213 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
6214 640cd7ff 2022-06-22 mark view->count = 0;
6216 145b6838 2022-06-16 stsp case KEY_RIGHT:
6218 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
6219 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
6221 640cd7ff 2022-06-22 mark view->count = 0;
6223 145b6838 2022-06-16 stsp case KEY_LEFT:
6225 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
6226 640cd7ff 2022-06-22 mark if (view->x <= 0)
6227 640cd7ff 2022-06-22 mark view->count = 0;
6230 1e37a5c2 2019-05-12 jcs s->done = 1;
6232 4deef56f 2021-09-02 naddy case 'g':
6233 4deef56f 2021-09-02 naddy case KEY_HOME:
6234 4deef56f 2021-09-02 naddy s->selected_line = 1;
6235 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6236 640cd7ff 2022-06-22 mark view->count = 0;
6238 4deef56f 2021-09-02 naddy case 'G':
6239 4deef56f 2021-09-02 naddy case KEY_END:
6240 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6241 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6242 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6244 9b058f45 2022-06-30 mark s->selected_line = eos;
6245 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6247 640cd7ff 2022-06-22 mark view->count = 0;
6250 1e37a5c2 2019-05-12 jcs case KEY_UP:
6251 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6252 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6253 1e37a5c2 2019-05-12 jcs s->selected_line--;
6254 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6255 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6256 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6258 640cd7ff 2022-06-22 mark view->count = 0;
6260 83cc4199 2022-06-13 stsp case CTRL('u'):
6262 83cc4199 2022-06-13 stsp nscroll /= 2;
6263 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6264 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6265 ea025d1d 2020-02-22 naddy case CTRL('b'):
6267 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6268 640cd7ff 2022-06-22 mark if (view->count > 1)
6269 640cd7ff 2022-06-22 mark nscroll += nscroll;
6270 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6271 640cd7ff 2022-06-22 mark view->count = 0;
6274 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6275 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6277 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6280 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6281 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6282 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6283 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6284 1e37a5c2 2019-05-12 jcs s->selected_line++;
6285 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6286 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6288 640cd7ff 2022-06-22 mark view->count = 0;
6291 1e37a5c2 2019-05-12 jcs case 'p': {
6292 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6294 640cd7ff 2022-06-22 mark view->count = 0;
6295 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6296 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6297 1e37a5c2 2019-05-12 jcs if (id == NULL)
6299 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6300 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6301 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6302 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6303 1e37a5c2 2019-05-12 jcs int obj_type;
6304 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6305 1e37a5c2 2019-05-12 jcs s->repo, id);
6308 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6309 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6310 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6311 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6314 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6315 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6316 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6319 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6320 a44927cc 2022-04-07 stsp pcommit, s->path);
6321 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6322 e5a0f69f 2018-08-18 stsp if (err) {
6323 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6324 1e37a5c2 2019-05-12 jcs err = NULL;
6325 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6328 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6330 1e37a5c2 2019-05-12 jcs free(blob_id);
6331 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6332 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6333 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6336 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6337 d7b5a0e8 2022-04-20 stsp &pid->id);
6338 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6340 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6341 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6343 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6348 1e37a5c2 2019-05-12 jcs s->done = 1;
6349 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6350 1e37a5c2 2019-05-12 jcs s->done = 0;
6351 1e37a5c2 2019-05-12 jcs if (thread_err)
6353 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6354 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6355 a5388363 2020-12-01 naddy err = run_blame(view);
6360 61417565 2022-06-20 mark case 'C': {
6361 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6363 640cd7ff 2022-06-22 mark view->count = 0;
6364 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6365 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6367 1e37a5c2 2019-05-12 jcs s->done = 1;
6368 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6369 1e37a5c2 2019-05-12 jcs s->done = 0;
6370 1e37a5c2 2019-05-12 jcs if (thread_err)
6372 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6373 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6374 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6375 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6376 a5388363 2020-12-01 naddy err = run_blame(view);
6382 05f04cdf 2022-07-20 mark view->count = 0;
6383 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6384 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6385 136e2bd4 2022-07-23 mark if (s->id_to_log)
6386 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6388 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6389 1e37a5c2 2019-05-12 jcs case '\r': {
6390 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6391 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6392 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6394 640cd7ff 2022-06-22 mark view->count = 0;
6395 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6396 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6397 1e37a5c2 2019-05-12 jcs if (id == NULL)
6399 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6402 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6403 c0f61fa4 2022-07-11 mark if (*new_view) {
6404 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6405 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6408 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6410 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6411 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6413 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6414 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6415 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6416 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6417 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6421 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6422 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6423 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6425 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6428 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6429 c0f61fa4 2022-07-11 mark s->selected_line;
6430 c0f61fa4 2022-07-11 mark if (*new_view)
6431 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6432 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6433 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6434 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6439 e78dc838 2020-12-04 stsp view->focussed = 0;
6440 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6441 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6442 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6443 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6444 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6445 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6448 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6451 e78dc838 2020-12-04 stsp view->focus_child = 1;
6453 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6458 83cc4199 2022-06-13 stsp case CTRL('d'):
6460 83cc4199 2022-06-13 stsp nscroll /= 2;
6461 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6462 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6463 ea025d1d 2020-02-22 naddy case CTRL('f'):
6466 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6467 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6468 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6469 640cd7ff 2022-06-22 mark view->count = 0;
6472 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6473 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6474 83cc4199 2022-06-13 stsp s->selected_line +=
6475 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6476 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6478 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6479 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6481 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6482 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6484 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6485 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6486 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6487 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6491 640cd7ff 2022-06-22 mark view->count = 0;
6494 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6497 917d79a7 2022-07-01 stsp static const struct got_error *
6498 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6500 917d79a7 2022-07-01 stsp const struct got_error *err;
6501 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6503 917d79a7 2022-07-01 stsp view->count = 0;
6504 917d79a7 2022-07-01 stsp s->done = 1;
6505 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6506 917d79a7 2022-07-01 stsp s->done = 0;
6508 917d79a7 2022-07-01 stsp return err;
6509 917d79a7 2022-07-01 stsp return run_blame(view);
6512 a70480e0 2018-06-23 stsp static const struct got_error *
6513 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6515 a70480e0 2018-06-23 stsp const struct got_error *error;
6516 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6517 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6518 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6519 0587e10c 2020-07-23 stsp char *link_target = NULL;
6520 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6521 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6522 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6524 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6525 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6527 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6528 a70480e0 2018-06-23 stsp switch (ch) {
6530 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6533 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6534 69069811 2018-08-02 stsp if (repo_path == NULL)
6535 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6539 17020d27 2019-03-07 stsp usage_blame();
6540 a70480e0 2018-06-23 stsp /* NOTREACHED */
6544 a70480e0 2018-06-23 stsp argc -= optind;
6545 a70480e0 2018-06-23 stsp argv += optind;
6547 f135c941 2020-02-20 stsp if (argc != 1)
6548 a70480e0 2018-06-23 stsp usage_blame();
6550 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6551 0ae84acc 2022-06-15 tracey if (error != NULL)
6552 0ae84acc 2022-06-15 tracey goto done;
6554 69069811 2018-08-02 stsp if (repo_path == NULL) {
6555 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6556 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6557 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6558 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6559 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6560 c156c7a4 2020-12-18 stsp goto done;
6561 f135c941 2020-02-20 stsp if (worktree)
6562 eb41ed75 2019-02-05 stsp repo_path =
6563 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6565 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6566 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6567 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6568 c156c7a4 2020-12-18 stsp goto done;
6572 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6573 c02c541e 2019-03-29 stsp if (error != NULL)
6574 8e94dd5b 2019-01-04 stsp goto done;
6576 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6577 f135c941 2020-02-20 stsp worktree);
6578 c02c541e 2019-03-29 stsp if (error)
6579 92205607 2019-01-04 stsp goto done;
6581 f135c941 2020-02-20 stsp init_curses();
6583 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6584 51a10b52 2020-12-26 stsp if (error)
6585 51a10b52 2020-12-26 stsp goto done;
6587 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6588 eb41ed75 2019-02-05 stsp if (error)
6589 69069811 2018-08-02 stsp goto done;
6591 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6592 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6593 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6594 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6595 a70480e0 2018-06-23 stsp if (error != NULL)
6596 66b4983c 2018-06-23 stsp goto done;
6597 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6598 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6600 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6601 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6603 a19e88aa 2018-06-23 stsp if (error != NULL)
6604 8b473291 2019-02-21 stsp goto done;
6606 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6607 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6608 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6609 e1cd8fed 2018-08-01 stsp goto done;
6612 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6613 a44927cc 2022-04-07 stsp if (error)
6614 a44927cc 2022-04-07 stsp goto done;
6616 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6617 a44927cc 2022-04-07 stsp commit, repo);
6618 7cbe629d 2018-08-04 stsp if (error)
6619 7cbe629d 2018-08-04 stsp goto done;
6621 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6622 78756c87 2020-11-24 stsp commit_id, repo);
6623 0587e10c 2020-07-23 stsp if (error)
6624 0587e10c 2020-07-23 stsp goto done;
6625 12314ad4 2019-08-31 stsp if (worktree) {
6626 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6627 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6628 12314ad4 2019-08-31 stsp worktree = NULL;
6630 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6632 69069811 2018-08-02 stsp free(repo_path);
6633 f135c941 2020-02-20 stsp free(in_repo_path);
6634 0587e10c 2020-07-23 stsp free(link_target);
6635 69069811 2018-08-02 stsp free(cwd);
6636 a70480e0 2018-06-23 stsp free(commit_id);
6637 a44927cc 2022-04-07 stsp if (commit)
6638 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6639 eb41ed75 2019-02-05 stsp if (worktree)
6640 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6641 1d0f4054 2021-06-17 stsp if (repo) {
6642 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6643 1d0f4054 2021-06-17 stsp if (error == NULL)
6644 1d0f4054 2021-06-17 stsp error = close_err;
6646 0ae84acc 2022-06-15 tracey if (pack_fds) {
6647 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6648 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6649 0ae84acc 2022-06-15 tracey if (error == NULL)
6650 0ae84acc 2022-06-15 tracey error = pack_err;
6652 51a10b52 2020-12-26 stsp tog_free_refs();
6653 a70480e0 2018-06-23 stsp return error;
6656 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6657 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6659 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6660 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6661 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6662 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6663 0c6ad1bc 2022-09-09 mark char *index = NULL;
6664 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6665 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6666 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6668 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6669 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6670 9b058f45 2022-06-30 mark --limit; /* border */
6672 f7d12f7e 2018-08-01 stsp werase(view->window);
6674 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6675 ffd1d5e5 2018-06-23 stsp return NULL;
6677 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6680 ffd1d5e5 2018-06-23 stsp return err;
6681 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6682 a3404814 2018-09-02 stsp wstandout(view->window);
6683 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6685 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6686 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6687 0c6ad1bc 2022-09-09 mark free(wline);
6688 0c6ad1bc 2022-09-09 mark wline = NULL;
6689 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6690 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6692 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6693 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6694 a3404814 2018-09-02 stsp wstandend(view->window);
6695 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6696 0c6ad1bc 2022-09-09 mark return NULL;
6698 df68a56b 2022-08-12 mark i += s->selected;
6699 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6700 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6701 df68a56b 2022-08-12 mark if (s->tree != s->root)
6702 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6704 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6705 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6706 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6707 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6708 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6709 0c6ad1bc 2022-09-09 mark free(index);
6711 ce52c690 2018-06-23 stsp return err;
6712 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6713 2550e4c3 2018-07-13 stsp free(wline);
6714 2550e4c3 2018-07-13 stsp wline = NULL;
6715 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6716 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6717 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6718 ffd1d5e5 2018-06-23 stsp return NULL;
6719 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6720 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6721 a1eca9bb 2018-06-23 stsp return NULL;
6723 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6724 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6725 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6726 0cf4efb1 2018-09-29 stsp if (view->focussed)
6727 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6728 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6730 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6731 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6732 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6733 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6734 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6735 ffd1d5e5 2018-06-23 stsp return NULL;
6739 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6742 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6743 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6744 848d6979 2019-08-12 stsp const char *modestr = "";
6745 56e0773d 2019-11-28 stsp mode_t mode;
6747 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6748 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6750 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6751 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6752 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6754 638f9024 2019-05-13 stsp return got_error_from_errno(
6755 230a42bd 2019-05-11 jcs "got_object_id_str");
6757 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6758 63c5ca5d 2019-08-24 stsp modestr = "$";
6759 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6762 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6763 d86d3b18 2020-12-01 naddy te, s->repo);
6764 0d6c6ee3 2020-05-20 stsp if (err) {
6765 0d6c6ee3 2020-05-20 stsp free(id_str);
6766 0d6c6ee3 2020-05-20 stsp return err;
6768 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6769 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6770 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6772 848d6979 2019-08-12 stsp modestr = "@";
6774 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6775 848d6979 2019-08-12 stsp modestr = "/";
6776 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6777 848d6979 2019-08-12 stsp modestr = "*";
6778 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6779 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6780 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6781 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6782 1d13200f 2018-07-12 stsp free(id_str);
6783 0d6c6ee3 2020-05-20 stsp free(link_target);
6784 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6786 1d13200f 2018-07-12 stsp free(id_str);
6787 0d6c6ee3 2020-05-20 stsp free(link_target);
6788 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6790 ffd1d5e5 2018-06-23 stsp if (err) {
6791 ffd1d5e5 2018-06-23 stsp free(line);
6794 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6795 0cf4efb1 2018-09-29 stsp if (view->focussed)
6796 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6797 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6799 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6801 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6802 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6803 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6805 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6806 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6807 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6808 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6809 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6810 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6811 ffd1d5e5 2018-06-23 stsp free(line);
6812 2550e4c3 2018-07-13 stsp free(wline);
6813 2550e4c3 2018-07-13 stsp wline = NULL;
6815 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6816 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6817 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6821 ffd1d5e5 2018-06-23 stsp return err;
6824 ffd1d5e5 2018-06-23 stsp static void
6825 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6827 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6828 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6829 fa86c4bf 2020-11-29 stsp int i = 0;
6831 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6834 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6835 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6836 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6837 fa86c4bf 2020-11-29 stsp if (!isroot)
6838 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6841 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6842 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6846 9b058f45 2022-06-30 mark static const struct got_error *
6847 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6849 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6850 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6851 ffd1d5e5 2018-06-23 stsp int n = 0;
6853 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6854 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6855 694d3271 2020-12-01 naddy s->first_displayed_entry);
6857 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6859 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6860 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6861 94b80cfa 2022-08-01 mark if (last) {
6862 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6863 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6865 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6866 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6867 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6871 9b058f45 2022-06-30 mark return NULL;
6874 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6875 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6876 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6878 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6879 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6880 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6882 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6883 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6884 56e0773d 2019-11-28 stsp + 1 /* slash */;
6886 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6888 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6889 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6890 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6892 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6893 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6894 d9765a41 2018-06-23 stsp while (pt) {
6895 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6896 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6897 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6898 cb2ebc8a 2018-06-23 stsp goto done;
6900 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6901 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6902 cb2ebc8a 2018-06-23 stsp goto done;
6904 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6907 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6908 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6909 ce52c690 2018-06-23 stsp goto done;
6913 ce52c690 2018-06-23 stsp if (err) {
6914 ce52c690 2018-06-23 stsp free(*path);
6915 ce52c690 2018-06-23 stsp *path = NULL;
6917 ce52c690 2018-06-23 stsp return err;
6920 ce52c690 2018-06-23 stsp static const struct got_error *
6921 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6922 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6923 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6925 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6926 ce52c690 2018-06-23 stsp char *path;
6927 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6929 a0de39f3 2019-08-09 stsp *new_view = NULL;
6931 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6933 ce52c690 2018-06-23 stsp return err;
6935 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6936 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6937 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6938 83ce39e3 2019-08-12 stsp goto done;
6941 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6942 e5a0f69f 2018-08-18 stsp if (err) {
6943 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6944 fc06ba56 2019-08-22 stsp err = NULL;
6945 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6947 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6949 83ce39e3 2019-08-12 stsp free(path);
6950 69efd4c4 2018-07-18 stsp return err;
6953 69efd4c4 2018-07-18 stsp static const struct got_error *
6954 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6955 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6957 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6958 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6959 69efd4c4 2018-07-18 stsp char *path;
6961 a0de39f3 2019-08-09 stsp *new_view = NULL;
6963 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6964 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6965 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6967 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6969 69efd4c4 2018-07-18 stsp return err;
6971 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6974 e5a0f69f 2018-08-18 stsp view_close(log_view);
6976 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6977 cb2ebc8a 2018-06-23 stsp free(path);
6978 cb2ebc8a 2018-06-23 stsp return err;
6981 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6982 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6983 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6985 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6986 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6987 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6988 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6990 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6991 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6993 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6994 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6995 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6997 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6999 bc573f3b 2021-07-10 stsp goto done;
7002 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7003 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7004 bc573f3b 2021-07-10 stsp * closed on demand.
7006 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7007 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7009 bc573f3b 2021-07-10 stsp goto done;
7010 bc573f3b 2021-07-10 stsp s->tree = s->root;
7012 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7013 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7014 ffd1d5e5 2018-06-23 stsp goto done;
7016 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7017 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7018 ffd1d5e5 2018-06-23 stsp goto done;
7021 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7022 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7023 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7024 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7025 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7026 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7027 9cd7cbd1 2020-12-07 stsp goto done;
7030 fb2756b9 2018-08-04 stsp s->repo = repo;
7032 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7033 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7034 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7035 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7037 c0b01bdb 2019-11-08 stsp goto done;
7038 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7039 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7041 c0b01bdb 2019-11-08 stsp goto done;
7042 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7043 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7044 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7046 c0b01bdb 2019-11-08 stsp goto done;
7048 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7049 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7050 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7052 11b20872 2019-11-08 stsp goto done;
7054 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7055 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7057 c0b01bdb 2019-11-08 stsp goto done;
7060 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7061 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7062 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7063 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7064 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7066 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7067 bc573f3b 2021-07-10 stsp if (commit)
7068 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7070 bc573f3b 2021-07-10 stsp close_tree_view(view);
7071 ad80ab7b 2018-08-04 stsp return err;
7074 e5a0f69f 2018-08-18 stsp static const struct got_error *
7075 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7077 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7079 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7080 fb2756b9 2018-08-04 stsp free(s->tree_label);
7081 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7082 6484ec90 2018-09-29 stsp free(s->commit_id);
7083 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7084 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7085 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7086 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7087 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7088 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7089 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7090 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7091 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7092 ad80ab7b 2018-08-04 stsp free(parent);
7095 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7096 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7097 bc573f3b 2021-07-10 stsp if (s->root)
7098 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7099 7c32bd05 2019-06-22 stsp return NULL;
7102 7c32bd05 2019-06-22 stsp static const struct got_error *
7103 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7105 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7107 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7108 7c32bd05 2019-06-22 stsp return NULL;
7111 7c32bd05 2019-06-22 stsp static int
7112 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7114 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7116 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7120 7c32bd05 2019-06-22 stsp static const struct got_error *
7121 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7123 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7124 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7126 7c32bd05 2019-06-22 stsp if (!view->searching) {
7127 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7128 7c32bd05 2019-06-22 stsp return NULL;
7131 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7132 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7133 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7134 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7135 56e0773d 2019-11-28 stsp s->selected_entry);
7137 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7139 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7140 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7142 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7143 56e0773d 2019-11-28 stsp s->selected_entry);
7146 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7147 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7148 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7149 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7151 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7154 7c32bd05 2019-06-22 stsp while (1) {
7155 56e0773d 2019-11-28 stsp if (te == NULL) {
7156 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7157 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7158 ac66afb8 2019-06-24 stsp return NULL;
7160 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7161 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7163 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7166 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7167 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7168 56e0773d 2019-11-28 stsp s->matched_entry = te;
7172 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7173 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7175 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7178 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7179 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7180 7c32bd05 2019-06-22 stsp s->selected = 0;
7183 e5a0f69f 2018-08-18 stsp return NULL;
7186 ad80ab7b 2018-08-04 stsp static const struct got_error *
7187 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7189 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7190 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7191 e5a0f69f 2018-08-18 stsp char *parent_path;
7193 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7195 e5a0f69f 2018-08-18 stsp return err;
7197 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7198 e5a0f69f 2018-08-18 stsp free(parent_path);
7200 9b058f45 2022-06-30 mark view_border(view);
7201 e5a0f69f 2018-08-18 stsp return err;
7204 94b80cfa 2022-08-01 mark static const struct got_error *
7205 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7207 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7208 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7209 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7210 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7211 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7212 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7214 94b80cfa 2022-08-01 mark g = view->gline;
7215 94b80cfa 2022-08-01 mark view->gline = 0;
7217 94b80cfa 2022-08-01 mark if (g == 0)
7219 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7220 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7222 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7223 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7224 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7226 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7227 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7228 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7230 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7231 94b80cfa 2022-08-01 mark last += off;
7233 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7234 94b80cfa 2022-08-01 mark s->selected = g - first;
7235 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7238 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7239 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7243 94b80cfa 2022-08-01 mark if (i < g) {
7244 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7246 94b80cfa 2022-08-01 mark return err;
7247 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7248 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7249 94b80cfa 2022-08-01 mark first + s->selected < g &&
7250 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7251 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7252 94b80cfa 2022-08-01 mark first += off;
7253 94b80cfa 2022-08-01 mark s->selected = g - first;
7255 94b80cfa 2022-08-01 mark } else if (i > g)
7256 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7258 94b80cfa 2022-08-01 mark if (g < nlines &&
7259 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7260 94b80cfa 2022-08-01 mark s->selected = g - 1;
7262 94b80cfa 2022-08-01 mark return NULL;
7265 e5a0f69f 2018-08-18 stsp static const struct got_error *
7266 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7268 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7269 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7270 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7271 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7273 94b80cfa 2022-08-01 mark if (view->gline)
7274 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7276 e5a0f69f 2018-08-18 stsp switch (ch) {
7278 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7279 640cd7ff 2022-06-22 mark view->count = 0;
7282 640cd7ff 2022-06-22 mark view->count = 0;
7283 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7285 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7288 640cd7ff 2022-06-22 mark view->count = 0;
7289 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7291 e4526bf5 2021-09-03 naddy case 'g':
7292 e4526bf5 2021-09-03 naddy case KEY_HOME:
7293 e4526bf5 2021-09-03 naddy s->selected = 0;
7294 640cd7ff 2022-06-22 mark view->count = 0;
7295 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7296 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7297 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7299 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7301 e4526bf5 2021-09-03 naddy case 'G':
7302 9b058f45 2022-06-30 mark case KEY_END: {
7303 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7305 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7306 9b058f45 2022-06-30 mark --eos; /* border */
7307 e4526bf5 2021-09-03 naddy s->selected = 0;
7308 640cd7ff 2022-06-22 mark view->count = 0;
7309 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7310 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7311 e4526bf5 2021-09-03 naddy if (te == NULL) {
7312 ad055527 2022-07-16 mark if (s->tree != s->root) {
7313 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7318 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7319 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7321 e4526bf5 2021-09-03 naddy if (n > 0)
7322 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7326 1e37a5c2 2019-05-12 jcs case KEY_UP:
7327 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7328 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7329 1e37a5c2 2019-05-12 jcs s->selected--;
7332 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7333 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7334 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7335 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7336 640cd7ff 2022-06-22 mark view->count = 0;
7338 83cc4199 2022-06-13 stsp case CTRL('u'):
7340 83cc4199 2022-06-13 stsp nscroll /= 2;
7341 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7342 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7343 ea025d1d 2020-02-22 naddy case CTRL('b'):
7345 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7346 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7347 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7348 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7350 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7351 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7353 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7354 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7355 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7356 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7357 640cd7ff 2022-06-22 mark view->count = 0;
7360 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7361 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7362 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7363 1e37a5c2 2019-05-12 jcs s->selected++;
7366 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7367 640cd7ff 2022-06-22 mark == NULL) {
7368 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7369 640cd7ff 2022-06-22 mark view->count = 0;
7372 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7374 83cc4199 2022-06-13 stsp case CTRL('d'):
7376 83cc4199 2022-06-13 stsp nscroll /= 2;
7377 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7378 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7379 ea025d1d 2020-02-22 naddy case CTRL('f'):
7381 48bb96f0 2022-06-20 naddy case ' ':
7382 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7384 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7385 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7386 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7387 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7389 640cd7ff 2022-06-22 mark view->count = 0;
7392 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7394 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7396 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7397 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7398 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7399 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7400 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7401 640cd7ff 2022-06-22 mark view->count = 0;
7404 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7405 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7407 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7408 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7409 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7410 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7411 1e37a5c2 2019-05-12 jcs s->selected_entry =
7412 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7413 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7414 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7415 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7419 1e37a5c2 2019-05-12 jcs free(parent);
7420 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7421 56e0773d 2019-11-28 stsp s->selected_entry))) {
7422 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7423 640cd7ff 2022-06-22 mark view->count = 0;
7424 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7425 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7428 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7429 941e9f74 2019-05-21 stsp if (err) {
7430 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7433 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7434 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7436 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7437 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7438 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7439 640cd7ff 2022-06-22 mark view->count = 0;
7442 640cd7ff 2022-06-22 mark view->count = 0;
7446 ffd1d5e5 2018-06-23 stsp return err;
7449 ffd1d5e5 2018-06-23 stsp __dead static void
7450 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7453 87411fa9 2022-06-16 stsp fprintf(stderr,
7454 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7455 ffd1d5e5 2018-06-23 stsp getprogname());
7459 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7460 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7462 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7463 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7464 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7465 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7466 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7467 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7468 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7469 4e97c21c 2020-12-06 stsp char *label = NULL;
7470 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7471 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7473 5221c383 2018-08-01 stsp struct tog_view *view;
7474 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7476 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7477 ffd1d5e5 2018-06-23 stsp switch (ch) {
7479 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7482 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7483 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7484 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7488 e99e2d15 2020-11-24 naddy usage_tree();
7489 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7493 ffd1d5e5 2018-06-23 stsp argc -= optind;
7494 ffd1d5e5 2018-06-23 stsp argv += optind;
7496 55cccc34 2020-02-20 stsp if (argc > 1)
7497 e99e2d15 2020-11-24 naddy usage_tree();
7499 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7500 0ae84acc 2022-06-15 tracey if (error != NULL)
7501 0ae84acc 2022-06-15 tracey goto done;
7503 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7504 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7505 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7506 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7507 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7508 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7509 c156c7a4 2020-12-18 stsp goto done;
7510 55cccc34 2020-02-20 stsp if (worktree)
7511 52185f70 2019-02-05 stsp repo_path =
7512 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7514 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7515 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7516 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7517 c156c7a4 2020-12-18 stsp goto done;
7521 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7522 c02c541e 2019-03-29 stsp if (error != NULL)
7523 52185f70 2019-02-05 stsp goto done;
7525 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7526 55cccc34 2020-02-20 stsp repo, worktree);
7527 55cccc34 2020-02-20 stsp if (error)
7528 55cccc34 2020-02-20 stsp goto done;
7530 55cccc34 2020-02-20 stsp init_curses();
7532 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7533 c02c541e 2019-03-29 stsp if (error)
7534 52185f70 2019-02-05 stsp goto done;
7536 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7537 51a10b52 2020-12-26 stsp if (error)
7538 51a10b52 2020-12-26 stsp goto done;
7540 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7541 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7542 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7543 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7544 4e97c21c 2020-12-06 stsp if (error)
7545 4e97c21c 2020-12-06 stsp goto done;
7546 4e97c21c 2020-12-06 stsp head_ref_name = label;
7548 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7549 4e97c21c 2020-12-06 stsp if (error == NULL)
7550 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7551 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7552 4e97c21c 2020-12-06 stsp goto done;
7553 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7554 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7555 4e97c21c 2020-12-06 stsp if (error)
7556 4e97c21c 2020-12-06 stsp goto done;
7559 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7560 a44927cc 2022-04-07 stsp if (error)
7561 a44927cc 2022-04-07 stsp goto done;
7563 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7564 5221c383 2018-08-01 stsp if (view == NULL) {
7565 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7566 5221c383 2018-08-01 stsp goto done;
7568 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7569 ad80ab7b 2018-08-04 stsp if (error)
7570 ad80ab7b 2018-08-04 stsp goto done;
7571 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7572 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7573 d91faf3b 2020-12-01 naddy in_repo_path);
7574 55cccc34 2020-02-20 stsp if (error)
7575 55cccc34 2020-02-20 stsp goto done;
7578 55cccc34 2020-02-20 stsp if (worktree) {
7579 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7580 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7581 55cccc34 2020-02-20 stsp worktree = NULL;
7583 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7585 52185f70 2019-02-05 stsp free(repo_path);
7586 e4a0e26d 2020-02-20 stsp free(cwd);
7587 ffd1d5e5 2018-06-23 stsp free(commit_id);
7588 4e97c21c 2020-12-06 stsp free(label);
7590 486cd271 2020-12-06 stsp got_ref_close(ref);
7591 1d0f4054 2021-06-17 stsp if (repo) {
7592 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7593 1d0f4054 2021-06-17 stsp if (error == NULL)
7594 1d0f4054 2021-06-17 stsp error = close_err;
7596 0ae84acc 2022-06-15 tracey if (pack_fds) {
7597 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7598 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7599 0ae84acc 2022-06-15 tracey if (error == NULL)
7600 0ae84acc 2022-06-15 tracey error = pack_err;
7602 51a10b52 2020-12-26 stsp tog_free_refs();
7603 ffd1d5e5 2018-06-23 stsp return error;
7606 6458efa5 2020-11-24 stsp static const struct got_error *
7607 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7609 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7610 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7612 6458efa5 2020-11-24 stsp s->nrefs = 0;
7613 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7614 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7615 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7616 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7617 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7620 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7621 6458efa5 2020-11-24 stsp if (re == NULL)
7622 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7624 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7625 8924d611 2020-12-26 stsp if (re->ref == NULL)
7626 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7627 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7628 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7631 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7632 6458efa5 2020-11-24 stsp return NULL;
7636 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7638 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7640 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7641 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7642 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7643 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7648 6458efa5 2020-11-24 stsp static const struct got_error *
7649 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7651 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7652 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7654 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7655 6458efa5 2020-11-24 stsp s->repo = repo;
7657 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7658 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7660 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7662 6458efa5 2020-11-24 stsp return err;
7664 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7665 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7666 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7667 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7669 6458efa5 2020-11-24 stsp goto done;
7671 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7672 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7673 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7675 6458efa5 2020-11-24 stsp goto done;
7677 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7678 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7679 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7681 cc488aa7 2022-01-23 stsp goto done;
7683 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7684 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7685 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7687 6458efa5 2020-11-24 stsp goto done;
7690 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7691 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7692 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7693 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7694 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7697 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7698 6458efa5 2020-11-24 stsp return err;
7701 6458efa5 2020-11-24 stsp static const struct got_error *
7702 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7704 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7706 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7707 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7709 6458efa5 2020-11-24 stsp return NULL;
7712 6458efa5 2020-11-24 stsp static const struct got_error *
7713 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7714 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7716 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7717 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7718 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7719 6458efa5 2020-11-24 stsp int obj_type;
7721 c42c9805 2020-11-24 stsp *commit_id = NULL;
7723 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7725 6458efa5 2020-11-24 stsp return err;
7727 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7729 6458efa5 2020-11-24 stsp goto done;
7731 6458efa5 2020-11-24 stsp switch (obj_type) {
7732 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7733 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7735 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7736 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7738 6458efa5 2020-11-24 stsp goto done;
7739 c42c9805 2020-11-24 stsp free(obj_id);
7740 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7741 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7743 6458efa5 2020-11-24 stsp goto done;
7744 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7745 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7746 c42c9805 2020-11-24 stsp goto done;
7748 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7749 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7750 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7751 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7752 c42c9805 2020-11-24 stsp goto done;
7756 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7762 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7763 c42c9805 2020-11-24 stsp if (err) {
7764 c42c9805 2020-11-24 stsp free(*commit_id);
7765 c42c9805 2020-11-24 stsp *commit_id = NULL;
7767 c42c9805 2020-11-24 stsp return err;
7770 c42c9805 2020-11-24 stsp static const struct got_error *
7771 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7772 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7774 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7775 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7776 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7778 c42c9805 2020-11-24 stsp *new_view = NULL;
7780 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7781 c42c9805 2020-11-24 stsp if (err) {
7782 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7783 c42c9805 2020-11-24 stsp return err;
7785 c42c9805 2020-11-24 stsp return NULL;
7788 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7789 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7790 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7791 6458efa5 2020-11-24 stsp goto done;
7794 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7795 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7798 6458efa5 2020-11-24 stsp view_close(log_view);
7800 6458efa5 2020-11-24 stsp *new_view = log_view;
7801 c42c9805 2020-11-24 stsp free(commit_id);
7802 6458efa5 2020-11-24 stsp return err;
7805 ce5b7c56 2019-07-09 stsp static void
7806 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7808 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7809 34ba6917 2020-11-30 stsp int i = 0;
7811 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7814 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7815 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7816 34ba6917 2020-11-30 stsp if (re == NULL)
7818 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7819 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7823 9b058f45 2022-06-30 mark static const struct got_error *
7824 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7826 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7827 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7828 6458efa5 2020-11-24 stsp int n = 0;
7830 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7831 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7833 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7835 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7836 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7837 94b80cfa 2022-08-01 mark if (last) {
7838 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7839 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7841 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7842 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7843 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7847 9b058f45 2022-06-30 mark return NULL;
7850 6458efa5 2020-11-24 stsp static const struct got_error *
7851 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7853 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7855 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7856 6458efa5 2020-11-24 stsp return NULL;
7859 6458efa5 2020-11-24 stsp static int
7860 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7862 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7864 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7868 6458efa5 2020-11-24 stsp static const struct got_error *
7869 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7871 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7872 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7874 6458efa5 2020-11-24 stsp if (!view->searching) {
7875 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7876 6458efa5 2020-11-24 stsp return NULL;
7879 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7880 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7881 6458efa5 2020-11-24 stsp if (s->selected_entry)
7882 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7884 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7885 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7887 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7888 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7890 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7891 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7894 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7895 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7896 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7897 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7899 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7902 6458efa5 2020-11-24 stsp while (1) {
7903 6458efa5 2020-11-24 stsp if (re == NULL) {
7904 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7905 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7906 6458efa5 2020-11-24 stsp return NULL;
7908 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7909 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7911 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7914 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7915 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7916 6458efa5 2020-11-24 stsp s->matched_entry = re;
7920 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7921 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7923 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7926 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7927 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7928 6458efa5 2020-11-24 stsp s->selected = 0;
7931 6458efa5 2020-11-24 stsp return NULL;
7934 6458efa5 2020-11-24 stsp static const struct got_error *
7935 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7937 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7938 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7939 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7940 6458efa5 2020-11-24 stsp char *line = NULL;
7941 6458efa5 2020-11-24 stsp wchar_t *wline;
7942 6458efa5 2020-11-24 stsp struct tog_color *tc;
7943 6458efa5 2020-11-24 stsp int width, n;
7944 6458efa5 2020-11-24 stsp int limit = view->nlines;
7946 6458efa5 2020-11-24 stsp werase(view->window);
7948 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7949 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7950 9b058f45 2022-06-30 mark --limit; /* border */
7952 6458efa5 2020-11-24 stsp if (limit == 0)
7953 6458efa5 2020-11-24 stsp return NULL;
7955 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7957 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7958 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7959 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7961 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7962 6458efa5 2020-11-24 stsp if (err) {
7963 6458efa5 2020-11-24 stsp free(line);
7964 6458efa5 2020-11-24 stsp return err;
7966 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7967 6458efa5 2020-11-24 stsp wstandout(view->window);
7968 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7969 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
7970 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
7971 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7972 6458efa5 2020-11-24 stsp wstandend(view->window);
7973 6458efa5 2020-11-24 stsp free(wline);
7974 6458efa5 2020-11-24 stsp wline = NULL;
7975 6458efa5 2020-11-24 stsp free(line);
7976 6458efa5 2020-11-24 stsp line = NULL;
7977 6458efa5 2020-11-24 stsp if (--limit <= 0)
7978 6458efa5 2020-11-24 stsp return NULL;
7981 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7982 6458efa5 2020-11-24 stsp char *line = NULL;
7983 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7985 b4996bee 2022-06-16 stsp if (s->show_date) {
7986 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7987 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7988 b4996bee 2022-06-16 stsp struct got_object_id *id;
7989 b4996bee 2022-06-16 stsp struct tm tm;
7992 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7994 b4996bee 2022-06-16 stsp return err;
7995 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7996 b4996bee 2022-06-16 stsp if (err) {
7997 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7999 b4996bee 2022-06-16 stsp return err;
8001 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8003 b4996bee 2022-06-16 stsp if (err) {
8005 b4996bee 2022-06-16 stsp return err;
8007 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8008 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8010 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8011 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8014 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8015 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8016 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8017 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8019 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8020 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8021 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8022 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8023 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8024 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8025 6458efa5 2020-11-24 stsp struct got_object_id *id;
8026 6458efa5 2020-11-24 stsp char *id_str;
8027 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8029 6458efa5 2020-11-24 stsp return err;
8030 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8031 6458efa5 2020-11-24 stsp if (err) {
8033 6458efa5 2020-11-24 stsp return err;
8035 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8036 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8037 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8039 6458efa5 2020-11-24 stsp free(id_str);
8040 6458efa5 2020-11-24 stsp return err;
8043 6458efa5 2020-11-24 stsp free(id_str);
8044 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8045 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8046 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8048 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8050 6458efa5 2020-11-24 stsp if (err) {
8051 6458efa5 2020-11-24 stsp free(line);
8052 6458efa5 2020-11-24 stsp return err;
8054 6458efa5 2020-11-24 stsp if (n == s->selected) {
8055 6458efa5 2020-11-24 stsp if (view->focussed)
8056 6458efa5 2020-11-24 stsp wstandout(view->window);
8057 6458efa5 2020-11-24 stsp s->selected_entry = re;
8059 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8061 6458efa5 2020-11-24 stsp wattr_on(view->window,
8062 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8063 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8065 6458efa5 2020-11-24 stsp wattr_off(view->window,
8066 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8067 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8068 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8069 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8070 6458efa5 2020-11-24 stsp wstandend(view->window);
8071 6458efa5 2020-11-24 stsp free(line);
8072 6458efa5 2020-11-24 stsp free(wline);
8073 6458efa5 2020-11-24 stsp wline = NULL;
8075 6458efa5 2020-11-24 stsp s->ndisplayed++;
8076 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8079 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8082 9b058f45 2022-06-30 mark view_border(view);
8083 6458efa5 2020-11-24 stsp return err;
8086 6458efa5 2020-11-24 stsp static const struct got_error *
8087 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8088 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8090 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8091 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8092 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8094 c42c9805 2020-11-24 stsp *new_view = NULL;
8096 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8097 c42c9805 2020-11-24 stsp if (err) {
8098 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8099 c42c9805 2020-11-24 stsp return err;
8101 c42c9805 2020-11-24 stsp return NULL;
8105 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8106 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8107 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8108 c42c9805 2020-11-24 stsp goto done;
8111 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8112 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8114 c42c9805 2020-11-24 stsp goto done;
8116 c42c9805 2020-11-24 stsp *new_view = tree_view;
8118 c42c9805 2020-11-24 stsp free(commit_id);
8119 c42c9805 2020-11-24 stsp return err;
8122 94b80cfa 2022-08-01 mark static const struct got_error *
8123 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8125 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8126 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8127 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8129 94b80cfa 2022-08-01 mark g = view->gline;
8130 94b80cfa 2022-08-01 mark view->gline = 0;
8132 94b80cfa 2022-08-01 mark if (g == 0)
8134 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8135 94b80cfa 2022-08-01 mark g = s->nrefs;
8137 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8138 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8139 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8140 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8141 94b80cfa 2022-08-01 mark return NULL;
8144 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8145 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8147 94b80cfa 2022-08-01 mark return err;
8148 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8149 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8150 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8151 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8152 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8153 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8155 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8156 94b80cfa 2022-08-01 mark s->selected = g - 1;
8158 94b80cfa 2022-08-01 mark return NULL;
8162 c42c9805 2020-11-24 stsp static const struct got_error *
8163 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8165 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8166 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8167 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8168 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8170 94b80cfa 2022-08-01 mark if (view->gline)
8171 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8173 6458efa5 2020-11-24 stsp switch (ch) {
8175 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8176 640cd7ff 2022-06-22 mark view->count = 0;
8179 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8180 640cd7ff 2022-06-22 mark view->count = 0;
8183 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8184 640cd7ff 2022-06-22 mark view->count = 0;
8185 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8186 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8187 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8190 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8191 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8192 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8195 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8196 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8198 6458efa5 2020-11-24 stsp case KEY_ENTER:
8199 6458efa5 2020-11-24 stsp case '\r':
8200 640cd7ff 2022-06-22 mark view->count = 0;
8201 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8203 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8206 640cd7ff 2022-06-22 mark view->count = 0;
8207 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8209 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8211 e4526bf5 2021-09-03 naddy case 'g':
8212 e4526bf5 2021-09-03 naddy case KEY_HOME:
8213 e4526bf5 2021-09-03 naddy s->selected = 0;
8214 640cd7ff 2022-06-22 mark view->count = 0;
8215 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8217 e4526bf5 2021-09-03 naddy case 'G':
8218 9b058f45 2022-06-30 mark case KEY_END: {
8219 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8221 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8222 9b058f45 2022-06-30 mark --eos; /* border */
8223 e4526bf5 2021-09-03 naddy s->selected = 0;
8224 640cd7ff 2022-06-22 mark view->count = 0;
8225 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8226 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8227 e4526bf5 2021-09-03 naddy if (re == NULL)
8229 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8230 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8232 e4526bf5 2021-09-03 naddy if (n > 0)
8233 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8237 6458efa5 2020-11-24 stsp case KEY_UP:
8238 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8239 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8240 6458efa5 2020-11-24 stsp s->selected--;
8243 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8244 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8245 640cd7ff 2022-06-22 mark view->count = 0;
8247 83cc4199 2022-06-13 stsp case CTRL('u'):
8249 83cc4199 2022-06-13 stsp nscroll /= 2;
8250 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8251 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8252 6458efa5 2020-11-24 stsp case CTRL('b'):
8254 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8255 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8256 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8257 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8258 640cd7ff 2022-06-22 mark view->count = 0;
8261 6458efa5 2020-11-24 stsp case KEY_DOWN:
8262 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8263 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8264 6458efa5 2020-11-24 stsp s->selected++;
8267 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8268 6458efa5 2020-11-24 stsp /* can't scroll any further */
8269 640cd7ff 2022-06-22 mark view->count = 0;
8272 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8274 83cc4199 2022-06-13 stsp case CTRL('d'):
8276 83cc4199 2022-06-13 stsp nscroll /= 2;
8277 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8278 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8279 6458efa5 2020-11-24 stsp case CTRL('f'):
8281 48bb96f0 2022-06-20 naddy case ' ':
8282 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8283 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8284 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8285 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8286 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8287 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8288 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8289 640cd7ff 2022-06-22 mark view->count = 0;
8292 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8294 6458efa5 2020-11-24 stsp case CTRL('l'):
8295 640cd7ff 2022-06-22 mark view->count = 0;
8296 8924d611 2020-12-26 stsp tog_free_refs();
8297 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8300 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8301 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8303 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8304 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8305 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8308 640cd7ff 2022-06-22 mark view->count = 0;
8312 6458efa5 2020-11-24 stsp return err;
8315 6458efa5 2020-11-24 stsp __dead static void
8316 6458efa5 2020-11-24 stsp usage_ref(void)
8319 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8320 6458efa5 2020-11-24 stsp getprogname());
8324 6458efa5 2020-11-24 stsp static const struct got_error *
8325 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8327 6458efa5 2020-11-24 stsp const struct got_error *error;
8328 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8329 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8330 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8332 6458efa5 2020-11-24 stsp struct tog_view *view;
8333 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8335 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8336 6458efa5 2020-11-24 stsp switch (ch) {
8338 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8339 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8340 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8344 e99e2d15 2020-11-24 naddy usage_ref();
8345 6458efa5 2020-11-24 stsp /* NOTREACHED */
8349 6458efa5 2020-11-24 stsp argc -= optind;
8350 6458efa5 2020-11-24 stsp argv += optind;
8352 6458efa5 2020-11-24 stsp if (argc > 1)
8353 e99e2d15 2020-11-24 naddy usage_ref();
8355 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8356 0ae84acc 2022-06-15 tracey if (error != NULL)
8357 0ae84acc 2022-06-15 tracey goto done;
8359 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8360 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8361 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8362 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8363 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8364 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8365 c156c7a4 2020-12-18 stsp goto done;
8366 6458efa5 2020-11-24 stsp if (worktree)
8367 6458efa5 2020-11-24 stsp repo_path =
8368 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8370 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8371 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8372 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8373 c156c7a4 2020-12-18 stsp goto done;
8377 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8378 6458efa5 2020-11-24 stsp if (error != NULL)
8379 6458efa5 2020-11-24 stsp goto done;
8381 6458efa5 2020-11-24 stsp init_curses();
8383 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8384 51a10b52 2020-12-26 stsp if (error)
8385 51a10b52 2020-12-26 stsp goto done;
8387 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8388 6458efa5 2020-11-24 stsp if (error)
8389 6458efa5 2020-11-24 stsp goto done;
8391 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8392 6458efa5 2020-11-24 stsp if (view == NULL) {
8393 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8394 6458efa5 2020-11-24 stsp goto done;
8397 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8398 6458efa5 2020-11-24 stsp if (error)
8399 6458efa5 2020-11-24 stsp goto done;
8401 6458efa5 2020-11-24 stsp if (worktree) {
8402 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8403 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8404 6458efa5 2020-11-24 stsp worktree = NULL;
8406 6458efa5 2020-11-24 stsp error = view_loop(view);
8408 6458efa5 2020-11-24 stsp free(repo_path);
8409 6458efa5 2020-11-24 stsp free(cwd);
8410 1d0f4054 2021-06-17 stsp if (repo) {
8411 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8412 1d0f4054 2021-06-17 stsp if (close_err)
8413 1d0f4054 2021-06-17 stsp error = close_err;
8415 0ae84acc 2022-06-15 tracey if (pack_fds) {
8416 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8417 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8418 0ae84acc 2022-06-15 tracey if (error == NULL)
8419 0ae84acc 2022-06-15 tracey error = pack_err;
8421 51a10b52 2020-12-26 stsp tog_free_refs();
8422 6458efa5 2020-11-24 stsp return error;
8425 ec2a9698 2022-09-15 mark static const struct got_error*
8426 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8427 ec2a9698 2022-09-15 mark const char *str)
8429 ec2a9698 2022-09-15 mark size_t len;
8431 ec2a9698 2022-09-15 mark if (win == NULL)
8432 ec2a9698 2022-09-15 mark win = stdscr;
8434 ec2a9698 2022-09-15 mark len = strlen(str);
8435 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8437 ec2a9698 2022-09-15 mark if (focus)
8438 ec2a9698 2022-09-15 mark wstandout(win);
8439 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8440 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8441 ec2a9698 2022-09-15 mark if (focus)
8442 ec2a9698 2022-09-15 mark wstandend(win);
8444 ec2a9698 2022-09-15 mark return NULL;
8447 136e2bd4 2022-07-23 mark static const struct got_error *
8448 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8452 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8453 ec2a9698 2022-09-15 mark if (p == NULL) {
8454 ec2a9698 2022-09-15 mark free(*line_offsets);
8455 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8456 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8459 ec2a9698 2022-09-15 mark *line_offsets = p;
8460 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8461 ec2a9698 2022-09-15 mark ++(*nlines);
8462 ec2a9698 2022-09-15 mark return NULL;
8465 ec2a9698 2022-09-15 mark static const struct got_error *
8466 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8470 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8471 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8472 ec2a9698 2022-09-15 mark size_t len = 1;
8474 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8477 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8478 ec2a9698 2022-09-15 mark if (t0 == NULL)
8479 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8481 ec2a9698 2022-09-15 mark len += strlen(t);
8482 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8483 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8485 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8488 ec2a9698 2022-09-15 mark return NULL;
8492 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8493 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8494 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8496 ec2a9698 2022-09-15 mark static const struct got_error *
8497 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8499 ec2a9698 2022-09-15 mark int n, len = width;
8501 ec2a9698 2022-09-15 mark if (km->keys) {
8502 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8503 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8504 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8506 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8507 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8509 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8511 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8512 ec2a9698 2022-09-15 mark if (t0 == NULL)
8513 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8515 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8516 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8517 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8518 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8519 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8520 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8521 ec2a9698 2022-09-15 mark if (n < 0) {
8523 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8525 ec2a9698 2022-09-15 mark first = 0;
8526 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8527 ec2a9698 2022-09-15 mark *off += n;
8531 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8532 ec2a9698 2022-09-15 mark if (n < 0)
8533 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8534 ec2a9698 2022-09-15 mark *off += n;
8536 ec2a9698 2022-09-15 mark return NULL;
8539 ec2a9698 2022-09-15 mark static const struct got_error *
8540 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8542 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8543 ec2a9698 2022-09-15 mark off_t off = 0;
8544 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8545 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8546 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8547 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8548 ec2a9698 2022-09-15 mark GENERATE_HELP
8549 ec2a9698 2022-09-15 mark #undef KEYMAP_
8550 ec2a9698 2022-09-15 mark #undef KEY_
8553 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8555 ec2a9698 2022-09-15 mark return err;
8557 ec2a9698 2022-09-15 mark n = nitems(km);
8558 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8560 ec2a9698 2022-09-15 mark return err;
8562 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8563 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8564 ec2a9698 2022-09-15 mark show = s->all;
8565 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8566 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8569 ec2a9698 2022-09-15 mark if (show) {
8570 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8572 ec2a9698 2022-09-15 mark return err;
8573 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8575 ec2a9698 2022-09-15 mark return err;
8578 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8580 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8581 ec2a9698 2022-09-15 mark return err;
8584 ec2a9698 2022-09-15 mark static const struct got_error *
8585 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8588 ec2a9698 2022-09-15 mark const struct got_error *err;
8590 ec2a9698 2022-09-15 mark free(s->line_offsets);
8591 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8592 ec2a9698 2022-09-15 mark s->nlines = 0;
8594 ec2a9698 2022-09-15 mark f = got_opentemp();
8595 ec2a9698 2022-09-15 mark if (f == NULL)
8596 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8599 ec2a9698 2022-09-15 mark err = format_help(s);
8601 ec2a9698 2022-09-15 mark return err;
8603 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8604 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8606 ec2a9698 2022-09-15 mark return NULL;
8609 ec2a9698 2022-09-15 mark static const struct got_error *
8610 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8612 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8613 ec2a9698 2022-09-15 mark return NULL;
8616 eaf2c13e 2022-09-17 mark static void
8617 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8618 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8620 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8622 eaf2c13e 2022-09-17 mark *f = s->f;
8623 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8624 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8625 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8626 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8627 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8628 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8631 ec2a9698 2022-09-15 mark static const struct got_error *
8632 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8634 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8635 ec2a9698 2022-09-15 mark const struct got_error *err;
8636 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8637 ec2a9698 2022-09-15 mark wchar_t *wline;
8638 ec2a9698 2022-09-15 mark char *line;
8639 ec2a9698 2022-09-15 mark ssize_t linelen;
8640 ec2a9698 2022-09-15 mark size_t linesz = 0;
8641 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8642 ec2a9698 2022-09-15 mark int eos = view->nlines;
8644 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8645 ec2a9698 2022-09-15 mark --eos; /* account for border */
8647 ec2a9698 2022-09-15 mark s->lineno = 0;
8648 ec2a9698 2022-09-15 mark rewind(s->f);
8649 ec2a9698 2022-09-15 mark werase(view->window);
8651 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8652 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8654 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8655 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8656 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8658 ec2a9698 2022-09-15 mark return err;
8659 ec2a9698 2022-09-15 mark if (eos <= 1)
8660 ec2a9698 2022-09-15 mark return NULL;
8661 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8664 ec2a9698 2022-09-15 mark s->eof = 0;
8665 ec2a9698 2022-09-15 mark view->maxx = 0;
8666 ec2a9698 2022-09-15 mark line = NULL;
8667 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8668 ec2a9698 2022-09-15 mark attr_t attr = 0;
8670 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8671 ec2a9698 2022-09-15 mark if (linelen == -1) {
8672 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8673 ec2a9698 2022-09-15 mark free(line);
8674 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8676 ec2a9698 2022-09-15 mark s->eof = 1;
8679 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8681 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8683 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8684 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8686 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8687 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8688 ec2a9698 2022-09-15 mark if (err) {
8689 ec2a9698 2022-09-15 mark free(line);
8690 ec2a9698 2022-09-15 mark return err;
8692 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8693 ec2a9698 2022-09-15 mark free(wline);
8694 ec2a9698 2022-09-15 mark wline = NULL;
8697 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8698 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8699 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8700 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8701 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8702 ec2a9698 2022-09-15 mark if (err) {
8703 ec2a9698 2022-09-15 mark free(line);
8704 ec2a9698 2022-09-15 mark return err;
8709 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8710 ec2a9698 2022-09-15 mark view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8711 ec2a9698 2022-09-15 mark if (err) {
8712 ec2a9698 2022-09-15 mark free(line);
8713 ec2a9698 2022-09-15 mark return err;
8715 ec2a9698 2022-09-15 mark rc = waddwstr(view->window, &wline[skip]);
8716 ec2a9698 2022-09-15 mark free(wline);
8717 ec2a9698 2022-09-15 mark wline = NULL;
8718 ec2a9698 2022-09-15 mark if (rc == ERR)
8719 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "waddwstr");
8721 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8722 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8723 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8725 ec2a9698 2022-09-15 mark if (width <= view->ncols)
8726 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8729 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8730 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8731 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8733 ec2a9698 2022-09-15 mark free(line);
8734 ec2a9698 2022-09-15 mark if (nprinted > 0)
8735 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8737 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8739 ec2a9698 2022-09-15 mark view_border(view);
8741 ec2a9698 2022-09-15 mark if (s->eof) {
8742 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8743 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8744 ec2a9698 2022-09-15 mark view->ncols - 1);
8745 ec2a9698 2022-09-15 mark if (rc == ERR)
8746 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8748 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8749 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8750 ec2a9698 2022-09-15 mark wstandout(view->window);
8751 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8752 ec2a9698 2022-09-15 mark view->ncols - 1);
8753 ec2a9698 2022-09-15 mark if (rc == ERR)
8754 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8755 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8756 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8757 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8758 ec2a9698 2022-09-15 mark if (rc == ERR)
8759 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8761 ec2a9698 2022-09-15 mark wstandend(view->window);
8764 ec2a9698 2022-09-15 mark return NULL;
8767 ec2a9698 2022-09-15 mark static const struct got_error *
8768 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8770 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8771 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8772 ec2a9698 2022-09-15 mark char *line = NULL;
8773 ec2a9698 2022-09-15 mark ssize_t linelen;
8774 ec2a9698 2022-09-15 mark size_t linesz = 0;
8775 ec2a9698 2022-09-15 mark int eos, nscroll;
8777 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8778 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8779 ec2a9698 2022-09-15 mark --eos; /* border */
8781 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8783 ec2a9698 2022-09-15 mark switch (ch) {
8785 ec2a9698 2022-09-15 mark view->x = 0;
8788 ec2a9698 2022-09-15 mark view->x = MAX(view->maxx - view->ncols / 3, 0);
8789 ec2a9698 2022-09-15 mark view->count = 0;
8791 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8793 ec2a9698 2022-09-15 mark if (view->x + view->ncols / 3 < view->maxx)
8794 ec2a9698 2022-09-15 mark view->x += 2;
8796 ec2a9698 2022-09-15 mark view->count = 0;
8798 ec2a9698 2022-09-15 mark case KEY_LEFT:
8800 ec2a9698 2022-09-15 mark view->x -= MIN(view->x, 2);
8801 ec2a9698 2022-09-15 mark if (view->x <= 0)
8802 ec2a9698 2022-09-15 mark view->count = 0;
8805 ec2a9698 2022-09-15 mark case KEY_HOME:
8806 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8807 ec2a9698 2022-09-15 mark view->count = 0;
8810 ec2a9698 2022-09-15 mark case KEY_END:
8811 ec2a9698 2022-09-15 mark view->count = 0;
8812 ec2a9698 2022-09-15 mark if (s->eof)
8814 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8815 ec2a9698 2022-09-15 mark s->eof = 1;
8818 ec2a9698 2022-09-15 mark case KEY_UP:
8819 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8820 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8822 ec2a9698 2022-09-15 mark view->count = 0;
8824 ec2a9698 2022-09-15 mark case CTRL('u'):
8826 ec2a9698 2022-09-15 mark nscroll /= 2;
8827 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8828 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8829 ec2a9698 2022-09-15 mark case CTRL('b'):
8831 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8832 ec2a9698 2022-09-15 mark view->count = 0;
8835 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8836 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8839 ec2a9698 2022-09-15 mark case KEY_DOWN:
8840 ec2a9698 2022-09-15 mark case CTRL('n'):
8841 ec2a9698 2022-09-15 mark if (!s->eof)
8842 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8844 ec2a9698 2022-09-15 mark view->count = 0;
8846 ec2a9698 2022-09-15 mark case CTRL('d'):
8848 ec2a9698 2022-09-15 mark nscroll /= 2;
8849 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8850 ec2a9698 2022-09-15 mark case KEY_NPAGE:
8851 ec2a9698 2022-09-15 mark case CTRL('f'):
8854 ec2a9698 2022-09-15 mark if (s->eof) {
8855 ec2a9698 2022-09-15 mark view->count = 0;
8858 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
8859 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8860 ec2a9698 2022-09-15 mark s->first_displayed_line++;
8861 ec2a9698 2022-09-15 mark if (linelen == -1) {
8862 ec2a9698 2022-09-15 mark if (feof(s->f))
8863 ec2a9698 2022-09-15 mark s->eof = 1;
8865 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
8869 ec2a9698 2022-09-15 mark free(line);
8872 ec2a9698 2022-09-15 mark view->count = 0;
8876 ec2a9698 2022-09-15 mark return err;
8879 ec2a9698 2022-09-15 mark static const struct got_error *
8880 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
8882 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8884 ec2a9698 2022-09-15 mark free(s->line_offsets);
8885 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8886 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
8887 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8889 ec2a9698 2022-09-15 mark return NULL;
8892 ec2a9698 2022-09-15 mark static const struct got_error *
8893 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
8895 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8898 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
8899 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8901 ec2a9698 2022-09-15 mark wclear(view->window);
8902 ec2a9698 2022-09-15 mark view->count = 0;
8903 ec2a9698 2022-09-15 mark view->x = 0;
8904 ec2a9698 2022-09-15 mark s->all = !s->all;
8905 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8906 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8907 ec2a9698 2022-09-15 mark s->matched_line = 0;
8909 ec2a9698 2022-09-15 mark return create_help(s);
8912 ec2a9698 2022-09-15 mark static const struct got_error *
8913 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
8915 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8916 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8918 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
8919 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8920 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8921 ec2a9698 2022-09-15 mark s->selected_line = 1;
8923 ec2a9698 2022-09-15 mark view->show = show_help_view;
8924 ec2a9698 2022-09-15 mark view->input = input_help_view;
8925 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
8926 ec2a9698 2022-09-15 mark view->close = close_help_view;
8927 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
8928 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
8929 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
8931 ec2a9698 2022-09-15 mark err = create_help(s);
8932 ec2a9698 2022-09-15 mark return err;
8935 ec2a9698 2022-09-15 mark static const struct got_error *
8936 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8937 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
8939 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
8941 136e2bd4 2022-07-23 mark *new_view = NULL;
8943 136e2bd4 2022-07-23 mark switch (request) {
8944 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
8945 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
8946 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
8948 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
8949 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
8950 136e2bd4 2022-07-23 mark view, s->repo);
8952 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8953 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8955 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
8956 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
8957 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
8959 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
8960 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
8963 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8964 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8966 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
8967 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
8968 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
8969 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
8970 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8971 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
8972 136e2bd4 2022-07-23 mark &view->state.tree);
8973 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8974 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
8975 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8976 136e2bd4 2022-07-23 mark view->state.ref.repo);
8978 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8979 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8981 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
8982 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8983 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
8984 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
8985 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
8986 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
8987 136e2bd4 2022-07-23 mark view->state.log.repo);
8988 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8989 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
8990 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8991 136e2bd4 2022-07-23 mark view->state.ref.repo);
8993 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8994 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8996 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
8997 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8998 136e2bd4 2022-07-23 mark if (*new_view == NULL)
8999 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9000 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9001 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9002 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9003 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9005 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9006 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9008 136e2bd4 2022-07-23 mark view_close(*new_view);
9010 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9011 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9012 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9013 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9014 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9016 ec2a9698 2022-09-15 mark view_close(*new_view);
9019 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9022 136e2bd4 2022-07-23 mark return err;
9026 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9027 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9029 6458efa5 2020-11-24 stsp static void
9030 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9032 9b058f45 2022-06-30 mark switch (view->type) {
9033 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9034 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9035 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9036 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9040 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9041 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9043 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9044 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9047 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9048 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9049 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9051 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9052 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9053 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9055 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9056 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9057 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9063 9b058f45 2022-06-30 mark view->offset = 0;
9067 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9068 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9070 9b058f45 2022-06-30 mark static const struct got_error *
9071 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9073 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9074 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9075 9b058f45 2022-06-30 mark int *selected = NULL;
9076 9b058f45 2022-06-30 mark int header, offset;
9078 9b058f45 2022-06-30 mark switch (view->type) {
9079 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9080 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9081 9b058f45 2022-06-30 mark header = 3;
9082 9b058f45 2022-06-30 mark scrolld = NULL;
9083 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9084 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9085 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9086 9b058f45 2022-06-30 mark s->selected_line -= offset;
9087 9b058f45 2022-06-30 mark view->offset = offset;
9091 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9092 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9093 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9094 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9095 9b058f45 2022-06-30 mark selected = &s->selected;
9098 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9099 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9100 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9101 9b058f45 2022-06-30 mark header = 3;
9102 9b058f45 2022-06-30 mark selected = &s->selected;
9105 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9106 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9107 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9108 9b058f45 2022-06-30 mark header = 5;
9109 9b058f45 2022-06-30 mark selected = &s->selected;
9113 9b058f45 2022-06-30 mark selected = NULL;
9114 9b058f45 2022-06-30 mark scrolld = NULL;
9115 9b058f45 2022-06-30 mark header = 0;
9119 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9120 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9121 9b058f45 2022-06-30 mark view->offset = offset;
9122 9b058f45 2022-06-30 mark if (scrolld && offset) {
9123 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9124 9b058f45 2022-06-30 mark *selected -= offset;
9128 9b058f45 2022-06-30 mark return err;
9131 9b058f45 2022-06-30 mark static void
9132 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9136 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9137 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9138 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9139 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9141 6879ba42 2020-10-01 naddy fputc('\n', fp);
9144 4ed7e80c 2018-05-20 stsp __dead static void
9145 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9147 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9149 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9150 6879ba42 2020-10-01 naddy getprogname());
9151 6879ba42 2020-10-01 naddy if (hflag) {
9152 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9153 6879ba42 2020-10-01 naddy list_commands(fp);
9155 6879ba42 2020-10-01 naddy exit(status);
9158 c2301be8 2018-04-30 stsp static char **
9159 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9161 ee85c5e8 2020-02-29 stsp va_list ap;
9162 c2301be8 2018-04-30 stsp char **argv;
9165 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9167 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9168 c2301be8 2018-04-30 stsp if (argv == NULL)
9169 c2301be8 2018-04-30 stsp err(1, "calloc");
9170 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9171 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9172 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9173 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9176 ee85c5e8 2020-02-29 stsp va_end(ap);
9177 c2301be8 2018-04-30 stsp return argv;
9181 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9182 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9183 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9184 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9185 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9187 ee85c5e8 2020-02-29 stsp static const struct got_error *
9188 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9190 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9191 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9192 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9193 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9194 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9195 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9196 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9197 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9198 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9200 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9201 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9202 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9204 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9205 0ae84acc 2022-06-15 tracey if (error != NULL)
9206 0ae84acc 2022-06-15 tracey goto done;
9208 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9209 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9210 ee85c5e8 2020-02-29 stsp goto done;
9212 ee85c5e8 2020-02-29 stsp if (worktree)
9213 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9215 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9216 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9217 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9218 ee85c5e8 2020-02-29 stsp goto done;
9221 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9222 ee85c5e8 2020-02-29 stsp if (error != NULL)
9223 ee85c5e8 2020-02-29 stsp goto done;
9225 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9226 ee85c5e8 2020-02-29 stsp repo, worktree);
9227 ee85c5e8 2020-02-29 stsp if (error)
9228 ee85c5e8 2020-02-29 stsp goto done;
9230 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9231 84de9106 2020-12-26 stsp if (error)
9232 84de9106 2020-12-26 stsp goto done;
9233 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9234 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9235 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9236 ee85c5e8 2020-02-29 stsp if (error)
9237 ee85c5e8 2020-02-29 stsp goto done;
9239 ee85c5e8 2020-02-29 stsp if (worktree) {
9240 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9241 ee85c5e8 2020-02-29 stsp worktree = NULL;
9244 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9245 a44927cc 2022-04-07 stsp if (error)
9246 a44927cc 2022-04-07 stsp goto done;
9248 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9249 ee85c5e8 2020-02-29 stsp if (error) {
9250 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9251 ee85c5e8 2020-02-29 stsp goto done;
9252 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9253 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9254 6879ba42 2020-10-01 naddy usage(1, 1);
9255 ee85c5e8 2020-02-29 stsp /* not reached */
9258 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9259 ee85c5e8 2020-02-29 stsp if (error)
9260 ee85c5e8 2020-02-29 stsp goto done;
9262 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9264 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9265 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9267 1d0f4054 2021-06-17 stsp if (repo) {
9268 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9269 1d0f4054 2021-06-17 stsp if (error == NULL)
9270 1d0f4054 2021-06-17 stsp error = close_err;
9272 a44927cc 2022-04-07 stsp if (commit)
9273 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9274 ee85c5e8 2020-02-29 stsp if (worktree)
9275 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9276 0ae84acc 2022-06-15 tracey if (pack_fds) {
9277 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9278 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9279 0ae84acc 2022-06-15 tracey if (error == NULL)
9280 0ae84acc 2022-06-15 tracey error = pack_err;
9283 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9284 ee85c5e8 2020-02-29 stsp free(commit_id);
9285 ee85c5e8 2020-02-29 stsp free(cwd);
9286 ee85c5e8 2020-02-29 stsp free(repo_path);
9287 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9288 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9290 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9291 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9292 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9294 87670572 2020-12-26 naddy tog_free_refs();
9295 ee85c5e8 2020-02-29 stsp return error;
9299 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9301 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9302 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9303 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9304 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9305 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9306 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9307 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9309 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9311 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9312 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9314 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9316 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9317 9f7d7167 2018-04-29 stsp switch (ch) {
9319 9f7d7167 2018-04-29 stsp hflag = 1;
9322 53ccebc2 2019-07-30 stsp Vflag = 1;
9325 6879ba42 2020-10-01 naddy usage(hflag, 1);
9326 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9330 9f7d7167 2018-04-29 stsp argc -= optind;
9331 9f7d7167 2018-04-29 stsp argv += optind;
9332 9814e6a3 2020-09-27 naddy optind = 1;
9333 c2301be8 2018-04-30 stsp optreset = 1;
9335 53ccebc2 2019-07-30 stsp if (Vflag) {
9336 53ccebc2 2019-07-30 stsp got_version_print_str();
9337 6879ba42 2020-10-01 naddy return 0;
9340 4010e238 2020-12-04 stsp #ifndef PROFILE
9341 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9342 4010e238 2020-12-04 stsp NULL) == -1)
9343 4010e238 2020-12-04 stsp err(1, "pledge");
9346 c2301be8 2018-04-30 stsp if (argc == 0) {
9347 f29d3e89 2018-06-23 stsp if (hflag)
9348 6879ba42 2020-10-01 naddy usage(hflag, 0);
9349 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9350 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9352 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9356 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9357 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9358 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9359 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9360 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9366 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9367 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9368 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9369 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9370 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9371 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9374 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9375 ee85c5e8 2020-02-29 stsp if (argc != 1)
9376 6879ba42 2020-10-01 naddy usage(0, 1);
9377 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9378 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9380 ee85c5e8 2020-02-29 stsp if (hflag)
9381 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9383 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9387 b46c1e04 2020-09-20 naddy putchar('\n');
9388 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9390 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9391 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9392 a2f4a359 2020-02-28 stsp free(cmd_argv);
9395 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9396 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9397 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9398 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9399 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9400 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);