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,
108 9b058f45 2022-06-30 mark enum tog_view_mode {
109 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
110 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
111 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
114 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
118 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
119 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
120 ba4f502b 2018-08-04 stsp struct got_object_id *id;
121 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
124 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 ba4f502b 2018-08-04 stsp struct commit_queue {
126 ba4f502b 2018-08-04 stsp int ncommits;
127 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
130 f26dddb7 2019-11-08 stsp struct tog_color {
131 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
132 6d17833f 2019-11-08 stsp regex_t regex;
133 6d17833f 2019-11-08 stsp short colorpair;
135 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
137 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 cc488aa7 2022-01-23 stsp static const struct got_error *
142 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 cc488aa7 2022-01-23 stsp struct got_reference* re2)
145 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
146 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
147 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
149 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
150 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
154 cc488aa7 2022-01-23 stsp return NULL;
155 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
157 cc488aa7 2022-01-23 stsp return NULL;
160 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 cc488aa7 2022-01-23 stsp return NULL;
164 11b20872 2019-11-08 stsp static const struct got_error *
165 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
167 51a10b52 2020-12-26 stsp const struct got_error *err;
169 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
173 51a10b52 2020-12-26 stsp return err;
175 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
179 51a10b52 2020-12-26 stsp static void
180 51a10b52 2020-12-26 stsp tog_free_refs(void)
182 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
183 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
184 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
186 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
189 51a10b52 2020-12-26 stsp static const struct got_error *
190 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
191 11b20872 2019-11-08 stsp int idx, short color)
193 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
194 11b20872 2019-11-08 stsp struct tog_color *tc;
195 11b20872 2019-11-08 stsp int regerr = 0;
197 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
198 11b20872 2019-11-08 stsp return NULL;
200 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
202 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
203 11b20872 2019-11-08 stsp if (tc == NULL)
204 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
205 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
206 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 11b20872 2019-11-08 stsp if (regerr) {
208 11b20872 2019-11-08 stsp static char regerr_msg[512];
209 11b20872 2019-11-08 stsp static char err_msg[512];
210 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
211 11b20872 2019-11-08 stsp sizeof(regerr_msg));
212 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 11b20872 2019-11-08 stsp regerr_msg);
214 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
216 11b20872 2019-11-08 stsp return err;
218 11b20872 2019-11-08 stsp tc->colorpair = idx;
219 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
220 11b20872 2019-11-08 stsp return NULL;
223 11b20872 2019-11-08 stsp static void
224 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
226 11b20872 2019-11-08 stsp struct tog_color *tc;
228 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
229 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
230 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
231 11b20872 2019-11-08 stsp regfree(&tc->regex);
236 336075a4 2022-06-25 op static struct tog_color *
237 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
239 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
241 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
242 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
246 11b20872 2019-11-08 stsp return NULL;
250 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 11b20872 2019-11-08 stsp return COLOR_CYAN;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 11b20872 2019-11-08 stsp return COLOR_YELLOW;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 11b20872 2019-11-08 stsp return COLOR_GREEN;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 91b8c405 2020-01-25 stsp return COLOR_CYAN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 11b20872 2019-11-08 stsp return COLOR_GREEN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 11b20872 2019-11-08 stsp return COLOR_CYAN;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 11b20872 2019-11-08 stsp return COLOR_YELLOW;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_GREEN;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
278 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
280 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
287 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
289 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
291 11b20872 2019-11-08 stsp if (val == NULL)
292 11b20872 2019-11-08 stsp return default_color_value(envvar);
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
295 11b20872 2019-11-08 stsp return COLOR_BLACK;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
297 11b20872 2019-11-08 stsp return COLOR_RED;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
299 11b20872 2019-11-08 stsp return COLOR_GREEN;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
301 11b20872 2019-11-08 stsp return COLOR_YELLOW;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
303 11b20872 2019-11-08 stsp return COLOR_BLUE;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
305 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
307 11b20872 2019-11-08 stsp return COLOR_CYAN;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
309 11b20872 2019-11-08 stsp return COLOR_WHITE;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
313 11b20872 2019-11-08 stsp return default_color_value(envvar);
317 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
318 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
319 3dbaef42 2020-11-24 stsp const char *label1, *label2;
320 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
321 f9d37699 2022-06-28 stsp int fd1, fd2;
322 15a087fe 2019-02-21 stsp int first_displayed_line;
323 15a087fe 2019-02-21 stsp int last_displayed_line;
325 15a087fe 2019-02-21 stsp int diff_context;
326 3dbaef42 2020-11-24 stsp int ignore_whitespace;
327 64453f7e 2020-11-21 stsp int force_text_diff;
328 15a087fe 2019-02-21 stsp struct got_repository *repo;
329 bddb1296 2019-11-08 stsp struct tog_colors colors;
330 fe621944 2020-11-10 stsp size_t nlines;
331 f44b1f58 2020-02-02 tracey off_t *line_offsets;
332 f44b1f58 2020-02-02 tracey int matched_line;
333 f44b1f58 2020-02-02 tracey int selected_line;
335 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
336 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
339 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
342 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
343 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
344 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
345 1a76625f 2018-10-22 stsp int commits_needed;
346 fb280deb 2021-08-30 stsp int load_all;
347 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
348 1a76625f 2018-10-22 stsp struct commit_queue *commits;
349 1a76625f 2018-10-22 stsp const char *in_repo_path;
350 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
351 1a76625f 2018-10-22 stsp struct got_repository *repo;
352 74467cc8 2022-06-15 stsp int *pack_fds;
353 1a76625f 2018-10-22 stsp int log_complete;
354 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
355 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
356 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
357 13add988 2019-10-15 stsp int *searching;
358 13add988 2019-10-15 stsp int *search_next_done;
359 13add988 2019-10-15 stsp regex_t *regex;
362 1a76625f 2018-10-22 stsp struct tog_log_view_state {
363 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
365 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
366 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
367 b01e7d3b 2018-08-04 stsp int selected;
368 b01e7d3b 2018-08-04 stsp char *in_repo_path;
369 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
370 b672a97a 2020-01-27 stsp int log_branches;
371 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
372 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
373 1a76625f 2018-10-22 stsp sig_atomic_t quit;
374 1a76625f 2018-10-22 stsp pthread_t thread;
375 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
376 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
377 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
378 11b20872 2019-11-08 stsp struct tog_colors colors;
379 10aab77f 2022-07-19 op int use_committer;
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
384 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
385 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
388 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
389 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
390 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
391 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
392 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
394 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
395 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
396 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
398 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
399 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
400 e9424729 2018-08-04 stsp int nlines;
402 e9424729 2018-08-04 stsp struct tog_view *view;
403 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
407 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
408 e9424729 2018-08-04 stsp const char *path;
409 e9424729 2018-08-04 stsp struct got_repository *repo;
410 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
411 e9424729 2018-08-04 stsp int *complete;
412 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
413 fc06ba56 2019-08-22 stsp void *cancel_arg;
416 e9424729 2018-08-04 stsp struct tog_blame {
418 be659d10 2020-11-18 stsp off_t filesize;
419 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
420 6fcac457 2018-11-19 stsp int nlines;
421 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
422 e9424729 2018-08-04 stsp pthread_t thread;
423 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
424 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
425 e9424729 2018-08-04 stsp const char *path;
426 0ae84acc 2022-06-15 tracey int *pack_fds;
429 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
430 7cbe629d 2018-08-04 stsp int first_displayed_line;
431 7cbe629d 2018-08-04 stsp int last_displayed_line;
432 7cbe629d 2018-08-04 stsp int selected_line;
433 c0f61fa4 2022-07-11 mark int last_diffed_line;
434 7cbe629d 2018-08-04 stsp int blame_complete;
437 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
438 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
439 e5a0f69f 2018-08-18 stsp char *path;
440 7cbe629d 2018-08-04 stsp struct got_repository *repo;
441 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
442 e9424729 2018-08-04 stsp struct tog_blame blame;
443 6c4c42e0 2019-06-24 stsp int matched_line;
444 11b20872 2019-11-08 stsp struct tog_colors colors;
447 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
448 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
449 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
452 ad80ab7b 2018-08-04 stsp int selected;
455 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
457 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
458 ad80ab7b 2018-08-04 stsp char *tree_label;
459 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
460 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
461 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
462 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
465 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
466 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
467 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
468 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
469 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
470 6458efa5 2020-11-24 stsp struct tog_colors colors;
473 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
474 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
475 6458efa5 2020-11-24 stsp struct got_reference *ref;
479 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
481 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
482 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
483 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
486 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
487 6458efa5 2020-11-24 stsp struct got_repository *repo;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
489 bddb1296 2019-11-08 stsp struct tog_colors colors;
493 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
495 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
496 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
497 669b5ffa 2018-10-07 stsp * there is enough screen estate.
499 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
500 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
502 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
503 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
504 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
506 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
507 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
509 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
511 cc3c9aac 2018-08-01 stsp struct tog_view {
512 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
513 26ed57b2 2018-05-19 stsp WINDOW *window;
514 26ed57b2 2018-05-19 stsp PANEL *panel;
515 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
516 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
517 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
518 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
519 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
520 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
521 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
522 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
524 669b5ffa 2018-10-07 stsp struct tog_view *parent;
525 669b5ffa 2018-10-07 stsp struct tog_view *child;
528 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
529 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
530 e78dc838 2020-12-04 stsp * between parent and child.
531 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
532 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
533 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
534 e78dc838 2020-12-04 stsp * situations.
536 e78dc838 2020-12-04 stsp int focus_child;
538 9b058f45 2022-06-30 mark enum tog_view_mode mode;
539 5dc9f4bc 2018-08-04 stsp /* type-specific state */
540 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
542 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
543 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
544 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
545 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
546 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
549 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
550 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
551 e78dc838 2020-12-04 stsp struct tog_view *, int);
552 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
553 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
554 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
556 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
557 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
558 c0c4acc8 2021-01-24 stsp int search_started;
559 60493ae3 2019-06-20 stsp int searching;
560 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
561 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
562 60493ae3 2019-06-20 stsp int search_next_done;
563 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
564 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
565 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
566 1803e47f 2019-06-22 stsp regex_t regex;
567 41605754 2020-11-12 stsp regmatch_t regmatch;
570 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
571 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
572 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
573 78756c87 2020-11-24 stsp struct got_repository *);
574 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
575 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
576 e78dc838 2020-12-04 stsp struct tog_view *, int);
577 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
579 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
580 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
582 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
583 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
584 78756c87 2020-11-24 stsp const char *, const char *, int);
585 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
587 e78dc838 2020-12-04 stsp struct tog_view *, int);
588 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
590 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
593 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
594 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
595 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
596 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
597 e78dc838 2020-12-04 stsp struct tog_view *, int);
598 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
600 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
603 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
604 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
605 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
606 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
607 e78dc838 2020-12-04 stsp struct tog_view *, int);
608 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
609 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
613 6458efa5 2020-11-24 stsp struct got_repository *);
614 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
615 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
616 e78dc838 2020-12-04 stsp struct tog_view *, int);
617 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
618 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
621 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
622 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
623 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
624 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
625 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
627 25791caa 2018-10-24 stsp static void
628 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
630 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
633 83baff54 2019-08-12 stsp static void
634 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
636 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
639 61266923 2020-01-14 stsp static void
640 61266923 2020-01-14 stsp tog_sigcont(int signo)
642 61266923 2020-01-14 stsp tog_sigcont_received = 1;
645 2497f032 2022-05-31 stsp static void
646 2497f032 2022-05-31 stsp tog_sigint(int signo)
648 2497f032 2022-05-31 stsp tog_sigint_received = 1;
651 2497f032 2022-05-31 stsp static void
652 2497f032 2022-05-31 stsp tog_sigterm(int signo)
654 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
658 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
660 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
661 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
664 e5a0f69f 2018-08-18 stsp static const struct got_error *
665 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
667 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
669 669b5ffa 2018-10-07 stsp if (view->child) {
670 5629093a 2022-07-11 stsp child_err = view_close(view->child);
671 669b5ffa 2018-10-07 stsp view->child = NULL;
673 e5a0f69f 2018-08-18 stsp if (view->close)
674 e5a0f69f 2018-08-18 stsp err = view->close(view);
675 ea5e7bb5 2018-08-01 stsp if (view->panel)
676 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
677 ea5e7bb5 2018-08-01 stsp if (view->window)
678 ea5e7bb5 2018-08-01 stsp delwin(view->window);
679 ea5e7bb5 2018-08-01 stsp free(view);
680 5629093a 2022-07-11 stsp return err ? err : child_err;
683 ea5e7bb5 2018-08-01 stsp static struct tog_view *
684 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
685 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
687 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
689 ea5e7bb5 2018-08-01 stsp if (view == NULL)
690 ea5e7bb5 2018-08-01 stsp return NULL;
692 d6b05b5b 2018-08-04 stsp view->type = type;
693 f7d12f7e 2018-08-01 stsp view->lines = LINES;
694 f7d12f7e 2018-08-01 stsp view->cols = COLS;
695 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
696 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
697 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
698 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
699 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
700 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
701 96a765a8 2018-08-04 stsp view_close(view);
702 ea5e7bb5 2018-08-01 stsp return NULL;
704 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
705 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
706 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
711 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
712 ea5e7bb5 2018-08-01 stsp return view;
716 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
718 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
720 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
723 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
725 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
727 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
730 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
732 5c60c32a 2018-10-18 stsp static const struct got_error *
733 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
735 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
737 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
738 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
739 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
741 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
742 9b058f45 2022-06-30 mark view->begin_x = 0;
743 571ccd73 2022-07-19 mark } else if (!view->resized) {
744 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
745 3c1dfe12 2022-07-08 mark view->cols > 119)
746 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
748 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
749 9b058f45 2022-06-30 mark view->begin_y = 0;
751 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
752 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
753 5c60c32a 2018-10-18 stsp view->lines = LINES;
754 5c60c32a 2018-10-18 stsp view->cols = COLS;
755 5c60c32a 2018-10-18 stsp err = view_resize(view);
757 5c60c32a 2018-10-18 stsp return err;
759 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
760 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
762 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
763 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
765 5c60c32a 2018-10-18 stsp return NULL;
768 5c60c32a 2018-10-18 stsp static const struct got_error *
769 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
771 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
773 5c60c32a 2018-10-18 stsp view->begin_x = 0;
774 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
775 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
776 5c60c32a 2018-10-18 stsp view->ncols = COLS;
777 5c60c32a 2018-10-18 stsp view->lines = LINES;
778 5c60c32a 2018-10-18 stsp view->cols = COLS;
779 5c60c32a 2018-10-18 stsp err = view_resize(view);
781 5c60c32a 2018-10-18 stsp return err;
783 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
784 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
786 5c60c32a 2018-10-18 stsp return NULL;
790 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
792 5c60c32a 2018-10-18 stsp return view->parent == NULL;
796 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
798 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
802 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
804 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
808 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
810 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
811 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
814 9b058f45 2022-06-30 mark static void
815 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
817 9b058f45 2022-06-30 mark PANEL *panel;
818 9b058f45 2022-06-30 mark const struct tog_view *view_above;
820 9b058f45 2022-06-30 mark if (view->parent)
821 9b058f45 2022-06-30 mark return view_border(view->parent);
823 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
824 9b058f45 2022-06-30 mark if (panel == NULL)
827 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
828 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
829 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
830 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
831 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
833 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
834 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
837 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
838 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
839 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
840 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
841 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
843 4d8c2215 2018-08-19 stsp static const struct got_error *
844 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
846 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
847 9b058f45 2022-06-30 mark int dif, nlines, ncols;
849 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
851 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
852 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
854 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
855 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
856 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
858 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
860 4dd27a72 2022-06-29 stsp if (view->child) {
861 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
863 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
864 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
865 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
866 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
867 0dbbbe90 2022-06-17 op ncols = COLS;
869 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
870 5c60c32a 2018-10-18 stsp if (view->child->focussed)
871 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
873 5c60c32a 2018-10-18 stsp show_panel(view->panel);
875 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
877 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
878 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
881 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
882 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
883 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
886 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
888 9b058f45 2022-06-30 mark return err;
889 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
890 9b058f45 2022-06-30 mark err = offset_selection_down(view);
892 9b058f45 2022-06-30 mark return err;
894 9b058f45 2022-06-30 mark view_border(view);
895 9b058f45 2022-06-30 mark update_panels();
896 9b058f45 2022-06-30 mark doupdate();
897 9b058f45 2022-06-30 mark show_panel(view->child->panel);
898 9b058f45 2022-06-30 mark nlines = view->nlines;
900 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
901 0dbbbe90 2022-06-17 op ncols = COLS;
903 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
904 571ccd73 2022-07-19 mark err = view->resize(view, dif);
906 571ccd73 2022-07-19 mark return err;
909 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
910 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
911 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
912 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
913 0dbbbe90 2022-06-17 op wclear(view->window);
915 0dbbbe90 2022-06-17 op view->nlines = nlines;
916 0dbbbe90 2022-06-17 op view->ncols = ncols;
917 0dbbbe90 2022-06-17 op view->lines = LINES;
918 0dbbbe90 2022-06-17 op view->cols = COLS;
920 5c60c32a 2018-10-18 stsp return NULL;
923 571ccd73 2022-07-19 mark static const struct got_error *
924 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
926 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
927 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
930 6fe51fee 2022-07-22 mark if (s->selected_entry)
931 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
934 571ccd73 2022-07-19 mark * Request commits to account for the increased
935 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
937 571ccd73 2022-07-19 mark if (s->commits.ncommits < n) {
938 571ccd73 2022-07-19 mark view->nscrolled = n - s->commits.ncommits + increase + 1;
939 571ccd73 2022-07-19 mark err = request_log_commits(view);
942 571ccd73 2022-07-19 mark return err;
945 d9a7ab53 2022-07-11 mark static void
946 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
948 d9a7ab53 2022-07-11 mark if (n == 0)
951 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
952 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
953 d9a7ab53 2022-07-11 mark view->parent->offset += n;
955 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
956 d9a7ab53 2022-07-11 mark } else if (view->offset) {
957 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
958 d9a7ab53 2022-07-11 mark view->offset -= n;
960 d9a7ab53 2022-07-11 mark view->offset = 0;
964 3c1dfe12 2022-07-08 mark static const struct got_error *
965 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
967 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
968 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
970 3c1dfe12 2022-07-08 mark if (view->parent)
971 3c1dfe12 2022-07-08 mark v = view->parent;
975 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
976 3c1dfe12 2022-07-08 mark return NULL;
978 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
980 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
981 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
982 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
983 3c1dfe12 2022-07-08 mark if (view->parent)
984 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
986 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
987 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
988 3c1dfe12 2022-07-08 mark view->count = 0;
989 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
990 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
991 3c1dfe12 2022-07-08 mark view->count = 0;
992 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
994 3c1dfe12 2022-07-08 mark v->ncols = COLS;
995 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
996 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
997 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
999 3c1dfe12 2022-07-08 mark return err;
1000 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1002 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1003 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1004 3c1dfe12 2022-07-08 mark if (view->parent)
1005 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1007 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1008 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1009 3c1dfe12 2022-07-08 mark view->count = 0;
1010 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1011 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1012 3c1dfe12 2022-07-08 mark view->count = 0;
1013 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1015 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1018 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1019 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1020 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1021 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1023 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1025 3c1dfe12 2022-07-08 mark return err;
1026 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1028 3c1dfe12 2022-07-08 mark return err;
1030 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1031 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1033 3c1dfe12 2022-07-08 mark return err;
1036 6fe51fee 2022-07-22 mark if (v->resize)
1037 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1038 6fe51fee 2022-07-22 mark else if (v->child->resize)
1039 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1041 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1043 3c1dfe12 2022-07-08 mark return err;
1046 3c1dfe12 2022-07-08 mark static void
1047 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1049 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1051 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1052 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1055 669b5ffa 2018-10-07 stsp static const struct got_error *
1056 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1058 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1060 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1061 669b5ffa 2018-10-07 stsp return NULL;
1063 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1064 669b5ffa 2018-10-07 stsp view->child = NULL;
1065 669b5ffa 2018-10-07 stsp return err;
1068 0dbbbe90 2022-06-17 op static const struct got_error *
1069 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1071 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1073 669b5ffa 2018-10-07 stsp view->child = child;
1074 669b5ffa 2018-10-07 stsp child->parent = view;
1076 3c1dfe12 2022-07-08 mark err = view_resize(view);
1078 3c1dfe12 2022-07-08 mark return err;
1080 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1081 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1083 3c1dfe12 2022-07-08 mark return err;
1086 34bc9ec9 2019-02-22 stsp static void
1087 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1089 25791caa 2018-10-24 stsp int cols, lines;
1090 25791caa 2018-10-24 stsp struct winsize size;
1092 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1093 25791caa 2018-10-24 stsp cols = 80; /* Default */
1094 25791caa 2018-10-24 stsp lines = 24;
1096 25791caa 2018-10-24 stsp cols = size.ws_col;
1097 25791caa 2018-10-24 stsp lines = size.ws_row;
1099 25791caa 2018-10-24 stsp resize_term(lines, cols);
1102 2b49a8ae 2019-06-22 stsp static const struct got_error *
1103 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1105 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1106 9b058f45 2022-06-30 mark struct tog_view *v = view;
1107 2b49a8ae 2019-06-22 stsp char pattern[1024];
1110 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1111 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1112 c0c4acc8 2021-01-24 stsp view->searching = 0;
1113 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1115 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1117 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1118 2b49a8ae 2019-06-22 stsp return NULL;
1120 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1121 9b058f45 2022-06-30 mark v = view->child;
1123 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1124 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1126 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1127 2b49a8ae 2019-06-22 stsp nocbreak();
1129 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1130 9b058f45 2022-06-30 mark wrefresh(v->window);
1133 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1134 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1135 2b49a8ae 2019-06-22 stsp return NULL;
1137 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1138 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1139 7c32bd05 2019-06-22 stsp if (err) {
1140 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1141 7c32bd05 2019-06-22 stsp return err;
1143 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1144 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1145 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1146 2b49a8ae 2019-06-22 stsp view->search_next(view);
1149 2b49a8ae 2019-06-22 stsp return NULL;
1152 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1153 d2366e29 2022-07-07 mark static const struct got_error *
1154 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1156 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1157 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1159 d2366e29 2022-07-07 mark if (view->parent)
1160 d2366e29 2022-07-07 mark v = view->parent;
1164 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1165 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1167 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1169 7532ccda 2022-07-11 mark if (!v->child)
1170 7532ccda 2022-07-11 mark return NULL;
1171 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1172 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1174 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1175 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1176 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1177 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1178 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1181 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1182 d2366e29 2022-07-07 mark v->ncols = COLS;
1183 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1184 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1186 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1188 d2366e29 2022-07-07 mark return err;
1190 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1191 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1192 d2366e29 2022-07-07 mark v->focus_child = 1;
1194 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1196 d2366e29 2022-07-07 mark return err;
1197 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1199 d2366e29 2022-07-07 mark return err;
1201 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1202 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1203 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1204 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1205 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1207 7532ccda 2022-07-11 mark offset_selection_up(v);
1208 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1210 dff91825 2022-07-22 mark if (v->resize)
1211 dff91825 2022-07-22 mark err = v->resize(v, 0);
1212 dff91825 2022-07-22 mark else if (v->child->resize)
1213 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1215 d2366e29 2022-07-07 mark return err;
1219 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1220 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1222 640cd7ff 2022-06-22 mark static int
1223 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1225 9b058f45 2022-06-30 mark struct tog_view *v = view;
1226 9b058f45 2022-06-30 mark int x, n = 0;
1228 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1229 9b058f45 2022-06-30 mark v = view->child;
1230 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1231 9b058f45 2022-06-30 mark v = view->parent;
1233 640cd7ff 2022-06-22 mark view->count = 0;
1234 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1235 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1236 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1237 9b058f45 2022-06-30 mark waddch(v->window, ':');
1240 9b058f45 2022-06-30 mark x = getcurx(v->window);
1241 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1242 9b058f45 2022-06-30 mark waddch(v->window, c);
1243 9b058f45 2022-06-30 mark wrefresh(v->window);
1247 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1248 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1250 640cd7ff 2022-06-22 mark if (n >= 9999999)
1251 640cd7ff 2022-06-22 mark n = 9999999;
1253 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1254 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1256 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1257 640cd7ff 2022-06-22 mark view->count = n;
1262 0cf4efb1 2018-09-29 stsp static const struct got_error *
1263 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1264 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1266 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1267 669b5ffa 2018-10-07 stsp struct tog_view *v;
1268 1a76625f 2018-10-22 stsp int ch, errcode;
1270 e5a0f69f 2018-08-18 stsp *new = NULL;
1272 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1273 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1274 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1275 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1276 640cd7ff 2022-06-22 mark view->count = 0;
1279 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1280 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1281 82954512 2020-02-03 stsp if (errcode)
1282 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1283 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1284 3da8ef85 2021-09-21 stsp sched_yield();
1285 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1286 82954512 2020-02-03 stsp if (errcode)
1287 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1288 82954512 2020-02-03 stsp "pthread_mutex_lock");
1289 60493ae3 2019-06-20 stsp view->search_next(view);
1290 60493ae3 2019-06-20 stsp return NULL;
1293 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE);
1294 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1295 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1296 1a76625f 2018-10-22 stsp if (errcode)
1297 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1298 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1299 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1301 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1302 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1303 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1304 a6d37fac 2022-07-03 mark view->count = 0;
1306 a6d37fac 2022-07-03 mark ch = view->ch;
1308 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1309 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1310 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1312 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1313 1a76625f 2018-10-22 stsp if (errcode)
1314 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1315 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1317 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1318 25791caa 2018-10-24 stsp tog_resizeterm();
1319 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1320 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1321 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1322 25791caa 2018-10-24 stsp err = view_resize(v);
1324 25791caa 2018-10-24 stsp return err;
1325 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1327 25791caa 2018-10-24 stsp return err;
1328 cdfcfb03 2020-12-06 stsp if (v->child) {
1329 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1331 cdfcfb03 2020-12-06 stsp return err;
1332 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1333 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1335 cdfcfb03 2020-12-06 stsp return err;
1336 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1337 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1339 3c1dfe12 2022-07-08 mark return err;
1345 e5a0f69f 2018-08-18 stsp switch (ch) {
1347 640cd7ff 2022-06-22 mark view->count = 0;
1348 1e37a5c2 2019-05-12 jcs if (view->child) {
1349 e78dc838 2020-12-04 stsp view->focussed = 0;
1350 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1351 e78dc838 2020-12-04 stsp view->focus_child = 1;
1352 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1353 e78dc838 2020-12-04 stsp view->focussed = 0;
1354 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1355 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1356 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1357 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1358 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1361 9b058f45 2022-06-30 mark return err;
1363 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1364 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1366 9b058f45 2022-06-30 mark return err;
1371 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1372 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1373 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1374 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1378 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1380 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1381 9970f7fc 2020-12-03 stsp view->dying = 1;
1387 640cd7ff 2022-06-22 mark view->count = 0;
1388 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1389 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1391 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1392 e78dc838 2020-12-04 stsp view->focussed = 0;
1393 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1394 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1396 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1398 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1402 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1403 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1405 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1406 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1407 e78dc838 2020-12-04 stsp view->focussed = 1;
1408 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1410 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1411 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1412 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1414 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1418 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1422 6fe51fee 2022-07-22 mark if (view->resize) {
1423 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1427 9b058f45 2022-06-30 mark if (view->parent)
1428 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1430 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1433 3c1dfe12 2022-07-08 mark view->count = 0;
1434 d2366e29 2022-07-07 mark err = switch_split(view);
1437 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1440 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1442 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1445 640cd7ff 2022-06-22 mark view->count = 0;
1446 60493ae3 2019-06-20 stsp if (view->search_start)
1447 2b49a8ae 2019-06-22 stsp view_search_start(view);
1449 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1453 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1454 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1455 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1456 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1457 60493ae3 2019-06-20 stsp view->search_next(view);
1459 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1462 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1463 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1465 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1466 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1467 917d79a7 2022-07-01 stsp if (v->reset) {
1468 917d79a7 2022-07-01 stsp err = v->reset(v);
1470 917d79a7 2022-07-01 stsp return err;
1472 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1473 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1475 917d79a7 2022-07-01 stsp return err;
1480 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1484 e5a0f69f 2018-08-18 stsp return err;
1488 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1490 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1491 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1493 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1495 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1498 669b5ffa 2018-10-07 stsp return view->focussed;
1501 bcbd79e2 2018-08-19 stsp static const struct got_error *
1502 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1504 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1505 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1506 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1507 d2366e29 2022-07-07 mark char *mode;
1508 fd823528 2018-10-22 stsp int fast_refresh = 10;
1509 1a76625f 2018-10-22 stsp int done = 0, errcode;
1511 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1512 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1513 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1515 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1517 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1518 1a76625f 2018-10-22 stsp if (errcode)
1519 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1521 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1522 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1524 1004088d 2018-09-29 stsp view->focussed = 1;
1525 878940b7 2018-09-29 stsp err = view->show(view);
1527 0cf4efb1 2018-09-29 stsp return err;
1528 0cf4efb1 2018-09-29 stsp update_panels();
1529 0cf4efb1 2018-09-29 stsp doupdate();
1530 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1531 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1532 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1533 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1534 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1536 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1539 9970f7fc 2020-12-03 stsp if (view->dying) {
1540 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1542 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1543 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1545 e78dc838 2020-12-04 stsp else if (view->parent)
1546 669b5ffa 2018-10-07 stsp prev = view->parent;
1548 e78dc838 2020-12-04 stsp if (view->parent) {
1549 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1550 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1551 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1552 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1553 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1556 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1557 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1559 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1561 9970f7fc 2020-12-03 stsp err = view_close(view);
1563 e5a0f69f 2018-08-18 stsp goto done;
1565 e78dc838 2020-12-04 stsp view = NULL;
1566 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1567 e78dc838 2020-12-04 stsp if (v->focussed)
1570 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1571 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1573 e78dc838 2020-12-04 stsp view = prev;
1574 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1575 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1576 e78dc838 2020-12-04 stsp tog_view_list_head);
1578 e78dc838 2020-12-04 stsp if (view) {
1579 e78dc838 2020-12-04 stsp if (view->focus_child) {
1580 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1581 e78dc838 2020-12-04 stsp view = view->child;
1583 e78dc838 2020-12-04 stsp view->focussed = 1;
1587 bcbd79e2 2018-08-19 stsp if (new_view) {
1588 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1589 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1590 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1591 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1593 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1594 86c66b02 2018-10-18 stsp err = view_close(v);
1596 86c66b02 2018-10-18 stsp goto done;
1599 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1600 fed7eaa8 2018-10-24 stsp view = new_view;
1602 669b5ffa 2018-10-07 stsp if (view) {
1603 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1604 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1605 e78dc838 2020-12-04 stsp view = view->child;
1607 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1608 e78dc838 2020-12-04 stsp view = view->parent;
1610 e78dc838 2020-12-04 stsp show_panel(view->panel);
1611 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1612 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1613 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1614 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1616 1a76625f 2018-10-22 stsp goto done;
1618 669b5ffa 2018-10-07 stsp err = view->show(view);
1620 1a76625f 2018-10-22 stsp goto done;
1621 669b5ffa 2018-10-07 stsp if (view->child) {
1622 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1624 1a76625f 2018-10-22 stsp goto done;
1626 1a76625f 2018-10-22 stsp update_panels();
1627 1a76625f 2018-10-22 stsp doupdate();
1631 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1632 5629093a 2022-07-11 stsp const struct got_error *close_err;
1633 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1634 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1635 5629093a 2022-07-11 stsp close_err = view_close(view);
1636 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1637 5629093a 2022-07-11 stsp err = close_err;
1640 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1641 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1642 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1644 e5a0f69f 2018-08-18 stsp return err;
1647 4ed7e80c 2018-05-20 stsp __dead static void
1648 9f7d7167 2018-04-29 stsp usage_log(void)
1651 c70c5802 2018-08-01 stsp fprintf(stderr,
1652 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1653 9f7d7167 2018-04-29 stsp getprogname());
1657 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1658 80ddbec8 2018-04-29 stsp static const struct got_error *
1659 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1661 00dfcb92 2018-06-11 stsp char *vis = NULL;
1662 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1664 963b370f 2018-05-20 stsp *ws = NULL;
1665 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1666 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1667 00dfcb92 2018-06-11 stsp int vislen;
1668 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1669 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1671 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1672 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1674 00dfcb92 2018-06-11 stsp return err;
1675 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1676 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1677 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1678 a7f50699 2018-06-11 stsp goto done;
1682 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1683 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1684 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1685 a7f50699 2018-06-11 stsp goto done;
1688 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1689 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1691 00dfcb92 2018-06-11 stsp free(vis);
1692 963b370f 2018-05-20 stsp if (err) {
1693 963b370f 2018-05-20 stsp free(*ws);
1694 963b370f 2018-05-20 stsp *ws = NULL;
1695 963b370f 2018-05-20 stsp *wlen = 0;
1697 963b370f 2018-05-20 stsp return err;
1700 145b6838 2022-06-16 stsp static const struct got_error *
1701 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1703 145b6838 2022-06-16 stsp char *dst;
1704 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1706 145b6838 2022-06-16 stsp *ptr = NULL;
1707 145b6838 2022-06-16 stsp n = len = strlen(src);
1708 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1709 145b6838 2022-06-16 stsp if (dst == NULL)
1710 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1712 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1713 145b6838 2022-06-16 stsp const char c = src[idx];
1715 145b6838 2022-06-16 stsp if (c == '\t') {
1716 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1719 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1720 6e1c41ad 2022-06-16 mark if (p == NULL) {
1721 6e1c41ad 2022-06-16 mark free(dst);
1722 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1727 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1730 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1734 145b6838 2022-06-16 stsp dst[sz] = '\0';
1735 145b6838 2022-06-16 stsp *ptr = dst;
1736 145b6838 2022-06-16 stsp return NULL;
1740 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1741 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1742 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1746 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1748 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1750 4e4a9ac8 2022-06-17 op if (n == 0) {
1751 4e4a9ac8 2022-06-17 op *rcol = cols;
1755 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1756 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1757 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1759 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1761 4e4a9ac8 2022-06-17 op if (width == -1) {
1763 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1766 4e4a9ac8 2022-06-17 op if (cols + width > n)
1768 4e4a9ac8 2022-06-17 op cols += width;
1771 4e4a9ac8 2022-06-17 op *rcol = cols;
1776 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1777 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1778 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1780 ccda2f4d 2022-06-16 stsp static const struct got_error *
1781 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1782 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1784 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1786 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1787 145b6838 2022-06-16 stsp char *exstr = NULL;
1788 963b370f 2018-05-20 stsp size_t wlen;
1789 4e4a9ac8 2022-06-17 op int i, scrollx;
1791 963b370f 2018-05-20 stsp *wlinep = NULL;
1792 b700b5d6 2018-07-10 stsp *widthp = 0;
1794 145b6838 2022-06-16 stsp if (expand) {
1795 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1797 145b6838 2022-06-16 stsp return err;
1800 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1801 145b6838 2022-06-16 stsp free(exstr);
1803 963b370f 2018-05-20 stsp return err;
1805 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1807 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1808 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1811 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1812 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1816 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1817 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1819 b700b5d6 2018-07-10 stsp if (widthp)
1820 b700b5d6 2018-07-10 stsp *widthp = cols;
1821 ccda2f4d 2022-06-16 stsp if (scrollxp)
1822 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1824 963b370f 2018-05-20 stsp free(wline);
1826 963b370f 2018-05-20 stsp *wlinep = wline;
1827 963b370f 2018-05-20 stsp return err;
1830 8b473291 2019-02-21 stsp static const struct got_error*
1831 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1832 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1834 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1835 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1837 8b473291 2019-02-21 stsp const char *name;
1839 8b473291 2019-02-21 stsp *refs_str = NULL;
1841 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1842 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1843 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1846 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1847 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1849 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1850 8b473291 2019-02-21 stsp name += 5;
1851 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1852 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1854 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1855 8b473291 2019-02-21 stsp name += 6;
1856 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1857 8b473291 2019-02-21 stsp name += 8;
1858 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1859 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1862 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1865 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1866 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1867 5d844a1e 2019-08-13 stsp if (err) {
1868 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1869 48cae60d 2020-09-22 stsp free(ref_id);
1872 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1873 5d844a1e 2019-08-13 stsp err = NULL;
1874 5d844a1e 2019-08-13 stsp tag = NULL;
1877 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1878 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1879 48cae60d 2020-09-22 stsp free(ref_id);
1881 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1882 52b5abe1 2019-08-13 stsp if (cmp != 0)
1884 8b473291 2019-02-21 stsp s = *refs_str;
1885 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1886 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1887 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1889 8b473291 2019-02-21 stsp *refs_str = NULL;
1895 8b473291 2019-02-21 stsp return err;
1898 963b370f 2018-05-20 stsp static const struct got_error *
1899 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1900 27a741e5 2019-09-11 stsp int col_tab_align)
1902 e6b8b890 2020-12-29 naddy char *smallerthan;
1904 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1905 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1906 5813d178 2019-03-09 stsp author = smallerthan + 1;
1907 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1908 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1909 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1912 5813d178 2019-03-09 stsp static const struct got_error *
1913 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1914 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1915 8fdc79fe 2020-12-01 naddy int author_display_cols)
1917 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1918 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1919 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1920 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1921 5813d178 2019-03-09 stsp char *author = NULL;
1922 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1923 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1924 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1925 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1926 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1927 ccb26ccd 2018-11-05 stsp struct tm tm;
1928 45d799e2 2018-12-23 stsp time_t committer_time;
1929 11b20872 2019-11-08 stsp struct tog_color *tc;
1931 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1932 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1933 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1934 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1935 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1937 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1938 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1940 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1941 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1943 11b20872 2019-11-08 stsp wattr_on(view->window,
1944 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1945 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1947 11b20872 2019-11-08 stsp wattr_off(view->window,
1948 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1949 27a741e5 2019-09-11 stsp col = limit;
1950 b39d25c7 2018-07-10 stsp if (col > avail)
1951 b39d25c7 2018-07-10 stsp goto done;
1953 6570a66d 2019-11-08 stsp if (avail >= 120) {
1954 6570a66d 2019-11-08 stsp char *id_str;
1955 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1957 6570a66d 2019-11-08 stsp goto done;
1958 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1960 11b20872 2019-11-08 stsp wattr_on(view->window,
1961 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1962 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1964 11b20872 2019-11-08 stsp wattr_off(view->window,
1965 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1966 6570a66d 2019-11-08 stsp free(id_str);
1968 6570a66d 2019-11-08 stsp if (col > avail)
1969 6570a66d 2019-11-08 stsp goto done;
1972 10aab77f 2022-07-19 op if (s->use_committer)
1973 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
1975 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
1976 5813d178 2019-03-09 stsp if (author == NULL) {
1977 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1978 80ddbec8 2018-04-29 stsp goto done;
1980 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1982 bb737323 2018-05-20 stsp goto done;
1983 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1985 11b20872 2019-11-08 stsp wattr_on(view->window,
1986 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1987 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1989 11b20872 2019-11-08 stsp wattr_off(view->window,
1990 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1991 bb737323 2018-05-20 stsp col += author_width;
1992 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1993 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1995 bb737323 2018-05-20 stsp author_width++;
1997 9c2eaf34 2018-05-20 stsp if (col > avail)
1998 9c2eaf34 2018-05-20 stsp goto done;
2000 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2002 6d9fbc00 2018-04-29 stsp goto done;
2003 bb737323 2018-05-20 stsp logmsg = logmsg0;
2004 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2006 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2007 bb737323 2018-05-20 stsp if (newline)
2008 bb737323 2018-05-20 stsp *newline = '\0';
2009 ccda2f4d 2022-06-16 stsp limit = avail - col;
2010 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2011 4d1f6af3 2022-06-17 op limit--; /* for the border */
2012 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2013 ccda2f4d 2022-06-16 stsp limit, col, 1);
2015 29688b02 2022-06-16 stsp goto done;
2016 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2017 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2018 27a741e5 2019-09-11 stsp while (col < avail) {
2019 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2023 80ddbec8 2018-04-29 stsp free(logmsg0);
2024 bb737323 2018-05-20 stsp free(wlogmsg);
2025 5813d178 2019-03-09 stsp free(author);
2026 bb737323 2018-05-20 stsp free(wauthor);
2027 80ddbec8 2018-04-29 stsp free(line);
2028 80ddbec8 2018-04-29 stsp return err;
2031 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2032 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2033 899d86c2 2018-05-10 stsp struct got_object_id *id)
2035 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2037 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2038 80ddbec8 2018-04-29 stsp if (entry == NULL)
2039 899d86c2 2018-05-10 stsp return NULL;
2041 899d86c2 2018-05-10 stsp entry->id = id;
2042 99db9666 2018-05-07 stsp entry->commit = commit;
2043 899d86c2 2018-05-10 stsp return entry;
2046 99db9666 2018-05-07 stsp static void
2047 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2049 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2051 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2052 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2053 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2054 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2055 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2056 99db9666 2018-05-07 stsp free(entry);
2059 99db9666 2018-05-07 stsp static void
2060 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2062 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2063 99db9666 2018-05-07 stsp pop_commit(commits);
2066 c4972b91 2018-05-07 stsp static const struct got_error *
2067 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2068 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2070 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2071 13add988 2019-10-15 stsp regmatch_t regmatch;
2072 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2074 13add988 2019-10-15 stsp *have_match = 0;
2076 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2078 13add988 2019-10-15 stsp return err;
2080 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2082 13add988 2019-10-15 stsp goto done;
2084 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2085 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2086 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2087 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2088 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2089 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2090 13add988 2019-10-15 stsp *have_match = 1;
2092 13add988 2019-10-15 stsp free(id_str);
2093 13add988 2019-10-15 stsp free(logmsg);
2094 13add988 2019-10-15 stsp return err;
2097 13add988 2019-10-15 stsp static const struct got_error *
2098 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2100 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2103 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2104 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2105 1a76625f 2018-10-22 stsp * while updating the display.
2108 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2109 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2110 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2111 1a76625f 2018-10-22 stsp int errcode;
2113 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2114 4e0d2870 2020-12-07 naddy NULL, NULL);
2115 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2118 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2121 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2122 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2123 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2127 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2128 1a76625f 2018-10-22 stsp if (errcode) {
2129 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2130 13add988 2019-10-15 stsp "pthread_mutex_lock");
2134 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2135 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2136 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2138 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2139 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2140 7c1452c1 2020-03-26 stsp int have_match;
2141 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2144 7c1452c1 2020-03-26 stsp if (have_match)
2145 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2148 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2149 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2150 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2151 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2154 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2156 9ba79e04 2018-06-11 stsp return err;
2159 2b779855 2020-12-05 naddy static void
2160 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2162 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2163 2b779855 2020-12-05 naddy int ncommits = 0;
2165 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2166 2b779855 2020-12-05 naddy while (entry) {
2167 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2168 2b779855 2020-12-05 naddy s->selected_entry = entry;
2171 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2172 2b779855 2020-12-05 naddy ncommits++;
2176 0553a4e3 2018-04-30 stsp static const struct got_error *
2177 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2179 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2180 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2181 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2182 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2183 60493ae3 2019-06-20 stsp int width;
2184 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2185 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2186 8b473291 2019-02-21 stsp char *refs_str = NULL;
2187 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2188 11b20872 2019-11-08 stsp struct tog_color *tc;
2189 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2191 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2192 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2193 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2194 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2196 ecb28ae0 2018-07-16 stsp return err;
2197 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2198 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2199 d2075bf3 2020-12-25 stsp if (refs) {
2200 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2201 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2203 d2075bf3 2020-12-25 stsp goto done;
2207 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2208 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2210 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2211 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2212 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2213 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2214 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2215 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2216 8f4ed634 2020-03-26 stsp goto done;
2219 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2221 f9686aa5 2020-03-27 stsp if (view->searching) {
2222 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2223 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2224 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2225 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2226 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2227 f9686aa5 2020-03-27 stsp search_str = "searching...";
2230 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2231 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2232 f9686aa5 2020-03-27 stsp search_str ? search_str :
2233 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2234 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2235 8f4ed634 2020-03-26 stsp goto done;
2239 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2240 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2241 87411fa9 2022-06-16 stsp "........................................",
2242 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2243 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2244 1a76625f 2018-10-22 stsp header = NULL;
2245 1a76625f 2018-10-22 stsp goto done;
2247 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2248 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2249 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2250 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2251 1a76625f 2018-10-22 stsp header = NULL;
2252 1a76625f 2018-10-22 stsp goto done;
2254 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2256 1a76625f 2018-10-22 stsp goto done;
2258 2814baeb 2018-08-01 stsp werase(view->window);
2260 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2261 a3404814 2018-09-02 stsp wstandout(view->window);
2262 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2264 11b20872 2019-11-08 stsp wattr_on(view->window,
2265 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2266 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2268 11b20872 2019-11-08 stsp wattr_off(view->window,
2269 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2270 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2271 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2274 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2275 a3404814 2018-09-02 stsp wstandend(view->window);
2276 ecb28ae0 2018-07-16 stsp free(wline);
2277 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2278 1a76625f 2018-10-22 stsp goto done;
2280 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2281 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2282 5813d178 2019-03-09 stsp ncommits = 0;
2283 145b6838 2022-06-16 stsp view->maxx = 0;
2284 5813d178 2019-03-09 stsp while (entry) {
2285 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2286 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2287 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2288 5813d178 2019-03-09 stsp int width;
2289 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2291 10aab77f 2022-07-19 op if (s->use_committer)
2292 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2294 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2295 5813d178 2019-03-09 stsp if (author == NULL) {
2296 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2297 5813d178 2019-03-09 stsp goto done;
2299 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2300 27a741e5 2019-09-11 stsp date_display_cols);
2301 5813d178 2019-03-09 stsp if (author_cols < width)
2302 5813d178 2019-03-09 stsp author_cols = width;
2303 5813d178 2019-03-09 stsp free(wauthor);
2304 5813d178 2019-03-09 stsp free(author);
2305 a310d9c3 2022-07-21 florian if (err)
2306 a310d9c3 2022-07-21 florian goto done;
2307 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2309 145b6838 2022-06-16 stsp goto done;
2310 145b6838 2022-06-16 stsp msg = msg0;
2311 145b6838 2022-06-16 stsp while (*msg == '\n')
2313 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2314 29688b02 2022-06-16 stsp *eol = '\0';
2315 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2316 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2318 29688b02 2022-06-16 stsp goto done;
2319 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2320 145b6838 2022-06-16 stsp free(msg0);
2321 29688b02 2022-06-16 stsp free(wmsg);
2322 7ca04879 2019-10-19 stsp ncommits++;
2323 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2326 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2327 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2328 867c6645 2018-07-10 stsp ncommits = 0;
2329 899d86c2 2018-05-10 stsp while (entry) {
2330 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2332 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2333 2814baeb 2018-08-01 stsp wstandout(view->window);
2334 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2335 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2336 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2337 2814baeb 2018-08-01 stsp wstandend(view->window);
2339 60493ae3 2019-06-20 stsp goto done;
2340 0553a4e3 2018-04-30 stsp ncommits++;
2341 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2342 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2345 9b058f45 2022-06-30 mark view_border(view);
2347 1a76625f 2018-10-22 stsp free(id_str);
2348 8b473291 2019-02-21 stsp free(refs_str);
2349 1a76625f 2018-10-22 stsp free(ncommits_str);
2350 1a76625f 2018-10-22 stsp free(header);
2351 80ddbec8 2018-04-29 stsp return err;
2354 07b55e75 2018-05-10 stsp static void
2355 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2357 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2358 07b55e75 2018-05-10 stsp int nscrolled = 0;
2360 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2361 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2364 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2365 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2366 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2367 07b55e75 2018-05-10 stsp if (entry) {
2368 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2369 07b55e75 2018-05-10 stsp nscrolled++;
2374 aa075928 2018-05-10 stsp static const struct got_error *
2375 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2377 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2378 5e224a3e 2019-02-22 stsp int errcode;
2380 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2382 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2383 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2384 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2385 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2386 7aafa0d1 2019-02-22 stsp if (errcode)
2387 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2388 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2391 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2392 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2394 7c1452c1 2020-03-26 stsp if (!wait)
2397 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2398 ffe38506 2020-12-01 naddy show_log_view(view);
2399 7c1452c1 2020-03-26 stsp update_panels();
2400 7c1452c1 2020-03-26 stsp doupdate();
2402 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2403 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2404 82954512 2020-02-03 stsp if (errcode)
2405 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2406 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2408 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2409 ffe38506 2020-12-01 naddy show_log_view(view);
2410 7c1452c1 2020-03-26 stsp update_panels();
2411 7c1452c1 2020-03-26 stsp doupdate();
2414 5e224a3e 2019-02-22 stsp return NULL;
2417 5e224a3e 2019-02-22 stsp static const struct got_error *
2418 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2420 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2421 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2423 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2424 2525dccb 2022-07-13 mark return NULL;
2426 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2427 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2428 9b058f45 2022-06-30 mark view->nscrolled = 0;
2430 9b058f45 2022-06-30 mark return err;
2433 9b058f45 2022-06-30 mark static const struct got_error *
2434 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2436 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2437 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2438 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2439 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2441 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2442 5e224a3e 2019-02-22 stsp return NULL;
2444 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2445 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2446 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2448 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2450 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2451 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2453 5e224a3e 2019-02-22 stsp return err;
2457 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2458 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2461 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2462 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2464 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2465 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2467 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2468 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2470 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2471 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2473 9b058f45 2022-06-30 mark view->nscrolled = 0;
2475 dd0a52c1 2018-05-20 stsp return err;
2478 cd0acaa7 2018-05-20 stsp static const struct got_error *
2479 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2480 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2481 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2483 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2484 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2485 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2487 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2488 15a94983 2018-12-23 stsp if (diff_view == NULL)
2489 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2491 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2492 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2493 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2494 e5a0f69f 2018-08-18 stsp if (err == NULL)
2495 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2496 cd0acaa7 2018-05-20 stsp return err;
2499 80ddbec8 2018-04-29 stsp static const struct got_error *
2500 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2501 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2503 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2505 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2506 941e9f74 2019-05-21 stsp if (parent == NULL)
2507 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2509 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2510 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2511 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2512 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2513 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2514 941e9f74 2019-05-21 stsp s->tree = subtree;
2515 941e9f74 2019-05-21 stsp s->selected = 0;
2516 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2517 941e9f74 2019-05-21 stsp return NULL;
2520 941e9f74 2019-05-21 stsp static const struct got_error *
2521 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2522 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2524 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2525 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2526 941e9f74 2019-05-21 stsp const char *p;
2527 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2529 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2531 941e9f74 2019-05-21 stsp while (*p) {
2532 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2533 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2534 56e0773d 2019-11-28 stsp char *te_name;
2536 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2539 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2540 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2541 941e9f74 2019-05-21 stsp if (slash == NULL)
2542 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2544 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2545 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2546 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2549 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2550 56e0773d 2019-11-28 stsp if (te == NULL) {
2551 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2552 56e0773d 2019-11-28 stsp free(te_name);
2555 56e0773d 2019-11-28 stsp free(te_name);
2556 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2558 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2559 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2561 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2562 941e9f74 2019-05-21 stsp if (slash)
2563 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2565 941e9f74 2019-05-21 stsp subpath = strdup(path);
2566 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2567 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2571 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2576 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2577 941e9f74 2019-05-21 stsp free(tree_id);
2581 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2582 941e9f74 2019-05-21 stsp if (err) {
2583 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2586 941e9f74 2019-05-21 stsp if (slash == NULL)
2588 941e9f74 2019-05-21 stsp free(subpath);
2589 941e9f74 2019-05-21 stsp subpath = NULL;
2590 941e9f74 2019-05-21 stsp p = slash;
2593 941e9f74 2019-05-21 stsp free(subpath);
2594 1a76625f 2018-10-22 stsp return err;
2597 61266923 2020-01-14 stsp static const struct got_error *
2598 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2599 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2600 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2602 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2603 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2604 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2606 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2607 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2608 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2610 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2612 55cccc34 2020-02-20 stsp return err;
2613 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2615 55cccc34 2020-02-20 stsp *new_view = tree_view;
2617 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2618 55cccc34 2020-02-20 stsp return NULL;
2620 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2623 55cccc34 2020-02-20 stsp static const struct got_error *
2624 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2626 61266923 2020-01-14 stsp sigset_t sigset;
2627 61266923 2020-01-14 stsp int errcode;
2629 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2630 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2632 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2633 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2634 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2635 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2636 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2637 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2638 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2639 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2640 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2642 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2643 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2644 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2646 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2647 61266923 2020-01-14 stsp if (errcode)
2648 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2650 61266923 2020-01-14 stsp return NULL;
2653 1a76625f 2018-10-22 stsp static void *
2654 1a76625f 2018-10-22 stsp log_thread(void *arg)
2656 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2657 1a76625f 2018-10-22 stsp int errcode = 0;
2658 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2659 1a76625f 2018-10-22 stsp int done = 0;
2662 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2663 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2665 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2666 5629093a 2022-07-11 stsp if (errcode) {
2667 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2668 61266923 2020-01-14 stsp return (void *)err;
2671 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2672 5629093a 2022-07-11 stsp if (err) {
2673 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2674 5629093a 2022-07-11 stsp goto done;
2677 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2678 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2679 5629093a 2022-07-11 stsp if (errcode) {
2680 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2681 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2682 5629093a 2022-07-11 stsp goto done;
2684 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2685 1a76625f 2018-10-22 stsp if (err) {
2686 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2687 5629093a 2022-07-11 stsp goto done;
2688 1a76625f 2018-10-22 stsp err = NULL;
2690 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2691 1a76625f 2018-10-22 stsp a->commits_needed--;
2693 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2694 3abe8080 2019-04-10 stsp if (errcode) {
2695 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2696 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2697 5629093a 2022-07-11 stsp goto done;
2698 3abe8080 2019-04-10 stsp } else if (*a->quit)
2700 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2701 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2702 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2703 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2706 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2707 7c1452c1 2020-03-26 stsp if (errcode) {
2708 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2709 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2710 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2711 5629093a 2022-07-11 stsp goto done;
2715 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2717 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2718 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2719 7c1452c1 2020-03-26 stsp &tog_mutex);
2720 5629093a 2022-07-11 stsp if (errcode) {
2721 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2722 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2723 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2724 5629093a 2022-07-11 stsp goto done;
2726 21355643 2020-12-06 stsp if (*a->quit)
2731 3abe8080 2019-04-10 stsp a->log_complete = 1;
2732 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2733 5629093a 2022-07-11 stsp if (errcode)
2734 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2736 5629093a 2022-07-11 stsp if (err) {
2737 5629093a 2022-07-11 stsp tog_thread_error = 1;
2738 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2740 1a76625f 2018-10-22 stsp return (void *)err;
2743 1a76625f 2018-10-22 stsp static const struct got_error *
2744 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2746 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2747 1a76625f 2018-10-22 stsp int errcode;
2749 1a76625f 2018-10-22 stsp if (s->thread) {
2750 1a76625f 2018-10-22 stsp s->quit = 1;
2751 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2752 1a76625f 2018-10-22 stsp if (errcode)
2753 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2754 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2755 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2756 1a76625f 2018-10-22 stsp if (errcode)
2757 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2758 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2759 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2760 1a76625f 2018-10-22 stsp if (errcode)
2761 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2762 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2763 1a76625f 2018-10-22 stsp if (errcode)
2764 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2765 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2766 1a76625f 2018-10-22 stsp s->thread = NULL;
2769 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2770 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2771 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2774 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2775 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2776 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2777 74467cc8 2022-06-15 stsp if (err == NULL)
2778 74467cc8 2022-06-15 stsp err = pack_err;
2779 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2782 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2783 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2784 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2787 5629093a 2022-07-11 stsp return err ? err : thread_err;
2790 9343a5fb 2018-06-23 stsp static const struct got_error *
2791 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2793 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2794 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2795 276b94a1 2020-11-13 naddy int errcode;
2797 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2799 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2800 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2801 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2803 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2804 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2805 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2807 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2808 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2809 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2810 1a76625f 2018-10-22 stsp free(s->start_id);
2811 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2812 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2813 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2814 1a76625f 2018-10-22 stsp return err;
2817 1a76625f 2018-10-22 stsp static const struct got_error *
2818 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2820 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2822 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2823 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2824 60493ae3 2019-06-20 stsp return NULL;
2827 60493ae3 2019-06-20 stsp static const struct got_error *
2828 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2830 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2831 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2832 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2834 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2835 f9686aa5 2020-03-27 stsp show_log_view(view);
2836 f9686aa5 2020-03-27 stsp update_panels();
2837 f9686aa5 2020-03-27 stsp doupdate();
2839 96e2b566 2019-07-08 stsp if (s->search_entry) {
2840 13add988 2019-10-15 stsp int errcode, ch;
2841 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2842 13add988 2019-10-15 stsp if (errcode)
2843 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2844 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2845 13add988 2019-10-15 stsp ch = wgetch(view->window);
2846 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2847 13add988 2019-10-15 stsp if (errcode)
2848 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2849 13add988 2019-10-15 stsp "pthread_mutex_lock");
2850 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2851 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2852 678cbce5 2019-07-28 stsp return NULL;
2854 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2855 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2857 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2858 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2859 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2860 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2861 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2864 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2865 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2866 f704b35c 2022-06-18 stsp * might have changed.
2868 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2869 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2870 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2872 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2874 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2875 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2876 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2878 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2879 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2882 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2885 60493ae3 2019-06-20 stsp while (1) {
2886 13add988 2019-10-15 stsp int have_match = 0;
2888 60493ae3 2019-06-20 stsp if (entry == NULL) {
2889 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2890 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2891 f9967bca 2020-03-27 stsp view->search_next_done =
2892 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2893 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2894 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2895 f801134a 2019-06-25 stsp return NULL;
2898 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2899 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2900 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2902 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2903 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2906 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2907 13add988 2019-10-15 stsp &view->regex);
2910 13add988 2019-10-15 stsp if (have_match) {
2911 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2912 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2916 96e2b566 2019-07-08 stsp s->search_entry = entry;
2917 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2918 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2920 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2923 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2924 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2925 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2926 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2928 60493ae3 2019-06-20 stsp return err;
2931 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2932 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2934 60493ae3 2019-06-20 stsp return err;
2939 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2941 60493ae3 2019-06-20 stsp return NULL;
2944 60493ae3 2019-06-20 stsp static const struct got_error *
2945 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2946 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2947 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2949 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2950 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2951 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2952 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2953 1a76625f 2018-10-22 stsp int errcode;
2955 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2956 f135c941 2020-02-20 stsp free(s->in_repo_path);
2957 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2958 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2959 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2962 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2963 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2964 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2966 fb2756b9 2018-08-04 stsp s->repo = repo;
2967 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2968 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2969 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2970 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2971 9cd7cbd1 2020-12-07 stsp goto done;
2974 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2975 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2976 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2977 5036bf37 2018-09-24 stsp goto done;
2979 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2981 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2982 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2983 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2984 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2986 11b20872 2019-11-08 stsp goto done;
2987 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2988 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2989 11b20872 2019-11-08 stsp if (err) {
2990 11b20872 2019-11-08 stsp free_colors(&s->colors);
2991 11b20872 2019-11-08 stsp goto done;
2993 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2994 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2995 11b20872 2019-11-08 stsp if (err) {
2996 11b20872 2019-11-08 stsp free_colors(&s->colors);
2997 11b20872 2019-11-08 stsp goto done;
3001 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3002 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3003 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3004 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3005 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3006 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3008 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3009 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3011 74467cc8 2022-06-15 stsp goto done;
3013 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3014 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3015 0ae84acc 2022-06-15 tracey if (err)
3016 0ae84acc 2022-06-15 tracey goto done;
3017 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3018 b672a97a 2020-01-27 stsp !s->log_branches);
3020 1a76625f 2018-10-22 stsp goto done;
3021 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3022 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3024 c5b78334 2020-01-12 stsp goto done;
3026 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3027 1a76625f 2018-10-22 stsp if (errcode) {
3028 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3029 1a76625f 2018-10-22 stsp goto done;
3031 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3032 7c1452c1 2020-03-26 stsp if (errcode) {
3033 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3034 7c1452c1 2020-03-26 stsp goto done;
3037 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3038 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3039 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3040 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3041 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3042 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3043 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3044 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3045 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3046 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3047 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3048 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3049 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3052 1a76625f 2018-10-22 stsp close_log_view(view);
3053 ba4f502b 2018-08-04 stsp return err;
3056 e5a0f69f 2018-08-18 stsp static const struct got_error *
3057 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3059 f2f6d207 2020-11-24 stsp const struct got_error *err;
3060 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3062 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3063 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3064 2b380cc8 2018-10-24 stsp &s->thread_args);
3065 2b380cc8 2018-10-24 stsp if (errcode)
3066 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3067 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3068 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3070 f2f6d207 2020-11-24 stsp return err;
3074 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3077 b880cc75 2022-06-30 mark static void
3078 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3080 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3082 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3083 b880cc75 2022-06-30 mark view->count = 0;
3084 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3087 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3089 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3091 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3092 b880cc75 2022-06-30 mark --s->selected;
3094 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3096 b880cc75 2022-06-30 mark select_commit(s);
3100 b880cc75 2022-06-30 mark static const struct got_error *
3101 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3103 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3104 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
3105 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3107 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
3108 b880cc75 2022-06-30 mark if (first == NULL) {
3109 b880cc75 2022-06-30 mark view->count = 0;
3110 b880cc75 2022-06-30 mark return NULL;
3113 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3114 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3115 b880cc75 2022-06-30 mark return NULL;
3117 b880cc75 2022-06-30 mark if (!page) {
3118 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3120 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3121 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3122 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3123 b880cc75 2022-06-30 mark ++s->selected;
3125 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3126 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3127 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3128 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3129 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3131 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3132 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3133 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3135 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
3137 b880cc75 2022-06-30 mark return err;
3138 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
3139 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3140 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
3144 b880cc75 2022-06-30 mark return err;
3147 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3148 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3150 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3151 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3153 b880cc75 2022-06-30 mark select_commit(s);
3155 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3156 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3157 b880cc75 2022-06-30 mark view->count = 0;
3159 b880cc75 2022-06-30 mark return NULL;
3162 9b058f45 2022-06-30 mark static void
3163 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3168 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3169 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3170 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3171 7532ccda 2022-07-11 mark else if (view->resized_y)
3172 7532ccda 2022-07-11 mark *y = view->resized_y;
3174 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3175 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3176 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3177 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3178 7532ccda 2022-07-11 mark else if (view->resized_x)
3179 7532ccda 2022-07-11 mark *x = view->resized_x;
3181 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3185 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3186 e5a0f69f 2018-08-18 stsp static const struct got_error *
3187 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3189 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3191 9b058f45 2022-06-30 mark view->nlines = y;
3192 d2366e29 2022-07-07 mark view->ncols = COLS;
3193 9b058f45 2022-06-30 mark err = view_resize(view);
3195 9b058f45 2022-06-30 mark return err;
3197 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3199 9b058f45 2022-06-30 mark return err;
3202 9b058f45 2022-06-30 mark static const struct got_error *
3203 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3205 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3206 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3207 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3208 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3209 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3210 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
3212 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3213 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3214 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3215 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3216 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3217 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3219 b880cc75 2022-06-30 mark return err;
3222 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3223 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3224 0dca135e 2022-06-30 mark --eos; /* border */
3226 528dedf3 2021-08-30 stsp switch (ch) {
3228 1e37a5c2 2019-05-12 jcs s->quit = 1;
3231 145b6838 2022-06-16 stsp view->x = 0;
3234 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3235 640cd7ff 2022-06-22 mark view->count = 0;
3237 145b6838 2022-06-16 stsp case KEY_RIGHT:
3239 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3240 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3242 640cd7ff 2022-06-22 mark view->count = 0;
3244 145b6838 2022-06-16 stsp case KEY_LEFT:
3246 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3247 640cd7ff 2022-06-22 mark if (view->x <= 0)
3248 640cd7ff 2022-06-22 mark view->count = 0;
3251 1e37a5c2 2019-05-12 jcs case KEY_UP:
3254 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3255 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3258 912a3f79 2021-08-30 j case KEY_HOME:
3259 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3260 640cd7ff 2022-06-22 mark view->count = 0;
3262 83cc4199 2022-06-13 stsp case CTRL('u'):
3264 83cc4199 2022-06-13 stsp nscroll /= 2;
3265 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3266 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3267 a4292ac5 2019-05-12 jcs case CTRL('b'):
3269 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3272 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3275 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3276 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3279 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3282 912a3f79 2021-08-30 j case KEY_END: {
3283 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3284 912a3f79 2021-08-30 j * traverse them all. */
3285 640cd7ff 2022-06-22 mark view->count = 0;
3286 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3287 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3288 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3291 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3292 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3293 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3294 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3296 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3297 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3299 f3bc9f1d 2021-09-05 naddy if (n > 0)
3300 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3301 2b779855 2020-12-05 naddy select_commit(s);
3304 80b7e8da 2022-06-11 stsp case CTRL('d'):
3306 83cc4199 2022-06-13 stsp nscroll /= 2;
3307 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3308 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3309 61417565 2022-06-20 mark case CTRL('f'):
3310 48bb96f0 2022-06-20 naddy case 'f':
3312 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3314 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3315 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3316 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3317 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3318 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3319 2b779855 2020-12-05 naddy select_commit(s);
3320 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3321 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3322 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3323 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3324 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3327 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3328 49b24ee5 2022-07-03 mark case '\r':
3329 640cd7ff 2022-06-22 mark view->count = 0;
3330 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3333 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3334 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3335 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3337 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3338 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3339 78756c87 2020-11-24 stsp view, s->repo);
3343 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3344 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3345 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3350 e78dc838 2020-12-04 stsp view->focussed = 0;
3351 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3352 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3353 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3355 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3356 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
3357 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3359 1e37a5c2 2019-05-12 jcs return err;
3360 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3363 e78dc838 2020-12-04 stsp view->focus_child = 1;
3365 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3368 640cd7ff 2022-06-22 mark view->count = 0;
3369 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3371 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3372 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3373 49b24ee5 2022-07-03 mark err = browse_commit_tree(&tree_view, begin_y, begin_x,
3374 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3378 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3379 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3380 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3384 e78dc838 2020-12-04 stsp view->focussed = 0;
3385 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3386 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
3387 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
3388 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3389 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
3390 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3392 1e37a5c2 2019-05-12 jcs return err;
3393 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3396 e78dc838 2020-12-04 stsp view->focus_child = 1;
3398 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3400 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3401 21355643 2020-12-06 stsp case CTRL('l'):
3403 640cd7ff 2022-06-22 mark view->count = 0;
3404 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3405 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3407 21355643 2020-12-06 stsp err = stop_log_thread(s);
3409 74cfe85e 2020-10-20 stsp return err;
3410 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3411 21355643 2020-12-06 stsp char *parent_path;
3412 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3414 21355643 2020-12-06 stsp return err;
3415 21355643 2020-12-06 stsp free(s->in_repo_path);
3416 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3417 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3418 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3419 21355643 2020-12-06 stsp struct got_object_id *start_id;
3420 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3421 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3422 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3424 21355643 2020-12-06 stsp return err;
3425 21355643 2020-12-06 stsp free(s->start_id);
3426 21355643 2020-12-06 stsp s->start_id = start_id;
3427 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3428 21355643 2020-12-06 stsp } else /* 'B' */
3429 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3431 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3432 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3434 b0dd8d27 2022-06-16 stsp return err;
3436 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3437 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3438 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3440 21355643 2020-12-06 stsp return err;
3441 51a10b52 2020-12-26 stsp tog_free_refs();
3442 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3444 ca51c541 2020-12-07 stsp return err;
3445 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3446 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3448 d01904d4 2019-06-25 stsp return err;
3449 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3450 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3452 b672a97a 2020-01-27 stsp return err;
3453 21355643 2020-12-06 stsp free_commits(&s->commits);
3454 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3455 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3456 21355643 2020-12-06 stsp s->selected_entry = NULL;
3457 21355643 2020-12-06 stsp s->selected = 0;
3458 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3459 21355643 2020-12-06 stsp s->quit = 0;
3460 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3461 dfee752e 2022-06-18 op s->matched_entry = NULL;
3462 dfee752e 2022-06-18 op s->search_entry = NULL;
3463 01a7bcaf 2022-07-22 mark view->offset = 0;
3466 640cd7ff 2022-06-22 mark view->count = 0;
3467 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3468 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3469 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3470 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3471 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3472 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3473 6458efa5 2020-11-24 stsp if (err) {
3474 6458efa5 2020-11-24 stsp view_close(ref_view);
3475 6458efa5 2020-11-24 stsp return err;
3477 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3478 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3479 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3483 e78dc838 2020-12-04 stsp view->focussed = 0;
3484 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3485 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
3486 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
3487 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3488 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
3489 6458efa5 2020-11-24 stsp err = view_close_child(view);
3491 6458efa5 2020-11-24 stsp return err;
3492 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3495 e78dc838 2020-12-04 stsp view->focus_child = 1;
3497 6458efa5 2020-11-24 stsp *new_view = ref_view;
3500 640cd7ff 2022-06-22 mark view->count = 0;
3504 80ddbec8 2018-04-29 stsp return err;
3507 4ed7e80c 2018-05-20 stsp static const struct got_error *
3508 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3510 c2db6724 2019-01-04 stsp const struct got_error *error;
3512 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3513 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3514 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3516 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3517 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3519 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3520 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3522 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3523 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3525 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3526 c2db6724 2019-01-04 stsp if (error != NULL)
3527 c2db6724 2019-01-04 stsp return error;
3529 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3530 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3532 c2db6724 2019-01-04 stsp return NULL;
3535 a915003a 2019-02-05 stsp static void
3536 a915003a 2019-02-05 stsp init_curses(void)
3539 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3540 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3541 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3543 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3544 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3545 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3546 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3547 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3549 a915003a 2019-02-05 stsp initscr();
3551 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3554 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3555 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3556 a915003a 2019-02-05 stsp curs_set(0);
3557 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3558 6d17833f 2019-11-08 stsp start_color();
3559 6d17833f 2019-11-08 stsp use_default_colors();
3563 c2db6724 2019-01-04 stsp static const struct got_error *
3564 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3565 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3567 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3569 f135c941 2020-02-20 stsp if (argc == 0) {
3570 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3571 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3572 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3573 f135c941 2020-02-20 stsp return NULL;
3576 f135c941 2020-02-20 stsp if (worktree) {
3577 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3580 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3582 f135c941 2020-02-20 stsp return err;
3583 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3584 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3585 bfd61697 2020-11-14 stsp p) == -1) {
3586 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3587 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3591 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3593 f135c941 2020-02-20 stsp return err;
3596 f135c941 2020-02-20 stsp static const struct got_error *
3597 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3599 80ddbec8 2018-04-29 stsp const struct got_error *error;
3600 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3601 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3602 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3603 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3604 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3605 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3606 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3607 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3608 04cc582a 2018-08-01 stsp struct tog_view *view;
3609 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3611 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3612 80ddbec8 2018-04-29 stsp switch (ch) {
3614 b672a97a 2020-01-27 stsp log_branches = 1;
3617 80ddbec8 2018-04-29 stsp start_commit = optarg;
3620 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3621 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3622 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3626 17020d27 2019-03-07 stsp usage_log();
3627 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3631 80ddbec8 2018-04-29 stsp argc -= optind;
3632 80ddbec8 2018-04-29 stsp argv += optind;
3634 f135c941 2020-02-20 stsp if (argc > 1)
3635 f135c941 2020-02-20 stsp usage_log();
3637 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3638 0ae84acc 2022-06-15 tracey if (error != NULL)
3639 0ae84acc 2022-06-15 tracey goto done;
3641 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3642 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3643 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3644 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3645 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3646 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3647 c156c7a4 2020-12-18 stsp goto done;
3648 a1fbf39a 2019-08-11 stsp if (worktree)
3649 6962eb72 2020-02-20 stsp repo_path =
3650 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3652 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3653 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3654 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3655 c156c7a4 2020-12-18 stsp goto done;
3659 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3660 80ddbec8 2018-04-29 stsp if (error != NULL)
3661 ecb28ae0 2018-07-16 stsp goto done;
3663 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3664 f135c941 2020-02-20 stsp repo, worktree);
3665 f135c941 2020-02-20 stsp if (error)
3666 f135c941 2020-02-20 stsp goto done;
3668 f135c941 2020-02-20 stsp init_curses();
3670 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3671 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3672 c02c541e 2019-03-29 stsp if (error)
3673 c02c541e 2019-03-29 stsp goto done;
3675 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3676 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3677 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3678 87670572 2020-12-26 naddy if (error)
3679 87670572 2020-12-26 naddy goto done;
3682 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3683 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3684 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3685 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3686 d8f38dc4 2020-12-05 stsp if (error)
3687 d8f38dc4 2020-12-05 stsp goto done;
3688 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3690 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3691 d8f38dc4 2020-12-05 stsp if (error == NULL)
3692 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3693 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3694 d8f38dc4 2020-12-05 stsp goto done;
3695 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3696 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3697 d8f38dc4 2020-12-05 stsp if (error)
3698 d8f38dc4 2020-12-05 stsp goto done;
3701 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3702 04cc582a 2018-08-01 stsp if (view == NULL) {
3703 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3704 04cc582a 2018-08-01 stsp goto done;
3706 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3707 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3708 ba4f502b 2018-08-04 stsp if (error)
3709 ba4f502b 2018-08-04 stsp goto done;
3710 2fc00ff4 2019-08-31 stsp if (worktree) {
3711 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3712 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3713 2fc00ff4 2019-08-31 stsp worktree = NULL;
3715 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3717 f135c941 2020-02-20 stsp free(in_repo_path);
3718 ecb28ae0 2018-07-16 stsp free(repo_path);
3719 ecb28ae0 2018-07-16 stsp free(cwd);
3720 899d86c2 2018-05-10 stsp free(start_id);
3721 d8f38dc4 2020-12-05 stsp free(label);
3723 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3724 1d0f4054 2021-06-17 stsp if (repo) {
3725 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3726 1d0f4054 2021-06-17 stsp if (error == NULL)
3727 1d0f4054 2021-06-17 stsp error = close_err;
3729 ec142235 2019-03-07 stsp if (worktree)
3730 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3731 0ae84acc 2022-06-15 tracey if (pack_fds) {
3732 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3733 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3734 0ae84acc 2022-06-15 tracey if (error == NULL)
3735 0ae84acc 2022-06-15 tracey error = pack_err;
3737 51a10b52 2020-12-26 stsp tog_free_refs();
3738 80ddbec8 2018-04-29 stsp return error;
3741 4ed7e80c 2018-05-20 stsp __dead static void
3742 9f7d7167 2018-04-29 stsp usage_diff(void)
3745 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3746 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3750 6d17833f 2019-11-08 stsp static int
3751 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3752 41605754 2020-11-12 stsp regmatch_t *regmatch)
3754 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3757 336075a4 2022-06-25 op static struct tog_color *
3758 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3760 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3762 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3763 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3764 6d17833f 2019-11-08 stsp return tc;
3767 6d17833f 2019-11-08 stsp return NULL;
3770 4ed7e80c 2018-05-20 stsp static const struct got_error *
3771 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3772 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3774 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3775 44a87665 2022-06-16 stsp char *exstr = NULL;
3776 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3777 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3778 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3779 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3781 41605754 2020-11-12 stsp *wtotal = 0;
3783 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3784 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3786 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3788 44a87665 2022-06-16 stsp return err;
3790 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3791 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3792 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3793 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3794 44a87665 2022-06-16 stsp goto done;
3796 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3797 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3798 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3799 1853e0f4 2022-06-16 stsp goto done;
3801 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3802 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3803 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3804 1853e0f4 2022-06-16 stsp goto done;
3807 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3808 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3809 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3811 1853e0f4 2022-06-16 stsp goto done;
3812 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3814 1853e0f4 2022-06-16 stsp free(wline);
3815 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3816 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3818 1853e0f4 2022-06-16 stsp goto done;
3819 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3820 1853e0f4 2022-06-16 stsp wlimit -= width;
3821 1853e0f4 2022-06-16 stsp *wtotal += width;
3824 41605754 2020-11-12 stsp if (wlimit > 0) {
3825 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3826 1853e0f4 2022-06-16 stsp size_t wlen;
3828 1853e0f4 2022-06-16 stsp free(wline);
3829 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3830 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3832 1853e0f4 2022-06-16 stsp goto done;
3833 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3834 1853e0f4 2022-06-16 stsp while (i < wlen) {
3835 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3836 1853e0f4 2022-06-16 stsp if (width == -1) {
3837 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3838 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3839 1853e0f4 2022-06-16 stsp goto done;
3841 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3843 61417565 2022-06-20 mark w += width;
3846 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3847 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3848 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3849 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3850 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3851 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3852 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3856 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3857 1853e0f4 2022-06-16 stsp free(wline);
3858 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3859 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3860 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3861 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3863 1853e0f4 2022-06-16 stsp goto done;
3864 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3866 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3867 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3869 1853e0f4 2022-06-16 stsp goto done;
3870 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3872 1853e0f4 2022-06-16 stsp *wtotal += width2;
3875 145b6838 2022-06-16 stsp free(wline);
3876 44a87665 2022-06-16 stsp free(exstr);
3877 1853e0f4 2022-06-16 stsp free(seg0);
3878 1853e0f4 2022-06-16 stsp free(seg1);
3879 1853e0f4 2022-06-16 stsp free(seg2);
3880 1853e0f4 2022-06-16 stsp return err;
3883 41605754 2020-11-12 stsp static const struct got_error *
3884 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3886 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3887 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3888 61e69b96 2018-05-20 stsp const struct got_error *err;
3889 fe621944 2020-11-10 stsp int nprinted = 0;
3890 b304db33 2018-05-20 stsp char *line;
3891 826082fe 2020-12-10 stsp size_t linesize = 0;
3892 826082fe 2020-12-10 stsp ssize_t linelen;
3893 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3894 61e69b96 2018-05-20 stsp wchar_t *wline;
3895 e0b650dd 2018-05-20 stsp int width;
3896 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3897 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3898 fe621944 2020-11-10 stsp off_t line_offset;
3900 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3901 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3902 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3904 f7d12f7e 2018-08-01 stsp werase(view->window);
3906 a3404814 2018-09-02 stsp if (header) {
3907 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3908 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3909 135a2da0 2020-11-11 stsp header) == -1)
3910 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3911 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3913 135a2da0 2020-11-11 stsp free(line);
3915 a3404814 2018-09-02 stsp return err;
3917 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3918 a3404814 2018-09-02 stsp wstandout(view->window);
3919 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3920 e54cc94a 2020-11-11 stsp free(wline);
3921 e54cc94a 2020-11-11 stsp wline = NULL;
3922 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3923 a3404814 2018-09-02 stsp wstandend(view->window);
3924 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3925 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3927 a3404814 2018-09-02 stsp if (max_lines <= 1)
3928 a3404814 2018-09-02 stsp return NULL;
3929 a3404814 2018-09-02 stsp max_lines--;
3932 89f1a395 2020-12-01 naddy s->eof = 0;
3933 145b6838 2022-06-16 stsp view->maxx = 0;
3934 826082fe 2020-12-10 stsp line = NULL;
3935 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3936 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3937 826082fe 2020-12-10 stsp if (linelen == -1) {
3938 826082fe 2020-12-10 stsp if (feof(s->f)) {
3939 826082fe 2020-12-10 stsp s->eof = 1;
3942 826082fe 2020-12-10 stsp free(line);
3943 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3946 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3947 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3948 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3949 1853e0f4 2022-06-16 stsp if (err) {
3950 1853e0f4 2022-06-16 stsp free(line);
3951 1853e0f4 2022-06-16 stsp return err;
3953 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3954 1853e0f4 2022-06-16 stsp free(wline);
3955 1853e0f4 2022-06-16 stsp wline = NULL;
3957 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3959 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3960 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3961 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3962 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3963 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3964 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3965 41605754 2020-11-12 stsp if (err) {
3966 41605754 2020-11-12 stsp free(line);
3967 41605754 2020-11-12 stsp return err;
3971 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3972 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3973 969c159c 2022-06-16 stsp if (err) {
3974 969c159c 2022-06-16 stsp free(line);
3975 969c159c 2022-06-16 stsp return err;
3977 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3978 969c159c 2022-06-16 stsp free(wline);
3979 41605754 2020-11-12 stsp wline = NULL;
3982 6d17833f 2019-11-08 stsp wattr_off(view->window,
3983 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3984 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3985 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3986 fe621944 2020-11-10 stsp nprinted++;
3988 826082fe 2020-12-10 stsp free(line);
3989 fe621944 2020-11-10 stsp if (nprinted >= 1)
3990 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3991 89f1a395 2020-12-01 naddy (nprinted - 1);
3993 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3995 9b058f45 2022-06-30 mark view_border(view);
3997 89f1a395 2020-12-01 naddy if (s->eof) {
3998 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3999 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4000 c3e9aa98 2019-05-13 jcs nprinted++;
4003 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4004 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4006 c3e9aa98 2019-05-13 jcs return err;
4009 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4010 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4011 e54cc94a 2020-11-11 stsp free(wline);
4012 e54cc94a 2020-11-11 stsp wline = NULL;
4013 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4016 26ed57b2 2018-05-19 stsp return NULL;
4019 abd2672a 2018-12-23 stsp static char *
4020 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4022 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4023 09867e48 2019-08-13 stsp char *p, *s;
4025 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4026 09867e48 2019-08-13 stsp if (tm == NULL)
4027 09867e48 2019-08-13 stsp return NULL;
4028 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4029 09867e48 2019-08-13 stsp if (s == NULL)
4030 09867e48 2019-08-13 stsp return NULL;
4031 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4033 abd2672a 2018-12-23 stsp *p = '\0';
4037 4ed7e80c 2018-05-20 stsp static const struct got_error *
4038 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4039 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4041 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4042 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4043 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4044 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4046 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4047 0208f208 2020-05-05 stsp if (qid != NULL) {
4048 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4049 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4050 d7b5a0e8 2022-04-20 stsp &qid->id);
4052 0208f208 2020-05-05 stsp return err;
4054 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4055 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4056 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4057 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4058 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4060 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4064 0208f208 2020-05-05 stsp if (tree_id1) {
4065 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4067 0208f208 2020-05-05 stsp goto done;
4070 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4071 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4073 0208f208 2020-05-05 stsp goto done;
4075 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4076 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4078 0208f208 2020-05-05 stsp if (tree1)
4079 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4080 0208f208 2020-05-05 stsp if (tree2)
4081 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4082 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4083 0208f208 2020-05-05 stsp return err;
4086 0208f208 2020-05-05 stsp static const struct got_error *
4087 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4091 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4092 fe621944 2020-11-10 stsp if (p == NULL)
4093 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4094 fe621944 2020-11-10 stsp *line_offsets = p;
4095 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4096 fe621944 2020-11-10 stsp (*nlines)++;
4097 fe621944 2020-11-10 stsp return NULL;
4100 fe621944 2020-11-10 stsp static const struct got_error *
4101 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4102 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4103 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4105 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4106 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4107 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4108 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4109 45d799e2 2018-12-23 stsp time_t committer_time;
4110 45d799e2 2018-12-23 stsp const char *author, *committer;
4111 8b473291 2019-02-21 stsp char *refs_str = NULL;
4112 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4113 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4114 fe621944 2020-11-10 stsp off_t outoff = 0;
4117 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4119 8b473291 2019-02-21 stsp if (refs) {
4120 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4122 8b473291 2019-02-21 stsp return err;
4125 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4127 abd2672a 2018-12-23 stsp return err;
4129 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4130 15a94983 2018-12-23 stsp if (err) {
4131 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4132 15a94983 2018-12-23 stsp goto done;
4135 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4137 fe621944 2020-11-10 stsp goto done;
4139 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4140 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4141 fe621944 2020-11-10 stsp if (n < 0) {
4142 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4143 abd2672a 2018-12-23 stsp goto done;
4145 fe621944 2020-11-10 stsp outoff += n;
4146 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4148 fe621944 2020-11-10 stsp goto done;
4150 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4151 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4152 fe621944 2020-11-10 stsp if (n < 0) {
4153 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4154 abd2672a 2018-12-23 stsp goto done;
4156 fe621944 2020-11-10 stsp outoff += n;
4157 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4159 fe621944 2020-11-10 stsp goto done;
4161 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4162 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4163 fe621944 2020-11-10 stsp if (datestr) {
4164 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4165 fe621944 2020-11-10 stsp if (n < 0) {
4166 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4167 fe621944 2020-11-10 stsp goto done;
4169 fe621944 2020-11-10 stsp outoff += n;
4170 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4172 fe621944 2020-11-10 stsp goto done;
4174 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4175 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4176 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4177 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4178 fe621944 2020-11-10 stsp if (n < 0) {
4179 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4180 fe621944 2020-11-10 stsp goto done;
4182 fe621944 2020-11-10 stsp outoff += n;
4183 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4185 fe621944 2020-11-10 stsp goto done;
4187 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4188 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4189 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4190 9f98ca05 2021-09-24 stsp int pn = 1;
4191 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4192 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4193 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4195 9f98ca05 2021-09-24 stsp goto done;
4196 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4197 9f98ca05 2021-09-24 stsp if (n < 0) {
4198 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4199 9f98ca05 2021-09-24 stsp goto done;
4201 9f98ca05 2021-09-24 stsp outoff += n;
4202 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
4204 9f98ca05 2021-09-24 stsp goto done;
4205 9f98ca05 2021-09-24 stsp free(id_str);
4206 9f98ca05 2021-09-24 stsp id_str = NULL;
4210 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4212 5943eee2 2019-08-13 stsp goto done;
4213 fe621944 2020-11-10 stsp s = logmsg;
4214 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4215 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4216 fe621944 2020-11-10 stsp if (n < 0) {
4217 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4218 fe621944 2020-11-10 stsp goto done;
4220 fe621944 2020-11-10 stsp outoff += n;
4221 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4223 fe621944 2020-11-10 stsp goto done;
4226 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4228 0208f208 2020-05-05 stsp goto done;
4229 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4230 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4231 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4232 fe621944 2020-11-10 stsp if (n < 0) {
4233 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4234 fe621944 2020-11-10 stsp goto done;
4236 fe621944 2020-11-10 stsp outoff += n;
4237 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4239 fe621944 2020-11-10 stsp goto done;
4240 0208f208 2020-05-05 stsp free((char *)pe->path);
4241 0208f208 2020-05-05 stsp free(pe->data);
4244 0208f208 2020-05-05 stsp fputc('\n', outfile);
4246 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4248 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4249 abd2672a 2018-12-23 stsp free(id_str);
4250 5943eee2 2019-08-13 stsp free(logmsg);
4251 8b473291 2019-02-21 stsp free(refs_str);
4252 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4253 7510f233 2020-08-09 stsp if (err) {
4254 ae6a6978 2020-08-09 stsp free(*line_offsets);
4255 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4256 ae6a6978 2020-08-09 stsp *nlines = 0;
4258 fe621944 2020-11-10 stsp return err;
4261 abd2672a 2018-12-23 stsp static const struct got_error *
4262 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4264 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4265 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4266 15a94983 2018-12-23 stsp int obj_type;
4268 fe621944 2020-11-10 stsp free(s->line_offsets);
4269 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4270 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4271 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4272 fe621944 2020-11-10 stsp s->nlines = 0;
4274 511a516b 2018-05-19 stsp f = got_opentemp();
4275 48ae06ee 2018-10-18 stsp if (f == NULL) {
4276 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4277 48ae06ee 2018-10-18 stsp goto done;
4279 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4280 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4281 fb43ecf1 2019-02-11 stsp goto done;
4285 15a94983 2018-12-23 stsp if (s->id1)
4286 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4288 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4290 15a94983 2018-12-23 stsp goto done;
4292 15a94983 2018-12-23 stsp switch (obj_type) {
4293 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4294 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4295 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4296 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4297 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4299 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4300 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4301 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4302 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4303 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4305 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4306 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4307 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4308 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4309 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4311 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4313 3ffacbe1 2020-02-02 tracey goto done;
4314 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4315 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4316 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4317 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4318 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4319 f44b1f58 2020-02-02 tracey if (err)
4320 f44b1f58 2020-02-02 tracey goto done;
4321 f44b1f58 2020-02-02 tracey } else {
4322 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4323 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4324 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4325 fe621944 2020-11-10 stsp err = write_commit_info(
4326 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4327 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4328 f44b1f58 2020-02-02 tracey if (err)
4329 f44b1f58 2020-02-02 tracey goto done;
4334 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4336 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4337 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4338 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4339 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4343 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4346 f44b1f58 2020-02-02 tracey if (err)
4347 f44b1f58 2020-02-02 tracey goto done;
4349 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4350 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4351 48ae06ee 2018-10-18 stsp return err;
4354 f5215bb9 2019-02-22 stsp static void
4355 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4357 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4358 f5215bb9 2019-02-22 stsp update_panels();
4359 f5215bb9 2019-02-22 stsp doupdate();
4362 f44b1f58 2020-02-02 tracey static const struct got_error *
4363 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4365 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4367 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4368 f44b1f58 2020-02-02 tracey return NULL;
4371 f44b1f58 2020-02-02 tracey static const struct got_error *
4372 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4374 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4375 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4376 f44b1f58 2020-02-02 tracey int lineno;
4377 cb713507 2022-06-16 stsp char *line = NULL;
4378 826082fe 2020-12-10 stsp size_t linesize = 0;
4379 826082fe 2020-12-10 stsp ssize_t linelen;
4381 f44b1f58 2020-02-02 tracey if (!view->searching) {
4382 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 f44b1f58 2020-02-02 tracey return NULL;
4386 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4387 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4388 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4390 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4392 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4394 f44b1f58 2020-02-02 tracey while (1) {
4395 f44b1f58 2020-02-02 tracey off_t offset;
4397 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4398 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4399 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4403 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4404 f44b1f58 2020-02-02 tracey lineno = 1;
4406 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4409 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4410 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4411 f44b1f58 2020-02-02 tracey free(line);
4412 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4414 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4415 cb713507 2022-06-16 stsp if (linelen != -1) {
4416 cb713507 2022-06-16 stsp char *exstr;
4417 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4420 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4421 cb713507 2022-06-16 stsp &view->regmatch)) {
4422 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4423 cb713507 2022-06-16 stsp s->matched_line = lineno;
4424 cb713507 2022-06-16 stsp free(exstr);
4427 cb713507 2022-06-16 stsp free(exstr);
4429 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4430 f44b1f58 2020-02-02 tracey lineno++;
4432 f44b1f58 2020-02-02 tracey lineno--;
4434 826082fe 2020-12-10 stsp free(line);
4436 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4437 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4438 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4441 145b6838 2022-06-16 stsp return err;
4444 48ae06ee 2018-10-18 stsp static const struct got_error *
4445 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4447 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4448 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4450 b72706c3 2022-06-01 stsp free(s->id1);
4451 b72706c3 2022-06-01 stsp s->id1 = NULL;
4452 b72706c3 2022-06-01 stsp free(s->id2);
4453 b72706c3 2022-06-01 stsp s->id2 = NULL;
4454 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4455 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4456 b72706c3 2022-06-01 stsp s->f = NULL;
4457 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4458 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4459 b72706c3 2022-06-01 stsp s->f1 = NULL;
4460 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4461 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4462 b72706c3 2022-06-01 stsp s->f2 = NULL;
4463 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4464 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4465 f9d37699 2022-06-28 stsp s->fd1 = -1;
4466 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4467 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4468 f9d37699 2022-06-28 stsp s->fd2 = -1;
4469 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4470 b72706c3 2022-06-01 stsp free(s->line_offsets);
4471 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4472 b72706c3 2022-06-01 stsp s->nlines = 0;
4473 b72706c3 2022-06-01 stsp return err;
4476 b72706c3 2022-06-01 stsp static const struct got_error *
4477 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4478 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4479 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4480 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4482 48ae06ee 2018-10-18 stsp const struct got_error *err;
4483 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4485 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4486 f9d37699 2022-06-28 stsp s->fd1 = -1;
4487 f9d37699 2022-06-28 stsp s->fd2 = -1;
4489 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4490 15a94983 2018-12-23 stsp int type1, type2;
4491 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4493 15a94983 2018-12-23 stsp return err;
4494 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4496 15a94983 2018-12-23 stsp return err;
4498 15a94983 2018-12-23 stsp if (type1 != type2)
4499 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4501 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4502 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4503 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4504 f44b1f58 2020-02-02 tracey s->repo = repo;
4505 f44b1f58 2020-02-02 tracey s->id1 = id1;
4506 f44b1f58 2020-02-02 tracey s->id2 = id2;
4507 3dbaef42 2020-11-24 stsp s->label1 = label1;
4508 3dbaef42 2020-11-24 stsp s->label2 = label2;
4510 15a94983 2018-12-23 stsp if (id1) {
4511 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4512 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4513 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4515 5465d566 2020-02-01 tracey s->id1 = NULL;
4517 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4518 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4519 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4520 b72706c3 2022-06-01 stsp goto done;
4523 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4524 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4525 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4526 a00719e9 2022-06-17 stsp goto done;
4529 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4530 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4531 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4532 b72706c3 2022-06-01 stsp goto done;
4535 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4536 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4537 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4538 f9d37699 2022-06-28 stsp goto done;
4541 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4542 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4543 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4544 f9d37699 2022-06-28 stsp goto done;
4547 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4548 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4549 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4550 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4551 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4552 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4553 5465d566 2020-02-01 tracey s->repo = repo;
4555 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4556 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4558 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4559 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4561 b72706c3 2022-06-01 stsp goto done;
4562 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4563 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4564 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4566 b72706c3 2022-06-01 stsp goto done;
4567 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4568 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4569 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4571 b72706c3 2022-06-01 stsp goto done;
4573 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4574 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4575 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4576 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4577 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4579 b72706c3 2022-06-01 stsp goto done;
4581 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4582 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4583 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4585 b72706c3 2022-06-01 stsp goto done;
4587 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4588 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4589 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4591 b72706c3 2022-06-01 stsp goto done;
4594 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4595 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4596 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4597 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4599 5465d566 2020-02-01 tracey err = create_diff(s);
4601 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4602 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4603 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4604 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4605 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4606 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4609 b72706c3 2022-06-01 stsp close_diff_view(view);
4610 e5a0f69f 2018-08-18 stsp return err;
4613 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4614 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4616 a3404814 2018-09-02 stsp const struct got_error *err;
4617 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4618 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4619 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4621 a3404814 2018-09-02 stsp if (s->id1) {
4622 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4624 a3404814 2018-09-02 stsp return err;
4625 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4627 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4629 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4631 a3404814 2018-09-02 stsp return err;
4632 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4634 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4635 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4636 a3404814 2018-09-02 stsp free(id_str1);
4637 a3404814 2018-09-02 stsp free(id_str2);
4638 a3404814 2018-09-02 stsp return err;
4640 a3404814 2018-09-02 stsp free(id_str1);
4641 a3404814 2018-09-02 stsp free(id_str2);
4643 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4644 267bb3b8 2021-08-01 stsp free(header);
4645 267bb3b8 2021-08-01 stsp return err;
4648 15a087fe 2019-02-21 stsp static const struct got_error *
4649 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4650 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4652 d7a04538 2019-02-21 stsp const struct got_error *err;
4653 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4654 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4655 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4657 15a087fe 2019-02-21 stsp free(s->id2);
4658 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4659 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4660 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4662 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4664 d7a04538 2019-02-21 stsp return err;
4665 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4666 15a087fe 2019-02-21 stsp free(s->id1);
4667 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4668 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4669 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4670 15a087fe 2019-02-21 stsp return NULL;
4673 0cf4efb1 2018-09-29 stsp static const struct got_error *
4674 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4676 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4678 917d79a7 2022-07-01 stsp view->count = 0;
4679 917d79a7 2022-07-01 stsp wclear(view->window);
4680 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4681 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4682 917d79a7 2022-07-01 stsp s->matched_line = 0;
4683 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4684 917d79a7 2022-07-01 stsp return create_diff(s);
4687 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4688 c0f61fa4 2022-07-11 mark int, int, int);
4689 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4690 c0f61fa4 2022-07-11 mark int, int);
4692 917d79a7 2022-07-01 stsp static const struct got_error *
4693 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4695 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4696 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4697 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4698 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4699 826082fe 2020-12-10 stsp char *line = NULL;
4700 826082fe 2020-12-10 stsp size_t linesize = 0;
4701 826082fe 2020-12-10 stsp ssize_t linelen;
4702 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4704 e5a0f69f 2018-08-18 stsp switch (ch) {
4706 145b6838 2022-06-16 stsp view->x = 0;
4709 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4710 640cd7ff 2022-06-22 mark view->count = 0;
4712 145b6838 2022-06-16 stsp case KEY_RIGHT:
4714 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4715 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4717 640cd7ff 2022-06-22 mark view->count = 0;
4719 145b6838 2022-06-16 stsp case KEY_LEFT:
4721 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4722 640cd7ff 2022-06-22 mark if (view->x <= 0)
4723 640cd7ff 2022-06-22 mark view->count = 0;
4727 3dbaef42 2020-11-24 stsp if (ch == 'a')
4728 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4729 3dbaef42 2020-11-24 stsp if (ch == 'w')
4730 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4731 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4734 912a3f79 2021-08-30 j case KEY_HOME:
4735 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4736 640cd7ff 2022-06-22 mark view->count = 0;
4739 912a3f79 2021-08-30 j case KEY_END:
4740 640cd7ff 2022-06-22 mark view->count = 0;
4744 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4748 1e37a5c2 2019-05-12 jcs case KEY_UP:
4749 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4750 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4751 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4753 640cd7ff 2022-06-22 mark view->count = 0;
4755 83cc4199 2022-06-13 stsp case CTRL('u'):
4757 83cc4199 2022-06-13 stsp nscroll /= 2;
4758 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4759 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4760 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4762 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4763 640cd7ff 2022-06-22 mark view->count = 0;
4767 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4768 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4771 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4772 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4773 1e37a5c2 2019-05-12 jcs if (!s->eof)
4774 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4776 640cd7ff 2022-06-22 mark view->count = 0;
4778 83cc4199 2022-06-13 stsp case CTRL('d'):
4780 83cc4199 2022-06-13 stsp nscroll /= 2;
4781 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4782 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4783 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4786 640cd7ff 2022-06-22 mark if (s->eof) {
4787 640cd7ff 2022-06-22 mark view->count = 0;
4791 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4792 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4793 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4794 826082fe 2020-12-10 stsp if (linelen == -1) {
4795 826082fe 2020-12-10 stsp if (feof(s->f)) {
4796 826082fe 2020-12-10 stsp s->eof = 1;
4798 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4802 826082fe 2020-12-10 stsp free(line);
4805 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4806 1e37a5c2 2019-05-12 jcs s->diff_context--;
4807 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4808 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4809 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4810 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4811 27829c9e 2020-11-21 stsp s->nlines) {
4812 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4813 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4816 640cd7ff 2022-06-22 mark view->count = 0;
4819 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4820 1e37a5c2 2019-05-12 jcs s->diff_context++;
4821 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4822 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4823 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4825 640cd7ff 2022-06-22 mark view->count = 0;
4831 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4835 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4836 640cd7ff 2022-06-22 mark view->count = 0;
4839 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4841 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4842 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4843 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4845 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4846 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4849 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4851 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4854 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4857 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4858 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4859 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4861 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4862 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4863 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4865 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4866 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4869 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4871 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4873 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4874 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4875 c0f61fa4 2022-07-11 mark bs->selected_line);
4876 c0f61fa4 2022-07-11 mark if (id == NULL)
4879 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
4882 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4886 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4887 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4888 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4889 145b6838 2022-06-16 stsp view->x = 0;
4891 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4892 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4895 640cd7ff 2022-06-22 mark view->count = 0;
4899 bcbd79e2 2018-08-19 stsp return err;
4902 4ed7e80c 2018-05-20 stsp static const struct got_error *
4903 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4905 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4906 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4907 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4908 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4909 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4910 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4911 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4912 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4913 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4914 3dbaef42 2020-11-24 stsp const char *errstr;
4915 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4916 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4918 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4919 26ed57b2 2018-05-19 stsp switch (ch) {
4921 64453f7e 2020-11-21 stsp force_text_diff = 1;
4924 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4926 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4927 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4928 5a20d08d 2022-02-09 op errstr, errstr);
4930 09b5bff8 2020-02-23 naddy case 'r':
4931 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4932 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4933 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4935 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4938 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4941 17020d27 2019-03-07 stsp usage_diff();
4942 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4946 26ed57b2 2018-05-19 stsp argc -= optind;
4947 26ed57b2 2018-05-19 stsp argv += optind;
4949 26ed57b2 2018-05-19 stsp if (argc == 0) {
4950 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4951 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4952 15a94983 2018-12-23 stsp id_str1 = argv[0];
4953 15a94983 2018-12-23 stsp id_str2 = argv[1];
4955 26ed57b2 2018-05-19 stsp usage_diff();
4957 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4958 0ae84acc 2022-06-15 tracey if (error)
4959 0ae84acc 2022-06-15 tracey goto done;
4961 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4962 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4963 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4964 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4965 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4966 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4967 c156c7a4 2020-12-18 stsp goto done;
4968 a273ac94 2020-02-23 naddy if (worktree)
4969 a273ac94 2020-02-23 naddy repo_path =
4970 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4972 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4973 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4974 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4975 c156c7a4 2020-12-18 stsp goto done;
4979 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4980 eb6600df 2019-01-04 stsp if (error)
4981 eb6600df 2019-01-04 stsp goto done;
4983 a273ac94 2020-02-23 naddy init_curses();
4985 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4986 51a10b52 2020-12-26 stsp if (error)
4987 51a10b52 2020-12-26 stsp goto done;
4989 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4990 26ed57b2 2018-05-19 stsp if (error)
4991 26ed57b2 2018-05-19 stsp goto done;
4993 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4994 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4995 26ed57b2 2018-05-19 stsp if (error)
4996 26ed57b2 2018-05-19 stsp goto done;
4998 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4999 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5000 26ed57b2 2018-05-19 stsp if (error)
5001 26ed57b2 2018-05-19 stsp goto done;
5003 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5004 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5005 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5006 ea5e7bb5 2018-08-01 stsp goto done;
5008 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5009 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5010 5dc9f4bc 2018-08-04 stsp if (error)
5011 5dc9f4bc 2018-08-04 stsp goto done;
5012 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5014 3dbaef42 2020-11-24 stsp free(label1);
5015 3dbaef42 2020-11-24 stsp free(label2);
5016 c02c541e 2019-03-29 stsp free(repo_path);
5017 a273ac94 2020-02-23 naddy free(cwd);
5018 1d0f4054 2021-06-17 stsp if (repo) {
5019 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5020 1d0f4054 2021-06-17 stsp if (error == NULL)
5021 1d0f4054 2021-06-17 stsp error = close_err;
5023 a273ac94 2020-02-23 naddy if (worktree)
5024 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5025 0ae84acc 2022-06-15 tracey if (pack_fds) {
5026 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5027 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5028 0ae84acc 2022-06-15 tracey if (error == NULL)
5029 0ae84acc 2022-06-15 tracey error = pack_err;
5031 51a10b52 2020-12-26 stsp tog_free_refs();
5032 26ed57b2 2018-05-19 stsp return error;
5035 4ed7e80c 2018-05-20 stsp __dead static void
5036 9f7d7167 2018-04-29 stsp usage_blame(void)
5039 87411fa9 2022-06-16 stsp fprintf(stderr,
5040 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5041 9f7d7167 2018-04-29 stsp getprogname());
5045 84451b3e 2018-07-10 stsp struct tog_blame_line {
5046 84451b3e 2018-07-10 stsp int annotated;
5047 84451b3e 2018-07-10 stsp struct got_object_id *id;
5050 4ed7e80c 2018-05-20 stsp static const struct got_error *
5051 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5053 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5054 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5055 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5056 84451b3e 2018-07-10 stsp const struct got_error *err;
5057 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5058 826082fe 2020-12-10 stsp char *line = NULL;
5059 826082fe 2020-12-10 stsp size_t linesize = 0;
5060 826082fe 2020-12-10 stsp ssize_t linelen;
5061 84451b3e 2018-07-10 stsp wchar_t *wline;
5062 27a741e5 2019-09-11 stsp int width;
5063 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5064 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5065 ab089a2a 2018-07-12 stsp char *id_str;
5066 11b20872 2019-11-08 stsp struct tog_color *tc;
5068 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5070 ab089a2a 2018-07-12 stsp return err;
5072 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5073 f7d12f7e 2018-08-01 stsp werase(view->window);
5075 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5076 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5077 ab089a2a 2018-07-12 stsp free(id_str);
5078 ab089a2a 2018-07-12 stsp return err;
5081 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5082 ab089a2a 2018-07-12 stsp free(line);
5083 2550e4c3 2018-07-13 stsp line = NULL;
5085 1cae65b4 2019-09-22 stsp return err;
5086 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5087 a3404814 2018-09-02 stsp wstandout(view->window);
5088 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5090 11b20872 2019-11-08 stsp wattr_on(view->window,
5091 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5092 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5094 11b20872 2019-11-08 stsp wattr_off(view->window,
5095 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5096 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5097 a3404814 2018-09-02 stsp wstandend(view->window);
5098 2550e4c3 2018-07-13 stsp free(wline);
5099 2550e4c3 2018-07-13 stsp wline = NULL;
5100 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5101 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5103 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5104 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5105 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5106 ab089a2a 2018-07-12 stsp free(id_str);
5107 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5109 ab089a2a 2018-07-12 stsp free(id_str);
5110 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5111 3f60a8ef 2018-07-10 stsp free(line);
5112 2550e4c3 2018-07-13 stsp line = NULL;
5114 3f60a8ef 2018-07-10 stsp return err;
5115 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5116 2550e4c3 2018-07-13 stsp free(wline);
5117 2550e4c3 2018-07-13 stsp wline = NULL;
5118 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5119 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5121 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5122 145b6838 2022-06-16 stsp view->maxx = 0;
5123 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5124 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5125 826082fe 2020-12-10 stsp if (linelen == -1) {
5126 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5127 826082fe 2020-12-10 stsp s->eof = 1;
5130 84451b3e 2018-07-10 stsp free(line);
5131 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5133 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5136 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5137 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5138 1853e0f4 2022-06-16 stsp if (err) {
5139 1853e0f4 2022-06-16 stsp free(line);
5140 1853e0f4 2022-06-16 stsp return err;
5142 1853e0f4 2022-06-16 stsp free(wline);
5143 1853e0f4 2022-06-16 stsp wline = NULL;
5144 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5146 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5147 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5149 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5150 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5151 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5152 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5153 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5154 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5155 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5156 8d0fe45a 2019-08-12 stsp char *id_str;
5157 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5158 87411fa9 2022-06-16 stsp blame_line->id);
5159 8d0fe45a 2019-08-12 stsp if (err) {
5160 8d0fe45a 2019-08-12 stsp free(line);
5161 8d0fe45a 2019-08-12 stsp return err;
5163 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5165 11b20872 2019-11-08 stsp wattr_on(view->window,
5166 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5167 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5169 11b20872 2019-11-08 stsp wattr_off(view->window,
5170 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5171 8d0fe45a 2019-08-12 stsp free(id_str);
5172 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5174 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5175 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5178 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5179 ee41ec32 2018-07-10 stsp prev_id = NULL;
5182 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5183 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5184 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5186 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5187 41605754 2020-11-12 stsp width = 9;
5188 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5189 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5190 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5191 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5192 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5193 41605754 2020-11-12 stsp if (err) {
5194 41605754 2020-11-12 stsp free(line);
5195 41605754 2020-11-12 stsp return err;
5197 41605754 2020-11-12 stsp width += 9;
5200 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5201 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5202 4cb9d869 2022-06-16 stsp if (err) {
5203 4cb9d869 2022-06-16 stsp free(line);
5204 4cb9d869 2022-06-16 stsp return err;
5206 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5207 a75b3e2d 2022-06-16 stsp width += 9;
5208 41605754 2020-11-12 stsp free(wline);
5209 41605754 2020-11-12 stsp wline = NULL;
5212 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5213 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5214 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5215 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5217 826082fe 2020-12-10 stsp free(line);
5218 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5220 9b058f45 2022-06-30 mark view_border(view);
5222 84451b3e 2018-07-10 stsp return NULL;
5225 84451b3e 2018-07-10 stsp static const struct got_error *
5226 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5227 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5229 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5230 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5231 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5232 1a76625f 2018-10-22 stsp int errcode;
5234 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5235 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5236 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5238 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5239 1a76625f 2018-10-22 stsp if (errcode)
5240 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5242 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5243 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5244 d68a0a7d 2018-07-10 stsp goto done;
5247 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5248 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5250 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5251 d68a0a7d 2018-07-10 stsp if (line->annotated)
5252 d68a0a7d 2018-07-10 stsp goto done;
5254 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5255 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5256 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5257 84451b3e 2018-07-10 stsp goto done;
5259 84451b3e 2018-07-10 stsp line->annotated = 1;
5261 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5262 1a76625f 2018-10-22 stsp if (errcode)
5263 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5264 84451b3e 2018-07-10 stsp return err;
5267 84451b3e 2018-07-10 stsp static void *
5268 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5270 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5271 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5272 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5273 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5274 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5276 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5277 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5278 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5280 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5281 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5282 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5283 e6e73e55 2022-06-30 tracey goto done;
5286 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5287 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5288 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5289 e6e73e55 2022-06-30 tracey goto done;
5291 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5292 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5293 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5294 e6e73e55 2022-06-30 tracey goto done;
5297 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5299 e6e73e55 2022-06-30 tracey goto done;
5301 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5302 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5303 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5304 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5305 fc06ba56 2019-08-22 stsp err = NULL;
5307 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5308 e6e73e55 2022-06-30 tracey if (errcode) {
5309 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5310 e6e73e55 2022-06-30 tracey goto done;
5313 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5314 1d0f4054 2021-06-17 stsp if (err == NULL)
5315 1d0f4054 2021-06-17 stsp err = close_err;
5316 c9beca56 2018-07-22 stsp ta->repo = NULL;
5317 c9beca56 2018-07-22 stsp *ta->complete = 1;
5319 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5320 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5321 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5324 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5325 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5326 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5327 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5328 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5329 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5330 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5331 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5333 18430de3 2018-07-10 stsp return (void *)err;
5336 245d91c1 2018-07-12 stsp static struct got_object_id *
5337 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5338 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5340 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5342 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5343 8d0fe45a 2019-08-12 stsp return NULL;
5345 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5346 c0f61fa4 2022-07-11 mark if (!line->annotated)
5347 c0f61fa4 2022-07-11 mark return NULL;
5349 c0f61fa4 2022-07-11 mark return line->id;
5352 c0f61fa4 2022-07-11 mark static struct got_object_id *
5353 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5354 c0f61fa4 2022-07-11 mark int lineno)
5356 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5358 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5359 c0f61fa4 2022-07-11 mark return NULL;
5361 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5362 245d91c1 2018-07-12 stsp if (!line->annotated)
5363 245d91c1 2018-07-12 stsp return NULL;
5365 245d91c1 2018-07-12 stsp return line->id;
5368 b880a918 2018-07-10 stsp static const struct got_error *
5369 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5371 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5374 245d91c1 2018-07-12 stsp if (blame->thread) {
5375 1a76625f 2018-10-22 stsp int errcode;
5376 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5377 1a76625f 2018-10-22 stsp if (errcode)
5378 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5379 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5380 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5381 1a76625f 2018-10-22 stsp if (errcode)
5382 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5383 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5384 1a76625f 2018-10-22 stsp if (errcode)
5385 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5386 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5387 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5388 245d91c1 2018-07-12 stsp err = NULL;
5389 245d91c1 2018-07-12 stsp blame->thread = NULL;
5391 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5392 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5393 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5394 1d0f4054 2021-06-17 stsp if (err == NULL)
5395 1d0f4054 2021-06-17 stsp err = close_err;
5396 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5398 245d91c1 2018-07-12 stsp if (blame->f) {
5399 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5400 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5401 245d91c1 2018-07-12 stsp blame->f = NULL;
5403 57670559 2018-12-23 stsp if (blame->lines) {
5404 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5405 57670559 2018-12-23 stsp free(blame->lines[i].id);
5406 57670559 2018-12-23 stsp free(blame->lines);
5407 57670559 2018-12-23 stsp blame->lines = NULL;
5409 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5410 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5411 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5412 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5413 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5414 0ae84acc 2022-06-15 tracey if (err == NULL)
5415 0ae84acc 2022-06-15 tracey err = pack_err;
5416 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5418 245d91c1 2018-07-12 stsp return err;
5421 245d91c1 2018-07-12 stsp static const struct got_error *
5422 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5424 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5425 fc06ba56 2019-08-22 stsp int *done = arg;
5426 fc06ba56 2019-08-22 stsp int errcode;
5428 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5429 fc06ba56 2019-08-22 stsp if (errcode)
5430 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5431 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5433 fc06ba56 2019-08-22 stsp if (*done)
5434 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5436 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5437 fc06ba56 2019-08-22 stsp if (errcode)
5438 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5439 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5441 fc06ba56 2019-08-22 stsp return err;
5444 fc06ba56 2019-08-22 stsp static const struct got_error *
5445 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5447 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5448 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5449 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5450 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5451 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5452 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5453 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5454 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5455 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5457 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5458 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5460 15a94983 2018-12-23 stsp return err;
5462 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5463 eb81bc23 2022-06-28 tracey if (fd == -1) {
5464 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5465 eb81bc23 2022-06-28 tracey goto done;
5468 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5470 a44927cc 2022-04-07 stsp goto done;
5472 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5474 84451b3e 2018-07-10 stsp goto done;
5476 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5477 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5478 84451b3e 2018-07-10 stsp goto done;
5481 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5483 a70480e0 2018-06-23 stsp goto done;
5484 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5485 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5486 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5487 84451b3e 2018-07-10 stsp goto done;
5489 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5490 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5492 1fddf795 2021-01-20 stsp goto done;
5493 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5494 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5495 84451b3e 2018-07-10 stsp goto done;
5498 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5499 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5500 b02560ec 2019-08-19 stsp blame->nlines--;
5502 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5503 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5504 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5505 84451b3e 2018-07-10 stsp goto done;
5508 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5510 bd24772e 2018-07-11 stsp goto done;
5511 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5512 0ae84acc 2022-06-15 tracey pack_fds);
5513 0ae84acc 2022-06-15 tracey if (err)
5514 0ae84acc 2022-06-15 tracey goto done;
5516 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5517 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5518 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5519 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5520 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5521 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5522 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5523 245d91c1 2018-07-12 stsp goto done;
5525 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5527 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5528 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5529 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5530 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5531 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5532 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5533 a5388363 2020-12-01 naddy s->blame_complete = 0;
5535 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5536 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5537 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5538 f5a09613 2020-12-13 naddy s->selected_line = 1;
5540 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5543 a44927cc 2022-04-07 stsp if (commit)
5544 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5545 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5546 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5548 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5549 27d434c2 2018-09-15 stsp free(obj_id);
5551 245d91c1 2018-07-12 stsp stop_blame(blame);
5552 245d91c1 2018-07-12 stsp return err;
5555 245d91c1 2018-07-12 stsp static const struct got_error *
5556 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5557 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5559 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5560 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5562 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5564 c4843652 2019-08-12 stsp s->path = strdup(path);
5565 c4843652 2019-08-12 stsp if (s->path == NULL)
5566 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5568 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5569 c4843652 2019-08-12 stsp if (err) {
5570 c4843652 2019-08-12 stsp free(s->path);
5571 7cbe629d 2018-08-04 stsp return err;
5574 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5575 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5576 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5577 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5578 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5579 fb2756b9 2018-08-04 stsp s->repo = repo;
5580 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5581 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5583 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5584 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5585 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5586 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5588 11b20872 2019-11-08 stsp return err;
5591 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5592 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5593 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5594 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5595 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5596 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5598 a5388363 2020-12-01 naddy return run_blame(view);
5601 e5a0f69f 2018-08-18 stsp static const struct got_error *
5602 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5604 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5605 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5607 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5608 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5610 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5611 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5612 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5613 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5614 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5617 e5a0f69f 2018-08-18 stsp free(s->path);
5618 11b20872 2019-11-08 stsp free_colors(&s->colors);
5619 e5a0f69f 2018-08-18 stsp return err;
5622 7cbe629d 2018-08-04 stsp static const struct got_error *
5623 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5625 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5627 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5628 6c4c42e0 2019-06-24 stsp return NULL;
5631 6c4c42e0 2019-06-24 stsp static const struct got_error *
5632 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5634 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5635 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5636 6c4c42e0 2019-06-24 stsp int lineno;
5637 cb713507 2022-06-16 stsp char *line = NULL;
5638 826082fe 2020-12-10 stsp size_t linesize = 0;
5639 826082fe 2020-12-10 stsp ssize_t linelen;
5641 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5642 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5643 6c4c42e0 2019-06-24 stsp return NULL;
5646 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5647 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5648 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5650 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5652 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5654 6c4c42e0 2019-06-24 stsp while (1) {
5655 6c4c42e0 2019-06-24 stsp off_t offset;
5657 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5658 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5659 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5663 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5664 6c4c42e0 2019-06-24 stsp lineno = 1;
5666 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5669 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5670 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5671 6c4c42e0 2019-06-24 stsp free(line);
5672 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5674 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5675 cb713507 2022-06-16 stsp if (linelen != -1) {
5676 cb713507 2022-06-16 stsp char *exstr;
5677 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5680 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5681 cb713507 2022-06-16 stsp &view->regmatch)) {
5682 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5683 cb713507 2022-06-16 stsp s->matched_line = lineno;
5684 cb713507 2022-06-16 stsp free(exstr);
5687 cb713507 2022-06-16 stsp free(exstr);
5689 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5694 826082fe 2020-12-10 stsp free(line);
5696 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5697 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5698 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5701 145b6838 2022-06-16 stsp return err;
5704 6c4c42e0 2019-06-24 stsp static const struct got_error *
5705 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5707 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5708 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5709 2b380cc8 2018-10-24 stsp int errcode;
5711 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5712 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5713 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5714 2b380cc8 2018-10-24 stsp if (errcode)
5715 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5717 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5720 51fe7530 2019-08-19 stsp if (s->blame_complete)
5721 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5723 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5725 9b058f45 2022-06-30 mark view_border(view);
5726 e5a0f69f 2018-08-18 stsp return err;
5729 e5a0f69f 2018-08-18 stsp static const struct got_error *
5730 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5731 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
5733 05f04cdf 2022-07-20 mark struct tog_view *log_view;
5734 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
5736 05f04cdf 2022-07-20 mark *new_view = NULL;
5738 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5739 05f04cdf 2022-07-20 mark if (log_view == NULL)
5740 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
5742 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5744 05f04cdf 2022-07-20 mark view_close(log_view);
5746 05f04cdf 2022-07-20 mark *new_view = log_view;
5748 05f04cdf 2022-07-20 mark return err;
5751 05f04cdf 2022-07-20 mark static const struct got_error *
5752 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5754 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5755 05f04cdf 2022-07-20 mark struct tog_view *diff_view, *log_view;
5756 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5757 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5759 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5760 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5761 9b058f45 2022-06-30 mark --eos; /* border */
5763 e5a0f69f 2018-08-18 stsp switch (ch) {
5765 145b6838 2022-06-16 stsp view->x = 0;
5768 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5769 640cd7ff 2022-06-22 mark view->count = 0;
5771 145b6838 2022-06-16 stsp case KEY_RIGHT:
5773 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5774 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5776 640cd7ff 2022-06-22 mark view->count = 0;
5778 145b6838 2022-06-16 stsp case KEY_LEFT:
5780 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5781 640cd7ff 2022-06-22 mark if (view->x <= 0)
5782 640cd7ff 2022-06-22 mark view->count = 0;
5785 1e37a5c2 2019-05-12 jcs s->done = 1;
5787 4deef56f 2021-09-02 naddy case 'g':
5788 4deef56f 2021-09-02 naddy case KEY_HOME:
5789 4deef56f 2021-09-02 naddy s->selected_line = 1;
5790 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5791 640cd7ff 2022-06-22 mark view->count = 0;
5793 4deef56f 2021-09-02 naddy case 'G':
5794 4deef56f 2021-09-02 naddy case KEY_END:
5795 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5796 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5797 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5799 9b058f45 2022-06-30 mark s->selected_line = eos;
5800 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5802 640cd7ff 2022-06-22 mark view->count = 0;
5805 1e37a5c2 2019-05-12 jcs case KEY_UP:
5806 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5807 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5808 1e37a5c2 2019-05-12 jcs s->selected_line--;
5809 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5810 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5811 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5813 640cd7ff 2022-06-22 mark view->count = 0;
5815 83cc4199 2022-06-13 stsp case CTRL('u'):
5817 83cc4199 2022-06-13 stsp nscroll /= 2;
5818 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5819 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5820 ea025d1d 2020-02-22 naddy case CTRL('b'):
5822 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5823 640cd7ff 2022-06-22 mark if (view->count > 1)
5824 640cd7ff 2022-06-22 mark nscroll += nscroll;
5825 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5826 640cd7ff 2022-06-22 mark view->count = 0;
5829 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5830 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5832 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5835 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5836 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5837 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5838 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5839 1e37a5c2 2019-05-12 jcs s->selected_line++;
5840 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5841 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5843 640cd7ff 2022-06-22 mark view->count = 0;
5846 1e37a5c2 2019-05-12 jcs case 'p': {
5847 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5849 640cd7ff 2022-06-22 mark view->count = 0;
5850 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5851 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5852 1e37a5c2 2019-05-12 jcs if (id == NULL)
5854 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5855 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5856 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5857 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5858 1e37a5c2 2019-05-12 jcs int obj_type;
5859 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5860 1e37a5c2 2019-05-12 jcs s->repo, id);
5863 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5864 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5865 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5866 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5869 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5870 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5871 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5874 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5875 a44927cc 2022-04-07 stsp pcommit, s->path);
5876 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5877 e5a0f69f 2018-08-18 stsp if (err) {
5878 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5879 1e37a5c2 2019-05-12 jcs err = NULL;
5880 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5883 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5885 1e37a5c2 2019-05-12 jcs free(blob_id);
5886 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5887 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5888 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5891 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5892 d7b5a0e8 2022-04-20 stsp &pid->id);
5893 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5895 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5896 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5898 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5903 1e37a5c2 2019-05-12 jcs s->done = 1;
5904 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5905 1e37a5c2 2019-05-12 jcs s->done = 0;
5906 1e37a5c2 2019-05-12 jcs if (thread_err)
5908 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5909 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5910 a5388363 2020-12-01 naddy err = run_blame(view);
5915 61417565 2022-06-20 mark case 'C': {
5916 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5918 640cd7ff 2022-06-22 mark view->count = 0;
5919 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5920 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5922 1e37a5c2 2019-05-12 jcs s->done = 1;
5923 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5924 1e37a5c2 2019-05-12 jcs s->done = 0;
5925 1e37a5c2 2019-05-12 jcs if (thread_err)
5927 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5928 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5929 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5930 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5931 a5388363 2020-12-01 naddy err = run_blame(view);
5936 05f04cdf 2022-07-20 mark case 'L': {
5937 05f04cdf 2022-07-20 mark struct got_object_id *id = NULL;
5939 05f04cdf 2022-07-20 mark view->count = 0;
5940 05f04cdf 2022-07-20 mark id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5941 05f04cdf 2022-07-20 mark s->first_displayed_line, s->selected_line);
5942 05f04cdf 2022-07-20 mark if (id == NULL)
5945 05f04cdf 2022-07-20 mark if (view_is_parent_view(view))
5946 05f04cdf 2022-07-20 mark view_get_split(view, &begin_y, &begin_x);
5947 05f04cdf 2022-07-20 mark err = log_annotated_line(&log_view, begin_y, begin_x,
5948 05f04cdf 2022-07-20 mark s->repo, id);
5951 05f04cdf 2022-07-20 mark if (view_is_parent_view(view) &&
5952 05f04cdf 2022-07-20 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5953 05f04cdf 2022-07-20 mark err = view_init_hsplit(view, begin_y);
5958 05f04cdf 2022-07-20 mark view->focussed = 0;
5959 05f04cdf 2022-07-20 mark log_view->focussed = 1;
5960 05f04cdf 2022-07-20 mark log_view->mode = view->mode;
5961 05f04cdf 2022-07-20 mark log_view->nlines = view->lines - begin_y;
5962 05f04cdf 2022-07-20 mark if (view_is_parent_view(view)) {
5963 05f04cdf 2022-07-20 mark view_transfer_size(log_view, view);
5964 05f04cdf 2022-07-20 mark err = view_close_child(view);
5966 05f04cdf 2022-07-20 mark return err;
5967 05f04cdf 2022-07-20 mark err = view_set_child(view, log_view);
5969 05f04cdf 2022-07-20 mark return err;
5970 05f04cdf 2022-07-20 mark view->focus_child = 1;
5972 05f04cdf 2022-07-20 mark *new_view = log_view;
5975 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5976 1e37a5c2 2019-05-12 jcs case '\r': {
5977 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5978 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5979 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5981 640cd7ff 2022-06-22 mark view->count = 0;
5982 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5983 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5984 1e37a5c2 2019-05-12 jcs if (id == NULL)
5986 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5989 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5990 c0f61fa4 2022-07-11 mark if (*new_view) {
5991 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
5992 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
5995 c0f61fa4 2022-07-11 mark diff_view = *new_view;
5997 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
5998 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6000 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6001 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6002 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6003 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6004 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6008 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6009 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6010 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6012 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6015 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6016 c0f61fa4 2022-07-11 mark s->selected_line;
6017 c0f61fa4 2022-07-11 mark if (*new_view)
6018 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6019 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6020 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6021 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6026 e78dc838 2020-12-04 stsp view->focussed = 0;
6027 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6028 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6029 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6030 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6031 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6032 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6035 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6038 e78dc838 2020-12-04 stsp view->focus_child = 1;
6040 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6045 83cc4199 2022-06-13 stsp case CTRL('d'):
6047 83cc4199 2022-06-13 stsp nscroll /= 2;
6048 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6049 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6050 ea025d1d 2020-02-22 naddy case CTRL('f'):
6053 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6054 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6055 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6056 640cd7ff 2022-06-22 mark view->count = 0;
6059 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6060 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6061 83cc4199 2022-06-13 stsp s->selected_line +=
6062 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6063 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6065 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6066 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6068 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6069 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6071 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6072 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6073 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6074 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6078 640cd7ff 2022-06-22 mark view->count = 0;
6081 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6084 917d79a7 2022-07-01 stsp static const struct got_error *
6085 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6087 917d79a7 2022-07-01 stsp const struct got_error *err;
6088 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6090 917d79a7 2022-07-01 stsp view->count = 0;
6091 917d79a7 2022-07-01 stsp s->done = 1;
6092 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6093 917d79a7 2022-07-01 stsp s->done = 0;
6095 917d79a7 2022-07-01 stsp return err;
6096 917d79a7 2022-07-01 stsp return run_blame(view);
6099 a70480e0 2018-06-23 stsp static const struct got_error *
6100 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6102 a70480e0 2018-06-23 stsp const struct got_error *error;
6103 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6104 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6105 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6106 0587e10c 2020-07-23 stsp char *link_target = NULL;
6107 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6108 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6109 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6111 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6112 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6114 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6115 a70480e0 2018-06-23 stsp switch (ch) {
6117 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6120 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6121 69069811 2018-08-02 stsp if (repo_path == NULL)
6122 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6126 17020d27 2019-03-07 stsp usage_blame();
6127 a70480e0 2018-06-23 stsp /* NOTREACHED */
6131 a70480e0 2018-06-23 stsp argc -= optind;
6132 a70480e0 2018-06-23 stsp argv += optind;
6134 f135c941 2020-02-20 stsp if (argc != 1)
6135 a70480e0 2018-06-23 stsp usage_blame();
6137 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6138 0ae84acc 2022-06-15 tracey if (error != NULL)
6139 0ae84acc 2022-06-15 tracey goto done;
6141 69069811 2018-08-02 stsp if (repo_path == NULL) {
6142 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6143 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6144 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6145 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6146 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6147 c156c7a4 2020-12-18 stsp goto done;
6148 f135c941 2020-02-20 stsp if (worktree)
6149 eb41ed75 2019-02-05 stsp repo_path =
6150 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6152 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6153 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6154 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6155 c156c7a4 2020-12-18 stsp goto done;
6159 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6160 c02c541e 2019-03-29 stsp if (error != NULL)
6161 8e94dd5b 2019-01-04 stsp goto done;
6163 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6164 f135c941 2020-02-20 stsp worktree);
6165 c02c541e 2019-03-29 stsp if (error)
6166 92205607 2019-01-04 stsp goto done;
6168 f135c941 2020-02-20 stsp init_curses();
6170 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6171 51a10b52 2020-12-26 stsp if (error)
6172 51a10b52 2020-12-26 stsp goto done;
6174 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6175 eb41ed75 2019-02-05 stsp if (error)
6176 69069811 2018-08-02 stsp goto done;
6178 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6179 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6180 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6181 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6182 a70480e0 2018-06-23 stsp if (error != NULL)
6183 66b4983c 2018-06-23 stsp goto done;
6184 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6185 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6187 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6188 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6190 a19e88aa 2018-06-23 stsp if (error != NULL)
6191 8b473291 2019-02-21 stsp goto done;
6193 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6194 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6195 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6196 e1cd8fed 2018-08-01 stsp goto done;
6199 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6200 a44927cc 2022-04-07 stsp if (error)
6201 a44927cc 2022-04-07 stsp goto done;
6203 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6204 a44927cc 2022-04-07 stsp commit, repo);
6205 7cbe629d 2018-08-04 stsp if (error)
6206 7cbe629d 2018-08-04 stsp goto done;
6208 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6209 78756c87 2020-11-24 stsp commit_id, repo);
6210 0587e10c 2020-07-23 stsp if (error)
6211 0587e10c 2020-07-23 stsp goto done;
6212 12314ad4 2019-08-31 stsp if (worktree) {
6213 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6214 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6215 12314ad4 2019-08-31 stsp worktree = NULL;
6217 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6219 69069811 2018-08-02 stsp free(repo_path);
6220 f135c941 2020-02-20 stsp free(in_repo_path);
6221 0587e10c 2020-07-23 stsp free(link_target);
6222 69069811 2018-08-02 stsp free(cwd);
6223 a70480e0 2018-06-23 stsp free(commit_id);
6224 a44927cc 2022-04-07 stsp if (commit)
6225 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6226 eb41ed75 2019-02-05 stsp if (worktree)
6227 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6228 1d0f4054 2021-06-17 stsp if (repo) {
6229 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6230 1d0f4054 2021-06-17 stsp if (error == NULL)
6231 1d0f4054 2021-06-17 stsp error = close_err;
6233 0ae84acc 2022-06-15 tracey if (pack_fds) {
6234 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6235 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6236 0ae84acc 2022-06-15 tracey if (error == NULL)
6237 0ae84acc 2022-06-15 tracey error = pack_err;
6239 51a10b52 2020-12-26 stsp tog_free_refs();
6240 a70480e0 2018-06-23 stsp return error;
6243 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6244 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6246 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6247 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6248 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6249 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6250 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6251 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6252 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6254 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6255 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6256 9b058f45 2022-06-30 mark --limit; /* border */
6258 f7d12f7e 2018-08-01 stsp werase(view->window);
6260 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6261 ffd1d5e5 2018-06-23 stsp return NULL;
6263 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6266 ffd1d5e5 2018-06-23 stsp return err;
6267 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6268 a3404814 2018-09-02 stsp wstandout(view->window);
6269 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6271 11b20872 2019-11-08 stsp wattr_on(view->window,
6272 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6273 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6275 11b20872 2019-11-08 stsp wattr_off(view->window,
6276 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6277 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6278 a3404814 2018-09-02 stsp wstandend(view->window);
6279 2550e4c3 2018-07-13 stsp free(wline);
6280 2550e4c3 2018-07-13 stsp wline = NULL;
6281 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6282 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6283 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6284 ffd1d5e5 2018-06-23 stsp return NULL;
6285 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6288 ce52c690 2018-06-23 stsp return err;
6289 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6290 2550e4c3 2018-07-13 stsp free(wline);
6291 2550e4c3 2018-07-13 stsp wline = NULL;
6292 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6293 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6294 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6295 ffd1d5e5 2018-06-23 stsp return NULL;
6296 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6297 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6298 a1eca9bb 2018-06-23 stsp return NULL;
6300 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6301 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6302 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6303 0cf4efb1 2018-09-29 stsp if (view->focussed)
6304 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6305 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6307 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6308 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6309 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6310 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6311 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6312 ffd1d5e5 2018-06-23 stsp return NULL;
6316 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6319 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6320 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6321 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6322 848d6979 2019-08-12 stsp const char *modestr = "";
6323 56e0773d 2019-11-28 stsp mode_t mode;
6325 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6326 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6328 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6329 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6330 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6332 638f9024 2019-05-13 stsp return got_error_from_errno(
6333 230a42bd 2019-05-11 jcs "got_object_id_str");
6335 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6336 63c5ca5d 2019-08-24 stsp modestr = "$";
6337 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6340 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6341 d86d3b18 2020-12-01 naddy te, s->repo);
6342 0d6c6ee3 2020-05-20 stsp if (err) {
6343 0d6c6ee3 2020-05-20 stsp free(id_str);
6344 0d6c6ee3 2020-05-20 stsp return err;
6346 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6347 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6348 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6350 848d6979 2019-08-12 stsp modestr = "@";
6352 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6353 848d6979 2019-08-12 stsp modestr = "/";
6354 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6355 848d6979 2019-08-12 stsp modestr = "*";
6356 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6357 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6358 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6359 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6360 1d13200f 2018-07-12 stsp free(id_str);
6361 0d6c6ee3 2020-05-20 stsp free(link_target);
6362 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6364 1d13200f 2018-07-12 stsp free(id_str);
6365 0d6c6ee3 2020-05-20 stsp free(link_target);
6366 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6368 ffd1d5e5 2018-06-23 stsp if (err) {
6369 ffd1d5e5 2018-06-23 stsp free(line);
6372 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6373 0cf4efb1 2018-09-29 stsp if (view->focussed)
6374 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6375 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6377 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6379 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6380 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6381 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6383 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6384 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6385 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6386 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6387 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6388 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6389 ffd1d5e5 2018-06-23 stsp free(line);
6390 2550e4c3 2018-07-13 stsp free(wline);
6391 2550e4c3 2018-07-13 stsp wline = NULL;
6393 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6394 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6395 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6399 ffd1d5e5 2018-06-23 stsp return err;
6402 ffd1d5e5 2018-06-23 stsp static void
6403 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6405 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6406 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6407 fa86c4bf 2020-11-29 stsp int i = 0;
6409 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6412 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6413 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6414 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6415 fa86c4bf 2020-11-29 stsp if (!isroot)
6416 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6419 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6420 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6424 9b058f45 2022-06-30 mark static const struct got_error *
6425 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6427 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6428 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6429 ffd1d5e5 2018-06-23 stsp int n = 0;
6431 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6432 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6433 694d3271 2020-12-01 naddy s->first_displayed_entry);
6435 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6437 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6438 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6440 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6441 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6442 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6443 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6447 9b058f45 2022-06-30 mark return NULL;
6450 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6451 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6452 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6454 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6455 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6456 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6458 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6459 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6460 56e0773d 2019-11-28 stsp + 1 /* slash */;
6462 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6464 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6465 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6466 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6468 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6469 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6470 d9765a41 2018-06-23 stsp while (pt) {
6471 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6472 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6473 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6474 cb2ebc8a 2018-06-23 stsp goto done;
6476 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6477 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6478 cb2ebc8a 2018-06-23 stsp goto done;
6480 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6483 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6484 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6485 ce52c690 2018-06-23 stsp goto done;
6489 ce52c690 2018-06-23 stsp if (err) {
6490 ce52c690 2018-06-23 stsp free(*path);
6491 ce52c690 2018-06-23 stsp *path = NULL;
6493 ce52c690 2018-06-23 stsp return err;
6496 ce52c690 2018-06-23 stsp static const struct got_error *
6497 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6498 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6499 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6501 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6502 ce52c690 2018-06-23 stsp char *path;
6503 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6505 a0de39f3 2019-08-09 stsp *new_view = NULL;
6507 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6509 ce52c690 2018-06-23 stsp return err;
6511 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6512 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6513 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6514 83ce39e3 2019-08-12 stsp goto done;
6517 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6518 e5a0f69f 2018-08-18 stsp if (err) {
6519 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6520 fc06ba56 2019-08-22 stsp err = NULL;
6521 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6523 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6525 83ce39e3 2019-08-12 stsp free(path);
6526 69efd4c4 2018-07-18 stsp return err;
6529 69efd4c4 2018-07-18 stsp static const struct got_error *
6530 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6531 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6533 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6534 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6535 69efd4c4 2018-07-18 stsp char *path;
6537 a0de39f3 2019-08-09 stsp *new_view = NULL;
6539 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6540 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6541 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6543 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6545 69efd4c4 2018-07-18 stsp return err;
6547 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6550 e5a0f69f 2018-08-18 stsp view_close(log_view);
6552 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6553 cb2ebc8a 2018-06-23 stsp free(path);
6554 cb2ebc8a 2018-06-23 stsp return err;
6557 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6558 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6559 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6561 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6562 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6563 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6564 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6566 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6567 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6569 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6570 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6571 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6573 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6575 bc573f3b 2021-07-10 stsp goto done;
6578 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6579 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6580 bc573f3b 2021-07-10 stsp * closed on demand.
6582 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6583 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6585 bc573f3b 2021-07-10 stsp goto done;
6586 bc573f3b 2021-07-10 stsp s->tree = s->root;
6588 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6589 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6590 ffd1d5e5 2018-06-23 stsp goto done;
6592 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6593 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6594 ffd1d5e5 2018-06-23 stsp goto done;
6597 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6598 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6599 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6600 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6601 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6602 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6603 9cd7cbd1 2020-12-07 stsp goto done;
6606 fb2756b9 2018-08-04 stsp s->repo = repo;
6608 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6609 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6610 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6611 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6613 c0b01bdb 2019-11-08 stsp goto done;
6614 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6615 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6617 c0b01bdb 2019-11-08 stsp goto done;
6618 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6619 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6620 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6622 c0b01bdb 2019-11-08 stsp goto done;
6624 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6625 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6626 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6628 11b20872 2019-11-08 stsp goto done;
6630 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6631 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6633 c0b01bdb 2019-11-08 stsp goto done;
6636 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6637 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6638 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6639 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6640 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6642 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6643 bc573f3b 2021-07-10 stsp if (commit)
6644 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6646 bc573f3b 2021-07-10 stsp close_tree_view(view);
6647 ad80ab7b 2018-08-04 stsp return err;
6650 e5a0f69f 2018-08-18 stsp static const struct got_error *
6651 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6653 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6655 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6656 fb2756b9 2018-08-04 stsp free(s->tree_label);
6657 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6658 6484ec90 2018-09-29 stsp free(s->commit_id);
6659 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6660 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6661 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6662 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6663 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6664 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6665 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6666 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6667 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6668 ad80ab7b 2018-08-04 stsp free(parent);
6671 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6672 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6673 bc573f3b 2021-07-10 stsp if (s->root)
6674 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6675 7c32bd05 2019-06-22 stsp return NULL;
6678 7c32bd05 2019-06-22 stsp static const struct got_error *
6679 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6681 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6683 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6684 7c32bd05 2019-06-22 stsp return NULL;
6687 7c32bd05 2019-06-22 stsp static int
6688 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6690 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6692 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6696 7c32bd05 2019-06-22 stsp static const struct got_error *
6697 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6699 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6700 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6702 7c32bd05 2019-06-22 stsp if (!view->searching) {
6703 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6704 7c32bd05 2019-06-22 stsp return NULL;
6707 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6708 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6709 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6710 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6711 56e0773d 2019-11-28 stsp s->selected_entry);
6713 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6715 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6716 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6718 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6719 56e0773d 2019-11-28 stsp s->selected_entry);
6722 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6723 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6724 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6725 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6727 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6730 7c32bd05 2019-06-22 stsp while (1) {
6731 56e0773d 2019-11-28 stsp if (te == NULL) {
6732 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6733 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6734 ac66afb8 2019-06-24 stsp return NULL;
6736 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6737 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6739 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6742 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6743 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6744 56e0773d 2019-11-28 stsp s->matched_entry = te;
6748 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6749 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6751 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6754 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6755 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6756 7c32bd05 2019-06-22 stsp s->selected = 0;
6759 e5a0f69f 2018-08-18 stsp return NULL;
6762 ad80ab7b 2018-08-04 stsp static const struct got_error *
6763 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6765 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6766 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6767 e5a0f69f 2018-08-18 stsp char *parent_path;
6769 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6771 e5a0f69f 2018-08-18 stsp return err;
6773 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6774 e5a0f69f 2018-08-18 stsp free(parent_path);
6776 9b058f45 2022-06-30 mark view_border(view);
6777 e5a0f69f 2018-08-18 stsp return err;
6780 e5a0f69f 2018-08-18 stsp static const struct got_error *
6781 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6783 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6784 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6785 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6786 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6787 49b24ee5 2022-07-03 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6789 e5a0f69f 2018-08-18 stsp switch (ch) {
6791 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6792 640cd7ff 2022-06-22 mark view->count = 0;
6795 640cd7ff 2022-06-22 mark view->count = 0;
6796 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6798 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6799 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6800 49b24ee5 2022-07-03 mark err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6801 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6802 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6803 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6807 e78dc838 2020-12-04 stsp view->focussed = 0;
6808 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6809 49b24ee5 2022-07-03 mark log_view->mode = view->mode;
6810 49b24ee5 2022-07-03 mark log_view->nlines = view->lines - begin_y;
6811 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6812 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
6813 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6815 1e37a5c2 2019-05-12 jcs return err;
6816 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6819 e78dc838 2020-12-04 stsp view->focus_child = 1;
6821 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6824 640cd7ff 2022-06-22 mark view->count = 0;
6825 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6826 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6827 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6828 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6829 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6830 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6831 152c1c93 2020-11-29 stsp if (err) {
6832 152c1c93 2020-11-29 stsp view_close(ref_view);
6833 152c1c93 2020-11-29 stsp return err;
6835 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6836 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6837 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6841 e78dc838 2020-12-04 stsp view->focussed = 0;
6842 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6843 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
6844 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
6845 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6846 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
6847 152c1c93 2020-11-29 stsp err = view_close_child(view);
6849 152c1c93 2020-11-29 stsp return err;
6850 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6853 e78dc838 2020-12-04 stsp view->focus_child = 1;
6855 152c1c93 2020-11-29 stsp *new_view = ref_view;
6857 e4526bf5 2021-09-03 naddy case 'g':
6858 e4526bf5 2021-09-03 naddy case KEY_HOME:
6859 e4526bf5 2021-09-03 naddy s->selected = 0;
6860 640cd7ff 2022-06-22 mark view->count = 0;
6861 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6862 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6863 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6865 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6867 e4526bf5 2021-09-03 naddy case 'G':
6868 9b058f45 2022-06-30 mark case KEY_END: {
6869 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6871 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6872 9b058f45 2022-06-30 mark --eos; /* border */
6873 e4526bf5 2021-09-03 naddy s->selected = 0;
6874 640cd7ff 2022-06-22 mark view->count = 0;
6875 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6876 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6877 e4526bf5 2021-09-03 naddy if (te == NULL) {
6878 ad055527 2022-07-16 mark if (s->tree != s->root) {
6879 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6884 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6885 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6887 e4526bf5 2021-09-03 naddy if (n > 0)
6888 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6892 1e37a5c2 2019-05-12 jcs case KEY_UP:
6893 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6894 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6895 1e37a5c2 2019-05-12 jcs s->selected--;
6898 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6899 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6900 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6901 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6902 640cd7ff 2022-06-22 mark view->count = 0;
6904 83cc4199 2022-06-13 stsp case CTRL('u'):
6906 83cc4199 2022-06-13 stsp nscroll /= 2;
6907 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6908 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6909 ea025d1d 2020-02-22 naddy case CTRL('b'):
6911 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6912 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6913 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6914 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6916 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6917 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6919 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6920 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6921 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6922 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6923 640cd7ff 2022-06-22 mark view->count = 0;
6926 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6927 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6928 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6929 1e37a5c2 2019-05-12 jcs s->selected++;
6932 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6933 640cd7ff 2022-06-22 mark == NULL) {
6934 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6935 640cd7ff 2022-06-22 mark view->count = 0;
6938 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6940 83cc4199 2022-06-13 stsp case CTRL('d'):
6942 83cc4199 2022-06-13 stsp nscroll /= 2;
6943 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6944 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6945 ea025d1d 2020-02-22 naddy case CTRL('f'):
6947 48bb96f0 2022-06-20 naddy case ' ':
6948 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6950 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6951 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6952 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6953 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6955 640cd7ff 2022-06-22 mark view->count = 0;
6958 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6960 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6962 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6963 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6964 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6965 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6966 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6967 640cd7ff 2022-06-22 mark view->count = 0;
6970 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6971 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6973 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6974 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6975 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6976 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6977 1e37a5c2 2019-05-12 jcs s->selected_entry =
6978 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6979 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6980 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6981 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6985 1e37a5c2 2019-05-12 jcs free(parent);
6986 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6987 56e0773d 2019-11-28 stsp s->selected_entry))) {
6988 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6989 640cd7ff 2022-06-22 mark view->count = 0;
6990 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6991 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6994 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6995 941e9f74 2019-05-21 stsp if (err) {
6996 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6999 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7000 56e0773d 2019-11-28 stsp s->selected_entry))) {
7001 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7002 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
7004 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
7005 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7007 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
7008 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7009 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7013 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7014 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7015 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7020 640cd7ff 2022-06-22 mark view->count = 0;
7021 e78dc838 2020-12-04 stsp view->focussed = 0;
7022 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7023 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
7024 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
7025 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7026 3c1dfe12 2022-07-08 mark view_transfer_size(blame_view, view);
7027 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7029 669b5ffa 2018-10-07 stsp return err;
7030 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
7033 e78dc838 2020-12-04 stsp view->focus_child = 1;
7035 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7038 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7039 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7040 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7041 640cd7ff 2022-06-22 mark view->count = 0;
7044 640cd7ff 2022-06-22 mark view->count = 0;
7048 ffd1d5e5 2018-06-23 stsp return err;
7051 ffd1d5e5 2018-06-23 stsp __dead static void
7052 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7055 87411fa9 2022-06-16 stsp fprintf(stderr,
7056 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7057 ffd1d5e5 2018-06-23 stsp getprogname());
7061 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7062 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7064 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7065 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7066 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7067 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7068 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7069 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7070 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7071 4e97c21c 2020-12-06 stsp char *label = NULL;
7072 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7073 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7075 5221c383 2018-08-01 stsp struct tog_view *view;
7076 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7078 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7079 ffd1d5e5 2018-06-23 stsp switch (ch) {
7081 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7084 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7085 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7086 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7090 e99e2d15 2020-11-24 naddy usage_tree();
7091 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7095 ffd1d5e5 2018-06-23 stsp argc -= optind;
7096 ffd1d5e5 2018-06-23 stsp argv += optind;
7098 55cccc34 2020-02-20 stsp if (argc > 1)
7099 e99e2d15 2020-11-24 naddy usage_tree();
7101 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7102 0ae84acc 2022-06-15 tracey if (error != NULL)
7103 0ae84acc 2022-06-15 tracey goto done;
7105 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7106 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7107 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7108 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7109 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7110 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7111 c156c7a4 2020-12-18 stsp goto done;
7112 55cccc34 2020-02-20 stsp if (worktree)
7113 52185f70 2019-02-05 stsp repo_path =
7114 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7116 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7117 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7118 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7119 c156c7a4 2020-12-18 stsp goto done;
7123 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7124 c02c541e 2019-03-29 stsp if (error != NULL)
7125 52185f70 2019-02-05 stsp goto done;
7127 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7128 55cccc34 2020-02-20 stsp repo, worktree);
7129 55cccc34 2020-02-20 stsp if (error)
7130 55cccc34 2020-02-20 stsp goto done;
7132 55cccc34 2020-02-20 stsp init_curses();
7134 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7135 c02c541e 2019-03-29 stsp if (error)
7136 52185f70 2019-02-05 stsp goto done;
7138 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7139 51a10b52 2020-12-26 stsp if (error)
7140 51a10b52 2020-12-26 stsp goto done;
7142 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7143 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7144 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7145 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7146 4e97c21c 2020-12-06 stsp if (error)
7147 4e97c21c 2020-12-06 stsp goto done;
7148 4e97c21c 2020-12-06 stsp head_ref_name = label;
7150 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7151 4e97c21c 2020-12-06 stsp if (error == NULL)
7152 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7153 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7154 4e97c21c 2020-12-06 stsp goto done;
7155 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7156 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7157 4e97c21c 2020-12-06 stsp if (error)
7158 4e97c21c 2020-12-06 stsp goto done;
7161 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7162 a44927cc 2022-04-07 stsp if (error)
7163 a44927cc 2022-04-07 stsp goto done;
7165 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7166 5221c383 2018-08-01 stsp if (view == NULL) {
7167 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7168 5221c383 2018-08-01 stsp goto done;
7170 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7171 ad80ab7b 2018-08-04 stsp if (error)
7172 ad80ab7b 2018-08-04 stsp goto done;
7173 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7174 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7175 d91faf3b 2020-12-01 naddy in_repo_path);
7176 55cccc34 2020-02-20 stsp if (error)
7177 55cccc34 2020-02-20 stsp goto done;
7180 55cccc34 2020-02-20 stsp if (worktree) {
7181 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7182 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7183 55cccc34 2020-02-20 stsp worktree = NULL;
7185 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7187 52185f70 2019-02-05 stsp free(repo_path);
7188 e4a0e26d 2020-02-20 stsp free(cwd);
7189 ffd1d5e5 2018-06-23 stsp free(commit_id);
7190 4e97c21c 2020-12-06 stsp free(label);
7192 486cd271 2020-12-06 stsp got_ref_close(ref);
7193 1d0f4054 2021-06-17 stsp if (repo) {
7194 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7195 1d0f4054 2021-06-17 stsp if (error == NULL)
7196 1d0f4054 2021-06-17 stsp error = close_err;
7198 0ae84acc 2022-06-15 tracey if (pack_fds) {
7199 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7200 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7201 0ae84acc 2022-06-15 tracey if (error == NULL)
7202 0ae84acc 2022-06-15 tracey error = pack_err;
7204 51a10b52 2020-12-26 stsp tog_free_refs();
7205 ffd1d5e5 2018-06-23 stsp return error;
7208 6458efa5 2020-11-24 stsp static const struct got_error *
7209 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7211 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7212 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7214 6458efa5 2020-11-24 stsp s->nrefs = 0;
7215 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7216 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7217 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7218 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7219 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7222 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7223 6458efa5 2020-11-24 stsp if (re == NULL)
7224 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7226 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7227 8924d611 2020-12-26 stsp if (re->ref == NULL)
7228 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7229 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7230 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7233 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7234 6458efa5 2020-11-24 stsp return NULL;
7238 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7240 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7242 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7243 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7244 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7245 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7250 6458efa5 2020-11-24 stsp static const struct got_error *
7251 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7253 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7254 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7256 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7257 6458efa5 2020-11-24 stsp s->repo = repo;
7259 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7260 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7262 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7264 6458efa5 2020-11-24 stsp return err;
7266 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7267 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7268 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7269 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7271 6458efa5 2020-11-24 stsp goto done;
7273 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7274 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7275 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7277 6458efa5 2020-11-24 stsp goto done;
7279 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7280 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7281 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7283 cc488aa7 2022-01-23 stsp goto done;
7285 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7286 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7287 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7289 6458efa5 2020-11-24 stsp goto done;
7292 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7293 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7294 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7295 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7296 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7299 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7300 6458efa5 2020-11-24 stsp return err;
7303 6458efa5 2020-11-24 stsp static const struct got_error *
7304 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7306 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7308 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7309 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7311 6458efa5 2020-11-24 stsp return NULL;
7314 6458efa5 2020-11-24 stsp static const struct got_error *
7315 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7316 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7318 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7319 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7320 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7321 6458efa5 2020-11-24 stsp int obj_type;
7323 c42c9805 2020-11-24 stsp *commit_id = NULL;
7325 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7327 6458efa5 2020-11-24 stsp return err;
7329 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7331 6458efa5 2020-11-24 stsp goto done;
7333 6458efa5 2020-11-24 stsp switch (obj_type) {
7334 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7335 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7337 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7338 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7340 6458efa5 2020-11-24 stsp goto done;
7341 c42c9805 2020-11-24 stsp free(obj_id);
7342 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7343 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7345 6458efa5 2020-11-24 stsp goto done;
7346 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7347 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7348 c42c9805 2020-11-24 stsp goto done;
7350 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7351 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7352 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7353 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7354 c42c9805 2020-11-24 stsp goto done;
7358 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7364 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7365 c42c9805 2020-11-24 stsp if (err) {
7366 c42c9805 2020-11-24 stsp free(*commit_id);
7367 c42c9805 2020-11-24 stsp *commit_id = NULL;
7369 c42c9805 2020-11-24 stsp return err;
7372 c42c9805 2020-11-24 stsp static const struct got_error *
7373 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7374 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7376 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7377 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7378 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7380 c42c9805 2020-11-24 stsp *new_view = NULL;
7382 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7383 c42c9805 2020-11-24 stsp if (err) {
7384 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7385 c42c9805 2020-11-24 stsp return err;
7387 c42c9805 2020-11-24 stsp return NULL;
7390 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7391 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7392 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7393 6458efa5 2020-11-24 stsp goto done;
7396 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7397 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7400 6458efa5 2020-11-24 stsp view_close(log_view);
7402 6458efa5 2020-11-24 stsp *new_view = log_view;
7403 c42c9805 2020-11-24 stsp free(commit_id);
7404 6458efa5 2020-11-24 stsp return err;
7407 ce5b7c56 2019-07-09 stsp static void
7408 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7410 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7411 34ba6917 2020-11-30 stsp int i = 0;
7413 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7416 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7417 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7418 34ba6917 2020-11-30 stsp if (re == NULL)
7420 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7421 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7425 9b058f45 2022-06-30 mark static const struct got_error *
7426 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7428 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7429 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7430 6458efa5 2020-11-24 stsp int n = 0;
7432 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7433 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7435 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7437 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7438 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7440 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7441 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7442 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7443 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7447 9b058f45 2022-06-30 mark return NULL;
7450 6458efa5 2020-11-24 stsp static const struct got_error *
7451 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7453 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7455 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7456 6458efa5 2020-11-24 stsp return NULL;
7459 6458efa5 2020-11-24 stsp static int
7460 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7462 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7464 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7468 6458efa5 2020-11-24 stsp static const struct got_error *
7469 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7471 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7472 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7474 6458efa5 2020-11-24 stsp if (!view->searching) {
7475 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7476 6458efa5 2020-11-24 stsp return NULL;
7479 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7480 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7481 6458efa5 2020-11-24 stsp if (s->selected_entry)
7482 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7484 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7485 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7487 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7488 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7490 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7491 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7494 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7495 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7496 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7497 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7499 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7502 6458efa5 2020-11-24 stsp while (1) {
7503 6458efa5 2020-11-24 stsp if (re == NULL) {
7504 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7505 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7506 6458efa5 2020-11-24 stsp return NULL;
7508 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7509 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7511 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7514 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7515 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7516 6458efa5 2020-11-24 stsp s->matched_entry = re;
7520 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7521 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7523 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7526 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7527 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7528 6458efa5 2020-11-24 stsp s->selected = 0;
7531 6458efa5 2020-11-24 stsp return NULL;
7534 6458efa5 2020-11-24 stsp static const struct got_error *
7535 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7537 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7538 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7539 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7540 6458efa5 2020-11-24 stsp char *line = NULL;
7541 6458efa5 2020-11-24 stsp wchar_t *wline;
7542 6458efa5 2020-11-24 stsp struct tog_color *tc;
7543 6458efa5 2020-11-24 stsp int width, n;
7544 6458efa5 2020-11-24 stsp int limit = view->nlines;
7546 6458efa5 2020-11-24 stsp werase(view->window);
7548 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7549 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7550 9b058f45 2022-06-30 mark --limit; /* border */
7552 6458efa5 2020-11-24 stsp if (limit == 0)
7553 6458efa5 2020-11-24 stsp return NULL;
7555 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7557 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7558 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7559 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7561 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7562 6458efa5 2020-11-24 stsp if (err) {
7563 6458efa5 2020-11-24 stsp free(line);
7564 6458efa5 2020-11-24 stsp return err;
7566 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7567 6458efa5 2020-11-24 stsp wstandout(view->window);
7568 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7569 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7570 6458efa5 2020-11-24 stsp wstandend(view->window);
7571 6458efa5 2020-11-24 stsp free(wline);
7572 6458efa5 2020-11-24 stsp wline = NULL;
7573 6458efa5 2020-11-24 stsp free(line);
7574 6458efa5 2020-11-24 stsp line = NULL;
7575 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7576 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7577 6458efa5 2020-11-24 stsp if (--limit <= 0)
7578 6458efa5 2020-11-24 stsp return NULL;
7581 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7582 6458efa5 2020-11-24 stsp char *line = NULL;
7583 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7585 b4996bee 2022-06-16 stsp if (s->show_date) {
7586 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7587 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7588 b4996bee 2022-06-16 stsp struct got_object_id *id;
7589 b4996bee 2022-06-16 stsp struct tm tm;
7592 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7594 b4996bee 2022-06-16 stsp return err;
7595 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7596 b4996bee 2022-06-16 stsp if (err) {
7597 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7599 b4996bee 2022-06-16 stsp return err;
7601 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7603 b4996bee 2022-06-16 stsp if (err) {
7605 b4996bee 2022-06-16 stsp return err;
7607 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7608 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7610 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7611 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7614 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7615 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7616 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7617 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7619 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7620 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7621 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7622 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7623 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7624 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7625 6458efa5 2020-11-24 stsp struct got_object_id *id;
7626 6458efa5 2020-11-24 stsp char *id_str;
7627 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7629 6458efa5 2020-11-24 stsp return err;
7630 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7631 6458efa5 2020-11-24 stsp if (err) {
7633 6458efa5 2020-11-24 stsp return err;
7635 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7636 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7637 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7639 6458efa5 2020-11-24 stsp free(id_str);
7640 6458efa5 2020-11-24 stsp return err;
7643 6458efa5 2020-11-24 stsp free(id_str);
7644 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7645 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7646 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7648 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7650 6458efa5 2020-11-24 stsp if (err) {
7651 6458efa5 2020-11-24 stsp free(line);
7652 6458efa5 2020-11-24 stsp return err;
7654 6458efa5 2020-11-24 stsp if (n == s->selected) {
7655 6458efa5 2020-11-24 stsp if (view->focussed)
7656 6458efa5 2020-11-24 stsp wstandout(view->window);
7657 6458efa5 2020-11-24 stsp s->selected_entry = re;
7659 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7661 6458efa5 2020-11-24 stsp wattr_on(view->window,
7662 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7663 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7665 6458efa5 2020-11-24 stsp wattr_off(view->window,
7666 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7667 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7668 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7669 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7670 6458efa5 2020-11-24 stsp wstandend(view->window);
7671 6458efa5 2020-11-24 stsp free(line);
7672 6458efa5 2020-11-24 stsp free(wline);
7673 6458efa5 2020-11-24 stsp wline = NULL;
7675 6458efa5 2020-11-24 stsp s->ndisplayed++;
7676 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7679 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7682 9b058f45 2022-06-30 mark view_border(view);
7683 6458efa5 2020-11-24 stsp return err;
7686 6458efa5 2020-11-24 stsp static const struct got_error *
7687 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7688 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7690 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7691 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7692 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7694 c42c9805 2020-11-24 stsp *new_view = NULL;
7696 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7697 c42c9805 2020-11-24 stsp if (err) {
7698 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7699 c42c9805 2020-11-24 stsp return err;
7701 c42c9805 2020-11-24 stsp return NULL;
7705 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7706 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7707 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7708 c42c9805 2020-11-24 stsp goto done;
7711 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7712 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7714 c42c9805 2020-11-24 stsp goto done;
7716 c42c9805 2020-11-24 stsp *new_view = tree_view;
7718 c42c9805 2020-11-24 stsp free(commit_id);
7719 c42c9805 2020-11-24 stsp return err;
7721 c42c9805 2020-11-24 stsp static const struct got_error *
7722 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7724 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7725 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7726 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7727 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7728 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7730 6458efa5 2020-11-24 stsp switch (ch) {
7732 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7733 640cd7ff 2022-06-22 mark view->count = 0;
7736 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7737 640cd7ff 2022-06-22 mark view->count = 0;
7740 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7741 640cd7ff 2022-06-22 mark view->count = 0;
7742 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7743 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7744 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7747 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7748 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7749 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7752 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7753 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7755 6458efa5 2020-11-24 stsp case KEY_ENTER:
7756 6458efa5 2020-11-24 stsp case '\r':
7757 640cd7ff 2022-06-22 mark view->count = 0;
7758 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7760 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7761 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7763 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7764 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7768 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7769 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7770 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7775 e78dc838 2020-12-04 stsp view->focussed = 0;
7776 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7777 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7778 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7779 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7780 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
7781 6458efa5 2020-11-24 stsp err = view_close_child(view);
7783 6458efa5 2020-11-24 stsp return err;
7784 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7787 e78dc838 2020-12-04 stsp view->focus_child = 1;
7789 6458efa5 2020-11-24 stsp *new_view = log_view;
7792 640cd7ff 2022-06-22 mark view->count = 0;
7793 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7795 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7796 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
7797 49b24ee5 2022-07-03 mark err = browse_ref_tree(&tree_view, begin_y, begin_x,
7798 49b24ee5 2022-07-03 mark s->selected_entry, s->repo);
7799 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7801 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7802 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7803 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
7807 e78dc838 2020-12-04 stsp view->focussed = 0;
7808 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7809 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
7810 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
7811 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7812 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
7813 c42c9805 2020-11-24 stsp err = view_close_child(view);
7815 c42c9805 2020-11-24 stsp return err;
7816 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7819 e78dc838 2020-12-04 stsp view->focus_child = 1;
7821 c42c9805 2020-11-24 stsp *new_view = tree_view;
7823 e4526bf5 2021-09-03 naddy case 'g':
7824 e4526bf5 2021-09-03 naddy case KEY_HOME:
7825 e4526bf5 2021-09-03 naddy s->selected = 0;
7826 640cd7ff 2022-06-22 mark view->count = 0;
7827 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7829 e4526bf5 2021-09-03 naddy case 'G':
7830 9b058f45 2022-06-30 mark case KEY_END: {
7831 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7833 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7834 9b058f45 2022-06-30 mark --eos; /* border */
7835 e4526bf5 2021-09-03 naddy s->selected = 0;
7836 640cd7ff 2022-06-22 mark view->count = 0;
7837 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7838 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7839 e4526bf5 2021-09-03 naddy if (re == NULL)
7841 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7842 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7844 e4526bf5 2021-09-03 naddy if (n > 0)
7845 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7849 6458efa5 2020-11-24 stsp case KEY_UP:
7850 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7851 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7852 6458efa5 2020-11-24 stsp s->selected--;
7855 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7856 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7857 640cd7ff 2022-06-22 mark view->count = 0;
7859 83cc4199 2022-06-13 stsp case CTRL('u'):
7861 83cc4199 2022-06-13 stsp nscroll /= 2;
7862 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7863 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7864 6458efa5 2020-11-24 stsp case CTRL('b'):
7866 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7867 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7868 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7869 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7870 640cd7ff 2022-06-22 mark view->count = 0;
7873 6458efa5 2020-11-24 stsp case KEY_DOWN:
7874 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7875 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7876 6458efa5 2020-11-24 stsp s->selected++;
7879 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7880 6458efa5 2020-11-24 stsp /* can't scroll any further */
7881 640cd7ff 2022-06-22 mark view->count = 0;
7884 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7886 83cc4199 2022-06-13 stsp case CTRL('d'):
7888 83cc4199 2022-06-13 stsp nscroll /= 2;
7889 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7890 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7891 6458efa5 2020-11-24 stsp case CTRL('f'):
7893 48bb96f0 2022-06-20 naddy case ' ':
7894 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7895 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7896 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7897 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7898 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7899 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7900 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7901 640cd7ff 2022-06-22 mark view->count = 0;
7904 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7906 6458efa5 2020-11-24 stsp case CTRL('l'):
7907 640cd7ff 2022-06-22 mark view->count = 0;
7908 8924d611 2020-12-26 stsp tog_free_refs();
7909 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7912 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7913 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7915 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7916 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7917 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7920 640cd7ff 2022-06-22 mark view->count = 0;
7924 6458efa5 2020-11-24 stsp return err;
7927 6458efa5 2020-11-24 stsp __dead static void
7928 6458efa5 2020-11-24 stsp usage_ref(void)
7931 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7932 6458efa5 2020-11-24 stsp getprogname());
7936 6458efa5 2020-11-24 stsp static const struct got_error *
7937 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7939 6458efa5 2020-11-24 stsp const struct got_error *error;
7940 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7941 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7942 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7944 6458efa5 2020-11-24 stsp struct tog_view *view;
7945 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7947 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7948 6458efa5 2020-11-24 stsp switch (ch) {
7950 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7951 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7952 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7956 e99e2d15 2020-11-24 naddy usage_ref();
7957 6458efa5 2020-11-24 stsp /* NOTREACHED */
7961 6458efa5 2020-11-24 stsp argc -= optind;
7962 6458efa5 2020-11-24 stsp argv += optind;
7964 6458efa5 2020-11-24 stsp if (argc > 1)
7965 e99e2d15 2020-11-24 naddy usage_ref();
7967 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7968 0ae84acc 2022-06-15 tracey if (error != NULL)
7969 0ae84acc 2022-06-15 tracey goto done;
7971 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7972 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7973 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7974 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7975 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7976 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7977 c156c7a4 2020-12-18 stsp goto done;
7978 6458efa5 2020-11-24 stsp if (worktree)
7979 6458efa5 2020-11-24 stsp repo_path =
7980 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7982 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7983 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7984 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7985 c156c7a4 2020-12-18 stsp goto done;
7989 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7990 6458efa5 2020-11-24 stsp if (error != NULL)
7991 6458efa5 2020-11-24 stsp goto done;
7993 6458efa5 2020-11-24 stsp init_curses();
7995 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7996 51a10b52 2020-12-26 stsp if (error)
7997 51a10b52 2020-12-26 stsp goto done;
7999 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8000 6458efa5 2020-11-24 stsp if (error)
8001 6458efa5 2020-11-24 stsp goto done;
8003 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8004 6458efa5 2020-11-24 stsp if (view == NULL) {
8005 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8006 6458efa5 2020-11-24 stsp goto done;
8009 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8010 6458efa5 2020-11-24 stsp if (error)
8011 6458efa5 2020-11-24 stsp goto done;
8013 6458efa5 2020-11-24 stsp if (worktree) {
8014 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8015 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8016 6458efa5 2020-11-24 stsp worktree = NULL;
8018 6458efa5 2020-11-24 stsp error = view_loop(view);
8020 6458efa5 2020-11-24 stsp free(repo_path);
8021 6458efa5 2020-11-24 stsp free(cwd);
8022 1d0f4054 2021-06-17 stsp if (repo) {
8023 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8024 1d0f4054 2021-06-17 stsp if (close_err)
8025 1d0f4054 2021-06-17 stsp error = close_err;
8027 0ae84acc 2022-06-15 tracey if (pack_fds) {
8028 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8029 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8030 0ae84acc 2022-06-15 tracey if (error == NULL)
8031 0ae84acc 2022-06-15 tracey error = pack_err;
8033 51a10b52 2020-12-26 stsp tog_free_refs();
8034 6458efa5 2020-11-24 stsp return error;
8038 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
8039 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
8041 6458efa5 2020-11-24 stsp static void
8042 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
8044 9b058f45 2022-06-30 mark switch (view->type) {
8045 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8046 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8047 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
8048 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
8052 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
8053 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
8055 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
8056 9b058f45 2022-06-30 mark s->selected_line += view->offset;
8059 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
8060 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
8061 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
8063 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
8064 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
8065 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
8067 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
8068 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
8069 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
8075 9b058f45 2022-06-30 mark view->offset = 0;
8079 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
8080 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
8082 9b058f45 2022-06-30 mark static const struct got_error *
8083 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
8085 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
8086 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
8087 9b058f45 2022-06-30 mark int *selected = NULL;
8088 9b058f45 2022-06-30 mark int header, offset;
8090 9b058f45 2022-06-30 mark switch (view->type) {
8091 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8092 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8093 9b058f45 2022-06-30 mark header = 3;
8094 9b058f45 2022-06-30 mark scrolld = NULL;
8095 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8096 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8097 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8098 9b058f45 2022-06-30 mark s->selected_line -= offset;
8099 9b058f45 2022-06-30 mark view->offset = offset;
8103 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8104 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8105 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8106 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8107 9b058f45 2022-06-30 mark selected = &s->selected;
8110 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8111 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8112 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8113 9b058f45 2022-06-30 mark header = 3;
8114 9b058f45 2022-06-30 mark selected = &s->selected;
8117 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8118 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8119 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8120 9b058f45 2022-06-30 mark header = 5;
8121 9b058f45 2022-06-30 mark selected = &s->selected;
8125 9b058f45 2022-06-30 mark selected = NULL;
8126 9b058f45 2022-06-30 mark scrolld = NULL;
8127 9b058f45 2022-06-30 mark header = 0;
8131 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8132 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8133 9b058f45 2022-06-30 mark view->offset = offset;
8134 9b058f45 2022-06-30 mark if (scrolld && offset) {
8135 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8136 9b058f45 2022-06-30 mark *selected -= offset;
8140 9b058f45 2022-06-30 mark return err;
8143 9b058f45 2022-06-30 mark static void
8144 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8148 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8149 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8150 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8151 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8153 6879ba42 2020-10-01 naddy fputc('\n', fp);
8156 4ed7e80c 2018-05-20 stsp __dead static void
8157 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8159 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8161 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8162 6879ba42 2020-10-01 naddy getprogname());
8163 6879ba42 2020-10-01 naddy if (hflag) {
8164 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8165 6879ba42 2020-10-01 naddy list_commands(fp);
8167 6879ba42 2020-10-01 naddy exit(status);
8170 c2301be8 2018-04-30 stsp static char **
8171 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8173 ee85c5e8 2020-02-29 stsp va_list ap;
8174 c2301be8 2018-04-30 stsp char **argv;
8177 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8179 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8180 c2301be8 2018-04-30 stsp if (argv == NULL)
8181 c2301be8 2018-04-30 stsp err(1, "calloc");
8182 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8183 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8184 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8185 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8188 ee85c5e8 2020-02-29 stsp va_end(ap);
8189 c2301be8 2018-04-30 stsp return argv;
8193 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8194 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8195 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8196 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8197 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8199 ee85c5e8 2020-02-29 stsp static const struct got_error *
8200 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8202 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8203 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8204 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8205 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8206 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8207 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8208 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8209 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8210 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8212 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8213 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8214 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8216 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8217 0ae84acc 2022-06-15 tracey if (error != NULL)
8218 0ae84acc 2022-06-15 tracey goto done;
8220 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8221 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8222 ee85c5e8 2020-02-29 stsp goto done;
8224 ee85c5e8 2020-02-29 stsp if (worktree)
8225 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8227 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8228 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8229 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8230 ee85c5e8 2020-02-29 stsp goto done;
8233 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8234 ee85c5e8 2020-02-29 stsp if (error != NULL)
8235 ee85c5e8 2020-02-29 stsp goto done;
8237 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8238 ee85c5e8 2020-02-29 stsp repo, worktree);
8239 ee85c5e8 2020-02-29 stsp if (error)
8240 ee85c5e8 2020-02-29 stsp goto done;
8242 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8243 84de9106 2020-12-26 stsp if (error)
8244 84de9106 2020-12-26 stsp goto done;
8245 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8246 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8247 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8248 ee85c5e8 2020-02-29 stsp if (error)
8249 ee85c5e8 2020-02-29 stsp goto done;
8251 ee85c5e8 2020-02-29 stsp if (worktree) {
8252 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8253 ee85c5e8 2020-02-29 stsp worktree = NULL;
8256 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8257 a44927cc 2022-04-07 stsp if (error)
8258 a44927cc 2022-04-07 stsp goto done;
8260 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8261 ee85c5e8 2020-02-29 stsp if (error) {
8262 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8263 ee85c5e8 2020-02-29 stsp goto done;
8264 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8265 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8266 6879ba42 2020-10-01 naddy usage(1, 1);
8267 ee85c5e8 2020-02-29 stsp /* not reached */
8270 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8271 1d0f4054 2021-06-17 stsp if (error == NULL)
8272 1d0f4054 2021-06-17 stsp error = close_err;
8273 ee85c5e8 2020-02-29 stsp repo = NULL;
8275 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8276 ee85c5e8 2020-02-29 stsp if (error)
8277 ee85c5e8 2020-02-29 stsp goto done;
8279 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8281 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8282 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8284 1d0f4054 2021-06-17 stsp if (repo) {
8285 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8286 1d0f4054 2021-06-17 stsp if (error == NULL)
8287 1d0f4054 2021-06-17 stsp error = close_err;
8289 a44927cc 2022-04-07 stsp if (commit)
8290 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8291 ee85c5e8 2020-02-29 stsp if (worktree)
8292 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8293 0ae84acc 2022-06-15 tracey if (pack_fds) {
8294 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8295 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8296 0ae84acc 2022-06-15 tracey if (error == NULL)
8297 0ae84acc 2022-06-15 tracey error = pack_err;
8300 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8301 ee85c5e8 2020-02-29 stsp free(commit_id);
8302 ee85c5e8 2020-02-29 stsp free(cwd);
8303 ee85c5e8 2020-02-29 stsp free(repo_path);
8304 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8305 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8307 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8308 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8309 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8311 87670572 2020-12-26 naddy tog_free_refs();
8312 ee85c5e8 2020-02-29 stsp return error;
8316 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8318 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8319 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8320 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8321 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8322 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8323 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8324 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8326 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8328 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8330 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8331 9f7d7167 2018-04-29 stsp switch (ch) {
8333 9f7d7167 2018-04-29 stsp hflag = 1;
8336 53ccebc2 2019-07-30 stsp Vflag = 1;
8339 6879ba42 2020-10-01 naddy usage(hflag, 1);
8340 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8344 9f7d7167 2018-04-29 stsp argc -= optind;
8345 9f7d7167 2018-04-29 stsp argv += optind;
8346 9814e6a3 2020-09-27 naddy optind = 1;
8347 c2301be8 2018-04-30 stsp optreset = 1;
8349 53ccebc2 2019-07-30 stsp if (Vflag) {
8350 53ccebc2 2019-07-30 stsp got_version_print_str();
8351 6879ba42 2020-10-01 naddy return 0;
8354 4010e238 2020-12-04 stsp #ifndef PROFILE
8355 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8356 4010e238 2020-12-04 stsp NULL) == -1)
8357 4010e238 2020-12-04 stsp err(1, "pledge");
8360 c2301be8 2018-04-30 stsp if (argc == 0) {
8361 f29d3e89 2018-06-23 stsp if (hflag)
8362 6879ba42 2020-10-01 naddy usage(hflag, 0);
8363 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8364 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8366 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8370 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8371 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8372 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8373 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8374 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8380 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8381 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8382 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8383 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8384 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8385 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8388 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8389 ee85c5e8 2020-02-29 stsp if (argc != 1)
8390 6879ba42 2020-10-01 naddy usage(0, 1);
8391 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8392 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8394 ee85c5e8 2020-02-29 stsp if (hflag)
8395 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8397 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8401 b46c1e04 2020-09-20 naddy putchar('\n');
8402 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8404 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8405 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8406 a2f4a359 2020-02-28 stsp free(cmd_argv);
8409 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8410 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);