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 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
110 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
111 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
112 ba4f502b 2018-08-04 stsp struct got_object_id *id;
113 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
116 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 ba4f502b 2018-08-04 stsp struct commit_queue {
118 ba4f502b 2018-08-04 stsp int ncommits;
119 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
122 f26dddb7 2019-11-08 stsp struct tog_color {
123 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
124 6d17833f 2019-11-08 stsp regex_t regex;
125 6d17833f 2019-11-08 stsp short colorpair;
127 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
129 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
132 cc488aa7 2022-01-23 stsp static const struct got_error *
133 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 cc488aa7 2022-01-23 stsp struct got_reference* re2)
136 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
137 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
138 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
140 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
141 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
145 cc488aa7 2022-01-23 stsp return NULL;
146 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
148 cc488aa7 2022-01-23 stsp return NULL;
151 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 cc488aa7 2022-01-23 stsp return NULL;
155 11b20872 2019-11-08 stsp static const struct got_error *
156 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
158 51a10b52 2020-12-26 stsp const struct got_error *err;
160 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
164 51a10b52 2020-12-26 stsp return err;
166 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
170 51a10b52 2020-12-26 stsp static void
171 51a10b52 2020-12-26 stsp tog_free_refs(void)
173 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
174 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
175 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
177 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
180 51a10b52 2020-12-26 stsp static const struct got_error *
181 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
182 11b20872 2019-11-08 stsp int idx, short color)
184 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
185 11b20872 2019-11-08 stsp struct tog_color *tc;
186 11b20872 2019-11-08 stsp int regerr = 0;
188 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
189 11b20872 2019-11-08 stsp return NULL;
191 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
193 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
194 11b20872 2019-11-08 stsp if (tc == NULL)
195 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
196 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
197 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 11b20872 2019-11-08 stsp if (regerr) {
199 11b20872 2019-11-08 stsp static char regerr_msg[512];
200 11b20872 2019-11-08 stsp static char err_msg[512];
201 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
202 11b20872 2019-11-08 stsp sizeof(regerr_msg));
203 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 11b20872 2019-11-08 stsp regerr_msg);
205 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
207 11b20872 2019-11-08 stsp return err;
209 11b20872 2019-11-08 stsp tc->colorpair = idx;
210 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
211 11b20872 2019-11-08 stsp return NULL;
214 11b20872 2019-11-08 stsp static void
215 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
217 11b20872 2019-11-08 stsp struct tog_color *tc;
219 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
220 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
221 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
222 11b20872 2019-11-08 stsp regfree(&tc->regex);
227 11b20872 2019-11-08 stsp struct tog_color *
228 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
230 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
232 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
233 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
237 11b20872 2019-11-08 stsp return NULL;
241 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
243 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
245 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 11b20872 2019-11-08 stsp return COLOR_CYAN;
247 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 11b20872 2019-11-08 stsp return COLOR_YELLOW;
249 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 11b20872 2019-11-08 stsp return COLOR_GREEN;
251 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
253 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
255 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 91b8c405 2020-01-25 stsp return COLOR_CYAN;
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 11b20872 2019-11-08 stsp return COLOR_GREEN;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 11b20872 2019-11-08 stsp return COLOR_GREEN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 11b20872 2019-11-08 stsp return COLOR_CYAN;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 11b20872 2019-11-08 stsp return COLOR_YELLOW;
265 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 6458efa5 2020-11-24 stsp return COLOR_GREEN;
267 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
269 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
271 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
278 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
280 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
282 11b20872 2019-11-08 stsp if (val == NULL)
283 11b20872 2019-11-08 stsp return default_color_value(envvar);
285 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
286 11b20872 2019-11-08 stsp return COLOR_BLACK;
287 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
288 11b20872 2019-11-08 stsp return COLOR_RED;
289 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
290 11b20872 2019-11-08 stsp return COLOR_GREEN;
291 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
292 11b20872 2019-11-08 stsp return COLOR_YELLOW;
293 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
294 11b20872 2019-11-08 stsp return COLOR_BLUE;
295 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
296 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
297 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
298 11b20872 2019-11-08 stsp return COLOR_CYAN;
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
300 11b20872 2019-11-08 stsp return COLOR_WHITE;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
304 11b20872 2019-11-08 stsp return default_color_value(envvar);
308 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
309 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
310 3dbaef42 2020-11-24 stsp const char *label1, *label2;
311 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
312 15a087fe 2019-02-21 stsp int first_displayed_line;
313 15a087fe 2019-02-21 stsp int last_displayed_line;
315 15a087fe 2019-02-21 stsp int diff_context;
316 3dbaef42 2020-11-24 stsp int ignore_whitespace;
317 64453f7e 2020-11-21 stsp int force_text_diff;
318 15a087fe 2019-02-21 stsp struct got_repository *repo;
319 bddb1296 2019-11-08 stsp struct tog_colors colors;
320 fe621944 2020-11-10 stsp size_t nlines;
321 f44b1f58 2020-02-02 tracey off_t *line_offsets;
322 f44b1f58 2020-02-02 tracey int matched_line;
323 f44b1f58 2020-02-02 tracey int selected_line;
325 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
326 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
329 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
332 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
333 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
334 1a76625f 2018-10-22 stsp int commits_needed;
335 fb280deb 2021-08-30 stsp int load_all;
336 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
337 1a76625f 2018-10-22 stsp struct commit_queue *commits;
338 1a76625f 2018-10-22 stsp const char *in_repo_path;
339 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
340 1a76625f 2018-10-22 stsp struct got_repository *repo;
341 74467cc8 2022-06-15 stsp int *pack_fds;
342 1a76625f 2018-10-22 stsp int log_complete;
343 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
344 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
345 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
346 13add988 2019-10-15 stsp int *searching;
347 13add988 2019-10-15 stsp int *search_next_done;
348 13add988 2019-10-15 stsp regex_t *regex;
351 1a76625f 2018-10-22 stsp struct tog_log_view_state {
352 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
353 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
354 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
355 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
356 b01e7d3b 2018-08-04 stsp int selected;
357 b01e7d3b 2018-08-04 stsp char *in_repo_path;
358 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
359 b672a97a 2020-01-27 stsp int log_branches;
360 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
361 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
362 1a76625f 2018-10-22 stsp sig_atomic_t quit;
363 1a76625f 2018-10-22 stsp pthread_t thread;
364 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
365 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
366 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
367 11b20872 2019-11-08 stsp struct tog_colors colors;
370 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
371 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
372 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
374 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
375 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
376 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
377 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
378 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
379 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
380 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
381 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
382 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
383 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
384 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
386 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
387 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
388 e9424729 2018-08-04 stsp int nlines;
390 e9424729 2018-08-04 stsp struct tog_view *view;
391 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
395 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
396 e9424729 2018-08-04 stsp const char *path;
397 e9424729 2018-08-04 stsp struct got_repository *repo;
398 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
399 e9424729 2018-08-04 stsp int *complete;
400 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
401 fc06ba56 2019-08-22 stsp void *cancel_arg;
404 e9424729 2018-08-04 stsp struct tog_blame {
406 be659d10 2020-11-18 stsp off_t filesize;
407 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
408 6fcac457 2018-11-19 stsp int nlines;
409 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
410 e9424729 2018-08-04 stsp pthread_t thread;
411 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
412 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
413 e9424729 2018-08-04 stsp const char *path;
414 0ae84acc 2022-06-15 tracey int *pack_fds;
417 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
418 7cbe629d 2018-08-04 stsp int first_displayed_line;
419 7cbe629d 2018-08-04 stsp int last_displayed_line;
420 7cbe629d 2018-08-04 stsp int selected_line;
421 7cbe629d 2018-08-04 stsp int blame_complete;
424 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
425 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
426 e5a0f69f 2018-08-18 stsp char *path;
427 7cbe629d 2018-08-04 stsp struct got_repository *repo;
428 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
429 e9424729 2018-08-04 stsp struct tog_blame blame;
430 6c4c42e0 2019-06-24 stsp int matched_line;
431 11b20872 2019-11-08 stsp struct tog_colors colors;
434 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
435 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
436 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
437 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
438 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
439 ad80ab7b 2018-08-04 stsp int selected;
442 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
445 ad80ab7b 2018-08-04 stsp char *tree_label;
446 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
447 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
448 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
452 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
453 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
455 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
456 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
457 6458efa5 2020-11-24 stsp struct tog_colors colors;
460 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
461 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
462 6458efa5 2020-11-24 stsp struct got_reference *ref;
466 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
469 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
470 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
471 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
472 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
473 7f66531d 2021-11-16 stsp int nrefs, ndisplayed, selected, show_ids, sort_by_date;
474 6458efa5 2020-11-24 stsp struct got_repository *repo;
475 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
476 bddb1296 2019-11-08 stsp struct tog_colors colors;
480 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
482 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
483 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
484 669b5ffa 2018-10-07 stsp * there is enough screen estate.
486 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
487 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
489 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
490 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
491 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
493 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
494 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
496 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
498 cc3c9aac 2018-08-01 stsp struct tog_view {
499 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
500 26ed57b2 2018-05-19 stsp WINDOW *window;
501 26ed57b2 2018-05-19 stsp PANEL *panel;
502 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
503 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
504 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
506 669b5ffa 2018-10-07 stsp struct tog_view *parent;
507 669b5ffa 2018-10-07 stsp struct tog_view *child;
510 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
511 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
512 e78dc838 2020-12-04 stsp * between parent and child.
513 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
514 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
515 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
516 e78dc838 2020-12-04 stsp * situations.
518 e78dc838 2020-12-04 stsp int focus_child;
520 5dc9f4bc 2018-08-04 stsp /* type-specific state */
521 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
523 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
524 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
525 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
526 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
527 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
530 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
531 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
532 e78dc838 2020-12-04 stsp struct tog_view *, int);
533 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
535 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
536 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
537 c0c4acc8 2021-01-24 stsp int search_started;
538 60493ae3 2019-06-20 stsp int searching;
539 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
540 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
541 60493ae3 2019-06-20 stsp int search_next_done;
542 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
543 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
544 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
545 1803e47f 2019-06-22 stsp regex_t regex;
546 41605754 2020-11-12 stsp regmatch_t regmatch;
549 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
550 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
551 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
552 78756c87 2020-11-24 stsp struct got_repository *);
553 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
554 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
555 e78dc838 2020-12-04 stsp struct tog_view *, int);
556 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
557 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
558 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
560 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
561 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
562 78756c87 2020-11-24 stsp const char *, const char *, int);
563 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
564 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
565 e78dc838 2020-12-04 stsp struct tog_view *, int);
566 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
567 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
568 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
570 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
571 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
572 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
573 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
574 e78dc838 2020-12-04 stsp struct tog_view *, int);
575 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
576 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
577 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
579 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
580 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
581 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
583 e78dc838 2020-12-04 stsp struct tog_view *, int);
584 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
585 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
586 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
588 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
589 6458efa5 2020-11-24 stsp struct got_repository *);
590 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
591 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
592 e78dc838 2020-12-04 stsp struct tog_view *, int);
593 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
594 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
595 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
597 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
598 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
599 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
600 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
601 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
603 25791caa 2018-10-24 stsp static void
604 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
606 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
609 83baff54 2019-08-12 stsp static void
610 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
612 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
615 61266923 2020-01-14 stsp static void
616 61266923 2020-01-14 stsp tog_sigcont(int signo)
618 61266923 2020-01-14 stsp tog_sigcont_received = 1;
621 2497f032 2022-05-31 stsp static void
622 2497f032 2022-05-31 stsp tog_sigint(int signo)
624 2497f032 2022-05-31 stsp tog_sigint_received = 1;
627 2497f032 2022-05-31 stsp static void
628 2497f032 2022-05-31 stsp tog_sigterm(int signo)
630 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
634 2497f032 2022-05-31 stsp tog_fatal_signal_received()
636 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
637 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
641 e5a0f69f 2018-08-18 stsp static const struct got_error *
642 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
644 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
646 669b5ffa 2018-10-07 stsp if (view->child) {
647 669b5ffa 2018-10-07 stsp view_close(view->child);
648 669b5ffa 2018-10-07 stsp view->child = NULL;
650 e5a0f69f 2018-08-18 stsp if (view->close)
651 e5a0f69f 2018-08-18 stsp err = view->close(view);
652 ea5e7bb5 2018-08-01 stsp if (view->panel)
653 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
654 ea5e7bb5 2018-08-01 stsp if (view->window)
655 ea5e7bb5 2018-08-01 stsp delwin(view->window);
656 ea5e7bb5 2018-08-01 stsp free(view);
657 e5a0f69f 2018-08-18 stsp return err;
660 ea5e7bb5 2018-08-01 stsp static struct tog_view *
661 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
662 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
664 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
666 ea5e7bb5 2018-08-01 stsp if (view == NULL)
667 ea5e7bb5 2018-08-01 stsp return NULL;
669 d6b05b5b 2018-08-04 stsp view->type = type;
670 f7d12f7e 2018-08-01 stsp view->lines = LINES;
671 f7d12f7e 2018-08-01 stsp view->cols = COLS;
672 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
673 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
674 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
675 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
676 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
677 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
678 96a765a8 2018-08-04 stsp view_close(view);
679 ea5e7bb5 2018-08-01 stsp return NULL;
681 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
682 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
683 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
684 96a765a8 2018-08-04 stsp view_close(view);
685 ea5e7bb5 2018-08-01 stsp return NULL;
688 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
689 ea5e7bb5 2018-08-01 stsp return view;
693 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
695 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
697 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
700 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
702 5c60c32a 2018-10-18 stsp static const struct got_error *
703 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
705 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
707 5c60c32a 2018-10-18 stsp view->begin_y = 0;
708 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
709 5c60c32a 2018-10-18 stsp view->nlines = LINES;
710 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
711 5c60c32a 2018-10-18 stsp view->lines = LINES;
712 5c60c32a 2018-10-18 stsp view->cols = COLS;
713 5c60c32a 2018-10-18 stsp err = view_resize(view);
715 5c60c32a 2018-10-18 stsp return err;
717 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
718 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
720 5c60c32a 2018-10-18 stsp return NULL;
723 5c60c32a 2018-10-18 stsp static const struct got_error *
724 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
726 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
728 5c60c32a 2018-10-18 stsp view->begin_x = 0;
729 5c60c32a 2018-10-18 stsp view->begin_y = 0;
730 5c60c32a 2018-10-18 stsp view->nlines = LINES;
731 5c60c32a 2018-10-18 stsp view->ncols = COLS;
732 5c60c32a 2018-10-18 stsp view->lines = LINES;
733 5c60c32a 2018-10-18 stsp view->cols = COLS;
734 5c60c32a 2018-10-18 stsp err = view_resize(view);
736 5c60c32a 2018-10-18 stsp return err;
738 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
739 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
741 5c60c32a 2018-10-18 stsp return NULL;
745 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
747 5c60c32a 2018-10-18 stsp return view->parent == NULL;
750 4d8c2215 2018-08-19 stsp static const struct got_error *
751 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
753 f7d12f7e 2018-08-01 stsp int nlines, ncols;
755 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
756 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
758 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
760 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
761 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
763 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
765 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
766 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
767 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
768 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
769 25791caa 2018-10-24 stsp wclear(view->window);
771 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
772 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
773 0cf4efb1 2018-09-29 stsp view->lines = LINES;
774 0cf4efb1 2018-09-29 stsp view->cols = COLS;
776 6e3e5d9c 2018-10-18 stsp if (view->child) {
777 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
778 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
779 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
780 5c60c32a 2018-10-18 stsp if (view->child->focussed)
781 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
783 5c60c32a 2018-10-18 stsp show_panel(view->panel);
785 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
786 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
790 5c60c32a 2018-10-18 stsp return NULL;
793 669b5ffa 2018-10-07 stsp static const struct got_error *
794 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
796 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
798 669b5ffa 2018-10-07 stsp if (view->child == NULL)
799 669b5ffa 2018-10-07 stsp return NULL;
801 669b5ffa 2018-10-07 stsp err = view_close(view->child);
802 669b5ffa 2018-10-07 stsp view->child = NULL;
803 669b5ffa 2018-10-07 stsp return err;
806 72a9cb46 2020-12-03 stsp static void
807 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
809 669b5ffa 2018-10-07 stsp view->child = child;
810 669b5ffa 2018-10-07 stsp child->parent = view;
814 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
816 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
819 34bc9ec9 2019-02-22 stsp static void
820 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
822 25791caa 2018-10-24 stsp int cols, lines;
823 25791caa 2018-10-24 stsp struct winsize size;
825 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
826 25791caa 2018-10-24 stsp cols = 80; /* Default */
827 25791caa 2018-10-24 stsp lines = 24;
829 25791caa 2018-10-24 stsp cols = size.ws_col;
830 25791caa 2018-10-24 stsp lines = size.ws_row;
832 25791caa 2018-10-24 stsp resize_term(lines, cols);
835 2b49a8ae 2019-06-22 stsp static const struct got_error *
836 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
838 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
839 2b49a8ae 2019-06-22 stsp char pattern[1024];
842 c0c4acc8 2021-01-24 stsp if (view->search_started) {
843 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
844 c0c4acc8 2021-01-24 stsp view->searching = 0;
845 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
847 c0c4acc8 2021-01-24 stsp view->search_started = 0;
849 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
850 2b49a8ae 2019-06-22 stsp return NULL;
852 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
853 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
855 2b49a8ae 2019-06-22 stsp nocbreak();
857 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
860 2b49a8ae 2019-06-22 stsp if (ret == ERR)
861 2b49a8ae 2019-06-22 stsp return NULL;
863 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
864 7c32bd05 2019-06-22 stsp err = view->search_start(view);
866 7c32bd05 2019-06-22 stsp regfree(&view->regex);
867 7c32bd05 2019-06-22 stsp return err;
869 c0c4acc8 2021-01-24 stsp view->search_started = 1;
870 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
871 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
872 2b49a8ae 2019-06-22 stsp view->search_next(view);
875 2b49a8ae 2019-06-22 stsp return NULL;
878 0cf4efb1 2018-09-29 stsp static const struct got_error *
879 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
880 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
882 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
883 669b5ffa 2018-10-07 stsp struct tog_view *v;
884 1a76625f 2018-10-22 stsp int ch, errcode;
886 e5a0f69f 2018-08-18 stsp *new = NULL;
888 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
889 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
890 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
891 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
893 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
894 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
895 82954512 2020-02-03 stsp if (errcode)
896 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
897 82954512 2020-02-03 stsp "pthread_mutex_unlock");
898 3da8ef85 2021-09-21 stsp sched_yield();
899 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
900 82954512 2020-02-03 stsp if (errcode)
901 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
902 82954512 2020-02-03 stsp "pthread_mutex_lock");
903 60493ae3 2019-06-20 stsp view->search_next(view);
904 60493ae3 2019-06-20 stsp return NULL;
907 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
908 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
909 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
910 1a76625f 2018-10-22 stsp if (errcode)
911 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
912 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
913 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
914 1a76625f 2018-10-22 stsp if (errcode)
915 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
916 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
918 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
919 25791caa 2018-10-24 stsp tog_resizeterm();
920 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
921 61266923 2020-01-14 stsp tog_sigcont_received = 0;
922 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
923 25791caa 2018-10-24 stsp err = view_resize(v);
925 25791caa 2018-10-24 stsp return err;
926 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
928 25791caa 2018-10-24 stsp return err;
929 cdfcfb03 2020-12-06 stsp if (v->child) {
930 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
932 cdfcfb03 2020-12-06 stsp return err;
933 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
934 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
936 cdfcfb03 2020-12-06 stsp return err;
941 e5a0f69f 2018-08-18 stsp switch (ch) {
943 1e37a5c2 2019-05-12 jcs if (view->child) {
944 e78dc838 2020-12-04 stsp view->focussed = 0;
945 e78dc838 2020-12-04 stsp view->child->focussed = 1;
946 e78dc838 2020-12-04 stsp view->focus_child = 1;
947 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
948 e78dc838 2020-12-04 stsp view->focussed = 0;
949 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
950 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
954 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
955 9970f7fc 2020-12-03 stsp view->dying = 1;
961 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
962 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
964 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
965 e78dc838 2020-12-04 stsp view->focussed = 0;
966 e78dc838 2020-12-04 stsp view->child->focussed = 1;
967 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
969 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
972 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
973 9970f7fc 2020-12-03 stsp KEY_RESIZE);
975 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
976 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
977 e78dc838 2020-12-04 stsp view->focussed = 1;
978 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
980 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
984 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
987 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
990 60493ae3 2019-06-20 stsp if (view->search_start)
991 2b49a8ae 2019-06-22 stsp view_search_start(view);
993 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
997 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
998 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
999 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1000 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1001 60493ae3 2019-06-20 stsp view->search_next(view);
1003 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1006 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1010 e5a0f69f 2018-08-18 stsp return err;
1014 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1016 0cf4efb1 2018-09-29 stsp PANEL *panel;
1017 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1019 669b5ffa 2018-10-07 stsp if (view->parent)
1020 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1022 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1023 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1026 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1027 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1028 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1032 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1034 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1035 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1037 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1039 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1042 669b5ffa 2018-10-07 stsp return view->focussed;
1045 bcbd79e2 2018-08-19 stsp static const struct got_error *
1046 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1048 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1049 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1050 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1051 fd823528 2018-10-22 stsp int fast_refresh = 10;
1052 1a76625f 2018-10-22 stsp int done = 0, errcode;
1054 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1055 1a76625f 2018-10-22 stsp if (errcode)
1056 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1058 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1059 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1061 1004088d 2018-09-29 stsp view->focussed = 1;
1062 878940b7 2018-09-29 stsp err = view->show(view);
1064 0cf4efb1 2018-09-29 stsp return err;
1065 0cf4efb1 2018-09-29 stsp update_panels();
1066 0cf4efb1 2018-09-29 stsp doupdate();
1067 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1068 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1069 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1070 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1072 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1075 9970f7fc 2020-12-03 stsp if (view->dying) {
1076 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1078 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1079 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1081 e78dc838 2020-12-04 stsp else if (view->parent)
1082 669b5ffa 2018-10-07 stsp prev = view->parent;
1084 e78dc838 2020-12-04 stsp if (view->parent) {
1085 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1086 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1088 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1090 9970f7fc 2020-12-03 stsp err = view_close(view);
1092 e5a0f69f 2018-08-18 stsp goto done;
1094 e78dc838 2020-12-04 stsp view = NULL;
1095 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1096 e78dc838 2020-12-04 stsp if (v->focussed)
1099 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1100 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1102 e78dc838 2020-12-04 stsp view = prev;
1103 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1104 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1105 e78dc838 2020-12-04 stsp tog_view_list_head);
1107 e78dc838 2020-12-04 stsp if (view) {
1108 e78dc838 2020-12-04 stsp if (view->focus_child) {
1109 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1110 e78dc838 2020-12-04 stsp view = view->child;
1112 e78dc838 2020-12-04 stsp view->focussed = 1;
1116 bcbd79e2 2018-08-19 stsp if (new_view) {
1117 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1118 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1119 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1120 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1122 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1123 86c66b02 2018-10-18 stsp err = view_close(v);
1125 86c66b02 2018-10-18 stsp goto done;
1128 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1129 fed7eaa8 2018-10-24 stsp view = new_view;
1131 669b5ffa 2018-10-07 stsp if (view) {
1132 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1133 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1134 e78dc838 2020-12-04 stsp view = view->child;
1136 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1137 e78dc838 2020-12-04 stsp view = view->parent;
1139 e78dc838 2020-12-04 stsp show_panel(view->panel);
1140 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1141 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1142 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1143 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1145 1a76625f 2018-10-22 stsp goto done;
1147 669b5ffa 2018-10-07 stsp err = view->show(view);
1149 1a76625f 2018-10-22 stsp goto done;
1150 669b5ffa 2018-10-07 stsp if (view->child) {
1151 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1153 1a76625f 2018-10-22 stsp goto done;
1155 1a76625f 2018-10-22 stsp update_panels();
1156 1a76625f 2018-10-22 stsp doupdate();
1160 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1161 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1162 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1163 e5a0f69f 2018-08-18 stsp view_close(view);
1166 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1167 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1168 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1170 e5a0f69f 2018-08-18 stsp return err;
1173 4ed7e80c 2018-05-20 stsp __dead static void
1174 9f7d7167 2018-04-29 stsp usage_log(void)
1177 c70c5802 2018-08-01 stsp fprintf(stderr,
1178 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1179 9f7d7167 2018-04-29 stsp getprogname());
1183 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1184 80ddbec8 2018-04-29 stsp static const struct got_error *
1185 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1187 00dfcb92 2018-06-11 stsp char *vis = NULL;
1188 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1190 963b370f 2018-05-20 stsp *ws = NULL;
1191 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1192 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1193 00dfcb92 2018-06-11 stsp int vislen;
1194 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1195 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1197 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1198 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1200 00dfcb92 2018-06-11 stsp return err;
1201 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1202 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1203 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1204 a7f50699 2018-06-11 stsp goto done;
1208 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1209 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1210 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1211 a7f50699 2018-06-11 stsp goto done;
1214 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1215 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1217 00dfcb92 2018-06-11 stsp free(vis);
1218 963b370f 2018-05-20 stsp if (err) {
1219 963b370f 2018-05-20 stsp free(*ws);
1220 963b370f 2018-05-20 stsp *ws = NULL;
1221 963b370f 2018-05-20 stsp *wlen = 0;
1223 963b370f 2018-05-20 stsp return err;
1226 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1227 963b370f 2018-05-20 stsp static const struct got_error *
1228 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1229 27a741e5 2019-09-11 stsp int col_tab_align)
1231 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1232 963b370f 2018-05-20 stsp int cols = 0;
1233 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1234 963b370f 2018-05-20 stsp size_t wlen;
1237 963b370f 2018-05-20 stsp *wlinep = NULL;
1238 b700b5d6 2018-07-10 stsp *widthp = 0;
1240 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1242 963b370f 2018-05-20 stsp return err;
1244 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1245 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1248 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1249 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1254 27a741e5 2019-09-11 stsp while (i < wlen) {
1255 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1257 27a741e5 2019-09-11 stsp if (width == 0) {
1262 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1263 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1265 27a741e5 2019-09-11 stsp cols += width;
1267 27a741e5 2019-09-11 stsp } else if (width == -1) {
1268 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1269 27a741e5 2019-09-11 stsp width = TABSIZE -
1270 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1272 748d5cab 2020-12-14 naddy width = 1;
1273 748d5cab 2020-12-14 naddy wline[i] = L'.';
1275 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1277 748d5cab 2020-12-14 naddy cols += width;
1280 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1281 963b370f 2018-05-20 stsp goto done;
1284 963b370f 2018-05-20 stsp wline[i] = L'\0';
1285 b700b5d6 2018-07-10 stsp if (widthp)
1286 b700b5d6 2018-07-10 stsp *widthp = cols;
1289 963b370f 2018-05-20 stsp free(wline);
1291 963b370f 2018-05-20 stsp *wlinep = wline;
1292 963b370f 2018-05-20 stsp return err;
1295 8b473291 2019-02-21 stsp static const struct got_error*
1296 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1297 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1299 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1300 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1302 8b473291 2019-02-21 stsp const char *name;
1304 8b473291 2019-02-21 stsp *refs_str = NULL;
1306 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1307 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1308 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1311 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1312 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1314 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1315 8b473291 2019-02-21 stsp name += 5;
1316 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1317 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1319 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1320 8b473291 2019-02-21 stsp name += 6;
1321 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1322 8b473291 2019-02-21 stsp name += 8;
1323 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1324 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1327 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1330 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1331 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1332 5d844a1e 2019-08-13 stsp if (err) {
1333 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1334 48cae60d 2020-09-22 stsp free(ref_id);
1337 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1338 5d844a1e 2019-08-13 stsp err = NULL;
1339 5d844a1e 2019-08-13 stsp tag = NULL;
1342 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1343 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1344 48cae60d 2020-09-22 stsp free(ref_id);
1346 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1347 52b5abe1 2019-08-13 stsp if (cmp != 0)
1349 8b473291 2019-02-21 stsp s = *refs_str;
1350 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1351 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1352 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1354 8b473291 2019-02-21 stsp *refs_str = NULL;
1360 8b473291 2019-02-21 stsp return err;
1363 963b370f 2018-05-20 stsp static const struct got_error *
1364 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1365 27a741e5 2019-09-11 stsp int col_tab_align)
1367 e6b8b890 2020-12-29 naddy char *smallerthan;
1369 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1370 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1371 5813d178 2019-03-09 stsp author = smallerthan + 1;
1372 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1373 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1376 5813d178 2019-03-09 stsp static const struct got_error *
1377 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1378 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1379 8fdc79fe 2020-12-01 naddy int author_display_cols)
1381 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1382 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1383 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1384 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1385 5813d178 2019-03-09 stsp char *author = NULL;
1386 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1387 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1388 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1389 bb737323 2018-05-20 stsp int col, limit;
1390 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1391 ccb26ccd 2018-11-05 stsp struct tm tm;
1392 45d799e2 2018-12-23 stsp time_t committer_time;
1393 11b20872 2019-11-08 stsp struct tog_color *tc;
1395 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1396 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1397 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1398 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1399 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1401 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1402 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1404 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1405 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1407 11b20872 2019-11-08 stsp wattr_on(view->window,
1408 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1409 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1411 11b20872 2019-11-08 stsp wattr_off(view->window,
1412 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1413 27a741e5 2019-09-11 stsp col = limit;
1414 b39d25c7 2018-07-10 stsp if (col > avail)
1415 b39d25c7 2018-07-10 stsp goto done;
1417 6570a66d 2019-11-08 stsp if (avail >= 120) {
1418 6570a66d 2019-11-08 stsp char *id_str;
1419 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1421 6570a66d 2019-11-08 stsp goto done;
1422 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1424 11b20872 2019-11-08 stsp wattr_on(view->window,
1425 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1426 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1428 11b20872 2019-11-08 stsp wattr_off(view->window,
1429 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1430 6570a66d 2019-11-08 stsp free(id_str);
1432 6570a66d 2019-11-08 stsp if (col > avail)
1433 6570a66d 2019-11-08 stsp goto done;
1436 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1437 5813d178 2019-03-09 stsp if (author == NULL) {
1438 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1439 80ddbec8 2018-04-29 stsp goto done;
1441 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1443 bb737323 2018-05-20 stsp goto done;
1444 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1446 11b20872 2019-11-08 stsp wattr_on(view->window,
1447 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1448 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1450 11b20872 2019-11-08 stsp wattr_off(view->window,
1451 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1452 bb737323 2018-05-20 stsp col += author_width;
1453 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1454 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1456 bb737323 2018-05-20 stsp author_width++;
1458 9c2eaf34 2018-05-20 stsp if (col > avail)
1459 9c2eaf34 2018-05-20 stsp goto done;
1461 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1463 6d9fbc00 2018-04-29 stsp goto done;
1464 bb737323 2018-05-20 stsp logmsg = logmsg0;
1465 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1467 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1468 bb737323 2018-05-20 stsp if (newline)
1469 bb737323 2018-05-20 stsp *newline = '\0';
1470 bb737323 2018-05-20 stsp limit = avail - col;
1471 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1473 bb737323 2018-05-20 stsp goto done;
1474 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1475 bb737323 2018-05-20 stsp col += logmsg_width;
1476 27a741e5 2019-09-11 stsp while (col < avail) {
1477 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1481 80ddbec8 2018-04-29 stsp free(logmsg0);
1482 bb737323 2018-05-20 stsp free(wlogmsg);
1483 5813d178 2019-03-09 stsp free(author);
1484 bb737323 2018-05-20 stsp free(wauthor);
1485 80ddbec8 2018-04-29 stsp free(line);
1486 80ddbec8 2018-04-29 stsp return err;
1489 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1490 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1491 899d86c2 2018-05-10 stsp struct got_object_id *id)
1493 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1495 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1496 80ddbec8 2018-04-29 stsp if (entry == NULL)
1497 899d86c2 2018-05-10 stsp return NULL;
1499 899d86c2 2018-05-10 stsp entry->id = id;
1500 99db9666 2018-05-07 stsp entry->commit = commit;
1501 899d86c2 2018-05-10 stsp return entry;
1504 99db9666 2018-05-07 stsp static void
1505 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1507 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1509 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1510 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1511 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1512 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1513 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1514 99db9666 2018-05-07 stsp free(entry);
1517 99db9666 2018-05-07 stsp static void
1518 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1520 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1521 99db9666 2018-05-07 stsp pop_commit(commits);
1524 c4972b91 2018-05-07 stsp static const struct got_error *
1525 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1526 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1528 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1529 13add988 2019-10-15 stsp regmatch_t regmatch;
1530 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1532 13add988 2019-10-15 stsp *have_match = 0;
1534 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1536 13add988 2019-10-15 stsp return err;
1538 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1540 13add988 2019-10-15 stsp goto done;
1542 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1543 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1544 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1545 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1546 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1547 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1548 13add988 2019-10-15 stsp *have_match = 1;
1550 13add988 2019-10-15 stsp free(id_str);
1551 13add988 2019-10-15 stsp free(logmsg);
1552 13add988 2019-10-15 stsp return err;
1555 13add988 2019-10-15 stsp static const struct got_error *
1556 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1558 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1561 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1562 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1563 1a76625f 2018-10-22 stsp * while updating the display.
1566 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1567 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1568 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1569 1a76625f 2018-10-22 stsp int errcode;
1571 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1572 4e0d2870 2020-12-07 naddy NULL, NULL);
1573 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1576 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1579 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1580 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1581 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1585 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1586 1a76625f 2018-10-22 stsp if (errcode) {
1587 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1588 13add988 2019-10-15 stsp "pthread_mutex_lock");
1592 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1593 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1594 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1596 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1597 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1598 7c1452c1 2020-03-26 stsp int have_match;
1599 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1602 7c1452c1 2020-03-26 stsp if (have_match)
1603 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1606 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1607 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1608 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1609 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1612 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1614 9ba79e04 2018-06-11 stsp return err;
1617 2b779855 2020-12-05 naddy static void
1618 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1620 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1621 2b779855 2020-12-05 naddy int ncommits = 0;
1623 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1624 2b779855 2020-12-05 naddy while (entry) {
1625 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1626 2b779855 2020-12-05 naddy s->selected_entry = entry;
1629 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1630 2b779855 2020-12-05 naddy ncommits++;
1634 0553a4e3 2018-04-30 stsp static const struct got_error *
1635 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1637 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1638 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1639 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1640 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1641 60493ae3 2019-06-20 stsp int width;
1642 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1643 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1644 8b473291 2019-02-21 stsp char *refs_str = NULL;
1645 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1646 11b20872 2019-11-08 stsp struct tog_color *tc;
1647 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1649 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1650 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1651 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1652 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1654 ecb28ae0 2018-07-16 stsp return err;
1655 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1656 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1657 d2075bf3 2020-12-25 stsp if (refs) {
1658 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1659 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1661 d2075bf3 2020-12-25 stsp goto done;
1665 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1666 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1668 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1669 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1670 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1671 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1672 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1673 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1674 8f4ed634 2020-03-26 stsp goto done;
1677 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1679 f9686aa5 2020-03-27 stsp if (view->searching) {
1680 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1681 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1682 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1683 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1684 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1685 f9686aa5 2020-03-27 stsp search_str = "searching...";
1688 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1689 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1690 f9686aa5 2020-03-27 stsp search_str ? search_str :
1691 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1692 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1693 8f4ed634 2020-03-26 stsp goto done;
1697 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1698 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1699 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1700 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1701 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1702 1a76625f 2018-10-22 stsp header = NULL;
1703 1a76625f 2018-10-22 stsp goto done;
1705 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1706 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1707 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1708 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1709 1a76625f 2018-10-22 stsp header = NULL;
1710 1a76625f 2018-10-22 stsp goto done;
1712 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1714 1a76625f 2018-10-22 stsp goto done;
1716 2814baeb 2018-08-01 stsp werase(view->window);
1718 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1719 a3404814 2018-09-02 stsp wstandout(view->window);
1720 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1722 11b20872 2019-11-08 stsp wattr_on(view->window,
1723 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1724 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1726 11b20872 2019-11-08 stsp wattr_off(view->window,
1727 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1728 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1729 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1732 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1733 a3404814 2018-09-02 stsp wstandend(view->window);
1734 ecb28ae0 2018-07-16 stsp free(wline);
1735 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1736 1a76625f 2018-10-22 stsp goto done;
1738 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1739 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1740 5813d178 2019-03-09 stsp ncommits = 0;
1741 5813d178 2019-03-09 stsp while (entry) {
1742 5813d178 2019-03-09 stsp char *author;
1743 5813d178 2019-03-09 stsp wchar_t *wauthor;
1744 5813d178 2019-03-09 stsp int width;
1745 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1747 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1748 5813d178 2019-03-09 stsp if (author == NULL) {
1749 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1750 5813d178 2019-03-09 stsp goto done;
1752 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1753 27a741e5 2019-09-11 stsp date_display_cols);
1754 5813d178 2019-03-09 stsp if (author_cols < width)
1755 5813d178 2019-03-09 stsp author_cols = width;
1756 5813d178 2019-03-09 stsp free(wauthor);
1757 5813d178 2019-03-09 stsp free(author);
1758 7ca04879 2019-10-19 stsp ncommits++;
1759 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1762 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1763 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1764 867c6645 2018-07-10 stsp ncommits = 0;
1765 899d86c2 2018-05-10 stsp while (entry) {
1766 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1768 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1769 2814baeb 2018-08-01 stsp wstandout(view->window);
1770 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1771 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1772 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1773 2814baeb 2018-08-01 stsp wstandend(view->window);
1775 60493ae3 2019-06-20 stsp goto done;
1776 0553a4e3 2018-04-30 stsp ncommits++;
1777 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1778 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1781 1a57306a 2018-09-02 stsp view_vborder(view);
1783 1a76625f 2018-10-22 stsp free(id_str);
1784 8b473291 2019-02-21 stsp free(refs_str);
1785 1a76625f 2018-10-22 stsp free(ncommits_str);
1786 1a76625f 2018-10-22 stsp free(header);
1787 80ddbec8 2018-04-29 stsp return err;
1790 07b55e75 2018-05-10 stsp static void
1791 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1793 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1794 07b55e75 2018-05-10 stsp int nscrolled = 0;
1796 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1797 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1800 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1801 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1802 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1803 07b55e75 2018-05-10 stsp if (entry) {
1804 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1805 07b55e75 2018-05-10 stsp nscrolled++;
1810 aa075928 2018-05-10 stsp static const struct got_error *
1811 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1813 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1814 5e224a3e 2019-02-22 stsp int errcode;
1816 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1818 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1819 ffe38506 2020-12-01 naddy if (ta->log_complete)
1822 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1823 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1824 7aafa0d1 2019-02-22 stsp if (errcode)
1825 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1826 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1829 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1830 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1832 7c1452c1 2020-03-26 stsp if (!wait)
1835 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1836 ffe38506 2020-12-01 naddy show_log_view(view);
1837 7c1452c1 2020-03-26 stsp update_panels();
1838 7c1452c1 2020-03-26 stsp doupdate();
1840 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1841 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1842 82954512 2020-02-03 stsp if (errcode)
1843 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1844 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1846 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1847 ffe38506 2020-12-01 naddy show_log_view(view);
1848 7c1452c1 2020-03-26 stsp update_panels();
1849 7c1452c1 2020-03-26 stsp doupdate();
1852 5e224a3e 2019-02-22 stsp return NULL;
1855 5e224a3e 2019-02-22 stsp static const struct got_error *
1856 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1858 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1859 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1860 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1861 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1863 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1864 5e224a3e 2019-02-22 stsp return NULL;
1866 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1867 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1868 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1870 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1872 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1873 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1875 5e224a3e 2019-02-22 stsp return err;
1879 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1880 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1883 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1885 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1886 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1888 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1889 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1891 dd0a52c1 2018-05-20 stsp return err;
1894 cd0acaa7 2018-05-20 stsp static const struct got_error *
1895 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1896 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1897 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1899 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1900 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1901 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1903 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1904 15a94983 2018-12-23 stsp if (diff_view == NULL)
1905 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1907 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1908 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1909 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1910 e5a0f69f 2018-08-18 stsp if (err == NULL)
1911 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1912 cd0acaa7 2018-05-20 stsp return err;
1915 80ddbec8 2018-04-29 stsp static const struct got_error *
1916 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1917 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1919 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1921 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1922 941e9f74 2019-05-21 stsp if (parent == NULL)
1923 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1925 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1926 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1927 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1928 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1929 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1930 941e9f74 2019-05-21 stsp s->tree = subtree;
1931 941e9f74 2019-05-21 stsp s->selected = 0;
1932 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1933 941e9f74 2019-05-21 stsp return NULL;
1936 941e9f74 2019-05-21 stsp static const struct got_error *
1937 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1938 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
1940 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1941 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1942 941e9f74 2019-05-21 stsp const char *p;
1943 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1945 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1947 941e9f74 2019-05-21 stsp while (*p) {
1948 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1949 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1950 56e0773d 2019-11-28 stsp char *te_name;
1952 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1955 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1956 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1957 941e9f74 2019-05-21 stsp if (slash == NULL)
1958 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1960 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1961 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1962 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1965 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1966 56e0773d 2019-11-28 stsp if (te == NULL) {
1967 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1968 56e0773d 2019-11-28 stsp free(te_name);
1971 56e0773d 2019-11-28 stsp free(te_name);
1972 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1974 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1975 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1977 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1978 941e9f74 2019-05-21 stsp if (slash)
1979 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1981 941e9f74 2019-05-21 stsp subpath = strdup(path);
1982 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1983 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1987 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
1992 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1993 941e9f74 2019-05-21 stsp free(tree_id);
1997 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1998 941e9f74 2019-05-21 stsp if (err) {
1999 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2002 941e9f74 2019-05-21 stsp if (slash == NULL)
2004 941e9f74 2019-05-21 stsp free(subpath);
2005 941e9f74 2019-05-21 stsp subpath = NULL;
2006 941e9f74 2019-05-21 stsp p = slash;
2009 941e9f74 2019-05-21 stsp free(subpath);
2010 1a76625f 2018-10-22 stsp return err;
2013 61266923 2020-01-14 stsp static const struct got_error *
2014 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2015 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2016 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2018 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2019 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2020 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2022 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2023 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2024 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2026 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2028 55cccc34 2020-02-20 stsp return err;
2029 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2031 55cccc34 2020-02-20 stsp *new_view = tree_view;
2033 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2034 55cccc34 2020-02-20 stsp return NULL;
2036 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2039 55cccc34 2020-02-20 stsp static const struct got_error *
2040 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2042 61266923 2020-01-14 stsp sigset_t sigset;
2043 61266923 2020-01-14 stsp int errcode;
2045 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2046 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2048 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2049 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2050 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2051 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2052 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2053 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2054 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2055 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2056 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2058 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2059 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2060 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2062 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2063 61266923 2020-01-14 stsp if (errcode)
2064 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2066 61266923 2020-01-14 stsp return NULL;
2069 1a76625f 2018-10-22 stsp static void *
2070 1a76625f 2018-10-22 stsp log_thread(void *arg)
2072 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2073 1a76625f 2018-10-22 stsp int errcode = 0;
2074 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2075 1a76625f 2018-10-22 stsp int done = 0;
2077 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2079 61266923 2020-01-14 stsp return (void *)err;
2081 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2082 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2083 1a76625f 2018-10-22 stsp if (err) {
2084 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2085 1a76625f 2018-10-22 stsp return (void *)err;
2086 1a76625f 2018-10-22 stsp err = NULL;
2088 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2089 1a76625f 2018-10-22 stsp a->commits_needed--;
2091 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2092 3abe8080 2019-04-10 stsp if (errcode) {
2093 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2094 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2096 3abe8080 2019-04-10 stsp } else if (*a->quit)
2098 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2099 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2100 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2101 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2104 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2105 7c1452c1 2020-03-26 stsp if (errcode) {
2106 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2107 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2108 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2113 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2115 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2116 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2117 7c1452c1 2020-03-26 stsp &tog_mutex);
2118 7c1452c1 2020-03-26 stsp if (errcode)
2119 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2120 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2121 21355643 2020-12-06 stsp if (*a->quit)
2126 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2127 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2128 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2129 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2131 3abe8080 2019-04-10 stsp a->log_complete = 1;
2132 1a76625f 2018-10-22 stsp return (void *)err;
2135 1a76625f 2018-10-22 stsp static const struct got_error *
2136 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2138 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2139 1a76625f 2018-10-22 stsp int errcode;
2141 1a76625f 2018-10-22 stsp if (s->thread) {
2142 1a76625f 2018-10-22 stsp s->quit = 1;
2143 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2144 1a76625f 2018-10-22 stsp if (errcode)
2145 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2146 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2147 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2148 1a76625f 2018-10-22 stsp if (errcode)
2149 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2150 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2151 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2152 1a76625f 2018-10-22 stsp if (errcode)
2153 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2154 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2155 1a76625f 2018-10-22 stsp if (errcode)
2156 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2157 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2158 1a76625f 2018-10-22 stsp s->thread = NULL;
2161 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2162 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2163 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2166 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2167 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2168 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2169 74467cc8 2022-06-15 stsp if (err == NULL)
2170 74467cc8 2022-06-15 stsp err = pack_err;
2171 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2174 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2175 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2176 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2179 9343a5fb 2018-06-23 stsp return err;
2182 9343a5fb 2018-06-23 stsp static const struct got_error *
2183 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2185 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2186 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2187 276b94a1 2020-11-13 naddy int errcode;
2189 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2191 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2192 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2193 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2195 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2196 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2197 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2199 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2200 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2201 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2202 1a76625f 2018-10-22 stsp free(s->start_id);
2203 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2204 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2205 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2206 1a76625f 2018-10-22 stsp return err;
2209 1a76625f 2018-10-22 stsp static const struct got_error *
2210 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2212 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2214 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2215 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2216 60493ae3 2019-06-20 stsp return NULL;
2219 60493ae3 2019-06-20 stsp static const struct got_error *
2220 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2222 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2223 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2224 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2226 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2227 f9686aa5 2020-03-27 stsp show_log_view(view);
2228 f9686aa5 2020-03-27 stsp update_panels();
2229 f9686aa5 2020-03-27 stsp doupdate();
2231 96e2b566 2019-07-08 stsp if (s->search_entry) {
2232 13add988 2019-10-15 stsp int errcode, ch;
2233 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2234 13add988 2019-10-15 stsp if (errcode)
2235 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2236 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2237 13add988 2019-10-15 stsp ch = wgetch(view->window);
2238 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2239 13add988 2019-10-15 stsp if (errcode)
2240 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2241 13add988 2019-10-15 stsp "pthread_mutex_lock");
2242 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2243 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2244 678cbce5 2019-07-28 stsp return NULL;
2246 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2247 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2249 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2250 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2251 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2252 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2253 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2255 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2256 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2258 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2261 60493ae3 2019-06-20 stsp while (1) {
2262 13add988 2019-10-15 stsp int have_match = 0;
2264 60493ae3 2019-06-20 stsp if (entry == NULL) {
2265 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2266 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2267 f9967bca 2020-03-27 stsp view->search_next_done =
2268 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2269 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2270 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2271 f801134a 2019-06-25 stsp return NULL;
2274 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2275 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2276 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2278 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2279 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2282 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2283 13add988 2019-10-15 stsp &view->regex);
2286 13add988 2019-10-15 stsp if (have_match) {
2287 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2288 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2292 96e2b566 2019-07-08 stsp s->search_entry = entry;
2293 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2294 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2296 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2299 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2300 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2301 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2302 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2304 60493ae3 2019-06-20 stsp return err;
2307 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2308 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2310 60493ae3 2019-06-20 stsp return err;
2315 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2317 60493ae3 2019-06-20 stsp return NULL;
2320 60493ae3 2019-06-20 stsp static const struct got_error *
2321 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2322 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2323 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2325 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2326 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2327 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2328 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2329 1a76625f 2018-10-22 stsp int errcode;
2331 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2332 f135c941 2020-02-20 stsp free(s->in_repo_path);
2333 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2334 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2335 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2338 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2339 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2340 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2342 fb2756b9 2018-08-04 stsp s->repo = repo;
2343 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2344 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2345 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2346 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2347 9cd7cbd1 2020-12-07 stsp goto done;
2350 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2351 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2353 5036bf37 2018-09-24 stsp goto done;
2355 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2357 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2358 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2359 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2360 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2362 11b20872 2019-11-08 stsp goto done;
2363 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2364 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2365 11b20872 2019-11-08 stsp if (err) {
2366 11b20872 2019-11-08 stsp free_colors(&s->colors);
2367 11b20872 2019-11-08 stsp goto done;
2369 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2370 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2371 11b20872 2019-11-08 stsp if (err) {
2372 11b20872 2019-11-08 stsp free_colors(&s->colors);
2373 11b20872 2019-11-08 stsp goto done;
2377 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2378 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2379 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2380 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2381 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2383 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2384 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2386 74467cc8 2022-06-15 stsp goto done;
2388 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2389 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2390 0ae84acc 2022-06-15 tracey if (err)
2391 0ae84acc 2022-06-15 tracey goto done;
2392 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2393 b672a97a 2020-01-27 stsp !s->log_branches);
2395 1a76625f 2018-10-22 stsp goto done;
2396 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2397 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2399 c5b78334 2020-01-12 stsp goto done;
2401 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2402 1a76625f 2018-10-22 stsp if (errcode) {
2403 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2404 1a76625f 2018-10-22 stsp goto done;
2406 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2407 7c1452c1 2020-03-26 stsp if (errcode) {
2408 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2409 7c1452c1 2020-03-26 stsp goto done;
2412 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2413 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2414 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2415 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2416 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2417 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2418 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2419 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2420 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2421 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2422 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2423 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2424 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2427 1a76625f 2018-10-22 stsp close_log_view(view);
2428 ba4f502b 2018-08-04 stsp return err;
2431 e5a0f69f 2018-08-18 stsp static const struct got_error *
2432 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2434 f2f6d207 2020-11-24 stsp const struct got_error *err;
2435 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2437 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2438 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2439 2b380cc8 2018-10-24 stsp &s->thread_args);
2440 2b380cc8 2018-10-24 stsp if (errcode)
2441 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2442 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2443 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2445 f2f6d207 2020-11-24 stsp return err;
2449 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2452 e5a0f69f 2018-08-18 stsp static const struct got_error *
2453 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2455 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2456 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2457 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2458 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2459 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2460 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
2462 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2463 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2464 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2465 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2466 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2467 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2468 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2469 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2470 fb280deb 2021-08-30 stsp select_commit(s);
2472 528dedf3 2021-08-30 stsp return NULL;
2475 528dedf3 2021-08-30 stsp switch (ch) {
2477 1e37a5c2 2019-05-12 jcs s->quit = 1;
2480 1e37a5c2 2019-05-12 jcs case KEY_UP:
2483 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2484 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2486 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2487 1e37a5c2 2019-05-12 jcs s->selected--;
2489 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2490 912a3f79 2021-08-30 j select_commit(s);
2493 912a3f79 2021-08-30 j case KEY_HOME:
2494 912a3f79 2021-08-30 j s->selected = 0;
2495 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2496 2b779855 2020-12-05 naddy select_commit(s);
2498 83cc4199 2022-06-13 stsp case CTRL('u'):
2500 83cc4199 2022-06-13 stsp nscroll /= 2;
2501 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2502 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2503 a4292ac5 2019-05-12 jcs case CTRL('b'):
2504 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2506 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2507 83cc4199 2022-06-13 stsp s->selected = MAX(0, s->selected - nscroll - 1);
2509 83cc4199 2022-06-13 stsp log_scroll_up(s, nscroll);
2510 2b779855 2020-12-05 naddy select_commit(s);
2513 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2516 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2517 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2519 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2520 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2521 1e37a5c2 2019-05-12 jcs s->selected++;
2523 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2527 912a3f79 2021-08-30 j select_commit(s);
2530 912a3f79 2021-08-30 j case KEY_END: {
2531 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2532 912a3f79 2021-08-30 j * traverse them all. */
2533 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2534 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2535 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2538 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2539 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2540 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2541 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2543 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2544 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2546 f3bc9f1d 2021-09-05 naddy if (n > 0)
2547 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2548 2b779855 2020-12-05 naddy select_commit(s);
2551 80b7e8da 2022-06-11 stsp case CTRL('d'):
2553 83cc4199 2022-06-13 stsp nscroll /= 2;
2554 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2555 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2556 83cc4199 2022-06-13 stsp case CTRL('f'): {
2557 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2558 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2559 1e37a5c2 2019-05-12 jcs if (first == NULL)
2561 83cc4199 2022-06-13 stsp err = log_scroll_down(view, nscroll);
2564 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2565 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2566 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2567 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2568 83cc4199 2022-06-13 stsp s->selected += MIN(s->last_displayed_entry->idx -
2569 83cc4199 2022-06-13 stsp s->selected_entry->idx, nscroll + 1);
2571 2b779855 2020-12-05 naddy select_commit(s);
2574 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2575 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2576 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2577 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2578 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2579 2b779855 2020-12-05 naddy select_commit(s);
2580 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2581 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2582 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2583 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2584 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2587 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2590 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2592 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2593 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2594 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2595 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2596 78756c87 2020-11-24 stsp view, s->repo);
2599 e78dc838 2020-12-04 stsp view->focussed = 0;
2600 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2601 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2602 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2604 1e37a5c2 2019-05-12 jcs return err;
2605 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2606 e78dc838 2020-12-04 stsp view->focus_child = 1;
2608 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2611 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2613 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2614 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2615 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2616 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2620 e78dc838 2020-12-04 stsp view->focussed = 0;
2621 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2622 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2623 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2625 1e37a5c2 2019-05-12 jcs return err;
2626 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2627 e78dc838 2020-12-04 stsp view->focus_child = 1;
2629 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2631 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2632 21355643 2020-12-06 stsp case CTRL('l'):
2634 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2635 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2637 21355643 2020-12-06 stsp err = stop_log_thread(s);
2639 74cfe85e 2020-10-20 stsp return err;
2640 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2641 21355643 2020-12-06 stsp char *parent_path;
2642 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2644 21355643 2020-12-06 stsp return err;
2645 21355643 2020-12-06 stsp free(s->in_repo_path);
2646 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2647 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2648 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2649 21355643 2020-12-06 stsp struct got_object_id *start_id;
2650 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2651 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2652 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2654 21355643 2020-12-06 stsp return err;
2655 21355643 2020-12-06 stsp free(s->start_id);
2656 21355643 2020-12-06 stsp s->start_id = start_id;
2657 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2658 21355643 2020-12-06 stsp } else /* 'B' */
2659 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2661 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
2662 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
2663 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2665 21355643 2020-12-06 stsp return err;
2666 51a10b52 2020-12-26 stsp tog_free_refs();
2667 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
2669 ca51c541 2020-12-07 stsp return err;
2670 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2671 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2673 d01904d4 2019-06-25 stsp return err;
2674 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2675 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2677 b672a97a 2020-01-27 stsp return err;
2678 21355643 2020-12-06 stsp free_commits(&s->commits);
2679 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2680 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2681 21355643 2020-12-06 stsp s->selected_entry = NULL;
2682 21355643 2020-12-06 stsp s->selected = 0;
2683 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2684 21355643 2020-12-06 stsp s->quit = 0;
2685 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2688 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2689 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2690 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2691 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2692 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2693 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2694 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2695 6458efa5 2020-11-24 stsp if (err) {
2696 6458efa5 2020-11-24 stsp view_close(ref_view);
2697 6458efa5 2020-11-24 stsp return err;
2699 e78dc838 2020-12-04 stsp view->focussed = 0;
2700 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2701 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2702 6458efa5 2020-11-24 stsp err = view_close_child(view);
2704 6458efa5 2020-11-24 stsp return err;
2705 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2706 e78dc838 2020-12-04 stsp view->focus_child = 1;
2708 6458efa5 2020-11-24 stsp *new_view = ref_view;
2714 80ddbec8 2018-04-29 stsp return err;
2717 4ed7e80c 2018-05-20 stsp static const struct got_error *
2718 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2720 c2db6724 2019-01-04 stsp const struct got_error *error;
2722 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2723 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2724 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2726 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2727 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2729 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2730 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2732 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2733 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2735 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2736 c2db6724 2019-01-04 stsp if (error != NULL)
2737 c2db6724 2019-01-04 stsp return error;
2739 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2740 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2742 c2db6724 2019-01-04 stsp return NULL;
2745 a915003a 2019-02-05 stsp static void
2746 a915003a 2019-02-05 stsp init_curses(void)
2749 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
2750 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
2751 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
2753 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
2754 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
2755 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
2756 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
2757 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
2759 a915003a 2019-02-05 stsp initscr();
2761 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2764 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2765 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2766 a915003a 2019-02-05 stsp curs_set(0);
2767 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2768 6d17833f 2019-11-08 stsp start_color();
2769 6d17833f 2019-11-08 stsp use_default_colors();
2773 c2db6724 2019-01-04 stsp static const struct got_error *
2774 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2775 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2777 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2779 f135c941 2020-02-20 stsp if (argc == 0) {
2780 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2781 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2782 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2783 f135c941 2020-02-20 stsp return NULL;
2786 f135c941 2020-02-20 stsp if (worktree) {
2787 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2790 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2792 f135c941 2020-02-20 stsp return err;
2793 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2794 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2795 bfd61697 2020-11-14 stsp p) == -1) {
2796 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2797 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2801 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2803 f135c941 2020-02-20 stsp return err;
2806 f135c941 2020-02-20 stsp static const struct got_error *
2807 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2809 80ddbec8 2018-04-29 stsp const struct got_error *error;
2810 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2811 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2812 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2813 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2814 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2815 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2816 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2817 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2818 04cc582a 2018-08-01 stsp struct tog_view *view;
2819 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
2821 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2822 80ddbec8 2018-04-29 stsp switch (ch) {
2824 b672a97a 2020-01-27 stsp log_branches = 1;
2827 80ddbec8 2018-04-29 stsp start_commit = optarg;
2830 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2831 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2832 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2836 17020d27 2019-03-07 stsp usage_log();
2837 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2841 80ddbec8 2018-04-29 stsp argc -= optind;
2842 80ddbec8 2018-04-29 stsp argv += optind;
2844 f135c941 2020-02-20 stsp if (argc > 1)
2845 f135c941 2020-02-20 stsp usage_log();
2847 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
2848 0ae84acc 2022-06-15 tracey if (error != NULL)
2849 0ae84acc 2022-06-15 tracey goto done;
2851 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2852 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2853 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2854 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2855 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2856 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2857 c156c7a4 2020-12-18 stsp goto done;
2858 a1fbf39a 2019-08-11 stsp if (worktree)
2859 6962eb72 2020-02-20 stsp repo_path =
2860 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2862 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2863 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2864 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2865 c156c7a4 2020-12-18 stsp goto done;
2869 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2870 80ddbec8 2018-04-29 stsp if (error != NULL)
2871 ecb28ae0 2018-07-16 stsp goto done;
2873 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2874 f135c941 2020-02-20 stsp repo, worktree);
2875 f135c941 2020-02-20 stsp if (error)
2876 f135c941 2020-02-20 stsp goto done;
2878 f135c941 2020-02-20 stsp init_curses();
2880 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2881 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2882 c02c541e 2019-03-29 stsp if (error)
2883 c02c541e 2019-03-29 stsp goto done;
2885 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2886 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2887 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
2888 87670572 2020-12-26 naddy if (error)
2889 87670572 2020-12-26 naddy goto done;
2892 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2893 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2894 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2895 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2896 d8f38dc4 2020-12-05 stsp if (error)
2897 d8f38dc4 2020-12-05 stsp goto done;
2898 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2900 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2901 d8f38dc4 2020-12-05 stsp if (error == NULL)
2902 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2903 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2904 d8f38dc4 2020-12-05 stsp goto done;
2905 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2906 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2907 d8f38dc4 2020-12-05 stsp if (error)
2908 d8f38dc4 2020-12-05 stsp goto done;
2911 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2912 04cc582a 2018-08-01 stsp if (view == NULL) {
2913 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2914 04cc582a 2018-08-01 stsp goto done;
2916 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2917 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2918 ba4f502b 2018-08-04 stsp if (error)
2919 ba4f502b 2018-08-04 stsp goto done;
2920 2fc00ff4 2019-08-31 stsp if (worktree) {
2921 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2922 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2923 2fc00ff4 2019-08-31 stsp worktree = NULL;
2925 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2927 f135c941 2020-02-20 stsp free(in_repo_path);
2928 ecb28ae0 2018-07-16 stsp free(repo_path);
2929 ecb28ae0 2018-07-16 stsp free(cwd);
2930 899d86c2 2018-05-10 stsp free(start_id);
2931 d8f38dc4 2020-12-05 stsp free(label);
2933 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2934 1d0f4054 2021-06-17 stsp if (repo) {
2935 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2936 1d0f4054 2021-06-17 stsp if (error == NULL)
2937 1d0f4054 2021-06-17 stsp error = close_err;
2939 ec142235 2019-03-07 stsp if (worktree)
2940 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2941 0ae84acc 2022-06-15 tracey if (pack_fds) {
2942 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
2943 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
2944 0ae84acc 2022-06-15 tracey if (error == NULL)
2945 0ae84acc 2022-06-15 tracey error = pack_err;
2947 51a10b52 2020-12-26 stsp tog_free_refs();
2948 80ddbec8 2018-04-29 stsp return error;
2951 4ed7e80c 2018-05-20 stsp __dead static void
2952 9f7d7167 2018-04-29 stsp usage_diff(void)
2955 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2956 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2960 6d17833f 2019-11-08 stsp static int
2961 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2962 41605754 2020-11-12 stsp regmatch_t *regmatch)
2964 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2967 f26dddb7 2019-11-08 stsp struct tog_color *
2968 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2970 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2972 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2973 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2974 6d17833f 2019-11-08 stsp return tc;
2977 6d17833f 2019-11-08 stsp return NULL;
2980 4ed7e80c 2018-05-20 stsp static const struct got_error *
2981 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2982 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2984 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2985 41605754 2020-11-12 stsp wchar_t *wline;
2986 41605754 2020-11-12 stsp int width;
2989 41605754 2020-11-12 stsp *wtotal = 0;
2991 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2992 41605754 2020-11-12 stsp if (s == NULL)
2993 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2995 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2996 41605754 2020-11-12 stsp if (err) {
2998 41605754 2020-11-12 stsp return err;
3000 41605754 2020-11-12 stsp waddwstr(window, wline);
3001 41605754 2020-11-12 stsp free(wline);
3003 41605754 2020-11-12 stsp wlimit -= width;
3004 41605754 2020-11-12 stsp *wtotal += width;
3006 41605754 2020-11-12 stsp if (wlimit > 0) {
3007 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
3008 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
3009 41605754 2020-11-12 stsp if (s == NULL) {
3010 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
3012 41605754 2020-11-12 stsp return err;
3014 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
3015 41605754 2020-11-12 stsp if (err) {
3017 41605754 2020-11-12 stsp return err;
3019 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
3020 41605754 2020-11-12 stsp waddwstr(window, wline);
3021 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
3022 41605754 2020-11-12 stsp free(wline);
3024 41605754 2020-11-12 stsp wlimit -= width;
3025 41605754 2020-11-12 stsp *wtotal += width;
3028 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3029 41605754 2020-11-12 stsp err = format_line(&wline, &width,
3030 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
3032 41605754 2020-11-12 stsp return err;
3033 41605754 2020-11-12 stsp waddwstr(window, wline);
3034 41605754 2020-11-12 stsp free(wline);
3035 41605754 2020-11-12 stsp *wtotal += width;
3038 41605754 2020-11-12 stsp return NULL;
3041 41605754 2020-11-12 stsp static const struct got_error *
3042 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3044 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3045 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3046 61e69b96 2018-05-20 stsp const struct got_error *err;
3047 fe621944 2020-11-10 stsp int nprinted = 0;
3048 b304db33 2018-05-20 stsp char *line;
3049 826082fe 2020-12-10 stsp size_t linesize = 0;
3050 826082fe 2020-12-10 stsp ssize_t linelen;
3051 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3052 61e69b96 2018-05-20 stsp wchar_t *wline;
3053 e0b650dd 2018-05-20 stsp int width;
3054 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3055 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3056 fe621944 2020-11-10 stsp off_t line_offset;
3058 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3059 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3060 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3062 f7d12f7e 2018-08-01 stsp werase(view->window);
3064 a3404814 2018-09-02 stsp if (header) {
3065 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3066 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3067 135a2da0 2020-11-11 stsp header) == -1)
3068 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3069 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3070 135a2da0 2020-11-11 stsp free(line);
3072 a3404814 2018-09-02 stsp return err;
3074 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3075 a3404814 2018-09-02 stsp wstandout(view->window);
3076 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3077 e54cc94a 2020-11-11 stsp free(wline);
3078 e54cc94a 2020-11-11 stsp wline = NULL;
3079 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3080 a3404814 2018-09-02 stsp wstandend(view->window);
3081 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3082 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3084 a3404814 2018-09-02 stsp if (max_lines <= 1)
3085 a3404814 2018-09-02 stsp return NULL;
3086 a3404814 2018-09-02 stsp max_lines--;
3089 89f1a395 2020-12-01 naddy s->eof = 0;
3090 826082fe 2020-12-10 stsp line = NULL;
3091 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3092 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3093 826082fe 2020-12-10 stsp if (linelen == -1) {
3094 826082fe 2020-12-10 stsp if (feof(s->f)) {
3095 826082fe 2020-12-10 stsp s->eof = 1;
3098 826082fe 2020-12-10 stsp free(line);
3099 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3102 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3104 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3105 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3106 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3107 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3108 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3109 41605754 2020-11-12 stsp view->window, regmatch);
3110 41605754 2020-11-12 stsp if (err) {
3111 41605754 2020-11-12 stsp free(line);
3112 41605754 2020-11-12 stsp return err;
3115 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3116 41605754 2020-11-12 stsp if (err) {
3117 41605754 2020-11-12 stsp free(line);
3118 41605754 2020-11-12 stsp return err;
3120 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3121 41605754 2020-11-12 stsp free(wline);
3122 41605754 2020-11-12 stsp wline = NULL;
3125 6d17833f 2019-11-08 stsp wattr_off(view->window,
3126 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3127 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3128 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3129 fe621944 2020-11-10 stsp nprinted++;
3131 826082fe 2020-12-10 stsp free(line);
3132 fe621944 2020-11-10 stsp if (nprinted >= 1)
3133 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3134 89f1a395 2020-12-01 naddy (nprinted - 1);
3136 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3138 1a57306a 2018-09-02 stsp view_vborder(view);
3140 89f1a395 2020-12-01 naddy if (s->eof) {
3141 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3142 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3143 c3e9aa98 2019-05-13 jcs nprinted++;
3146 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3148 c3e9aa98 2019-05-13 jcs return err;
3151 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3152 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3153 e54cc94a 2020-11-11 stsp free(wline);
3154 e54cc94a 2020-11-11 stsp wline = NULL;
3155 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3158 26ed57b2 2018-05-19 stsp return NULL;
3161 abd2672a 2018-12-23 stsp static char *
3162 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3164 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3165 09867e48 2019-08-13 stsp char *p, *s;
3167 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3168 09867e48 2019-08-13 stsp if (tm == NULL)
3169 09867e48 2019-08-13 stsp return NULL;
3170 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3171 09867e48 2019-08-13 stsp if (s == NULL)
3172 09867e48 2019-08-13 stsp return NULL;
3173 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3175 abd2672a 2018-12-23 stsp *p = '\0';
3179 4ed7e80c 2018-05-20 stsp static const struct got_error *
3180 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3181 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3183 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3184 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3185 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3186 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3188 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3189 0208f208 2020-05-05 stsp if (qid != NULL) {
3190 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3191 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3192 d7b5a0e8 2022-04-20 stsp &qid->id);
3194 0208f208 2020-05-05 stsp return err;
3196 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3197 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3198 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3199 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3200 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3202 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3206 0208f208 2020-05-05 stsp if (tree_id1) {
3207 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3209 0208f208 2020-05-05 stsp goto done;
3212 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3213 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3215 0208f208 2020-05-05 stsp goto done;
3217 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3218 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3220 0208f208 2020-05-05 stsp if (tree1)
3221 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3222 0208f208 2020-05-05 stsp if (tree2)
3223 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3224 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3225 0208f208 2020-05-05 stsp return err;
3228 0208f208 2020-05-05 stsp static const struct got_error *
3229 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3233 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3234 fe621944 2020-11-10 stsp if (p == NULL)
3235 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3236 fe621944 2020-11-10 stsp *line_offsets = p;
3237 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3238 fe621944 2020-11-10 stsp (*nlines)++;
3239 fe621944 2020-11-10 stsp return NULL;
3242 fe621944 2020-11-10 stsp static const struct got_error *
3243 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3244 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3245 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3247 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3248 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3249 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3250 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3251 45d799e2 2018-12-23 stsp time_t committer_time;
3252 45d799e2 2018-12-23 stsp const char *author, *committer;
3253 8b473291 2019-02-21 stsp char *refs_str = NULL;
3254 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3255 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3256 fe621944 2020-11-10 stsp off_t outoff = 0;
3259 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3261 8b473291 2019-02-21 stsp if (refs) {
3262 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3264 8b473291 2019-02-21 stsp return err;
3267 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3269 abd2672a 2018-12-23 stsp return err;
3271 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3272 15a94983 2018-12-23 stsp if (err) {
3273 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3274 15a94983 2018-12-23 stsp goto done;
3277 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3279 fe621944 2020-11-10 stsp goto done;
3281 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3282 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3283 fe621944 2020-11-10 stsp if (n < 0) {
3284 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3285 abd2672a 2018-12-23 stsp goto done;
3287 fe621944 2020-11-10 stsp outoff += n;
3288 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3290 fe621944 2020-11-10 stsp goto done;
3292 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3293 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3294 fe621944 2020-11-10 stsp if (n < 0) {
3295 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3296 abd2672a 2018-12-23 stsp goto done;
3298 fe621944 2020-11-10 stsp outoff += n;
3299 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3301 fe621944 2020-11-10 stsp goto done;
3303 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3304 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3305 fe621944 2020-11-10 stsp if (datestr) {
3306 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3307 fe621944 2020-11-10 stsp if (n < 0) {
3308 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3309 fe621944 2020-11-10 stsp goto done;
3311 fe621944 2020-11-10 stsp outoff += n;
3312 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3314 fe621944 2020-11-10 stsp goto done;
3316 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3317 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3318 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3319 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3320 fe621944 2020-11-10 stsp if (n < 0) {
3321 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3322 fe621944 2020-11-10 stsp goto done;
3324 fe621944 2020-11-10 stsp outoff += n;
3325 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3327 fe621944 2020-11-10 stsp goto done;
3329 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3330 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3331 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3332 9f98ca05 2021-09-24 stsp int pn = 1;
3333 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3334 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3335 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3337 9f98ca05 2021-09-24 stsp goto done;
3338 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3339 9f98ca05 2021-09-24 stsp if (n < 0) {
3340 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3341 9f98ca05 2021-09-24 stsp goto done;
3343 9f98ca05 2021-09-24 stsp outoff += n;
3344 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3346 9f98ca05 2021-09-24 stsp goto done;
3347 9f98ca05 2021-09-24 stsp free(id_str);
3348 9f98ca05 2021-09-24 stsp id_str = NULL;
3352 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3354 5943eee2 2019-08-13 stsp goto done;
3355 fe621944 2020-11-10 stsp s = logmsg;
3356 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3357 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3358 fe621944 2020-11-10 stsp if (n < 0) {
3359 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3360 fe621944 2020-11-10 stsp goto done;
3362 fe621944 2020-11-10 stsp outoff += n;
3363 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3365 fe621944 2020-11-10 stsp goto done;
3368 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3370 0208f208 2020-05-05 stsp goto done;
3371 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3372 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3373 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3374 fe621944 2020-11-10 stsp if (n < 0) {
3375 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3376 fe621944 2020-11-10 stsp goto done;
3378 fe621944 2020-11-10 stsp outoff += n;
3379 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3381 fe621944 2020-11-10 stsp goto done;
3382 0208f208 2020-05-05 stsp free((char *)pe->path);
3383 0208f208 2020-05-05 stsp free(pe->data);
3386 0208f208 2020-05-05 stsp fputc('\n', outfile);
3388 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3390 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3391 abd2672a 2018-12-23 stsp free(id_str);
3392 5943eee2 2019-08-13 stsp free(logmsg);
3393 8b473291 2019-02-21 stsp free(refs_str);
3394 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3395 7510f233 2020-08-09 stsp if (err) {
3396 ae6a6978 2020-08-09 stsp free(*line_offsets);
3397 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3398 ae6a6978 2020-08-09 stsp *nlines = 0;
3400 fe621944 2020-11-10 stsp return err;
3403 abd2672a 2018-12-23 stsp static const struct got_error *
3404 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3406 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3407 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3408 15a94983 2018-12-23 stsp int obj_type;
3410 fe621944 2020-11-10 stsp free(s->line_offsets);
3411 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3412 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3413 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3414 fe621944 2020-11-10 stsp s->nlines = 0;
3416 511a516b 2018-05-19 stsp f = got_opentemp();
3417 48ae06ee 2018-10-18 stsp if (f == NULL) {
3418 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3419 48ae06ee 2018-10-18 stsp goto done;
3421 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3422 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3423 fb43ecf1 2019-02-11 stsp goto done;
3427 15a94983 2018-12-23 stsp if (s->id1)
3428 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3430 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3432 15a94983 2018-12-23 stsp goto done;
3434 15a94983 2018-12-23 stsp switch (obj_type) {
3435 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3436 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3437 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3438 b72706c3 2022-06-01 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3439 b72706c3 2022-06-01 stsp s->repo, s->f);
3441 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3442 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3443 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3444 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3446 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3447 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3448 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3449 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3450 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3452 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3454 3ffacbe1 2020-02-02 tracey goto done;
3455 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3456 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3457 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3458 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3459 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3460 f44b1f58 2020-02-02 tracey if (err)
3461 f44b1f58 2020-02-02 tracey goto done;
3462 f44b1f58 2020-02-02 tracey } else {
3463 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3464 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3465 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3466 fe621944 2020-11-10 stsp err = write_commit_info(
3467 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3468 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3469 f44b1f58 2020-02-02 tracey if (err)
3470 f44b1f58 2020-02-02 tracey goto done;
3475 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3477 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3478 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3479 b72706c3 2022-06-01 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3483 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3486 f44b1f58 2020-02-02 tracey if (err)
3487 f44b1f58 2020-02-02 tracey goto done;
3489 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3490 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3491 48ae06ee 2018-10-18 stsp return err;
3494 f5215bb9 2019-02-22 stsp static void
3495 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3497 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3498 f5215bb9 2019-02-22 stsp update_panels();
3499 f5215bb9 2019-02-22 stsp doupdate();
3502 f44b1f58 2020-02-02 tracey static const struct got_error *
3503 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3505 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3507 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3508 f44b1f58 2020-02-02 tracey return NULL;
3511 f44b1f58 2020-02-02 tracey static const struct got_error *
3512 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3514 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3515 f44b1f58 2020-02-02 tracey int lineno;
3516 826082fe 2020-12-10 stsp char *line = NULL;
3517 826082fe 2020-12-10 stsp size_t linesize = 0;
3518 826082fe 2020-12-10 stsp ssize_t linelen;
3520 f44b1f58 2020-02-02 tracey if (!view->searching) {
3521 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3522 f44b1f58 2020-02-02 tracey return NULL;
3525 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3526 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3527 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3529 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3531 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
3533 f44b1f58 2020-02-02 tracey while (1) {
3534 f44b1f58 2020-02-02 tracey off_t offset;
3536 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3537 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3538 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3542 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3543 f44b1f58 2020-02-02 tracey lineno = 1;
3545 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3548 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3549 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3550 f44b1f58 2020-02-02 tracey free(line);
3551 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3553 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3554 826082fe 2020-12-10 stsp if (linelen != -1 &&
3555 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3556 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3557 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3560 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3561 f44b1f58 2020-02-02 tracey lineno++;
3563 f44b1f58 2020-02-02 tracey lineno--;
3565 826082fe 2020-12-10 stsp free(line);
3567 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3568 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3569 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3572 f44b1f58 2020-02-02 tracey return NULL;
3575 48ae06ee 2018-10-18 stsp static const struct got_error *
3576 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
3578 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
3579 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
3581 b72706c3 2022-06-01 stsp free(s->id1);
3582 b72706c3 2022-06-01 stsp s->id1 = NULL;
3583 b72706c3 2022-06-01 stsp free(s->id2);
3584 b72706c3 2022-06-01 stsp s->id2 = NULL;
3585 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
3586 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3587 b72706c3 2022-06-01 stsp s->f = NULL;
3588 b72706c3 2022-06-01 stsp if (s->f1 && fclose(s->f1) == EOF)
3589 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3590 b72706c3 2022-06-01 stsp s->f1 = NULL;
3591 b72706c3 2022-06-01 stsp if (s->f2 && fclose(s->f2) == EOF)
3592 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3593 b72706c3 2022-06-01 stsp s->f2 = NULL;
3594 b72706c3 2022-06-01 stsp free_colors(&s->colors);
3595 b72706c3 2022-06-01 stsp free(s->line_offsets);
3596 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
3597 b72706c3 2022-06-01 stsp s->nlines = 0;
3598 b72706c3 2022-06-01 stsp return err;
3601 b72706c3 2022-06-01 stsp static const struct got_error *
3602 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3603 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3604 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3605 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3607 48ae06ee 2018-10-18 stsp const struct got_error *err;
3608 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3610 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
3612 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3613 15a94983 2018-12-23 stsp int type1, type2;
3614 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3616 15a94983 2018-12-23 stsp return err;
3617 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3619 15a94983 2018-12-23 stsp return err;
3621 15a94983 2018-12-23 stsp if (type1 != type2)
3622 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3624 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3625 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3626 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3627 f44b1f58 2020-02-02 tracey s->repo = repo;
3628 f44b1f58 2020-02-02 tracey s->id1 = id1;
3629 f44b1f58 2020-02-02 tracey s->id2 = id2;
3630 3dbaef42 2020-11-24 stsp s->label1 = label1;
3631 3dbaef42 2020-11-24 stsp s->label2 = label2;
3633 15a94983 2018-12-23 stsp if (id1) {
3634 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3635 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3636 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3637 b72706c3 2022-06-01 stsp s->f1 = got_opentemp();
3638 b72706c3 2022-06-01 stsp if (s->f1 == NULL) {
3639 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3640 b72706c3 2022-06-01 stsp goto done;
3643 5465d566 2020-02-01 tracey s->id1 = NULL;
3645 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3646 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3647 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
3648 b72706c3 2022-06-01 stsp goto done;
3651 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
3652 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
3653 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3654 b72706c3 2022-06-01 stsp goto done;
3657 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3658 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3659 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3660 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3661 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3662 5465d566 2020-02-01 tracey s->log_view = log_view;
3663 5465d566 2020-02-01 tracey s->repo = repo;
3665 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3666 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3667 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3668 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3669 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3671 b72706c3 2022-06-01 stsp goto done;
3672 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3673 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3674 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3676 b72706c3 2022-06-01 stsp goto done;
3677 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3678 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3679 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3681 b72706c3 2022-06-01 stsp goto done;
3683 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3684 9f98ca05 2021-09-24 stsp "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3685 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3686 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3688 b72706c3 2022-06-01 stsp goto done;
3690 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3691 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3692 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3694 b72706c3 2022-06-01 stsp goto done;
3696 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3697 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3698 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3700 b72706c3 2022-06-01 stsp goto done;
3703 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3704 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3705 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3707 5465d566 2020-02-01 tracey err = create_diff(s);
3709 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3710 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3711 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3712 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3713 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3716 b72706c3 2022-06-01 stsp close_diff_view(view);
3717 e5a0f69f 2018-08-18 stsp return err;
3720 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3721 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3723 a3404814 2018-09-02 stsp const struct got_error *err;
3724 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3725 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3726 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3728 a3404814 2018-09-02 stsp if (s->id1) {
3729 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3731 a3404814 2018-09-02 stsp return err;
3732 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3734 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3736 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3738 a3404814 2018-09-02 stsp return err;
3739 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3741 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3742 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3743 a3404814 2018-09-02 stsp free(id_str1);
3744 a3404814 2018-09-02 stsp free(id_str2);
3745 a3404814 2018-09-02 stsp return err;
3747 a3404814 2018-09-02 stsp free(id_str1);
3748 a3404814 2018-09-02 stsp free(id_str2);
3750 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3751 267bb3b8 2021-08-01 stsp free(header);
3752 267bb3b8 2021-08-01 stsp return err;
3755 15a087fe 2019-02-21 stsp static const struct got_error *
3756 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3757 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3759 d7a04538 2019-02-21 stsp const struct got_error *err;
3760 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3761 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3762 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3764 15a087fe 2019-02-21 stsp free(s->id2);
3765 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3766 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3767 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3769 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3771 d7a04538 2019-02-21 stsp return err;
3772 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3773 15a087fe 2019-02-21 stsp free(s->id1);
3774 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3775 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3776 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3777 15a087fe 2019-02-21 stsp return NULL;
3780 0cf4efb1 2018-09-29 stsp static const struct got_error *
3781 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3783 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3784 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3785 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3786 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3787 826082fe 2020-12-10 stsp char *line = NULL;
3788 826082fe 2020-12-10 stsp size_t linesize = 0;
3789 826082fe 2020-12-10 stsp ssize_t linelen;
3790 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
3792 e5a0f69f 2018-08-18 stsp switch (ch) {
3795 3dbaef42 2020-11-24 stsp if (ch == 'a')
3796 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3797 3dbaef42 2020-11-24 stsp if (ch == 'w')
3798 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3799 64453f7e 2020-11-21 stsp wclear(view->window);
3800 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3801 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3802 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3803 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3804 64453f7e 2020-11-21 stsp err = create_diff(s);
3807 912a3f79 2021-08-30 j case KEY_HOME:
3808 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3811 912a3f79 2021-08-30 j case KEY_END:
3815 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3819 1e37a5c2 2019-05-12 jcs case KEY_UP:
3820 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3821 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3822 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3824 83cc4199 2022-06-13 stsp case CTRL('u'):
3826 83cc4199 2022-06-13 stsp nscroll /= 2;
3827 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3828 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3829 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3830 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3833 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
3834 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3837 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3838 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3839 1e37a5c2 2019-05-12 jcs if (!s->eof)
3840 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3842 83cc4199 2022-06-13 stsp case CTRL('d'):
3844 83cc4199 2022-06-13 stsp nscroll /= 2;
3845 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3846 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3847 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3849 00ba99a7 2019-05-12 jcs if (s->eof)
3852 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
3853 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3854 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3855 826082fe 2020-12-10 stsp if (linelen == -1) {
3856 826082fe 2020-12-10 stsp if (feof(s->f)) {
3857 826082fe 2020-12-10 stsp s->eof = 1;
3859 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3863 826082fe 2020-12-10 stsp free(line);
3866 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3867 1e37a5c2 2019-05-12 jcs s->diff_context--;
3868 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3869 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3870 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3871 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3872 27829c9e 2020-11-21 stsp s->nlines) {
3873 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3874 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3879 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3880 1e37a5c2 2019-05-12 jcs s->diff_context++;
3881 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3882 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3883 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3888 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3890 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3891 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3893 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3897 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3900 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3904 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3905 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3906 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3908 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3909 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3913 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3915 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3916 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3918 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3922 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3925 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3929 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3930 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3931 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3933 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3934 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3940 bcbd79e2 2018-08-19 stsp return err;
3943 4ed7e80c 2018-05-20 stsp static const struct got_error *
3944 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3946 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3947 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3948 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3949 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3950 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3951 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3952 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3953 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3954 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3955 3dbaef42 2020-11-24 stsp const char *errstr;
3956 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3957 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3959 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3960 26ed57b2 2018-05-19 stsp switch (ch) {
3962 64453f7e 2020-11-21 stsp force_text_diff = 1;
3965 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3967 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3968 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
3969 5a20d08d 2022-02-09 op errstr, errstr);
3971 09b5bff8 2020-02-23 naddy case 'r':
3972 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3973 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3974 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3976 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3979 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3982 17020d27 2019-03-07 stsp usage_diff();
3983 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3987 26ed57b2 2018-05-19 stsp argc -= optind;
3988 26ed57b2 2018-05-19 stsp argv += optind;
3990 26ed57b2 2018-05-19 stsp if (argc == 0) {
3991 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3992 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3993 15a94983 2018-12-23 stsp id_str1 = argv[0];
3994 15a94983 2018-12-23 stsp id_str2 = argv[1];
3996 26ed57b2 2018-05-19 stsp usage_diff();
3998 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3999 0ae84acc 2022-06-15 tracey if (error)
4000 0ae84acc 2022-06-15 tracey goto done;
4002 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4003 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4004 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4005 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4006 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4007 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4008 c156c7a4 2020-12-18 stsp goto done;
4009 a273ac94 2020-02-23 naddy if (worktree)
4010 a273ac94 2020-02-23 naddy repo_path =
4011 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4013 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4014 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4015 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4016 c156c7a4 2020-12-18 stsp goto done;
4020 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4021 eb6600df 2019-01-04 stsp if (error)
4022 eb6600df 2019-01-04 stsp goto done;
4024 a273ac94 2020-02-23 naddy init_curses();
4026 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4027 51a10b52 2020-12-26 stsp if (error)
4028 51a10b52 2020-12-26 stsp goto done;
4030 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4031 26ed57b2 2018-05-19 stsp if (error)
4032 26ed57b2 2018-05-19 stsp goto done;
4034 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4035 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4036 26ed57b2 2018-05-19 stsp if (error)
4037 26ed57b2 2018-05-19 stsp goto done;
4039 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4040 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4041 26ed57b2 2018-05-19 stsp if (error)
4042 26ed57b2 2018-05-19 stsp goto done;
4044 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4045 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4046 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4047 ea5e7bb5 2018-08-01 stsp goto done;
4049 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4050 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4051 5dc9f4bc 2018-08-04 stsp if (error)
4052 5dc9f4bc 2018-08-04 stsp goto done;
4053 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4055 3dbaef42 2020-11-24 stsp free(label1);
4056 3dbaef42 2020-11-24 stsp free(label2);
4057 c02c541e 2019-03-29 stsp free(repo_path);
4058 a273ac94 2020-02-23 naddy free(cwd);
4059 1d0f4054 2021-06-17 stsp if (repo) {
4060 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4061 1d0f4054 2021-06-17 stsp if (error == NULL)
4062 1d0f4054 2021-06-17 stsp error = close_err;
4064 a273ac94 2020-02-23 naddy if (worktree)
4065 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4066 0ae84acc 2022-06-15 tracey if (pack_fds) {
4067 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4068 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4069 0ae84acc 2022-06-15 tracey if (error == NULL)
4070 0ae84acc 2022-06-15 tracey error = pack_err;
4072 51a10b52 2020-12-26 stsp tog_free_refs();
4073 26ed57b2 2018-05-19 stsp return error;
4076 4ed7e80c 2018-05-20 stsp __dead static void
4077 9f7d7167 2018-04-29 stsp usage_blame(void)
4080 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4081 9f7d7167 2018-04-29 stsp getprogname());
4085 84451b3e 2018-07-10 stsp struct tog_blame_line {
4086 84451b3e 2018-07-10 stsp int annotated;
4087 84451b3e 2018-07-10 stsp struct got_object_id *id;
4090 4ed7e80c 2018-05-20 stsp static const struct got_error *
4091 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4093 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4094 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4095 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4096 84451b3e 2018-07-10 stsp const struct got_error *err;
4097 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4098 826082fe 2020-12-10 stsp char *line = NULL;
4099 826082fe 2020-12-10 stsp size_t linesize = 0;
4100 826082fe 2020-12-10 stsp ssize_t linelen;
4101 84451b3e 2018-07-10 stsp wchar_t *wline;
4102 27a741e5 2019-09-11 stsp int width;
4103 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4104 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4105 ab089a2a 2018-07-12 stsp char *id_str;
4106 11b20872 2019-11-08 stsp struct tog_color *tc;
4108 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4110 ab089a2a 2018-07-12 stsp return err;
4112 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4113 f7d12f7e 2018-08-01 stsp werase(view->window);
4115 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4116 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4117 ab089a2a 2018-07-12 stsp free(id_str);
4118 ab089a2a 2018-07-12 stsp return err;
4121 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4122 ab089a2a 2018-07-12 stsp free(line);
4123 2550e4c3 2018-07-13 stsp line = NULL;
4125 1cae65b4 2019-09-22 stsp return err;
4126 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4127 a3404814 2018-09-02 stsp wstandout(view->window);
4128 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4130 11b20872 2019-11-08 stsp wattr_on(view->window,
4131 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4132 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4134 11b20872 2019-11-08 stsp wattr_off(view->window,
4135 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4136 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4137 a3404814 2018-09-02 stsp wstandend(view->window);
4138 2550e4c3 2018-07-13 stsp free(wline);
4139 2550e4c3 2018-07-13 stsp wline = NULL;
4140 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4141 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4143 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4144 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4145 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4146 ab089a2a 2018-07-12 stsp free(id_str);
4147 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4149 ab089a2a 2018-07-12 stsp free(id_str);
4150 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4151 3f60a8ef 2018-07-10 stsp free(line);
4152 2550e4c3 2018-07-13 stsp line = NULL;
4154 3f60a8ef 2018-07-10 stsp return err;
4155 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4156 2550e4c3 2018-07-13 stsp free(wline);
4157 2550e4c3 2018-07-13 stsp wline = NULL;
4158 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4159 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4161 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4162 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4163 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4164 826082fe 2020-12-10 stsp if (linelen == -1) {
4165 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4166 826082fe 2020-12-10 stsp s->eof = 1;
4169 84451b3e 2018-07-10 stsp free(line);
4170 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4172 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4175 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4176 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4178 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4179 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4180 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4181 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4182 27a741e5 2019-09-11 stsp !(view->focussed &&
4183 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4184 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4185 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4186 8d0fe45a 2019-08-12 stsp char *id_str;
4187 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4188 8d0fe45a 2019-08-12 stsp if (err) {
4189 8d0fe45a 2019-08-12 stsp free(line);
4190 8d0fe45a 2019-08-12 stsp return err;
4192 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4194 11b20872 2019-11-08 stsp wattr_on(view->window,
4195 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4196 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4198 11b20872 2019-11-08 stsp wattr_off(view->window,
4199 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4200 8d0fe45a 2019-08-12 stsp free(id_str);
4201 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4203 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4204 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4207 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4208 ee41ec32 2018-07-10 stsp prev_id = NULL;
4211 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4212 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4213 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4215 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4216 41605754 2020-11-12 stsp width = 9;
4217 41605754 2020-11-12 stsp wline = wcsdup(L"");
4218 41605754 2020-11-12 stsp if (wline == NULL) {
4219 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4220 41605754 2020-11-12 stsp free(line);
4221 41605754 2020-11-12 stsp return err;
4223 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4224 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4225 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4226 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4227 41605754 2020-11-12 stsp view->window, regmatch);
4228 41605754 2020-11-12 stsp if (err) {
4229 41605754 2020-11-12 stsp free(line);
4230 41605754 2020-11-12 stsp return err;
4232 41605754 2020-11-12 stsp width += 9;
4234 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4235 41605754 2020-11-12 stsp view->ncols - 9, 9);
4236 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4237 41605754 2020-11-12 stsp free(wline);
4238 41605754 2020-11-12 stsp wline = NULL;
4239 41605754 2020-11-12 stsp width += 9;
4242 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4243 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4244 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4245 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4247 826082fe 2020-12-10 stsp free(line);
4248 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4250 1a57306a 2018-09-02 stsp view_vborder(view);
4252 84451b3e 2018-07-10 stsp return NULL;
4255 84451b3e 2018-07-10 stsp static const struct got_error *
4256 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4257 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4259 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4260 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4261 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4262 1a76625f 2018-10-22 stsp int errcode;
4264 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4265 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4266 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4268 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4269 1a76625f 2018-10-22 stsp if (errcode)
4270 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4272 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4273 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4274 d68a0a7d 2018-07-10 stsp goto done;
4277 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4278 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4280 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4281 d68a0a7d 2018-07-10 stsp if (line->annotated)
4282 d68a0a7d 2018-07-10 stsp goto done;
4284 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4285 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4286 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4287 84451b3e 2018-07-10 stsp goto done;
4289 84451b3e 2018-07-10 stsp line->annotated = 1;
4291 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4292 1a76625f 2018-10-22 stsp if (errcode)
4293 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4294 84451b3e 2018-07-10 stsp return err;
4297 84451b3e 2018-07-10 stsp static void *
4298 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4300 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4301 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4302 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4303 1a76625f 2018-10-22 stsp int errcode;
4305 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4307 61266923 2020-01-14 stsp return (void *)err;
4309 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4310 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4311 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4312 fc06ba56 2019-08-22 stsp err = NULL;
4314 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4315 1a76625f 2018-10-22 stsp if (errcode)
4316 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4317 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4319 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4320 1d0f4054 2021-06-17 stsp if (err == NULL)
4321 1d0f4054 2021-06-17 stsp err = close_err;
4322 c9beca56 2018-07-22 stsp ta->repo = NULL;
4323 c9beca56 2018-07-22 stsp *ta->complete = 1;
4325 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4326 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4327 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4329 18430de3 2018-07-10 stsp return (void *)err;
4332 245d91c1 2018-07-12 stsp static struct got_object_id *
4333 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4334 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4336 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4338 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4339 8d0fe45a 2019-08-12 stsp return NULL;
4341 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4342 245d91c1 2018-07-12 stsp if (!line->annotated)
4343 245d91c1 2018-07-12 stsp return NULL;
4345 245d91c1 2018-07-12 stsp return line->id;
4348 b880a918 2018-07-10 stsp static const struct got_error *
4349 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4351 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4354 245d91c1 2018-07-12 stsp if (blame->thread) {
4355 1a76625f 2018-10-22 stsp int errcode;
4356 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4357 1a76625f 2018-10-22 stsp if (errcode)
4358 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4359 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4360 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4361 1a76625f 2018-10-22 stsp if (errcode)
4362 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4363 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4364 1a76625f 2018-10-22 stsp if (errcode)
4365 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4366 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4367 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4368 245d91c1 2018-07-12 stsp err = NULL;
4369 245d91c1 2018-07-12 stsp blame->thread = NULL;
4371 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4372 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4373 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4374 1d0f4054 2021-06-17 stsp if (err == NULL)
4375 1d0f4054 2021-06-17 stsp err = close_err;
4376 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4378 245d91c1 2018-07-12 stsp if (blame->f) {
4379 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4380 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4381 245d91c1 2018-07-12 stsp blame->f = NULL;
4383 57670559 2018-12-23 stsp if (blame->lines) {
4384 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4385 57670559 2018-12-23 stsp free(blame->lines[i].id);
4386 57670559 2018-12-23 stsp free(blame->lines);
4387 57670559 2018-12-23 stsp blame->lines = NULL;
4389 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4390 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4391 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
4392 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4393 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
4394 0ae84acc 2022-06-15 tracey if (err == NULL)
4395 0ae84acc 2022-06-15 tracey err = pack_err;
4396 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
4398 245d91c1 2018-07-12 stsp return err;
4401 245d91c1 2018-07-12 stsp static const struct got_error *
4402 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4404 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4405 fc06ba56 2019-08-22 stsp int *done = arg;
4406 fc06ba56 2019-08-22 stsp int errcode;
4408 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4409 fc06ba56 2019-08-22 stsp if (errcode)
4410 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4411 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4413 fc06ba56 2019-08-22 stsp if (*done)
4414 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4416 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4417 fc06ba56 2019-08-22 stsp if (errcode)
4418 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4419 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4421 fc06ba56 2019-08-22 stsp return err;
4424 fc06ba56 2019-08-22 stsp static const struct got_error *
4425 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4427 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4428 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4429 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4430 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4431 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4432 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4433 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4434 15a94983 2018-12-23 stsp int obj_type;
4435 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4437 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
4438 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
4440 15a94983 2018-12-23 stsp return err;
4442 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4444 a44927cc 2022-04-07 stsp goto done;
4446 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4448 84451b3e 2018-07-10 stsp goto done;
4450 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4451 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4452 84451b3e 2018-07-10 stsp goto done;
4455 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4457 a70480e0 2018-06-23 stsp goto done;
4458 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4459 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4460 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4461 84451b3e 2018-07-10 stsp goto done;
4463 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4464 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4466 1fddf795 2021-01-20 stsp goto done;
4467 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4468 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4469 84451b3e 2018-07-10 stsp goto done;
4472 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4473 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4474 b02560ec 2019-08-19 stsp blame->nlines--;
4476 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4477 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4478 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4479 84451b3e 2018-07-10 stsp goto done;
4482 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
4484 bd24772e 2018-07-11 stsp goto done;
4485 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4486 0ae84acc 2022-06-15 tracey pack_fds);
4487 0ae84acc 2022-06-15 tracey if (err)
4488 0ae84acc 2022-06-15 tracey goto done;
4490 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
4491 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4492 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4493 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4494 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4495 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4496 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4497 245d91c1 2018-07-12 stsp goto done;
4499 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4501 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4502 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4503 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4504 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4505 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4506 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4507 a5388363 2020-12-01 naddy s->blame_complete = 0;
4509 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4510 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4511 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4512 f5a09613 2020-12-13 naddy s->selected_line = 1;
4514 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4517 a44927cc 2022-04-07 stsp if (commit)
4518 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
4520 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4521 27d434c2 2018-09-15 stsp free(obj_id);
4523 245d91c1 2018-07-12 stsp stop_blame(blame);
4524 245d91c1 2018-07-12 stsp return err;
4527 245d91c1 2018-07-12 stsp static const struct got_error *
4528 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4529 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4531 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4532 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4534 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4536 c4843652 2019-08-12 stsp s->path = strdup(path);
4537 c4843652 2019-08-12 stsp if (s->path == NULL)
4538 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4540 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4541 c4843652 2019-08-12 stsp if (err) {
4542 c4843652 2019-08-12 stsp free(s->path);
4543 7cbe629d 2018-08-04 stsp return err;
4546 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4547 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4548 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4549 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4550 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4551 fb2756b9 2018-08-04 stsp s->repo = repo;
4552 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4553 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4555 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4556 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4558 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4560 11b20872 2019-11-08 stsp return err;
4563 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4564 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4565 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4566 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4567 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4569 a5388363 2020-12-01 naddy return run_blame(view);
4572 e5a0f69f 2018-08-18 stsp static const struct got_error *
4573 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4575 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4576 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4578 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4579 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4581 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4582 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4583 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4584 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4585 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4588 e5a0f69f 2018-08-18 stsp free(s->path);
4589 11b20872 2019-11-08 stsp free_colors(&s->colors);
4590 e5a0f69f 2018-08-18 stsp return err;
4593 7cbe629d 2018-08-04 stsp static const struct got_error *
4594 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4596 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4598 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4599 6c4c42e0 2019-06-24 stsp return NULL;
4602 6c4c42e0 2019-06-24 stsp static const struct got_error *
4603 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4605 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4606 6c4c42e0 2019-06-24 stsp int lineno;
4607 826082fe 2020-12-10 stsp char *line = NULL;
4608 826082fe 2020-12-10 stsp size_t linesize = 0;
4609 826082fe 2020-12-10 stsp ssize_t linelen;
4611 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4612 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4613 6c4c42e0 2019-06-24 stsp return NULL;
4616 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4617 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4618 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4620 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4622 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
4624 6c4c42e0 2019-06-24 stsp while (1) {
4625 6c4c42e0 2019-06-24 stsp off_t offset;
4627 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4628 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4629 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4633 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4634 6c4c42e0 2019-06-24 stsp lineno = 1;
4636 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4639 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4640 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4641 6c4c42e0 2019-06-24 stsp free(line);
4642 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4644 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4645 826082fe 2020-12-10 stsp if (linelen != -1 &&
4646 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4647 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4648 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4651 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4656 826082fe 2020-12-10 stsp free(line);
4658 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4659 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4660 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4663 6c4c42e0 2019-06-24 stsp return NULL;
4666 6c4c42e0 2019-06-24 stsp static const struct got_error *
4667 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4669 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4670 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4671 2b380cc8 2018-10-24 stsp int errcode;
4673 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4674 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4675 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4676 2b380cc8 2018-10-24 stsp if (errcode)
4677 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4679 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4682 51fe7530 2019-08-19 stsp if (s->blame_complete)
4683 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4685 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4687 669b5ffa 2018-10-07 stsp view_vborder(view);
4688 e5a0f69f 2018-08-18 stsp return err;
4691 e5a0f69f 2018-08-18 stsp static const struct got_error *
4692 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4694 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4695 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4696 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4697 83cc4199 2022-06-13 stsp int begin_x = 0, nscroll = view->nlines - 2;
4699 e5a0f69f 2018-08-18 stsp switch (ch) {
4701 1e37a5c2 2019-05-12 jcs s->done = 1;
4703 4deef56f 2021-09-02 naddy case 'g':
4704 4deef56f 2021-09-02 naddy case KEY_HOME:
4705 4deef56f 2021-09-02 naddy s->selected_line = 1;
4706 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4708 4deef56f 2021-09-02 naddy case 'G':
4709 4deef56f 2021-09-02 naddy case KEY_END:
4710 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4711 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4712 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4714 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4715 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4716 4deef56f 2021-09-02 naddy (view->nlines - 3);
4720 1e37a5c2 2019-05-12 jcs case KEY_UP:
4721 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4722 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4723 1e37a5c2 2019-05-12 jcs s->selected_line--;
4724 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4725 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4726 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4728 83cc4199 2022-06-13 stsp case CTRL('u'):
4730 83cc4199 2022-06-13 stsp nscroll /= 2;
4731 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4732 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4733 ea025d1d 2020-02-22 naddy case CTRL('b'):
4734 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4735 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
4738 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
4739 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
4741 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4744 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4745 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4746 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4747 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4748 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4749 1e37a5c2 2019-05-12 jcs s->selected_line++;
4750 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4751 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4752 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4755 1e37a5c2 2019-05-12 jcs case 'p': {
4756 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4757 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4758 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4759 1e37a5c2 2019-05-12 jcs if (id == NULL)
4761 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4762 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
4763 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4764 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4765 1e37a5c2 2019-05-12 jcs int obj_type;
4766 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4767 1e37a5c2 2019-05-12 jcs s->repo, id);
4770 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4771 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4772 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4773 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4776 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4777 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
4778 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
4781 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4782 a44927cc 2022-04-07 stsp pcommit, s->path);
4783 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
4784 e5a0f69f 2018-08-18 stsp if (err) {
4785 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4786 1e37a5c2 2019-05-12 jcs err = NULL;
4787 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4790 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4792 1e37a5c2 2019-05-12 jcs free(blob_id);
4793 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4794 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4795 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4798 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4799 d7b5a0e8 2022-04-20 stsp &pid->id);
4800 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4802 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4803 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
4805 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4810 1e37a5c2 2019-05-12 jcs s->done = 1;
4811 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4812 1e37a5c2 2019-05-12 jcs s->done = 0;
4813 1e37a5c2 2019-05-12 jcs if (thread_err)
4815 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4816 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4817 a5388363 2020-12-01 naddy err = run_blame(view);
4822 1e37a5c2 2019-05-12 jcs case 'B': {
4823 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4824 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4825 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
4827 1e37a5c2 2019-05-12 jcs s->done = 1;
4828 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4829 1e37a5c2 2019-05-12 jcs s->done = 0;
4830 1e37a5c2 2019-05-12 jcs if (thread_err)
4832 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4833 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4834 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4835 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4836 a5388363 2020-12-01 naddy err = run_blame(view);
4841 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4842 1e37a5c2 2019-05-12 jcs case '\r': {
4843 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4844 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4845 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4846 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4847 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4848 1e37a5c2 2019-05-12 jcs if (id == NULL)
4850 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4853 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4854 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4855 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4856 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4857 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4858 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4859 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4860 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4863 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4864 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4865 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4867 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4870 e78dc838 2020-12-04 stsp view->focussed = 0;
4871 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4872 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4873 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4876 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4877 e78dc838 2020-12-04 stsp view->focus_child = 1;
4879 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4884 83cc4199 2022-06-13 stsp case CTRL('d'):
4886 83cc4199 2022-06-13 stsp nscroll /= 2;
4887 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4888 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4889 ea025d1d 2020-02-22 naddy case CTRL('f'):
4891 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4892 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4893 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4896 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4897 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4898 83cc4199 2022-06-13 stsp s->selected_line +=
4899 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
4900 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
4902 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
4903 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
4905 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4906 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
4908 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4909 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4910 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4911 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4917 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4920 a70480e0 2018-06-23 stsp static const struct got_error *
4921 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4923 a70480e0 2018-06-23 stsp const struct got_error *error;
4924 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4925 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4926 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4927 0587e10c 2020-07-23 stsp char *link_target = NULL;
4928 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4929 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4930 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4932 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4933 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4935 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4936 a70480e0 2018-06-23 stsp switch (ch) {
4938 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4941 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4942 69069811 2018-08-02 stsp if (repo_path == NULL)
4943 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4947 17020d27 2019-03-07 stsp usage_blame();
4948 a70480e0 2018-06-23 stsp /* NOTREACHED */
4952 a70480e0 2018-06-23 stsp argc -= optind;
4953 a70480e0 2018-06-23 stsp argv += optind;
4955 f135c941 2020-02-20 stsp if (argc != 1)
4956 a70480e0 2018-06-23 stsp usage_blame();
4958 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4959 0ae84acc 2022-06-15 tracey if (error != NULL)
4960 0ae84acc 2022-06-15 tracey goto done;
4962 69069811 2018-08-02 stsp if (repo_path == NULL) {
4963 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4964 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4965 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4966 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4967 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4968 c156c7a4 2020-12-18 stsp goto done;
4969 f135c941 2020-02-20 stsp if (worktree)
4970 eb41ed75 2019-02-05 stsp repo_path =
4971 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4973 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4974 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4975 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4976 c156c7a4 2020-12-18 stsp goto done;
4980 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4981 c02c541e 2019-03-29 stsp if (error != NULL)
4982 8e94dd5b 2019-01-04 stsp goto done;
4984 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4985 f135c941 2020-02-20 stsp worktree);
4986 c02c541e 2019-03-29 stsp if (error)
4987 92205607 2019-01-04 stsp goto done;
4989 f135c941 2020-02-20 stsp init_curses();
4991 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4992 51a10b52 2020-12-26 stsp if (error)
4993 51a10b52 2020-12-26 stsp goto done;
4995 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4996 eb41ed75 2019-02-05 stsp if (error)
4997 69069811 2018-08-02 stsp goto done;
4999 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5000 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5001 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5002 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5003 a70480e0 2018-06-23 stsp if (error != NULL)
5004 66b4983c 2018-06-23 stsp goto done;
5005 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5006 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5008 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5009 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5011 a19e88aa 2018-06-23 stsp if (error != NULL)
5012 8b473291 2019-02-21 stsp goto done;
5014 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5015 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5016 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5017 e1cd8fed 2018-08-01 stsp goto done;
5020 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5021 a44927cc 2022-04-07 stsp if (error)
5022 a44927cc 2022-04-07 stsp goto done;
5024 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5025 a44927cc 2022-04-07 stsp commit, repo);
5026 7cbe629d 2018-08-04 stsp if (error)
5027 7cbe629d 2018-08-04 stsp goto done;
5029 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5030 78756c87 2020-11-24 stsp commit_id, repo);
5031 0587e10c 2020-07-23 stsp if (error)
5032 0587e10c 2020-07-23 stsp goto done;
5033 12314ad4 2019-08-31 stsp if (worktree) {
5034 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5035 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5036 12314ad4 2019-08-31 stsp worktree = NULL;
5038 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5040 69069811 2018-08-02 stsp free(repo_path);
5041 f135c941 2020-02-20 stsp free(in_repo_path);
5042 0587e10c 2020-07-23 stsp free(link_target);
5043 69069811 2018-08-02 stsp free(cwd);
5044 a70480e0 2018-06-23 stsp free(commit_id);
5045 a44927cc 2022-04-07 stsp if (commit)
5046 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5047 eb41ed75 2019-02-05 stsp if (worktree)
5048 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5049 1d0f4054 2021-06-17 stsp if (repo) {
5050 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5051 1d0f4054 2021-06-17 stsp if (error == NULL)
5052 1d0f4054 2021-06-17 stsp error = close_err;
5054 0ae84acc 2022-06-15 tracey if (pack_fds) {
5055 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5056 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5057 0ae84acc 2022-06-15 tracey if (error == NULL)
5058 0ae84acc 2022-06-15 tracey error = pack_err;
5060 51a10b52 2020-12-26 stsp tog_free_refs();
5061 a70480e0 2018-06-23 stsp return error;
5064 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5065 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5067 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5068 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5069 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5070 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5071 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5072 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5073 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5075 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5077 f7d12f7e 2018-08-01 stsp werase(view->window);
5079 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5080 ffd1d5e5 2018-06-23 stsp return NULL;
5082 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5084 ffd1d5e5 2018-06-23 stsp return err;
5085 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5086 a3404814 2018-09-02 stsp wstandout(view->window);
5087 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5089 11b20872 2019-11-08 stsp wattr_on(view->window,
5090 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5091 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5093 11b20872 2019-11-08 stsp wattr_off(view->window,
5094 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5095 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5096 a3404814 2018-09-02 stsp wstandend(view->window);
5097 2550e4c3 2018-07-13 stsp free(wline);
5098 2550e4c3 2018-07-13 stsp wline = NULL;
5099 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5100 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5101 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5102 ffd1d5e5 2018-06-23 stsp return NULL;
5103 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
5105 ce52c690 2018-06-23 stsp return err;
5106 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5107 2550e4c3 2018-07-13 stsp free(wline);
5108 2550e4c3 2018-07-13 stsp wline = NULL;
5109 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5110 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5111 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5112 ffd1d5e5 2018-06-23 stsp return NULL;
5113 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5114 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5115 a1eca9bb 2018-06-23 stsp return NULL;
5117 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5118 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5119 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5120 0cf4efb1 2018-09-29 stsp if (view->focussed)
5121 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5122 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5124 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5125 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5126 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5127 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5128 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5129 ffd1d5e5 2018-06-23 stsp return NULL;
5133 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5136 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5137 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5138 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5139 848d6979 2019-08-12 stsp const char *modestr = "";
5140 56e0773d 2019-11-28 stsp mode_t mode;
5142 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5143 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5145 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5146 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5147 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5149 638f9024 2019-05-13 stsp return got_error_from_errno(
5150 230a42bd 2019-05-11 jcs "got_object_id_str");
5152 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5153 63c5ca5d 2019-08-24 stsp modestr = "$";
5154 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5157 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5158 d86d3b18 2020-12-01 naddy te, s->repo);
5159 0d6c6ee3 2020-05-20 stsp if (err) {
5160 0d6c6ee3 2020-05-20 stsp free(id_str);
5161 0d6c6ee3 2020-05-20 stsp return err;
5163 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5164 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5165 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5167 848d6979 2019-08-12 stsp modestr = "@";
5169 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5170 848d6979 2019-08-12 stsp modestr = "/";
5171 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5172 848d6979 2019-08-12 stsp modestr = "*";
5173 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5174 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5175 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5176 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5177 1d13200f 2018-07-12 stsp free(id_str);
5178 0d6c6ee3 2020-05-20 stsp free(link_target);
5179 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5181 1d13200f 2018-07-12 stsp free(id_str);
5182 0d6c6ee3 2020-05-20 stsp free(link_target);
5183 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5184 ffd1d5e5 2018-06-23 stsp if (err) {
5185 ffd1d5e5 2018-06-23 stsp free(line);
5188 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5189 0cf4efb1 2018-09-29 stsp if (view->focussed)
5190 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5191 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5193 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5195 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5196 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5197 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5199 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5200 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5201 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5202 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5203 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5204 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5205 ffd1d5e5 2018-06-23 stsp free(line);
5206 2550e4c3 2018-07-13 stsp free(wline);
5207 2550e4c3 2018-07-13 stsp wline = NULL;
5209 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5210 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5211 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5215 ffd1d5e5 2018-06-23 stsp return err;
5218 ffd1d5e5 2018-06-23 stsp static void
5219 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5221 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5222 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5223 fa86c4bf 2020-11-29 stsp int i = 0;
5225 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5228 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5229 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5230 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5231 fa86c4bf 2020-11-29 stsp if (!isroot)
5232 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5235 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5236 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5240 fa86c4bf 2020-11-29 stsp static void
5241 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5243 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5244 ffd1d5e5 2018-06-23 stsp int n = 0;
5246 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5247 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5248 694d3271 2020-12-01 naddy s->first_displayed_entry);
5250 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5252 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5253 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5254 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5255 768394f3 2019-01-24 stsp if (last) {
5256 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5257 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5262 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5263 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5264 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5266 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5267 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5268 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5270 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5271 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5272 56e0773d 2019-11-28 stsp + 1 /* slash */;
5274 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5276 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5277 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5278 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5280 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5281 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5282 d9765a41 2018-06-23 stsp while (pt) {
5283 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5284 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5285 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5286 cb2ebc8a 2018-06-23 stsp goto done;
5288 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5289 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5290 cb2ebc8a 2018-06-23 stsp goto done;
5292 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5295 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5296 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5297 ce52c690 2018-06-23 stsp goto done;
5301 ce52c690 2018-06-23 stsp if (err) {
5302 ce52c690 2018-06-23 stsp free(*path);
5303 ce52c690 2018-06-23 stsp *path = NULL;
5305 ce52c690 2018-06-23 stsp return err;
5308 ce52c690 2018-06-23 stsp static const struct got_error *
5309 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5310 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5311 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5313 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5314 ce52c690 2018-06-23 stsp char *path;
5315 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5317 a0de39f3 2019-08-09 stsp *new_view = NULL;
5319 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5321 ce52c690 2018-06-23 stsp return err;
5323 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5324 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5325 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5326 83ce39e3 2019-08-12 stsp goto done;
5329 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5330 e5a0f69f 2018-08-18 stsp if (err) {
5331 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5332 fc06ba56 2019-08-22 stsp err = NULL;
5333 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5335 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5337 83ce39e3 2019-08-12 stsp free(path);
5338 69efd4c4 2018-07-18 stsp return err;
5341 69efd4c4 2018-07-18 stsp static const struct got_error *
5342 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5343 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5345 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5346 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5347 69efd4c4 2018-07-18 stsp char *path;
5349 a0de39f3 2019-08-09 stsp *new_view = NULL;
5351 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5352 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5353 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5355 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5357 69efd4c4 2018-07-18 stsp return err;
5359 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5362 e5a0f69f 2018-08-18 stsp view_close(log_view);
5364 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5365 cb2ebc8a 2018-06-23 stsp free(path);
5366 cb2ebc8a 2018-06-23 stsp return err;
5369 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5370 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5371 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5373 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5374 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5375 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5376 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5378 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5379 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5381 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5382 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5383 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5385 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5387 bc573f3b 2021-07-10 stsp goto done;
5390 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5391 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5392 bc573f3b 2021-07-10 stsp * closed on demand.
5394 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5395 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5397 bc573f3b 2021-07-10 stsp goto done;
5398 bc573f3b 2021-07-10 stsp s->tree = s->root;
5400 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5401 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5402 ffd1d5e5 2018-06-23 stsp goto done;
5404 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5405 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5406 ffd1d5e5 2018-06-23 stsp goto done;
5409 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5410 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5411 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5412 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5413 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5414 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5415 9cd7cbd1 2020-12-07 stsp goto done;
5418 fb2756b9 2018-08-04 stsp s->repo = repo;
5420 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5421 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5422 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5423 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5425 c0b01bdb 2019-11-08 stsp goto done;
5426 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5427 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5429 c0b01bdb 2019-11-08 stsp goto done;
5430 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5431 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5432 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5434 c0b01bdb 2019-11-08 stsp goto done;
5436 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5437 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5438 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5440 11b20872 2019-11-08 stsp goto done;
5442 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5443 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5445 c0b01bdb 2019-11-08 stsp goto done;
5448 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5449 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5450 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5451 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5452 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5454 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5455 bc573f3b 2021-07-10 stsp if (commit)
5456 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5458 bc573f3b 2021-07-10 stsp close_tree_view(view);
5459 ad80ab7b 2018-08-04 stsp return err;
5462 e5a0f69f 2018-08-18 stsp static const struct got_error *
5463 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5465 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5467 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5468 fb2756b9 2018-08-04 stsp free(s->tree_label);
5469 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5470 6484ec90 2018-09-29 stsp free(s->commit_id);
5471 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5472 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5473 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5474 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5475 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5476 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5477 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5478 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5479 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5480 ad80ab7b 2018-08-04 stsp free(parent);
5483 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5484 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5485 bc573f3b 2021-07-10 stsp if (s->root)
5486 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5487 7c32bd05 2019-06-22 stsp return NULL;
5490 7c32bd05 2019-06-22 stsp static const struct got_error *
5491 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5493 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5495 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5496 7c32bd05 2019-06-22 stsp return NULL;
5499 7c32bd05 2019-06-22 stsp static int
5500 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5502 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5504 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5508 7c32bd05 2019-06-22 stsp static const struct got_error *
5509 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5511 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5512 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5514 7c32bd05 2019-06-22 stsp if (!view->searching) {
5515 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5516 7c32bd05 2019-06-22 stsp return NULL;
5519 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5520 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5521 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5522 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5523 56e0773d 2019-11-28 stsp s->selected_entry);
5525 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5527 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5528 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5530 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5531 56e0773d 2019-11-28 stsp s->selected_entry);
5534 487cd7d2 2021-12-17 stsp if (s->selected_entry)
5535 487cd7d2 2021-12-17 stsp te = s->selected_entry;
5536 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
5537 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5539 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5542 7c32bd05 2019-06-22 stsp while (1) {
5543 56e0773d 2019-11-28 stsp if (te == NULL) {
5544 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5545 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5546 ac66afb8 2019-06-24 stsp return NULL;
5548 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5549 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5551 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5554 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5555 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5556 56e0773d 2019-11-28 stsp s->matched_entry = te;
5560 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5561 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5563 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5566 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5567 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5568 7c32bd05 2019-06-22 stsp s->selected = 0;
5571 e5a0f69f 2018-08-18 stsp return NULL;
5574 ad80ab7b 2018-08-04 stsp static const struct got_error *
5575 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5577 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5578 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5579 e5a0f69f 2018-08-18 stsp char *parent_path;
5581 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5583 e5a0f69f 2018-08-18 stsp return err;
5585 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5586 e5a0f69f 2018-08-18 stsp free(parent_path);
5588 669b5ffa 2018-10-07 stsp view_vborder(view);
5589 e5a0f69f 2018-08-18 stsp return err;
5592 e5a0f69f 2018-08-18 stsp static const struct got_error *
5593 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5595 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5596 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5597 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5598 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5599 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
5601 e5a0f69f 2018-08-18 stsp switch (ch) {
5603 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5606 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5608 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5609 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5610 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5611 e78dc838 2020-12-04 stsp view->focussed = 0;
5612 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5613 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5614 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5616 1e37a5c2 2019-05-12 jcs return err;
5617 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5618 e78dc838 2020-12-04 stsp view->focus_child = 1;
5620 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5623 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5624 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5625 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5626 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5627 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5628 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5629 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5630 152c1c93 2020-11-29 stsp if (err) {
5631 152c1c93 2020-11-29 stsp view_close(ref_view);
5632 152c1c93 2020-11-29 stsp return err;
5634 e78dc838 2020-12-04 stsp view->focussed = 0;
5635 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5636 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5637 152c1c93 2020-11-29 stsp err = view_close_child(view);
5639 152c1c93 2020-11-29 stsp return err;
5640 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5641 e78dc838 2020-12-04 stsp view->focus_child = 1;
5643 152c1c93 2020-11-29 stsp *new_view = ref_view;
5645 e4526bf5 2021-09-03 naddy case 'g':
5646 e4526bf5 2021-09-03 naddy case KEY_HOME:
5647 e4526bf5 2021-09-03 naddy s->selected = 0;
5648 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5649 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5650 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5652 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5654 e4526bf5 2021-09-03 naddy case 'G':
5655 e4526bf5 2021-09-03 naddy case KEY_END:
5656 e4526bf5 2021-09-03 naddy s->selected = 0;
5657 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5658 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5659 e4526bf5 2021-09-03 naddy if (te == NULL) {
5660 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5661 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5666 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5667 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5669 e4526bf5 2021-09-03 naddy if (n > 0)
5670 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5673 1e37a5c2 2019-05-12 jcs case KEY_UP:
5674 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5675 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5676 1e37a5c2 2019-05-12 jcs s->selected--;
5679 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5681 83cc4199 2022-06-13 stsp case CTRL('u'):
5683 83cc4199 2022-06-13 stsp nscroll /= 2;
5684 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5685 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5686 ea025d1d 2020-02-22 naddy case CTRL('b'):
5687 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5688 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5689 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5690 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5692 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5693 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5695 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
5698 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5699 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5700 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5701 1e37a5c2 2019-05-12 jcs s->selected++;
5704 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5706 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5708 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5710 83cc4199 2022-06-13 stsp case CTRL('d'):
5712 83cc4199 2022-06-13 stsp nscroll /= 2;
5713 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5714 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5715 ea025d1d 2020-02-22 naddy case CTRL('f'):
5716 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5718 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5719 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5720 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
5721 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
5724 83cc4199 2022-06-13 stsp tree_scroll_down(s, nscroll);
5726 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5728 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5729 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5730 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5731 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5732 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5734 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5735 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5737 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5738 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5739 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5740 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5741 1e37a5c2 2019-05-12 jcs s->selected_entry =
5742 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5743 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5744 1e37a5c2 2019-05-12 jcs free(parent);
5745 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5746 56e0773d 2019-11-28 stsp s->selected_entry))) {
5747 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5748 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5749 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5752 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5753 941e9f74 2019-05-21 stsp if (err) {
5754 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5757 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5758 56e0773d 2019-11-28 stsp s->selected_entry))) {
5759 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5760 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5761 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5763 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5764 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5765 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5768 e78dc838 2020-12-04 stsp view->focussed = 0;
5769 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5770 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5771 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5773 669b5ffa 2018-10-07 stsp return err;
5774 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5775 e78dc838 2020-12-04 stsp view->focus_child = 1;
5777 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5780 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5781 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5782 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5788 ffd1d5e5 2018-06-23 stsp return err;
5791 ffd1d5e5 2018-06-23 stsp __dead static void
5792 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5795 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5796 ffd1d5e5 2018-06-23 stsp getprogname());
5800 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5801 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5803 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5804 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5805 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5806 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5807 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5808 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5809 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5810 4e97c21c 2020-12-06 stsp char *label = NULL;
5811 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5812 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5814 5221c383 2018-08-01 stsp struct tog_view *view;
5815 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5817 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5818 ffd1d5e5 2018-06-23 stsp switch (ch) {
5820 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5823 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5824 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5825 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5829 e99e2d15 2020-11-24 naddy usage_tree();
5830 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5834 ffd1d5e5 2018-06-23 stsp argc -= optind;
5835 ffd1d5e5 2018-06-23 stsp argv += optind;
5837 55cccc34 2020-02-20 stsp if (argc > 1)
5838 e99e2d15 2020-11-24 naddy usage_tree();
5840 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5841 0ae84acc 2022-06-15 tracey if (error != NULL)
5842 0ae84acc 2022-06-15 tracey goto done;
5844 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5845 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5846 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5847 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5848 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5849 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5850 c156c7a4 2020-12-18 stsp goto done;
5851 55cccc34 2020-02-20 stsp if (worktree)
5852 52185f70 2019-02-05 stsp repo_path =
5853 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5855 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5856 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5857 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5858 c156c7a4 2020-12-18 stsp goto done;
5862 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5863 c02c541e 2019-03-29 stsp if (error != NULL)
5864 52185f70 2019-02-05 stsp goto done;
5866 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5867 55cccc34 2020-02-20 stsp repo, worktree);
5868 55cccc34 2020-02-20 stsp if (error)
5869 55cccc34 2020-02-20 stsp goto done;
5871 55cccc34 2020-02-20 stsp init_curses();
5873 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5874 c02c541e 2019-03-29 stsp if (error)
5875 52185f70 2019-02-05 stsp goto done;
5877 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5878 51a10b52 2020-12-26 stsp if (error)
5879 51a10b52 2020-12-26 stsp goto done;
5881 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5882 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5883 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5884 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5885 4e97c21c 2020-12-06 stsp if (error)
5886 4e97c21c 2020-12-06 stsp goto done;
5887 4e97c21c 2020-12-06 stsp head_ref_name = label;
5889 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5890 4e97c21c 2020-12-06 stsp if (error == NULL)
5891 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5892 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5893 4e97c21c 2020-12-06 stsp goto done;
5894 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5895 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5896 4e97c21c 2020-12-06 stsp if (error)
5897 4e97c21c 2020-12-06 stsp goto done;
5900 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5901 a44927cc 2022-04-07 stsp if (error)
5902 a44927cc 2022-04-07 stsp goto done;
5904 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5905 5221c383 2018-08-01 stsp if (view == NULL) {
5906 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5907 5221c383 2018-08-01 stsp goto done;
5909 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5910 ad80ab7b 2018-08-04 stsp if (error)
5911 ad80ab7b 2018-08-04 stsp goto done;
5912 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5913 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
5914 d91faf3b 2020-12-01 naddy in_repo_path);
5915 55cccc34 2020-02-20 stsp if (error)
5916 55cccc34 2020-02-20 stsp goto done;
5919 55cccc34 2020-02-20 stsp if (worktree) {
5920 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5921 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5922 55cccc34 2020-02-20 stsp worktree = NULL;
5924 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5926 52185f70 2019-02-05 stsp free(repo_path);
5927 e4a0e26d 2020-02-20 stsp free(cwd);
5928 ffd1d5e5 2018-06-23 stsp free(commit_id);
5929 4e97c21c 2020-12-06 stsp free(label);
5931 486cd271 2020-12-06 stsp got_ref_close(ref);
5932 1d0f4054 2021-06-17 stsp if (repo) {
5933 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5934 1d0f4054 2021-06-17 stsp if (error == NULL)
5935 1d0f4054 2021-06-17 stsp error = close_err;
5937 0ae84acc 2022-06-15 tracey if (pack_fds) {
5938 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5939 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5940 0ae84acc 2022-06-15 tracey if (error == NULL)
5941 0ae84acc 2022-06-15 tracey error = pack_err;
5943 51a10b52 2020-12-26 stsp tog_free_refs();
5944 ffd1d5e5 2018-06-23 stsp return error;
5947 6458efa5 2020-11-24 stsp static const struct got_error *
5948 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5950 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5951 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5953 6458efa5 2020-11-24 stsp s->nrefs = 0;
5954 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5955 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
5956 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
5957 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
5958 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
5961 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5962 6458efa5 2020-11-24 stsp if (re == NULL)
5963 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5965 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5966 8924d611 2020-12-26 stsp if (re->ref == NULL)
5967 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5968 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5969 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5972 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5973 6458efa5 2020-11-24 stsp return NULL;
5977 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5979 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5981 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5982 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5983 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5984 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5989 6458efa5 2020-11-24 stsp static const struct got_error *
5990 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5992 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5993 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5995 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5996 6458efa5 2020-11-24 stsp s->repo = repo;
5998 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5999 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6001 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6003 6458efa5 2020-11-24 stsp return err;
6005 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6006 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6007 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6008 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6010 6458efa5 2020-11-24 stsp goto done;
6012 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6013 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6014 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6016 6458efa5 2020-11-24 stsp goto done;
6018 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6019 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6020 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6022 cc488aa7 2022-01-23 stsp goto done;
6024 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6025 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6026 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6028 6458efa5 2020-11-24 stsp goto done;
6031 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6032 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6033 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6034 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6035 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6038 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6039 6458efa5 2020-11-24 stsp return err;
6042 6458efa5 2020-11-24 stsp static const struct got_error *
6043 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6045 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6047 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6048 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6050 6458efa5 2020-11-24 stsp return NULL;
6053 6458efa5 2020-11-24 stsp static const struct got_error *
6054 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6055 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6057 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6058 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6059 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6060 6458efa5 2020-11-24 stsp int obj_type;
6062 c42c9805 2020-11-24 stsp *commit_id = NULL;
6064 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6066 6458efa5 2020-11-24 stsp return err;
6068 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6070 6458efa5 2020-11-24 stsp goto done;
6072 6458efa5 2020-11-24 stsp switch (obj_type) {
6073 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6074 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6076 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6077 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6079 6458efa5 2020-11-24 stsp goto done;
6080 c42c9805 2020-11-24 stsp free(obj_id);
6081 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6082 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6084 6458efa5 2020-11-24 stsp goto done;
6085 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6086 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6087 c42c9805 2020-11-24 stsp goto done;
6089 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6090 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6091 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6092 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6093 c42c9805 2020-11-24 stsp goto done;
6097 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6103 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6104 c42c9805 2020-11-24 stsp if (err) {
6105 c42c9805 2020-11-24 stsp free(*commit_id);
6106 c42c9805 2020-11-24 stsp *commit_id = NULL;
6108 c42c9805 2020-11-24 stsp return err;
6111 c42c9805 2020-11-24 stsp static const struct got_error *
6112 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6113 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6115 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6116 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6117 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6119 c42c9805 2020-11-24 stsp *new_view = NULL;
6121 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6122 c42c9805 2020-11-24 stsp if (err) {
6123 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6124 c42c9805 2020-11-24 stsp return err;
6126 c42c9805 2020-11-24 stsp return NULL;
6129 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6130 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6131 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6132 6458efa5 2020-11-24 stsp goto done;
6135 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6136 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6139 6458efa5 2020-11-24 stsp view_close(log_view);
6141 6458efa5 2020-11-24 stsp *new_view = log_view;
6142 c42c9805 2020-11-24 stsp free(commit_id);
6143 6458efa5 2020-11-24 stsp return err;
6146 ce5b7c56 2019-07-09 stsp static void
6147 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6149 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6150 34ba6917 2020-11-30 stsp int i = 0;
6152 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6155 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6156 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6157 34ba6917 2020-11-30 stsp if (re == NULL)
6159 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6160 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6164 34ba6917 2020-11-30 stsp static void
6165 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6167 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6168 6458efa5 2020-11-24 stsp int n = 0;
6170 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6171 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6173 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6175 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6176 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6177 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6178 6458efa5 2020-11-24 stsp if (last) {
6179 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6180 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6185 6458efa5 2020-11-24 stsp static const struct got_error *
6186 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6188 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6190 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6191 6458efa5 2020-11-24 stsp return NULL;
6194 6458efa5 2020-11-24 stsp static int
6195 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6197 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6199 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6203 6458efa5 2020-11-24 stsp static const struct got_error *
6204 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6206 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6207 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6209 6458efa5 2020-11-24 stsp if (!view->searching) {
6210 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6211 6458efa5 2020-11-24 stsp return NULL;
6214 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6215 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6216 6458efa5 2020-11-24 stsp if (s->selected_entry)
6217 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6219 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6220 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6222 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6223 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6225 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6226 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6229 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6230 487cd7d2 2021-12-17 stsp re = s->selected_entry;
6231 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6232 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6234 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6237 6458efa5 2020-11-24 stsp while (1) {
6238 6458efa5 2020-11-24 stsp if (re == NULL) {
6239 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6240 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6241 6458efa5 2020-11-24 stsp return NULL;
6243 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6244 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6246 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6249 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6250 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6251 6458efa5 2020-11-24 stsp s->matched_entry = re;
6255 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6256 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6258 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6261 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6262 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6263 6458efa5 2020-11-24 stsp s->selected = 0;
6266 6458efa5 2020-11-24 stsp return NULL;
6269 6458efa5 2020-11-24 stsp static const struct got_error *
6270 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6272 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6273 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6274 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6275 6458efa5 2020-11-24 stsp char *line = NULL;
6276 6458efa5 2020-11-24 stsp wchar_t *wline;
6277 6458efa5 2020-11-24 stsp struct tog_color *tc;
6278 6458efa5 2020-11-24 stsp int width, n;
6279 6458efa5 2020-11-24 stsp int limit = view->nlines;
6281 6458efa5 2020-11-24 stsp werase(view->window);
6283 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6285 6458efa5 2020-11-24 stsp if (limit == 0)
6286 6458efa5 2020-11-24 stsp return NULL;
6288 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6290 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6291 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6292 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6294 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6295 6458efa5 2020-11-24 stsp if (err) {
6296 6458efa5 2020-11-24 stsp free(line);
6297 6458efa5 2020-11-24 stsp return err;
6299 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6300 6458efa5 2020-11-24 stsp wstandout(view->window);
6301 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6302 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6303 6458efa5 2020-11-24 stsp wstandend(view->window);
6304 6458efa5 2020-11-24 stsp free(wline);
6305 6458efa5 2020-11-24 stsp wline = NULL;
6306 6458efa5 2020-11-24 stsp free(line);
6307 6458efa5 2020-11-24 stsp line = NULL;
6308 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6309 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6310 6458efa5 2020-11-24 stsp if (--limit <= 0)
6311 6458efa5 2020-11-24 stsp return NULL;
6314 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6315 6458efa5 2020-11-24 stsp char *line = NULL;
6317 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6318 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6319 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6320 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6321 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6322 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6323 6458efa5 2020-11-24 stsp struct got_object_id *id;
6324 6458efa5 2020-11-24 stsp char *id_str;
6325 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6327 6458efa5 2020-11-24 stsp return err;
6328 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6329 6458efa5 2020-11-24 stsp if (err) {
6331 6458efa5 2020-11-24 stsp return err;
6333 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6334 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6335 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6337 6458efa5 2020-11-24 stsp free(id_str);
6338 6458efa5 2020-11-24 stsp return err;
6341 6458efa5 2020-11-24 stsp free(id_str);
6343 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6344 6458efa5 2020-11-24 stsp if (line == NULL)
6345 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6348 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6349 6458efa5 2020-11-24 stsp if (err) {
6350 6458efa5 2020-11-24 stsp free(line);
6351 6458efa5 2020-11-24 stsp return err;
6353 6458efa5 2020-11-24 stsp if (n == s->selected) {
6354 6458efa5 2020-11-24 stsp if (view->focussed)
6355 6458efa5 2020-11-24 stsp wstandout(view->window);
6356 6458efa5 2020-11-24 stsp s->selected_entry = re;
6358 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6360 6458efa5 2020-11-24 stsp wattr_on(view->window,
6361 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6362 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6364 6458efa5 2020-11-24 stsp wattr_off(view->window,
6365 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6366 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6367 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6368 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6369 6458efa5 2020-11-24 stsp wstandend(view->window);
6370 6458efa5 2020-11-24 stsp free(line);
6371 6458efa5 2020-11-24 stsp free(wline);
6372 6458efa5 2020-11-24 stsp wline = NULL;
6374 6458efa5 2020-11-24 stsp s->ndisplayed++;
6375 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6378 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6381 6458efa5 2020-11-24 stsp view_vborder(view);
6382 6458efa5 2020-11-24 stsp return err;
6385 6458efa5 2020-11-24 stsp static const struct got_error *
6386 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6387 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6389 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6390 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6391 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6393 c42c9805 2020-11-24 stsp *new_view = NULL;
6395 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6396 c42c9805 2020-11-24 stsp if (err) {
6397 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6398 c42c9805 2020-11-24 stsp return err;
6400 c42c9805 2020-11-24 stsp return NULL;
6404 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6405 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6406 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6407 c42c9805 2020-11-24 stsp goto done;
6410 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6411 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6413 c42c9805 2020-11-24 stsp goto done;
6415 c42c9805 2020-11-24 stsp *new_view = tree_view;
6417 c42c9805 2020-11-24 stsp free(commit_id);
6418 c42c9805 2020-11-24 stsp return err;
6420 c42c9805 2020-11-24 stsp static const struct got_error *
6421 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6423 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6424 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6425 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6426 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6427 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
6429 6458efa5 2020-11-24 stsp switch (ch) {
6431 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6434 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
6435 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6436 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
6437 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
6440 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
6441 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
6442 50617b77 2021-11-20 stsp &tog_refs, s->repo);
6445 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
6446 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
6448 6458efa5 2020-11-24 stsp case KEY_ENTER:
6449 6458efa5 2020-11-24 stsp case '\r':
6450 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6452 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6453 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6454 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6456 e78dc838 2020-12-04 stsp view->focussed = 0;
6457 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6458 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6459 6458efa5 2020-11-24 stsp err = view_close_child(view);
6461 6458efa5 2020-11-24 stsp return err;
6462 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6463 e78dc838 2020-12-04 stsp view->focus_child = 1;
6465 6458efa5 2020-11-24 stsp *new_view = log_view;
6468 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6470 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6471 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6472 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6474 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6476 e78dc838 2020-12-04 stsp view->focussed = 0;
6477 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6478 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6479 c42c9805 2020-11-24 stsp err = view_close_child(view);
6481 c42c9805 2020-11-24 stsp return err;
6482 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6483 e78dc838 2020-12-04 stsp view->focus_child = 1;
6485 c42c9805 2020-11-24 stsp *new_view = tree_view;
6487 e4526bf5 2021-09-03 naddy case 'g':
6488 e4526bf5 2021-09-03 naddy case KEY_HOME:
6489 e4526bf5 2021-09-03 naddy s->selected = 0;
6490 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6492 e4526bf5 2021-09-03 naddy case 'G':
6493 e4526bf5 2021-09-03 naddy case KEY_END:
6494 e4526bf5 2021-09-03 naddy s->selected = 0;
6495 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6496 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6497 e4526bf5 2021-09-03 naddy if (re == NULL)
6499 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6500 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6502 e4526bf5 2021-09-03 naddy if (n > 0)
6503 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6506 6458efa5 2020-11-24 stsp case KEY_UP:
6507 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6508 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6509 6458efa5 2020-11-24 stsp s->selected--;
6512 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6514 83cc4199 2022-06-13 stsp case CTRL('u'):
6516 83cc4199 2022-06-13 stsp nscroll /= 2;
6517 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6518 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6519 6458efa5 2020-11-24 stsp case CTRL('b'):
6520 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6521 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
6522 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
6525 6458efa5 2020-11-24 stsp case KEY_DOWN:
6526 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6527 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6528 6458efa5 2020-11-24 stsp s->selected++;
6531 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6532 6458efa5 2020-11-24 stsp /* can't scroll any further */
6534 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6536 83cc4199 2022-06-13 stsp case CTRL('d'):
6538 83cc4199 2022-06-13 stsp nscroll /= 2;
6539 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6540 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6541 6458efa5 2020-11-24 stsp case CTRL('f'):
6542 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6543 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6544 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6545 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6546 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6549 83cc4199 2022-06-13 stsp ref_scroll_down(s, nscroll);
6551 6458efa5 2020-11-24 stsp case CTRL('l'):
6552 8924d611 2020-12-26 stsp tog_free_refs();
6553 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
6556 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6557 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6559 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6560 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6561 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6567 6458efa5 2020-11-24 stsp return err;
6570 6458efa5 2020-11-24 stsp __dead static void
6571 6458efa5 2020-11-24 stsp usage_ref(void)
6574 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6575 6458efa5 2020-11-24 stsp getprogname());
6579 6458efa5 2020-11-24 stsp static const struct got_error *
6580 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6582 6458efa5 2020-11-24 stsp const struct got_error *error;
6583 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6584 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6585 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6587 6458efa5 2020-11-24 stsp struct tog_view *view;
6588 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6590 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6591 6458efa5 2020-11-24 stsp switch (ch) {
6593 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6594 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6595 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6599 e99e2d15 2020-11-24 naddy usage_ref();
6600 6458efa5 2020-11-24 stsp /* NOTREACHED */
6604 6458efa5 2020-11-24 stsp argc -= optind;
6605 6458efa5 2020-11-24 stsp argv += optind;
6607 6458efa5 2020-11-24 stsp if (argc > 1)
6608 e99e2d15 2020-11-24 naddy usage_ref();
6610 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6611 0ae84acc 2022-06-15 tracey if (error != NULL)
6612 0ae84acc 2022-06-15 tracey goto done;
6614 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6615 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6616 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6617 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6618 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6619 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6620 c156c7a4 2020-12-18 stsp goto done;
6621 6458efa5 2020-11-24 stsp if (worktree)
6622 6458efa5 2020-11-24 stsp repo_path =
6623 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6625 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6626 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6627 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6628 c156c7a4 2020-12-18 stsp goto done;
6632 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6633 6458efa5 2020-11-24 stsp if (error != NULL)
6634 6458efa5 2020-11-24 stsp goto done;
6636 6458efa5 2020-11-24 stsp init_curses();
6638 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6639 51a10b52 2020-12-26 stsp if (error)
6640 51a10b52 2020-12-26 stsp goto done;
6642 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6643 6458efa5 2020-11-24 stsp if (error)
6644 6458efa5 2020-11-24 stsp goto done;
6646 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6647 6458efa5 2020-11-24 stsp if (view == NULL) {
6648 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6649 6458efa5 2020-11-24 stsp goto done;
6652 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6653 6458efa5 2020-11-24 stsp if (error)
6654 6458efa5 2020-11-24 stsp goto done;
6656 6458efa5 2020-11-24 stsp if (worktree) {
6657 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6658 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6659 6458efa5 2020-11-24 stsp worktree = NULL;
6661 6458efa5 2020-11-24 stsp error = view_loop(view);
6663 6458efa5 2020-11-24 stsp free(repo_path);
6664 6458efa5 2020-11-24 stsp free(cwd);
6665 1d0f4054 2021-06-17 stsp if (repo) {
6666 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6667 1d0f4054 2021-06-17 stsp if (close_err)
6668 1d0f4054 2021-06-17 stsp error = close_err;
6670 0ae84acc 2022-06-15 tracey if (pack_fds) {
6671 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6672 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6673 0ae84acc 2022-06-15 tracey if (error == NULL)
6674 0ae84acc 2022-06-15 tracey error = pack_err;
6676 51a10b52 2020-12-26 stsp tog_free_refs();
6677 6458efa5 2020-11-24 stsp return error;
6680 6458efa5 2020-11-24 stsp static void
6681 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6685 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6686 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6687 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
6688 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6690 6879ba42 2020-10-01 naddy fputc('\n', fp);
6693 4ed7e80c 2018-05-20 stsp __dead static void
6694 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6696 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6698 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6699 6879ba42 2020-10-01 naddy getprogname());
6700 6879ba42 2020-10-01 naddy if (hflag) {
6701 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6702 6879ba42 2020-10-01 naddy list_commands(fp);
6704 6879ba42 2020-10-01 naddy exit(status);
6707 c2301be8 2018-04-30 stsp static char **
6708 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6710 ee85c5e8 2020-02-29 stsp va_list ap;
6711 c2301be8 2018-04-30 stsp char **argv;
6714 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6716 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6717 c2301be8 2018-04-30 stsp if (argv == NULL)
6718 c2301be8 2018-04-30 stsp err(1, "calloc");
6719 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6720 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6721 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6722 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6725 ee85c5e8 2020-02-29 stsp va_end(ap);
6726 c2301be8 2018-04-30 stsp return argv;
6730 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6731 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6732 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6733 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6734 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6736 ee85c5e8 2020-02-29 stsp static const struct got_error *
6737 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6739 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6740 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6741 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6742 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6743 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6744 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6745 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6746 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6747 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6749 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6750 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6751 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6753 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6754 0ae84acc 2022-06-15 tracey if (error != NULL)
6755 0ae84acc 2022-06-15 tracey goto done;
6757 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6758 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6759 ee85c5e8 2020-02-29 stsp goto done;
6761 ee85c5e8 2020-02-29 stsp if (worktree)
6762 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6764 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6765 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6766 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6767 ee85c5e8 2020-02-29 stsp goto done;
6770 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6771 ee85c5e8 2020-02-29 stsp if (error != NULL)
6772 ee85c5e8 2020-02-29 stsp goto done;
6774 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6775 ee85c5e8 2020-02-29 stsp repo, worktree);
6776 ee85c5e8 2020-02-29 stsp if (error)
6777 ee85c5e8 2020-02-29 stsp goto done;
6779 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6780 84de9106 2020-12-26 stsp if (error)
6781 84de9106 2020-12-26 stsp goto done;
6782 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6783 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6784 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6785 ee85c5e8 2020-02-29 stsp if (error)
6786 ee85c5e8 2020-02-29 stsp goto done;
6788 ee85c5e8 2020-02-29 stsp if (worktree) {
6789 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6790 ee85c5e8 2020-02-29 stsp worktree = NULL;
6793 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6794 a44927cc 2022-04-07 stsp if (error)
6795 a44927cc 2022-04-07 stsp goto done;
6797 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6798 ee85c5e8 2020-02-29 stsp if (error) {
6799 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6800 ee85c5e8 2020-02-29 stsp goto done;
6801 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6802 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6803 6879ba42 2020-10-01 naddy usage(1, 1);
6804 ee85c5e8 2020-02-29 stsp /* not reached */
6807 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6808 1d0f4054 2021-06-17 stsp if (error == NULL)
6809 1d0f4054 2021-06-17 stsp error = close_err;
6810 ee85c5e8 2020-02-29 stsp repo = NULL;
6812 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6813 ee85c5e8 2020-02-29 stsp if (error)
6814 ee85c5e8 2020-02-29 stsp goto done;
6816 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6818 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6819 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6821 1d0f4054 2021-06-17 stsp if (repo) {
6822 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6823 1d0f4054 2021-06-17 stsp if (error == NULL)
6824 1d0f4054 2021-06-17 stsp error = close_err;
6826 a44927cc 2022-04-07 stsp if (commit)
6827 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6828 ee85c5e8 2020-02-29 stsp if (worktree)
6829 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6830 0ae84acc 2022-06-15 tracey if (pack_fds) {
6831 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6832 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6833 0ae84acc 2022-06-15 tracey if (error == NULL)
6834 0ae84acc 2022-06-15 tracey error = pack_err;
6837 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6838 ee85c5e8 2020-02-29 stsp free(commit_id);
6839 ee85c5e8 2020-02-29 stsp free(cwd);
6840 ee85c5e8 2020-02-29 stsp free(repo_path);
6841 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6842 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6844 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6845 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6846 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6848 87670572 2020-12-26 naddy tog_free_refs();
6849 ee85c5e8 2020-02-29 stsp return error;
6853 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6855 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6856 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6857 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6858 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6859 3e166534 2022-02-16 naddy static const struct option longopts[] = {
6860 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6861 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6864 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6866 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6867 9f7d7167 2018-04-29 stsp switch (ch) {
6869 9f7d7167 2018-04-29 stsp hflag = 1;
6872 53ccebc2 2019-07-30 stsp Vflag = 1;
6875 6879ba42 2020-10-01 naddy usage(hflag, 1);
6876 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6880 9f7d7167 2018-04-29 stsp argc -= optind;
6881 9f7d7167 2018-04-29 stsp argv += optind;
6882 9814e6a3 2020-09-27 naddy optind = 1;
6883 c2301be8 2018-04-30 stsp optreset = 1;
6885 53ccebc2 2019-07-30 stsp if (Vflag) {
6886 53ccebc2 2019-07-30 stsp got_version_print_str();
6887 6879ba42 2020-10-01 naddy return 0;
6890 4010e238 2020-12-04 stsp #ifndef PROFILE
6891 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6892 4010e238 2020-12-04 stsp NULL) == -1)
6893 4010e238 2020-12-04 stsp err(1, "pledge");
6896 c2301be8 2018-04-30 stsp if (argc == 0) {
6897 f29d3e89 2018-06-23 stsp if (hflag)
6898 6879ba42 2020-10-01 naddy usage(hflag, 0);
6899 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6900 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6902 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6906 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6907 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6908 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6909 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6910 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6916 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6917 ee85c5e8 2020-02-29 stsp if (argc != 1)
6918 6879ba42 2020-10-01 naddy usage(0, 1);
6919 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6920 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6922 ee85c5e8 2020-02-29 stsp if (hflag)
6923 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6925 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6929 b46c1e04 2020-09-20 naddy putchar('\n');
6930 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6932 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6933 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6934 a2f4a359 2020-02-28 stsp free(cmd_argv);
6937 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6938 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);