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 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
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 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
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 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
2909 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
2911 b880cc75 2022-06-30 mark return err;
2914 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
2915 0dca135e 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2916 0dca135e 2022-06-30 mark view_is_splitscreen(view->child))
2917 0dca135e 2022-06-30 mark --eos; /* border */
2920 528dedf3 2021-08-30 stsp switch (ch) {
2922 1e37a5c2 2019-05-12 jcs s->quit = 1;
2925 145b6838 2022-06-16 stsp view->x = 0;
2928 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2929 640cd7ff 2022-06-22 mark view->count = 0;
2931 145b6838 2022-06-16 stsp case KEY_RIGHT:
2933 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2934 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2936 640cd7ff 2022-06-22 mark view->count = 0;
2938 145b6838 2022-06-16 stsp case KEY_LEFT:
2940 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2941 640cd7ff 2022-06-22 mark if (view->x <= 0)
2942 640cd7ff 2022-06-22 mark view->count = 0;
2945 1e37a5c2 2019-05-12 jcs case KEY_UP:
2948 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2949 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
2952 912a3f79 2021-08-30 j case KEY_HOME:
2953 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
2954 640cd7ff 2022-06-22 mark view->count = 0;
2956 83cc4199 2022-06-13 stsp case CTRL('u'):
2958 83cc4199 2022-06-13 stsp nscroll /= 2;
2959 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2960 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2961 a4292ac5 2019-05-12 jcs case CTRL('b'):
2963 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
2966 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2969 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2970 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
2973 912a3f79 2021-08-30 j case KEY_END: {
2974 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2975 912a3f79 2021-08-30 j * traverse them all. */
2976 640cd7ff 2022-06-22 mark view->count = 0;
2977 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2978 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2979 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2982 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2983 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2984 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
2985 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2987 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2988 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2990 f3bc9f1d 2021-09-05 naddy if (n > 0)
2991 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2992 2b779855 2020-12-05 naddy select_commit(s);
2995 80b7e8da 2022-06-11 stsp case CTRL('d'):
2997 83cc4199 2022-06-13 stsp nscroll /= 2;
2998 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2999 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3000 61417565 2022-06-20 mark case CTRL('f'):
3001 48bb96f0 2022-06-20 naddy case 'f':
3003 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3005 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3006 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3007 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3008 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3009 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3010 2b779855 2020-12-05 naddy select_commit(s);
3011 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3012 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3013 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3014 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3015 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3018 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3019 9b058f45 2022-06-30 mark case '\r': {
3020 640cd7ff 2022-06-22 mark view->count = 0;
3021 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3024 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3025 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3026 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3028 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3029 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3030 78756c87 2020-11-24 stsp view, s->repo);
3034 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3035 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3040 e78dc838 2020-12-04 stsp view->focussed = 0;
3041 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3042 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3043 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3045 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3046 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3048 1e37a5c2 2019-05-12 jcs return err;
3049 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3052 e78dc838 2020-12-04 stsp view->focus_child = 1;
3054 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3058 640cd7ff 2022-06-22 mark view->count = 0;
3059 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3061 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3062 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3063 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
3064 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3068 e78dc838 2020-12-04 stsp view->focussed = 0;
3069 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3070 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3071 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3073 1e37a5c2 2019-05-12 jcs return err;
3074 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3077 e78dc838 2020-12-04 stsp view->focus_child = 1;
3079 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3081 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3082 21355643 2020-12-06 stsp case CTRL('l'):
3084 640cd7ff 2022-06-22 mark view->count = 0;
3085 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3086 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3088 21355643 2020-12-06 stsp err = stop_log_thread(s);
3090 74cfe85e 2020-10-20 stsp return err;
3091 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3092 21355643 2020-12-06 stsp char *parent_path;
3093 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3095 21355643 2020-12-06 stsp return err;
3096 21355643 2020-12-06 stsp free(s->in_repo_path);
3097 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3098 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3099 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3100 21355643 2020-12-06 stsp struct got_object_id *start_id;
3101 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3102 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3103 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3105 21355643 2020-12-06 stsp return err;
3106 21355643 2020-12-06 stsp free(s->start_id);
3107 21355643 2020-12-06 stsp s->start_id = start_id;
3108 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3109 21355643 2020-12-06 stsp } else /* 'B' */
3110 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3112 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3113 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3115 b0dd8d27 2022-06-16 stsp return err;
3117 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3118 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3119 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3121 21355643 2020-12-06 stsp return err;
3122 51a10b52 2020-12-26 stsp tog_free_refs();
3123 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3125 ca51c541 2020-12-07 stsp return err;
3126 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3127 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3129 d01904d4 2019-06-25 stsp return err;
3130 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3131 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3133 b672a97a 2020-01-27 stsp return err;
3134 21355643 2020-12-06 stsp free_commits(&s->commits);
3135 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3136 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3137 21355643 2020-12-06 stsp s->selected_entry = NULL;
3138 21355643 2020-12-06 stsp s->selected = 0;
3139 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3140 21355643 2020-12-06 stsp s->quit = 0;
3141 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3142 dfee752e 2022-06-18 op s->matched_entry = NULL;
3143 dfee752e 2022-06-18 op s->search_entry = NULL;
3146 640cd7ff 2022-06-22 mark view->count = 0;
3147 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3148 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
3149 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
3150 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
3151 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3152 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3153 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3154 6458efa5 2020-11-24 stsp if (err) {
3155 6458efa5 2020-11-24 stsp view_close(ref_view);
3156 6458efa5 2020-11-24 stsp return err;
3158 e78dc838 2020-12-04 stsp view->focussed = 0;
3159 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3160 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3161 6458efa5 2020-11-24 stsp err = view_close_child(view);
3163 6458efa5 2020-11-24 stsp return err;
3164 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3167 e78dc838 2020-12-04 stsp view->focus_child = 1;
3169 6458efa5 2020-11-24 stsp *new_view = ref_view;
3172 640cd7ff 2022-06-22 mark view->count = 0;
3176 80ddbec8 2018-04-29 stsp return err;
3179 4ed7e80c 2018-05-20 stsp static const struct got_error *
3180 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3182 c2db6724 2019-01-04 stsp const struct got_error *error;
3184 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3185 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3186 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3188 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3189 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3191 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3192 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3194 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3195 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3197 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3198 c2db6724 2019-01-04 stsp if (error != NULL)
3199 c2db6724 2019-01-04 stsp return error;
3201 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3202 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3204 c2db6724 2019-01-04 stsp return NULL;
3207 a915003a 2019-02-05 stsp static void
3208 a915003a 2019-02-05 stsp init_curses(void)
3211 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3212 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3213 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3215 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3216 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3217 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3218 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3219 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3221 a915003a 2019-02-05 stsp initscr();
3223 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3226 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3227 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3228 a915003a 2019-02-05 stsp curs_set(0);
3229 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3230 6d17833f 2019-11-08 stsp start_color();
3231 6d17833f 2019-11-08 stsp use_default_colors();
3235 c2db6724 2019-01-04 stsp static const struct got_error *
3236 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3237 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3239 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3241 f135c941 2020-02-20 stsp if (argc == 0) {
3242 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3243 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3244 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3245 f135c941 2020-02-20 stsp return NULL;
3248 f135c941 2020-02-20 stsp if (worktree) {
3249 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3252 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3254 f135c941 2020-02-20 stsp return err;
3255 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3256 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3257 bfd61697 2020-11-14 stsp p) == -1) {
3258 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3259 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3263 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3265 f135c941 2020-02-20 stsp return err;
3268 f135c941 2020-02-20 stsp static const struct got_error *
3269 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3271 80ddbec8 2018-04-29 stsp const struct got_error *error;
3272 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3273 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3274 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3275 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3276 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3277 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3278 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3279 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3280 04cc582a 2018-08-01 stsp struct tog_view *view;
3281 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3283 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3284 80ddbec8 2018-04-29 stsp switch (ch) {
3286 b672a97a 2020-01-27 stsp log_branches = 1;
3289 80ddbec8 2018-04-29 stsp start_commit = optarg;
3292 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3293 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3294 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3298 17020d27 2019-03-07 stsp usage_log();
3299 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3303 80ddbec8 2018-04-29 stsp argc -= optind;
3304 80ddbec8 2018-04-29 stsp argv += optind;
3306 f135c941 2020-02-20 stsp if (argc > 1)
3307 f135c941 2020-02-20 stsp usage_log();
3309 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3310 0ae84acc 2022-06-15 tracey if (error != NULL)
3311 0ae84acc 2022-06-15 tracey goto done;
3313 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3314 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3315 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3316 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3317 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3318 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3319 c156c7a4 2020-12-18 stsp goto done;
3320 a1fbf39a 2019-08-11 stsp if (worktree)
3321 6962eb72 2020-02-20 stsp repo_path =
3322 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3324 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3325 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3326 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3327 c156c7a4 2020-12-18 stsp goto done;
3331 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3332 80ddbec8 2018-04-29 stsp if (error != NULL)
3333 ecb28ae0 2018-07-16 stsp goto done;
3335 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3336 f135c941 2020-02-20 stsp repo, worktree);
3337 f135c941 2020-02-20 stsp if (error)
3338 f135c941 2020-02-20 stsp goto done;
3340 f135c941 2020-02-20 stsp init_curses();
3342 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3343 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3344 c02c541e 2019-03-29 stsp if (error)
3345 c02c541e 2019-03-29 stsp goto done;
3347 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3348 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3349 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3350 87670572 2020-12-26 naddy if (error)
3351 87670572 2020-12-26 naddy goto done;
3354 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3355 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3356 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3357 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3358 d8f38dc4 2020-12-05 stsp if (error)
3359 d8f38dc4 2020-12-05 stsp goto done;
3360 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3362 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3363 d8f38dc4 2020-12-05 stsp if (error == NULL)
3364 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3365 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3366 d8f38dc4 2020-12-05 stsp goto done;
3367 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3368 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3369 d8f38dc4 2020-12-05 stsp if (error)
3370 d8f38dc4 2020-12-05 stsp goto done;
3373 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3374 04cc582a 2018-08-01 stsp if (view == NULL) {
3375 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3376 04cc582a 2018-08-01 stsp goto done;
3378 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3379 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3380 ba4f502b 2018-08-04 stsp if (error)
3381 ba4f502b 2018-08-04 stsp goto done;
3382 2fc00ff4 2019-08-31 stsp if (worktree) {
3383 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3384 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3385 2fc00ff4 2019-08-31 stsp worktree = NULL;
3387 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3389 f135c941 2020-02-20 stsp free(in_repo_path);
3390 ecb28ae0 2018-07-16 stsp free(repo_path);
3391 ecb28ae0 2018-07-16 stsp free(cwd);
3392 899d86c2 2018-05-10 stsp free(start_id);
3393 d8f38dc4 2020-12-05 stsp free(label);
3395 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3396 1d0f4054 2021-06-17 stsp if (repo) {
3397 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3398 1d0f4054 2021-06-17 stsp if (error == NULL)
3399 1d0f4054 2021-06-17 stsp error = close_err;
3401 ec142235 2019-03-07 stsp if (worktree)
3402 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3403 0ae84acc 2022-06-15 tracey if (pack_fds) {
3404 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3405 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3406 0ae84acc 2022-06-15 tracey if (error == NULL)
3407 0ae84acc 2022-06-15 tracey error = pack_err;
3409 51a10b52 2020-12-26 stsp tog_free_refs();
3410 80ddbec8 2018-04-29 stsp return error;
3413 4ed7e80c 2018-05-20 stsp __dead static void
3414 9f7d7167 2018-04-29 stsp usage_diff(void)
3417 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3418 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3422 6d17833f 2019-11-08 stsp static int
3423 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3424 41605754 2020-11-12 stsp regmatch_t *regmatch)
3426 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3429 336075a4 2022-06-25 op static struct tog_color *
3430 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3432 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3434 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3435 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3436 6d17833f 2019-11-08 stsp return tc;
3439 6d17833f 2019-11-08 stsp return NULL;
3442 4ed7e80c 2018-05-20 stsp static const struct got_error *
3443 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3444 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3446 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3447 44a87665 2022-06-16 stsp char *exstr = NULL;
3448 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3449 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3450 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3451 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3453 41605754 2020-11-12 stsp *wtotal = 0;
3455 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3456 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3458 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3460 44a87665 2022-06-16 stsp return err;
3462 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3463 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3464 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3465 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3466 44a87665 2022-06-16 stsp goto done;
3468 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3469 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3470 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3471 1853e0f4 2022-06-16 stsp goto done;
3473 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3474 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3475 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3476 1853e0f4 2022-06-16 stsp goto done;
3479 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3480 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3481 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3483 1853e0f4 2022-06-16 stsp goto done;
3484 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3486 1853e0f4 2022-06-16 stsp free(wline);
3487 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3488 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3490 1853e0f4 2022-06-16 stsp goto done;
3491 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3492 1853e0f4 2022-06-16 stsp wlimit -= width;
3493 1853e0f4 2022-06-16 stsp *wtotal += width;
3496 41605754 2020-11-12 stsp if (wlimit > 0) {
3497 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3498 1853e0f4 2022-06-16 stsp size_t wlen;
3500 1853e0f4 2022-06-16 stsp free(wline);
3501 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3502 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3504 1853e0f4 2022-06-16 stsp goto done;
3505 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3506 1853e0f4 2022-06-16 stsp while (i < wlen) {
3507 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3508 1853e0f4 2022-06-16 stsp if (width == -1) {
3509 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3510 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3511 1853e0f4 2022-06-16 stsp goto done;
3513 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3515 61417565 2022-06-20 mark w += width;
3518 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3519 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3520 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3521 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3522 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3523 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3524 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3528 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3529 1853e0f4 2022-06-16 stsp free(wline);
3530 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3531 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3532 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3533 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3535 1853e0f4 2022-06-16 stsp goto done;
3536 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3538 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3539 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3541 1853e0f4 2022-06-16 stsp goto done;
3542 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3544 1853e0f4 2022-06-16 stsp *wtotal += width2;
3547 145b6838 2022-06-16 stsp free(wline);
3548 44a87665 2022-06-16 stsp free(exstr);
3549 1853e0f4 2022-06-16 stsp free(seg0);
3550 1853e0f4 2022-06-16 stsp free(seg1);
3551 1853e0f4 2022-06-16 stsp free(seg2);
3552 1853e0f4 2022-06-16 stsp return err;
3555 41605754 2020-11-12 stsp static const struct got_error *
3556 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3558 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3559 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3560 61e69b96 2018-05-20 stsp const struct got_error *err;
3561 fe621944 2020-11-10 stsp int nprinted = 0;
3562 b304db33 2018-05-20 stsp char *line;
3563 826082fe 2020-12-10 stsp size_t linesize = 0;
3564 826082fe 2020-12-10 stsp ssize_t linelen;
3565 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3566 61e69b96 2018-05-20 stsp wchar_t *wline;
3567 e0b650dd 2018-05-20 stsp int width;
3568 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3569 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3570 fe621944 2020-11-10 stsp off_t line_offset;
3572 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3573 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3574 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3576 f7d12f7e 2018-08-01 stsp werase(view->window);
3578 a3404814 2018-09-02 stsp if (header) {
3579 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3580 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3581 135a2da0 2020-11-11 stsp header) == -1)
3582 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3583 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3585 135a2da0 2020-11-11 stsp free(line);
3587 a3404814 2018-09-02 stsp return err;
3589 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3590 a3404814 2018-09-02 stsp wstandout(view->window);
3591 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3592 e54cc94a 2020-11-11 stsp free(wline);
3593 e54cc94a 2020-11-11 stsp wline = NULL;
3594 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3595 a3404814 2018-09-02 stsp wstandend(view->window);
3596 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3597 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3599 a3404814 2018-09-02 stsp if (max_lines <= 1)
3600 a3404814 2018-09-02 stsp return NULL;
3601 a3404814 2018-09-02 stsp max_lines--;
3604 89f1a395 2020-12-01 naddy s->eof = 0;
3605 145b6838 2022-06-16 stsp view->maxx = 0;
3606 826082fe 2020-12-10 stsp line = NULL;
3607 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3608 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3609 826082fe 2020-12-10 stsp if (linelen == -1) {
3610 826082fe 2020-12-10 stsp if (feof(s->f)) {
3611 826082fe 2020-12-10 stsp s->eof = 1;
3614 826082fe 2020-12-10 stsp free(line);
3615 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3618 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3619 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3620 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3621 1853e0f4 2022-06-16 stsp if (err) {
3622 1853e0f4 2022-06-16 stsp free(line);
3623 1853e0f4 2022-06-16 stsp return err;
3625 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3626 1853e0f4 2022-06-16 stsp free(wline);
3627 1853e0f4 2022-06-16 stsp wline = NULL;
3629 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3631 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3632 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3633 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3634 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3635 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3636 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3637 41605754 2020-11-12 stsp if (err) {
3638 41605754 2020-11-12 stsp free(line);
3639 41605754 2020-11-12 stsp return err;
3643 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3644 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3645 969c159c 2022-06-16 stsp if (err) {
3646 969c159c 2022-06-16 stsp free(line);
3647 969c159c 2022-06-16 stsp return err;
3649 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3650 969c159c 2022-06-16 stsp free(wline);
3651 41605754 2020-11-12 stsp wline = NULL;
3654 6d17833f 2019-11-08 stsp wattr_off(view->window,
3655 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3656 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3657 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3658 fe621944 2020-11-10 stsp nprinted++;
3660 826082fe 2020-12-10 stsp free(line);
3661 fe621944 2020-11-10 stsp if (nprinted >= 1)
3662 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3663 89f1a395 2020-12-01 naddy (nprinted - 1);
3665 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3667 9b058f45 2022-06-30 mark view_border(view);
3669 89f1a395 2020-12-01 naddy if (s->eof) {
3670 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3671 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3672 c3e9aa98 2019-05-13 jcs nprinted++;
3675 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3676 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3678 c3e9aa98 2019-05-13 jcs return err;
3681 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3682 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3683 e54cc94a 2020-11-11 stsp free(wline);
3684 e54cc94a 2020-11-11 stsp wline = NULL;
3685 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3688 26ed57b2 2018-05-19 stsp return NULL;
3691 abd2672a 2018-12-23 stsp static char *
3692 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3694 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3695 09867e48 2019-08-13 stsp char *p, *s;
3697 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3698 09867e48 2019-08-13 stsp if (tm == NULL)
3699 09867e48 2019-08-13 stsp return NULL;
3700 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3701 09867e48 2019-08-13 stsp if (s == NULL)
3702 09867e48 2019-08-13 stsp return NULL;
3703 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3705 abd2672a 2018-12-23 stsp *p = '\0';
3709 4ed7e80c 2018-05-20 stsp static const struct got_error *
3710 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3711 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3713 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3714 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3715 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3716 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3718 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3719 0208f208 2020-05-05 stsp if (qid != NULL) {
3720 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3721 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3722 d7b5a0e8 2022-04-20 stsp &qid->id);
3724 0208f208 2020-05-05 stsp return err;
3726 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3727 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3728 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3729 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3730 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3732 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3736 0208f208 2020-05-05 stsp if (tree_id1) {
3737 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3739 0208f208 2020-05-05 stsp goto done;
3742 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3743 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3745 0208f208 2020-05-05 stsp goto done;
3747 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3748 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3750 0208f208 2020-05-05 stsp if (tree1)
3751 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3752 0208f208 2020-05-05 stsp if (tree2)
3753 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3754 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3755 0208f208 2020-05-05 stsp return err;
3758 0208f208 2020-05-05 stsp static const struct got_error *
3759 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3763 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3764 fe621944 2020-11-10 stsp if (p == NULL)
3765 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3766 fe621944 2020-11-10 stsp *line_offsets = p;
3767 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3768 fe621944 2020-11-10 stsp (*nlines)++;
3769 fe621944 2020-11-10 stsp return NULL;
3772 fe621944 2020-11-10 stsp static const struct got_error *
3773 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3774 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3775 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3777 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3778 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3779 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3780 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3781 45d799e2 2018-12-23 stsp time_t committer_time;
3782 45d799e2 2018-12-23 stsp const char *author, *committer;
3783 8b473291 2019-02-21 stsp char *refs_str = NULL;
3784 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3785 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3786 fe621944 2020-11-10 stsp off_t outoff = 0;
3789 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3791 8b473291 2019-02-21 stsp if (refs) {
3792 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3794 8b473291 2019-02-21 stsp return err;
3797 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3799 abd2672a 2018-12-23 stsp return err;
3801 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3802 15a94983 2018-12-23 stsp if (err) {
3803 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3804 15a94983 2018-12-23 stsp goto done;
3807 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3809 fe621944 2020-11-10 stsp goto done;
3811 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3812 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3813 fe621944 2020-11-10 stsp if (n < 0) {
3814 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3815 abd2672a 2018-12-23 stsp goto done;
3817 fe621944 2020-11-10 stsp outoff += n;
3818 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3820 fe621944 2020-11-10 stsp goto done;
3822 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3823 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3824 fe621944 2020-11-10 stsp if (n < 0) {
3825 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3826 abd2672a 2018-12-23 stsp goto done;
3828 fe621944 2020-11-10 stsp outoff += n;
3829 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3831 fe621944 2020-11-10 stsp goto done;
3833 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3834 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3835 fe621944 2020-11-10 stsp if (datestr) {
3836 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3837 fe621944 2020-11-10 stsp if (n < 0) {
3838 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3839 fe621944 2020-11-10 stsp goto done;
3841 fe621944 2020-11-10 stsp outoff += n;
3842 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3844 fe621944 2020-11-10 stsp goto done;
3846 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3847 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3848 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3849 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3850 fe621944 2020-11-10 stsp if (n < 0) {
3851 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3852 fe621944 2020-11-10 stsp goto done;
3854 fe621944 2020-11-10 stsp outoff += n;
3855 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3857 fe621944 2020-11-10 stsp goto done;
3859 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3860 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3861 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3862 9f98ca05 2021-09-24 stsp int pn = 1;
3863 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3864 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3865 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3867 9f98ca05 2021-09-24 stsp goto done;
3868 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3869 9f98ca05 2021-09-24 stsp if (n < 0) {
3870 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3871 9f98ca05 2021-09-24 stsp goto done;
3873 9f98ca05 2021-09-24 stsp outoff += n;
3874 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3876 9f98ca05 2021-09-24 stsp goto done;
3877 9f98ca05 2021-09-24 stsp free(id_str);
3878 9f98ca05 2021-09-24 stsp id_str = NULL;
3882 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3884 5943eee2 2019-08-13 stsp goto done;
3885 fe621944 2020-11-10 stsp s = logmsg;
3886 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3887 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3888 fe621944 2020-11-10 stsp if (n < 0) {
3889 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3890 fe621944 2020-11-10 stsp goto done;
3892 fe621944 2020-11-10 stsp outoff += n;
3893 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3895 fe621944 2020-11-10 stsp goto done;
3898 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3900 0208f208 2020-05-05 stsp goto done;
3901 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3902 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3903 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3904 fe621944 2020-11-10 stsp if (n < 0) {
3905 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3906 fe621944 2020-11-10 stsp goto done;
3908 fe621944 2020-11-10 stsp outoff += n;
3909 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3911 fe621944 2020-11-10 stsp goto done;
3912 0208f208 2020-05-05 stsp free((char *)pe->path);
3913 0208f208 2020-05-05 stsp free(pe->data);
3916 0208f208 2020-05-05 stsp fputc('\n', outfile);
3918 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3920 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3921 abd2672a 2018-12-23 stsp free(id_str);
3922 5943eee2 2019-08-13 stsp free(logmsg);
3923 8b473291 2019-02-21 stsp free(refs_str);
3924 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3925 7510f233 2020-08-09 stsp if (err) {
3926 ae6a6978 2020-08-09 stsp free(*line_offsets);
3927 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3928 ae6a6978 2020-08-09 stsp *nlines = 0;
3930 fe621944 2020-11-10 stsp return err;
3933 abd2672a 2018-12-23 stsp static const struct got_error *
3934 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3936 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3937 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3938 15a94983 2018-12-23 stsp int obj_type;
3940 fe621944 2020-11-10 stsp free(s->line_offsets);
3941 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3942 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3943 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3944 fe621944 2020-11-10 stsp s->nlines = 0;
3946 511a516b 2018-05-19 stsp f = got_opentemp();
3947 48ae06ee 2018-10-18 stsp if (f == NULL) {
3948 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3949 48ae06ee 2018-10-18 stsp goto done;
3951 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3952 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3953 fb43ecf1 2019-02-11 stsp goto done;
3957 15a94983 2018-12-23 stsp if (s->id1)
3958 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3960 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3962 15a94983 2018-12-23 stsp goto done;
3964 15a94983 2018-12-23 stsp switch (obj_type) {
3965 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3966 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3967 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3968 f9d37699 2022-06-28 stsp s->label1, s->label2, s->diff_context,
3969 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3971 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3972 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3973 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3974 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3975 f9d37699 2022-06-28 stsp s->repo, s->f);
3977 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3978 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3979 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3980 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3981 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3983 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3985 3ffacbe1 2020-02-02 tracey goto done;
3986 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3987 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3988 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3989 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3990 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3991 f44b1f58 2020-02-02 tracey if (err)
3992 f44b1f58 2020-02-02 tracey goto done;
3993 f44b1f58 2020-02-02 tracey } else {
3994 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3995 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3996 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3997 fe621944 2020-11-10 stsp err = write_commit_info(
3998 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3999 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4000 f44b1f58 2020-02-02 tracey if (err)
4001 f44b1f58 2020-02-02 tracey goto done;
4006 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4008 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4009 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4010 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
4011 f9d37699 2022-06-28 stsp s->repo, s->f);
4015 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4018 f44b1f58 2020-02-02 tracey if (err)
4019 f44b1f58 2020-02-02 tracey goto done;
4021 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4022 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4023 48ae06ee 2018-10-18 stsp return err;
4026 f5215bb9 2019-02-22 stsp static void
4027 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4029 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4030 f5215bb9 2019-02-22 stsp update_panels();
4031 f5215bb9 2019-02-22 stsp doupdate();
4034 f44b1f58 2020-02-02 tracey static const struct got_error *
4035 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4037 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4039 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4040 f44b1f58 2020-02-02 tracey return NULL;
4043 f44b1f58 2020-02-02 tracey static const struct got_error *
4044 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4046 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4047 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4048 f44b1f58 2020-02-02 tracey int lineno;
4049 cb713507 2022-06-16 stsp char *line = NULL;
4050 826082fe 2020-12-10 stsp size_t linesize = 0;
4051 826082fe 2020-12-10 stsp ssize_t linelen;
4053 f44b1f58 2020-02-02 tracey if (!view->searching) {
4054 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4055 f44b1f58 2020-02-02 tracey return NULL;
4058 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4059 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4060 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4062 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4064 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4066 f44b1f58 2020-02-02 tracey while (1) {
4067 f44b1f58 2020-02-02 tracey off_t offset;
4069 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4070 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4071 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4075 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4076 f44b1f58 2020-02-02 tracey lineno = 1;
4078 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4081 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4082 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4083 f44b1f58 2020-02-02 tracey free(line);
4084 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4086 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4087 cb713507 2022-06-16 stsp if (linelen != -1) {
4088 cb713507 2022-06-16 stsp char *exstr;
4089 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4092 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4093 cb713507 2022-06-16 stsp &view->regmatch)) {
4094 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4095 cb713507 2022-06-16 stsp s->matched_line = lineno;
4096 cb713507 2022-06-16 stsp free(exstr);
4099 cb713507 2022-06-16 stsp free(exstr);
4101 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4102 f44b1f58 2020-02-02 tracey lineno++;
4104 f44b1f58 2020-02-02 tracey lineno--;
4106 826082fe 2020-12-10 stsp free(line);
4108 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4109 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4110 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4113 145b6838 2022-06-16 stsp return err;
4116 48ae06ee 2018-10-18 stsp static const struct got_error *
4117 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4119 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4120 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4122 b72706c3 2022-06-01 stsp free(s->id1);
4123 b72706c3 2022-06-01 stsp s->id1 = NULL;
4124 b72706c3 2022-06-01 stsp free(s->id2);
4125 b72706c3 2022-06-01 stsp s->id2 = NULL;
4126 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4127 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4128 b72706c3 2022-06-01 stsp s->f = NULL;
4129 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4130 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4131 b72706c3 2022-06-01 stsp s->f1 = NULL;
4132 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4133 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4134 b72706c3 2022-06-01 stsp s->f2 = NULL;
4135 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4136 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4137 f9d37699 2022-06-28 stsp s->fd1 = -1;
4138 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4139 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4140 f9d37699 2022-06-28 stsp s->fd2 = -1;
4141 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4142 b72706c3 2022-06-01 stsp free(s->line_offsets);
4143 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4144 b72706c3 2022-06-01 stsp s->nlines = 0;
4145 b72706c3 2022-06-01 stsp return err;
4148 b72706c3 2022-06-01 stsp static const struct got_error *
4149 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4150 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4151 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4152 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4154 48ae06ee 2018-10-18 stsp const struct got_error *err;
4155 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4157 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4158 f9d37699 2022-06-28 stsp s->fd1 = -1;
4159 f9d37699 2022-06-28 stsp s->fd2 = -1;
4161 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4162 15a94983 2018-12-23 stsp int type1, type2;
4163 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4165 15a94983 2018-12-23 stsp return err;
4166 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4168 15a94983 2018-12-23 stsp return err;
4170 15a94983 2018-12-23 stsp if (type1 != type2)
4171 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4173 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4174 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4175 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4176 f44b1f58 2020-02-02 tracey s->repo = repo;
4177 f44b1f58 2020-02-02 tracey s->id1 = id1;
4178 f44b1f58 2020-02-02 tracey s->id2 = id2;
4179 3dbaef42 2020-11-24 stsp s->label1 = label1;
4180 3dbaef42 2020-11-24 stsp s->label2 = label2;
4182 15a94983 2018-12-23 stsp if (id1) {
4183 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4184 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4185 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4187 5465d566 2020-02-01 tracey s->id1 = NULL;
4189 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4190 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4191 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4192 b72706c3 2022-06-01 stsp goto done;
4195 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4196 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4197 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4198 a00719e9 2022-06-17 stsp goto done;
4201 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4202 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4203 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4204 b72706c3 2022-06-01 stsp goto done;
4207 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4208 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4209 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4210 f9d37699 2022-06-28 stsp goto done;
4213 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4214 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4215 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4216 f9d37699 2022-06-28 stsp goto done;
4219 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4220 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4221 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4222 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4223 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4224 5465d566 2020-02-01 tracey s->log_view = log_view;
4225 5465d566 2020-02-01 tracey s->repo = repo;
4227 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4228 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4229 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4230 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4231 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4233 b72706c3 2022-06-01 stsp goto done;
4234 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4235 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4236 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4238 b72706c3 2022-06-01 stsp goto done;
4239 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4240 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4241 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4243 b72706c3 2022-06-01 stsp goto done;
4245 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4246 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4247 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4248 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4249 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
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 "^(from|via): ", TOG_COLOR_AUTHOR,
4255 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4257 b72706c3 2022-06-01 stsp goto done;
4259 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4260 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4261 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4263 b72706c3 2022-06-01 stsp goto done;
4266 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4267 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4268 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4270 5465d566 2020-02-01 tracey err = create_diff(s);
4272 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4273 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4274 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4275 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4276 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4279 b72706c3 2022-06-01 stsp close_diff_view(view);
4280 e5a0f69f 2018-08-18 stsp return err;
4283 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4284 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4286 a3404814 2018-09-02 stsp const struct got_error *err;
4287 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4288 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4289 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4291 a3404814 2018-09-02 stsp if (s->id1) {
4292 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4294 a3404814 2018-09-02 stsp return err;
4295 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4297 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4299 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4301 a3404814 2018-09-02 stsp return err;
4302 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4304 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4306 a3404814 2018-09-02 stsp free(id_str1);
4307 a3404814 2018-09-02 stsp free(id_str2);
4308 a3404814 2018-09-02 stsp return err;
4310 a3404814 2018-09-02 stsp free(id_str1);
4311 a3404814 2018-09-02 stsp free(id_str2);
4313 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4314 267bb3b8 2021-08-01 stsp free(header);
4315 267bb3b8 2021-08-01 stsp return err;
4318 15a087fe 2019-02-21 stsp static const struct got_error *
4319 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4320 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4322 d7a04538 2019-02-21 stsp const struct got_error *err;
4323 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4324 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4325 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4327 15a087fe 2019-02-21 stsp free(s->id2);
4328 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4329 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4330 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4332 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4334 d7a04538 2019-02-21 stsp return err;
4335 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4336 15a087fe 2019-02-21 stsp free(s->id1);
4337 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4338 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4339 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4340 15a087fe 2019-02-21 stsp return NULL;
4343 0cf4efb1 2018-09-29 stsp static const struct got_error *
4344 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4346 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4347 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4348 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4349 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4350 826082fe 2020-12-10 stsp char *line = NULL;
4351 826082fe 2020-12-10 stsp size_t linesize = 0;
4352 826082fe 2020-12-10 stsp ssize_t linelen;
4353 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4355 e5a0f69f 2018-08-18 stsp switch (ch) {
4357 145b6838 2022-06-16 stsp view->x = 0;
4360 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4361 640cd7ff 2022-06-22 mark view->count = 0;
4363 145b6838 2022-06-16 stsp case KEY_RIGHT:
4365 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4366 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4368 640cd7ff 2022-06-22 mark view->count = 0;
4370 145b6838 2022-06-16 stsp case KEY_LEFT:
4372 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4373 640cd7ff 2022-06-22 mark if (view->x <= 0)
4374 640cd7ff 2022-06-22 mark view->count = 0;
4378 3dbaef42 2020-11-24 stsp if (ch == 'a')
4379 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4380 3dbaef42 2020-11-24 stsp if (ch == 'w')
4381 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4382 64453f7e 2020-11-21 stsp wclear(view->window);
4383 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
4384 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4385 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4386 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
4387 64453f7e 2020-11-21 stsp err = create_diff(s);
4388 640cd7ff 2022-06-22 mark view->count = 0;
4391 912a3f79 2021-08-30 j case KEY_HOME:
4392 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4393 640cd7ff 2022-06-22 mark view->count = 0;
4396 912a3f79 2021-08-30 j case KEY_END:
4397 640cd7ff 2022-06-22 mark view->count = 0;
4401 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4405 1e37a5c2 2019-05-12 jcs case KEY_UP:
4406 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4407 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4408 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4410 640cd7ff 2022-06-22 mark view->count = 0;
4412 83cc4199 2022-06-13 stsp case CTRL('u'):
4414 83cc4199 2022-06-13 stsp nscroll /= 2;
4415 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4416 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4417 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4419 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4420 640cd7ff 2022-06-22 mark view->count = 0;
4424 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4425 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4428 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4429 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4430 1e37a5c2 2019-05-12 jcs if (!s->eof)
4431 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4433 640cd7ff 2022-06-22 mark view->count = 0;
4435 83cc4199 2022-06-13 stsp case CTRL('d'):
4437 83cc4199 2022-06-13 stsp nscroll /= 2;
4438 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4439 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4440 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4443 640cd7ff 2022-06-22 mark if (s->eof) {
4444 640cd7ff 2022-06-22 mark view->count = 0;
4448 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4449 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4450 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4451 826082fe 2020-12-10 stsp if (linelen == -1) {
4452 826082fe 2020-12-10 stsp if (feof(s->f)) {
4453 826082fe 2020-12-10 stsp s->eof = 1;
4455 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4459 826082fe 2020-12-10 stsp free(line);
4462 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4463 1e37a5c2 2019-05-12 jcs s->diff_context--;
4464 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4465 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4466 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4467 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4468 27829c9e 2020-11-21 stsp s->nlines) {
4469 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4470 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4473 640cd7ff 2022-06-22 mark view->count = 0;
4476 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4477 1e37a5c2 2019-05-12 jcs s->diff_context++;
4478 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4479 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4480 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4482 640cd7ff 2022-06-22 mark view->count = 0;
4486 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4487 640cd7ff 2022-06-22 mark view->count = 0;
4490 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4491 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4493 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4494 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4498 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4501 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4505 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4506 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4507 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4508 145b6838 2022-06-16 stsp view->x = 0;
4510 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4511 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4515 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4516 640cd7ff 2022-06-22 mark view->count = 0;
4519 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4520 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4522 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4523 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4527 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4530 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4534 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4535 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4536 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4537 145b6838 2022-06-16 stsp view->x = 0;
4539 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4540 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4543 640cd7ff 2022-06-22 mark view->count = 0;
4547 bcbd79e2 2018-08-19 stsp return err;
4550 4ed7e80c 2018-05-20 stsp static const struct got_error *
4551 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4553 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4554 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4555 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4556 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4557 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4558 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4559 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4560 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4561 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4562 3dbaef42 2020-11-24 stsp const char *errstr;
4563 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4564 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4566 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4567 26ed57b2 2018-05-19 stsp switch (ch) {
4569 64453f7e 2020-11-21 stsp force_text_diff = 1;
4572 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4574 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4575 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4576 5a20d08d 2022-02-09 op errstr, errstr);
4578 09b5bff8 2020-02-23 naddy case 'r':
4579 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4580 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4581 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4583 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4586 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4589 17020d27 2019-03-07 stsp usage_diff();
4590 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4594 26ed57b2 2018-05-19 stsp argc -= optind;
4595 26ed57b2 2018-05-19 stsp argv += optind;
4597 26ed57b2 2018-05-19 stsp if (argc == 0) {
4598 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4599 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4600 15a94983 2018-12-23 stsp id_str1 = argv[0];
4601 15a94983 2018-12-23 stsp id_str2 = argv[1];
4603 26ed57b2 2018-05-19 stsp usage_diff();
4605 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4606 0ae84acc 2022-06-15 tracey if (error)
4607 0ae84acc 2022-06-15 tracey goto done;
4609 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4610 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4611 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4612 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4613 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4614 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4615 c156c7a4 2020-12-18 stsp goto done;
4616 a273ac94 2020-02-23 naddy if (worktree)
4617 a273ac94 2020-02-23 naddy repo_path =
4618 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4620 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4621 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4622 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4623 c156c7a4 2020-12-18 stsp goto done;
4627 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4628 eb6600df 2019-01-04 stsp if (error)
4629 eb6600df 2019-01-04 stsp goto done;
4631 a273ac94 2020-02-23 naddy init_curses();
4633 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4634 51a10b52 2020-12-26 stsp if (error)
4635 51a10b52 2020-12-26 stsp goto done;
4637 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4638 26ed57b2 2018-05-19 stsp if (error)
4639 26ed57b2 2018-05-19 stsp goto done;
4641 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4642 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4643 26ed57b2 2018-05-19 stsp if (error)
4644 26ed57b2 2018-05-19 stsp goto done;
4646 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4647 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4648 26ed57b2 2018-05-19 stsp if (error)
4649 26ed57b2 2018-05-19 stsp goto done;
4651 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4652 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4653 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4654 ea5e7bb5 2018-08-01 stsp goto done;
4656 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4657 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4658 5dc9f4bc 2018-08-04 stsp if (error)
4659 5dc9f4bc 2018-08-04 stsp goto done;
4660 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4662 3dbaef42 2020-11-24 stsp free(label1);
4663 3dbaef42 2020-11-24 stsp free(label2);
4664 c02c541e 2019-03-29 stsp free(repo_path);
4665 a273ac94 2020-02-23 naddy free(cwd);
4666 1d0f4054 2021-06-17 stsp if (repo) {
4667 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4668 1d0f4054 2021-06-17 stsp if (error == NULL)
4669 1d0f4054 2021-06-17 stsp error = close_err;
4671 a273ac94 2020-02-23 naddy if (worktree)
4672 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4673 0ae84acc 2022-06-15 tracey if (pack_fds) {
4674 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4675 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4676 0ae84acc 2022-06-15 tracey if (error == NULL)
4677 0ae84acc 2022-06-15 tracey error = pack_err;
4679 51a10b52 2020-12-26 stsp tog_free_refs();
4680 26ed57b2 2018-05-19 stsp return error;
4683 4ed7e80c 2018-05-20 stsp __dead static void
4684 9f7d7167 2018-04-29 stsp usage_blame(void)
4687 87411fa9 2022-06-16 stsp fprintf(stderr,
4688 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4689 9f7d7167 2018-04-29 stsp getprogname());
4693 84451b3e 2018-07-10 stsp struct tog_blame_line {
4694 84451b3e 2018-07-10 stsp int annotated;
4695 84451b3e 2018-07-10 stsp struct got_object_id *id;
4698 4ed7e80c 2018-05-20 stsp static const struct got_error *
4699 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4701 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4702 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4703 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4704 84451b3e 2018-07-10 stsp const struct got_error *err;
4705 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4706 826082fe 2020-12-10 stsp char *line = NULL;
4707 826082fe 2020-12-10 stsp size_t linesize = 0;
4708 826082fe 2020-12-10 stsp ssize_t linelen;
4709 84451b3e 2018-07-10 stsp wchar_t *wline;
4710 27a741e5 2019-09-11 stsp int width;
4711 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4712 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4713 ab089a2a 2018-07-12 stsp char *id_str;
4714 11b20872 2019-11-08 stsp struct tog_color *tc;
4716 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4718 ab089a2a 2018-07-12 stsp return err;
4720 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4721 f7d12f7e 2018-08-01 stsp werase(view->window);
4723 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4724 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4725 ab089a2a 2018-07-12 stsp free(id_str);
4726 ab089a2a 2018-07-12 stsp return err;
4729 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4730 ab089a2a 2018-07-12 stsp free(line);
4731 2550e4c3 2018-07-13 stsp line = NULL;
4733 1cae65b4 2019-09-22 stsp return err;
4734 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4735 a3404814 2018-09-02 stsp wstandout(view->window);
4736 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4738 11b20872 2019-11-08 stsp wattr_on(view->window,
4739 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4740 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4742 11b20872 2019-11-08 stsp wattr_off(view->window,
4743 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4744 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4745 a3404814 2018-09-02 stsp wstandend(view->window);
4746 2550e4c3 2018-07-13 stsp free(wline);
4747 2550e4c3 2018-07-13 stsp wline = NULL;
4748 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4749 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4751 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4752 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4753 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4754 ab089a2a 2018-07-12 stsp free(id_str);
4755 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4757 ab089a2a 2018-07-12 stsp free(id_str);
4758 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4759 3f60a8ef 2018-07-10 stsp free(line);
4760 2550e4c3 2018-07-13 stsp line = NULL;
4762 3f60a8ef 2018-07-10 stsp return err;
4763 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4764 2550e4c3 2018-07-13 stsp free(wline);
4765 2550e4c3 2018-07-13 stsp wline = NULL;
4766 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4767 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4769 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4770 145b6838 2022-06-16 stsp view->maxx = 0;
4771 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4772 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4773 826082fe 2020-12-10 stsp if (linelen == -1) {
4774 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4775 826082fe 2020-12-10 stsp s->eof = 1;
4778 84451b3e 2018-07-10 stsp free(line);
4779 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4781 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4784 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4785 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4786 1853e0f4 2022-06-16 stsp if (err) {
4787 1853e0f4 2022-06-16 stsp free(line);
4788 1853e0f4 2022-06-16 stsp return err;
4790 1853e0f4 2022-06-16 stsp free(wline);
4791 1853e0f4 2022-06-16 stsp wline = NULL;
4792 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4794 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4795 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4797 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4798 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4799 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4800 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4801 27a741e5 2019-09-11 stsp !(view->focussed &&
4802 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4803 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4804 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4805 8d0fe45a 2019-08-12 stsp char *id_str;
4806 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4807 87411fa9 2022-06-16 stsp blame_line->id);
4808 8d0fe45a 2019-08-12 stsp if (err) {
4809 8d0fe45a 2019-08-12 stsp free(line);
4810 8d0fe45a 2019-08-12 stsp return err;
4812 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4814 11b20872 2019-11-08 stsp wattr_on(view->window,
4815 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4816 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4818 11b20872 2019-11-08 stsp wattr_off(view->window,
4819 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4820 8d0fe45a 2019-08-12 stsp free(id_str);
4821 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4823 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4824 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4827 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4828 ee41ec32 2018-07-10 stsp prev_id = NULL;
4831 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4832 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4833 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4835 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4836 41605754 2020-11-12 stsp width = 9;
4837 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4838 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4839 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4840 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4841 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4842 41605754 2020-11-12 stsp if (err) {
4843 41605754 2020-11-12 stsp free(line);
4844 41605754 2020-11-12 stsp return err;
4846 41605754 2020-11-12 stsp width += 9;
4849 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4850 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4851 4cb9d869 2022-06-16 stsp if (err) {
4852 4cb9d869 2022-06-16 stsp free(line);
4853 4cb9d869 2022-06-16 stsp return err;
4855 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4856 a75b3e2d 2022-06-16 stsp width += 9;
4857 41605754 2020-11-12 stsp free(wline);
4858 41605754 2020-11-12 stsp wline = NULL;
4861 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4862 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4863 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4864 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4866 826082fe 2020-12-10 stsp free(line);
4867 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4869 9b058f45 2022-06-30 mark view_border(view);
4871 84451b3e 2018-07-10 stsp return NULL;
4874 84451b3e 2018-07-10 stsp static const struct got_error *
4875 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4876 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4878 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4879 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4880 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4881 1a76625f 2018-10-22 stsp int errcode;
4883 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4884 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4885 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4887 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4888 1a76625f 2018-10-22 stsp if (errcode)
4889 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4891 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4892 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4893 d68a0a7d 2018-07-10 stsp goto done;
4896 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4897 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4899 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4900 d68a0a7d 2018-07-10 stsp if (line->annotated)
4901 d68a0a7d 2018-07-10 stsp goto done;
4903 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4904 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4905 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4906 84451b3e 2018-07-10 stsp goto done;
4908 84451b3e 2018-07-10 stsp line->annotated = 1;
4910 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4911 1a76625f 2018-10-22 stsp if (errcode)
4912 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4913 84451b3e 2018-07-10 stsp return err;
4916 84451b3e 2018-07-10 stsp static void *
4917 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4919 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4920 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4921 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4922 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
4923 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
4925 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
4926 e6e73e55 2022-06-30 tracey if (fd1 == -1)
4927 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
4929 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
4930 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
4931 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
4932 e6e73e55 2022-06-30 tracey goto done;
4935 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
4936 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
4937 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4938 e6e73e55 2022-06-30 tracey goto done;
4940 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
4941 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
4942 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4943 e6e73e55 2022-06-30 tracey goto done;
4946 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4948 e6e73e55 2022-06-30 tracey goto done;
4950 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4951 e6e73e55 2022-06-30 tracey blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1,
4953 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4954 fc06ba56 2019-08-22 stsp err = NULL;
4956 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4957 e6e73e55 2022-06-30 tracey if (errcode) {
4958 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
4959 e6e73e55 2022-06-30 tracey goto done;
4962 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4963 1d0f4054 2021-06-17 stsp if (err == NULL)
4964 1d0f4054 2021-06-17 stsp err = close_err;
4965 c9beca56 2018-07-22 stsp ta->repo = NULL;
4966 c9beca56 2018-07-22 stsp *ta->complete = 1;
4968 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4969 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4970 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4973 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4974 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
4975 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4976 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
4977 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
4978 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
4979 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
4980 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
4982 18430de3 2018-07-10 stsp return (void *)err;
4985 245d91c1 2018-07-12 stsp static struct got_object_id *
4986 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4987 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4989 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4991 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4992 8d0fe45a 2019-08-12 stsp return NULL;
4994 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4995 245d91c1 2018-07-12 stsp if (!line->annotated)
4996 245d91c1 2018-07-12 stsp return NULL;
4998 245d91c1 2018-07-12 stsp return line->id;
5001 b880a918 2018-07-10 stsp static const struct got_error *
5002 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5004 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5007 245d91c1 2018-07-12 stsp if (blame->thread) {
5008 1a76625f 2018-10-22 stsp int errcode;
5009 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5010 1a76625f 2018-10-22 stsp if (errcode)
5011 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5012 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5013 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5014 1a76625f 2018-10-22 stsp if (errcode)
5015 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5016 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5017 1a76625f 2018-10-22 stsp if (errcode)
5018 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5019 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5020 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5021 245d91c1 2018-07-12 stsp err = NULL;
5022 245d91c1 2018-07-12 stsp blame->thread = NULL;
5024 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5025 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5026 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5027 1d0f4054 2021-06-17 stsp if (err == NULL)
5028 1d0f4054 2021-06-17 stsp err = close_err;
5029 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5031 245d91c1 2018-07-12 stsp if (blame->f) {
5032 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5033 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5034 245d91c1 2018-07-12 stsp blame->f = NULL;
5036 57670559 2018-12-23 stsp if (blame->lines) {
5037 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5038 57670559 2018-12-23 stsp free(blame->lines[i].id);
5039 57670559 2018-12-23 stsp free(blame->lines);
5040 57670559 2018-12-23 stsp blame->lines = NULL;
5042 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5043 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5044 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5045 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5046 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5047 0ae84acc 2022-06-15 tracey if (err == NULL)
5048 0ae84acc 2022-06-15 tracey err = pack_err;
5049 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5051 245d91c1 2018-07-12 stsp return err;
5054 245d91c1 2018-07-12 stsp static const struct got_error *
5055 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5057 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5058 fc06ba56 2019-08-22 stsp int *done = arg;
5059 fc06ba56 2019-08-22 stsp int errcode;
5061 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5062 fc06ba56 2019-08-22 stsp if (errcode)
5063 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5064 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5066 fc06ba56 2019-08-22 stsp if (*done)
5067 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5069 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5070 fc06ba56 2019-08-22 stsp if (errcode)
5071 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5072 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5074 fc06ba56 2019-08-22 stsp return err;
5077 fc06ba56 2019-08-22 stsp static const struct got_error *
5078 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5080 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5081 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5082 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5083 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5084 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5085 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5086 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5087 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5088 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5090 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5091 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5093 15a94983 2018-12-23 stsp return err;
5095 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5096 eb81bc23 2022-06-28 tracey if (fd == -1) {
5097 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5098 eb81bc23 2022-06-28 tracey goto done;
5101 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5103 a44927cc 2022-04-07 stsp goto done;
5105 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5107 84451b3e 2018-07-10 stsp goto done;
5109 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5110 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5111 84451b3e 2018-07-10 stsp goto done;
5114 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5116 a70480e0 2018-06-23 stsp goto done;
5117 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5118 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5119 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5120 84451b3e 2018-07-10 stsp goto done;
5122 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5123 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5125 1fddf795 2021-01-20 stsp goto done;
5126 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5127 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5128 84451b3e 2018-07-10 stsp goto done;
5131 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5132 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5133 b02560ec 2019-08-19 stsp blame->nlines--;
5135 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5136 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5137 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5138 84451b3e 2018-07-10 stsp goto done;
5141 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5143 bd24772e 2018-07-11 stsp goto done;
5144 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5145 0ae84acc 2022-06-15 tracey pack_fds);
5146 0ae84acc 2022-06-15 tracey if (err)
5147 0ae84acc 2022-06-15 tracey goto done;
5149 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5150 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5151 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5152 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5153 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5154 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5155 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5156 245d91c1 2018-07-12 stsp goto done;
5158 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5160 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5161 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5162 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5163 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5164 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5165 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5166 a5388363 2020-12-01 naddy s->blame_complete = 0;
5168 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5169 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5170 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5171 f5a09613 2020-12-13 naddy s->selected_line = 1;
5173 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5176 a44927cc 2022-04-07 stsp if (commit)
5177 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5178 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5179 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5181 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5182 27d434c2 2018-09-15 stsp free(obj_id);
5184 245d91c1 2018-07-12 stsp stop_blame(blame);
5185 245d91c1 2018-07-12 stsp return err;
5188 245d91c1 2018-07-12 stsp static const struct got_error *
5189 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5190 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5192 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5193 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5195 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5197 c4843652 2019-08-12 stsp s->path = strdup(path);
5198 c4843652 2019-08-12 stsp if (s->path == NULL)
5199 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5201 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5202 c4843652 2019-08-12 stsp if (err) {
5203 c4843652 2019-08-12 stsp free(s->path);
5204 7cbe629d 2018-08-04 stsp return err;
5207 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5208 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5209 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5210 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5211 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5212 fb2756b9 2018-08-04 stsp s->repo = repo;
5213 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5214 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5216 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5217 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5218 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5219 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5221 11b20872 2019-11-08 stsp return err;
5224 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5225 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5226 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5227 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5228 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5230 a5388363 2020-12-01 naddy return run_blame(view);
5233 e5a0f69f 2018-08-18 stsp static const struct got_error *
5234 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5236 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5237 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5239 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5240 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5242 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5243 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5244 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5245 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5246 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5249 e5a0f69f 2018-08-18 stsp free(s->path);
5250 11b20872 2019-11-08 stsp free_colors(&s->colors);
5251 e5a0f69f 2018-08-18 stsp return err;
5254 7cbe629d 2018-08-04 stsp static const struct got_error *
5255 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5257 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5259 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5260 6c4c42e0 2019-06-24 stsp return NULL;
5263 6c4c42e0 2019-06-24 stsp static const struct got_error *
5264 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5266 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5267 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5268 6c4c42e0 2019-06-24 stsp int lineno;
5269 cb713507 2022-06-16 stsp char *line = NULL;
5270 826082fe 2020-12-10 stsp size_t linesize = 0;
5271 826082fe 2020-12-10 stsp ssize_t linelen;
5273 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5274 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5275 6c4c42e0 2019-06-24 stsp return NULL;
5278 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5279 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5280 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5282 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5284 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5286 6c4c42e0 2019-06-24 stsp while (1) {
5287 6c4c42e0 2019-06-24 stsp off_t offset;
5289 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5290 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5291 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5295 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5296 6c4c42e0 2019-06-24 stsp lineno = 1;
5298 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5301 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5302 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5303 6c4c42e0 2019-06-24 stsp free(line);
5304 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5306 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5307 cb713507 2022-06-16 stsp if (linelen != -1) {
5308 cb713507 2022-06-16 stsp char *exstr;
5309 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5312 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5313 cb713507 2022-06-16 stsp &view->regmatch)) {
5314 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5315 cb713507 2022-06-16 stsp s->matched_line = lineno;
5316 cb713507 2022-06-16 stsp free(exstr);
5319 cb713507 2022-06-16 stsp free(exstr);
5321 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5326 826082fe 2020-12-10 stsp free(line);
5328 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5329 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5330 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5333 145b6838 2022-06-16 stsp return err;
5336 6c4c42e0 2019-06-24 stsp static const struct got_error *
5337 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5339 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5340 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5341 2b380cc8 2018-10-24 stsp int errcode;
5343 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5344 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5345 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5346 2b380cc8 2018-10-24 stsp if (errcode)
5347 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5349 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5352 51fe7530 2019-08-19 stsp if (s->blame_complete)
5353 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5355 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5357 9b058f45 2022-06-30 mark view_border(view);
5358 e5a0f69f 2018-08-18 stsp return err;
5361 e5a0f69f 2018-08-18 stsp static const struct got_error *
5362 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5364 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5365 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5366 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5367 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5369 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5370 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5371 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5372 9b058f45 2022-06-30 mark --eos; /* border */
5374 e5a0f69f 2018-08-18 stsp switch (ch) {
5376 145b6838 2022-06-16 stsp view->x = 0;
5379 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5380 640cd7ff 2022-06-22 mark view->count = 0;
5382 145b6838 2022-06-16 stsp case KEY_RIGHT:
5384 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5385 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5387 640cd7ff 2022-06-22 mark view->count = 0;
5389 145b6838 2022-06-16 stsp case KEY_LEFT:
5391 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5392 640cd7ff 2022-06-22 mark if (view->x <= 0)
5393 640cd7ff 2022-06-22 mark view->count = 0;
5396 1e37a5c2 2019-05-12 jcs s->done = 1;
5398 4deef56f 2021-09-02 naddy case 'g':
5399 4deef56f 2021-09-02 naddy case KEY_HOME:
5400 4deef56f 2021-09-02 naddy s->selected_line = 1;
5401 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5402 640cd7ff 2022-06-22 mark view->count = 0;
5404 4deef56f 2021-09-02 naddy case 'G':
5405 4deef56f 2021-09-02 naddy case KEY_END:
5406 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5407 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5408 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5410 9b058f45 2022-06-30 mark s->selected_line = eos;
5411 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5413 640cd7ff 2022-06-22 mark view->count = 0;
5416 1e37a5c2 2019-05-12 jcs case KEY_UP:
5417 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5418 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5419 1e37a5c2 2019-05-12 jcs s->selected_line--;
5420 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5421 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5422 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5424 640cd7ff 2022-06-22 mark view->count = 0;
5426 83cc4199 2022-06-13 stsp case CTRL('u'):
5428 83cc4199 2022-06-13 stsp nscroll /= 2;
5429 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5430 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5431 ea025d1d 2020-02-22 naddy case CTRL('b'):
5433 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5434 640cd7ff 2022-06-22 mark if (view->count > 1)
5435 640cd7ff 2022-06-22 mark nscroll += nscroll;
5436 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5437 640cd7ff 2022-06-22 mark view->count = 0;
5440 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5441 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5443 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5446 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5447 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5448 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5449 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5450 1e37a5c2 2019-05-12 jcs s->selected_line++;
5451 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5452 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5454 640cd7ff 2022-06-22 mark view->count = 0;
5457 1e37a5c2 2019-05-12 jcs case 'p': {
5458 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5460 640cd7ff 2022-06-22 mark view->count = 0;
5461 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5462 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5463 1e37a5c2 2019-05-12 jcs if (id == NULL)
5465 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5466 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5467 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5468 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5469 1e37a5c2 2019-05-12 jcs int obj_type;
5470 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5471 1e37a5c2 2019-05-12 jcs s->repo, id);
5474 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5475 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5476 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5477 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5480 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5481 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5482 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5485 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5486 a44927cc 2022-04-07 stsp pcommit, s->path);
5487 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5488 e5a0f69f 2018-08-18 stsp if (err) {
5489 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5490 1e37a5c2 2019-05-12 jcs err = NULL;
5491 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5494 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5496 1e37a5c2 2019-05-12 jcs free(blob_id);
5497 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5498 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5499 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5502 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5503 d7b5a0e8 2022-04-20 stsp &pid->id);
5504 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5506 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5507 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5509 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5514 1e37a5c2 2019-05-12 jcs s->done = 1;
5515 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5516 1e37a5c2 2019-05-12 jcs s->done = 0;
5517 1e37a5c2 2019-05-12 jcs if (thread_err)
5519 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5520 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5521 a5388363 2020-12-01 naddy err = run_blame(view);
5526 61417565 2022-06-20 mark case 'C': {
5527 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5529 640cd7ff 2022-06-22 mark view->count = 0;
5530 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5531 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5533 1e37a5c2 2019-05-12 jcs s->done = 1;
5534 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5535 1e37a5c2 2019-05-12 jcs s->done = 0;
5536 1e37a5c2 2019-05-12 jcs if (thread_err)
5538 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5539 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5540 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5541 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5542 a5388363 2020-12-01 naddy err = run_blame(view);
5547 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5548 1e37a5c2 2019-05-12 jcs case '\r': {
5549 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5550 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5551 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5553 640cd7ff 2022-06-22 mark view->count = 0;
5554 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5555 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5556 1e37a5c2 2019-05-12 jcs if (id == NULL)
5558 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5561 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5562 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5563 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
5565 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5566 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5567 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5568 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5571 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5572 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5573 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5575 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5578 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5579 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5584 e78dc838 2020-12-04 stsp view->focussed = 0;
5585 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5586 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5587 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5588 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5589 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5592 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5595 e78dc838 2020-12-04 stsp view->focus_child = 1;
5597 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5602 83cc4199 2022-06-13 stsp case CTRL('d'):
5604 83cc4199 2022-06-13 stsp nscroll /= 2;
5605 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5606 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5607 ea025d1d 2020-02-22 naddy case CTRL('f'):
5610 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5611 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5612 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5613 640cd7ff 2022-06-22 mark view->count = 0;
5616 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5617 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5618 83cc4199 2022-06-13 stsp s->selected_line +=
5619 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5620 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5622 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5623 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5625 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5626 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5628 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5629 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5630 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5631 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5635 640cd7ff 2022-06-22 mark view->count = 0;
5638 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5641 a70480e0 2018-06-23 stsp static const struct got_error *
5642 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5644 a70480e0 2018-06-23 stsp const struct got_error *error;
5645 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5646 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5647 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5648 0587e10c 2020-07-23 stsp char *link_target = NULL;
5649 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5650 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5651 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5653 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5654 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5656 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5657 a70480e0 2018-06-23 stsp switch (ch) {
5659 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5662 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5663 69069811 2018-08-02 stsp if (repo_path == NULL)
5664 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5668 17020d27 2019-03-07 stsp usage_blame();
5669 a70480e0 2018-06-23 stsp /* NOTREACHED */
5673 a70480e0 2018-06-23 stsp argc -= optind;
5674 a70480e0 2018-06-23 stsp argv += optind;
5676 f135c941 2020-02-20 stsp if (argc != 1)
5677 a70480e0 2018-06-23 stsp usage_blame();
5679 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5680 0ae84acc 2022-06-15 tracey if (error != NULL)
5681 0ae84acc 2022-06-15 tracey goto done;
5683 69069811 2018-08-02 stsp if (repo_path == NULL) {
5684 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5685 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5686 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5687 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5688 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5689 c156c7a4 2020-12-18 stsp goto done;
5690 f135c941 2020-02-20 stsp if (worktree)
5691 eb41ed75 2019-02-05 stsp repo_path =
5692 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5694 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5695 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5696 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5697 c156c7a4 2020-12-18 stsp goto done;
5701 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5702 c02c541e 2019-03-29 stsp if (error != NULL)
5703 8e94dd5b 2019-01-04 stsp goto done;
5705 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5706 f135c941 2020-02-20 stsp worktree);
5707 c02c541e 2019-03-29 stsp if (error)
5708 92205607 2019-01-04 stsp goto done;
5710 f135c941 2020-02-20 stsp init_curses();
5712 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5713 51a10b52 2020-12-26 stsp if (error)
5714 51a10b52 2020-12-26 stsp goto done;
5716 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5717 eb41ed75 2019-02-05 stsp if (error)
5718 69069811 2018-08-02 stsp goto done;
5720 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5721 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5722 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5723 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5724 a70480e0 2018-06-23 stsp if (error != NULL)
5725 66b4983c 2018-06-23 stsp goto done;
5726 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5727 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5729 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5730 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5732 a19e88aa 2018-06-23 stsp if (error != NULL)
5733 8b473291 2019-02-21 stsp goto done;
5735 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5736 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5737 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5738 e1cd8fed 2018-08-01 stsp goto done;
5741 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5742 a44927cc 2022-04-07 stsp if (error)
5743 a44927cc 2022-04-07 stsp goto done;
5745 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5746 a44927cc 2022-04-07 stsp commit, repo);
5747 7cbe629d 2018-08-04 stsp if (error)
5748 7cbe629d 2018-08-04 stsp goto done;
5750 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5751 78756c87 2020-11-24 stsp commit_id, repo);
5752 0587e10c 2020-07-23 stsp if (error)
5753 0587e10c 2020-07-23 stsp goto done;
5754 12314ad4 2019-08-31 stsp if (worktree) {
5755 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5756 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5757 12314ad4 2019-08-31 stsp worktree = NULL;
5759 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5761 69069811 2018-08-02 stsp free(repo_path);
5762 f135c941 2020-02-20 stsp free(in_repo_path);
5763 0587e10c 2020-07-23 stsp free(link_target);
5764 69069811 2018-08-02 stsp free(cwd);
5765 a70480e0 2018-06-23 stsp free(commit_id);
5766 a44927cc 2022-04-07 stsp if (commit)
5767 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5768 eb41ed75 2019-02-05 stsp if (worktree)
5769 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5770 1d0f4054 2021-06-17 stsp if (repo) {
5771 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5772 1d0f4054 2021-06-17 stsp if (error == NULL)
5773 1d0f4054 2021-06-17 stsp error = close_err;
5775 0ae84acc 2022-06-15 tracey if (pack_fds) {
5776 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5777 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5778 0ae84acc 2022-06-15 tracey if (error == NULL)
5779 0ae84acc 2022-06-15 tracey error = pack_err;
5781 51a10b52 2020-12-26 stsp tog_free_refs();
5782 a70480e0 2018-06-23 stsp return error;
5785 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5786 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5788 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5789 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5790 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5791 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5792 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5793 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5794 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5796 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5797 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5798 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5799 9b058f45 2022-06-30 mark --limit; /* border */
5801 f7d12f7e 2018-08-01 stsp werase(view->window);
5803 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5804 ffd1d5e5 2018-06-23 stsp return NULL;
5806 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5809 ffd1d5e5 2018-06-23 stsp return err;
5810 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5811 a3404814 2018-09-02 stsp wstandout(view->window);
5812 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5814 11b20872 2019-11-08 stsp wattr_on(view->window,
5815 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5816 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5818 11b20872 2019-11-08 stsp wattr_off(view->window,
5819 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5820 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5821 a3404814 2018-09-02 stsp wstandend(view->window);
5822 2550e4c3 2018-07-13 stsp free(wline);
5823 2550e4c3 2018-07-13 stsp wline = NULL;
5824 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5825 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5826 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5827 ffd1d5e5 2018-06-23 stsp return NULL;
5828 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5831 ce52c690 2018-06-23 stsp return err;
5832 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5833 2550e4c3 2018-07-13 stsp free(wline);
5834 2550e4c3 2018-07-13 stsp wline = NULL;
5835 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5836 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5837 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5838 ffd1d5e5 2018-06-23 stsp return NULL;
5839 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5840 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5841 a1eca9bb 2018-06-23 stsp return NULL;
5843 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5844 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5845 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5846 0cf4efb1 2018-09-29 stsp if (view->focussed)
5847 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5848 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5850 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5851 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5852 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5853 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5854 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5855 ffd1d5e5 2018-06-23 stsp return NULL;
5859 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5862 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5863 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5864 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5865 848d6979 2019-08-12 stsp const char *modestr = "";
5866 56e0773d 2019-11-28 stsp mode_t mode;
5868 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5869 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5871 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5872 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5873 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5875 638f9024 2019-05-13 stsp return got_error_from_errno(
5876 230a42bd 2019-05-11 jcs "got_object_id_str");
5878 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5879 63c5ca5d 2019-08-24 stsp modestr = "$";
5880 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5883 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5884 d86d3b18 2020-12-01 naddy te, s->repo);
5885 0d6c6ee3 2020-05-20 stsp if (err) {
5886 0d6c6ee3 2020-05-20 stsp free(id_str);
5887 0d6c6ee3 2020-05-20 stsp return err;
5889 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5890 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5891 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5893 848d6979 2019-08-12 stsp modestr = "@";
5895 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5896 848d6979 2019-08-12 stsp modestr = "/";
5897 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5898 848d6979 2019-08-12 stsp modestr = "*";
5899 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5900 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5901 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5902 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5903 1d13200f 2018-07-12 stsp free(id_str);
5904 0d6c6ee3 2020-05-20 stsp free(link_target);
5905 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5907 1d13200f 2018-07-12 stsp free(id_str);
5908 0d6c6ee3 2020-05-20 stsp free(link_target);
5909 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5911 ffd1d5e5 2018-06-23 stsp if (err) {
5912 ffd1d5e5 2018-06-23 stsp free(line);
5915 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5916 0cf4efb1 2018-09-29 stsp if (view->focussed)
5917 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5918 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5920 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5922 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5923 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5924 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5926 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5927 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5928 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5929 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5930 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5931 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5932 ffd1d5e5 2018-06-23 stsp free(line);
5933 2550e4c3 2018-07-13 stsp free(wline);
5934 2550e4c3 2018-07-13 stsp wline = NULL;
5936 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5937 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5938 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5942 ffd1d5e5 2018-06-23 stsp return err;
5945 ffd1d5e5 2018-06-23 stsp static void
5946 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5948 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5949 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5950 fa86c4bf 2020-11-29 stsp int i = 0;
5952 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5955 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5956 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5957 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5958 fa86c4bf 2020-11-29 stsp if (!isroot)
5959 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5962 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5963 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5967 9b058f45 2022-06-30 mark static const struct got_error *
5968 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
5970 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
5971 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5972 ffd1d5e5 2018-06-23 stsp int n = 0;
5974 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5975 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5976 694d3271 2020-12-01 naddy s->first_displayed_entry);
5978 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5980 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5981 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
5983 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
5984 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
5985 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5986 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5990 9b058f45 2022-06-30 mark return NULL;
5993 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5994 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5995 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5997 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5998 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5999 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6001 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6002 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6003 56e0773d 2019-11-28 stsp + 1 /* slash */;
6005 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6007 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6008 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6009 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6011 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6012 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6013 d9765a41 2018-06-23 stsp while (pt) {
6014 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6015 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6016 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6017 cb2ebc8a 2018-06-23 stsp goto done;
6019 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6020 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6021 cb2ebc8a 2018-06-23 stsp goto done;
6023 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6026 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6027 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6028 ce52c690 2018-06-23 stsp goto done;
6032 ce52c690 2018-06-23 stsp if (err) {
6033 ce52c690 2018-06-23 stsp free(*path);
6034 ce52c690 2018-06-23 stsp *path = NULL;
6036 ce52c690 2018-06-23 stsp return err;
6039 ce52c690 2018-06-23 stsp static const struct got_error *
6040 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6041 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6042 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6044 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6045 ce52c690 2018-06-23 stsp char *path;
6046 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6048 a0de39f3 2019-08-09 stsp *new_view = NULL;
6050 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6052 ce52c690 2018-06-23 stsp return err;
6054 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6055 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6056 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6057 83ce39e3 2019-08-12 stsp goto done;
6060 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6061 e5a0f69f 2018-08-18 stsp if (err) {
6062 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6063 fc06ba56 2019-08-22 stsp err = NULL;
6064 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6066 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6068 83ce39e3 2019-08-12 stsp free(path);
6069 69efd4c4 2018-07-18 stsp return err;
6072 69efd4c4 2018-07-18 stsp static const struct got_error *
6073 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6074 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6076 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6077 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6078 69efd4c4 2018-07-18 stsp char *path;
6080 a0de39f3 2019-08-09 stsp *new_view = NULL;
6082 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6083 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6084 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6086 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6088 69efd4c4 2018-07-18 stsp return err;
6090 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6093 e5a0f69f 2018-08-18 stsp view_close(log_view);
6095 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6096 cb2ebc8a 2018-06-23 stsp free(path);
6097 cb2ebc8a 2018-06-23 stsp return err;
6100 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6101 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6102 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6104 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6105 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6106 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6107 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6109 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6110 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6112 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6113 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6114 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6116 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6118 bc573f3b 2021-07-10 stsp goto done;
6121 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6122 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6123 bc573f3b 2021-07-10 stsp * closed on demand.
6125 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6126 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6128 bc573f3b 2021-07-10 stsp goto done;
6129 bc573f3b 2021-07-10 stsp s->tree = s->root;
6131 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6132 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6133 ffd1d5e5 2018-06-23 stsp goto done;
6135 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6136 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6137 ffd1d5e5 2018-06-23 stsp goto done;
6140 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6141 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6142 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6143 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6144 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6145 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6146 9cd7cbd1 2020-12-07 stsp goto done;
6149 fb2756b9 2018-08-04 stsp s->repo = repo;
6151 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6152 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6153 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6154 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6156 c0b01bdb 2019-11-08 stsp goto done;
6157 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6158 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6160 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_DIRECTORY,
6163 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6165 c0b01bdb 2019-11-08 stsp goto done;
6167 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6168 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6169 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6171 11b20872 2019-11-08 stsp goto done;
6173 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6174 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6176 c0b01bdb 2019-11-08 stsp goto done;
6179 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6180 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6181 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6182 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6183 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6185 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6186 bc573f3b 2021-07-10 stsp if (commit)
6187 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6189 bc573f3b 2021-07-10 stsp close_tree_view(view);
6190 ad80ab7b 2018-08-04 stsp return err;
6193 e5a0f69f 2018-08-18 stsp static const struct got_error *
6194 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6196 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6198 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6199 fb2756b9 2018-08-04 stsp free(s->tree_label);
6200 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6201 6484ec90 2018-09-29 stsp free(s->commit_id);
6202 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6203 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6204 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6205 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6206 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6207 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6208 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6209 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6210 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6211 ad80ab7b 2018-08-04 stsp free(parent);
6214 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6215 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6216 bc573f3b 2021-07-10 stsp if (s->root)
6217 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6218 7c32bd05 2019-06-22 stsp return NULL;
6221 7c32bd05 2019-06-22 stsp static const struct got_error *
6222 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6224 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6226 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6227 7c32bd05 2019-06-22 stsp return NULL;
6230 7c32bd05 2019-06-22 stsp static int
6231 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6233 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6235 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6239 7c32bd05 2019-06-22 stsp static const struct got_error *
6240 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6242 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6243 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6245 7c32bd05 2019-06-22 stsp if (!view->searching) {
6246 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6247 7c32bd05 2019-06-22 stsp return NULL;
6250 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6251 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6252 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6253 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6254 56e0773d 2019-11-28 stsp s->selected_entry);
6256 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6258 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6259 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6261 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6262 56e0773d 2019-11-28 stsp s->selected_entry);
6265 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6266 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6267 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6268 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6270 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6273 7c32bd05 2019-06-22 stsp while (1) {
6274 56e0773d 2019-11-28 stsp if (te == NULL) {
6275 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6276 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6277 ac66afb8 2019-06-24 stsp return NULL;
6279 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6280 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6282 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6285 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6286 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6287 56e0773d 2019-11-28 stsp s->matched_entry = te;
6291 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6292 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6294 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6297 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6298 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6299 7c32bd05 2019-06-22 stsp s->selected = 0;
6302 e5a0f69f 2018-08-18 stsp return NULL;
6305 ad80ab7b 2018-08-04 stsp static const struct got_error *
6306 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6308 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6309 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6310 e5a0f69f 2018-08-18 stsp char *parent_path;
6312 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6314 e5a0f69f 2018-08-18 stsp return err;
6316 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6317 e5a0f69f 2018-08-18 stsp free(parent_path);
6319 9b058f45 2022-06-30 mark view_border(view);
6320 e5a0f69f 2018-08-18 stsp return err;
6323 e5a0f69f 2018-08-18 stsp static const struct got_error *
6324 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6326 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6327 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6328 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6329 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6330 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
6332 e5a0f69f 2018-08-18 stsp switch (ch) {
6334 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6335 640cd7ff 2022-06-22 mark view->count = 0;
6338 640cd7ff 2022-06-22 mark view->count = 0;
6339 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6341 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6342 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
6343 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
6344 e78dc838 2020-12-04 stsp view->focussed = 0;
6345 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6346 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6347 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6349 1e37a5c2 2019-05-12 jcs return err;
6350 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6353 e78dc838 2020-12-04 stsp view->focus_child = 1;
6355 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6358 640cd7ff 2022-06-22 mark view->count = 0;
6359 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6360 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
6361 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
6362 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
6363 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6364 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6365 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6366 152c1c93 2020-11-29 stsp if (err) {
6367 152c1c93 2020-11-29 stsp view_close(ref_view);
6368 152c1c93 2020-11-29 stsp return err;
6370 e78dc838 2020-12-04 stsp view->focussed = 0;
6371 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6372 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6373 152c1c93 2020-11-29 stsp err = view_close_child(view);
6375 152c1c93 2020-11-29 stsp return err;
6376 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6379 e78dc838 2020-12-04 stsp view->focus_child = 1;
6381 152c1c93 2020-11-29 stsp *new_view = ref_view;
6383 e4526bf5 2021-09-03 naddy case 'g':
6384 e4526bf5 2021-09-03 naddy case KEY_HOME:
6385 e4526bf5 2021-09-03 naddy s->selected = 0;
6386 640cd7ff 2022-06-22 mark view->count = 0;
6387 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6388 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6389 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6391 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6393 e4526bf5 2021-09-03 naddy case 'G':
6394 9b058f45 2022-06-30 mark case KEY_END: {
6395 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6397 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6398 9b058f45 2022-06-30 mark --eos; /* border */
6399 e4526bf5 2021-09-03 naddy s->selected = 0;
6400 640cd7ff 2022-06-22 mark view->count = 0;
6401 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6402 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6403 e4526bf5 2021-09-03 naddy if (te == NULL) {
6404 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6405 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6410 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6411 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6413 e4526bf5 2021-09-03 naddy if (n > 0)
6414 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6418 1e37a5c2 2019-05-12 jcs case KEY_UP:
6419 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6420 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6421 1e37a5c2 2019-05-12 jcs s->selected--;
6424 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6425 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6426 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6427 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6428 640cd7ff 2022-06-22 mark view->count = 0;
6430 83cc4199 2022-06-13 stsp case CTRL('u'):
6432 83cc4199 2022-06-13 stsp nscroll /= 2;
6433 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6434 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6435 ea025d1d 2020-02-22 naddy case CTRL('b'):
6437 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6438 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6439 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6440 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6442 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6443 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6445 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6446 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6447 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6448 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6449 640cd7ff 2022-06-22 mark view->count = 0;
6452 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6453 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6454 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6455 1e37a5c2 2019-05-12 jcs s->selected++;
6458 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6459 640cd7ff 2022-06-22 mark == NULL) {
6460 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6461 640cd7ff 2022-06-22 mark view->count = 0;
6464 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6466 83cc4199 2022-06-13 stsp case CTRL('d'):
6468 83cc4199 2022-06-13 stsp nscroll /= 2;
6469 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6470 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6471 ea025d1d 2020-02-22 naddy case CTRL('f'):
6473 48bb96f0 2022-06-20 naddy case ' ':
6474 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6476 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6477 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6478 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6479 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6481 640cd7ff 2022-06-22 mark view->count = 0;
6484 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6486 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6488 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6489 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6490 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6491 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6492 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6493 640cd7ff 2022-06-22 mark view->count = 0;
6496 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6497 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6499 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6500 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6501 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6502 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6503 1e37a5c2 2019-05-12 jcs s->selected_entry =
6504 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6505 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6506 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6507 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6511 1e37a5c2 2019-05-12 jcs free(parent);
6512 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6513 56e0773d 2019-11-28 stsp s->selected_entry))) {
6514 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6515 640cd7ff 2022-06-22 mark view->count = 0;
6516 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6517 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6520 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6521 941e9f74 2019-05-21 stsp if (err) {
6522 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6525 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6526 56e0773d 2019-11-28 stsp s->selected_entry))) {
6527 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6528 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6530 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6531 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6533 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6534 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6535 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6539 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6540 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6545 640cd7ff 2022-06-22 mark view->count = 0;
6546 e78dc838 2020-12-04 stsp view->focussed = 0;
6547 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6548 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6549 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6550 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6551 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6553 669b5ffa 2018-10-07 stsp return err;
6554 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6557 e78dc838 2020-12-04 stsp view->focus_child = 1;
6559 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6562 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6563 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6564 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6565 640cd7ff 2022-06-22 mark view->count = 0;
6568 640cd7ff 2022-06-22 mark view->count = 0;
6572 ffd1d5e5 2018-06-23 stsp return err;
6575 ffd1d5e5 2018-06-23 stsp __dead static void
6576 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6579 87411fa9 2022-06-16 stsp fprintf(stderr,
6580 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6581 ffd1d5e5 2018-06-23 stsp getprogname());
6585 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6586 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6588 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6589 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6590 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6591 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6592 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6593 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6594 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6595 4e97c21c 2020-12-06 stsp char *label = NULL;
6596 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6597 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6599 5221c383 2018-08-01 stsp struct tog_view *view;
6600 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6602 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6603 ffd1d5e5 2018-06-23 stsp switch (ch) {
6605 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6608 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6609 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6610 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6614 e99e2d15 2020-11-24 naddy usage_tree();
6615 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6619 ffd1d5e5 2018-06-23 stsp argc -= optind;
6620 ffd1d5e5 2018-06-23 stsp argv += optind;
6622 55cccc34 2020-02-20 stsp if (argc > 1)
6623 e99e2d15 2020-11-24 naddy usage_tree();
6625 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6626 0ae84acc 2022-06-15 tracey if (error != NULL)
6627 0ae84acc 2022-06-15 tracey goto done;
6629 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6630 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6631 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6632 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6633 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6634 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6635 c156c7a4 2020-12-18 stsp goto done;
6636 55cccc34 2020-02-20 stsp if (worktree)
6637 52185f70 2019-02-05 stsp repo_path =
6638 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6640 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6641 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6642 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6643 c156c7a4 2020-12-18 stsp goto done;
6647 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6648 c02c541e 2019-03-29 stsp if (error != NULL)
6649 52185f70 2019-02-05 stsp goto done;
6651 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6652 55cccc34 2020-02-20 stsp repo, worktree);
6653 55cccc34 2020-02-20 stsp if (error)
6654 55cccc34 2020-02-20 stsp goto done;
6656 55cccc34 2020-02-20 stsp init_curses();
6658 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6659 c02c541e 2019-03-29 stsp if (error)
6660 52185f70 2019-02-05 stsp goto done;
6662 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6663 51a10b52 2020-12-26 stsp if (error)
6664 51a10b52 2020-12-26 stsp goto done;
6666 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6667 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6668 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6669 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6670 4e97c21c 2020-12-06 stsp if (error)
6671 4e97c21c 2020-12-06 stsp goto done;
6672 4e97c21c 2020-12-06 stsp head_ref_name = label;
6674 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6675 4e97c21c 2020-12-06 stsp if (error == NULL)
6676 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6677 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6678 4e97c21c 2020-12-06 stsp goto done;
6679 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6680 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6681 4e97c21c 2020-12-06 stsp if (error)
6682 4e97c21c 2020-12-06 stsp goto done;
6685 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6686 a44927cc 2022-04-07 stsp if (error)
6687 a44927cc 2022-04-07 stsp goto done;
6689 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6690 5221c383 2018-08-01 stsp if (view == NULL) {
6691 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6692 5221c383 2018-08-01 stsp goto done;
6694 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6695 ad80ab7b 2018-08-04 stsp if (error)
6696 ad80ab7b 2018-08-04 stsp goto done;
6697 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6698 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6699 d91faf3b 2020-12-01 naddy in_repo_path);
6700 55cccc34 2020-02-20 stsp if (error)
6701 55cccc34 2020-02-20 stsp goto done;
6704 55cccc34 2020-02-20 stsp if (worktree) {
6705 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6706 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6707 55cccc34 2020-02-20 stsp worktree = NULL;
6709 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6711 52185f70 2019-02-05 stsp free(repo_path);
6712 e4a0e26d 2020-02-20 stsp free(cwd);
6713 ffd1d5e5 2018-06-23 stsp free(commit_id);
6714 4e97c21c 2020-12-06 stsp free(label);
6716 486cd271 2020-12-06 stsp got_ref_close(ref);
6717 1d0f4054 2021-06-17 stsp if (repo) {
6718 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6719 1d0f4054 2021-06-17 stsp if (error == NULL)
6720 1d0f4054 2021-06-17 stsp error = close_err;
6722 0ae84acc 2022-06-15 tracey if (pack_fds) {
6723 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6724 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6725 0ae84acc 2022-06-15 tracey if (error == NULL)
6726 0ae84acc 2022-06-15 tracey error = pack_err;
6728 51a10b52 2020-12-26 stsp tog_free_refs();
6729 ffd1d5e5 2018-06-23 stsp return error;
6732 6458efa5 2020-11-24 stsp static const struct got_error *
6733 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6735 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6736 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6738 6458efa5 2020-11-24 stsp s->nrefs = 0;
6739 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6740 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6741 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6742 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6743 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6746 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6747 6458efa5 2020-11-24 stsp if (re == NULL)
6748 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6750 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6751 8924d611 2020-12-26 stsp if (re->ref == NULL)
6752 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6753 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6754 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6757 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6758 6458efa5 2020-11-24 stsp return NULL;
6762 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6764 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6766 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6767 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6768 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6769 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6774 6458efa5 2020-11-24 stsp static const struct got_error *
6775 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6777 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6778 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6780 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6781 6458efa5 2020-11-24 stsp s->repo = repo;
6783 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6784 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6786 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6788 6458efa5 2020-11-24 stsp return err;
6790 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6791 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6792 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6793 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6795 6458efa5 2020-11-24 stsp goto done;
6797 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6798 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6799 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6801 6458efa5 2020-11-24 stsp goto done;
6803 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6804 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6805 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6807 cc488aa7 2022-01-23 stsp goto done;
6809 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6810 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6811 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6813 6458efa5 2020-11-24 stsp goto done;
6816 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6817 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6818 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6819 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6820 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6823 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6824 6458efa5 2020-11-24 stsp return err;
6827 6458efa5 2020-11-24 stsp static const struct got_error *
6828 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6830 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6832 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6833 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6835 6458efa5 2020-11-24 stsp return NULL;
6838 6458efa5 2020-11-24 stsp static const struct got_error *
6839 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6840 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6842 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6843 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6844 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6845 6458efa5 2020-11-24 stsp int obj_type;
6847 c42c9805 2020-11-24 stsp *commit_id = NULL;
6849 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6851 6458efa5 2020-11-24 stsp return err;
6853 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6855 6458efa5 2020-11-24 stsp goto done;
6857 6458efa5 2020-11-24 stsp switch (obj_type) {
6858 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6859 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6861 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6862 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6864 6458efa5 2020-11-24 stsp goto done;
6865 c42c9805 2020-11-24 stsp free(obj_id);
6866 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6867 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6869 6458efa5 2020-11-24 stsp goto done;
6870 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6871 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6872 c42c9805 2020-11-24 stsp goto done;
6874 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6875 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6876 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6877 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6878 c42c9805 2020-11-24 stsp goto done;
6882 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6888 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6889 c42c9805 2020-11-24 stsp if (err) {
6890 c42c9805 2020-11-24 stsp free(*commit_id);
6891 c42c9805 2020-11-24 stsp *commit_id = NULL;
6893 c42c9805 2020-11-24 stsp return err;
6896 c42c9805 2020-11-24 stsp static const struct got_error *
6897 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6898 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6900 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6901 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6902 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6904 c42c9805 2020-11-24 stsp *new_view = NULL;
6906 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6907 c42c9805 2020-11-24 stsp if (err) {
6908 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6909 c42c9805 2020-11-24 stsp return err;
6911 c42c9805 2020-11-24 stsp return NULL;
6914 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6915 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6916 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6917 6458efa5 2020-11-24 stsp goto done;
6920 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6921 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6924 6458efa5 2020-11-24 stsp view_close(log_view);
6926 6458efa5 2020-11-24 stsp *new_view = log_view;
6927 c42c9805 2020-11-24 stsp free(commit_id);
6928 6458efa5 2020-11-24 stsp return err;
6931 ce5b7c56 2019-07-09 stsp static void
6932 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6934 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6935 34ba6917 2020-11-30 stsp int i = 0;
6937 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6940 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6941 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6942 34ba6917 2020-11-30 stsp if (re == NULL)
6944 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6945 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6949 9b058f45 2022-06-30 mark static const struct got_error *
6950 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
6952 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
6953 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6954 6458efa5 2020-11-24 stsp int n = 0;
6956 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6957 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6959 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6961 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6962 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6964 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
6965 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
6966 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6967 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6971 9b058f45 2022-06-30 mark return NULL;
6974 6458efa5 2020-11-24 stsp static const struct got_error *
6975 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6977 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6979 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6980 6458efa5 2020-11-24 stsp return NULL;
6983 6458efa5 2020-11-24 stsp static int
6984 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6986 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6988 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6992 6458efa5 2020-11-24 stsp static const struct got_error *
6993 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6995 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6996 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6998 6458efa5 2020-11-24 stsp if (!view->searching) {
6999 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7000 6458efa5 2020-11-24 stsp return NULL;
7003 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7004 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7005 6458efa5 2020-11-24 stsp if (s->selected_entry)
7006 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7008 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7009 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7011 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7012 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7014 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7015 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7018 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7019 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7020 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7021 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7023 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7026 6458efa5 2020-11-24 stsp while (1) {
7027 6458efa5 2020-11-24 stsp if (re == NULL) {
7028 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7029 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7030 6458efa5 2020-11-24 stsp return NULL;
7032 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7033 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7035 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7038 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7039 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7040 6458efa5 2020-11-24 stsp s->matched_entry = re;
7044 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7045 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7047 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7050 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7051 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7052 6458efa5 2020-11-24 stsp s->selected = 0;
7055 6458efa5 2020-11-24 stsp return NULL;
7058 6458efa5 2020-11-24 stsp static const struct got_error *
7059 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7061 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7062 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7063 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7064 6458efa5 2020-11-24 stsp char *line = NULL;
7065 6458efa5 2020-11-24 stsp wchar_t *wline;
7066 6458efa5 2020-11-24 stsp struct tog_color *tc;
7067 6458efa5 2020-11-24 stsp int width, n;
7068 6458efa5 2020-11-24 stsp int limit = view->nlines;
7070 6458efa5 2020-11-24 stsp werase(view->window);
7072 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7073 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7074 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
7075 9b058f45 2022-06-30 mark --limit; /* border */
7077 6458efa5 2020-11-24 stsp if (limit == 0)
7078 6458efa5 2020-11-24 stsp return NULL;
7080 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7082 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7083 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7084 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7086 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7087 6458efa5 2020-11-24 stsp if (err) {
7088 6458efa5 2020-11-24 stsp free(line);
7089 6458efa5 2020-11-24 stsp return err;
7091 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7092 6458efa5 2020-11-24 stsp wstandout(view->window);
7093 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7094 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7095 6458efa5 2020-11-24 stsp wstandend(view->window);
7096 6458efa5 2020-11-24 stsp free(wline);
7097 6458efa5 2020-11-24 stsp wline = NULL;
7098 6458efa5 2020-11-24 stsp free(line);
7099 6458efa5 2020-11-24 stsp line = NULL;
7100 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7101 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7102 6458efa5 2020-11-24 stsp if (--limit <= 0)
7103 6458efa5 2020-11-24 stsp return NULL;
7106 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7107 6458efa5 2020-11-24 stsp char *line = NULL;
7108 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7110 b4996bee 2022-06-16 stsp if (s->show_date) {
7111 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7112 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7113 b4996bee 2022-06-16 stsp struct got_object_id *id;
7114 b4996bee 2022-06-16 stsp struct tm tm;
7117 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7119 b4996bee 2022-06-16 stsp return err;
7120 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7121 b4996bee 2022-06-16 stsp if (err) {
7122 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7124 b4996bee 2022-06-16 stsp return err;
7126 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7128 b4996bee 2022-06-16 stsp if (err) {
7130 b4996bee 2022-06-16 stsp return err;
7132 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7133 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7135 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7136 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7139 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7140 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7141 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7142 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7144 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7145 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7146 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7147 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7148 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7149 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7150 6458efa5 2020-11-24 stsp struct got_object_id *id;
7151 6458efa5 2020-11-24 stsp char *id_str;
7152 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7154 6458efa5 2020-11-24 stsp return err;
7155 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7156 6458efa5 2020-11-24 stsp if (err) {
7158 6458efa5 2020-11-24 stsp return err;
7160 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7161 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7162 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7164 6458efa5 2020-11-24 stsp free(id_str);
7165 6458efa5 2020-11-24 stsp return err;
7168 6458efa5 2020-11-24 stsp free(id_str);
7169 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7170 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7171 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7173 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7175 6458efa5 2020-11-24 stsp if (err) {
7176 6458efa5 2020-11-24 stsp free(line);
7177 6458efa5 2020-11-24 stsp return err;
7179 6458efa5 2020-11-24 stsp if (n == s->selected) {
7180 6458efa5 2020-11-24 stsp if (view->focussed)
7181 6458efa5 2020-11-24 stsp wstandout(view->window);
7182 6458efa5 2020-11-24 stsp s->selected_entry = re;
7184 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7186 6458efa5 2020-11-24 stsp wattr_on(view->window,
7187 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7188 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7190 6458efa5 2020-11-24 stsp wattr_off(view->window,
7191 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7192 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7193 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7194 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7195 6458efa5 2020-11-24 stsp wstandend(view->window);
7196 6458efa5 2020-11-24 stsp free(line);
7197 6458efa5 2020-11-24 stsp free(wline);
7198 6458efa5 2020-11-24 stsp wline = NULL;
7200 6458efa5 2020-11-24 stsp s->ndisplayed++;
7201 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7204 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7207 9b058f45 2022-06-30 mark view_border(view);
7208 6458efa5 2020-11-24 stsp return err;
7211 6458efa5 2020-11-24 stsp static const struct got_error *
7212 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
7213 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7215 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7216 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7217 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7219 c42c9805 2020-11-24 stsp *new_view = NULL;
7221 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7222 c42c9805 2020-11-24 stsp if (err) {
7223 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7224 c42c9805 2020-11-24 stsp return err;
7226 c42c9805 2020-11-24 stsp return NULL;
7230 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7231 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7232 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7233 c42c9805 2020-11-24 stsp goto done;
7236 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7237 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7239 c42c9805 2020-11-24 stsp goto done;
7241 c42c9805 2020-11-24 stsp *new_view = tree_view;
7243 c42c9805 2020-11-24 stsp free(commit_id);
7244 c42c9805 2020-11-24 stsp return err;
7246 c42c9805 2020-11-24 stsp static const struct got_error *
7247 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7249 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7250 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7251 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7252 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7253 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7255 6458efa5 2020-11-24 stsp switch (ch) {
7257 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7258 640cd7ff 2022-06-22 mark view->count = 0;
7261 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7262 640cd7ff 2022-06-22 mark view->count = 0;
7265 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7266 640cd7ff 2022-06-22 mark view->count = 0;
7267 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7268 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7269 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7272 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7273 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7274 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7277 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7278 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7280 6458efa5 2020-11-24 stsp case KEY_ENTER:
7281 6458efa5 2020-11-24 stsp case '\r':
7282 640cd7ff 2022-06-22 mark view->count = 0;
7283 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7285 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7286 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7288 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7289 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7293 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7294 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7299 e78dc838 2020-12-04 stsp view->focussed = 0;
7300 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7301 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7302 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7303 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7304 6458efa5 2020-11-24 stsp err = view_close_child(view);
7306 6458efa5 2020-11-24 stsp return err;
7307 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7310 e78dc838 2020-12-04 stsp view->focus_child = 1;
7312 6458efa5 2020-11-24 stsp *new_view = log_view;
7315 640cd7ff 2022-06-22 mark view->count = 0;
7316 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7318 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7319 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
7320 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7322 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7324 e78dc838 2020-12-04 stsp view->focussed = 0;
7325 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7326 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7327 c42c9805 2020-11-24 stsp err = view_close_child(view);
7329 c42c9805 2020-11-24 stsp return err;
7330 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7333 e78dc838 2020-12-04 stsp view->focus_child = 1;
7335 c42c9805 2020-11-24 stsp *new_view = tree_view;
7337 e4526bf5 2021-09-03 naddy case 'g':
7338 e4526bf5 2021-09-03 naddy case KEY_HOME:
7339 e4526bf5 2021-09-03 naddy s->selected = 0;
7340 640cd7ff 2022-06-22 mark view->count = 0;
7341 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7343 e4526bf5 2021-09-03 naddy case 'G':
7344 9b058f45 2022-06-30 mark case KEY_END: {
7345 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7347 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7348 9b058f45 2022-06-30 mark --eos; /* border */
7349 e4526bf5 2021-09-03 naddy s->selected = 0;
7350 640cd7ff 2022-06-22 mark view->count = 0;
7351 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7352 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7353 e4526bf5 2021-09-03 naddy if (re == NULL)
7355 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7356 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7358 e4526bf5 2021-09-03 naddy if (n > 0)
7359 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7363 6458efa5 2020-11-24 stsp case KEY_UP:
7364 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7365 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7366 6458efa5 2020-11-24 stsp s->selected--;
7369 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7370 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7371 640cd7ff 2022-06-22 mark view->count = 0;
7373 83cc4199 2022-06-13 stsp case CTRL('u'):
7375 83cc4199 2022-06-13 stsp nscroll /= 2;
7376 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7377 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7378 6458efa5 2020-11-24 stsp case CTRL('b'):
7380 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7381 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7382 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7383 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7384 640cd7ff 2022-06-22 mark view->count = 0;
7387 6458efa5 2020-11-24 stsp case KEY_DOWN:
7388 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7389 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7390 6458efa5 2020-11-24 stsp s->selected++;
7393 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7394 6458efa5 2020-11-24 stsp /* can't scroll any further */
7395 640cd7ff 2022-06-22 mark view->count = 0;
7398 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7400 83cc4199 2022-06-13 stsp case CTRL('d'):
7402 83cc4199 2022-06-13 stsp nscroll /= 2;
7403 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7404 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7405 6458efa5 2020-11-24 stsp case CTRL('f'):
7407 48bb96f0 2022-06-20 naddy case ' ':
7408 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7409 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7410 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7411 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7412 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7413 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7414 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7415 640cd7ff 2022-06-22 mark view->count = 0;
7418 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7420 6458efa5 2020-11-24 stsp case CTRL('l'):
7421 640cd7ff 2022-06-22 mark view->count = 0;
7422 8924d611 2020-12-26 stsp tog_free_refs();
7423 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7426 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7427 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7429 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7430 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7431 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7434 640cd7ff 2022-06-22 mark view->count = 0;
7438 6458efa5 2020-11-24 stsp return err;
7441 6458efa5 2020-11-24 stsp __dead static void
7442 6458efa5 2020-11-24 stsp usage_ref(void)
7445 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7446 6458efa5 2020-11-24 stsp getprogname());
7450 6458efa5 2020-11-24 stsp static const struct got_error *
7451 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7453 6458efa5 2020-11-24 stsp const struct got_error *error;
7454 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7455 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7456 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7458 6458efa5 2020-11-24 stsp struct tog_view *view;
7459 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7461 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7462 6458efa5 2020-11-24 stsp switch (ch) {
7464 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7465 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7466 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7470 e99e2d15 2020-11-24 naddy usage_ref();
7471 6458efa5 2020-11-24 stsp /* NOTREACHED */
7475 6458efa5 2020-11-24 stsp argc -= optind;
7476 6458efa5 2020-11-24 stsp argv += optind;
7478 6458efa5 2020-11-24 stsp if (argc > 1)
7479 e99e2d15 2020-11-24 naddy usage_ref();
7481 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7482 0ae84acc 2022-06-15 tracey if (error != NULL)
7483 0ae84acc 2022-06-15 tracey goto done;
7485 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7486 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7487 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7488 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7489 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7490 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7491 c156c7a4 2020-12-18 stsp goto done;
7492 6458efa5 2020-11-24 stsp if (worktree)
7493 6458efa5 2020-11-24 stsp repo_path =
7494 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7496 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7497 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7498 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7499 c156c7a4 2020-12-18 stsp goto done;
7503 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7504 6458efa5 2020-11-24 stsp if (error != NULL)
7505 6458efa5 2020-11-24 stsp goto done;
7507 6458efa5 2020-11-24 stsp init_curses();
7509 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7510 51a10b52 2020-12-26 stsp if (error)
7511 51a10b52 2020-12-26 stsp goto done;
7513 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7514 6458efa5 2020-11-24 stsp if (error)
7515 6458efa5 2020-11-24 stsp goto done;
7517 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7518 6458efa5 2020-11-24 stsp if (view == NULL) {
7519 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7520 6458efa5 2020-11-24 stsp goto done;
7523 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7524 6458efa5 2020-11-24 stsp if (error)
7525 6458efa5 2020-11-24 stsp goto done;
7527 6458efa5 2020-11-24 stsp if (worktree) {
7528 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7529 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7530 6458efa5 2020-11-24 stsp worktree = NULL;
7532 6458efa5 2020-11-24 stsp error = view_loop(view);
7534 6458efa5 2020-11-24 stsp free(repo_path);
7535 6458efa5 2020-11-24 stsp free(cwd);
7536 1d0f4054 2021-06-17 stsp if (repo) {
7537 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7538 1d0f4054 2021-06-17 stsp if (close_err)
7539 1d0f4054 2021-06-17 stsp error = close_err;
7541 0ae84acc 2022-06-15 tracey if (pack_fds) {
7542 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7543 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7544 0ae84acc 2022-06-15 tracey if (error == NULL)
7545 0ae84acc 2022-06-15 tracey error = pack_err;
7547 51a10b52 2020-12-26 stsp tog_free_refs();
7548 6458efa5 2020-11-24 stsp return error;
7552 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7553 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7555 6458efa5 2020-11-24 stsp static void
7556 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7558 9b058f45 2022-06-30 mark switch (view->type) {
7559 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7560 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7561 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7562 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7566 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7567 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7569 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7570 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7573 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7574 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7575 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7577 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7578 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7579 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7581 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7582 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7583 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7589 9b058f45 2022-06-30 mark view->offset = 0;
7593 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7594 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7596 9b058f45 2022-06-30 mark static const struct got_error *
7597 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7599 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7600 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7601 9b058f45 2022-06-30 mark int *selected = NULL;
7602 9b058f45 2022-06-30 mark int header, offset;
7604 9b058f45 2022-06-30 mark switch (view->type) {
7605 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7606 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7607 9b058f45 2022-06-30 mark header = 3;
7608 9b058f45 2022-06-30 mark scrolld = NULL;
7609 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7610 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7611 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7612 9b058f45 2022-06-30 mark s->selected_line -= offset;
7613 9b058f45 2022-06-30 mark view->offset = offset;
7617 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7618 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7619 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7620 9b058f45 2022-06-30 mark header = 3;
7621 9b058f45 2022-06-30 mark selected = &s->selected;
7624 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7625 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7626 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7627 9b058f45 2022-06-30 mark header = 3;
7628 9b058f45 2022-06-30 mark selected = &s->selected;
7631 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7632 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7633 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7634 9b058f45 2022-06-30 mark header = 5;
7635 9b058f45 2022-06-30 mark selected = &s->selected;
7639 9b058f45 2022-06-30 mark selected = NULL;
7640 9b058f45 2022-06-30 mark scrolld = NULL;
7641 9b058f45 2022-06-30 mark header = 0;
7645 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7646 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7647 9b058f45 2022-06-30 mark view->offset = offset;
7648 9b058f45 2022-06-30 mark if (scrolld && offset) {
7649 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7650 9b058f45 2022-06-30 mark *selected -= offset;
7654 9b058f45 2022-06-30 mark return err;
7657 9b058f45 2022-06-30 mark static void
7658 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7662 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7663 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7664 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7665 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7667 6879ba42 2020-10-01 naddy fputc('\n', fp);
7670 4ed7e80c 2018-05-20 stsp __dead static void
7671 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7673 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7675 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7676 6879ba42 2020-10-01 naddy getprogname());
7677 6879ba42 2020-10-01 naddy if (hflag) {
7678 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7679 6879ba42 2020-10-01 naddy list_commands(fp);
7681 6879ba42 2020-10-01 naddy exit(status);
7684 c2301be8 2018-04-30 stsp static char **
7685 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7687 ee85c5e8 2020-02-29 stsp va_list ap;
7688 c2301be8 2018-04-30 stsp char **argv;
7691 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7693 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7694 c2301be8 2018-04-30 stsp if (argv == NULL)
7695 c2301be8 2018-04-30 stsp err(1, "calloc");
7696 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7697 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7698 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7699 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7702 ee85c5e8 2020-02-29 stsp va_end(ap);
7703 c2301be8 2018-04-30 stsp return argv;
7707 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7708 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7709 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7710 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7711 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7713 ee85c5e8 2020-02-29 stsp static const struct got_error *
7714 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7716 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7717 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7718 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7719 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7720 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7721 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7722 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7723 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7724 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7726 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7727 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7728 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7730 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7731 0ae84acc 2022-06-15 tracey if (error != NULL)
7732 0ae84acc 2022-06-15 tracey goto done;
7734 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7735 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7736 ee85c5e8 2020-02-29 stsp goto done;
7738 ee85c5e8 2020-02-29 stsp if (worktree)
7739 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7741 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7742 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7743 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7744 ee85c5e8 2020-02-29 stsp goto done;
7747 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7748 ee85c5e8 2020-02-29 stsp if (error != NULL)
7749 ee85c5e8 2020-02-29 stsp goto done;
7751 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7752 ee85c5e8 2020-02-29 stsp repo, worktree);
7753 ee85c5e8 2020-02-29 stsp if (error)
7754 ee85c5e8 2020-02-29 stsp goto done;
7756 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7757 84de9106 2020-12-26 stsp if (error)
7758 84de9106 2020-12-26 stsp goto done;
7759 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7760 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7761 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7762 ee85c5e8 2020-02-29 stsp if (error)
7763 ee85c5e8 2020-02-29 stsp goto done;
7765 ee85c5e8 2020-02-29 stsp if (worktree) {
7766 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7767 ee85c5e8 2020-02-29 stsp worktree = NULL;
7770 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7771 a44927cc 2022-04-07 stsp if (error)
7772 a44927cc 2022-04-07 stsp goto done;
7774 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7775 ee85c5e8 2020-02-29 stsp if (error) {
7776 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7777 ee85c5e8 2020-02-29 stsp goto done;
7778 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7779 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7780 6879ba42 2020-10-01 naddy usage(1, 1);
7781 ee85c5e8 2020-02-29 stsp /* not reached */
7784 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7785 1d0f4054 2021-06-17 stsp if (error == NULL)
7786 1d0f4054 2021-06-17 stsp error = close_err;
7787 ee85c5e8 2020-02-29 stsp repo = NULL;
7789 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7790 ee85c5e8 2020-02-29 stsp if (error)
7791 ee85c5e8 2020-02-29 stsp goto done;
7793 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7795 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7796 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7798 1d0f4054 2021-06-17 stsp if (repo) {
7799 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7800 1d0f4054 2021-06-17 stsp if (error == NULL)
7801 1d0f4054 2021-06-17 stsp error = close_err;
7803 a44927cc 2022-04-07 stsp if (commit)
7804 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7805 ee85c5e8 2020-02-29 stsp if (worktree)
7806 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7807 0ae84acc 2022-06-15 tracey if (pack_fds) {
7808 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7809 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7810 0ae84acc 2022-06-15 tracey if (error == NULL)
7811 0ae84acc 2022-06-15 tracey error = pack_err;
7814 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7815 ee85c5e8 2020-02-29 stsp free(commit_id);
7816 ee85c5e8 2020-02-29 stsp free(cwd);
7817 ee85c5e8 2020-02-29 stsp free(repo_path);
7818 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7819 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7821 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7822 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7823 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7825 87670572 2020-12-26 naddy tog_free_refs();
7826 ee85c5e8 2020-02-29 stsp return error;
7830 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7832 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7833 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7834 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7835 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7836 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7837 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7838 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7841 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7843 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7844 9f7d7167 2018-04-29 stsp switch (ch) {
7846 9f7d7167 2018-04-29 stsp hflag = 1;
7849 53ccebc2 2019-07-30 stsp Vflag = 1;
7852 6879ba42 2020-10-01 naddy usage(hflag, 1);
7853 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7857 9f7d7167 2018-04-29 stsp argc -= optind;
7858 9f7d7167 2018-04-29 stsp argv += optind;
7859 9814e6a3 2020-09-27 naddy optind = 1;
7860 c2301be8 2018-04-30 stsp optreset = 1;
7862 53ccebc2 2019-07-30 stsp if (Vflag) {
7863 53ccebc2 2019-07-30 stsp got_version_print_str();
7864 6879ba42 2020-10-01 naddy return 0;
7867 4010e238 2020-12-04 stsp #ifndef PROFILE
7868 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7869 4010e238 2020-12-04 stsp NULL) == -1)
7870 4010e238 2020-12-04 stsp err(1, "pledge");
7873 c2301be8 2018-04-30 stsp if (argc == 0) {
7874 f29d3e89 2018-06-23 stsp if (hflag)
7875 6879ba42 2020-10-01 naddy usage(hflag, 0);
7876 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7877 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7879 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7883 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7884 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7885 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7886 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7887 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7893 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7894 ee85c5e8 2020-02-29 stsp if (argc != 1)
7895 6879ba42 2020-10-01 naddy usage(0, 1);
7896 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7897 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7899 ee85c5e8 2020-02-29 stsp if (hflag)
7900 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7902 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7906 b46c1e04 2020-09-20 naddy putchar('\n');
7907 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7909 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7910 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7911 a2f4a359 2020-02-28 stsp free(cmd_argv);
7914 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7915 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);