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;
140 cc488aa7 2022-01-23 stsp static const struct got_error *
141 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
142 cc488aa7 2022-01-23 stsp struct got_reference* re2)
144 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
145 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
146 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
148 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
149 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
150 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
153 cc488aa7 2022-01-23 stsp return NULL;
154 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
156 cc488aa7 2022-01-23 stsp return NULL;
159 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
160 cc488aa7 2022-01-23 stsp return NULL;
163 11b20872 2019-11-08 stsp static const struct got_error *
164 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
166 51a10b52 2020-12-26 stsp const struct got_error *err;
168 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
169 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
172 51a10b52 2020-12-26 stsp return err;
174 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
178 51a10b52 2020-12-26 stsp static void
179 51a10b52 2020-12-26 stsp tog_free_refs(void)
181 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
182 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
183 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
185 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
188 51a10b52 2020-12-26 stsp static const struct got_error *
189 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
190 11b20872 2019-11-08 stsp int idx, short color)
192 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
193 11b20872 2019-11-08 stsp struct tog_color *tc;
194 11b20872 2019-11-08 stsp int regerr = 0;
196 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
197 11b20872 2019-11-08 stsp return NULL;
199 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
201 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
202 11b20872 2019-11-08 stsp if (tc == NULL)
203 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
204 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
205 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
206 11b20872 2019-11-08 stsp if (regerr) {
207 11b20872 2019-11-08 stsp static char regerr_msg[512];
208 11b20872 2019-11-08 stsp static char err_msg[512];
209 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
210 11b20872 2019-11-08 stsp sizeof(regerr_msg));
211 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
212 11b20872 2019-11-08 stsp regerr_msg);
213 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 11b20872 2019-11-08 stsp return err;
217 11b20872 2019-11-08 stsp tc->colorpair = idx;
218 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
219 11b20872 2019-11-08 stsp return NULL;
222 11b20872 2019-11-08 stsp static void
223 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
225 11b20872 2019-11-08 stsp struct tog_color *tc;
227 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
228 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
229 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
230 11b20872 2019-11-08 stsp regfree(&tc->regex);
235 336075a4 2022-06-25 op static struct tog_color *
236 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
238 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
240 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
241 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
245 11b20872 2019-11-08 stsp return NULL;
249 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
251 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
252 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
253 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
254 11b20872 2019-11-08 stsp return COLOR_CYAN;
255 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
256 11b20872 2019-11-08 stsp return COLOR_YELLOW;
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
258 11b20872 2019-11-08 stsp return COLOR_GREEN;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
260 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
262 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
264 91b8c405 2020-01-25 stsp return COLOR_CYAN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_GREEN;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
268 11b20872 2019-11-08 stsp return COLOR_GREEN;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
270 11b20872 2019-11-08 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_YELLOW;
273 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
274 6458efa5 2020-11-24 stsp return COLOR_GREEN;
275 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
276 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
277 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
278 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
279 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
280 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
286 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
288 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
290 11b20872 2019-11-08 stsp if (val == NULL)
291 11b20872 2019-11-08 stsp return default_color_value(envvar);
293 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
294 11b20872 2019-11-08 stsp return COLOR_BLACK;
295 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
296 11b20872 2019-11-08 stsp return COLOR_RED;
297 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
298 11b20872 2019-11-08 stsp return COLOR_GREEN;
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
300 11b20872 2019-11-08 stsp return COLOR_YELLOW;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
302 11b20872 2019-11-08 stsp return COLOR_BLUE;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
304 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
306 11b20872 2019-11-08 stsp return COLOR_CYAN;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
308 11b20872 2019-11-08 stsp return COLOR_WHITE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
312 11b20872 2019-11-08 stsp return default_color_value(envvar);
316 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
317 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
318 3dbaef42 2020-11-24 stsp const char *label1, *label2;
319 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
320 f9d37699 2022-06-28 stsp int fd1, fd2;
321 15a087fe 2019-02-21 stsp int first_displayed_line;
322 15a087fe 2019-02-21 stsp int last_displayed_line;
324 15a087fe 2019-02-21 stsp int diff_context;
325 3dbaef42 2020-11-24 stsp int ignore_whitespace;
326 64453f7e 2020-11-21 stsp int force_text_diff;
327 15a087fe 2019-02-21 stsp struct got_repository *repo;
328 bddb1296 2019-11-08 stsp struct tog_colors colors;
329 fe621944 2020-11-10 stsp size_t nlines;
330 f44b1f58 2020-02-02 tracey off_t *line_offsets;
331 f44b1f58 2020-02-02 tracey int matched_line;
332 f44b1f58 2020-02-02 tracey int selected_line;
334 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
335 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
338 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
341 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
342 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
343 1a76625f 2018-10-22 stsp int commits_needed;
344 fb280deb 2021-08-30 stsp int load_all;
345 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
346 1a76625f 2018-10-22 stsp struct commit_queue *commits;
347 1a76625f 2018-10-22 stsp const char *in_repo_path;
348 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
349 1a76625f 2018-10-22 stsp struct got_repository *repo;
350 74467cc8 2022-06-15 stsp int *pack_fds;
351 1a76625f 2018-10-22 stsp int log_complete;
352 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
353 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
354 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
355 13add988 2019-10-15 stsp int *searching;
356 13add988 2019-10-15 stsp int *search_next_done;
357 13add988 2019-10-15 stsp regex_t *regex;
360 1a76625f 2018-10-22 stsp struct tog_log_view_state {
361 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
362 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
363 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
365 b01e7d3b 2018-08-04 stsp int selected;
366 b01e7d3b 2018-08-04 stsp char *in_repo_path;
367 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
368 b672a97a 2020-01-27 stsp int log_branches;
369 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
370 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
371 1a76625f 2018-10-22 stsp sig_atomic_t quit;
372 1a76625f 2018-10-22 stsp pthread_t thread;
373 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
374 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
375 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
376 11b20872 2019-11-08 stsp struct tog_colors colors;
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
381 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
385 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
387 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
388 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
389 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
390 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
391 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
392 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
393 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
395 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
396 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
397 e9424729 2018-08-04 stsp int nlines;
399 e9424729 2018-08-04 stsp struct tog_view *view;
400 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
404 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
405 e9424729 2018-08-04 stsp const char *path;
406 e9424729 2018-08-04 stsp struct got_repository *repo;
407 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
408 e9424729 2018-08-04 stsp int *complete;
409 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
410 fc06ba56 2019-08-22 stsp void *cancel_arg;
413 e9424729 2018-08-04 stsp struct tog_blame {
415 be659d10 2020-11-18 stsp off_t filesize;
416 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
417 6fcac457 2018-11-19 stsp int nlines;
418 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
419 e9424729 2018-08-04 stsp pthread_t thread;
420 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
421 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
422 e9424729 2018-08-04 stsp const char *path;
423 0ae84acc 2022-06-15 tracey int *pack_fds;
426 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
427 7cbe629d 2018-08-04 stsp int first_displayed_line;
428 7cbe629d 2018-08-04 stsp int last_displayed_line;
429 7cbe629d 2018-08-04 stsp int selected_line;
430 7cbe629d 2018-08-04 stsp int blame_complete;
433 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
434 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
435 e5a0f69f 2018-08-18 stsp char *path;
436 7cbe629d 2018-08-04 stsp struct got_repository *repo;
437 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
438 e9424729 2018-08-04 stsp struct tog_blame blame;
439 6c4c42e0 2019-06-24 stsp int matched_line;
440 11b20872 2019-11-08 stsp struct tog_colors colors;
443 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
444 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
445 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
446 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
447 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
448 ad80ab7b 2018-08-04 stsp int selected;
451 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
453 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
454 ad80ab7b 2018-08-04 stsp char *tree_label;
455 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
456 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
457 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
458 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
459 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
460 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
461 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
462 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
463 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
464 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
465 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
466 6458efa5 2020-11-24 stsp struct tog_colors colors;
469 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
470 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
471 6458efa5 2020-11-24 stsp struct got_reference *ref;
475 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
477 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
478 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
479 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
481 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
482 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
483 6458efa5 2020-11-24 stsp struct got_repository *repo;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
485 bddb1296 2019-11-08 stsp struct tog_colors colors;
489 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
491 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
492 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
493 669b5ffa 2018-10-07 stsp * there is enough screen estate.
495 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
496 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
498 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
499 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
500 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
502 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
503 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
505 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
507 cc3c9aac 2018-08-01 stsp struct tog_view {
508 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
509 26ed57b2 2018-05-19 stsp WINDOW *window;
510 26ed57b2 2018-05-19 stsp PANEL *panel;
511 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
512 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
513 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
514 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
515 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
516 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
518 669b5ffa 2018-10-07 stsp struct tog_view *parent;
519 669b5ffa 2018-10-07 stsp struct tog_view *child;
522 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
523 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
524 e78dc838 2020-12-04 stsp * between parent and child.
525 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
526 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
527 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
528 e78dc838 2020-12-04 stsp * situations.
530 e78dc838 2020-12-04 stsp int focus_child;
532 9b058f45 2022-06-30 mark enum tog_view_mode mode;
533 5dc9f4bc 2018-08-04 stsp /* type-specific state */
534 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
536 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
537 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
538 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
539 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
540 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
543 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
544 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
545 e78dc838 2020-12-04 stsp struct tog_view *, int);
546 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
548 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
549 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
550 c0c4acc8 2021-01-24 stsp int search_started;
551 60493ae3 2019-06-20 stsp int searching;
552 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
553 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
554 60493ae3 2019-06-20 stsp int search_next_done;
555 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
556 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
557 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
558 1803e47f 2019-06-22 stsp regex_t regex;
559 41605754 2020-11-12 stsp regmatch_t regmatch;
562 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
563 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
564 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
565 78756c87 2020-11-24 stsp struct got_repository *);
566 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
567 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
568 e78dc838 2020-12-04 stsp struct tog_view *, int);
569 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
570 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
571 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
573 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
574 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
575 78756c87 2020-11-24 stsp const char *, const char *, int);
576 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
577 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
578 e78dc838 2020-12-04 stsp struct tog_view *, int);
579 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
580 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
581 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
584 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
585 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
587 e78dc838 2020-12-04 stsp struct tog_view *, int);
588 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
589 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
590 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
592 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
593 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
594 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
596 e78dc838 2020-12-04 stsp struct tog_view *, int);
597 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
598 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
599 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
601 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
602 6458efa5 2020-11-24 stsp struct got_repository *);
603 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
604 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
605 e78dc838 2020-12-04 stsp struct tog_view *, int);
606 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
607 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
608 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
610 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
611 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
612 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
613 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
614 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
616 25791caa 2018-10-24 stsp static void
617 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
619 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
622 83baff54 2019-08-12 stsp static void
623 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
625 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
628 61266923 2020-01-14 stsp static void
629 61266923 2020-01-14 stsp tog_sigcont(int signo)
631 61266923 2020-01-14 stsp tog_sigcont_received = 1;
634 2497f032 2022-05-31 stsp static void
635 2497f032 2022-05-31 stsp tog_sigint(int signo)
637 2497f032 2022-05-31 stsp tog_sigint_received = 1;
640 2497f032 2022-05-31 stsp static void
641 2497f032 2022-05-31 stsp tog_sigterm(int signo)
643 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
647 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
649 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
650 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
654 e5a0f69f 2018-08-18 stsp static const struct got_error *
655 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
657 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
659 669b5ffa 2018-10-07 stsp if (view->child) {
660 669b5ffa 2018-10-07 stsp view_close(view->child);
661 669b5ffa 2018-10-07 stsp view->child = NULL;
663 e5a0f69f 2018-08-18 stsp if (view->close)
664 e5a0f69f 2018-08-18 stsp err = view->close(view);
665 ea5e7bb5 2018-08-01 stsp if (view->panel)
666 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
667 ea5e7bb5 2018-08-01 stsp if (view->window)
668 ea5e7bb5 2018-08-01 stsp delwin(view->window);
669 ea5e7bb5 2018-08-01 stsp free(view);
670 e5a0f69f 2018-08-18 stsp return err;
673 ea5e7bb5 2018-08-01 stsp static struct tog_view *
674 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
675 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
677 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
679 ea5e7bb5 2018-08-01 stsp if (view == NULL)
680 ea5e7bb5 2018-08-01 stsp return NULL;
682 d6b05b5b 2018-08-04 stsp view->type = type;
683 f7d12f7e 2018-08-01 stsp view->lines = LINES;
684 f7d12f7e 2018-08-01 stsp view->cols = COLS;
685 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
686 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
687 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
688 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
689 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
690 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
691 96a765a8 2018-08-04 stsp view_close(view);
692 ea5e7bb5 2018-08-01 stsp return NULL;
694 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
695 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
696 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
697 96a765a8 2018-08-04 stsp view_close(view);
698 ea5e7bb5 2018-08-01 stsp return NULL;
701 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
702 ea5e7bb5 2018-08-01 stsp return view;
706 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
708 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
710 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
713 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
715 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
717 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
720 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
722 5c60c32a 2018-10-18 stsp static const struct got_error *
723 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
725 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
727 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
728 9b058f45 2022-06-30 mark view->begin_y = view_split_begin_y(view->nlines);
729 9b058f45 2022-06-30 mark view->begin_x = 0;
731 9b058f45 2022-06-30 mark view->begin_x = view_split_begin_x(0);
732 9b058f45 2022-06-30 mark view->begin_y = 0;
734 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
735 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
736 5c60c32a 2018-10-18 stsp view->lines = LINES;
737 5c60c32a 2018-10-18 stsp view->cols = COLS;
738 5c60c32a 2018-10-18 stsp err = view_resize(view);
740 5c60c32a 2018-10-18 stsp return err;
742 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
743 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
745 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
746 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
748 5c60c32a 2018-10-18 stsp return NULL;
751 5c60c32a 2018-10-18 stsp static const struct got_error *
752 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
754 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
756 5c60c32a 2018-10-18 stsp view->begin_x = 0;
757 5c60c32a 2018-10-18 stsp view->begin_y = 0;
758 5c60c32a 2018-10-18 stsp view->nlines = LINES;
759 5c60c32a 2018-10-18 stsp view->ncols = COLS;
760 5c60c32a 2018-10-18 stsp view->lines = LINES;
761 5c60c32a 2018-10-18 stsp view->cols = COLS;
762 5c60c32a 2018-10-18 stsp err = view_resize(view);
764 5c60c32a 2018-10-18 stsp return err;
766 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
767 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
769 5c60c32a 2018-10-18 stsp return NULL;
773 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
775 5c60c32a 2018-10-18 stsp return view->parent == NULL;
779 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
781 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
785 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
787 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
790 9b058f45 2022-06-30 mark static void
791 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
793 9b058f45 2022-06-30 mark PANEL *panel;
794 9b058f45 2022-06-30 mark const struct tog_view *view_above;
796 9b058f45 2022-06-30 mark if (view->parent)
797 9b058f45 2022-06-30 mark return view_border(view->parent);
799 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
800 9b058f45 2022-06-30 mark if (panel == NULL)
803 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
804 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
805 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
806 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
807 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
809 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
810 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
813 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
814 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
815 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
817 4d8c2215 2018-08-19 stsp static const struct got_error *
818 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
820 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
821 9b058f45 2022-06-30 mark int dif, nlines, ncols;
823 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
825 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
826 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
828 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
829 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
830 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
832 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
834 4dd27a72 2022-06-29 stsp if (view->child) {
835 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
837 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
838 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
839 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
840 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
841 0dbbbe90 2022-06-17 op ncols = COLS;
843 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
844 5c60c32a 2018-10-18 stsp if (view->child->focussed)
845 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
847 5c60c32a 2018-10-18 stsp show_panel(view->panel);
849 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
851 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
852 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
855 9b058f45 2022-06-30 mark * Request commits if terminal height was increased in a log
856 9b058f45 2022-06-30 mark * view so we have enough commits loaded to populate the view.
858 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG && dif > 0) {
859 9b058f45 2022-06-30 mark struct tog_log_view_state *ts = &view->state.log;
861 9b058f45 2022-06-30 mark if (ts->commits.ncommits < ts->selected_entry->idx +
862 9b058f45 2022-06-30 mark view->lines - ts->selected) {
863 9b058f45 2022-06-30 mark view->nscrolled = ts->selected_entry->idx +
864 9b058f45 2022-06-30 mark view->lines - ts->selected -
865 9b058f45 2022-06-30 mark ts->commits.ncommits + dif;
866 9b058f45 2022-06-30 mark err = request_log_commits(view);
868 9b058f45 2022-06-30 mark return err;
873 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
874 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
875 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
878 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
880 9b058f45 2022-06-30 mark return err;
881 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
882 9b058f45 2022-06-30 mark err = offset_selection_down(view);
884 9b058f45 2022-06-30 mark return err;
886 9b058f45 2022-06-30 mark view_border(view);
887 9b058f45 2022-06-30 mark update_panels();
888 9b058f45 2022-06-30 mark doupdate();
889 9b058f45 2022-06-30 mark show_panel(view->child->panel);
890 9b058f45 2022-06-30 mark nlines = view->nlines;
892 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
893 0dbbbe90 2022-06-17 op ncols = COLS;
895 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
896 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
897 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
898 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
899 0dbbbe90 2022-06-17 op wclear(view->window);
901 0dbbbe90 2022-06-17 op view->nlines = nlines;
902 0dbbbe90 2022-06-17 op view->ncols = ncols;
903 0dbbbe90 2022-06-17 op view->lines = LINES;
904 0dbbbe90 2022-06-17 op view->cols = COLS;
906 5c60c32a 2018-10-18 stsp return NULL;
909 669b5ffa 2018-10-07 stsp static const struct got_error *
910 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
912 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
914 669b5ffa 2018-10-07 stsp if (view->child == NULL)
915 669b5ffa 2018-10-07 stsp return NULL;
917 669b5ffa 2018-10-07 stsp err = view_close(view->child);
918 669b5ffa 2018-10-07 stsp view->child = NULL;
919 669b5ffa 2018-10-07 stsp return err;
922 0dbbbe90 2022-06-17 op static const struct got_error *
923 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
925 669b5ffa 2018-10-07 stsp view->child = child;
926 669b5ffa 2018-10-07 stsp child->parent = view;
928 0dbbbe90 2022-06-17 op return view_resize(view);
931 34bc9ec9 2019-02-22 stsp static void
932 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
934 25791caa 2018-10-24 stsp int cols, lines;
935 25791caa 2018-10-24 stsp struct winsize size;
937 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
938 25791caa 2018-10-24 stsp cols = 80; /* Default */
939 25791caa 2018-10-24 stsp lines = 24;
941 25791caa 2018-10-24 stsp cols = size.ws_col;
942 25791caa 2018-10-24 stsp lines = size.ws_row;
944 25791caa 2018-10-24 stsp resize_term(lines, cols);
947 2b49a8ae 2019-06-22 stsp static const struct got_error *
948 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
950 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
951 9b058f45 2022-06-30 mark struct tog_view *v = view;
952 2b49a8ae 2019-06-22 stsp char pattern[1024];
955 c0c4acc8 2021-01-24 stsp if (view->search_started) {
956 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
957 c0c4acc8 2021-01-24 stsp view->searching = 0;
958 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
960 c0c4acc8 2021-01-24 stsp view->search_started = 0;
962 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
963 2b49a8ae 2019-06-22 stsp return NULL;
965 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
966 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
967 9b058f45 2022-06-30 mark v = view->child;
969 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
970 9b058f45 2022-06-30 mark wclrtoeol(v->window);
972 2b49a8ae 2019-06-22 stsp nocbreak();
974 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
975 9b058f45 2022-06-30 mark wrefresh(v->window);
978 2b49a8ae 2019-06-22 stsp if (ret == ERR)
979 2b49a8ae 2019-06-22 stsp return NULL;
981 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
982 7c32bd05 2019-06-22 stsp err = view->search_start(view);
984 7c32bd05 2019-06-22 stsp regfree(&view->regex);
985 7c32bd05 2019-06-22 stsp return err;
987 c0c4acc8 2021-01-24 stsp view->search_started = 1;
988 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
989 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
990 2b49a8ae 2019-06-22 stsp view->search_next(view);
993 2b49a8ae 2019-06-22 stsp return NULL;
997 640cd7ff 2022-06-22 mark * Compute view->count from numeric user input. User has five-tenths of a
998 640cd7ff 2022-06-22 mark * second to follow each numeric keypress with another number to form count.
999 640cd7ff 2022-06-22 mark * Return first non-numeric input or ERR and assign total to view->count.
1000 640cd7ff 2022-06-22 mark * XXX Should we add support for user-defined timeout?
1002 640cd7ff 2022-06-22 mark static int
1003 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1005 9b058f45 2022-06-30 mark struct tog_view *v = view;
1006 9b058f45 2022-06-30 mark int x, n = 0;
1008 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1009 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
1010 9b058f45 2022-06-30 mark v = view->child;
1011 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1012 9b058f45 2022-06-30 mark v = view->parent;
1014 640cd7ff 2022-06-22 mark view->count = 0;
1015 640cd7ff 2022-06-22 mark halfdelay(5); /* block for half a second */
1016 9b058f45 2022-06-30 mark wattron(v->window, A_BOLD);
1017 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1018 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1019 9b058f45 2022-06-30 mark waddch(v->window, ':');
1022 9b058f45 2022-06-30 mark x = getcurx(v->window);
1023 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1024 9b058f45 2022-06-30 mark waddch(v->window, c);
1025 9b058f45 2022-06-30 mark wrefresh(v->window);
1029 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1030 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1032 640cd7ff 2022-06-22 mark if (n >= 9999999)
1033 640cd7ff 2022-06-22 mark n = 9999999;
1035 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1036 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1038 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1039 640cd7ff 2022-06-22 mark view->count = n;
1041 9b058f45 2022-06-30 mark wattroff(v->window, A_BOLD);
1042 640cd7ff 2022-06-22 mark cbreak(); /* return to blocking */
1046 0cf4efb1 2018-09-29 stsp static const struct got_error *
1047 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1048 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1050 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1051 669b5ffa 2018-10-07 stsp struct tog_view *v;
1052 1a76625f 2018-10-22 stsp int ch, errcode;
1054 e5a0f69f 2018-08-18 stsp *new = NULL;
1056 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1057 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1058 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1059 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1060 640cd7ff 2022-06-22 mark view->count = 0;
1063 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1064 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1065 82954512 2020-02-03 stsp if (errcode)
1066 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1067 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1068 3da8ef85 2021-09-21 stsp sched_yield();
1069 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1070 82954512 2020-02-03 stsp if (errcode)
1071 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1072 82954512 2020-02-03 stsp "pthread_mutex_lock");
1073 60493ae3 2019-06-20 stsp view->search_next(view);
1074 60493ae3 2019-06-20 stsp return NULL;
1077 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
1078 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1079 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1080 1a76625f 2018-10-22 stsp if (errcode)
1081 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1082 640cd7ff 2022-06-22 mark /* If we have an unfinished count, don't get a new key map. */
1083 640cd7ff 2022-06-22 mark ch = view->ch;
1084 640cd7ff 2022-06-22 mark if ((view->count && --view->count == 0) || !view->count) {
1085 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1086 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1087 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1090 1a76625f 2018-10-22 stsp if (errcode)
1091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1092 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
1094 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1095 25791caa 2018-10-24 stsp tog_resizeterm();
1096 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1097 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1098 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1099 25791caa 2018-10-24 stsp err = view_resize(v);
1101 25791caa 2018-10-24 stsp return err;
1102 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1104 25791caa 2018-10-24 stsp return err;
1105 cdfcfb03 2020-12-06 stsp if (v->child) {
1106 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1108 cdfcfb03 2020-12-06 stsp return err;
1109 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1110 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1112 cdfcfb03 2020-12-06 stsp return err;
1117 e5a0f69f 2018-08-18 stsp switch (ch) {
1119 640cd7ff 2022-06-22 mark view->count = 0;
1120 1e37a5c2 2019-05-12 jcs if (view->child) {
1121 e78dc838 2020-12-04 stsp view->focussed = 0;
1122 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1123 e78dc838 2020-12-04 stsp view->focus_child = 1;
1124 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1125 e78dc838 2020-12-04 stsp view->focussed = 0;
1126 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1127 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1128 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1129 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1130 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1131 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1133 9b058f45 2022-06-30 mark return err;
1135 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1136 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1138 9b058f45 2022-06-30 mark return err;
1143 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1144 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1145 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1146 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1150 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1151 9b058f45 2022-06-30 mark view->parent->mode = TOG_VIEW_SPLIT_NONE;
1153 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1154 9970f7fc 2020-12-03 stsp view->dying = 1;
1160 640cd7ff 2022-06-22 mark view->count = 0;
1161 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1162 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1164 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1165 e78dc838 2020-12-04 stsp view->focussed = 0;
1166 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1167 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1169 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1172 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1173 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1175 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1176 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1177 e78dc838 2020-12-04 stsp view->focussed = 1;
1178 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1180 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1181 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1182 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1186 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1190 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1191 9b058f45 2022-06-30 mark err = request_log_commits(view);
1195 9b058f45 2022-06-30 mark if (view->parent)
1196 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1198 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1200 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1203 640cd7ff 2022-06-22 mark view->count = 0;
1204 60493ae3 2019-06-20 stsp if (view->search_start)
1205 2b49a8ae 2019-06-22 stsp view_search_start(view);
1207 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1211 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1212 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1213 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1214 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1215 60493ae3 2019-06-20 stsp view->search_next(view);
1217 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1220 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1224 e5a0f69f 2018-08-18 stsp return err;
1228 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1230 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1231 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1233 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1235 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1238 669b5ffa 2018-10-07 stsp return view->focussed;
1241 bcbd79e2 2018-08-19 stsp static const struct got_error *
1242 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1244 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1245 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1246 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1247 fd823528 2018-10-22 stsp int fast_refresh = 10;
1248 1a76625f 2018-10-22 stsp int done = 0, errcode;
1250 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1251 1a76625f 2018-10-22 stsp if (errcode)
1252 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1254 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1255 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1257 1004088d 2018-09-29 stsp view->focussed = 1;
1258 878940b7 2018-09-29 stsp err = view->show(view);
1260 0cf4efb1 2018-09-29 stsp return err;
1261 0cf4efb1 2018-09-29 stsp update_panels();
1262 0cf4efb1 2018-09-29 stsp doupdate();
1263 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1264 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1265 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1266 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1268 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1271 9970f7fc 2020-12-03 stsp if (view->dying) {
1272 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1274 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1275 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1277 e78dc838 2020-12-04 stsp else if (view->parent)
1278 669b5ffa 2018-10-07 stsp prev = view->parent;
1280 e78dc838 2020-12-04 stsp if (view->parent) {
1281 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1282 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1283 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1284 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1285 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1289 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1291 9970f7fc 2020-12-03 stsp err = view_close(view);
1293 e5a0f69f 2018-08-18 stsp goto done;
1295 e78dc838 2020-12-04 stsp view = NULL;
1296 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1297 e78dc838 2020-12-04 stsp if (v->focussed)
1300 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1301 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1303 e78dc838 2020-12-04 stsp view = prev;
1304 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1305 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1306 e78dc838 2020-12-04 stsp tog_view_list_head);
1308 e78dc838 2020-12-04 stsp if (view) {
1309 e78dc838 2020-12-04 stsp if (view->focus_child) {
1310 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1311 e78dc838 2020-12-04 stsp view = view->child;
1313 e78dc838 2020-12-04 stsp view->focussed = 1;
1317 bcbd79e2 2018-08-19 stsp if (new_view) {
1318 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1319 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1320 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1321 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1323 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1324 86c66b02 2018-10-18 stsp err = view_close(v);
1326 86c66b02 2018-10-18 stsp goto done;
1329 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1330 fed7eaa8 2018-10-24 stsp view = new_view;
1332 669b5ffa 2018-10-07 stsp if (view) {
1333 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1334 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1335 e78dc838 2020-12-04 stsp view = view->child;
1337 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1338 e78dc838 2020-12-04 stsp view = view->parent;
1340 e78dc838 2020-12-04 stsp show_panel(view->panel);
1341 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1342 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1343 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1344 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1346 1a76625f 2018-10-22 stsp goto done;
1348 669b5ffa 2018-10-07 stsp err = view->show(view);
1350 1a76625f 2018-10-22 stsp goto done;
1351 669b5ffa 2018-10-07 stsp if (view->child) {
1352 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1354 1a76625f 2018-10-22 stsp goto done;
1356 1a76625f 2018-10-22 stsp update_panels();
1357 1a76625f 2018-10-22 stsp doupdate();
1361 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1362 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1363 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1364 e5a0f69f 2018-08-18 stsp view_close(view);
1367 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1368 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1369 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1371 e5a0f69f 2018-08-18 stsp return err;
1374 4ed7e80c 2018-05-20 stsp __dead static void
1375 9f7d7167 2018-04-29 stsp usage_log(void)
1378 c70c5802 2018-08-01 stsp fprintf(stderr,
1379 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1380 9f7d7167 2018-04-29 stsp getprogname());
1384 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1385 80ddbec8 2018-04-29 stsp static const struct got_error *
1386 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1388 00dfcb92 2018-06-11 stsp char *vis = NULL;
1389 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1391 963b370f 2018-05-20 stsp *ws = NULL;
1392 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1393 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1394 00dfcb92 2018-06-11 stsp int vislen;
1395 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1396 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1398 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1399 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1401 00dfcb92 2018-06-11 stsp return err;
1402 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1403 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1404 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1405 a7f50699 2018-06-11 stsp goto done;
1409 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1410 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1411 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1412 a7f50699 2018-06-11 stsp goto done;
1415 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1416 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1418 00dfcb92 2018-06-11 stsp free(vis);
1419 963b370f 2018-05-20 stsp if (err) {
1420 963b370f 2018-05-20 stsp free(*ws);
1421 963b370f 2018-05-20 stsp *ws = NULL;
1422 963b370f 2018-05-20 stsp *wlen = 0;
1424 963b370f 2018-05-20 stsp return err;
1427 145b6838 2022-06-16 stsp static const struct got_error *
1428 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1430 145b6838 2022-06-16 stsp char *dst;
1431 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1433 145b6838 2022-06-16 stsp *ptr = NULL;
1434 145b6838 2022-06-16 stsp n = len = strlen(src);
1435 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1436 145b6838 2022-06-16 stsp if (dst == NULL)
1437 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1439 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1440 145b6838 2022-06-16 stsp const char c = src[idx];
1442 145b6838 2022-06-16 stsp if (c == '\t') {
1443 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1446 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1447 6e1c41ad 2022-06-16 mark if (p == NULL) {
1448 6e1c41ad 2022-06-16 mark free(dst);
1449 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1454 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1457 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1461 145b6838 2022-06-16 stsp dst[sz] = '\0';
1462 145b6838 2022-06-16 stsp *ptr = dst;
1463 145b6838 2022-06-16 stsp return NULL;
1467 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1468 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1469 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1473 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1475 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1477 4e4a9ac8 2022-06-17 op if (n == 0) {
1478 4e4a9ac8 2022-06-17 op *rcol = cols;
1482 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1483 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1484 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1486 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1488 4e4a9ac8 2022-06-17 op if (width == -1) {
1490 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1493 4e4a9ac8 2022-06-17 op if (cols + width > n)
1495 4e4a9ac8 2022-06-17 op cols += width;
1498 4e4a9ac8 2022-06-17 op *rcol = cols;
1503 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1504 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1505 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1507 ccda2f4d 2022-06-16 stsp static const struct got_error *
1508 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1509 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1511 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1513 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1514 145b6838 2022-06-16 stsp char *exstr = NULL;
1515 963b370f 2018-05-20 stsp size_t wlen;
1516 4e4a9ac8 2022-06-17 op int i, scrollx;
1518 963b370f 2018-05-20 stsp *wlinep = NULL;
1519 b700b5d6 2018-07-10 stsp *widthp = 0;
1521 145b6838 2022-06-16 stsp if (expand) {
1522 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1524 145b6838 2022-06-16 stsp return err;
1527 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1528 145b6838 2022-06-16 stsp free(exstr);
1530 963b370f 2018-05-20 stsp return err;
1532 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1534 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1535 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1538 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1539 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1543 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1544 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1546 b700b5d6 2018-07-10 stsp if (widthp)
1547 b700b5d6 2018-07-10 stsp *widthp = cols;
1548 ccda2f4d 2022-06-16 stsp if (scrollxp)
1549 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1551 963b370f 2018-05-20 stsp free(wline);
1553 963b370f 2018-05-20 stsp *wlinep = wline;
1554 963b370f 2018-05-20 stsp return err;
1557 8b473291 2019-02-21 stsp static const struct got_error*
1558 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1559 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1561 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1562 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1564 8b473291 2019-02-21 stsp const char *name;
1566 8b473291 2019-02-21 stsp *refs_str = NULL;
1568 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1569 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1570 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1573 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1574 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1576 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1577 8b473291 2019-02-21 stsp name += 5;
1578 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1579 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1581 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1582 8b473291 2019-02-21 stsp name += 6;
1583 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1584 8b473291 2019-02-21 stsp name += 8;
1585 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1586 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1589 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1592 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1593 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1594 5d844a1e 2019-08-13 stsp if (err) {
1595 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1596 48cae60d 2020-09-22 stsp free(ref_id);
1599 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1600 5d844a1e 2019-08-13 stsp err = NULL;
1601 5d844a1e 2019-08-13 stsp tag = NULL;
1604 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1605 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1606 48cae60d 2020-09-22 stsp free(ref_id);
1608 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1609 52b5abe1 2019-08-13 stsp if (cmp != 0)
1611 8b473291 2019-02-21 stsp s = *refs_str;
1612 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1613 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1614 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1616 8b473291 2019-02-21 stsp *refs_str = NULL;
1622 8b473291 2019-02-21 stsp return err;
1625 963b370f 2018-05-20 stsp static const struct got_error *
1626 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1627 27a741e5 2019-09-11 stsp int col_tab_align)
1629 e6b8b890 2020-12-29 naddy char *smallerthan;
1631 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1632 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1633 5813d178 2019-03-09 stsp author = smallerthan + 1;
1634 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1635 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1636 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1639 5813d178 2019-03-09 stsp static const struct got_error *
1640 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1641 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1642 8fdc79fe 2020-12-01 naddy int author_display_cols)
1644 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1645 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1646 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1647 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1648 5813d178 2019-03-09 stsp char *author = NULL;
1649 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1650 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1651 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1652 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1653 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1654 ccb26ccd 2018-11-05 stsp struct tm tm;
1655 45d799e2 2018-12-23 stsp time_t committer_time;
1656 11b20872 2019-11-08 stsp struct tog_color *tc;
1658 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1659 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1660 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1661 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1662 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1664 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1665 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1667 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1668 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1670 11b20872 2019-11-08 stsp wattr_on(view->window,
1671 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1672 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1674 11b20872 2019-11-08 stsp wattr_off(view->window,
1675 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1676 27a741e5 2019-09-11 stsp col = limit;
1677 b39d25c7 2018-07-10 stsp if (col > avail)
1678 b39d25c7 2018-07-10 stsp goto done;
1680 6570a66d 2019-11-08 stsp if (avail >= 120) {
1681 6570a66d 2019-11-08 stsp char *id_str;
1682 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1684 6570a66d 2019-11-08 stsp goto done;
1685 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1687 11b20872 2019-11-08 stsp wattr_on(view->window,
1688 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1689 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1691 11b20872 2019-11-08 stsp wattr_off(view->window,
1692 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1693 6570a66d 2019-11-08 stsp free(id_str);
1695 6570a66d 2019-11-08 stsp if (col > avail)
1696 6570a66d 2019-11-08 stsp goto done;
1699 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1700 5813d178 2019-03-09 stsp if (author == NULL) {
1701 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1702 80ddbec8 2018-04-29 stsp goto done;
1704 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1706 bb737323 2018-05-20 stsp goto done;
1707 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1709 11b20872 2019-11-08 stsp wattr_on(view->window,
1710 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1711 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1713 11b20872 2019-11-08 stsp wattr_off(view->window,
1714 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1715 bb737323 2018-05-20 stsp col += author_width;
1716 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1717 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1719 bb737323 2018-05-20 stsp author_width++;
1721 9c2eaf34 2018-05-20 stsp if (col > avail)
1722 9c2eaf34 2018-05-20 stsp goto done;
1724 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1726 6d9fbc00 2018-04-29 stsp goto done;
1727 bb737323 2018-05-20 stsp logmsg = logmsg0;
1728 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1730 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1731 bb737323 2018-05-20 stsp if (newline)
1732 bb737323 2018-05-20 stsp *newline = '\0';
1733 ccda2f4d 2022-06-16 stsp limit = avail - col;
1734 63ba1a3a 2022-06-21 op if (view->child && view_is_splitscreen(view->child) && limit > 0)
1735 4d1f6af3 2022-06-17 op limit--; /* for the border */
1736 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1737 ccda2f4d 2022-06-16 stsp limit, col, 1);
1739 29688b02 2022-06-16 stsp goto done;
1740 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1741 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1742 27a741e5 2019-09-11 stsp while (col < avail) {
1743 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1747 80ddbec8 2018-04-29 stsp free(logmsg0);
1748 bb737323 2018-05-20 stsp free(wlogmsg);
1749 5813d178 2019-03-09 stsp free(author);
1750 bb737323 2018-05-20 stsp free(wauthor);
1751 80ddbec8 2018-04-29 stsp free(line);
1752 80ddbec8 2018-04-29 stsp return err;
1755 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1756 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1757 899d86c2 2018-05-10 stsp struct got_object_id *id)
1759 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1761 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1762 80ddbec8 2018-04-29 stsp if (entry == NULL)
1763 899d86c2 2018-05-10 stsp return NULL;
1765 899d86c2 2018-05-10 stsp entry->id = id;
1766 99db9666 2018-05-07 stsp entry->commit = commit;
1767 899d86c2 2018-05-10 stsp return entry;
1770 99db9666 2018-05-07 stsp static void
1771 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1773 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1775 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1776 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1777 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1778 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1779 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1780 99db9666 2018-05-07 stsp free(entry);
1783 99db9666 2018-05-07 stsp static void
1784 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1786 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1787 99db9666 2018-05-07 stsp pop_commit(commits);
1790 c4972b91 2018-05-07 stsp static const struct got_error *
1791 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1792 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1794 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1795 13add988 2019-10-15 stsp regmatch_t regmatch;
1796 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1798 13add988 2019-10-15 stsp *have_match = 0;
1800 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1802 13add988 2019-10-15 stsp return err;
1804 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1806 13add988 2019-10-15 stsp goto done;
1808 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1809 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1810 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1811 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1812 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1813 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1814 13add988 2019-10-15 stsp *have_match = 1;
1816 13add988 2019-10-15 stsp free(id_str);
1817 13add988 2019-10-15 stsp free(logmsg);
1818 13add988 2019-10-15 stsp return err;
1821 13add988 2019-10-15 stsp static const struct got_error *
1822 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1824 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1827 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1828 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1829 1a76625f 2018-10-22 stsp * while updating the display.
1832 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1833 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1834 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1835 1a76625f 2018-10-22 stsp int errcode;
1837 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1838 4e0d2870 2020-12-07 naddy NULL, NULL);
1839 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1842 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1845 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1846 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1847 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1851 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1852 1a76625f 2018-10-22 stsp if (errcode) {
1853 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1854 13add988 2019-10-15 stsp "pthread_mutex_lock");
1858 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1859 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1860 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1862 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1863 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1864 7c1452c1 2020-03-26 stsp int have_match;
1865 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1868 7c1452c1 2020-03-26 stsp if (have_match)
1869 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1872 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1873 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1874 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1875 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1878 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1880 9ba79e04 2018-06-11 stsp return err;
1883 2b779855 2020-12-05 naddy static void
1884 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1886 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1887 2b779855 2020-12-05 naddy int ncommits = 0;
1889 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1890 2b779855 2020-12-05 naddy while (entry) {
1891 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1892 2b779855 2020-12-05 naddy s->selected_entry = entry;
1895 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1896 2b779855 2020-12-05 naddy ncommits++;
1900 0553a4e3 2018-04-30 stsp static const struct got_error *
1901 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1903 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1904 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1905 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1906 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1907 60493ae3 2019-06-20 stsp int width;
1908 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1909 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1910 8b473291 2019-02-21 stsp char *refs_str = NULL;
1911 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1912 11b20872 2019-11-08 stsp struct tog_color *tc;
1913 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1915 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1916 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1917 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1918 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1920 ecb28ae0 2018-07-16 stsp return err;
1921 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1922 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1923 d2075bf3 2020-12-25 stsp if (refs) {
1924 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1925 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1927 d2075bf3 2020-12-25 stsp goto done;
1931 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1932 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1934 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1935 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1936 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1937 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1938 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1939 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1940 8f4ed634 2020-03-26 stsp goto done;
1943 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1945 f9686aa5 2020-03-27 stsp if (view->searching) {
1946 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1947 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1948 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1949 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1950 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1951 f9686aa5 2020-03-27 stsp search_str = "searching...";
1954 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1955 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1956 f9686aa5 2020-03-27 stsp search_str ? search_str :
1957 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1958 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1959 8f4ed634 2020-03-26 stsp goto done;
1963 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1964 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1965 87411fa9 2022-06-16 stsp "........................................",
1966 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1967 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1968 1a76625f 2018-10-22 stsp header = NULL;
1969 1a76625f 2018-10-22 stsp goto done;
1971 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1972 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1973 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1974 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1975 1a76625f 2018-10-22 stsp header = NULL;
1976 1a76625f 2018-10-22 stsp goto done;
1978 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1980 1a76625f 2018-10-22 stsp goto done;
1982 2814baeb 2018-08-01 stsp werase(view->window);
1984 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1985 a3404814 2018-09-02 stsp wstandout(view->window);
1986 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1988 11b20872 2019-11-08 stsp wattr_on(view->window,
1989 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1990 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1992 11b20872 2019-11-08 stsp wattr_off(view->window,
1993 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1994 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1995 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1998 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1999 a3404814 2018-09-02 stsp wstandend(view->window);
2000 ecb28ae0 2018-07-16 stsp free(wline);
2001 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2002 1a76625f 2018-10-22 stsp goto done;
2004 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2005 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2006 5813d178 2019-03-09 stsp ncommits = 0;
2007 145b6838 2022-06-16 stsp view->maxx = 0;
2008 5813d178 2019-03-09 stsp while (entry) {
2009 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2010 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2011 5813d178 2019-03-09 stsp int width;
2012 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2014 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2015 5813d178 2019-03-09 stsp if (author == NULL) {
2016 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2017 5813d178 2019-03-09 stsp goto done;
2019 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2020 27a741e5 2019-09-11 stsp date_display_cols);
2021 5813d178 2019-03-09 stsp if (author_cols < width)
2022 5813d178 2019-03-09 stsp author_cols = width;
2023 5813d178 2019-03-09 stsp free(wauthor);
2024 5813d178 2019-03-09 stsp free(author);
2025 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
2027 145b6838 2022-06-16 stsp goto done;
2028 145b6838 2022-06-16 stsp msg = msg0;
2029 145b6838 2022-06-16 stsp while (*msg == '\n')
2031 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2032 29688b02 2022-06-16 stsp *eol = '\0';
2033 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2034 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2036 29688b02 2022-06-16 stsp goto done;
2037 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2038 145b6838 2022-06-16 stsp free(msg0);
2039 29688b02 2022-06-16 stsp free(wmsg);
2040 7ca04879 2019-10-19 stsp ncommits++;
2041 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2044 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2045 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2046 867c6645 2018-07-10 stsp ncommits = 0;
2047 899d86c2 2018-05-10 stsp while (entry) {
2048 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2050 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2051 2814baeb 2018-08-01 stsp wstandout(view->window);
2052 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2053 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2054 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2055 2814baeb 2018-08-01 stsp wstandend(view->window);
2057 60493ae3 2019-06-20 stsp goto done;
2058 0553a4e3 2018-04-30 stsp ncommits++;
2059 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2060 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2063 9b058f45 2022-06-30 mark view_border(view);
2065 1a76625f 2018-10-22 stsp free(id_str);
2066 8b473291 2019-02-21 stsp free(refs_str);
2067 1a76625f 2018-10-22 stsp free(ncommits_str);
2068 1a76625f 2018-10-22 stsp free(header);
2069 80ddbec8 2018-04-29 stsp return err;
2072 07b55e75 2018-05-10 stsp static void
2073 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2075 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2076 07b55e75 2018-05-10 stsp int nscrolled = 0;
2078 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2079 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2082 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2083 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2084 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2085 07b55e75 2018-05-10 stsp if (entry) {
2086 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2087 07b55e75 2018-05-10 stsp nscrolled++;
2092 aa075928 2018-05-10 stsp static const struct got_error *
2093 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2095 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2096 5e224a3e 2019-02-22 stsp int errcode;
2098 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2100 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2101 ffe38506 2020-12-01 naddy if (ta->log_complete)
2104 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2105 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2106 7aafa0d1 2019-02-22 stsp if (errcode)
2107 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2108 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2111 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2112 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2114 7c1452c1 2020-03-26 stsp if (!wait)
2117 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2118 ffe38506 2020-12-01 naddy show_log_view(view);
2119 7c1452c1 2020-03-26 stsp update_panels();
2120 7c1452c1 2020-03-26 stsp doupdate();
2122 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2123 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2124 82954512 2020-02-03 stsp if (errcode)
2125 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2126 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2128 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2129 ffe38506 2020-12-01 naddy show_log_view(view);
2130 7c1452c1 2020-03-26 stsp update_panels();
2131 7c1452c1 2020-03-26 stsp doupdate();
2134 5e224a3e 2019-02-22 stsp return NULL;
2137 5e224a3e 2019-02-22 stsp static const struct got_error *
2138 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2140 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2141 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2143 9b058f45 2022-06-30 mark state->thread_args.commits_needed = view->nscrolled;
2144 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2145 9b058f45 2022-06-30 mark view->nscrolled = 0;
2147 9b058f45 2022-06-30 mark return err;
2150 9b058f45 2022-06-30 mark static const struct got_error *
2151 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2153 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2154 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2155 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2156 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2158 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2159 5e224a3e 2019-02-22 stsp return NULL;
2161 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2162 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2163 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2165 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2167 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2168 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2170 5e224a3e 2019-02-22 stsp return err;
2174 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2175 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2178 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2179 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2181 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2182 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2184 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2185 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2187 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
2188 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2190 9b058f45 2022-06-30 mark view->nscrolled = 0;
2192 dd0a52c1 2018-05-20 stsp return err;
2195 cd0acaa7 2018-05-20 stsp static const struct got_error *
2196 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2197 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2198 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2200 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2201 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2202 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2204 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2205 15a94983 2018-12-23 stsp if (diff_view == NULL)
2206 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2208 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2209 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2210 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2211 e5a0f69f 2018-08-18 stsp if (err == NULL)
2212 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2213 cd0acaa7 2018-05-20 stsp return err;
2216 80ddbec8 2018-04-29 stsp static const struct got_error *
2217 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2218 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2220 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2222 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2223 941e9f74 2019-05-21 stsp if (parent == NULL)
2224 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2226 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2227 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2228 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2229 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2230 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2231 941e9f74 2019-05-21 stsp s->tree = subtree;
2232 941e9f74 2019-05-21 stsp s->selected = 0;
2233 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2234 941e9f74 2019-05-21 stsp return NULL;
2237 941e9f74 2019-05-21 stsp static const struct got_error *
2238 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2239 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2241 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2242 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2243 941e9f74 2019-05-21 stsp const char *p;
2244 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2246 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2248 941e9f74 2019-05-21 stsp while (*p) {
2249 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2250 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2251 56e0773d 2019-11-28 stsp char *te_name;
2253 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2256 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2257 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2258 941e9f74 2019-05-21 stsp if (slash == NULL)
2259 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2261 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2262 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2263 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2266 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2267 56e0773d 2019-11-28 stsp if (te == NULL) {
2268 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2269 56e0773d 2019-11-28 stsp free(te_name);
2272 56e0773d 2019-11-28 stsp free(te_name);
2273 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2275 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2276 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2278 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2279 941e9f74 2019-05-21 stsp if (slash)
2280 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2282 941e9f74 2019-05-21 stsp subpath = strdup(path);
2283 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2284 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2288 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2293 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2294 941e9f74 2019-05-21 stsp free(tree_id);
2298 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2299 941e9f74 2019-05-21 stsp if (err) {
2300 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2303 941e9f74 2019-05-21 stsp if (slash == NULL)
2305 941e9f74 2019-05-21 stsp free(subpath);
2306 941e9f74 2019-05-21 stsp subpath = NULL;
2307 941e9f74 2019-05-21 stsp p = slash;
2310 941e9f74 2019-05-21 stsp free(subpath);
2311 1a76625f 2018-10-22 stsp return err;
2314 61266923 2020-01-14 stsp static const struct got_error *
2315 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2316 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2317 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2319 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2320 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2321 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2323 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2324 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2325 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2327 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2329 55cccc34 2020-02-20 stsp return err;
2330 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2332 55cccc34 2020-02-20 stsp *new_view = tree_view;
2334 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2335 55cccc34 2020-02-20 stsp return NULL;
2337 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2340 55cccc34 2020-02-20 stsp static const struct got_error *
2341 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2343 61266923 2020-01-14 stsp sigset_t sigset;
2344 61266923 2020-01-14 stsp int errcode;
2346 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2347 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2349 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2350 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2351 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2352 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2353 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2354 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2355 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2356 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2357 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2359 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2360 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2361 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2363 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2364 61266923 2020-01-14 stsp if (errcode)
2365 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2367 61266923 2020-01-14 stsp return NULL;
2370 1a76625f 2018-10-22 stsp static void *
2371 1a76625f 2018-10-22 stsp log_thread(void *arg)
2373 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2374 1a76625f 2018-10-22 stsp int errcode = 0;
2375 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2376 1a76625f 2018-10-22 stsp int done = 0;
2378 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2380 61266923 2020-01-14 stsp return (void *)err;
2382 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2383 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2384 1a76625f 2018-10-22 stsp if (err) {
2385 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2386 1a76625f 2018-10-22 stsp return (void *)err;
2387 1a76625f 2018-10-22 stsp err = NULL;
2389 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2390 1a76625f 2018-10-22 stsp a->commits_needed--;
2392 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2393 3abe8080 2019-04-10 stsp if (errcode) {
2394 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2395 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2397 3abe8080 2019-04-10 stsp } else if (*a->quit)
2399 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2400 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2401 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2402 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2405 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2406 7c1452c1 2020-03-26 stsp if (errcode) {
2407 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2408 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2409 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2414 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2416 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2417 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2418 7c1452c1 2020-03-26 stsp &tog_mutex);
2419 7c1452c1 2020-03-26 stsp if (errcode)
2420 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2421 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2422 21355643 2020-12-06 stsp if (*a->quit)
2427 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2428 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2429 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2430 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2432 3abe8080 2019-04-10 stsp a->log_complete = 1;
2433 1a76625f 2018-10-22 stsp return (void *)err;
2436 1a76625f 2018-10-22 stsp static const struct got_error *
2437 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2439 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2440 1a76625f 2018-10-22 stsp int errcode;
2442 1a76625f 2018-10-22 stsp if (s->thread) {
2443 1a76625f 2018-10-22 stsp s->quit = 1;
2444 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2445 1a76625f 2018-10-22 stsp if (errcode)
2446 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2447 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2448 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2449 1a76625f 2018-10-22 stsp if (errcode)
2450 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2451 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2452 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2453 1a76625f 2018-10-22 stsp if (errcode)
2454 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2455 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2456 1a76625f 2018-10-22 stsp if (errcode)
2457 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2458 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2459 1a76625f 2018-10-22 stsp s->thread = NULL;
2462 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2463 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2464 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2467 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2468 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2469 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2470 74467cc8 2022-06-15 stsp if (err == NULL)
2471 74467cc8 2022-06-15 stsp err = pack_err;
2472 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2475 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2476 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2477 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2480 9343a5fb 2018-06-23 stsp return err;
2483 9343a5fb 2018-06-23 stsp static const struct got_error *
2484 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2486 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2487 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2488 276b94a1 2020-11-13 naddy int errcode;
2490 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2492 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2493 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2494 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2496 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2497 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2498 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2500 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2501 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2502 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2503 1a76625f 2018-10-22 stsp free(s->start_id);
2504 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2505 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2506 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2507 1a76625f 2018-10-22 stsp return err;
2510 1a76625f 2018-10-22 stsp static const struct got_error *
2511 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2513 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2515 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2516 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2517 60493ae3 2019-06-20 stsp return NULL;
2520 60493ae3 2019-06-20 stsp static const struct got_error *
2521 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2523 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2524 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2525 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2527 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2528 f9686aa5 2020-03-27 stsp show_log_view(view);
2529 f9686aa5 2020-03-27 stsp update_panels();
2530 f9686aa5 2020-03-27 stsp doupdate();
2532 96e2b566 2019-07-08 stsp if (s->search_entry) {
2533 13add988 2019-10-15 stsp int errcode, ch;
2534 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2535 13add988 2019-10-15 stsp if (errcode)
2536 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2537 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2538 13add988 2019-10-15 stsp ch = wgetch(view->window);
2539 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2540 13add988 2019-10-15 stsp if (errcode)
2541 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2542 13add988 2019-10-15 stsp "pthread_mutex_lock");
2543 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2544 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2545 678cbce5 2019-07-28 stsp return NULL;
2547 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2548 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2550 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2551 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2552 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2553 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2554 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2557 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2558 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2559 f704b35c 2022-06-18 stsp * might have changed.
2561 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2562 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2563 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2565 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2567 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2568 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2569 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2571 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2572 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2575 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2578 60493ae3 2019-06-20 stsp while (1) {
2579 13add988 2019-10-15 stsp int have_match = 0;
2581 60493ae3 2019-06-20 stsp if (entry == NULL) {
2582 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2583 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2584 f9967bca 2020-03-27 stsp view->search_next_done =
2585 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2586 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2587 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2588 f801134a 2019-06-25 stsp return NULL;
2591 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2592 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2593 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2595 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2596 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2599 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2600 13add988 2019-10-15 stsp &view->regex);
2603 13add988 2019-10-15 stsp if (have_match) {
2604 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2605 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2609 96e2b566 2019-07-08 stsp s->search_entry = entry;
2610 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2611 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2613 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2616 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2617 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2618 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2619 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2621 60493ae3 2019-06-20 stsp return err;
2624 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2625 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2627 60493ae3 2019-06-20 stsp return err;
2632 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2634 60493ae3 2019-06-20 stsp return NULL;
2637 60493ae3 2019-06-20 stsp static const struct got_error *
2638 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2639 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2640 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2642 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2643 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2644 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2645 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2646 1a76625f 2018-10-22 stsp int errcode;
2648 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2649 f135c941 2020-02-20 stsp free(s->in_repo_path);
2650 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2651 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2652 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2655 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2656 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2657 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2659 fb2756b9 2018-08-04 stsp s->repo = repo;
2660 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2661 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2662 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2663 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2664 9cd7cbd1 2020-12-07 stsp goto done;
2667 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2668 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2669 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2670 5036bf37 2018-09-24 stsp goto done;
2672 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2674 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2675 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2676 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2677 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2679 11b20872 2019-11-08 stsp goto done;
2680 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2681 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2682 11b20872 2019-11-08 stsp if (err) {
2683 11b20872 2019-11-08 stsp free_colors(&s->colors);
2684 11b20872 2019-11-08 stsp goto done;
2686 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2687 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2688 11b20872 2019-11-08 stsp if (err) {
2689 11b20872 2019-11-08 stsp free_colors(&s->colors);
2690 11b20872 2019-11-08 stsp goto done;
2694 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2695 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2696 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2697 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2698 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2700 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2701 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2703 74467cc8 2022-06-15 stsp goto done;
2705 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2706 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2707 0ae84acc 2022-06-15 tracey if (err)
2708 0ae84acc 2022-06-15 tracey goto done;
2709 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2710 b672a97a 2020-01-27 stsp !s->log_branches);
2712 1a76625f 2018-10-22 stsp goto done;
2713 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2714 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2716 c5b78334 2020-01-12 stsp goto done;
2718 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2719 1a76625f 2018-10-22 stsp if (errcode) {
2720 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2721 1a76625f 2018-10-22 stsp goto done;
2723 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2724 7c1452c1 2020-03-26 stsp if (errcode) {
2725 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2726 7c1452c1 2020-03-26 stsp goto done;
2729 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2730 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2731 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2732 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2733 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2734 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2735 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2736 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2737 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2738 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2739 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2740 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2741 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2744 1a76625f 2018-10-22 stsp close_log_view(view);
2745 ba4f502b 2018-08-04 stsp return err;
2748 e5a0f69f 2018-08-18 stsp static const struct got_error *
2749 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2751 f2f6d207 2020-11-24 stsp const struct got_error *err;
2752 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2754 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2755 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2756 2b380cc8 2018-10-24 stsp &s->thread_args);
2757 2b380cc8 2018-10-24 stsp if (errcode)
2758 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2759 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2760 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2762 f2f6d207 2020-11-24 stsp return err;
2766 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2769 b880cc75 2022-06-30 mark static void
2770 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
2772 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2774 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
2775 b880cc75 2022-06-30 mark view->count = 0;
2776 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
2779 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2781 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2783 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
2784 b880cc75 2022-06-30 mark --s->selected;
2786 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2788 b880cc75 2022-06-30 mark select_commit(s);
2792 b880cc75 2022-06-30 mark static const struct got_error *
2793 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
2795 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2796 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
2797 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
2799 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
2800 b880cc75 2022-06-30 mark if (first == NULL) {
2801 b880cc75 2022-06-30 mark view->count = 0;
2802 b880cc75 2022-06-30 mark return NULL;
2805 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2806 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
2807 b880cc75 2022-06-30 mark return NULL;
2809 b880cc75 2022-06-30 mark if (!page) {
2810 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
2812 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2813 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
2814 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
2815 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
2816 b880cc75 2022-06-30 mark ++s->selected;
2818 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
2819 b880cc75 2022-06-30 mark } else if (s->thread_args.log_complete) {
2820 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2821 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
2822 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
2824 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
2825 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
2826 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2828 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
2830 b880cc75 2022-06-30 mark return err;
2831 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
2832 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2833 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
2837 b880cc75 2022-06-30 mark return err;
2840 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
2841 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
2843 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
2844 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2846 b880cc75 2022-06-30 mark select_commit(s);
2848 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2849 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
2850 b880cc75 2022-06-30 mark view->count = 0;
2852 b880cc75 2022-06-30 mark return NULL;
2856 9b058f45 2022-06-30 mark * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2857 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2858 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_HRZN horizontal split
2859 9b058f45 2022-06-30 mark * Assign start column and line of the new split to *x and *y, respectively,
2860 9b058f45 2022-06-30 mark * and assign view mode to view->mode.
2862 9b058f45 2022-06-30 mark static void
2863 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
2865 9b058f45 2022-06-30 mark char *mode;
2867 9b058f45 2022-06-30 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
2869 9b058f45 2022-06-30 mark if (!mode || mode[0] != 'h') {
2870 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_VERT;
2871 9b058f45 2022-06-30 mark *x = view_split_begin_x(view->begin_x);
2872 9b058f45 2022-06-30 mark } else if (mode && mode[0] == 'h') {
2873 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_HRZN;
2874 9b058f45 2022-06-30 mark *y = view_split_begin_y(view->lines);
2878 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
2879 e5a0f69f 2018-08-18 stsp static const struct got_error *
2880 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
2882 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2884 9b058f45 2022-06-30 mark view->nlines = y;
2885 9b058f45 2022-06-30 mark err = view_resize(view);
2887 9b058f45 2022-06-30 mark return err;
2889 9b058f45 2022-06-30 mark err = offset_selection_down(view);
2891 9b058f45 2022-06-30 mark return err;
2894 9b058f45 2022-06-30 mark static const struct got_error *
2895 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2897 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2898 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2899 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2900 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2901 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2902 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0, n, nscroll = view->nlines - 1;
2904 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2905 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2906 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2907 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2908 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2909 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
2911 b880cc75 2022-06-30 mark return err;
2914 528dedf3 2021-08-30 stsp switch (ch) {
2916 1e37a5c2 2019-05-12 jcs s->quit = 1;
2919 145b6838 2022-06-16 stsp view->x = 0;
2922 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2923 640cd7ff 2022-06-22 mark view->count = 0;
2925 145b6838 2022-06-16 stsp case KEY_RIGHT:
2927 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2928 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2930 640cd7ff 2022-06-22 mark view->count = 0;
2932 145b6838 2022-06-16 stsp case KEY_LEFT:
2934 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2935 640cd7ff 2022-06-22 mark if (view->x <= 0)
2936 640cd7ff 2022-06-22 mark view->count = 0;
2939 1e37a5c2 2019-05-12 jcs case KEY_UP:
2942 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2943 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
2946 912a3f79 2021-08-30 j case KEY_HOME:
2947 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
2948 640cd7ff 2022-06-22 mark view->count = 0;
2950 83cc4199 2022-06-13 stsp case CTRL('u'):
2952 83cc4199 2022-06-13 stsp nscroll /= 2;
2953 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2954 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2955 a4292ac5 2019-05-12 jcs case CTRL('b'):
2957 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
2960 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2963 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2964 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
2967 912a3f79 2021-08-30 j case KEY_END: {
2968 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2969 912a3f79 2021-08-30 j * traverse them all. */
2970 640cd7ff 2022-06-22 mark view->count = 0;
2971 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2972 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2973 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2976 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2977 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2978 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2979 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2981 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2982 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2984 f3bc9f1d 2021-09-05 naddy if (n > 0)
2985 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2986 2b779855 2020-12-05 naddy select_commit(s);
2989 80b7e8da 2022-06-11 stsp case CTRL('d'):
2991 83cc4199 2022-06-13 stsp nscroll /= 2;
2992 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2993 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2994 61417565 2022-06-20 mark case CTRL('f'):
2995 48bb96f0 2022-06-20 naddy case 'f':
2997 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
2999 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3000 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3001 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3002 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3003 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3004 2b779855 2020-12-05 naddy select_commit(s);
3005 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3006 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3007 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3008 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3009 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3012 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3013 9b058f45 2022-06-30 mark case '\r': {
3014 640cd7ff 2022-06-22 mark view->count = 0;
3015 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3018 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3019 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3020 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3022 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3023 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3024 78756c87 2020-11-24 stsp view, s->repo);
3028 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3029 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3034 e78dc838 2020-12-04 stsp view->focussed = 0;
3035 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3036 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3037 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3039 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3040 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3042 1e37a5c2 2019-05-12 jcs return err;
3043 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3046 e78dc838 2020-12-04 stsp view->focus_child = 1;
3048 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3052 640cd7ff 2022-06-22 mark view->count = 0;
3053 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3055 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3056 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3057 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
3058 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3062 e78dc838 2020-12-04 stsp view->focussed = 0;
3063 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3064 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3065 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3067 1e37a5c2 2019-05-12 jcs return err;
3068 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3071 e78dc838 2020-12-04 stsp view->focus_child = 1;
3073 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3075 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3076 21355643 2020-12-06 stsp case CTRL('l'):
3078 640cd7ff 2022-06-22 mark view->count = 0;
3079 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3080 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3082 21355643 2020-12-06 stsp err = stop_log_thread(s);
3084 74cfe85e 2020-10-20 stsp return err;
3085 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3086 21355643 2020-12-06 stsp char *parent_path;
3087 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3089 21355643 2020-12-06 stsp return err;
3090 21355643 2020-12-06 stsp free(s->in_repo_path);
3091 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3092 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3093 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3094 21355643 2020-12-06 stsp struct got_object_id *start_id;
3095 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3096 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3097 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3099 21355643 2020-12-06 stsp return err;
3100 21355643 2020-12-06 stsp free(s->start_id);
3101 21355643 2020-12-06 stsp s->start_id = start_id;
3102 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3103 21355643 2020-12-06 stsp } else /* 'B' */
3104 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3106 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3107 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3109 b0dd8d27 2022-06-16 stsp return err;
3111 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3112 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3113 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3115 21355643 2020-12-06 stsp return err;
3116 51a10b52 2020-12-26 stsp tog_free_refs();
3117 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3119 ca51c541 2020-12-07 stsp return err;
3120 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3121 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3123 d01904d4 2019-06-25 stsp return err;
3124 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3125 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3127 b672a97a 2020-01-27 stsp return err;
3128 21355643 2020-12-06 stsp free_commits(&s->commits);
3129 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3130 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3131 21355643 2020-12-06 stsp s->selected_entry = NULL;
3132 21355643 2020-12-06 stsp s->selected = 0;
3133 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3134 21355643 2020-12-06 stsp s->quit = 0;
3135 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3136 dfee752e 2022-06-18 op s->matched_entry = NULL;
3137 dfee752e 2022-06-18 op s->search_entry = NULL;
3140 640cd7ff 2022-06-22 mark view->count = 0;
3141 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3142 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
3143 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
3144 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
3145 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3146 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3147 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3148 6458efa5 2020-11-24 stsp if (err) {
3149 6458efa5 2020-11-24 stsp view_close(ref_view);
3150 6458efa5 2020-11-24 stsp return err;
3152 e78dc838 2020-12-04 stsp view->focussed = 0;
3153 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3154 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3155 6458efa5 2020-11-24 stsp err = view_close_child(view);
3157 6458efa5 2020-11-24 stsp return err;
3158 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3161 e78dc838 2020-12-04 stsp view->focus_child = 1;
3163 6458efa5 2020-11-24 stsp *new_view = ref_view;
3166 640cd7ff 2022-06-22 mark view->count = 0;
3170 80ddbec8 2018-04-29 stsp return err;
3173 4ed7e80c 2018-05-20 stsp static const struct got_error *
3174 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3176 c2db6724 2019-01-04 stsp const struct got_error *error;
3178 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3179 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3180 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3182 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3183 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3185 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3186 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3188 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3189 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3191 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3192 c2db6724 2019-01-04 stsp if (error != NULL)
3193 c2db6724 2019-01-04 stsp return error;
3195 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3196 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3198 c2db6724 2019-01-04 stsp return NULL;
3201 a915003a 2019-02-05 stsp static void
3202 a915003a 2019-02-05 stsp init_curses(void)
3205 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3206 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3207 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3209 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3210 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3211 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3212 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3213 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3215 a915003a 2019-02-05 stsp initscr();
3217 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3220 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3221 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3222 a915003a 2019-02-05 stsp curs_set(0);
3223 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3224 6d17833f 2019-11-08 stsp start_color();
3225 6d17833f 2019-11-08 stsp use_default_colors();
3229 c2db6724 2019-01-04 stsp static const struct got_error *
3230 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3231 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3233 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3235 f135c941 2020-02-20 stsp if (argc == 0) {
3236 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3237 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3238 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3239 f135c941 2020-02-20 stsp return NULL;
3242 f135c941 2020-02-20 stsp if (worktree) {
3243 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3246 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3248 f135c941 2020-02-20 stsp return err;
3249 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3250 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3251 bfd61697 2020-11-14 stsp p) == -1) {
3252 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3253 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3257 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3259 f135c941 2020-02-20 stsp return err;
3262 f135c941 2020-02-20 stsp static const struct got_error *
3263 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3265 80ddbec8 2018-04-29 stsp const struct got_error *error;
3266 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3267 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3268 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3269 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3270 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3271 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3272 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3273 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3274 04cc582a 2018-08-01 stsp struct tog_view *view;
3275 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3277 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3278 80ddbec8 2018-04-29 stsp switch (ch) {
3280 b672a97a 2020-01-27 stsp log_branches = 1;
3283 80ddbec8 2018-04-29 stsp start_commit = optarg;
3286 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3287 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3288 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3292 17020d27 2019-03-07 stsp usage_log();
3293 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3297 80ddbec8 2018-04-29 stsp argc -= optind;
3298 80ddbec8 2018-04-29 stsp argv += optind;
3300 f135c941 2020-02-20 stsp if (argc > 1)
3301 f135c941 2020-02-20 stsp usage_log();
3303 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3304 0ae84acc 2022-06-15 tracey if (error != NULL)
3305 0ae84acc 2022-06-15 tracey goto done;
3307 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3308 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3309 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3310 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3311 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3312 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3313 c156c7a4 2020-12-18 stsp goto done;
3314 a1fbf39a 2019-08-11 stsp if (worktree)
3315 6962eb72 2020-02-20 stsp repo_path =
3316 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3318 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3319 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3320 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3321 c156c7a4 2020-12-18 stsp goto done;
3325 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3326 80ddbec8 2018-04-29 stsp if (error != NULL)
3327 ecb28ae0 2018-07-16 stsp goto done;
3329 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3330 f135c941 2020-02-20 stsp repo, worktree);
3331 f135c941 2020-02-20 stsp if (error)
3332 f135c941 2020-02-20 stsp goto done;
3334 f135c941 2020-02-20 stsp init_curses();
3336 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3337 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3338 c02c541e 2019-03-29 stsp if (error)
3339 c02c541e 2019-03-29 stsp goto done;
3341 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3342 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3343 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3344 87670572 2020-12-26 naddy if (error)
3345 87670572 2020-12-26 naddy goto done;
3348 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3349 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3350 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3351 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3352 d8f38dc4 2020-12-05 stsp if (error)
3353 d8f38dc4 2020-12-05 stsp goto done;
3354 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3356 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3357 d8f38dc4 2020-12-05 stsp if (error == NULL)
3358 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3359 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3360 d8f38dc4 2020-12-05 stsp goto done;
3361 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3362 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3363 d8f38dc4 2020-12-05 stsp if (error)
3364 d8f38dc4 2020-12-05 stsp goto done;
3367 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3368 04cc582a 2018-08-01 stsp if (view == NULL) {
3369 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3370 04cc582a 2018-08-01 stsp goto done;
3372 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3373 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3374 ba4f502b 2018-08-04 stsp if (error)
3375 ba4f502b 2018-08-04 stsp goto done;
3376 2fc00ff4 2019-08-31 stsp if (worktree) {
3377 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3378 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3379 2fc00ff4 2019-08-31 stsp worktree = NULL;
3381 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3383 f135c941 2020-02-20 stsp free(in_repo_path);
3384 ecb28ae0 2018-07-16 stsp free(repo_path);
3385 ecb28ae0 2018-07-16 stsp free(cwd);
3386 899d86c2 2018-05-10 stsp free(start_id);
3387 d8f38dc4 2020-12-05 stsp free(label);
3389 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3390 1d0f4054 2021-06-17 stsp if (repo) {
3391 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3392 1d0f4054 2021-06-17 stsp if (error == NULL)
3393 1d0f4054 2021-06-17 stsp error = close_err;
3395 ec142235 2019-03-07 stsp if (worktree)
3396 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3397 0ae84acc 2022-06-15 tracey if (pack_fds) {
3398 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3399 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3400 0ae84acc 2022-06-15 tracey if (error == NULL)
3401 0ae84acc 2022-06-15 tracey error = pack_err;
3403 51a10b52 2020-12-26 stsp tog_free_refs();
3404 80ddbec8 2018-04-29 stsp return error;
3407 4ed7e80c 2018-05-20 stsp __dead static void
3408 9f7d7167 2018-04-29 stsp usage_diff(void)
3411 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3412 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3416 6d17833f 2019-11-08 stsp static int
3417 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3418 41605754 2020-11-12 stsp regmatch_t *regmatch)
3420 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3423 336075a4 2022-06-25 op static struct tog_color *
3424 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3426 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3428 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3429 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3430 6d17833f 2019-11-08 stsp return tc;
3433 6d17833f 2019-11-08 stsp return NULL;
3436 4ed7e80c 2018-05-20 stsp static const struct got_error *
3437 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3438 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3440 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3441 44a87665 2022-06-16 stsp char *exstr = NULL;
3442 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3443 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3444 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3445 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3447 41605754 2020-11-12 stsp *wtotal = 0;
3449 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3450 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3452 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3454 44a87665 2022-06-16 stsp return err;
3456 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3457 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3458 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3459 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3460 44a87665 2022-06-16 stsp goto done;
3462 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3463 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3464 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3465 1853e0f4 2022-06-16 stsp goto done;
3467 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3468 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3469 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3470 1853e0f4 2022-06-16 stsp goto done;
3473 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3474 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3475 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3477 1853e0f4 2022-06-16 stsp goto done;
3478 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3480 1853e0f4 2022-06-16 stsp free(wline);
3481 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3482 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3484 1853e0f4 2022-06-16 stsp goto done;
3485 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3486 1853e0f4 2022-06-16 stsp wlimit -= width;
3487 1853e0f4 2022-06-16 stsp *wtotal += width;
3490 41605754 2020-11-12 stsp if (wlimit > 0) {
3491 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3492 1853e0f4 2022-06-16 stsp size_t wlen;
3494 1853e0f4 2022-06-16 stsp free(wline);
3495 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3496 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3498 1853e0f4 2022-06-16 stsp goto done;
3499 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3500 1853e0f4 2022-06-16 stsp while (i < wlen) {
3501 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3502 1853e0f4 2022-06-16 stsp if (width == -1) {
3503 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3504 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3505 1853e0f4 2022-06-16 stsp goto done;
3507 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3509 61417565 2022-06-20 mark w += width;
3512 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3513 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3514 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3515 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3516 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3517 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3518 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3522 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3523 1853e0f4 2022-06-16 stsp free(wline);
3524 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3525 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3526 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3527 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3529 1853e0f4 2022-06-16 stsp goto done;
3530 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3532 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3533 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3535 1853e0f4 2022-06-16 stsp goto done;
3536 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3538 1853e0f4 2022-06-16 stsp *wtotal += width2;
3541 145b6838 2022-06-16 stsp free(wline);
3542 44a87665 2022-06-16 stsp free(exstr);
3543 1853e0f4 2022-06-16 stsp free(seg0);
3544 1853e0f4 2022-06-16 stsp free(seg1);
3545 1853e0f4 2022-06-16 stsp free(seg2);
3546 1853e0f4 2022-06-16 stsp return err;
3549 41605754 2020-11-12 stsp static const struct got_error *
3550 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3552 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3553 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3554 61e69b96 2018-05-20 stsp const struct got_error *err;
3555 fe621944 2020-11-10 stsp int nprinted = 0;
3556 b304db33 2018-05-20 stsp char *line;
3557 826082fe 2020-12-10 stsp size_t linesize = 0;
3558 826082fe 2020-12-10 stsp ssize_t linelen;
3559 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3560 61e69b96 2018-05-20 stsp wchar_t *wline;
3561 e0b650dd 2018-05-20 stsp int width;
3562 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3563 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3564 fe621944 2020-11-10 stsp off_t line_offset;
3566 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3567 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3568 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3570 f7d12f7e 2018-08-01 stsp werase(view->window);
3572 a3404814 2018-09-02 stsp if (header) {
3573 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3574 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3575 135a2da0 2020-11-11 stsp header) == -1)
3576 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3577 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3579 135a2da0 2020-11-11 stsp free(line);
3581 a3404814 2018-09-02 stsp return err;
3583 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3584 a3404814 2018-09-02 stsp wstandout(view->window);
3585 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3586 e54cc94a 2020-11-11 stsp free(wline);
3587 e54cc94a 2020-11-11 stsp wline = NULL;
3588 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3589 a3404814 2018-09-02 stsp wstandend(view->window);
3590 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3591 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3593 a3404814 2018-09-02 stsp if (max_lines <= 1)
3594 a3404814 2018-09-02 stsp return NULL;
3595 a3404814 2018-09-02 stsp max_lines--;
3598 89f1a395 2020-12-01 naddy s->eof = 0;
3599 145b6838 2022-06-16 stsp view->maxx = 0;
3600 826082fe 2020-12-10 stsp line = NULL;
3601 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3602 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3603 826082fe 2020-12-10 stsp if (linelen == -1) {
3604 826082fe 2020-12-10 stsp if (feof(s->f)) {
3605 826082fe 2020-12-10 stsp s->eof = 1;
3608 826082fe 2020-12-10 stsp free(line);
3609 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3612 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3613 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3614 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3615 1853e0f4 2022-06-16 stsp if (err) {
3616 1853e0f4 2022-06-16 stsp free(line);
3617 1853e0f4 2022-06-16 stsp return err;
3619 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3620 1853e0f4 2022-06-16 stsp free(wline);
3621 1853e0f4 2022-06-16 stsp wline = NULL;
3623 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3625 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3626 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3627 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3628 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3629 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3630 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3631 41605754 2020-11-12 stsp if (err) {
3632 41605754 2020-11-12 stsp free(line);
3633 41605754 2020-11-12 stsp return err;
3637 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3638 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3639 969c159c 2022-06-16 stsp if (err) {
3640 969c159c 2022-06-16 stsp free(line);
3641 969c159c 2022-06-16 stsp return err;
3643 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3644 969c159c 2022-06-16 stsp free(wline);
3645 41605754 2020-11-12 stsp wline = NULL;
3648 6d17833f 2019-11-08 stsp wattr_off(view->window,
3649 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3650 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3651 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3652 fe621944 2020-11-10 stsp nprinted++;
3654 826082fe 2020-12-10 stsp free(line);
3655 fe621944 2020-11-10 stsp if (nprinted >= 1)
3656 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3657 89f1a395 2020-12-01 naddy (nprinted - 1);
3659 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3661 9b058f45 2022-06-30 mark view_border(view);
3663 89f1a395 2020-12-01 naddy if (s->eof) {
3664 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3665 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3666 c3e9aa98 2019-05-13 jcs nprinted++;
3669 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3670 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3672 c3e9aa98 2019-05-13 jcs return err;
3675 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3676 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3677 e54cc94a 2020-11-11 stsp free(wline);
3678 e54cc94a 2020-11-11 stsp wline = NULL;
3679 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3682 26ed57b2 2018-05-19 stsp return NULL;
3685 abd2672a 2018-12-23 stsp static char *
3686 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3688 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3689 09867e48 2019-08-13 stsp char *p, *s;
3691 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3692 09867e48 2019-08-13 stsp if (tm == NULL)
3693 09867e48 2019-08-13 stsp return NULL;
3694 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3695 09867e48 2019-08-13 stsp if (s == NULL)
3696 09867e48 2019-08-13 stsp return NULL;
3697 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3699 abd2672a 2018-12-23 stsp *p = '\0';
3703 4ed7e80c 2018-05-20 stsp static const struct got_error *
3704 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3705 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3707 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3708 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3709 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3710 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3712 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3713 0208f208 2020-05-05 stsp if (qid != NULL) {
3714 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3715 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3716 d7b5a0e8 2022-04-20 stsp &qid->id);
3718 0208f208 2020-05-05 stsp return err;
3720 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3721 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3722 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3723 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3724 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3726 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3730 0208f208 2020-05-05 stsp if (tree_id1) {
3731 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3733 0208f208 2020-05-05 stsp goto done;
3736 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3737 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3739 0208f208 2020-05-05 stsp goto done;
3741 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3742 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3744 0208f208 2020-05-05 stsp if (tree1)
3745 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3746 0208f208 2020-05-05 stsp if (tree2)
3747 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3748 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3749 0208f208 2020-05-05 stsp return err;
3752 0208f208 2020-05-05 stsp static const struct got_error *
3753 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3757 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3758 fe621944 2020-11-10 stsp if (p == NULL)
3759 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3760 fe621944 2020-11-10 stsp *line_offsets = p;
3761 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3762 fe621944 2020-11-10 stsp (*nlines)++;
3763 fe621944 2020-11-10 stsp return NULL;
3766 fe621944 2020-11-10 stsp static const struct got_error *
3767 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3768 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3769 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3771 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3772 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3773 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3774 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3775 45d799e2 2018-12-23 stsp time_t committer_time;
3776 45d799e2 2018-12-23 stsp const char *author, *committer;
3777 8b473291 2019-02-21 stsp char *refs_str = NULL;
3778 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3779 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3780 fe621944 2020-11-10 stsp off_t outoff = 0;
3783 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3785 8b473291 2019-02-21 stsp if (refs) {
3786 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3788 8b473291 2019-02-21 stsp return err;
3791 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3793 abd2672a 2018-12-23 stsp return err;
3795 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3796 15a94983 2018-12-23 stsp if (err) {
3797 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3798 15a94983 2018-12-23 stsp goto done;
3801 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3803 fe621944 2020-11-10 stsp goto done;
3805 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3806 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3807 fe621944 2020-11-10 stsp if (n < 0) {
3808 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3809 abd2672a 2018-12-23 stsp goto done;
3811 fe621944 2020-11-10 stsp outoff += n;
3812 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3814 fe621944 2020-11-10 stsp goto done;
3816 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3817 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3818 fe621944 2020-11-10 stsp if (n < 0) {
3819 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3820 abd2672a 2018-12-23 stsp goto done;
3822 fe621944 2020-11-10 stsp outoff += n;
3823 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3825 fe621944 2020-11-10 stsp goto done;
3827 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3828 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3829 fe621944 2020-11-10 stsp if (datestr) {
3830 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3831 fe621944 2020-11-10 stsp if (n < 0) {
3832 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3833 fe621944 2020-11-10 stsp goto done;
3835 fe621944 2020-11-10 stsp outoff += n;
3836 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3838 fe621944 2020-11-10 stsp goto done;
3840 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3841 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3842 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3843 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3844 fe621944 2020-11-10 stsp if (n < 0) {
3845 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3846 fe621944 2020-11-10 stsp goto done;
3848 fe621944 2020-11-10 stsp outoff += n;
3849 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3851 fe621944 2020-11-10 stsp goto done;
3853 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3854 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3855 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3856 9f98ca05 2021-09-24 stsp int pn = 1;
3857 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3858 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3859 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3861 9f98ca05 2021-09-24 stsp goto done;
3862 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3863 9f98ca05 2021-09-24 stsp if (n < 0) {
3864 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3865 9f98ca05 2021-09-24 stsp goto done;
3867 9f98ca05 2021-09-24 stsp outoff += n;
3868 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3870 9f98ca05 2021-09-24 stsp goto done;
3871 9f98ca05 2021-09-24 stsp free(id_str);
3872 9f98ca05 2021-09-24 stsp id_str = NULL;
3876 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3878 5943eee2 2019-08-13 stsp goto done;
3879 fe621944 2020-11-10 stsp s = logmsg;
3880 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3881 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3882 fe621944 2020-11-10 stsp if (n < 0) {
3883 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3884 fe621944 2020-11-10 stsp goto done;
3886 fe621944 2020-11-10 stsp outoff += n;
3887 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3889 fe621944 2020-11-10 stsp goto done;
3892 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3894 0208f208 2020-05-05 stsp goto done;
3895 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3896 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3897 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3898 fe621944 2020-11-10 stsp if (n < 0) {
3899 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3900 fe621944 2020-11-10 stsp goto done;
3902 fe621944 2020-11-10 stsp outoff += n;
3903 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3905 fe621944 2020-11-10 stsp goto done;
3906 0208f208 2020-05-05 stsp free((char *)pe->path);
3907 0208f208 2020-05-05 stsp free(pe->data);
3910 0208f208 2020-05-05 stsp fputc('\n', outfile);
3912 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3914 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3915 abd2672a 2018-12-23 stsp free(id_str);
3916 5943eee2 2019-08-13 stsp free(logmsg);
3917 8b473291 2019-02-21 stsp free(refs_str);
3918 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3919 7510f233 2020-08-09 stsp if (err) {
3920 ae6a6978 2020-08-09 stsp free(*line_offsets);
3921 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3922 ae6a6978 2020-08-09 stsp *nlines = 0;
3924 fe621944 2020-11-10 stsp return err;
3927 abd2672a 2018-12-23 stsp static const struct got_error *
3928 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3930 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3931 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3932 15a94983 2018-12-23 stsp int obj_type;
3934 fe621944 2020-11-10 stsp free(s->line_offsets);
3935 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3936 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3937 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3938 fe621944 2020-11-10 stsp s->nlines = 0;
3940 511a516b 2018-05-19 stsp f = got_opentemp();
3941 48ae06ee 2018-10-18 stsp if (f == NULL) {
3942 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3943 48ae06ee 2018-10-18 stsp goto done;
3945 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3946 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3947 fb43ecf1 2019-02-11 stsp goto done;
3951 15a94983 2018-12-23 stsp if (s->id1)
3952 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3954 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3956 15a94983 2018-12-23 stsp goto done;
3958 15a94983 2018-12-23 stsp switch (obj_type) {
3959 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3960 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3961 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3962 f9d37699 2022-06-28 stsp s->label1, s->label2, s->diff_context,
3963 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3965 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3966 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3967 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3968 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3969 f9d37699 2022-06-28 stsp s->repo, s->f);
3971 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3972 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3973 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3974 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3975 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3977 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3979 3ffacbe1 2020-02-02 tracey goto done;
3980 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3981 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3982 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3983 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3984 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3985 f44b1f58 2020-02-02 tracey if (err)
3986 f44b1f58 2020-02-02 tracey goto done;
3987 f44b1f58 2020-02-02 tracey } else {
3988 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3989 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3990 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3991 fe621944 2020-11-10 stsp err = write_commit_info(
3992 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3993 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3994 f44b1f58 2020-02-02 tracey if (err)
3995 f44b1f58 2020-02-02 tracey goto done;
4000 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4002 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4003 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4004 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
4005 f9d37699 2022-06-28 stsp s->repo, s->f);
4009 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4012 f44b1f58 2020-02-02 tracey if (err)
4013 f44b1f58 2020-02-02 tracey goto done;
4015 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4016 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4017 48ae06ee 2018-10-18 stsp return err;
4020 f5215bb9 2019-02-22 stsp static void
4021 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4023 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4024 f5215bb9 2019-02-22 stsp update_panels();
4025 f5215bb9 2019-02-22 stsp doupdate();
4028 f44b1f58 2020-02-02 tracey static const struct got_error *
4029 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4031 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4033 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4034 f44b1f58 2020-02-02 tracey return NULL;
4037 f44b1f58 2020-02-02 tracey static const struct got_error *
4038 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4040 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4041 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4042 f44b1f58 2020-02-02 tracey int lineno;
4043 cb713507 2022-06-16 stsp char *line = NULL;
4044 826082fe 2020-12-10 stsp size_t linesize = 0;
4045 826082fe 2020-12-10 stsp ssize_t linelen;
4047 f44b1f58 2020-02-02 tracey if (!view->searching) {
4048 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4049 f44b1f58 2020-02-02 tracey return NULL;
4052 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4053 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4054 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4056 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4058 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4060 f44b1f58 2020-02-02 tracey while (1) {
4061 f44b1f58 2020-02-02 tracey off_t offset;
4063 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4064 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4065 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4069 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4070 f44b1f58 2020-02-02 tracey lineno = 1;
4072 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4075 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4076 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4077 f44b1f58 2020-02-02 tracey free(line);
4078 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4080 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4081 cb713507 2022-06-16 stsp if (linelen != -1) {
4082 cb713507 2022-06-16 stsp char *exstr;
4083 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4086 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4087 cb713507 2022-06-16 stsp &view->regmatch)) {
4088 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4089 cb713507 2022-06-16 stsp s->matched_line = lineno;
4090 cb713507 2022-06-16 stsp free(exstr);
4093 cb713507 2022-06-16 stsp free(exstr);
4095 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4096 f44b1f58 2020-02-02 tracey lineno++;
4098 f44b1f58 2020-02-02 tracey lineno--;
4100 826082fe 2020-12-10 stsp free(line);
4102 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4103 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4104 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4107 145b6838 2022-06-16 stsp return err;
4110 48ae06ee 2018-10-18 stsp static const struct got_error *
4111 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4113 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4114 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4116 b72706c3 2022-06-01 stsp free(s->id1);
4117 b72706c3 2022-06-01 stsp s->id1 = NULL;
4118 b72706c3 2022-06-01 stsp free(s->id2);
4119 b72706c3 2022-06-01 stsp s->id2 = NULL;
4120 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4121 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4122 b72706c3 2022-06-01 stsp s->f = NULL;
4123 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4124 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4125 b72706c3 2022-06-01 stsp s->f1 = NULL;
4126 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4127 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4128 b72706c3 2022-06-01 stsp s->f2 = NULL;
4129 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4130 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4131 f9d37699 2022-06-28 stsp s->fd1 = -1;
4132 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4133 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4134 f9d37699 2022-06-28 stsp s->fd2 = -1;
4135 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4136 b72706c3 2022-06-01 stsp free(s->line_offsets);
4137 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4138 b72706c3 2022-06-01 stsp s->nlines = 0;
4139 b72706c3 2022-06-01 stsp return err;
4142 b72706c3 2022-06-01 stsp static const struct got_error *
4143 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4144 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4145 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4146 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4148 48ae06ee 2018-10-18 stsp const struct got_error *err;
4149 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4151 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4152 f9d37699 2022-06-28 stsp s->fd1 = -1;
4153 f9d37699 2022-06-28 stsp s->fd2 = -1;
4155 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4156 15a94983 2018-12-23 stsp int type1, type2;
4157 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4159 15a94983 2018-12-23 stsp return err;
4160 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4162 15a94983 2018-12-23 stsp return err;
4164 15a94983 2018-12-23 stsp if (type1 != type2)
4165 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4167 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4168 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4169 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4170 f44b1f58 2020-02-02 tracey s->repo = repo;
4171 f44b1f58 2020-02-02 tracey s->id1 = id1;
4172 f44b1f58 2020-02-02 tracey s->id2 = id2;
4173 3dbaef42 2020-11-24 stsp s->label1 = label1;
4174 3dbaef42 2020-11-24 stsp s->label2 = label2;
4176 15a94983 2018-12-23 stsp if (id1) {
4177 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4178 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4179 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4181 5465d566 2020-02-01 tracey s->id1 = NULL;
4183 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4184 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4185 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4186 b72706c3 2022-06-01 stsp goto done;
4189 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4190 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4191 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4192 a00719e9 2022-06-17 stsp goto done;
4195 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4196 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4197 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4198 b72706c3 2022-06-01 stsp goto done;
4201 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4202 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4203 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4204 f9d37699 2022-06-28 stsp goto done;
4207 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4208 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4209 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4210 f9d37699 2022-06-28 stsp goto done;
4213 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4214 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4215 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4216 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4217 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4218 5465d566 2020-02-01 tracey s->log_view = log_view;
4219 5465d566 2020-02-01 tracey s->repo = repo;
4221 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4222 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4223 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4224 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4225 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4227 b72706c3 2022-06-01 stsp goto done;
4228 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4229 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4230 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4232 b72706c3 2022-06-01 stsp goto done;
4233 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4234 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4235 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4237 b72706c3 2022-06-01 stsp goto done;
4239 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4240 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4241 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4242 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4243 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4245 b72706c3 2022-06-01 stsp goto done;
4247 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4248 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4249 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4251 b72706c3 2022-06-01 stsp goto done;
4253 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4254 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4255 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4257 b72706c3 2022-06-01 stsp goto done;
4260 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4261 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4262 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4264 5465d566 2020-02-01 tracey err = create_diff(s);
4266 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4267 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4268 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4269 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4270 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4273 b72706c3 2022-06-01 stsp close_diff_view(view);
4274 e5a0f69f 2018-08-18 stsp return err;
4277 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4278 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4280 a3404814 2018-09-02 stsp const struct got_error *err;
4281 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4282 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4283 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4285 a3404814 2018-09-02 stsp if (s->id1) {
4286 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4288 a3404814 2018-09-02 stsp return err;
4289 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4291 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4293 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4295 a3404814 2018-09-02 stsp return err;
4296 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4298 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4299 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4300 a3404814 2018-09-02 stsp free(id_str1);
4301 a3404814 2018-09-02 stsp free(id_str2);
4302 a3404814 2018-09-02 stsp return err;
4304 a3404814 2018-09-02 stsp free(id_str1);
4305 a3404814 2018-09-02 stsp free(id_str2);
4307 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4308 267bb3b8 2021-08-01 stsp free(header);
4309 267bb3b8 2021-08-01 stsp return err;
4312 15a087fe 2019-02-21 stsp static const struct got_error *
4313 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4314 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4316 d7a04538 2019-02-21 stsp const struct got_error *err;
4317 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4318 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4319 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4321 15a087fe 2019-02-21 stsp free(s->id2);
4322 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4323 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4324 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4326 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4328 d7a04538 2019-02-21 stsp return err;
4329 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4330 15a087fe 2019-02-21 stsp free(s->id1);
4331 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4332 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4333 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4334 15a087fe 2019-02-21 stsp return NULL;
4337 0cf4efb1 2018-09-29 stsp static const struct got_error *
4338 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4340 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4341 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4342 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4343 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4344 826082fe 2020-12-10 stsp char *line = NULL;
4345 826082fe 2020-12-10 stsp size_t linesize = 0;
4346 826082fe 2020-12-10 stsp ssize_t linelen;
4347 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4349 e5a0f69f 2018-08-18 stsp switch (ch) {
4351 145b6838 2022-06-16 stsp view->x = 0;
4354 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4355 640cd7ff 2022-06-22 mark view->count = 0;
4357 145b6838 2022-06-16 stsp case KEY_RIGHT:
4359 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4360 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4362 640cd7ff 2022-06-22 mark view->count = 0;
4364 145b6838 2022-06-16 stsp case KEY_LEFT:
4366 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4367 640cd7ff 2022-06-22 mark if (view->x <= 0)
4368 640cd7ff 2022-06-22 mark view->count = 0;
4372 3dbaef42 2020-11-24 stsp if (ch == 'a')
4373 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4374 3dbaef42 2020-11-24 stsp if (ch == 'w')
4375 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4376 64453f7e 2020-11-21 stsp wclear(view->window);
4377 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
4378 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4379 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4380 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
4381 64453f7e 2020-11-21 stsp err = create_diff(s);
4382 640cd7ff 2022-06-22 mark view->count = 0;
4385 912a3f79 2021-08-30 j case KEY_HOME:
4386 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4387 640cd7ff 2022-06-22 mark view->count = 0;
4390 912a3f79 2021-08-30 j case KEY_END:
4391 640cd7ff 2022-06-22 mark view->count = 0;
4395 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4399 1e37a5c2 2019-05-12 jcs case KEY_UP:
4400 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4401 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4402 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4404 640cd7ff 2022-06-22 mark view->count = 0;
4406 83cc4199 2022-06-13 stsp case CTRL('u'):
4408 83cc4199 2022-06-13 stsp nscroll /= 2;
4409 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4410 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4411 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4413 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4414 640cd7ff 2022-06-22 mark view->count = 0;
4418 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4419 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4422 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4423 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4424 1e37a5c2 2019-05-12 jcs if (!s->eof)
4425 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4427 640cd7ff 2022-06-22 mark view->count = 0;
4429 83cc4199 2022-06-13 stsp case CTRL('d'):
4431 83cc4199 2022-06-13 stsp nscroll /= 2;
4432 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4433 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4434 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4437 640cd7ff 2022-06-22 mark if (s->eof) {
4438 640cd7ff 2022-06-22 mark view->count = 0;
4442 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4443 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4444 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4445 826082fe 2020-12-10 stsp if (linelen == -1) {
4446 826082fe 2020-12-10 stsp if (feof(s->f)) {
4447 826082fe 2020-12-10 stsp s->eof = 1;
4449 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4453 826082fe 2020-12-10 stsp free(line);
4456 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4457 1e37a5c2 2019-05-12 jcs s->diff_context--;
4458 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4459 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4460 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4461 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4462 27829c9e 2020-11-21 stsp s->nlines) {
4463 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4464 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4467 640cd7ff 2022-06-22 mark view->count = 0;
4470 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4471 1e37a5c2 2019-05-12 jcs s->diff_context++;
4472 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4473 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4474 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4476 640cd7ff 2022-06-22 mark view->count = 0;
4480 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4481 640cd7ff 2022-06-22 mark view->count = 0;
4484 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4485 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4487 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4488 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4492 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4495 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4499 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4500 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4501 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4502 145b6838 2022-06-16 stsp view->x = 0;
4504 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4505 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4509 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4510 640cd7ff 2022-06-22 mark view->count = 0;
4513 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4514 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4516 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4517 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4521 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4524 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4528 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4529 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4530 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4531 145b6838 2022-06-16 stsp view->x = 0;
4533 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4534 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4537 640cd7ff 2022-06-22 mark view->count = 0;
4541 bcbd79e2 2018-08-19 stsp return err;
4544 4ed7e80c 2018-05-20 stsp static const struct got_error *
4545 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4547 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4548 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4549 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4550 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4551 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4552 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4553 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4554 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4555 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4556 3dbaef42 2020-11-24 stsp const char *errstr;
4557 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4558 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4560 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4561 26ed57b2 2018-05-19 stsp switch (ch) {
4563 64453f7e 2020-11-21 stsp force_text_diff = 1;
4566 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4568 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4569 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4570 5a20d08d 2022-02-09 op errstr, errstr);
4572 09b5bff8 2020-02-23 naddy case 'r':
4573 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4574 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4575 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4577 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4580 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4583 17020d27 2019-03-07 stsp usage_diff();
4584 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4588 26ed57b2 2018-05-19 stsp argc -= optind;
4589 26ed57b2 2018-05-19 stsp argv += optind;
4591 26ed57b2 2018-05-19 stsp if (argc == 0) {
4592 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4593 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4594 15a94983 2018-12-23 stsp id_str1 = argv[0];
4595 15a94983 2018-12-23 stsp id_str2 = argv[1];
4597 26ed57b2 2018-05-19 stsp usage_diff();
4599 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4600 0ae84acc 2022-06-15 tracey if (error)
4601 0ae84acc 2022-06-15 tracey goto done;
4603 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4604 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4605 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4606 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4607 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4608 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4609 c156c7a4 2020-12-18 stsp goto done;
4610 a273ac94 2020-02-23 naddy if (worktree)
4611 a273ac94 2020-02-23 naddy repo_path =
4612 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4614 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4615 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4616 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4617 c156c7a4 2020-12-18 stsp goto done;
4621 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4622 eb6600df 2019-01-04 stsp if (error)
4623 eb6600df 2019-01-04 stsp goto done;
4625 a273ac94 2020-02-23 naddy init_curses();
4627 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4628 51a10b52 2020-12-26 stsp if (error)
4629 51a10b52 2020-12-26 stsp goto done;
4631 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4632 26ed57b2 2018-05-19 stsp if (error)
4633 26ed57b2 2018-05-19 stsp goto done;
4635 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4636 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4637 26ed57b2 2018-05-19 stsp if (error)
4638 26ed57b2 2018-05-19 stsp goto done;
4640 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4641 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4642 26ed57b2 2018-05-19 stsp if (error)
4643 26ed57b2 2018-05-19 stsp goto done;
4645 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4646 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4647 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4648 ea5e7bb5 2018-08-01 stsp goto done;
4650 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4651 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4652 5dc9f4bc 2018-08-04 stsp if (error)
4653 5dc9f4bc 2018-08-04 stsp goto done;
4654 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4656 3dbaef42 2020-11-24 stsp free(label1);
4657 3dbaef42 2020-11-24 stsp free(label2);
4658 c02c541e 2019-03-29 stsp free(repo_path);
4659 a273ac94 2020-02-23 naddy free(cwd);
4660 1d0f4054 2021-06-17 stsp if (repo) {
4661 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4662 1d0f4054 2021-06-17 stsp if (error == NULL)
4663 1d0f4054 2021-06-17 stsp error = close_err;
4665 a273ac94 2020-02-23 naddy if (worktree)
4666 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4667 0ae84acc 2022-06-15 tracey if (pack_fds) {
4668 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4669 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4670 0ae84acc 2022-06-15 tracey if (error == NULL)
4671 0ae84acc 2022-06-15 tracey error = pack_err;
4673 51a10b52 2020-12-26 stsp tog_free_refs();
4674 26ed57b2 2018-05-19 stsp return error;
4677 4ed7e80c 2018-05-20 stsp __dead static void
4678 9f7d7167 2018-04-29 stsp usage_blame(void)
4681 87411fa9 2022-06-16 stsp fprintf(stderr,
4682 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4683 9f7d7167 2018-04-29 stsp getprogname());
4687 84451b3e 2018-07-10 stsp struct tog_blame_line {
4688 84451b3e 2018-07-10 stsp int annotated;
4689 84451b3e 2018-07-10 stsp struct got_object_id *id;
4692 4ed7e80c 2018-05-20 stsp static const struct got_error *
4693 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4695 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4696 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4697 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4698 84451b3e 2018-07-10 stsp const struct got_error *err;
4699 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4700 826082fe 2020-12-10 stsp char *line = NULL;
4701 826082fe 2020-12-10 stsp size_t linesize = 0;
4702 826082fe 2020-12-10 stsp ssize_t linelen;
4703 84451b3e 2018-07-10 stsp wchar_t *wline;
4704 27a741e5 2019-09-11 stsp int width;
4705 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4706 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4707 ab089a2a 2018-07-12 stsp char *id_str;
4708 11b20872 2019-11-08 stsp struct tog_color *tc;
4710 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4712 ab089a2a 2018-07-12 stsp return err;
4714 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4715 f7d12f7e 2018-08-01 stsp werase(view->window);
4717 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4718 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4719 ab089a2a 2018-07-12 stsp free(id_str);
4720 ab089a2a 2018-07-12 stsp return err;
4723 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4724 ab089a2a 2018-07-12 stsp free(line);
4725 2550e4c3 2018-07-13 stsp line = NULL;
4727 1cae65b4 2019-09-22 stsp return err;
4728 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4729 a3404814 2018-09-02 stsp wstandout(view->window);
4730 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4732 11b20872 2019-11-08 stsp wattr_on(view->window,
4733 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4734 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4736 11b20872 2019-11-08 stsp wattr_off(view->window,
4737 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4738 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4739 a3404814 2018-09-02 stsp wstandend(view->window);
4740 2550e4c3 2018-07-13 stsp free(wline);
4741 2550e4c3 2018-07-13 stsp wline = NULL;
4742 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4743 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4745 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4746 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4747 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4748 ab089a2a 2018-07-12 stsp free(id_str);
4749 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4751 ab089a2a 2018-07-12 stsp free(id_str);
4752 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4753 3f60a8ef 2018-07-10 stsp free(line);
4754 2550e4c3 2018-07-13 stsp line = NULL;
4756 3f60a8ef 2018-07-10 stsp return err;
4757 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4758 2550e4c3 2018-07-13 stsp free(wline);
4759 2550e4c3 2018-07-13 stsp wline = NULL;
4760 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4761 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4763 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4764 145b6838 2022-06-16 stsp view->maxx = 0;
4765 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4766 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4767 826082fe 2020-12-10 stsp if (linelen == -1) {
4768 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4769 826082fe 2020-12-10 stsp s->eof = 1;
4772 84451b3e 2018-07-10 stsp free(line);
4773 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4775 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4778 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4779 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4780 1853e0f4 2022-06-16 stsp if (err) {
4781 1853e0f4 2022-06-16 stsp free(line);
4782 1853e0f4 2022-06-16 stsp return err;
4784 1853e0f4 2022-06-16 stsp free(wline);
4785 1853e0f4 2022-06-16 stsp wline = NULL;
4786 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4788 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4789 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4791 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4792 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4793 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4794 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4795 27a741e5 2019-09-11 stsp !(view->focussed &&
4796 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4797 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4798 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4799 8d0fe45a 2019-08-12 stsp char *id_str;
4800 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4801 87411fa9 2022-06-16 stsp blame_line->id);
4802 8d0fe45a 2019-08-12 stsp if (err) {
4803 8d0fe45a 2019-08-12 stsp free(line);
4804 8d0fe45a 2019-08-12 stsp return err;
4806 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4808 11b20872 2019-11-08 stsp wattr_on(view->window,
4809 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4810 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4812 11b20872 2019-11-08 stsp wattr_off(view->window,
4813 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4814 8d0fe45a 2019-08-12 stsp free(id_str);
4815 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4817 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4818 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4821 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4822 ee41ec32 2018-07-10 stsp prev_id = NULL;
4825 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4826 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4827 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4829 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4830 41605754 2020-11-12 stsp width = 9;
4831 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4832 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4833 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4834 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4835 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4836 41605754 2020-11-12 stsp if (err) {
4837 41605754 2020-11-12 stsp free(line);
4838 41605754 2020-11-12 stsp return err;
4840 41605754 2020-11-12 stsp width += 9;
4843 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4844 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4845 4cb9d869 2022-06-16 stsp if (err) {
4846 4cb9d869 2022-06-16 stsp free(line);
4847 4cb9d869 2022-06-16 stsp return err;
4849 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4850 a75b3e2d 2022-06-16 stsp width += 9;
4851 41605754 2020-11-12 stsp free(wline);
4852 41605754 2020-11-12 stsp wline = NULL;
4855 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4856 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4857 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4858 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4860 826082fe 2020-12-10 stsp free(line);
4861 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4863 9b058f45 2022-06-30 mark view_border(view);
4865 84451b3e 2018-07-10 stsp return NULL;
4868 84451b3e 2018-07-10 stsp static const struct got_error *
4869 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4870 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4872 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4873 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4874 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4875 1a76625f 2018-10-22 stsp int errcode;
4877 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4878 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4879 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4881 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4882 1a76625f 2018-10-22 stsp if (errcode)
4883 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4885 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4886 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4887 d68a0a7d 2018-07-10 stsp goto done;
4890 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4891 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4893 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4894 d68a0a7d 2018-07-10 stsp if (line->annotated)
4895 d68a0a7d 2018-07-10 stsp goto done;
4897 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4898 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4899 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4900 84451b3e 2018-07-10 stsp goto done;
4902 84451b3e 2018-07-10 stsp line->annotated = 1;
4904 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4905 1a76625f 2018-10-22 stsp if (errcode)
4906 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4907 84451b3e 2018-07-10 stsp return err;
4910 84451b3e 2018-07-10 stsp static void *
4911 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4913 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4914 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4915 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4916 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
4917 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
4919 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
4920 e6e73e55 2022-06-30 tracey if (fd1 == -1)
4921 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
4923 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
4924 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
4925 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
4926 e6e73e55 2022-06-30 tracey goto done;
4929 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
4930 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
4931 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4932 e6e73e55 2022-06-30 tracey goto done;
4934 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
4935 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
4936 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4937 e6e73e55 2022-06-30 tracey goto done;
4940 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4942 e6e73e55 2022-06-30 tracey goto done;
4944 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4945 e6e73e55 2022-06-30 tracey blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1,
4947 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4948 fc06ba56 2019-08-22 stsp err = NULL;
4950 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4951 e6e73e55 2022-06-30 tracey if (errcode) {
4952 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
4953 e6e73e55 2022-06-30 tracey goto done;
4956 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4957 1d0f4054 2021-06-17 stsp if (err == NULL)
4958 1d0f4054 2021-06-17 stsp err = close_err;
4959 c9beca56 2018-07-22 stsp ta->repo = NULL;
4960 c9beca56 2018-07-22 stsp *ta->complete = 1;
4962 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4963 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4964 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4967 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4968 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
4969 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4970 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
4971 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
4972 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
4973 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
4974 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
4976 18430de3 2018-07-10 stsp return (void *)err;
4979 245d91c1 2018-07-12 stsp static struct got_object_id *
4980 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4981 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4983 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4985 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4986 8d0fe45a 2019-08-12 stsp return NULL;
4988 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4989 245d91c1 2018-07-12 stsp if (!line->annotated)
4990 245d91c1 2018-07-12 stsp return NULL;
4992 245d91c1 2018-07-12 stsp return line->id;
4995 b880a918 2018-07-10 stsp static const struct got_error *
4996 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4998 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5001 245d91c1 2018-07-12 stsp if (blame->thread) {
5002 1a76625f 2018-10-22 stsp int errcode;
5003 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5004 1a76625f 2018-10-22 stsp if (errcode)
5005 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5006 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5007 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5008 1a76625f 2018-10-22 stsp if (errcode)
5009 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5010 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5011 1a76625f 2018-10-22 stsp if (errcode)
5012 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5013 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5014 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5015 245d91c1 2018-07-12 stsp err = NULL;
5016 245d91c1 2018-07-12 stsp blame->thread = NULL;
5018 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5019 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5020 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5021 1d0f4054 2021-06-17 stsp if (err == NULL)
5022 1d0f4054 2021-06-17 stsp err = close_err;
5023 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5025 245d91c1 2018-07-12 stsp if (blame->f) {
5026 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5027 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5028 245d91c1 2018-07-12 stsp blame->f = NULL;
5030 57670559 2018-12-23 stsp if (blame->lines) {
5031 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5032 57670559 2018-12-23 stsp free(blame->lines[i].id);
5033 57670559 2018-12-23 stsp free(blame->lines);
5034 57670559 2018-12-23 stsp blame->lines = NULL;
5036 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5037 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5038 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5039 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5040 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5041 0ae84acc 2022-06-15 tracey if (err == NULL)
5042 0ae84acc 2022-06-15 tracey err = pack_err;
5043 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5045 245d91c1 2018-07-12 stsp return err;
5048 245d91c1 2018-07-12 stsp static const struct got_error *
5049 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5051 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5052 fc06ba56 2019-08-22 stsp int *done = arg;
5053 fc06ba56 2019-08-22 stsp int errcode;
5055 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5056 fc06ba56 2019-08-22 stsp if (errcode)
5057 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5058 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5060 fc06ba56 2019-08-22 stsp if (*done)
5061 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5063 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5064 fc06ba56 2019-08-22 stsp if (errcode)
5065 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5066 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5068 fc06ba56 2019-08-22 stsp return err;
5071 fc06ba56 2019-08-22 stsp static const struct got_error *
5072 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5074 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5075 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5076 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5077 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5078 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5079 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5080 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5081 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5082 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5084 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5085 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5087 15a94983 2018-12-23 stsp return err;
5089 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5090 eb81bc23 2022-06-28 tracey if (fd == -1) {
5091 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5092 eb81bc23 2022-06-28 tracey goto done;
5095 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5097 a44927cc 2022-04-07 stsp goto done;
5099 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5101 84451b3e 2018-07-10 stsp goto done;
5103 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5104 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5105 84451b3e 2018-07-10 stsp goto done;
5108 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5110 a70480e0 2018-06-23 stsp goto done;
5111 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5112 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5113 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5114 84451b3e 2018-07-10 stsp goto done;
5116 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5117 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5119 1fddf795 2021-01-20 stsp goto done;
5120 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5121 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5122 84451b3e 2018-07-10 stsp goto done;
5125 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5126 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5127 b02560ec 2019-08-19 stsp blame->nlines--;
5129 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5130 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5131 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5132 84451b3e 2018-07-10 stsp goto done;
5135 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5137 bd24772e 2018-07-11 stsp goto done;
5138 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5139 0ae84acc 2022-06-15 tracey pack_fds);
5140 0ae84acc 2022-06-15 tracey if (err)
5141 0ae84acc 2022-06-15 tracey goto done;
5143 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5144 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5145 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5146 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5147 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5148 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5149 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5150 245d91c1 2018-07-12 stsp goto done;
5152 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5154 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5155 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5156 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5157 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5158 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5159 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5160 a5388363 2020-12-01 naddy s->blame_complete = 0;
5162 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5163 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5164 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5165 f5a09613 2020-12-13 naddy s->selected_line = 1;
5167 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5170 a44927cc 2022-04-07 stsp if (commit)
5171 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5172 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5173 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5175 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5176 27d434c2 2018-09-15 stsp free(obj_id);
5178 245d91c1 2018-07-12 stsp stop_blame(blame);
5179 245d91c1 2018-07-12 stsp return err;
5182 245d91c1 2018-07-12 stsp static const struct got_error *
5183 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5184 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5186 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5187 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5189 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5191 c4843652 2019-08-12 stsp s->path = strdup(path);
5192 c4843652 2019-08-12 stsp if (s->path == NULL)
5193 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5195 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5196 c4843652 2019-08-12 stsp if (err) {
5197 c4843652 2019-08-12 stsp free(s->path);
5198 7cbe629d 2018-08-04 stsp return err;
5201 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5202 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5203 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5204 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5205 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5206 fb2756b9 2018-08-04 stsp s->repo = repo;
5207 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5208 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5210 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5211 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5212 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5213 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5215 11b20872 2019-11-08 stsp return err;
5218 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5219 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5220 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5221 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5222 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5224 a5388363 2020-12-01 naddy return run_blame(view);
5227 e5a0f69f 2018-08-18 stsp static const struct got_error *
5228 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5230 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5231 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5233 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5234 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5236 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5237 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5238 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5239 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5240 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5243 e5a0f69f 2018-08-18 stsp free(s->path);
5244 11b20872 2019-11-08 stsp free_colors(&s->colors);
5245 e5a0f69f 2018-08-18 stsp return err;
5248 7cbe629d 2018-08-04 stsp static const struct got_error *
5249 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5251 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5253 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5254 6c4c42e0 2019-06-24 stsp return NULL;
5257 6c4c42e0 2019-06-24 stsp static const struct got_error *
5258 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5260 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5261 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5262 6c4c42e0 2019-06-24 stsp int lineno;
5263 cb713507 2022-06-16 stsp char *line = NULL;
5264 826082fe 2020-12-10 stsp size_t linesize = 0;
5265 826082fe 2020-12-10 stsp ssize_t linelen;
5267 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5268 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5269 6c4c42e0 2019-06-24 stsp return NULL;
5272 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5273 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5274 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5276 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5278 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5280 6c4c42e0 2019-06-24 stsp while (1) {
5281 6c4c42e0 2019-06-24 stsp off_t offset;
5283 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5284 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5285 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5289 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5290 6c4c42e0 2019-06-24 stsp lineno = 1;
5292 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5295 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5296 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5297 6c4c42e0 2019-06-24 stsp free(line);
5298 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5300 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5301 cb713507 2022-06-16 stsp if (linelen != -1) {
5302 cb713507 2022-06-16 stsp char *exstr;
5303 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5306 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5307 cb713507 2022-06-16 stsp &view->regmatch)) {
5308 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5309 cb713507 2022-06-16 stsp s->matched_line = lineno;
5310 cb713507 2022-06-16 stsp free(exstr);
5313 cb713507 2022-06-16 stsp free(exstr);
5315 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5320 826082fe 2020-12-10 stsp free(line);
5322 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5323 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5324 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5327 145b6838 2022-06-16 stsp return err;
5330 6c4c42e0 2019-06-24 stsp static const struct got_error *
5331 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5333 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5334 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5335 2b380cc8 2018-10-24 stsp int errcode;
5337 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5338 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5339 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5340 2b380cc8 2018-10-24 stsp if (errcode)
5341 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5343 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5346 51fe7530 2019-08-19 stsp if (s->blame_complete)
5347 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5349 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5351 9b058f45 2022-06-30 mark view_border(view);
5352 e5a0f69f 2018-08-18 stsp return err;
5355 e5a0f69f 2018-08-18 stsp static const struct got_error *
5356 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5358 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5359 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5360 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5361 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5363 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5364 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5365 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5366 9b058f45 2022-06-30 mark --eos; /* border */
5368 e5a0f69f 2018-08-18 stsp switch (ch) {
5370 145b6838 2022-06-16 stsp view->x = 0;
5373 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5374 640cd7ff 2022-06-22 mark view->count = 0;
5376 145b6838 2022-06-16 stsp case KEY_RIGHT:
5378 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5379 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5381 640cd7ff 2022-06-22 mark view->count = 0;
5383 145b6838 2022-06-16 stsp case KEY_LEFT:
5385 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5386 640cd7ff 2022-06-22 mark if (view->x <= 0)
5387 640cd7ff 2022-06-22 mark view->count = 0;
5390 1e37a5c2 2019-05-12 jcs s->done = 1;
5392 4deef56f 2021-09-02 naddy case 'g':
5393 4deef56f 2021-09-02 naddy case KEY_HOME:
5394 4deef56f 2021-09-02 naddy s->selected_line = 1;
5395 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5396 640cd7ff 2022-06-22 mark view->count = 0;
5398 4deef56f 2021-09-02 naddy case 'G':
5399 4deef56f 2021-09-02 naddy case KEY_END:
5400 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5401 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5402 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5404 9b058f45 2022-06-30 mark s->selected_line = eos;
5405 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5407 640cd7ff 2022-06-22 mark view->count = 0;
5410 1e37a5c2 2019-05-12 jcs case KEY_UP:
5411 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5412 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5413 1e37a5c2 2019-05-12 jcs s->selected_line--;
5414 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5415 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5416 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5418 640cd7ff 2022-06-22 mark view->count = 0;
5420 83cc4199 2022-06-13 stsp case CTRL('u'):
5422 83cc4199 2022-06-13 stsp nscroll /= 2;
5423 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5424 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5425 ea025d1d 2020-02-22 naddy case CTRL('b'):
5427 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5428 640cd7ff 2022-06-22 mark if (view->count > 1)
5429 640cd7ff 2022-06-22 mark nscroll += nscroll;
5430 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5431 640cd7ff 2022-06-22 mark view->count = 0;
5434 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5435 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5437 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5440 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5441 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5442 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5443 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5444 1e37a5c2 2019-05-12 jcs s->selected_line++;
5445 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5446 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5448 640cd7ff 2022-06-22 mark view->count = 0;
5451 1e37a5c2 2019-05-12 jcs case 'p': {
5452 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5454 640cd7ff 2022-06-22 mark view->count = 0;
5455 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5456 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5457 1e37a5c2 2019-05-12 jcs if (id == NULL)
5459 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5460 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5461 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5462 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5463 1e37a5c2 2019-05-12 jcs int obj_type;
5464 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5465 1e37a5c2 2019-05-12 jcs s->repo, id);
5468 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5469 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5470 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5471 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5474 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5475 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5476 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5479 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5480 a44927cc 2022-04-07 stsp pcommit, s->path);
5481 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5482 e5a0f69f 2018-08-18 stsp if (err) {
5483 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5484 1e37a5c2 2019-05-12 jcs err = NULL;
5485 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5488 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5490 1e37a5c2 2019-05-12 jcs free(blob_id);
5491 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5492 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5493 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5496 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5497 d7b5a0e8 2022-04-20 stsp &pid->id);
5498 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5500 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5501 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5503 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5508 1e37a5c2 2019-05-12 jcs s->done = 1;
5509 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5510 1e37a5c2 2019-05-12 jcs s->done = 0;
5511 1e37a5c2 2019-05-12 jcs if (thread_err)
5513 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5514 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5515 a5388363 2020-12-01 naddy err = run_blame(view);
5520 61417565 2022-06-20 mark case 'C': {
5521 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5523 640cd7ff 2022-06-22 mark view->count = 0;
5524 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5525 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5527 1e37a5c2 2019-05-12 jcs s->done = 1;
5528 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5529 1e37a5c2 2019-05-12 jcs s->done = 0;
5530 1e37a5c2 2019-05-12 jcs if (thread_err)
5532 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5533 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5534 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5535 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5536 a5388363 2020-12-01 naddy err = run_blame(view);
5541 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5542 1e37a5c2 2019-05-12 jcs case '\r': {
5543 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5544 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5545 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5547 640cd7ff 2022-06-22 mark view->count = 0;
5548 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5549 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5550 1e37a5c2 2019-05-12 jcs if (id == NULL)
5552 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5555 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5556 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5557 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
5559 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5560 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5561 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5562 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5565 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5566 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5567 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5569 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5572 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5573 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5578 e78dc838 2020-12-04 stsp view->focussed = 0;
5579 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5580 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5581 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5582 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5583 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5586 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5589 e78dc838 2020-12-04 stsp view->focus_child = 1;
5591 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5596 83cc4199 2022-06-13 stsp case CTRL('d'):
5598 83cc4199 2022-06-13 stsp nscroll /= 2;
5599 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5600 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5601 ea025d1d 2020-02-22 naddy case CTRL('f'):
5604 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5605 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5606 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5607 640cd7ff 2022-06-22 mark view->count = 0;
5610 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5611 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5612 83cc4199 2022-06-13 stsp s->selected_line +=
5613 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5614 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5616 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5617 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5619 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5620 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5622 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5623 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5624 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5625 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5629 640cd7ff 2022-06-22 mark view->count = 0;
5632 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5635 a70480e0 2018-06-23 stsp static const struct got_error *
5636 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5638 a70480e0 2018-06-23 stsp const struct got_error *error;
5639 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5640 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5641 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5642 0587e10c 2020-07-23 stsp char *link_target = NULL;
5643 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5644 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5645 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5647 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5648 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5650 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5651 a70480e0 2018-06-23 stsp switch (ch) {
5653 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5656 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5657 69069811 2018-08-02 stsp if (repo_path == NULL)
5658 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5662 17020d27 2019-03-07 stsp usage_blame();
5663 a70480e0 2018-06-23 stsp /* NOTREACHED */
5667 a70480e0 2018-06-23 stsp argc -= optind;
5668 a70480e0 2018-06-23 stsp argv += optind;
5670 f135c941 2020-02-20 stsp if (argc != 1)
5671 a70480e0 2018-06-23 stsp usage_blame();
5673 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5674 0ae84acc 2022-06-15 tracey if (error != NULL)
5675 0ae84acc 2022-06-15 tracey goto done;
5677 69069811 2018-08-02 stsp if (repo_path == NULL) {
5678 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5679 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5680 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5681 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5682 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5683 c156c7a4 2020-12-18 stsp goto done;
5684 f135c941 2020-02-20 stsp if (worktree)
5685 eb41ed75 2019-02-05 stsp repo_path =
5686 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5688 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5689 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5690 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5691 c156c7a4 2020-12-18 stsp goto done;
5695 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5696 c02c541e 2019-03-29 stsp if (error != NULL)
5697 8e94dd5b 2019-01-04 stsp goto done;
5699 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5700 f135c941 2020-02-20 stsp worktree);
5701 c02c541e 2019-03-29 stsp if (error)
5702 92205607 2019-01-04 stsp goto done;
5704 f135c941 2020-02-20 stsp init_curses();
5706 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5707 51a10b52 2020-12-26 stsp if (error)
5708 51a10b52 2020-12-26 stsp goto done;
5710 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5711 eb41ed75 2019-02-05 stsp if (error)
5712 69069811 2018-08-02 stsp goto done;
5714 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5715 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5716 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5717 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5718 a70480e0 2018-06-23 stsp if (error != NULL)
5719 66b4983c 2018-06-23 stsp goto done;
5720 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5721 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5723 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5724 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5726 a19e88aa 2018-06-23 stsp if (error != NULL)
5727 8b473291 2019-02-21 stsp goto done;
5729 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5730 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5731 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5732 e1cd8fed 2018-08-01 stsp goto done;
5735 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5736 a44927cc 2022-04-07 stsp if (error)
5737 a44927cc 2022-04-07 stsp goto done;
5739 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5740 a44927cc 2022-04-07 stsp commit, repo);
5741 7cbe629d 2018-08-04 stsp if (error)
5742 7cbe629d 2018-08-04 stsp goto done;
5744 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5745 78756c87 2020-11-24 stsp commit_id, repo);
5746 0587e10c 2020-07-23 stsp if (error)
5747 0587e10c 2020-07-23 stsp goto done;
5748 12314ad4 2019-08-31 stsp if (worktree) {
5749 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5750 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5751 12314ad4 2019-08-31 stsp worktree = NULL;
5753 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5755 69069811 2018-08-02 stsp free(repo_path);
5756 f135c941 2020-02-20 stsp free(in_repo_path);
5757 0587e10c 2020-07-23 stsp free(link_target);
5758 69069811 2018-08-02 stsp free(cwd);
5759 a70480e0 2018-06-23 stsp free(commit_id);
5760 a44927cc 2022-04-07 stsp if (commit)
5761 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5762 eb41ed75 2019-02-05 stsp if (worktree)
5763 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5764 1d0f4054 2021-06-17 stsp if (repo) {
5765 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5766 1d0f4054 2021-06-17 stsp if (error == NULL)
5767 1d0f4054 2021-06-17 stsp error = close_err;
5769 0ae84acc 2022-06-15 tracey if (pack_fds) {
5770 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5771 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5772 0ae84acc 2022-06-15 tracey if (error == NULL)
5773 0ae84acc 2022-06-15 tracey error = pack_err;
5775 51a10b52 2020-12-26 stsp tog_free_refs();
5776 a70480e0 2018-06-23 stsp return error;
5779 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5780 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5782 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5783 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5784 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5785 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5786 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5787 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5788 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5790 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5791 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5792 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5793 9b058f45 2022-06-30 mark --limit; /* border */
5795 f7d12f7e 2018-08-01 stsp werase(view->window);
5797 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5798 ffd1d5e5 2018-06-23 stsp return NULL;
5800 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5803 ffd1d5e5 2018-06-23 stsp return err;
5804 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5805 a3404814 2018-09-02 stsp wstandout(view->window);
5806 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5808 11b20872 2019-11-08 stsp wattr_on(view->window,
5809 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5810 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5812 11b20872 2019-11-08 stsp wattr_off(view->window,
5813 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5814 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5815 a3404814 2018-09-02 stsp wstandend(view->window);
5816 2550e4c3 2018-07-13 stsp free(wline);
5817 2550e4c3 2018-07-13 stsp wline = NULL;
5818 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5819 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5820 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5821 ffd1d5e5 2018-06-23 stsp return NULL;
5822 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5825 ce52c690 2018-06-23 stsp return err;
5826 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5827 2550e4c3 2018-07-13 stsp free(wline);
5828 2550e4c3 2018-07-13 stsp wline = NULL;
5829 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5830 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5831 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5832 ffd1d5e5 2018-06-23 stsp return NULL;
5833 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5834 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5835 a1eca9bb 2018-06-23 stsp return NULL;
5837 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5838 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5839 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5840 0cf4efb1 2018-09-29 stsp if (view->focussed)
5841 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5842 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5844 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5845 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5846 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5847 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5848 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5849 ffd1d5e5 2018-06-23 stsp return NULL;
5853 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5856 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5857 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5858 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5859 848d6979 2019-08-12 stsp const char *modestr = "";
5860 56e0773d 2019-11-28 stsp mode_t mode;
5862 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5863 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5865 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5866 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5867 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5869 638f9024 2019-05-13 stsp return got_error_from_errno(
5870 230a42bd 2019-05-11 jcs "got_object_id_str");
5872 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5873 63c5ca5d 2019-08-24 stsp modestr = "$";
5874 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5877 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5878 d86d3b18 2020-12-01 naddy te, s->repo);
5879 0d6c6ee3 2020-05-20 stsp if (err) {
5880 0d6c6ee3 2020-05-20 stsp free(id_str);
5881 0d6c6ee3 2020-05-20 stsp return err;
5883 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5884 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5885 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5887 848d6979 2019-08-12 stsp modestr = "@";
5889 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5890 848d6979 2019-08-12 stsp modestr = "/";
5891 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5892 848d6979 2019-08-12 stsp modestr = "*";
5893 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5894 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5895 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5896 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5897 1d13200f 2018-07-12 stsp free(id_str);
5898 0d6c6ee3 2020-05-20 stsp free(link_target);
5899 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5901 1d13200f 2018-07-12 stsp free(id_str);
5902 0d6c6ee3 2020-05-20 stsp free(link_target);
5903 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5905 ffd1d5e5 2018-06-23 stsp if (err) {
5906 ffd1d5e5 2018-06-23 stsp free(line);
5909 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5910 0cf4efb1 2018-09-29 stsp if (view->focussed)
5911 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5912 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5914 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5916 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5917 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5918 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5920 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5921 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5922 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5923 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5924 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5925 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5926 ffd1d5e5 2018-06-23 stsp free(line);
5927 2550e4c3 2018-07-13 stsp free(wline);
5928 2550e4c3 2018-07-13 stsp wline = NULL;
5930 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5931 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5932 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5936 ffd1d5e5 2018-06-23 stsp return err;
5939 ffd1d5e5 2018-06-23 stsp static void
5940 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5942 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5943 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5944 fa86c4bf 2020-11-29 stsp int i = 0;
5946 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5949 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5950 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5951 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5952 fa86c4bf 2020-11-29 stsp if (!isroot)
5953 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5956 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5957 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5961 9b058f45 2022-06-30 mark static const struct got_error *
5962 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
5964 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
5965 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5966 ffd1d5e5 2018-06-23 stsp int n = 0;
5968 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5969 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5970 694d3271 2020-12-01 naddy s->first_displayed_entry);
5972 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5974 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5975 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
5977 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
5978 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
5979 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5980 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5984 9b058f45 2022-06-30 mark return NULL;
5987 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5988 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5989 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5991 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5992 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5993 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5995 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5996 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5997 56e0773d 2019-11-28 stsp + 1 /* slash */;
5999 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6001 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6002 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6003 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6005 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6006 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6007 d9765a41 2018-06-23 stsp while (pt) {
6008 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6009 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6010 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6011 cb2ebc8a 2018-06-23 stsp goto done;
6013 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6014 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6015 cb2ebc8a 2018-06-23 stsp goto done;
6017 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6020 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6021 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6022 ce52c690 2018-06-23 stsp goto done;
6026 ce52c690 2018-06-23 stsp if (err) {
6027 ce52c690 2018-06-23 stsp free(*path);
6028 ce52c690 2018-06-23 stsp *path = NULL;
6030 ce52c690 2018-06-23 stsp return err;
6033 ce52c690 2018-06-23 stsp static const struct got_error *
6034 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6035 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6036 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6038 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6039 ce52c690 2018-06-23 stsp char *path;
6040 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6042 a0de39f3 2019-08-09 stsp *new_view = NULL;
6044 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6046 ce52c690 2018-06-23 stsp return err;
6048 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6049 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6050 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6051 83ce39e3 2019-08-12 stsp goto done;
6054 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6055 e5a0f69f 2018-08-18 stsp if (err) {
6056 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6057 fc06ba56 2019-08-22 stsp err = NULL;
6058 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6060 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6062 83ce39e3 2019-08-12 stsp free(path);
6063 69efd4c4 2018-07-18 stsp return err;
6066 69efd4c4 2018-07-18 stsp static const struct got_error *
6067 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6068 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6070 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6071 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6072 69efd4c4 2018-07-18 stsp char *path;
6074 a0de39f3 2019-08-09 stsp *new_view = NULL;
6076 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6077 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6078 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6080 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6082 69efd4c4 2018-07-18 stsp return err;
6084 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6087 e5a0f69f 2018-08-18 stsp view_close(log_view);
6089 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6090 cb2ebc8a 2018-06-23 stsp free(path);
6091 cb2ebc8a 2018-06-23 stsp return err;
6094 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6095 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6096 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6098 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6099 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6100 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6101 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6103 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6104 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6106 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6107 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6108 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6110 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6112 bc573f3b 2021-07-10 stsp goto done;
6115 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6116 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6117 bc573f3b 2021-07-10 stsp * closed on demand.
6119 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6120 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6122 bc573f3b 2021-07-10 stsp goto done;
6123 bc573f3b 2021-07-10 stsp s->tree = s->root;
6125 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6126 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6127 ffd1d5e5 2018-06-23 stsp goto done;
6129 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6130 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6131 ffd1d5e5 2018-06-23 stsp goto done;
6134 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6135 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6136 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6137 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6138 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6139 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6140 9cd7cbd1 2020-12-07 stsp goto done;
6143 fb2756b9 2018-08-04 stsp s->repo = repo;
6145 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6146 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6147 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6148 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6150 c0b01bdb 2019-11-08 stsp goto done;
6151 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6152 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6154 c0b01bdb 2019-11-08 stsp goto done;
6155 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6156 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6157 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6159 c0b01bdb 2019-11-08 stsp goto done;
6161 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6162 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6163 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6165 11b20872 2019-11-08 stsp goto done;
6167 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6168 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6170 c0b01bdb 2019-11-08 stsp goto done;
6173 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6174 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6175 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6176 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6177 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6179 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6180 bc573f3b 2021-07-10 stsp if (commit)
6181 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6183 bc573f3b 2021-07-10 stsp close_tree_view(view);
6184 ad80ab7b 2018-08-04 stsp return err;
6187 e5a0f69f 2018-08-18 stsp static const struct got_error *
6188 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6190 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6192 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6193 fb2756b9 2018-08-04 stsp free(s->tree_label);
6194 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6195 6484ec90 2018-09-29 stsp free(s->commit_id);
6196 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6197 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6198 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6199 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6200 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6201 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6202 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6203 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6204 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6205 ad80ab7b 2018-08-04 stsp free(parent);
6208 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6209 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6210 bc573f3b 2021-07-10 stsp if (s->root)
6211 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6212 7c32bd05 2019-06-22 stsp return NULL;
6215 7c32bd05 2019-06-22 stsp static const struct got_error *
6216 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6218 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6220 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6221 7c32bd05 2019-06-22 stsp return NULL;
6224 7c32bd05 2019-06-22 stsp static int
6225 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6227 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6229 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6233 7c32bd05 2019-06-22 stsp static const struct got_error *
6234 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6236 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6237 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6239 7c32bd05 2019-06-22 stsp if (!view->searching) {
6240 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6241 7c32bd05 2019-06-22 stsp return NULL;
6244 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6245 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6246 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6247 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6248 56e0773d 2019-11-28 stsp s->selected_entry);
6250 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6252 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6253 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6255 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6256 56e0773d 2019-11-28 stsp s->selected_entry);
6259 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6260 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6261 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6262 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6264 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6267 7c32bd05 2019-06-22 stsp while (1) {
6268 56e0773d 2019-11-28 stsp if (te == NULL) {
6269 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6270 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6271 ac66afb8 2019-06-24 stsp return NULL;
6273 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6274 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6276 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6279 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6280 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6281 56e0773d 2019-11-28 stsp s->matched_entry = te;
6285 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6286 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6288 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6291 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6292 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6293 7c32bd05 2019-06-22 stsp s->selected = 0;
6296 e5a0f69f 2018-08-18 stsp return NULL;
6299 ad80ab7b 2018-08-04 stsp static const struct got_error *
6300 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6302 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6303 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6304 e5a0f69f 2018-08-18 stsp char *parent_path;
6306 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6308 e5a0f69f 2018-08-18 stsp return err;
6310 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6311 e5a0f69f 2018-08-18 stsp free(parent_path);
6313 9b058f45 2022-06-30 mark view_border(view);
6314 e5a0f69f 2018-08-18 stsp return err;
6317 e5a0f69f 2018-08-18 stsp static const struct got_error *
6318 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6320 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6321 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6322 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6323 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6324 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
6326 e5a0f69f 2018-08-18 stsp switch (ch) {
6328 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6329 640cd7ff 2022-06-22 mark view->count = 0;
6332 640cd7ff 2022-06-22 mark view->count = 0;
6333 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6335 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6336 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
6337 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
6338 e78dc838 2020-12-04 stsp view->focussed = 0;
6339 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6340 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6341 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6343 1e37a5c2 2019-05-12 jcs return err;
6344 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6347 e78dc838 2020-12-04 stsp view->focus_child = 1;
6349 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6352 640cd7ff 2022-06-22 mark view->count = 0;
6353 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6354 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
6355 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
6356 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
6357 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6358 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6359 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6360 152c1c93 2020-11-29 stsp if (err) {
6361 152c1c93 2020-11-29 stsp view_close(ref_view);
6362 152c1c93 2020-11-29 stsp return err;
6364 e78dc838 2020-12-04 stsp view->focussed = 0;
6365 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6366 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6367 152c1c93 2020-11-29 stsp err = view_close_child(view);
6369 152c1c93 2020-11-29 stsp return err;
6370 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6373 e78dc838 2020-12-04 stsp view->focus_child = 1;
6375 152c1c93 2020-11-29 stsp *new_view = ref_view;
6377 e4526bf5 2021-09-03 naddy case 'g':
6378 e4526bf5 2021-09-03 naddy case KEY_HOME:
6379 e4526bf5 2021-09-03 naddy s->selected = 0;
6380 640cd7ff 2022-06-22 mark view->count = 0;
6381 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6382 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6383 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6385 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6387 e4526bf5 2021-09-03 naddy case 'G':
6388 9b058f45 2022-06-30 mark case KEY_END: {
6389 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6391 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6392 9b058f45 2022-06-30 mark --eos; /* border */
6393 e4526bf5 2021-09-03 naddy s->selected = 0;
6394 640cd7ff 2022-06-22 mark view->count = 0;
6395 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6396 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6397 e4526bf5 2021-09-03 naddy if (te == NULL) {
6398 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6399 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6404 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6405 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6407 e4526bf5 2021-09-03 naddy if (n > 0)
6408 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6412 1e37a5c2 2019-05-12 jcs case KEY_UP:
6413 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6414 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6415 1e37a5c2 2019-05-12 jcs s->selected--;
6418 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6419 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6420 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6421 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6422 640cd7ff 2022-06-22 mark view->count = 0;
6424 83cc4199 2022-06-13 stsp case CTRL('u'):
6426 83cc4199 2022-06-13 stsp nscroll /= 2;
6427 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6428 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6429 ea025d1d 2020-02-22 naddy case CTRL('b'):
6431 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6432 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6433 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6434 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6436 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6437 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6439 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6440 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6441 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6442 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6443 640cd7ff 2022-06-22 mark view->count = 0;
6446 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6447 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6448 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6449 1e37a5c2 2019-05-12 jcs s->selected++;
6452 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6453 640cd7ff 2022-06-22 mark == NULL) {
6454 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6455 640cd7ff 2022-06-22 mark view->count = 0;
6458 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6460 83cc4199 2022-06-13 stsp case CTRL('d'):
6462 83cc4199 2022-06-13 stsp nscroll /= 2;
6463 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6464 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6465 ea025d1d 2020-02-22 naddy case CTRL('f'):
6467 48bb96f0 2022-06-20 naddy case ' ':
6468 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6470 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6471 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6472 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6473 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6475 640cd7ff 2022-06-22 mark view->count = 0;
6478 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6480 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6482 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6483 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6484 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6485 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6486 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6487 640cd7ff 2022-06-22 mark view->count = 0;
6490 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6491 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6493 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6494 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6495 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6496 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6497 1e37a5c2 2019-05-12 jcs s->selected_entry =
6498 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6499 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6500 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6501 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6505 1e37a5c2 2019-05-12 jcs free(parent);
6506 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6507 56e0773d 2019-11-28 stsp s->selected_entry))) {
6508 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6509 640cd7ff 2022-06-22 mark view->count = 0;
6510 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6511 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6514 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6515 941e9f74 2019-05-21 stsp if (err) {
6516 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6519 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6520 56e0773d 2019-11-28 stsp s->selected_entry))) {
6521 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6522 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6524 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6525 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6527 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6528 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6529 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6533 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6534 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6539 640cd7ff 2022-06-22 mark view->count = 0;
6540 e78dc838 2020-12-04 stsp view->focussed = 0;
6541 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6542 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6543 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6544 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6545 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6547 669b5ffa 2018-10-07 stsp return err;
6548 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6551 e78dc838 2020-12-04 stsp view->focus_child = 1;
6553 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6556 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6557 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6558 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6559 640cd7ff 2022-06-22 mark view->count = 0;
6562 640cd7ff 2022-06-22 mark view->count = 0;
6566 ffd1d5e5 2018-06-23 stsp return err;
6569 ffd1d5e5 2018-06-23 stsp __dead static void
6570 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6573 87411fa9 2022-06-16 stsp fprintf(stderr,
6574 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6575 ffd1d5e5 2018-06-23 stsp getprogname());
6579 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6580 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6582 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6583 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6584 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6585 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6586 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6587 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6588 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6589 4e97c21c 2020-12-06 stsp char *label = NULL;
6590 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6591 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6593 5221c383 2018-08-01 stsp struct tog_view *view;
6594 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6596 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6597 ffd1d5e5 2018-06-23 stsp switch (ch) {
6599 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6602 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6603 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6604 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6608 e99e2d15 2020-11-24 naddy usage_tree();
6609 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6613 ffd1d5e5 2018-06-23 stsp argc -= optind;
6614 ffd1d5e5 2018-06-23 stsp argv += optind;
6616 55cccc34 2020-02-20 stsp if (argc > 1)
6617 e99e2d15 2020-11-24 naddy usage_tree();
6619 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6620 0ae84acc 2022-06-15 tracey if (error != NULL)
6621 0ae84acc 2022-06-15 tracey goto done;
6623 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6624 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6625 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6626 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6627 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6628 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6629 c156c7a4 2020-12-18 stsp goto done;
6630 55cccc34 2020-02-20 stsp if (worktree)
6631 52185f70 2019-02-05 stsp repo_path =
6632 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6634 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6635 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6636 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6637 c156c7a4 2020-12-18 stsp goto done;
6641 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6642 c02c541e 2019-03-29 stsp if (error != NULL)
6643 52185f70 2019-02-05 stsp goto done;
6645 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6646 55cccc34 2020-02-20 stsp repo, worktree);
6647 55cccc34 2020-02-20 stsp if (error)
6648 55cccc34 2020-02-20 stsp goto done;
6650 55cccc34 2020-02-20 stsp init_curses();
6652 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6653 c02c541e 2019-03-29 stsp if (error)
6654 52185f70 2019-02-05 stsp goto done;
6656 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6657 51a10b52 2020-12-26 stsp if (error)
6658 51a10b52 2020-12-26 stsp goto done;
6660 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6661 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6662 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6663 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6664 4e97c21c 2020-12-06 stsp if (error)
6665 4e97c21c 2020-12-06 stsp goto done;
6666 4e97c21c 2020-12-06 stsp head_ref_name = label;
6668 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6669 4e97c21c 2020-12-06 stsp if (error == NULL)
6670 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6671 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6672 4e97c21c 2020-12-06 stsp goto done;
6673 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6674 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6675 4e97c21c 2020-12-06 stsp if (error)
6676 4e97c21c 2020-12-06 stsp goto done;
6679 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6680 a44927cc 2022-04-07 stsp if (error)
6681 a44927cc 2022-04-07 stsp goto done;
6683 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6684 5221c383 2018-08-01 stsp if (view == NULL) {
6685 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6686 5221c383 2018-08-01 stsp goto done;
6688 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6689 ad80ab7b 2018-08-04 stsp if (error)
6690 ad80ab7b 2018-08-04 stsp goto done;
6691 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6692 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6693 d91faf3b 2020-12-01 naddy in_repo_path);
6694 55cccc34 2020-02-20 stsp if (error)
6695 55cccc34 2020-02-20 stsp goto done;
6698 55cccc34 2020-02-20 stsp if (worktree) {
6699 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6700 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6701 55cccc34 2020-02-20 stsp worktree = NULL;
6703 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6705 52185f70 2019-02-05 stsp free(repo_path);
6706 e4a0e26d 2020-02-20 stsp free(cwd);
6707 ffd1d5e5 2018-06-23 stsp free(commit_id);
6708 4e97c21c 2020-12-06 stsp free(label);
6710 486cd271 2020-12-06 stsp got_ref_close(ref);
6711 1d0f4054 2021-06-17 stsp if (repo) {
6712 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6713 1d0f4054 2021-06-17 stsp if (error == NULL)
6714 1d0f4054 2021-06-17 stsp error = close_err;
6716 0ae84acc 2022-06-15 tracey if (pack_fds) {
6717 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6718 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6719 0ae84acc 2022-06-15 tracey if (error == NULL)
6720 0ae84acc 2022-06-15 tracey error = pack_err;
6722 51a10b52 2020-12-26 stsp tog_free_refs();
6723 ffd1d5e5 2018-06-23 stsp return error;
6726 6458efa5 2020-11-24 stsp static const struct got_error *
6727 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6729 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6730 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6732 6458efa5 2020-11-24 stsp s->nrefs = 0;
6733 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6734 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6735 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6736 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6737 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6740 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6741 6458efa5 2020-11-24 stsp if (re == NULL)
6742 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6744 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6745 8924d611 2020-12-26 stsp if (re->ref == NULL)
6746 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6747 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6748 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6751 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6752 6458efa5 2020-11-24 stsp return NULL;
6756 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6758 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6760 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6761 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6762 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6763 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6768 6458efa5 2020-11-24 stsp static const struct got_error *
6769 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6771 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6772 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6774 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6775 6458efa5 2020-11-24 stsp s->repo = repo;
6777 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6778 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6780 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6782 6458efa5 2020-11-24 stsp return err;
6784 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6785 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6786 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6787 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6789 6458efa5 2020-11-24 stsp goto done;
6791 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6792 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6793 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6795 6458efa5 2020-11-24 stsp goto done;
6797 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6798 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6799 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6801 cc488aa7 2022-01-23 stsp goto done;
6803 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6804 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6805 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6807 6458efa5 2020-11-24 stsp goto done;
6810 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6811 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6812 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6813 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6814 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6817 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6818 6458efa5 2020-11-24 stsp return err;
6821 6458efa5 2020-11-24 stsp static const struct got_error *
6822 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6824 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6826 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6827 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6829 6458efa5 2020-11-24 stsp return NULL;
6832 6458efa5 2020-11-24 stsp static const struct got_error *
6833 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6834 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6836 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6837 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6838 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6839 6458efa5 2020-11-24 stsp int obj_type;
6841 c42c9805 2020-11-24 stsp *commit_id = NULL;
6843 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6845 6458efa5 2020-11-24 stsp return err;
6847 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6849 6458efa5 2020-11-24 stsp goto done;
6851 6458efa5 2020-11-24 stsp switch (obj_type) {
6852 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6853 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6855 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6856 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6858 6458efa5 2020-11-24 stsp goto done;
6859 c42c9805 2020-11-24 stsp free(obj_id);
6860 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6861 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6863 6458efa5 2020-11-24 stsp goto done;
6864 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6865 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6866 c42c9805 2020-11-24 stsp goto done;
6868 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6869 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6870 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6871 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6872 c42c9805 2020-11-24 stsp goto done;
6876 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6882 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6883 c42c9805 2020-11-24 stsp if (err) {
6884 c42c9805 2020-11-24 stsp free(*commit_id);
6885 c42c9805 2020-11-24 stsp *commit_id = NULL;
6887 c42c9805 2020-11-24 stsp return err;
6890 c42c9805 2020-11-24 stsp static const struct got_error *
6891 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6892 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6894 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6895 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6896 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6898 c42c9805 2020-11-24 stsp *new_view = NULL;
6900 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6901 c42c9805 2020-11-24 stsp if (err) {
6902 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6903 c42c9805 2020-11-24 stsp return err;
6905 c42c9805 2020-11-24 stsp return NULL;
6908 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6909 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6910 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6911 6458efa5 2020-11-24 stsp goto done;
6914 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6915 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6918 6458efa5 2020-11-24 stsp view_close(log_view);
6920 6458efa5 2020-11-24 stsp *new_view = log_view;
6921 c42c9805 2020-11-24 stsp free(commit_id);
6922 6458efa5 2020-11-24 stsp return err;
6925 ce5b7c56 2019-07-09 stsp static void
6926 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6928 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6929 34ba6917 2020-11-30 stsp int i = 0;
6931 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6934 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6935 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6936 34ba6917 2020-11-30 stsp if (re == NULL)
6938 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6939 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6943 9b058f45 2022-06-30 mark static const struct got_error *
6944 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
6946 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
6947 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6948 6458efa5 2020-11-24 stsp int n = 0;
6950 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6951 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6953 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6955 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6956 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6958 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
6959 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
6960 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6961 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6965 9b058f45 2022-06-30 mark return NULL;
6968 6458efa5 2020-11-24 stsp static const struct got_error *
6969 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6971 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6973 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6974 6458efa5 2020-11-24 stsp return NULL;
6977 6458efa5 2020-11-24 stsp static int
6978 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6980 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6982 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6986 6458efa5 2020-11-24 stsp static const struct got_error *
6987 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6989 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6990 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6992 6458efa5 2020-11-24 stsp if (!view->searching) {
6993 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6994 6458efa5 2020-11-24 stsp return NULL;
6997 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6998 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6999 6458efa5 2020-11-24 stsp if (s->selected_entry)
7000 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7002 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7003 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7005 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7006 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7008 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7009 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7012 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7013 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7014 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7015 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7017 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7020 6458efa5 2020-11-24 stsp while (1) {
7021 6458efa5 2020-11-24 stsp if (re == NULL) {
7022 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7023 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7024 6458efa5 2020-11-24 stsp return NULL;
7026 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7027 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7029 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7032 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7033 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7034 6458efa5 2020-11-24 stsp s->matched_entry = re;
7038 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7039 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7041 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7044 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7045 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7046 6458efa5 2020-11-24 stsp s->selected = 0;
7049 6458efa5 2020-11-24 stsp return NULL;
7052 6458efa5 2020-11-24 stsp static const struct got_error *
7053 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7055 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7056 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7057 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7058 6458efa5 2020-11-24 stsp char *line = NULL;
7059 6458efa5 2020-11-24 stsp wchar_t *wline;
7060 6458efa5 2020-11-24 stsp struct tog_color *tc;
7061 6458efa5 2020-11-24 stsp int width, n;
7062 6458efa5 2020-11-24 stsp int limit = view->nlines;
7064 6458efa5 2020-11-24 stsp werase(view->window);
7066 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7067 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7068 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
7069 9b058f45 2022-06-30 mark --limit; /* border */
7071 6458efa5 2020-11-24 stsp if (limit == 0)
7072 6458efa5 2020-11-24 stsp return NULL;
7074 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7076 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7077 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7078 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7080 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7081 6458efa5 2020-11-24 stsp if (err) {
7082 6458efa5 2020-11-24 stsp free(line);
7083 6458efa5 2020-11-24 stsp return err;
7085 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7086 6458efa5 2020-11-24 stsp wstandout(view->window);
7087 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7088 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7089 6458efa5 2020-11-24 stsp wstandend(view->window);
7090 6458efa5 2020-11-24 stsp free(wline);
7091 6458efa5 2020-11-24 stsp wline = NULL;
7092 6458efa5 2020-11-24 stsp free(line);
7093 6458efa5 2020-11-24 stsp line = NULL;
7094 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7095 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7096 6458efa5 2020-11-24 stsp if (--limit <= 0)
7097 6458efa5 2020-11-24 stsp return NULL;
7100 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7101 6458efa5 2020-11-24 stsp char *line = NULL;
7102 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7104 b4996bee 2022-06-16 stsp if (s->show_date) {
7105 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7106 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7107 b4996bee 2022-06-16 stsp struct got_object_id *id;
7108 b4996bee 2022-06-16 stsp struct tm tm;
7111 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7113 b4996bee 2022-06-16 stsp return err;
7114 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7115 b4996bee 2022-06-16 stsp if (err) {
7116 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7118 b4996bee 2022-06-16 stsp return err;
7120 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7122 b4996bee 2022-06-16 stsp if (err) {
7124 b4996bee 2022-06-16 stsp return err;
7126 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7127 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7129 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7130 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7133 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7134 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7135 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7136 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7138 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7139 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7140 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7141 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7142 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7143 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7144 6458efa5 2020-11-24 stsp struct got_object_id *id;
7145 6458efa5 2020-11-24 stsp char *id_str;
7146 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7148 6458efa5 2020-11-24 stsp return err;
7149 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7150 6458efa5 2020-11-24 stsp if (err) {
7152 6458efa5 2020-11-24 stsp return err;
7154 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7155 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7156 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7158 6458efa5 2020-11-24 stsp free(id_str);
7159 6458efa5 2020-11-24 stsp return err;
7162 6458efa5 2020-11-24 stsp free(id_str);
7163 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7164 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7165 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7167 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7169 6458efa5 2020-11-24 stsp if (err) {
7170 6458efa5 2020-11-24 stsp free(line);
7171 6458efa5 2020-11-24 stsp return err;
7173 6458efa5 2020-11-24 stsp if (n == s->selected) {
7174 6458efa5 2020-11-24 stsp if (view->focussed)
7175 6458efa5 2020-11-24 stsp wstandout(view->window);
7176 6458efa5 2020-11-24 stsp s->selected_entry = re;
7178 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7180 6458efa5 2020-11-24 stsp wattr_on(view->window,
7181 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7182 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7184 6458efa5 2020-11-24 stsp wattr_off(view->window,
7185 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7186 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7187 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7188 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7189 6458efa5 2020-11-24 stsp wstandend(view->window);
7190 6458efa5 2020-11-24 stsp free(line);
7191 6458efa5 2020-11-24 stsp free(wline);
7192 6458efa5 2020-11-24 stsp wline = NULL;
7194 6458efa5 2020-11-24 stsp s->ndisplayed++;
7195 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7198 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7201 9b058f45 2022-06-30 mark view_border(view);
7202 6458efa5 2020-11-24 stsp return err;
7205 6458efa5 2020-11-24 stsp static const struct got_error *
7206 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
7207 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7209 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7210 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7211 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7213 c42c9805 2020-11-24 stsp *new_view = NULL;
7215 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7216 c42c9805 2020-11-24 stsp if (err) {
7217 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7218 c42c9805 2020-11-24 stsp return err;
7220 c42c9805 2020-11-24 stsp return NULL;
7224 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7225 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7226 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7227 c42c9805 2020-11-24 stsp goto done;
7230 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7231 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7233 c42c9805 2020-11-24 stsp goto done;
7235 c42c9805 2020-11-24 stsp *new_view = tree_view;
7237 c42c9805 2020-11-24 stsp free(commit_id);
7238 c42c9805 2020-11-24 stsp return err;
7240 c42c9805 2020-11-24 stsp static const struct got_error *
7241 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7243 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7244 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7245 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7246 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7247 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7249 6458efa5 2020-11-24 stsp switch (ch) {
7251 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7252 640cd7ff 2022-06-22 mark view->count = 0;
7255 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7256 640cd7ff 2022-06-22 mark view->count = 0;
7259 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7260 640cd7ff 2022-06-22 mark view->count = 0;
7261 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7262 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7263 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7266 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7267 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7268 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7271 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7272 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7274 6458efa5 2020-11-24 stsp case KEY_ENTER:
7275 6458efa5 2020-11-24 stsp case '\r':
7276 640cd7ff 2022-06-22 mark view->count = 0;
7277 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7279 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7280 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7282 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7283 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7287 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7288 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7293 e78dc838 2020-12-04 stsp view->focussed = 0;
7294 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7295 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7296 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7297 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7298 6458efa5 2020-11-24 stsp err = view_close_child(view);
7300 6458efa5 2020-11-24 stsp return err;
7301 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7304 e78dc838 2020-12-04 stsp view->focus_child = 1;
7306 6458efa5 2020-11-24 stsp *new_view = log_view;
7309 640cd7ff 2022-06-22 mark view->count = 0;
7310 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7312 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7313 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
7314 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7316 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7318 e78dc838 2020-12-04 stsp view->focussed = 0;
7319 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7320 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7321 c42c9805 2020-11-24 stsp err = view_close_child(view);
7323 c42c9805 2020-11-24 stsp return err;
7324 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7327 e78dc838 2020-12-04 stsp view->focus_child = 1;
7329 c42c9805 2020-11-24 stsp *new_view = tree_view;
7331 e4526bf5 2021-09-03 naddy case 'g':
7332 e4526bf5 2021-09-03 naddy case KEY_HOME:
7333 e4526bf5 2021-09-03 naddy s->selected = 0;
7334 640cd7ff 2022-06-22 mark view->count = 0;
7335 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7337 e4526bf5 2021-09-03 naddy case 'G':
7338 9b058f45 2022-06-30 mark case KEY_END: {
7339 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7341 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7342 9b058f45 2022-06-30 mark --eos; /* border */
7343 e4526bf5 2021-09-03 naddy s->selected = 0;
7344 640cd7ff 2022-06-22 mark view->count = 0;
7345 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7346 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7347 e4526bf5 2021-09-03 naddy if (re == NULL)
7349 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7350 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7352 e4526bf5 2021-09-03 naddy if (n > 0)
7353 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7357 6458efa5 2020-11-24 stsp case KEY_UP:
7358 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7359 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7360 6458efa5 2020-11-24 stsp s->selected--;
7363 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7364 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7365 640cd7ff 2022-06-22 mark view->count = 0;
7367 83cc4199 2022-06-13 stsp case CTRL('u'):
7369 83cc4199 2022-06-13 stsp nscroll /= 2;
7370 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7371 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7372 6458efa5 2020-11-24 stsp case CTRL('b'):
7374 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7375 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7376 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7377 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7378 640cd7ff 2022-06-22 mark view->count = 0;
7381 6458efa5 2020-11-24 stsp case KEY_DOWN:
7382 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7383 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7384 6458efa5 2020-11-24 stsp s->selected++;
7387 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7388 6458efa5 2020-11-24 stsp /* can't scroll any further */
7389 640cd7ff 2022-06-22 mark view->count = 0;
7392 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7394 83cc4199 2022-06-13 stsp case CTRL('d'):
7396 83cc4199 2022-06-13 stsp nscroll /= 2;
7397 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7398 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7399 6458efa5 2020-11-24 stsp case CTRL('f'):
7401 48bb96f0 2022-06-20 naddy case ' ':
7402 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7403 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7404 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7405 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7406 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7407 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7408 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7409 640cd7ff 2022-06-22 mark view->count = 0;
7412 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7414 6458efa5 2020-11-24 stsp case CTRL('l'):
7415 640cd7ff 2022-06-22 mark view->count = 0;
7416 8924d611 2020-12-26 stsp tog_free_refs();
7417 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7420 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7421 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7423 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7424 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7425 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7428 640cd7ff 2022-06-22 mark view->count = 0;
7432 6458efa5 2020-11-24 stsp return err;
7435 6458efa5 2020-11-24 stsp __dead static void
7436 6458efa5 2020-11-24 stsp usage_ref(void)
7439 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7440 6458efa5 2020-11-24 stsp getprogname());
7444 6458efa5 2020-11-24 stsp static const struct got_error *
7445 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7447 6458efa5 2020-11-24 stsp const struct got_error *error;
7448 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7449 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7450 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7452 6458efa5 2020-11-24 stsp struct tog_view *view;
7453 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7455 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7456 6458efa5 2020-11-24 stsp switch (ch) {
7458 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7459 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7460 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7464 e99e2d15 2020-11-24 naddy usage_ref();
7465 6458efa5 2020-11-24 stsp /* NOTREACHED */
7469 6458efa5 2020-11-24 stsp argc -= optind;
7470 6458efa5 2020-11-24 stsp argv += optind;
7472 6458efa5 2020-11-24 stsp if (argc > 1)
7473 e99e2d15 2020-11-24 naddy usage_ref();
7475 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7476 0ae84acc 2022-06-15 tracey if (error != NULL)
7477 0ae84acc 2022-06-15 tracey goto done;
7479 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7480 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7481 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7482 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7483 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7484 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7485 c156c7a4 2020-12-18 stsp goto done;
7486 6458efa5 2020-11-24 stsp if (worktree)
7487 6458efa5 2020-11-24 stsp repo_path =
7488 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7490 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7491 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7492 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7493 c156c7a4 2020-12-18 stsp goto done;
7497 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7498 6458efa5 2020-11-24 stsp if (error != NULL)
7499 6458efa5 2020-11-24 stsp goto done;
7501 6458efa5 2020-11-24 stsp init_curses();
7503 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7504 51a10b52 2020-12-26 stsp if (error)
7505 51a10b52 2020-12-26 stsp goto done;
7507 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7508 6458efa5 2020-11-24 stsp if (error)
7509 6458efa5 2020-11-24 stsp goto done;
7511 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7512 6458efa5 2020-11-24 stsp if (view == NULL) {
7513 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7514 6458efa5 2020-11-24 stsp goto done;
7517 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7518 6458efa5 2020-11-24 stsp if (error)
7519 6458efa5 2020-11-24 stsp goto done;
7521 6458efa5 2020-11-24 stsp if (worktree) {
7522 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7523 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7524 6458efa5 2020-11-24 stsp worktree = NULL;
7526 6458efa5 2020-11-24 stsp error = view_loop(view);
7528 6458efa5 2020-11-24 stsp free(repo_path);
7529 6458efa5 2020-11-24 stsp free(cwd);
7530 1d0f4054 2021-06-17 stsp if (repo) {
7531 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7532 1d0f4054 2021-06-17 stsp if (close_err)
7533 1d0f4054 2021-06-17 stsp error = close_err;
7535 0ae84acc 2022-06-15 tracey if (pack_fds) {
7536 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7537 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7538 0ae84acc 2022-06-15 tracey if (error == NULL)
7539 0ae84acc 2022-06-15 tracey error = pack_err;
7541 51a10b52 2020-12-26 stsp tog_free_refs();
7542 6458efa5 2020-11-24 stsp return error;
7546 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7547 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7549 6458efa5 2020-11-24 stsp static void
7550 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7552 9b058f45 2022-06-30 mark switch (view->type) {
7553 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7554 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7555 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7556 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7560 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7561 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7563 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7564 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7567 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7568 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7569 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7571 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7572 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7573 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7575 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7576 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7577 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7583 9b058f45 2022-06-30 mark view->offset = 0;
7587 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7588 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7590 9b058f45 2022-06-30 mark static const struct got_error *
7591 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7593 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7594 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7595 9b058f45 2022-06-30 mark int *selected = NULL;
7596 9b058f45 2022-06-30 mark int header, offset;
7598 9b058f45 2022-06-30 mark switch (view->type) {
7599 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7600 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7601 9b058f45 2022-06-30 mark header = 3;
7602 9b058f45 2022-06-30 mark scrolld = NULL;
7603 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7604 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7605 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7606 9b058f45 2022-06-30 mark s->selected_line -= offset;
7607 9b058f45 2022-06-30 mark view->offset = offset;
7611 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7612 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7613 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7614 9b058f45 2022-06-30 mark header = 3;
7615 9b058f45 2022-06-30 mark selected = &s->selected;
7618 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7619 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7620 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7621 9b058f45 2022-06-30 mark header = 3;
7622 9b058f45 2022-06-30 mark selected = &s->selected;
7625 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7626 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7627 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7628 9b058f45 2022-06-30 mark header = 5;
7629 9b058f45 2022-06-30 mark selected = &s->selected;
7633 9b058f45 2022-06-30 mark selected = NULL;
7634 9b058f45 2022-06-30 mark scrolld = NULL;
7635 9b058f45 2022-06-30 mark header = 0;
7639 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7640 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7641 9b058f45 2022-06-30 mark view->offset = offset;
7642 9b058f45 2022-06-30 mark if (scrolld && offset) {
7643 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7644 9b058f45 2022-06-30 mark *selected -= offset;
7648 9b058f45 2022-06-30 mark return err;
7651 9b058f45 2022-06-30 mark static void
7652 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7656 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7657 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7658 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7659 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7661 6879ba42 2020-10-01 naddy fputc('\n', fp);
7664 4ed7e80c 2018-05-20 stsp __dead static void
7665 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7667 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7669 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7670 6879ba42 2020-10-01 naddy getprogname());
7671 6879ba42 2020-10-01 naddy if (hflag) {
7672 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7673 6879ba42 2020-10-01 naddy list_commands(fp);
7675 6879ba42 2020-10-01 naddy exit(status);
7678 c2301be8 2018-04-30 stsp static char **
7679 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7681 ee85c5e8 2020-02-29 stsp va_list ap;
7682 c2301be8 2018-04-30 stsp char **argv;
7685 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7687 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7688 c2301be8 2018-04-30 stsp if (argv == NULL)
7689 c2301be8 2018-04-30 stsp err(1, "calloc");
7690 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7691 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7692 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7693 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7696 ee85c5e8 2020-02-29 stsp va_end(ap);
7697 c2301be8 2018-04-30 stsp return argv;
7701 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7702 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7703 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7704 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7705 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7707 ee85c5e8 2020-02-29 stsp static const struct got_error *
7708 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7710 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7711 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7712 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7713 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7714 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7715 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7716 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7717 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7718 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7720 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7721 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7722 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7724 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7725 0ae84acc 2022-06-15 tracey if (error != NULL)
7726 0ae84acc 2022-06-15 tracey goto done;
7728 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7729 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7730 ee85c5e8 2020-02-29 stsp goto done;
7732 ee85c5e8 2020-02-29 stsp if (worktree)
7733 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7735 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7736 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7737 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7738 ee85c5e8 2020-02-29 stsp goto done;
7741 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7742 ee85c5e8 2020-02-29 stsp if (error != NULL)
7743 ee85c5e8 2020-02-29 stsp goto done;
7745 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7746 ee85c5e8 2020-02-29 stsp repo, worktree);
7747 ee85c5e8 2020-02-29 stsp if (error)
7748 ee85c5e8 2020-02-29 stsp goto done;
7750 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7751 84de9106 2020-12-26 stsp if (error)
7752 84de9106 2020-12-26 stsp goto done;
7753 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7754 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7755 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7756 ee85c5e8 2020-02-29 stsp if (error)
7757 ee85c5e8 2020-02-29 stsp goto done;
7759 ee85c5e8 2020-02-29 stsp if (worktree) {
7760 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7761 ee85c5e8 2020-02-29 stsp worktree = NULL;
7764 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7765 a44927cc 2022-04-07 stsp if (error)
7766 a44927cc 2022-04-07 stsp goto done;
7768 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7769 ee85c5e8 2020-02-29 stsp if (error) {
7770 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7771 ee85c5e8 2020-02-29 stsp goto done;
7772 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7773 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7774 6879ba42 2020-10-01 naddy usage(1, 1);
7775 ee85c5e8 2020-02-29 stsp /* not reached */
7778 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7779 1d0f4054 2021-06-17 stsp if (error == NULL)
7780 1d0f4054 2021-06-17 stsp error = close_err;
7781 ee85c5e8 2020-02-29 stsp repo = NULL;
7783 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7784 ee85c5e8 2020-02-29 stsp if (error)
7785 ee85c5e8 2020-02-29 stsp goto done;
7787 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7789 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7790 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7792 1d0f4054 2021-06-17 stsp if (repo) {
7793 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7794 1d0f4054 2021-06-17 stsp if (error == NULL)
7795 1d0f4054 2021-06-17 stsp error = close_err;
7797 a44927cc 2022-04-07 stsp if (commit)
7798 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7799 ee85c5e8 2020-02-29 stsp if (worktree)
7800 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7801 0ae84acc 2022-06-15 tracey if (pack_fds) {
7802 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7803 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7804 0ae84acc 2022-06-15 tracey if (error == NULL)
7805 0ae84acc 2022-06-15 tracey error = pack_err;
7808 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7809 ee85c5e8 2020-02-29 stsp free(commit_id);
7810 ee85c5e8 2020-02-29 stsp free(cwd);
7811 ee85c5e8 2020-02-29 stsp free(repo_path);
7812 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7813 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7815 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7816 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7817 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7819 87670572 2020-12-26 naddy tog_free_refs();
7820 ee85c5e8 2020-02-29 stsp return error;
7824 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7826 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7827 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7828 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7829 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7830 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7831 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7832 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7835 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7837 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7838 9f7d7167 2018-04-29 stsp switch (ch) {
7840 9f7d7167 2018-04-29 stsp hflag = 1;
7843 53ccebc2 2019-07-30 stsp Vflag = 1;
7846 6879ba42 2020-10-01 naddy usage(hflag, 1);
7847 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7851 9f7d7167 2018-04-29 stsp argc -= optind;
7852 9f7d7167 2018-04-29 stsp argv += optind;
7853 9814e6a3 2020-09-27 naddy optind = 1;
7854 c2301be8 2018-04-30 stsp optreset = 1;
7856 53ccebc2 2019-07-30 stsp if (Vflag) {
7857 53ccebc2 2019-07-30 stsp got_version_print_str();
7858 6879ba42 2020-10-01 naddy return 0;
7861 4010e238 2020-12-04 stsp #ifndef PROFILE
7862 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7863 4010e238 2020-12-04 stsp NULL) == -1)
7864 4010e238 2020-12-04 stsp err(1, "pledge");
7867 c2301be8 2018-04-30 stsp if (argc == 0) {
7868 f29d3e89 2018-06-23 stsp if (hflag)
7869 6879ba42 2020-10-01 naddy usage(hflag, 0);
7870 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7871 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7873 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7877 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7878 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7879 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7880 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7881 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7887 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7888 ee85c5e8 2020-02-29 stsp if (argc != 1)
7889 6879ba42 2020-10-01 naddy usage(0, 1);
7890 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7891 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7893 ee85c5e8 2020-02-29 stsp if (hflag)
7894 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7896 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7900 b46c1e04 2020-09-20 naddy putchar('\n');
7901 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7903 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7904 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7905 a2f4a359 2020-02-28 stsp free(cmd_argv);
7908 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7909 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);