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;
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 1a76625f 2018-10-22 stsp int log_complete;
342 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
343 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
344 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
345 13add988 2019-10-15 stsp int *searching;
346 13add988 2019-10-15 stsp int *search_next_done;
347 13add988 2019-10-15 stsp regex_t *regex;
350 1a76625f 2018-10-22 stsp struct tog_log_view_state {
351 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
352 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
353 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
354 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
355 b01e7d3b 2018-08-04 stsp int selected;
356 b01e7d3b 2018-08-04 stsp char *in_repo_path;
357 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
358 b672a97a 2020-01-27 stsp int log_branches;
359 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
360 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
361 1a76625f 2018-10-22 stsp sig_atomic_t quit;
362 1a76625f 2018-10-22 stsp pthread_t thread;
363 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
364 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
365 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
366 11b20872 2019-11-08 stsp struct tog_colors colors;
369 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
370 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
371 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
372 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
373 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
374 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
375 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
376 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
377 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
378 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
379 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
380 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
381 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
382 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
383 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
385 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
386 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
387 e9424729 2018-08-04 stsp int nlines;
389 e9424729 2018-08-04 stsp struct tog_view *view;
390 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
394 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
395 e9424729 2018-08-04 stsp const char *path;
396 e9424729 2018-08-04 stsp struct got_repository *repo;
397 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
398 e9424729 2018-08-04 stsp int *complete;
399 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
400 fc06ba56 2019-08-22 stsp void *cancel_arg;
403 e9424729 2018-08-04 stsp struct tog_blame {
405 be659d10 2020-11-18 stsp off_t filesize;
406 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
407 6fcac457 2018-11-19 stsp int nlines;
408 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
409 e9424729 2018-08-04 stsp pthread_t thread;
410 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
411 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
412 e9424729 2018-08-04 stsp const char *path;
415 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
416 7cbe629d 2018-08-04 stsp int first_displayed_line;
417 7cbe629d 2018-08-04 stsp int last_displayed_line;
418 7cbe629d 2018-08-04 stsp int selected_line;
419 7cbe629d 2018-08-04 stsp int blame_complete;
422 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
423 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
424 e5a0f69f 2018-08-18 stsp char *path;
425 7cbe629d 2018-08-04 stsp struct got_repository *repo;
426 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
427 e9424729 2018-08-04 stsp struct tog_blame blame;
428 6c4c42e0 2019-06-24 stsp int matched_line;
429 11b20872 2019-11-08 stsp struct tog_colors colors;
432 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
433 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
434 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
435 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
436 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
437 ad80ab7b 2018-08-04 stsp int selected;
440 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
442 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
443 ad80ab7b 2018-08-04 stsp char *tree_label;
444 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
445 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
446 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
447 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
448 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
449 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
450 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
451 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
452 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
453 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
454 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
455 6458efa5 2020-11-24 stsp struct tog_colors colors;
458 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
459 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
460 6458efa5 2020-11-24 stsp struct got_reference *ref;
464 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
466 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
467 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
468 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
469 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
470 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
471 7f66531d 2021-11-16 stsp int nrefs, ndisplayed, selected, show_ids, sort_by_date;
472 6458efa5 2020-11-24 stsp struct got_repository *repo;
473 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
474 bddb1296 2019-11-08 stsp struct tog_colors colors;
478 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
480 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
481 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
482 669b5ffa 2018-10-07 stsp * there is enough screen estate.
484 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
485 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
487 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
488 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
489 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
491 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
492 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
494 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
496 cc3c9aac 2018-08-01 stsp struct tog_view {
497 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
498 26ed57b2 2018-05-19 stsp WINDOW *window;
499 26ed57b2 2018-05-19 stsp PANEL *panel;
500 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
501 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
502 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
504 669b5ffa 2018-10-07 stsp struct tog_view *parent;
505 669b5ffa 2018-10-07 stsp struct tog_view *child;
508 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
509 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
510 e78dc838 2020-12-04 stsp * between parent and child.
511 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
512 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
513 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
514 e78dc838 2020-12-04 stsp * situations.
516 e78dc838 2020-12-04 stsp int focus_child;
518 5dc9f4bc 2018-08-04 stsp /* type-specific state */
519 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
521 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
522 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
523 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
524 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
525 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
528 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
529 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
530 e78dc838 2020-12-04 stsp struct tog_view *, int);
531 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
533 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
534 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
535 c0c4acc8 2021-01-24 stsp int search_started;
536 60493ae3 2019-06-20 stsp int searching;
537 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
538 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
539 60493ae3 2019-06-20 stsp int search_next_done;
540 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
541 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
542 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
543 1803e47f 2019-06-22 stsp regex_t regex;
544 41605754 2020-11-12 stsp regmatch_t regmatch;
547 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
548 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
549 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
550 78756c87 2020-11-24 stsp struct got_repository *);
551 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
552 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
553 e78dc838 2020-12-04 stsp struct tog_view *, int);
554 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
555 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
556 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
558 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
559 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
560 78756c87 2020-11-24 stsp const char *, const char *, int);
561 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
562 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
563 e78dc838 2020-12-04 stsp struct tog_view *, int);
564 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
565 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
566 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
568 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
569 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
570 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
571 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
572 e78dc838 2020-12-04 stsp struct tog_view *, int);
573 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
574 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
575 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
577 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
578 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
579 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
583 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
584 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
586 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
587 6458efa5 2020-11-24 stsp struct got_repository *);
588 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
589 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
592 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
593 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
595 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
596 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
597 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
599 25791caa 2018-10-24 stsp static void
600 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
602 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
605 83baff54 2019-08-12 stsp static void
606 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
608 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
611 61266923 2020-01-14 stsp static void
612 61266923 2020-01-14 stsp tog_sigcont(int signo)
614 61266923 2020-01-14 stsp tog_sigcont_received = 1;
617 e5a0f69f 2018-08-18 stsp static const struct got_error *
618 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
620 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
622 669b5ffa 2018-10-07 stsp if (view->child) {
623 669b5ffa 2018-10-07 stsp view_close(view->child);
624 669b5ffa 2018-10-07 stsp view->child = NULL;
626 e5a0f69f 2018-08-18 stsp if (view->close)
627 e5a0f69f 2018-08-18 stsp err = view->close(view);
628 ea5e7bb5 2018-08-01 stsp if (view->panel)
629 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
630 ea5e7bb5 2018-08-01 stsp if (view->window)
631 ea5e7bb5 2018-08-01 stsp delwin(view->window);
632 ea5e7bb5 2018-08-01 stsp free(view);
633 e5a0f69f 2018-08-18 stsp return err;
636 ea5e7bb5 2018-08-01 stsp static struct tog_view *
637 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
638 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
640 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
642 ea5e7bb5 2018-08-01 stsp if (view == NULL)
643 ea5e7bb5 2018-08-01 stsp return NULL;
645 d6b05b5b 2018-08-04 stsp view->type = type;
646 f7d12f7e 2018-08-01 stsp view->lines = LINES;
647 f7d12f7e 2018-08-01 stsp view->cols = COLS;
648 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
649 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
650 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
651 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
652 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
653 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
654 96a765a8 2018-08-04 stsp view_close(view);
655 ea5e7bb5 2018-08-01 stsp return NULL;
657 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
658 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
659 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
660 96a765a8 2018-08-04 stsp view_close(view);
661 ea5e7bb5 2018-08-01 stsp return NULL;
664 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
665 ea5e7bb5 2018-08-01 stsp return view;
669 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
671 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
673 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
676 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
678 5c60c32a 2018-10-18 stsp static const struct got_error *
679 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
681 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
683 5c60c32a 2018-10-18 stsp view->begin_y = 0;
684 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
685 5c60c32a 2018-10-18 stsp view->nlines = LINES;
686 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
687 5c60c32a 2018-10-18 stsp view->lines = LINES;
688 5c60c32a 2018-10-18 stsp view->cols = COLS;
689 5c60c32a 2018-10-18 stsp err = view_resize(view);
691 5c60c32a 2018-10-18 stsp return err;
693 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
694 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
696 5c60c32a 2018-10-18 stsp return NULL;
699 5c60c32a 2018-10-18 stsp static const struct got_error *
700 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
702 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
704 5c60c32a 2018-10-18 stsp view->begin_x = 0;
705 5c60c32a 2018-10-18 stsp view->begin_y = 0;
706 5c60c32a 2018-10-18 stsp view->nlines = LINES;
707 5c60c32a 2018-10-18 stsp view->ncols = COLS;
708 5c60c32a 2018-10-18 stsp view->lines = LINES;
709 5c60c32a 2018-10-18 stsp view->cols = COLS;
710 5c60c32a 2018-10-18 stsp err = view_resize(view);
712 5c60c32a 2018-10-18 stsp return err;
714 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
715 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
717 5c60c32a 2018-10-18 stsp return NULL;
721 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
723 5c60c32a 2018-10-18 stsp return view->parent == NULL;
726 4d8c2215 2018-08-19 stsp static const struct got_error *
727 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
729 f7d12f7e 2018-08-01 stsp int nlines, ncols;
731 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
732 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
734 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
736 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
737 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
739 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
741 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
742 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
743 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
744 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
745 25791caa 2018-10-24 stsp wclear(view->window);
747 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
748 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
749 0cf4efb1 2018-09-29 stsp view->lines = LINES;
750 0cf4efb1 2018-09-29 stsp view->cols = COLS;
752 6e3e5d9c 2018-10-18 stsp if (view->child) {
753 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
754 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
755 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
756 5c60c32a 2018-10-18 stsp if (view->child->focussed)
757 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
759 5c60c32a 2018-10-18 stsp show_panel(view->panel);
761 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
762 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
766 5c60c32a 2018-10-18 stsp return NULL;
769 669b5ffa 2018-10-07 stsp static const struct got_error *
770 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
772 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
774 669b5ffa 2018-10-07 stsp if (view->child == NULL)
775 669b5ffa 2018-10-07 stsp return NULL;
777 669b5ffa 2018-10-07 stsp err = view_close(view->child);
778 669b5ffa 2018-10-07 stsp view->child = NULL;
779 669b5ffa 2018-10-07 stsp return err;
782 72a9cb46 2020-12-03 stsp static void
783 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
785 669b5ffa 2018-10-07 stsp view->child = child;
786 669b5ffa 2018-10-07 stsp child->parent = view;
790 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
792 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
795 34bc9ec9 2019-02-22 stsp static void
796 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
798 25791caa 2018-10-24 stsp int cols, lines;
799 25791caa 2018-10-24 stsp struct winsize size;
801 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
802 25791caa 2018-10-24 stsp cols = 80; /* Default */
803 25791caa 2018-10-24 stsp lines = 24;
805 25791caa 2018-10-24 stsp cols = size.ws_col;
806 25791caa 2018-10-24 stsp lines = size.ws_row;
808 25791caa 2018-10-24 stsp resize_term(lines, cols);
811 2b49a8ae 2019-06-22 stsp static const struct got_error *
812 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
814 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
815 2b49a8ae 2019-06-22 stsp char pattern[1024];
818 c0c4acc8 2021-01-24 stsp if (view->search_started) {
819 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
820 c0c4acc8 2021-01-24 stsp view->searching = 0;
821 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
823 c0c4acc8 2021-01-24 stsp view->search_started = 0;
825 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
826 2b49a8ae 2019-06-22 stsp return NULL;
828 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
829 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
831 2b49a8ae 2019-06-22 stsp nocbreak();
833 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
836 2b49a8ae 2019-06-22 stsp if (ret == ERR)
837 2b49a8ae 2019-06-22 stsp return NULL;
839 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
840 7c32bd05 2019-06-22 stsp err = view->search_start(view);
842 7c32bd05 2019-06-22 stsp regfree(&view->regex);
843 7c32bd05 2019-06-22 stsp return err;
845 c0c4acc8 2021-01-24 stsp view->search_started = 1;
846 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
847 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
848 2b49a8ae 2019-06-22 stsp view->search_next(view);
851 2b49a8ae 2019-06-22 stsp return NULL;
854 0cf4efb1 2018-09-29 stsp static const struct got_error *
855 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
856 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
858 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
859 669b5ffa 2018-10-07 stsp struct tog_view *v;
860 1a76625f 2018-10-22 stsp int ch, errcode;
862 e5a0f69f 2018-08-18 stsp *new = NULL;
864 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
865 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
866 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
867 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
869 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
870 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
871 82954512 2020-02-03 stsp if (errcode)
872 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
873 82954512 2020-02-03 stsp "pthread_mutex_unlock");
874 3da8ef85 2021-09-21 stsp sched_yield();
875 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
876 82954512 2020-02-03 stsp if (errcode)
877 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
878 82954512 2020-02-03 stsp "pthread_mutex_lock");
879 60493ae3 2019-06-20 stsp view->search_next(view);
880 60493ae3 2019-06-20 stsp return NULL;
883 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
884 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
885 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
886 1a76625f 2018-10-22 stsp if (errcode)
887 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
888 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
889 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
890 1a76625f 2018-10-22 stsp if (errcode)
891 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
892 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
894 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
895 25791caa 2018-10-24 stsp tog_resizeterm();
896 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
897 61266923 2020-01-14 stsp tog_sigcont_received = 0;
898 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
899 25791caa 2018-10-24 stsp err = view_resize(v);
901 25791caa 2018-10-24 stsp return err;
902 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
904 25791caa 2018-10-24 stsp return err;
905 cdfcfb03 2020-12-06 stsp if (v->child) {
906 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
908 cdfcfb03 2020-12-06 stsp return err;
909 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
910 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
912 cdfcfb03 2020-12-06 stsp return err;
917 e5a0f69f 2018-08-18 stsp switch (ch) {
919 1e37a5c2 2019-05-12 jcs if (view->child) {
920 e78dc838 2020-12-04 stsp view->focussed = 0;
921 e78dc838 2020-12-04 stsp view->child->focussed = 1;
922 e78dc838 2020-12-04 stsp view->focus_child = 1;
923 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
924 e78dc838 2020-12-04 stsp view->focussed = 0;
925 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
926 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
930 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
931 9970f7fc 2020-12-03 stsp view->dying = 1;
937 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
938 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
940 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
941 e78dc838 2020-12-04 stsp view->focussed = 0;
942 e78dc838 2020-12-04 stsp view->child->focussed = 1;
943 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
945 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
948 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
949 9970f7fc 2020-12-03 stsp KEY_RESIZE);
951 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
952 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
953 e78dc838 2020-12-04 stsp view->focussed = 1;
954 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
956 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
960 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
963 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
966 60493ae3 2019-06-20 stsp if (view->search_start)
967 2b49a8ae 2019-06-22 stsp view_search_start(view);
969 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
973 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
974 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
975 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
976 60493ae3 2019-06-20 stsp view->search_next_done = 0;
977 60493ae3 2019-06-20 stsp view->search_next(view);
979 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
982 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
986 e5a0f69f 2018-08-18 stsp return err;
990 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
992 0cf4efb1 2018-09-29 stsp PANEL *panel;
993 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
995 669b5ffa 2018-10-07 stsp if (view->parent)
996 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
998 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
999 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1002 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1003 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1004 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1008 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1010 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1011 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1013 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1015 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1018 669b5ffa 2018-10-07 stsp return view->focussed;
1021 bcbd79e2 2018-08-19 stsp static const struct got_error *
1022 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1024 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1025 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1026 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1027 fd823528 2018-10-22 stsp int fast_refresh = 10;
1028 1a76625f 2018-10-22 stsp int done = 0, errcode;
1030 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1031 1a76625f 2018-10-22 stsp if (errcode)
1032 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1034 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1035 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1037 1004088d 2018-09-29 stsp view->focussed = 1;
1038 878940b7 2018-09-29 stsp err = view->show(view);
1040 0cf4efb1 2018-09-29 stsp return err;
1041 0cf4efb1 2018-09-29 stsp update_panels();
1042 0cf4efb1 2018-09-29 stsp doupdate();
1043 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1044 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1045 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1046 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1048 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1051 9970f7fc 2020-12-03 stsp if (view->dying) {
1052 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1054 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1055 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1057 e78dc838 2020-12-04 stsp else if (view->parent)
1058 669b5ffa 2018-10-07 stsp prev = view->parent;
1060 e78dc838 2020-12-04 stsp if (view->parent) {
1061 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1062 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1064 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1066 9970f7fc 2020-12-03 stsp err = view_close(view);
1068 e5a0f69f 2018-08-18 stsp goto done;
1070 e78dc838 2020-12-04 stsp view = NULL;
1071 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1072 e78dc838 2020-12-04 stsp if (v->focussed)
1075 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1076 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1078 e78dc838 2020-12-04 stsp view = prev;
1079 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1080 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1081 e78dc838 2020-12-04 stsp tog_view_list_head);
1083 e78dc838 2020-12-04 stsp if (view) {
1084 e78dc838 2020-12-04 stsp if (view->focus_child) {
1085 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1086 e78dc838 2020-12-04 stsp view = view->child;
1088 e78dc838 2020-12-04 stsp view->focussed = 1;
1092 bcbd79e2 2018-08-19 stsp if (new_view) {
1093 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1094 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1095 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1096 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1098 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1099 86c66b02 2018-10-18 stsp err = view_close(v);
1101 86c66b02 2018-10-18 stsp goto done;
1104 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1105 fed7eaa8 2018-10-24 stsp view = new_view;
1107 669b5ffa 2018-10-07 stsp if (view) {
1108 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1109 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1110 e78dc838 2020-12-04 stsp view = view->child;
1112 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1113 e78dc838 2020-12-04 stsp view = view->parent;
1115 e78dc838 2020-12-04 stsp show_panel(view->panel);
1116 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1117 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1118 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1119 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1121 1a76625f 2018-10-22 stsp goto done;
1123 669b5ffa 2018-10-07 stsp err = view->show(view);
1125 1a76625f 2018-10-22 stsp goto done;
1126 669b5ffa 2018-10-07 stsp if (view->child) {
1127 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1129 1a76625f 2018-10-22 stsp goto done;
1131 1a76625f 2018-10-22 stsp update_panels();
1132 1a76625f 2018-10-22 stsp doupdate();
1136 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1137 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1138 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1139 e5a0f69f 2018-08-18 stsp view_close(view);
1142 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1143 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1144 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1146 e5a0f69f 2018-08-18 stsp return err;
1149 4ed7e80c 2018-05-20 stsp __dead static void
1150 9f7d7167 2018-04-29 stsp usage_log(void)
1153 c70c5802 2018-08-01 stsp fprintf(stderr,
1154 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1155 9f7d7167 2018-04-29 stsp getprogname());
1159 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1160 80ddbec8 2018-04-29 stsp static const struct got_error *
1161 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1163 00dfcb92 2018-06-11 stsp char *vis = NULL;
1164 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1166 963b370f 2018-05-20 stsp *ws = NULL;
1167 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1168 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1169 00dfcb92 2018-06-11 stsp int vislen;
1170 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1171 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1173 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1174 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1176 00dfcb92 2018-06-11 stsp return err;
1177 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1178 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1179 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1180 a7f50699 2018-06-11 stsp goto done;
1184 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1185 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1186 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1187 a7f50699 2018-06-11 stsp goto done;
1190 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1191 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1193 00dfcb92 2018-06-11 stsp free(vis);
1194 963b370f 2018-05-20 stsp if (err) {
1195 963b370f 2018-05-20 stsp free(*ws);
1196 963b370f 2018-05-20 stsp *ws = NULL;
1197 963b370f 2018-05-20 stsp *wlen = 0;
1199 963b370f 2018-05-20 stsp return err;
1202 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1203 963b370f 2018-05-20 stsp static const struct got_error *
1204 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1205 27a741e5 2019-09-11 stsp int col_tab_align)
1207 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1208 963b370f 2018-05-20 stsp int cols = 0;
1209 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1210 963b370f 2018-05-20 stsp size_t wlen;
1213 963b370f 2018-05-20 stsp *wlinep = NULL;
1214 b700b5d6 2018-07-10 stsp *widthp = 0;
1216 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1218 963b370f 2018-05-20 stsp return err;
1220 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1221 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1224 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1225 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1230 27a741e5 2019-09-11 stsp while (i < wlen) {
1231 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1233 27a741e5 2019-09-11 stsp if (width == 0) {
1238 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1239 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1241 27a741e5 2019-09-11 stsp cols += width;
1243 27a741e5 2019-09-11 stsp } else if (width == -1) {
1244 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1245 27a741e5 2019-09-11 stsp width = TABSIZE -
1246 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1248 748d5cab 2020-12-14 naddy width = 1;
1249 748d5cab 2020-12-14 naddy wline[i] = L'.';
1251 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1253 748d5cab 2020-12-14 naddy cols += width;
1256 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1257 963b370f 2018-05-20 stsp goto done;
1260 963b370f 2018-05-20 stsp wline[i] = L'\0';
1261 b700b5d6 2018-07-10 stsp if (widthp)
1262 b700b5d6 2018-07-10 stsp *widthp = cols;
1265 963b370f 2018-05-20 stsp free(wline);
1267 963b370f 2018-05-20 stsp *wlinep = wline;
1268 963b370f 2018-05-20 stsp return err;
1271 8b473291 2019-02-21 stsp static const struct got_error*
1272 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1273 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1275 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1276 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1278 8b473291 2019-02-21 stsp const char *name;
1280 8b473291 2019-02-21 stsp *refs_str = NULL;
1282 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1283 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1284 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1287 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1288 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1290 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1291 8b473291 2019-02-21 stsp name += 5;
1292 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1293 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1295 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1296 8b473291 2019-02-21 stsp name += 6;
1297 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1298 8b473291 2019-02-21 stsp name += 8;
1299 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1300 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1303 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1306 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1307 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1308 5d844a1e 2019-08-13 stsp if (err) {
1309 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1310 48cae60d 2020-09-22 stsp free(ref_id);
1313 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1314 5d844a1e 2019-08-13 stsp err = NULL;
1315 5d844a1e 2019-08-13 stsp tag = NULL;
1318 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1319 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1320 48cae60d 2020-09-22 stsp free(ref_id);
1322 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1323 52b5abe1 2019-08-13 stsp if (cmp != 0)
1325 8b473291 2019-02-21 stsp s = *refs_str;
1326 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1327 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1328 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1330 8b473291 2019-02-21 stsp *refs_str = NULL;
1336 8b473291 2019-02-21 stsp return err;
1339 963b370f 2018-05-20 stsp static const struct got_error *
1340 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1341 27a741e5 2019-09-11 stsp int col_tab_align)
1343 e6b8b890 2020-12-29 naddy char *smallerthan;
1345 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1346 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1347 5813d178 2019-03-09 stsp author = smallerthan + 1;
1348 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1349 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1352 5813d178 2019-03-09 stsp static const struct got_error *
1353 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1354 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1355 8fdc79fe 2020-12-01 naddy int author_display_cols)
1357 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1358 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1359 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1360 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1361 5813d178 2019-03-09 stsp char *author = NULL;
1362 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1363 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1364 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1365 bb737323 2018-05-20 stsp int col, limit;
1366 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1367 ccb26ccd 2018-11-05 stsp struct tm tm;
1368 45d799e2 2018-12-23 stsp time_t committer_time;
1369 11b20872 2019-11-08 stsp struct tog_color *tc;
1371 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1372 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1373 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1374 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1375 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1377 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1378 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1380 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1381 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1383 11b20872 2019-11-08 stsp wattr_on(view->window,
1384 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1385 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1387 11b20872 2019-11-08 stsp wattr_off(view->window,
1388 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1389 27a741e5 2019-09-11 stsp col = limit;
1390 b39d25c7 2018-07-10 stsp if (col > avail)
1391 b39d25c7 2018-07-10 stsp goto done;
1393 6570a66d 2019-11-08 stsp if (avail >= 120) {
1394 6570a66d 2019-11-08 stsp char *id_str;
1395 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1397 6570a66d 2019-11-08 stsp goto done;
1398 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1400 11b20872 2019-11-08 stsp wattr_on(view->window,
1401 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1402 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1404 11b20872 2019-11-08 stsp wattr_off(view->window,
1405 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1406 6570a66d 2019-11-08 stsp free(id_str);
1408 6570a66d 2019-11-08 stsp if (col > avail)
1409 6570a66d 2019-11-08 stsp goto done;
1412 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1413 5813d178 2019-03-09 stsp if (author == NULL) {
1414 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1415 80ddbec8 2018-04-29 stsp goto done;
1417 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1419 bb737323 2018-05-20 stsp goto done;
1420 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1422 11b20872 2019-11-08 stsp wattr_on(view->window,
1423 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1424 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1426 11b20872 2019-11-08 stsp wattr_off(view->window,
1427 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1428 bb737323 2018-05-20 stsp col += author_width;
1429 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1430 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1432 bb737323 2018-05-20 stsp author_width++;
1434 9c2eaf34 2018-05-20 stsp if (col > avail)
1435 9c2eaf34 2018-05-20 stsp goto done;
1437 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1439 6d9fbc00 2018-04-29 stsp goto done;
1440 bb737323 2018-05-20 stsp logmsg = logmsg0;
1441 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1443 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1444 bb737323 2018-05-20 stsp if (newline)
1445 bb737323 2018-05-20 stsp *newline = '\0';
1446 bb737323 2018-05-20 stsp limit = avail - col;
1447 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1449 bb737323 2018-05-20 stsp goto done;
1450 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1451 bb737323 2018-05-20 stsp col += logmsg_width;
1452 27a741e5 2019-09-11 stsp while (col < avail) {
1453 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1457 80ddbec8 2018-04-29 stsp free(logmsg0);
1458 bb737323 2018-05-20 stsp free(wlogmsg);
1459 5813d178 2019-03-09 stsp free(author);
1460 bb737323 2018-05-20 stsp free(wauthor);
1461 80ddbec8 2018-04-29 stsp free(line);
1462 80ddbec8 2018-04-29 stsp return err;
1465 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1466 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1467 899d86c2 2018-05-10 stsp struct got_object_id *id)
1469 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1471 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1472 80ddbec8 2018-04-29 stsp if (entry == NULL)
1473 899d86c2 2018-05-10 stsp return NULL;
1475 899d86c2 2018-05-10 stsp entry->id = id;
1476 99db9666 2018-05-07 stsp entry->commit = commit;
1477 899d86c2 2018-05-10 stsp return entry;
1480 99db9666 2018-05-07 stsp static void
1481 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1483 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1485 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1486 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1487 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1488 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1489 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1490 99db9666 2018-05-07 stsp free(entry);
1493 99db9666 2018-05-07 stsp static void
1494 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1496 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1497 99db9666 2018-05-07 stsp pop_commit(commits);
1500 c4972b91 2018-05-07 stsp static const struct got_error *
1501 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1502 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1504 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1505 13add988 2019-10-15 stsp regmatch_t regmatch;
1506 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1508 13add988 2019-10-15 stsp *have_match = 0;
1510 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1512 13add988 2019-10-15 stsp return err;
1514 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1516 13add988 2019-10-15 stsp goto done;
1518 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1519 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1520 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1521 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1522 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1523 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1524 13add988 2019-10-15 stsp *have_match = 1;
1526 13add988 2019-10-15 stsp free(id_str);
1527 13add988 2019-10-15 stsp free(logmsg);
1528 13add988 2019-10-15 stsp return err;
1531 13add988 2019-10-15 stsp static const struct got_error *
1532 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1534 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1537 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1538 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1539 1a76625f 2018-10-22 stsp * while updating the display.
1542 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1543 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1544 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1545 1a76625f 2018-10-22 stsp int errcode;
1547 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1548 4e0d2870 2020-12-07 naddy NULL, NULL);
1549 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1552 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1555 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1556 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1557 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1561 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1562 1a76625f 2018-10-22 stsp if (errcode) {
1563 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1564 13add988 2019-10-15 stsp "pthread_mutex_lock");
1568 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1569 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1570 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1572 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1573 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1574 7c1452c1 2020-03-26 stsp int have_match;
1575 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1578 7c1452c1 2020-03-26 stsp if (have_match)
1579 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1582 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1583 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1584 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1585 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1588 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1590 9ba79e04 2018-06-11 stsp return err;
1593 2b779855 2020-12-05 naddy static void
1594 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1596 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1597 2b779855 2020-12-05 naddy int ncommits = 0;
1599 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1600 2b779855 2020-12-05 naddy while (entry) {
1601 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1602 2b779855 2020-12-05 naddy s->selected_entry = entry;
1605 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1606 2b779855 2020-12-05 naddy ncommits++;
1610 0553a4e3 2018-04-30 stsp static const struct got_error *
1611 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1613 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1614 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1615 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1616 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1617 60493ae3 2019-06-20 stsp int width;
1618 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1619 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1620 8b473291 2019-02-21 stsp char *refs_str = NULL;
1621 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1622 11b20872 2019-11-08 stsp struct tog_color *tc;
1623 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1625 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1626 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1627 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1628 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1630 ecb28ae0 2018-07-16 stsp return err;
1631 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1632 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1633 d2075bf3 2020-12-25 stsp if (refs) {
1634 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1635 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1637 d2075bf3 2020-12-25 stsp goto done;
1641 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1642 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1644 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1645 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1646 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1647 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1648 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1649 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1650 8f4ed634 2020-03-26 stsp goto done;
1653 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1655 f9686aa5 2020-03-27 stsp if (view->searching) {
1656 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1657 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1658 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1659 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1660 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1661 f9686aa5 2020-03-27 stsp search_str = "searching...";
1664 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1665 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1666 f9686aa5 2020-03-27 stsp search_str ? search_str :
1667 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1668 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1669 8f4ed634 2020-03-26 stsp goto done;
1673 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1674 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1675 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1676 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1677 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1678 1a76625f 2018-10-22 stsp header = NULL;
1679 1a76625f 2018-10-22 stsp goto done;
1681 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1682 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1683 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1684 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1685 1a76625f 2018-10-22 stsp header = NULL;
1686 1a76625f 2018-10-22 stsp goto done;
1688 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1690 1a76625f 2018-10-22 stsp goto done;
1692 2814baeb 2018-08-01 stsp werase(view->window);
1694 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1695 a3404814 2018-09-02 stsp wstandout(view->window);
1696 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1698 11b20872 2019-11-08 stsp wattr_on(view->window,
1699 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1700 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1702 11b20872 2019-11-08 stsp wattr_off(view->window,
1703 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1704 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1705 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1708 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1709 a3404814 2018-09-02 stsp wstandend(view->window);
1710 ecb28ae0 2018-07-16 stsp free(wline);
1711 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1712 1a76625f 2018-10-22 stsp goto done;
1714 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1715 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1716 5813d178 2019-03-09 stsp ncommits = 0;
1717 5813d178 2019-03-09 stsp while (entry) {
1718 5813d178 2019-03-09 stsp char *author;
1719 5813d178 2019-03-09 stsp wchar_t *wauthor;
1720 5813d178 2019-03-09 stsp int width;
1721 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1723 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1724 5813d178 2019-03-09 stsp if (author == NULL) {
1725 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1726 5813d178 2019-03-09 stsp goto done;
1728 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1729 27a741e5 2019-09-11 stsp date_display_cols);
1730 5813d178 2019-03-09 stsp if (author_cols < width)
1731 5813d178 2019-03-09 stsp author_cols = width;
1732 5813d178 2019-03-09 stsp free(wauthor);
1733 5813d178 2019-03-09 stsp free(author);
1734 7ca04879 2019-10-19 stsp ncommits++;
1735 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1738 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1739 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1740 867c6645 2018-07-10 stsp ncommits = 0;
1741 899d86c2 2018-05-10 stsp while (entry) {
1742 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1744 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1745 2814baeb 2018-08-01 stsp wstandout(view->window);
1746 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1747 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1748 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1749 2814baeb 2018-08-01 stsp wstandend(view->window);
1751 60493ae3 2019-06-20 stsp goto done;
1752 0553a4e3 2018-04-30 stsp ncommits++;
1753 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1754 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1757 1a57306a 2018-09-02 stsp view_vborder(view);
1759 1a76625f 2018-10-22 stsp free(id_str);
1760 8b473291 2019-02-21 stsp free(refs_str);
1761 1a76625f 2018-10-22 stsp free(ncommits_str);
1762 1a76625f 2018-10-22 stsp free(header);
1763 80ddbec8 2018-04-29 stsp return err;
1766 07b55e75 2018-05-10 stsp static void
1767 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1769 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1770 07b55e75 2018-05-10 stsp int nscrolled = 0;
1772 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1773 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1776 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1777 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1778 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1779 07b55e75 2018-05-10 stsp if (entry) {
1780 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1781 07b55e75 2018-05-10 stsp nscrolled++;
1786 aa075928 2018-05-10 stsp static const struct got_error *
1787 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1789 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1790 5e224a3e 2019-02-22 stsp int errcode;
1792 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1794 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1795 ffe38506 2020-12-01 naddy if (ta->log_complete)
1798 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1799 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1800 7aafa0d1 2019-02-22 stsp if (errcode)
1801 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1802 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1805 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1806 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1808 7c1452c1 2020-03-26 stsp if (!wait)
1811 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1812 ffe38506 2020-12-01 naddy show_log_view(view);
1813 7c1452c1 2020-03-26 stsp update_panels();
1814 7c1452c1 2020-03-26 stsp doupdate();
1816 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1817 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1818 82954512 2020-02-03 stsp if (errcode)
1819 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1820 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1822 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1823 ffe38506 2020-12-01 naddy show_log_view(view);
1824 7c1452c1 2020-03-26 stsp update_panels();
1825 7c1452c1 2020-03-26 stsp doupdate();
1828 5e224a3e 2019-02-22 stsp return NULL;
1831 5e224a3e 2019-02-22 stsp static const struct got_error *
1832 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1834 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1835 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1836 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1837 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1839 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1840 5e224a3e 2019-02-22 stsp return NULL;
1842 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1843 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1844 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1846 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1848 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1849 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1851 5e224a3e 2019-02-22 stsp return err;
1855 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1856 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1859 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1861 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1862 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1864 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1865 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1867 dd0a52c1 2018-05-20 stsp return err;
1870 cd0acaa7 2018-05-20 stsp static const struct got_error *
1871 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1872 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1873 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1875 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1876 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1877 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1879 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1880 15a94983 2018-12-23 stsp if (diff_view == NULL)
1881 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1883 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1884 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1885 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1886 e5a0f69f 2018-08-18 stsp if (err == NULL)
1887 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1888 cd0acaa7 2018-05-20 stsp return err;
1891 80ddbec8 2018-04-29 stsp static const struct got_error *
1892 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1893 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1895 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1897 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1898 941e9f74 2019-05-21 stsp if (parent == NULL)
1899 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1901 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1902 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1903 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1904 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1905 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1906 941e9f74 2019-05-21 stsp s->tree = subtree;
1907 941e9f74 2019-05-21 stsp s->selected = 0;
1908 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1909 941e9f74 2019-05-21 stsp return NULL;
1912 941e9f74 2019-05-21 stsp static const struct got_error *
1913 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1914 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
1916 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1917 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1918 941e9f74 2019-05-21 stsp const char *p;
1919 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1921 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1923 941e9f74 2019-05-21 stsp while (*p) {
1924 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1925 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1926 56e0773d 2019-11-28 stsp char *te_name;
1928 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1931 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1932 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1933 941e9f74 2019-05-21 stsp if (slash == NULL)
1934 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1936 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1937 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1938 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1941 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1942 56e0773d 2019-11-28 stsp if (te == NULL) {
1943 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1944 56e0773d 2019-11-28 stsp free(te_name);
1947 56e0773d 2019-11-28 stsp free(te_name);
1948 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1950 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1951 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1953 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1954 941e9f74 2019-05-21 stsp if (slash)
1955 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1957 941e9f74 2019-05-21 stsp subpath = strdup(path);
1958 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1959 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1963 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
1968 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1969 941e9f74 2019-05-21 stsp free(tree_id);
1973 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1974 941e9f74 2019-05-21 stsp if (err) {
1975 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1978 941e9f74 2019-05-21 stsp if (slash == NULL)
1980 941e9f74 2019-05-21 stsp free(subpath);
1981 941e9f74 2019-05-21 stsp subpath = NULL;
1982 941e9f74 2019-05-21 stsp p = slash;
1985 941e9f74 2019-05-21 stsp free(subpath);
1986 1a76625f 2018-10-22 stsp return err;
1989 61266923 2020-01-14 stsp static const struct got_error *
1990 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1991 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1992 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
1994 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1995 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1996 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1998 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1999 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2000 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2002 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2004 55cccc34 2020-02-20 stsp return err;
2005 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2007 55cccc34 2020-02-20 stsp *new_view = tree_view;
2009 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2010 55cccc34 2020-02-20 stsp return NULL;
2012 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2015 55cccc34 2020-02-20 stsp static const struct got_error *
2016 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2018 61266923 2020-01-14 stsp sigset_t sigset;
2019 61266923 2020-01-14 stsp int errcode;
2021 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2022 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2024 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
2025 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2026 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2027 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2028 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2030 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2031 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2032 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2034 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2035 61266923 2020-01-14 stsp if (errcode)
2036 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2038 61266923 2020-01-14 stsp return NULL;
2041 1a76625f 2018-10-22 stsp static void *
2042 1a76625f 2018-10-22 stsp log_thread(void *arg)
2044 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2045 1a76625f 2018-10-22 stsp int errcode = 0;
2046 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2047 1a76625f 2018-10-22 stsp int done = 0;
2049 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2051 61266923 2020-01-14 stsp return (void *)err;
2053 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2054 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2055 1a76625f 2018-10-22 stsp if (err) {
2056 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2057 1a76625f 2018-10-22 stsp return (void *)err;
2058 1a76625f 2018-10-22 stsp err = NULL;
2060 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2061 1a76625f 2018-10-22 stsp a->commits_needed--;
2063 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2064 3abe8080 2019-04-10 stsp if (errcode) {
2065 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2066 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2068 3abe8080 2019-04-10 stsp } else if (*a->quit)
2070 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2071 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2072 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2073 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2076 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2077 7c1452c1 2020-03-26 stsp if (errcode) {
2078 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2079 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2080 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2085 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2087 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2088 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2089 7c1452c1 2020-03-26 stsp &tog_mutex);
2090 7c1452c1 2020-03-26 stsp if (errcode)
2091 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2092 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2093 21355643 2020-12-06 stsp if (*a->quit)
2098 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2099 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2100 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2101 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2103 3abe8080 2019-04-10 stsp a->log_complete = 1;
2104 1a76625f 2018-10-22 stsp return (void *)err;
2107 1a76625f 2018-10-22 stsp static const struct got_error *
2108 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2110 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2111 1a76625f 2018-10-22 stsp int errcode;
2113 1a76625f 2018-10-22 stsp if (s->thread) {
2114 1a76625f 2018-10-22 stsp s->quit = 1;
2115 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2116 1a76625f 2018-10-22 stsp if (errcode)
2117 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2118 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2119 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2120 1a76625f 2018-10-22 stsp if (errcode)
2121 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2122 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2123 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2124 1a76625f 2018-10-22 stsp if (errcode)
2125 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2126 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2127 1a76625f 2018-10-22 stsp if (errcode)
2128 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2129 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2130 1a76625f 2018-10-22 stsp s->thread = NULL;
2133 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2134 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2135 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2138 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2139 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2140 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2143 9343a5fb 2018-06-23 stsp return err;
2146 9343a5fb 2018-06-23 stsp static const struct got_error *
2147 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2149 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2150 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2151 276b94a1 2020-11-13 naddy int errcode;
2153 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2155 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2156 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2157 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2159 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2160 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2161 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2163 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2164 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2165 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2166 1a76625f 2018-10-22 stsp free(s->start_id);
2167 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2168 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2169 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2170 1a76625f 2018-10-22 stsp return err;
2173 1a76625f 2018-10-22 stsp static const struct got_error *
2174 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2176 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2178 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2179 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2180 60493ae3 2019-06-20 stsp return NULL;
2183 60493ae3 2019-06-20 stsp static const struct got_error *
2184 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2186 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2187 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2188 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2190 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2191 f9686aa5 2020-03-27 stsp show_log_view(view);
2192 f9686aa5 2020-03-27 stsp update_panels();
2193 f9686aa5 2020-03-27 stsp doupdate();
2195 96e2b566 2019-07-08 stsp if (s->search_entry) {
2196 13add988 2019-10-15 stsp int errcode, ch;
2197 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2198 13add988 2019-10-15 stsp if (errcode)
2199 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2200 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2201 13add988 2019-10-15 stsp ch = wgetch(view->window);
2202 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2203 13add988 2019-10-15 stsp if (errcode)
2204 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2205 13add988 2019-10-15 stsp "pthread_mutex_lock");
2206 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2207 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2208 678cbce5 2019-07-28 stsp return NULL;
2210 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2211 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2213 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2214 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2215 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2216 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2217 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2219 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2220 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2222 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2225 60493ae3 2019-06-20 stsp while (1) {
2226 13add988 2019-10-15 stsp int have_match = 0;
2228 60493ae3 2019-06-20 stsp if (entry == NULL) {
2229 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2230 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2231 f9967bca 2020-03-27 stsp view->search_next_done =
2232 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2233 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2234 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2235 f801134a 2019-06-25 stsp return NULL;
2238 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2239 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2240 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2242 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2243 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2246 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2247 13add988 2019-10-15 stsp &view->regex);
2250 13add988 2019-10-15 stsp if (have_match) {
2251 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2252 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2256 96e2b566 2019-07-08 stsp s->search_entry = entry;
2257 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2258 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2260 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2263 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2264 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2265 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2266 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2268 60493ae3 2019-06-20 stsp return err;
2271 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2272 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2274 60493ae3 2019-06-20 stsp return err;
2279 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2281 60493ae3 2019-06-20 stsp return NULL;
2284 60493ae3 2019-06-20 stsp static const struct got_error *
2285 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2286 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2287 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2289 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2290 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2291 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2292 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2293 1a76625f 2018-10-22 stsp int errcode;
2295 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2296 f135c941 2020-02-20 stsp free(s->in_repo_path);
2297 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2298 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2299 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2302 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2303 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2304 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2306 fb2756b9 2018-08-04 stsp s->repo = repo;
2307 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2308 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2309 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2310 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2311 9cd7cbd1 2020-12-07 stsp goto done;
2314 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2315 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2316 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2317 5036bf37 2018-09-24 stsp goto done;
2319 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2321 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2322 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2323 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2324 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2326 11b20872 2019-11-08 stsp goto done;
2327 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2328 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2329 11b20872 2019-11-08 stsp if (err) {
2330 11b20872 2019-11-08 stsp free_colors(&s->colors);
2331 11b20872 2019-11-08 stsp goto done;
2333 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2334 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2335 11b20872 2019-11-08 stsp if (err) {
2336 11b20872 2019-11-08 stsp free_colors(&s->colors);
2337 11b20872 2019-11-08 stsp goto done;
2341 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2342 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2343 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2344 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2345 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2347 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2349 1a76625f 2018-10-22 stsp goto done;
2350 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2351 b672a97a 2020-01-27 stsp !s->log_branches);
2353 1a76625f 2018-10-22 stsp goto done;
2354 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2355 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2357 c5b78334 2020-01-12 stsp goto done;
2359 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2360 1a76625f 2018-10-22 stsp if (errcode) {
2361 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2362 1a76625f 2018-10-22 stsp goto done;
2364 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2365 7c1452c1 2020-03-26 stsp if (errcode) {
2366 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2367 7c1452c1 2020-03-26 stsp goto done;
2370 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2371 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2372 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2373 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2374 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2375 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2376 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2377 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2378 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2379 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2380 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2381 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2382 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2385 1a76625f 2018-10-22 stsp close_log_view(view);
2386 ba4f502b 2018-08-04 stsp return err;
2389 e5a0f69f 2018-08-18 stsp static const struct got_error *
2390 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2392 f2f6d207 2020-11-24 stsp const struct got_error *err;
2393 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2395 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2396 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2397 2b380cc8 2018-10-24 stsp &s->thread_args);
2398 2b380cc8 2018-10-24 stsp if (errcode)
2399 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2400 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2401 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2403 f2f6d207 2020-11-24 stsp return err;
2407 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2410 e5a0f69f 2018-08-18 stsp static const struct got_error *
2411 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2413 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2414 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2415 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2416 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2417 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2418 f3bc9f1d 2021-09-05 naddy int begin_x = 0, n;
2420 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2421 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2422 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2423 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2424 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2425 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2426 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2427 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2428 fb280deb 2021-08-30 stsp select_commit(s);
2430 528dedf3 2021-08-30 stsp return NULL;
2433 528dedf3 2021-08-30 stsp switch (ch) {
2435 1e37a5c2 2019-05-12 jcs s->quit = 1;
2438 1e37a5c2 2019-05-12 jcs case KEY_UP:
2441 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2442 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2444 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2445 1e37a5c2 2019-05-12 jcs s->selected--;
2447 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2448 912a3f79 2021-08-30 j select_commit(s);
2451 912a3f79 2021-08-30 j case KEY_HOME:
2452 912a3f79 2021-08-30 j s->selected = 0;
2453 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2454 2b779855 2020-12-05 naddy select_commit(s);
2456 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2457 a4292ac5 2019-05-12 jcs case CTRL('b'):
2458 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2460 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2461 1e37a5c2 2019-05-12 jcs s->selected = 0;
2463 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2464 2b779855 2020-12-05 naddy select_commit(s);
2467 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2470 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2471 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2473 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2474 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2475 1e37a5c2 2019-05-12 jcs s->selected++;
2477 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2481 912a3f79 2021-08-30 j select_commit(s);
2484 912a3f79 2021-08-30 j case KEY_END: {
2485 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2486 912a3f79 2021-08-30 j * traverse them all. */
2487 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2488 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2489 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2492 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2493 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2494 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2495 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2497 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2498 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2500 f3bc9f1d 2021-09-05 naddy if (n > 0)
2501 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2502 2b779855 2020-12-05 naddy select_commit(s);
2505 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2506 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2507 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2508 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2509 1e37a5c2 2019-05-12 jcs if (first == NULL)
2511 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2514 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2515 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2516 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2517 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2518 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2519 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2521 2b779855 2020-12-05 naddy select_commit(s);
2524 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2525 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2526 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2527 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2528 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2529 2b779855 2020-12-05 naddy select_commit(s);
2530 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2531 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2532 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2533 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2534 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2537 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2540 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2542 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2543 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2544 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2545 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2546 78756c87 2020-11-24 stsp view, s->repo);
2549 e78dc838 2020-12-04 stsp view->focussed = 0;
2550 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2551 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2552 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2554 1e37a5c2 2019-05-12 jcs return err;
2555 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2556 e78dc838 2020-12-04 stsp view->focus_child = 1;
2558 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2561 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2563 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2564 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2565 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2566 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2570 e78dc838 2020-12-04 stsp view->focussed = 0;
2571 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2572 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2573 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2575 1e37a5c2 2019-05-12 jcs return err;
2576 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2577 e78dc838 2020-12-04 stsp view->focus_child = 1;
2579 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2581 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2582 21355643 2020-12-06 stsp case CTRL('l'):
2584 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2585 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2587 21355643 2020-12-06 stsp err = stop_log_thread(s);
2589 74cfe85e 2020-10-20 stsp return err;
2590 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2591 21355643 2020-12-06 stsp char *parent_path;
2592 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2594 21355643 2020-12-06 stsp return err;
2595 21355643 2020-12-06 stsp free(s->in_repo_path);
2596 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2597 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2598 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2599 21355643 2020-12-06 stsp struct got_object_id *start_id;
2600 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2601 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2602 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2604 21355643 2020-12-06 stsp return err;
2605 21355643 2020-12-06 stsp free(s->start_id);
2606 21355643 2020-12-06 stsp s->start_id = start_id;
2607 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2608 21355643 2020-12-06 stsp } else /* 'B' */
2609 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2611 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2612 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2614 21355643 2020-12-06 stsp return err;
2615 51a10b52 2020-12-26 stsp tog_free_refs();
2616 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
2618 ca51c541 2020-12-07 stsp return err;
2619 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2620 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2622 d01904d4 2019-06-25 stsp return err;
2623 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2624 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2626 b672a97a 2020-01-27 stsp return err;
2627 21355643 2020-12-06 stsp free_commits(&s->commits);
2628 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2629 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2630 21355643 2020-12-06 stsp s->selected_entry = NULL;
2631 21355643 2020-12-06 stsp s->selected = 0;
2632 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2633 21355643 2020-12-06 stsp s->quit = 0;
2634 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2637 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2638 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2639 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2640 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2641 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2642 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2643 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2644 6458efa5 2020-11-24 stsp if (err) {
2645 6458efa5 2020-11-24 stsp view_close(ref_view);
2646 6458efa5 2020-11-24 stsp return err;
2648 e78dc838 2020-12-04 stsp view->focussed = 0;
2649 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2650 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2651 6458efa5 2020-11-24 stsp err = view_close_child(view);
2653 6458efa5 2020-11-24 stsp return err;
2654 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2655 e78dc838 2020-12-04 stsp view->focus_child = 1;
2657 6458efa5 2020-11-24 stsp *new_view = ref_view;
2663 80ddbec8 2018-04-29 stsp return err;
2666 4ed7e80c 2018-05-20 stsp static const struct got_error *
2667 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2669 c2db6724 2019-01-04 stsp const struct got_error *error;
2671 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2672 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2673 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2675 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2676 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2678 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2679 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2681 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2682 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2684 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2685 c2db6724 2019-01-04 stsp if (error != NULL)
2686 c2db6724 2019-01-04 stsp return error;
2688 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2689 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2691 c2db6724 2019-01-04 stsp return NULL;
2694 a915003a 2019-02-05 stsp static void
2695 a915003a 2019-02-05 stsp init_curses(void)
2697 a915003a 2019-02-05 stsp initscr();
2699 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2702 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2703 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2704 a915003a 2019-02-05 stsp curs_set(0);
2705 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2706 6d17833f 2019-11-08 stsp start_color();
2707 6d17833f 2019-11-08 stsp use_default_colors();
2709 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2710 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2711 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2714 c2db6724 2019-01-04 stsp static const struct got_error *
2715 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2716 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2718 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2720 f135c941 2020-02-20 stsp if (argc == 0) {
2721 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2722 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2723 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2724 f135c941 2020-02-20 stsp return NULL;
2727 f135c941 2020-02-20 stsp if (worktree) {
2728 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2731 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2733 f135c941 2020-02-20 stsp return err;
2734 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2735 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2736 bfd61697 2020-11-14 stsp p) == -1) {
2737 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2738 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2742 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2744 f135c941 2020-02-20 stsp return err;
2747 f135c941 2020-02-20 stsp static const struct got_error *
2748 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2750 80ddbec8 2018-04-29 stsp const struct got_error *error;
2751 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2752 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2753 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2754 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2755 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2756 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2757 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2758 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2759 04cc582a 2018-08-01 stsp struct tog_view *view;
2761 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2762 80ddbec8 2018-04-29 stsp switch (ch) {
2764 b672a97a 2020-01-27 stsp log_branches = 1;
2767 80ddbec8 2018-04-29 stsp start_commit = optarg;
2770 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2771 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2772 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2776 17020d27 2019-03-07 stsp usage_log();
2777 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2781 80ddbec8 2018-04-29 stsp argc -= optind;
2782 80ddbec8 2018-04-29 stsp argv += optind;
2784 f135c941 2020-02-20 stsp if (argc > 1)
2785 f135c941 2020-02-20 stsp usage_log();
2787 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2788 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2789 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2790 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2791 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2792 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2793 c156c7a4 2020-12-18 stsp goto done;
2794 a1fbf39a 2019-08-11 stsp if (worktree)
2795 6962eb72 2020-02-20 stsp repo_path =
2796 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2798 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2799 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2800 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2801 c156c7a4 2020-12-18 stsp goto done;
2805 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2806 80ddbec8 2018-04-29 stsp if (error != NULL)
2807 ecb28ae0 2018-07-16 stsp goto done;
2809 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2810 f135c941 2020-02-20 stsp repo, worktree);
2811 f135c941 2020-02-20 stsp if (error)
2812 f135c941 2020-02-20 stsp goto done;
2814 f135c941 2020-02-20 stsp init_curses();
2816 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2817 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2818 c02c541e 2019-03-29 stsp if (error)
2819 c02c541e 2019-03-29 stsp goto done;
2821 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2822 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2823 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
2824 87670572 2020-12-26 naddy if (error)
2825 87670572 2020-12-26 naddy goto done;
2828 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2829 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2830 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2831 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2832 d8f38dc4 2020-12-05 stsp if (error)
2833 d8f38dc4 2020-12-05 stsp goto done;
2834 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2836 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2837 d8f38dc4 2020-12-05 stsp if (error == NULL)
2838 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2839 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2840 d8f38dc4 2020-12-05 stsp goto done;
2841 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2842 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2843 d8f38dc4 2020-12-05 stsp if (error)
2844 d8f38dc4 2020-12-05 stsp goto done;
2847 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2848 04cc582a 2018-08-01 stsp if (view == NULL) {
2849 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2850 04cc582a 2018-08-01 stsp goto done;
2852 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2853 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2854 ba4f502b 2018-08-04 stsp if (error)
2855 ba4f502b 2018-08-04 stsp goto done;
2856 2fc00ff4 2019-08-31 stsp if (worktree) {
2857 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2858 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2859 2fc00ff4 2019-08-31 stsp worktree = NULL;
2861 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2863 f135c941 2020-02-20 stsp free(in_repo_path);
2864 ecb28ae0 2018-07-16 stsp free(repo_path);
2865 ecb28ae0 2018-07-16 stsp free(cwd);
2866 899d86c2 2018-05-10 stsp free(start_id);
2867 d8f38dc4 2020-12-05 stsp free(label);
2869 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2870 1d0f4054 2021-06-17 stsp if (repo) {
2871 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2872 1d0f4054 2021-06-17 stsp if (error == NULL)
2873 1d0f4054 2021-06-17 stsp error = close_err;
2875 ec142235 2019-03-07 stsp if (worktree)
2876 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2877 51a10b52 2020-12-26 stsp tog_free_refs();
2878 80ddbec8 2018-04-29 stsp return error;
2881 4ed7e80c 2018-05-20 stsp __dead static void
2882 9f7d7167 2018-04-29 stsp usage_diff(void)
2885 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2886 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2890 6d17833f 2019-11-08 stsp static int
2891 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2892 41605754 2020-11-12 stsp regmatch_t *regmatch)
2894 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2897 f26dddb7 2019-11-08 stsp struct tog_color *
2898 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2900 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2902 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2903 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2904 6d17833f 2019-11-08 stsp return tc;
2907 6d17833f 2019-11-08 stsp return NULL;
2910 4ed7e80c 2018-05-20 stsp static const struct got_error *
2911 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2912 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2914 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2915 41605754 2020-11-12 stsp wchar_t *wline;
2916 41605754 2020-11-12 stsp int width;
2919 41605754 2020-11-12 stsp *wtotal = 0;
2921 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2922 41605754 2020-11-12 stsp if (s == NULL)
2923 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2925 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2926 41605754 2020-11-12 stsp if (err) {
2928 41605754 2020-11-12 stsp return err;
2930 41605754 2020-11-12 stsp waddwstr(window, wline);
2931 41605754 2020-11-12 stsp free(wline);
2933 41605754 2020-11-12 stsp wlimit -= width;
2934 41605754 2020-11-12 stsp *wtotal += width;
2936 41605754 2020-11-12 stsp if (wlimit > 0) {
2937 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2938 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2939 41605754 2020-11-12 stsp if (s == NULL) {
2940 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2942 41605754 2020-11-12 stsp return err;
2944 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2945 41605754 2020-11-12 stsp if (err) {
2947 41605754 2020-11-12 stsp return err;
2949 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2950 41605754 2020-11-12 stsp waddwstr(window, wline);
2951 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2952 41605754 2020-11-12 stsp free(wline);
2954 41605754 2020-11-12 stsp wlimit -= width;
2955 41605754 2020-11-12 stsp *wtotal += width;
2958 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2959 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2960 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2962 41605754 2020-11-12 stsp return err;
2963 41605754 2020-11-12 stsp waddwstr(window, wline);
2964 41605754 2020-11-12 stsp free(wline);
2965 41605754 2020-11-12 stsp *wtotal += width;
2968 41605754 2020-11-12 stsp return NULL;
2971 41605754 2020-11-12 stsp static const struct got_error *
2972 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2974 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2975 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2976 61e69b96 2018-05-20 stsp const struct got_error *err;
2977 fe621944 2020-11-10 stsp int nprinted = 0;
2978 b304db33 2018-05-20 stsp char *line;
2979 826082fe 2020-12-10 stsp size_t linesize = 0;
2980 826082fe 2020-12-10 stsp ssize_t linelen;
2981 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2982 61e69b96 2018-05-20 stsp wchar_t *wline;
2983 e0b650dd 2018-05-20 stsp int width;
2984 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2985 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2986 fe621944 2020-11-10 stsp off_t line_offset;
2988 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2989 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2990 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2992 f7d12f7e 2018-08-01 stsp werase(view->window);
2994 a3404814 2018-09-02 stsp if (header) {
2995 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2996 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2997 135a2da0 2020-11-11 stsp header) == -1)
2998 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2999 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3000 135a2da0 2020-11-11 stsp free(line);
3002 a3404814 2018-09-02 stsp return err;
3004 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3005 a3404814 2018-09-02 stsp wstandout(view->window);
3006 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3007 e54cc94a 2020-11-11 stsp free(wline);
3008 e54cc94a 2020-11-11 stsp wline = NULL;
3009 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3010 a3404814 2018-09-02 stsp wstandend(view->window);
3011 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3012 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3014 a3404814 2018-09-02 stsp if (max_lines <= 1)
3015 a3404814 2018-09-02 stsp return NULL;
3016 a3404814 2018-09-02 stsp max_lines--;
3019 89f1a395 2020-12-01 naddy s->eof = 0;
3020 826082fe 2020-12-10 stsp line = NULL;
3021 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3022 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3023 826082fe 2020-12-10 stsp if (linelen == -1) {
3024 826082fe 2020-12-10 stsp if (feof(s->f)) {
3025 826082fe 2020-12-10 stsp s->eof = 1;
3028 826082fe 2020-12-10 stsp free(line);
3029 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3032 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3034 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3035 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3036 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3037 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3038 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3039 41605754 2020-11-12 stsp view->window, regmatch);
3040 41605754 2020-11-12 stsp if (err) {
3041 41605754 2020-11-12 stsp free(line);
3042 41605754 2020-11-12 stsp return err;
3045 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3046 41605754 2020-11-12 stsp if (err) {
3047 41605754 2020-11-12 stsp free(line);
3048 41605754 2020-11-12 stsp return err;
3050 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3051 41605754 2020-11-12 stsp free(wline);
3052 41605754 2020-11-12 stsp wline = NULL;
3055 6d17833f 2019-11-08 stsp wattr_off(view->window,
3056 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3057 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3058 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3059 fe621944 2020-11-10 stsp nprinted++;
3061 826082fe 2020-12-10 stsp free(line);
3062 fe621944 2020-11-10 stsp if (nprinted >= 1)
3063 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3064 89f1a395 2020-12-01 naddy (nprinted - 1);
3066 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3068 1a57306a 2018-09-02 stsp view_vborder(view);
3070 89f1a395 2020-12-01 naddy if (s->eof) {
3071 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3072 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3073 c3e9aa98 2019-05-13 jcs nprinted++;
3076 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3078 c3e9aa98 2019-05-13 jcs return err;
3081 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3082 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3083 e54cc94a 2020-11-11 stsp free(wline);
3084 e54cc94a 2020-11-11 stsp wline = NULL;
3085 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3088 26ed57b2 2018-05-19 stsp return NULL;
3091 abd2672a 2018-12-23 stsp static char *
3092 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3094 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3095 09867e48 2019-08-13 stsp char *p, *s;
3097 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3098 09867e48 2019-08-13 stsp if (tm == NULL)
3099 09867e48 2019-08-13 stsp return NULL;
3100 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3101 09867e48 2019-08-13 stsp if (s == NULL)
3102 09867e48 2019-08-13 stsp return NULL;
3103 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3105 abd2672a 2018-12-23 stsp *p = '\0';
3109 4ed7e80c 2018-05-20 stsp static const struct got_error *
3110 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3111 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3113 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3114 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3115 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3116 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3118 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3119 0208f208 2020-05-05 stsp if (qid != NULL) {
3120 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3121 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3122 d7b5a0e8 2022-04-20 stsp &qid->id);
3124 0208f208 2020-05-05 stsp return err;
3126 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3127 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3128 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3129 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3130 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3132 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3136 0208f208 2020-05-05 stsp if (tree_id1) {
3137 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3139 0208f208 2020-05-05 stsp goto done;
3142 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3143 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3145 0208f208 2020-05-05 stsp goto done;
3147 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3148 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3150 0208f208 2020-05-05 stsp if (tree1)
3151 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3152 0208f208 2020-05-05 stsp if (tree2)
3153 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3154 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3155 0208f208 2020-05-05 stsp return err;
3158 0208f208 2020-05-05 stsp static const struct got_error *
3159 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3163 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3164 fe621944 2020-11-10 stsp if (p == NULL)
3165 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3166 fe621944 2020-11-10 stsp *line_offsets = p;
3167 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3168 fe621944 2020-11-10 stsp (*nlines)++;
3169 fe621944 2020-11-10 stsp return NULL;
3172 fe621944 2020-11-10 stsp static const struct got_error *
3173 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3174 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3175 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3177 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3178 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3179 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3180 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3181 45d799e2 2018-12-23 stsp time_t committer_time;
3182 45d799e2 2018-12-23 stsp const char *author, *committer;
3183 8b473291 2019-02-21 stsp char *refs_str = NULL;
3184 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3185 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3186 fe621944 2020-11-10 stsp off_t outoff = 0;
3189 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3191 8b473291 2019-02-21 stsp if (refs) {
3192 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3194 8b473291 2019-02-21 stsp return err;
3197 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3199 abd2672a 2018-12-23 stsp return err;
3201 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3202 15a94983 2018-12-23 stsp if (err) {
3203 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3204 15a94983 2018-12-23 stsp goto done;
3207 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3209 fe621944 2020-11-10 stsp goto done;
3211 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3212 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3213 fe621944 2020-11-10 stsp if (n < 0) {
3214 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3215 abd2672a 2018-12-23 stsp goto done;
3217 fe621944 2020-11-10 stsp outoff += n;
3218 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3220 fe621944 2020-11-10 stsp goto done;
3222 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3223 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3224 fe621944 2020-11-10 stsp if (n < 0) {
3225 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3226 abd2672a 2018-12-23 stsp goto done;
3228 fe621944 2020-11-10 stsp outoff += n;
3229 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3231 fe621944 2020-11-10 stsp goto done;
3233 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3234 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3235 fe621944 2020-11-10 stsp if (datestr) {
3236 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3237 fe621944 2020-11-10 stsp if (n < 0) {
3238 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3239 fe621944 2020-11-10 stsp goto done;
3241 fe621944 2020-11-10 stsp outoff += n;
3242 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3244 fe621944 2020-11-10 stsp goto done;
3246 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3247 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3248 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3249 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3250 fe621944 2020-11-10 stsp if (n < 0) {
3251 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3252 fe621944 2020-11-10 stsp goto done;
3254 fe621944 2020-11-10 stsp outoff += n;
3255 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3257 fe621944 2020-11-10 stsp goto done;
3259 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3260 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3261 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3262 9f98ca05 2021-09-24 stsp int pn = 1;
3263 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3264 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3265 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3267 9f98ca05 2021-09-24 stsp goto done;
3268 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3269 9f98ca05 2021-09-24 stsp if (n < 0) {
3270 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3271 9f98ca05 2021-09-24 stsp goto done;
3273 9f98ca05 2021-09-24 stsp outoff += n;
3274 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3276 9f98ca05 2021-09-24 stsp goto done;
3277 9f98ca05 2021-09-24 stsp free(id_str);
3278 9f98ca05 2021-09-24 stsp id_str = NULL;
3282 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3284 5943eee2 2019-08-13 stsp goto done;
3285 fe621944 2020-11-10 stsp s = logmsg;
3286 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3287 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3288 fe621944 2020-11-10 stsp if (n < 0) {
3289 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3290 fe621944 2020-11-10 stsp goto done;
3292 fe621944 2020-11-10 stsp outoff += n;
3293 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3295 fe621944 2020-11-10 stsp goto done;
3298 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3300 0208f208 2020-05-05 stsp goto done;
3301 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3302 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3303 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3304 fe621944 2020-11-10 stsp if (n < 0) {
3305 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3306 fe621944 2020-11-10 stsp goto done;
3308 fe621944 2020-11-10 stsp outoff += n;
3309 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3311 fe621944 2020-11-10 stsp goto done;
3312 0208f208 2020-05-05 stsp free((char *)pe->path);
3313 0208f208 2020-05-05 stsp free(pe->data);
3316 0208f208 2020-05-05 stsp fputc('\n', outfile);
3318 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3320 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3321 abd2672a 2018-12-23 stsp free(id_str);
3322 5943eee2 2019-08-13 stsp free(logmsg);
3323 8b473291 2019-02-21 stsp free(refs_str);
3324 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3325 7510f233 2020-08-09 stsp if (err) {
3326 ae6a6978 2020-08-09 stsp free(*line_offsets);
3327 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3328 ae6a6978 2020-08-09 stsp *nlines = 0;
3330 fe621944 2020-11-10 stsp return err;
3333 abd2672a 2018-12-23 stsp static const struct got_error *
3334 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3336 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3337 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3338 15a94983 2018-12-23 stsp int obj_type;
3340 fe621944 2020-11-10 stsp free(s->line_offsets);
3341 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3342 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3343 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3344 fe621944 2020-11-10 stsp s->nlines = 0;
3346 511a516b 2018-05-19 stsp f = got_opentemp();
3347 48ae06ee 2018-10-18 stsp if (f == NULL) {
3348 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3349 48ae06ee 2018-10-18 stsp goto done;
3351 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3352 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3353 fb43ecf1 2019-02-11 stsp goto done;
3357 15a94983 2018-12-23 stsp if (s->id1)
3358 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3360 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3362 15a94983 2018-12-23 stsp goto done;
3364 15a94983 2018-12-23 stsp switch (obj_type) {
3365 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3366 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3367 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3368 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3370 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3371 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3372 67b631c9 2021-10-10 stsp s->id1, s->id2, NULL, "", "", s->diff_context,
3373 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3375 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3376 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3377 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3378 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3379 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3381 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3383 3ffacbe1 2020-02-02 tracey goto done;
3384 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3385 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3386 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3387 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3388 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3389 f44b1f58 2020-02-02 tracey if (err)
3390 f44b1f58 2020-02-02 tracey goto done;
3391 f44b1f58 2020-02-02 tracey } else {
3392 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3393 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3394 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3395 fe621944 2020-11-10 stsp err = write_commit_info(
3396 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3397 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3398 f44b1f58 2020-02-02 tracey if (err)
3399 f44b1f58 2020-02-02 tracey goto done;
3404 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3406 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3407 67b631c9 2021-10-10 stsp s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3408 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3412 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3415 f44b1f58 2020-02-02 tracey if (err)
3416 f44b1f58 2020-02-02 tracey goto done;
3418 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3419 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3420 48ae06ee 2018-10-18 stsp return err;
3423 f5215bb9 2019-02-22 stsp static void
3424 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3426 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3427 f5215bb9 2019-02-22 stsp update_panels();
3428 f5215bb9 2019-02-22 stsp doupdate();
3431 f44b1f58 2020-02-02 tracey static const struct got_error *
3432 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3434 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3436 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3437 f44b1f58 2020-02-02 tracey return NULL;
3440 f44b1f58 2020-02-02 tracey static const struct got_error *
3441 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3443 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3444 f44b1f58 2020-02-02 tracey int lineno;
3445 826082fe 2020-12-10 stsp char *line = NULL;
3446 826082fe 2020-12-10 stsp size_t linesize = 0;
3447 826082fe 2020-12-10 stsp ssize_t linelen;
3449 f44b1f58 2020-02-02 tracey if (!view->searching) {
3450 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3451 f44b1f58 2020-02-02 tracey return NULL;
3454 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3455 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3456 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3458 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3460 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
3462 f44b1f58 2020-02-02 tracey while (1) {
3463 f44b1f58 2020-02-02 tracey off_t offset;
3465 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3466 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3467 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3471 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3472 f44b1f58 2020-02-02 tracey lineno = 1;
3474 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3477 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3478 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3479 f44b1f58 2020-02-02 tracey free(line);
3480 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3482 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3483 826082fe 2020-12-10 stsp if (linelen != -1 &&
3484 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3485 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3486 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3489 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3490 f44b1f58 2020-02-02 tracey lineno++;
3492 f44b1f58 2020-02-02 tracey lineno--;
3494 826082fe 2020-12-10 stsp free(line);
3496 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3497 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3498 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3501 f44b1f58 2020-02-02 tracey return NULL;
3504 48ae06ee 2018-10-18 stsp static const struct got_error *
3505 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3506 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3507 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3508 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3510 48ae06ee 2018-10-18 stsp const struct got_error *err;
3511 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3513 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3514 15a94983 2018-12-23 stsp int type1, type2;
3515 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3517 15a94983 2018-12-23 stsp return err;
3518 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3520 15a94983 2018-12-23 stsp return err;
3522 15a94983 2018-12-23 stsp if (type1 != type2)
3523 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3525 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3526 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3527 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3528 f44b1f58 2020-02-02 tracey s->repo = repo;
3529 f44b1f58 2020-02-02 tracey s->id1 = id1;
3530 f44b1f58 2020-02-02 tracey s->id2 = id2;
3531 3dbaef42 2020-11-24 stsp s->label1 = label1;
3532 3dbaef42 2020-11-24 stsp s->label2 = label2;
3534 15a94983 2018-12-23 stsp if (id1) {
3535 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3536 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3537 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3539 5465d566 2020-02-01 tracey s->id1 = NULL;
3541 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3542 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3543 5465d566 2020-02-01 tracey free(s->id1);
3544 5465d566 2020-02-01 tracey s->id1 = NULL;
3545 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3547 5465d566 2020-02-01 tracey s->f = NULL;
3548 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3549 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3550 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3551 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3552 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3553 5465d566 2020-02-01 tracey s->log_view = log_view;
3554 5465d566 2020-02-01 tracey s->repo = repo;
3556 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3557 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3558 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3559 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3560 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3562 6d17833f 2019-11-08 stsp return err;
3563 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3564 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3565 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3566 6d17833f 2019-11-08 stsp if (err) {
3567 5465d566 2020-02-01 tracey free_colors(&s->colors);
3568 6d17833f 2019-11-08 stsp return err;
3570 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3571 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3572 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3573 6d17833f 2019-11-08 stsp if (err) {
3574 5465d566 2020-02-01 tracey free_colors(&s->colors);
3575 6d17833f 2019-11-08 stsp return err;
3578 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3579 9f98ca05 2021-09-24 stsp "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3580 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3581 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3582 6d17833f 2019-11-08 stsp if (err) {
3583 5465d566 2020-02-01 tracey free_colors(&s->colors);
3584 6d17833f 2019-11-08 stsp return err;
3587 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3588 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3589 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3590 11b20872 2019-11-08 stsp if (err) {
3591 5465d566 2020-02-01 tracey free_colors(&s->colors);
3592 11b20872 2019-11-08 stsp return err;
3595 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3596 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3597 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3598 11b20872 2019-11-08 stsp if (err) {
3599 5465d566 2020-02-01 tracey free_colors(&s->colors);
3600 11b20872 2019-11-08 stsp return err;
3604 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3605 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3606 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3608 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3609 fe621944 2020-11-10 stsp s->nlines = 0;
3610 5465d566 2020-02-01 tracey err = create_diff(s);
3611 48ae06ee 2018-10-18 stsp if (err) {
3612 5465d566 2020-02-01 tracey free(s->id1);
3613 5465d566 2020-02-01 tracey s->id1 = NULL;
3614 5465d566 2020-02-01 tracey free(s->id2);
3615 5465d566 2020-02-01 tracey s->id2 = NULL;
3616 78756c87 2020-11-24 stsp free_colors(&s->colors);
3617 48ae06ee 2018-10-18 stsp return err;
3620 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3621 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3622 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3623 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3624 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3626 5dc9f4bc 2018-08-04 stsp return NULL;
3629 e5a0f69f 2018-08-18 stsp static const struct got_error *
3630 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3632 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3633 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3635 5465d566 2020-02-01 tracey free(s->id1);
3636 5465d566 2020-02-01 tracey s->id1 = NULL;
3637 5465d566 2020-02-01 tracey free(s->id2);
3638 5465d566 2020-02-01 tracey s->id2 = NULL;
3639 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3640 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3641 5465d566 2020-02-01 tracey free_colors(&s->colors);
3642 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3643 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3644 fe621944 2020-11-10 stsp s->nlines = 0;
3645 e5a0f69f 2018-08-18 stsp return err;
3648 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3649 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3651 a3404814 2018-09-02 stsp const struct got_error *err;
3652 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3653 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3654 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3656 a3404814 2018-09-02 stsp if (s->id1) {
3657 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3659 a3404814 2018-09-02 stsp return err;
3660 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3662 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3664 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3666 a3404814 2018-09-02 stsp return err;
3667 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3669 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3670 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3671 a3404814 2018-09-02 stsp free(id_str1);
3672 a3404814 2018-09-02 stsp free(id_str2);
3673 a3404814 2018-09-02 stsp return err;
3675 a3404814 2018-09-02 stsp free(id_str1);
3676 a3404814 2018-09-02 stsp free(id_str2);
3678 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3679 267bb3b8 2021-08-01 stsp free(header);
3680 267bb3b8 2021-08-01 stsp return err;
3683 15a087fe 2019-02-21 stsp static const struct got_error *
3684 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3685 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3687 d7a04538 2019-02-21 stsp const struct got_error *err;
3688 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3689 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3690 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3692 15a087fe 2019-02-21 stsp free(s->id2);
3693 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3694 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3695 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3697 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3699 d7a04538 2019-02-21 stsp return err;
3700 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3701 15a087fe 2019-02-21 stsp free(s->id1);
3702 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3703 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3704 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3705 15a087fe 2019-02-21 stsp return NULL;
3708 0cf4efb1 2018-09-29 stsp static const struct got_error *
3709 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3711 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3712 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3713 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3714 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3715 826082fe 2020-12-10 stsp char *line = NULL;
3716 826082fe 2020-12-10 stsp size_t linesize = 0;
3717 826082fe 2020-12-10 stsp ssize_t linelen;
3720 e5a0f69f 2018-08-18 stsp switch (ch) {
3723 3dbaef42 2020-11-24 stsp if (ch == 'a')
3724 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3725 3dbaef42 2020-11-24 stsp if (ch == 'w')
3726 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3727 64453f7e 2020-11-21 stsp wclear(view->window);
3728 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3729 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3730 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3731 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3732 64453f7e 2020-11-21 stsp err = create_diff(s);
3735 912a3f79 2021-08-30 j case KEY_HOME:
3736 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3739 912a3f79 2021-08-30 j case KEY_END:
3743 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3747 1e37a5c2 2019-05-12 jcs case KEY_UP:
3748 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3749 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3750 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3752 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3753 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3754 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3757 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3758 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3759 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3762 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3763 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3764 1e37a5c2 2019-05-12 jcs if (!s->eof)
3765 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3767 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3768 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3770 00ba99a7 2019-05-12 jcs if (s->eof)
3773 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3774 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3775 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3776 826082fe 2020-12-10 stsp if (linelen == -1) {
3777 826082fe 2020-12-10 stsp if (feof(s->f)) {
3778 826082fe 2020-12-10 stsp s->eof = 1;
3780 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3784 826082fe 2020-12-10 stsp free(line);
3787 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3788 1e37a5c2 2019-05-12 jcs s->diff_context--;
3789 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3790 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3791 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3792 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3793 27829c9e 2020-11-21 stsp s->nlines) {
3794 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3795 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3800 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3801 1e37a5c2 2019-05-12 jcs s->diff_context++;
3802 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3803 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3804 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3809 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3811 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3812 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3814 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3818 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3821 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3825 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3826 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3827 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3829 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3830 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3834 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3836 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3837 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3839 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3843 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3846 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3850 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3851 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3852 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3854 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3855 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3861 bcbd79e2 2018-08-19 stsp return err;
3864 4ed7e80c 2018-05-20 stsp static const struct got_error *
3865 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3867 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3868 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3869 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3870 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3871 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3872 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3873 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3874 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3875 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3876 3dbaef42 2020-11-24 stsp const char *errstr;
3877 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3879 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3880 26ed57b2 2018-05-19 stsp switch (ch) {
3882 64453f7e 2020-11-21 stsp force_text_diff = 1;
3885 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3887 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3888 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
3889 5a20d08d 2022-02-09 op errstr, errstr);
3891 09b5bff8 2020-02-23 naddy case 'r':
3892 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3893 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3894 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3896 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3899 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3902 17020d27 2019-03-07 stsp usage_diff();
3903 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3907 26ed57b2 2018-05-19 stsp argc -= optind;
3908 26ed57b2 2018-05-19 stsp argv += optind;
3910 26ed57b2 2018-05-19 stsp if (argc == 0) {
3911 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3912 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3913 15a94983 2018-12-23 stsp id_str1 = argv[0];
3914 15a94983 2018-12-23 stsp id_str2 = argv[1];
3916 26ed57b2 2018-05-19 stsp usage_diff();
3918 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3919 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3920 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3921 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3922 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3923 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3924 c156c7a4 2020-12-18 stsp goto done;
3925 a273ac94 2020-02-23 naddy if (worktree)
3926 a273ac94 2020-02-23 naddy repo_path =
3927 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3929 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3930 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3931 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3932 c156c7a4 2020-12-18 stsp goto done;
3936 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3937 eb6600df 2019-01-04 stsp if (error)
3938 eb6600df 2019-01-04 stsp goto done;
3940 a273ac94 2020-02-23 naddy init_curses();
3942 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3943 51a10b52 2020-12-26 stsp if (error)
3944 51a10b52 2020-12-26 stsp goto done;
3946 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3947 26ed57b2 2018-05-19 stsp if (error)
3948 26ed57b2 2018-05-19 stsp goto done;
3950 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3951 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3952 26ed57b2 2018-05-19 stsp if (error)
3953 26ed57b2 2018-05-19 stsp goto done;
3955 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3956 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3957 26ed57b2 2018-05-19 stsp if (error)
3958 26ed57b2 2018-05-19 stsp goto done;
3960 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3961 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3962 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3963 ea5e7bb5 2018-08-01 stsp goto done;
3965 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3966 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3967 5dc9f4bc 2018-08-04 stsp if (error)
3968 5dc9f4bc 2018-08-04 stsp goto done;
3969 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3971 3dbaef42 2020-11-24 stsp free(label1);
3972 3dbaef42 2020-11-24 stsp free(label2);
3973 c02c541e 2019-03-29 stsp free(repo_path);
3974 a273ac94 2020-02-23 naddy free(cwd);
3975 1d0f4054 2021-06-17 stsp if (repo) {
3976 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3977 1d0f4054 2021-06-17 stsp if (error == NULL)
3978 1d0f4054 2021-06-17 stsp error = close_err;
3980 a273ac94 2020-02-23 naddy if (worktree)
3981 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3982 51a10b52 2020-12-26 stsp tog_free_refs();
3983 26ed57b2 2018-05-19 stsp return error;
3986 4ed7e80c 2018-05-20 stsp __dead static void
3987 9f7d7167 2018-04-29 stsp usage_blame(void)
3990 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3991 9f7d7167 2018-04-29 stsp getprogname());
3995 84451b3e 2018-07-10 stsp struct tog_blame_line {
3996 84451b3e 2018-07-10 stsp int annotated;
3997 84451b3e 2018-07-10 stsp struct got_object_id *id;
4000 4ed7e80c 2018-05-20 stsp static const struct got_error *
4001 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4003 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4004 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4005 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4006 84451b3e 2018-07-10 stsp const struct got_error *err;
4007 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4008 826082fe 2020-12-10 stsp char *line = NULL;
4009 826082fe 2020-12-10 stsp size_t linesize = 0;
4010 826082fe 2020-12-10 stsp ssize_t linelen;
4011 84451b3e 2018-07-10 stsp wchar_t *wline;
4012 27a741e5 2019-09-11 stsp int width;
4013 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4014 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4015 ab089a2a 2018-07-12 stsp char *id_str;
4016 11b20872 2019-11-08 stsp struct tog_color *tc;
4018 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4020 ab089a2a 2018-07-12 stsp return err;
4022 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4023 f7d12f7e 2018-08-01 stsp werase(view->window);
4025 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4026 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4027 ab089a2a 2018-07-12 stsp free(id_str);
4028 ab089a2a 2018-07-12 stsp return err;
4031 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4032 ab089a2a 2018-07-12 stsp free(line);
4033 2550e4c3 2018-07-13 stsp line = NULL;
4035 1cae65b4 2019-09-22 stsp return err;
4036 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4037 a3404814 2018-09-02 stsp wstandout(view->window);
4038 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4040 11b20872 2019-11-08 stsp wattr_on(view->window,
4041 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4042 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4044 11b20872 2019-11-08 stsp wattr_off(view->window,
4045 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4046 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4047 a3404814 2018-09-02 stsp wstandend(view->window);
4048 2550e4c3 2018-07-13 stsp free(wline);
4049 2550e4c3 2018-07-13 stsp wline = NULL;
4050 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4051 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4053 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4054 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4055 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4056 ab089a2a 2018-07-12 stsp free(id_str);
4057 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4059 ab089a2a 2018-07-12 stsp free(id_str);
4060 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4061 3f60a8ef 2018-07-10 stsp free(line);
4062 2550e4c3 2018-07-13 stsp line = NULL;
4064 3f60a8ef 2018-07-10 stsp return err;
4065 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4066 2550e4c3 2018-07-13 stsp free(wline);
4067 2550e4c3 2018-07-13 stsp wline = NULL;
4068 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4069 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4071 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4072 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4073 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4074 826082fe 2020-12-10 stsp if (linelen == -1) {
4075 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4076 826082fe 2020-12-10 stsp s->eof = 1;
4079 84451b3e 2018-07-10 stsp free(line);
4080 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4082 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4085 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4086 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4088 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4089 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4090 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4091 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4092 27a741e5 2019-09-11 stsp !(view->focussed &&
4093 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4094 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4095 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4096 8d0fe45a 2019-08-12 stsp char *id_str;
4097 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4098 8d0fe45a 2019-08-12 stsp if (err) {
4099 8d0fe45a 2019-08-12 stsp free(line);
4100 8d0fe45a 2019-08-12 stsp return err;
4102 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4104 11b20872 2019-11-08 stsp wattr_on(view->window,
4105 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4106 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4108 11b20872 2019-11-08 stsp wattr_off(view->window,
4109 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4110 8d0fe45a 2019-08-12 stsp free(id_str);
4111 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4113 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4114 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4117 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4118 ee41ec32 2018-07-10 stsp prev_id = NULL;
4121 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4122 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4123 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4125 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4126 41605754 2020-11-12 stsp width = 9;
4127 41605754 2020-11-12 stsp wline = wcsdup(L"");
4128 41605754 2020-11-12 stsp if (wline == NULL) {
4129 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4130 41605754 2020-11-12 stsp free(line);
4131 41605754 2020-11-12 stsp return err;
4133 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4134 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4135 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4136 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4137 41605754 2020-11-12 stsp view->window, regmatch);
4138 41605754 2020-11-12 stsp if (err) {
4139 41605754 2020-11-12 stsp free(line);
4140 41605754 2020-11-12 stsp return err;
4142 41605754 2020-11-12 stsp width += 9;
4144 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4145 41605754 2020-11-12 stsp view->ncols - 9, 9);
4146 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4147 41605754 2020-11-12 stsp free(wline);
4148 41605754 2020-11-12 stsp wline = NULL;
4149 41605754 2020-11-12 stsp width += 9;
4152 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4153 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4154 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4155 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4157 826082fe 2020-12-10 stsp free(line);
4158 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4160 1a57306a 2018-09-02 stsp view_vborder(view);
4162 84451b3e 2018-07-10 stsp return NULL;
4165 84451b3e 2018-07-10 stsp static const struct got_error *
4166 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4167 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4169 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4170 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4171 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4172 1a76625f 2018-10-22 stsp int errcode;
4174 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4175 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4176 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4178 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4179 1a76625f 2018-10-22 stsp if (errcode)
4180 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4182 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4183 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4184 d68a0a7d 2018-07-10 stsp goto done;
4187 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4188 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4190 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4191 d68a0a7d 2018-07-10 stsp if (line->annotated)
4192 d68a0a7d 2018-07-10 stsp goto done;
4194 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4195 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4196 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4197 84451b3e 2018-07-10 stsp goto done;
4199 84451b3e 2018-07-10 stsp line->annotated = 1;
4201 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4202 1a76625f 2018-10-22 stsp if (errcode)
4203 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4204 84451b3e 2018-07-10 stsp return err;
4207 84451b3e 2018-07-10 stsp static void *
4208 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4210 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4211 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4212 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4213 1a76625f 2018-10-22 stsp int errcode;
4215 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4217 61266923 2020-01-14 stsp return (void *)err;
4219 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4220 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4221 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4222 fc06ba56 2019-08-22 stsp err = NULL;
4224 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4225 1a76625f 2018-10-22 stsp if (errcode)
4226 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4227 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4229 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4230 1d0f4054 2021-06-17 stsp if (err == NULL)
4231 1d0f4054 2021-06-17 stsp err = close_err;
4232 c9beca56 2018-07-22 stsp ta->repo = NULL;
4233 c9beca56 2018-07-22 stsp *ta->complete = 1;
4235 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4236 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4237 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4239 18430de3 2018-07-10 stsp return (void *)err;
4242 245d91c1 2018-07-12 stsp static struct got_object_id *
4243 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4244 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4246 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4248 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4249 8d0fe45a 2019-08-12 stsp return NULL;
4251 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4252 245d91c1 2018-07-12 stsp if (!line->annotated)
4253 245d91c1 2018-07-12 stsp return NULL;
4255 245d91c1 2018-07-12 stsp return line->id;
4258 b880a918 2018-07-10 stsp static const struct got_error *
4259 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4261 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4264 245d91c1 2018-07-12 stsp if (blame->thread) {
4265 1a76625f 2018-10-22 stsp int errcode;
4266 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4267 1a76625f 2018-10-22 stsp if (errcode)
4268 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4269 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4270 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4271 1a76625f 2018-10-22 stsp if (errcode)
4272 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4273 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4274 1a76625f 2018-10-22 stsp if (errcode)
4275 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4276 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4277 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4278 245d91c1 2018-07-12 stsp err = NULL;
4279 245d91c1 2018-07-12 stsp blame->thread = NULL;
4281 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4282 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4283 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4284 1d0f4054 2021-06-17 stsp if (err == NULL)
4285 1d0f4054 2021-06-17 stsp err = close_err;
4286 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4288 245d91c1 2018-07-12 stsp if (blame->f) {
4289 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4290 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4291 245d91c1 2018-07-12 stsp blame->f = NULL;
4293 57670559 2018-12-23 stsp if (blame->lines) {
4294 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4295 57670559 2018-12-23 stsp free(blame->lines[i].id);
4296 57670559 2018-12-23 stsp free(blame->lines);
4297 57670559 2018-12-23 stsp blame->lines = NULL;
4299 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4300 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4302 245d91c1 2018-07-12 stsp return err;
4305 245d91c1 2018-07-12 stsp static const struct got_error *
4306 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4308 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4309 fc06ba56 2019-08-22 stsp int *done = arg;
4310 fc06ba56 2019-08-22 stsp int errcode;
4312 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4313 fc06ba56 2019-08-22 stsp if (errcode)
4314 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4315 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4317 fc06ba56 2019-08-22 stsp if (*done)
4318 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4320 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4321 fc06ba56 2019-08-22 stsp if (errcode)
4322 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4323 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4325 fc06ba56 2019-08-22 stsp return err;
4328 fc06ba56 2019-08-22 stsp static const struct got_error *
4329 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4331 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4332 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4333 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4334 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4335 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4336 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4337 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4338 15a94983 2018-12-23 stsp int obj_type;
4340 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
4341 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
4343 15a94983 2018-12-23 stsp return err;
4345 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4347 a44927cc 2022-04-07 stsp goto done;
4349 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4351 84451b3e 2018-07-10 stsp goto done;
4353 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4354 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4355 84451b3e 2018-07-10 stsp goto done;
4358 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4360 a70480e0 2018-06-23 stsp goto done;
4361 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4362 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4363 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4364 84451b3e 2018-07-10 stsp goto done;
4366 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4367 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4369 1fddf795 2021-01-20 stsp goto done;
4370 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4371 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4372 84451b3e 2018-07-10 stsp goto done;
4375 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4376 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4377 b02560ec 2019-08-19 stsp blame->nlines--;
4379 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4380 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4381 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4382 84451b3e 2018-07-10 stsp goto done;
4385 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4387 bd24772e 2018-07-11 stsp goto done;
4389 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4390 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4391 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4392 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4393 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4394 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4395 245d91c1 2018-07-12 stsp goto done;
4397 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4399 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4400 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4401 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4402 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4403 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4404 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4405 a5388363 2020-12-01 naddy s->blame_complete = 0;
4407 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4408 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4409 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4410 f5a09613 2020-12-13 naddy s->selected_line = 1;
4412 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4415 a44927cc 2022-04-07 stsp if (commit)
4416 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
4418 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4419 27d434c2 2018-09-15 stsp free(obj_id);
4421 245d91c1 2018-07-12 stsp stop_blame(blame);
4422 245d91c1 2018-07-12 stsp return err;
4425 245d91c1 2018-07-12 stsp static const struct got_error *
4426 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4427 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4429 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4430 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4432 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4434 c4843652 2019-08-12 stsp s->path = strdup(path);
4435 c4843652 2019-08-12 stsp if (s->path == NULL)
4436 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4438 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4439 c4843652 2019-08-12 stsp if (err) {
4440 c4843652 2019-08-12 stsp free(s->path);
4441 7cbe629d 2018-08-04 stsp return err;
4444 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4445 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4446 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4447 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4448 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4449 fb2756b9 2018-08-04 stsp s->repo = repo;
4450 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4451 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4453 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4454 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4455 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4456 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4458 11b20872 2019-11-08 stsp return err;
4461 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4462 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4463 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4464 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4465 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4467 a5388363 2020-12-01 naddy return run_blame(view);
4470 e5a0f69f 2018-08-18 stsp static const struct got_error *
4471 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4473 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4474 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4476 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4477 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4479 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4480 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4481 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4482 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4483 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4486 e5a0f69f 2018-08-18 stsp free(s->path);
4487 11b20872 2019-11-08 stsp free_colors(&s->colors);
4489 e5a0f69f 2018-08-18 stsp return err;
4492 7cbe629d 2018-08-04 stsp static const struct got_error *
4493 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4495 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4497 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4498 6c4c42e0 2019-06-24 stsp return NULL;
4501 6c4c42e0 2019-06-24 stsp static const struct got_error *
4502 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4504 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4505 6c4c42e0 2019-06-24 stsp int lineno;
4506 826082fe 2020-12-10 stsp char *line = NULL;
4507 826082fe 2020-12-10 stsp size_t linesize = 0;
4508 826082fe 2020-12-10 stsp ssize_t linelen;
4510 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4511 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4512 6c4c42e0 2019-06-24 stsp return NULL;
4515 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4516 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4517 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4519 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4521 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
4523 6c4c42e0 2019-06-24 stsp while (1) {
4524 6c4c42e0 2019-06-24 stsp off_t offset;
4526 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4527 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4528 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4532 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4533 6c4c42e0 2019-06-24 stsp lineno = 1;
4535 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4538 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4539 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4540 6c4c42e0 2019-06-24 stsp free(line);
4541 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4543 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4544 826082fe 2020-12-10 stsp if (linelen != -1 &&
4545 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4546 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4547 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4550 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4555 826082fe 2020-12-10 stsp free(line);
4557 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4558 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4559 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4562 6c4c42e0 2019-06-24 stsp return NULL;
4565 6c4c42e0 2019-06-24 stsp static const struct got_error *
4566 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4568 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4569 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4570 2b380cc8 2018-10-24 stsp int errcode;
4572 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4573 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4574 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4575 2b380cc8 2018-10-24 stsp if (errcode)
4576 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4578 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4581 51fe7530 2019-08-19 stsp if (s->blame_complete)
4582 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4584 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4586 669b5ffa 2018-10-07 stsp view_vborder(view);
4587 e5a0f69f 2018-08-18 stsp return err;
4590 e5a0f69f 2018-08-18 stsp static const struct got_error *
4591 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4593 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4594 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4595 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4596 669b5ffa 2018-10-07 stsp int begin_x = 0;
4598 e5a0f69f 2018-08-18 stsp switch (ch) {
4600 1e37a5c2 2019-05-12 jcs s->done = 1;
4602 4deef56f 2021-09-02 naddy case 'g':
4603 4deef56f 2021-09-02 naddy case KEY_HOME:
4604 4deef56f 2021-09-02 naddy s->selected_line = 1;
4605 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4607 4deef56f 2021-09-02 naddy case 'G':
4608 4deef56f 2021-09-02 naddy case KEY_END:
4609 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4610 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4611 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4613 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4614 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4615 4deef56f 2021-09-02 naddy (view->nlines - 3);
4619 1e37a5c2 2019-05-12 jcs case KEY_UP:
4620 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4621 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4622 1e37a5c2 2019-05-12 jcs s->selected_line--;
4623 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4624 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4625 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4627 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4628 ea025d1d 2020-02-22 naddy case CTRL('b'):
4629 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4630 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4633 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4634 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4635 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4637 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4640 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4641 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4642 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4643 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4644 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4645 1e37a5c2 2019-05-12 jcs s->selected_line++;
4646 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4647 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4648 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4651 1e37a5c2 2019-05-12 jcs case 'p': {
4652 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4653 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4654 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4655 1e37a5c2 2019-05-12 jcs if (id == NULL)
4657 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4658 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
4659 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4660 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4661 1e37a5c2 2019-05-12 jcs int obj_type;
4662 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4663 1e37a5c2 2019-05-12 jcs s->repo, id);
4666 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4667 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4668 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4669 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4672 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4673 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
4674 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
4677 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4678 a44927cc 2022-04-07 stsp pcommit, s->path);
4679 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
4680 e5a0f69f 2018-08-18 stsp if (err) {
4681 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4682 1e37a5c2 2019-05-12 jcs err = NULL;
4683 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4686 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4688 1e37a5c2 2019-05-12 jcs free(blob_id);
4689 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4690 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4691 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4694 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4695 d7b5a0e8 2022-04-20 stsp &pid->id);
4696 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4698 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4699 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
4701 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4706 1e37a5c2 2019-05-12 jcs s->done = 1;
4707 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4708 1e37a5c2 2019-05-12 jcs s->done = 0;
4709 1e37a5c2 2019-05-12 jcs if (thread_err)
4711 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4712 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4713 a5388363 2020-12-01 naddy err = run_blame(view);
4718 1e37a5c2 2019-05-12 jcs case 'B': {
4719 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4720 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4721 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
4723 1e37a5c2 2019-05-12 jcs s->done = 1;
4724 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4725 1e37a5c2 2019-05-12 jcs s->done = 0;
4726 1e37a5c2 2019-05-12 jcs if (thread_err)
4728 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4729 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4730 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4731 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4732 a5388363 2020-12-01 naddy err = run_blame(view);
4737 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4738 1e37a5c2 2019-05-12 jcs case '\r': {
4739 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4740 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4741 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4742 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4743 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4744 1e37a5c2 2019-05-12 jcs if (id == NULL)
4746 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4749 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4750 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4751 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4752 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4753 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4754 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4755 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4756 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4759 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4760 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4761 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4763 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4766 e78dc838 2020-12-04 stsp view->focussed = 0;
4767 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4768 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4769 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4772 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4773 e78dc838 2020-12-04 stsp view->focus_child = 1;
4775 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4780 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4781 ea025d1d 2020-02-22 naddy case CTRL('f'):
4783 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4784 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4785 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4788 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4789 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4790 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4791 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4794 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4795 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4796 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4797 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4799 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4800 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4801 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4803 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4804 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4805 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4806 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4812 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4815 a70480e0 2018-06-23 stsp static const struct got_error *
4816 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4818 a70480e0 2018-06-23 stsp const struct got_error *error;
4819 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4820 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4821 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4822 0587e10c 2020-07-23 stsp char *link_target = NULL;
4823 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4824 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4825 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4827 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4829 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4830 a70480e0 2018-06-23 stsp switch (ch) {
4832 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4835 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4836 69069811 2018-08-02 stsp if (repo_path == NULL)
4837 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4841 17020d27 2019-03-07 stsp usage_blame();
4842 a70480e0 2018-06-23 stsp /* NOTREACHED */
4846 a70480e0 2018-06-23 stsp argc -= optind;
4847 a70480e0 2018-06-23 stsp argv += optind;
4849 f135c941 2020-02-20 stsp if (argc != 1)
4850 a70480e0 2018-06-23 stsp usage_blame();
4852 69069811 2018-08-02 stsp if (repo_path == NULL) {
4853 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4854 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4855 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4856 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4857 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4858 c156c7a4 2020-12-18 stsp goto done;
4859 f135c941 2020-02-20 stsp if (worktree)
4860 eb41ed75 2019-02-05 stsp repo_path =
4861 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4863 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4864 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4865 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4866 c156c7a4 2020-12-18 stsp goto done;
4870 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4871 c02c541e 2019-03-29 stsp if (error != NULL)
4872 8e94dd5b 2019-01-04 stsp goto done;
4874 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4875 f135c941 2020-02-20 stsp worktree);
4876 c02c541e 2019-03-29 stsp if (error)
4877 92205607 2019-01-04 stsp goto done;
4879 f135c941 2020-02-20 stsp init_curses();
4881 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4882 51a10b52 2020-12-26 stsp if (error)
4883 51a10b52 2020-12-26 stsp goto done;
4885 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4886 eb41ed75 2019-02-05 stsp if (error)
4887 69069811 2018-08-02 stsp goto done;
4889 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4890 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4891 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4892 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4893 a70480e0 2018-06-23 stsp if (error != NULL)
4894 66b4983c 2018-06-23 stsp goto done;
4895 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4896 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4898 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4899 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4901 a19e88aa 2018-06-23 stsp if (error != NULL)
4902 8b473291 2019-02-21 stsp goto done;
4904 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4905 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4906 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4907 e1cd8fed 2018-08-01 stsp goto done;
4910 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4911 a44927cc 2022-04-07 stsp if (error)
4912 a44927cc 2022-04-07 stsp goto done;
4914 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4915 a44927cc 2022-04-07 stsp commit, repo);
4916 7cbe629d 2018-08-04 stsp if (error)
4917 7cbe629d 2018-08-04 stsp goto done;
4919 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4920 78756c87 2020-11-24 stsp commit_id, repo);
4921 0587e10c 2020-07-23 stsp if (error)
4922 0587e10c 2020-07-23 stsp goto done;
4923 12314ad4 2019-08-31 stsp if (worktree) {
4924 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4925 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4926 12314ad4 2019-08-31 stsp worktree = NULL;
4928 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4930 69069811 2018-08-02 stsp free(repo_path);
4931 f135c941 2020-02-20 stsp free(in_repo_path);
4932 0587e10c 2020-07-23 stsp free(link_target);
4933 69069811 2018-08-02 stsp free(cwd);
4934 a70480e0 2018-06-23 stsp free(commit_id);
4935 a44927cc 2022-04-07 stsp if (commit)
4936 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
4937 eb41ed75 2019-02-05 stsp if (worktree)
4938 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4939 1d0f4054 2021-06-17 stsp if (repo) {
4940 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4941 1d0f4054 2021-06-17 stsp if (error == NULL)
4942 1d0f4054 2021-06-17 stsp error = close_err;
4944 51a10b52 2020-12-26 stsp tog_free_refs();
4945 a70480e0 2018-06-23 stsp return error;
4948 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4949 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4951 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4952 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4953 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4954 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4955 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4956 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4957 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4959 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4961 f7d12f7e 2018-08-01 stsp werase(view->window);
4963 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4964 ffd1d5e5 2018-06-23 stsp return NULL;
4966 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4968 ffd1d5e5 2018-06-23 stsp return err;
4969 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4970 a3404814 2018-09-02 stsp wstandout(view->window);
4971 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4973 11b20872 2019-11-08 stsp wattr_on(view->window,
4974 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4975 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4977 11b20872 2019-11-08 stsp wattr_off(view->window,
4978 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4979 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4980 a3404814 2018-09-02 stsp wstandend(view->window);
4981 2550e4c3 2018-07-13 stsp free(wline);
4982 2550e4c3 2018-07-13 stsp wline = NULL;
4983 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4984 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4985 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4986 ffd1d5e5 2018-06-23 stsp return NULL;
4987 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4989 ce52c690 2018-06-23 stsp return err;
4990 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4991 2550e4c3 2018-07-13 stsp free(wline);
4992 2550e4c3 2018-07-13 stsp wline = NULL;
4993 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4994 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4995 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4996 ffd1d5e5 2018-06-23 stsp return NULL;
4997 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4998 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4999 a1eca9bb 2018-06-23 stsp return NULL;
5001 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5002 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5003 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5004 0cf4efb1 2018-09-29 stsp if (view->focussed)
5005 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5006 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5008 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5009 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5010 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5011 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5012 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5013 ffd1d5e5 2018-06-23 stsp return NULL;
5017 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5020 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5021 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5022 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5023 848d6979 2019-08-12 stsp const char *modestr = "";
5024 56e0773d 2019-11-28 stsp mode_t mode;
5026 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5027 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5029 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5030 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5031 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5033 638f9024 2019-05-13 stsp return got_error_from_errno(
5034 230a42bd 2019-05-11 jcs "got_object_id_str");
5036 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5037 63c5ca5d 2019-08-24 stsp modestr = "$";
5038 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5041 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5042 d86d3b18 2020-12-01 naddy te, s->repo);
5043 0d6c6ee3 2020-05-20 stsp if (err) {
5044 0d6c6ee3 2020-05-20 stsp free(id_str);
5045 0d6c6ee3 2020-05-20 stsp return err;
5047 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5048 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5049 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5051 848d6979 2019-08-12 stsp modestr = "@";
5053 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5054 848d6979 2019-08-12 stsp modestr = "/";
5055 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5056 848d6979 2019-08-12 stsp modestr = "*";
5057 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5058 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5059 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5060 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5061 1d13200f 2018-07-12 stsp free(id_str);
5062 0d6c6ee3 2020-05-20 stsp free(link_target);
5063 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5065 1d13200f 2018-07-12 stsp free(id_str);
5066 0d6c6ee3 2020-05-20 stsp free(link_target);
5067 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5068 ffd1d5e5 2018-06-23 stsp if (err) {
5069 ffd1d5e5 2018-06-23 stsp free(line);
5072 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5073 0cf4efb1 2018-09-29 stsp if (view->focussed)
5074 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5075 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5077 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5079 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5080 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5081 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5083 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5084 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5085 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5086 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5087 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5088 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5089 ffd1d5e5 2018-06-23 stsp free(line);
5090 2550e4c3 2018-07-13 stsp free(wline);
5091 2550e4c3 2018-07-13 stsp wline = NULL;
5093 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5094 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5095 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5099 ffd1d5e5 2018-06-23 stsp return err;
5102 ffd1d5e5 2018-06-23 stsp static void
5103 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5105 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5106 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5107 fa86c4bf 2020-11-29 stsp int i = 0;
5109 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5112 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5113 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5114 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5115 fa86c4bf 2020-11-29 stsp if (!isroot)
5116 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5119 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5120 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5124 fa86c4bf 2020-11-29 stsp static void
5125 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5127 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5128 ffd1d5e5 2018-06-23 stsp int n = 0;
5130 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5131 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5132 694d3271 2020-12-01 naddy s->first_displayed_entry);
5134 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5136 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5137 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5138 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5139 768394f3 2019-01-24 stsp if (last) {
5140 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5141 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5146 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5147 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5148 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5150 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5151 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5152 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5154 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5155 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5156 56e0773d 2019-11-28 stsp + 1 /* slash */;
5158 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5160 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5161 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5162 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5164 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5165 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5166 d9765a41 2018-06-23 stsp while (pt) {
5167 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5168 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5169 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5170 cb2ebc8a 2018-06-23 stsp goto done;
5172 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5173 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5174 cb2ebc8a 2018-06-23 stsp goto done;
5176 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5179 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5180 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5181 ce52c690 2018-06-23 stsp goto done;
5185 ce52c690 2018-06-23 stsp if (err) {
5186 ce52c690 2018-06-23 stsp free(*path);
5187 ce52c690 2018-06-23 stsp *path = NULL;
5189 ce52c690 2018-06-23 stsp return err;
5192 ce52c690 2018-06-23 stsp static const struct got_error *
5193 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5194 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5195 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5197 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5198 ce52c690 2018-06-23 stsp char *path;
5199 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5201 a0de39f3 2019-08-09 stsp *new_view = NULL;
5203 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5205 ce52c690 2018-06-23 stsp return err;
5207 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5208 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5209 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5210 83ce39e3 2019-08-12 stsp goto done;
5213 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5214 e5a0f69f 2018-08-18 stsp if (err) {
5215 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5216 fc06ba56 2019-08-22 stsp err = NULL;
5217 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5219 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5221 83ce39e3 2019-08-12 stsp free(path);
5222 69efd4c4 2018-07-18 stsp return err;
5225 69efd4c4 2018-07-18 stsp static const struct got_error *
5226 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5227 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5229 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5230 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5231 69efd4c4 2018-07-18 stsp char *path;
5233 a0de39f3 2019-08-09 stsp *new_view = NULL;
5235 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5236 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5237 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5239 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5241 69efd4c4 2018-07-18 stsp return err;
5243 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5246 e5a0f69f 2018-08-18 stsp view_close(log_view);
5248 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5249 cb2ebc8a 2018-06-23 stsp free(path);
5250 cb2ebc8a 2018-06-23 stsp return err;
5253 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5254 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5255 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5257 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5258 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5259 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5260 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5262 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5263 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5265 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5266 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5267 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5269 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5271 bc573f3b 2021-07-10 stsp goto done;
5274 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5275 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5276 bc573f3b 2021-07-10 stsp * closed on demand.
5278 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5279 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5281 bc573f3b 2021-07-10 stsp goto done;
5282 bc573f3b 2021-07-10 stsp s->tree = s->root;
5284 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5285 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5286 ffd1d5e5 2018-06-23 stsp goto done;
5288 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5289 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5290 ffd1d5e5 2018-06-23 stsp goto done;
5293 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5294 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5295 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5296 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5297 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5298 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5299 9cd7cbd1 2020-12-07 stsp goto done;
5302 fb2756b9 2018-08-04 stsp s->repo = repo;
5304 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5305 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5306 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5307 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5309 c0b01bdb 2019-11-08 stsp goto done;
5310 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5311 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5313 c0b01bdb 2019-11-08 stsp goto done;
5314 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5315 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5316 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5318 c0b01bdb 2019-11-08 stsp goto done;
5320 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5321 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5322 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5324 11b20872 2019-11-08 stsp goto done;
5326 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5327 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5329 c0b01bdb 2019-11-08 stsp goto done;
5332 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5333 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5334 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5335 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5336 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5338 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5339 bc573f3b 2021-07-10 stsp if (commit)
5340 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5342 bc573f3b 2021-07-10 stsp close_tree_view(view);
5343 ad80ab7b 2018-08-04 stsp return err;
5346 e5a0f69f 2018-08-18 stsp static const struct got_error *
5347 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5349 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5351 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5352 fb2756b9 2018-08-04 stsp free(s->tree_label);
5353 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5354 6484ec90 2018-09-29 stsp free(s->commit_id);
5355 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5356 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5357 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5358 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5359 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5360 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5361 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5362 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5363 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5364 ad80ab7b 2018-08-04 stsp free(parent);
5367 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5368 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5369 bc573f3b 2021-07-10 stsp if (s->root)
5370 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5371 7c32bd05 2019-06-22 stsp return NULL;
5374 7c32bd05 2019-06-22 stsp static const struct got_error *
5375 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5377 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5379 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5380 7c32bd05 2019-06-22 stsp return NULL;
5383 7c32bd05 2019-06-22 stsp static int
5384 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5386 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5388 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5392 7c32bd05 2019-06-22 stsp static const struct got_error *
5393 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5395 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5396 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5398 7c32bd05 2019-06-22 stsp if (!view->searching) {
5399 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5400 7c32bd05 2019-06-22 stsp return NULL;
5403 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5404 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5405 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5406 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5407 56e0773d 2019-11-28 stsp s->selected_entry);
5409 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5411 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5412 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5414 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5415 56e0773d 2019-11-28 stsp s->selected_entry);
5418 487cd7d2 2021-12-17 stsp if (s->selected_entry)
5419 487cd7d2 2021-12-17 stsp te = s->selected_entry;
5420 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
5421 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5423 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5426 7c32bd05 2019-06-22 stsp while (1) {
5427 56e0773d 2019-11-28 stsp if (te == NULL) {
5428 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5429 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5430 ac66afb8 2019-06-24 stsp return NULL;
5432 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5433 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5435 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5438 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5439 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5440 56e0773d 2019-11-28 stsp s->matched_entry = te;
5444 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5445 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5447 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5450 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5451 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5452 7c32bd05 2019-06-22 stsp s->selected = 0;
5455 e5a0f69f 2018-08-18 stsp return NULL;
5458 ad80ab7b 2018-08-04 stsp static const struct got_error *
5459 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5461 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5462 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5463 e5a0f69f 2018-08-18 stsp char *parent_path;
5465 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5467 e5a0f69f 2018-08-18 stsp return err;
5469 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5470 e5a0f69f 2018-08-18 stsp free(parent_path);
5472 669b5ffa 2018-10-07 stsp view_vborder(view);
5473 e5a0f69f 2018-08-18 stsp return err;
5476 e5a0f69f 2018-08-18 stsp static const struct got_error *
5477 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5479 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5480 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5481 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5482 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5483 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
5485 e5a0f69f 2018-08-18 stsp switch (ch) {
5487 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5490 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5492 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5493 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5494 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5495 e78dc838 2020-12-04 stsp view->focussed = 0;
5496 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5497 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5498 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5500 1e37a5c2 2019-05-12 jcs return err;
5501 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5502 e78dc838 2020-12-04 stsp view->focus_child = 1;
5504 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5507 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5508 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5509 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5510 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5511 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5512 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5513 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5514 152c1c93 2020-11-29 stsp if (err) {
5515 152c1c93 2020-11-29 stsp view_close(ref_view);
5516 152c1c93 2020-11-29 stsp return err;
5518 e78dc838 2020-12-04 stsp view->focussed = 0;
5519 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5520 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5521 152c1c93 2020-11-29 stsp err = view_close_child(view);
5523 152c1c93 2020-11-29 stsp return err;
5524 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5525 e78dc838 2020-12-04 stsp view->focus_child = 1;
5527 152c1c93 2020-11-29 stsp *new_view = ref_view;
5529 e4526bf5 2021-09-03 naddy case 'g':
5530 e4526bf5 2021-09-03 naddy case KEY_HOME:
5531 e4526bf5 2021-09-03 naddy s->selected = 0;
5532 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5533 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5534 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5536 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5538 e4526bf5 2021-09-03 naddy case 'G':
5539 e4526bf5 2021-09-03 naddy case KEY_END:
5540 e4526bf5 2021-09-03 naddy s->selected = 0;
5541 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5542 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5543 e4526bf5 2021-09-03 naddy if (te == NULL) {
5544 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5545 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5550 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5551 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5553 e4526bf5 2021-09-03 naddy if (n > 0)
5554 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5557 1e37a5c2 2019-05-12 jcs case KEY_UP:
5558 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5559 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5560 1e37a5c2 2019-05-12 jcs s->selected--;
5563 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5565 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5566 ea025d1d 2020-02-22 naddy case CTRL('b'):
5567 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5568 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5569 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5570 fa86c4bf 2020-11-29 stsp s->selected = 0;
5572 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5573 fa86c4bf 2020-11-29 stsp s->selected = 0;
5575 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5578 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5579 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5580 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5581 1e37a5c2 2019-05-12 jcs s->selected++;
5584 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5586 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5588 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5590 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5591 ea025d1d 2020-02-22 naddy case CTRL('f'):
5592 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5594 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5595 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5596 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5599 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5601 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5603 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5604 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5605 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5606 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5607 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5609 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5610 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5612 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5613 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5614 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5615 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5616 1e37a5c2 2019-05-12 jcs s->selected_entry =
5617 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5618 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5619 1e37a5c2 2019-05-12 jcs free(parent);
5620 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5621 56e0773d 2019-11-28 stsp s->selected_entry))) {
5622 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5623 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5624 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5627 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5628 941e9f74 2019-05-21 stsp if (err) {
5629 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5632 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5633 56e0773d 2019-11-28 stsp s->selected_entry))) {
5634 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5635 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5636 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5638 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5639 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5640 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5643 e78dc838 2020-12-04 stsp view->focussed = 0;
5644 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5645 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5646 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5648 669b5ffa 2018-10-07 stsp return err;
5649 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5650 e78dc838 2020-12-04 stsp view->focus_child = 1;
5652 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5655 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5656 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5657 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5663 ffd1d5e5 2018-06-23 stsp return err;
5666 ffd1d5e5 2018-06-23 stsp __dead static void
5667 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5670 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5671 ffd1d5e5 2018-06-23 stsp getprogname());
5675 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5676 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5678 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5679 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5680 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5681 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5682 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5683 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5684 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5685 4e97c21c 2020-12-06 stsp char *label = NULL;
5686 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5687 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5689 5221c383 2018-08-01 stsp struct tog_view *view;
5691 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5692 ffd1d5e5 2018-06-23 stsp switch (ch) {
5694 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5697 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5698 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5699 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5703 e99e2d15 2020-11-24 naddy usage_tree();
5704 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5708 ffd1d5e5 2018-06-23 stsp argc -= optind;
5709 ffd1d5e5 2018-06-23 stsp argv += optind;
5711 55cccc34 2020-02-20 stsp if (argc > 1)
5712 e99e2d15 2020-11-24 naddy usage_tree();
5714 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5715 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5716 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5717 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5718 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5719 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5720 c156c7a4 2020-12-18 stsp goto done;
5721 55cccc34 2020-02-20 stsp if (worktree)
5722 52185f70 2019-02-05 stsp repo_path =
5723 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5725 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5726 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5727 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5728 c156c7a4 2020-12-18 stsp goto done;
5732 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5733 c02c541e 2019-03-29 stsp if (error != NULL)
5734 52185f70 2019-02-05 stsp goto done;
5736 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5737 55cccc34 2020-02-20 stsp repo, worktree);
5738 55cccc34 2020-02-20 stsp if (error)
5739 55cccc34 2020-02-20 stsp goto done;
5741 55cccc34 2020-02-20 stsp init_curses();
5743 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5744 c02c541e 2019-03-29 stsp if (error)
5745 52185f70 2019-02-05 stsp goto done;
5747 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5748 51a10b52 2020-12-26 stsp if (error)
5749 51a10b52 2020-12-26 stsp goto done;
5751 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5752 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5753 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5754 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5755 4e97c21c 2020-12-06 stsp if (error)
5756 4e97c21c 2020-12-06 stsp goto done;
5757 4e97c21c 2020-12-06 stsp head_ref_name = label;
5759 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5760 4e97c21c 2020-12-06 stsp if (error == NULL)
5761 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5762 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5763 4e97c21c 2020-12-06 stsp goto done;
5764 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5765 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5766 4e97c21c 2020-12-06 stsp if (error)
5767 4e97c21c 2020-12-06 stsp goto done;
5770 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5771 a44927cc 2022-04-07 stsp if (error)
5772 a44927cc 2022-04-07 stsp goto done;
5774 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5775 5221c383 2018-08-01 stsp if (view == NULL) {
5776 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5777 5221c383 2018-08-01 stsp goto done;
5779 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5780 ad80ab7b 2018-08-04 stsp if (error)
5781 ad80ab7b 2018-08-04 stsp goto done;
5782 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5783 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
5784 d91faf3b 2020-12-01 naddy in_repo_path);
5785 55cccc34 2020-02-20 stsp if (error)
5786 55cccc34 2020-02-20 stsp goto done;
5789 55cccc34 2020-02-20 stsp if (worktree) {
5790 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5791 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5792 55cccc34 2020-02-20 stsp worktree = NULL;
5794 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5796 52185f70 2019-02-05 stsp free(repo_path);
5797 e4a0e26d 2020-02-20 stsp free(cwd);
5798 ffd1d5e5 2018-06-23 stsp free(commit_id);
5799 4e97c21c 2020-12-06 stsp free(label);
5801 486cd271 2020-12-06 stsp got_ref_close(ref);
5802 1d0f4054 2021-06-17 stsp if (repo) {
5803 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5804 1d0f4054 2021-06-17 stsp if (error == NULL)
5805 1d0f4054 2021-06-17 stsp error = close_err;
5807 51a10b52 2020-12-26 stsp tog_free_refs();
5808 ffd1d5e5 2018-06-23 stsp return error;
5811 6458efa5 2020-11-24 stsp static const struct got_error *
5812 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5814 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5815 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5817 6458efa5 2020-11-24 stsp s->nrefs = 0;
5818 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5819 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
5820 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
5821 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
5822 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
5825 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5826 6458efa5 2020-11-24 stsp if (re == NULL)
5827 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5829 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5830 8924d611 2020-12-26 stsp if (re->ref == NULL)
5831 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5832 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5833 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5836 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5837 6458efa5 2020-11-24 stsp return NULL;
5841 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5843 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5845 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5846 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5847 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5848 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5853 6458efa5 2020-11-24 stsp static const struct got_error *
5854 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5856 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5857 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5859 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5860 6458efa5 2020-11-24 stsp s->repo = repo;
5862 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5863 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5865 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5867 6458efa5 2020-11-24 stsp return err;
5869 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5870 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5871 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5872 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5874 6458efa5 2020-11-24 stsp goto done;
5876 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5877 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5878 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5880 6458efa5 2020-11-24 stsp goto done;
5882 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5883 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5884 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5886 cc488aa7 2022-01-23 stsp goto done;
5888 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
5889 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
5890 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
5892 6458efa5 2020-11-24 stsp goto done;
5895 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5896 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5897 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5898 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5899 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5902 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5903 6458efa5 2020-11-24 stsp return err;
5906 6458efa5 2020-11-24 stsp static const struct got_error *
5907 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5909 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5911 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5912 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5914 6458efa5 2020-11-24 stsp return NULL;
5917 6458efa5 2020-11-24 stsp static const struct got_error *
5918 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5919 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5921 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5922 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5923 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5924 6458efa5 2020-11-24 stsp int obj_type;
5926 c42c9805 2020-11-24 stsp *commit_id = NULL;
5928 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5930 6458efa5 2020-11-24 stsp return err;
5932 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5934 6458efa5 2020-11-24 stsp goto done;
5936 6458efa5 2020-11-24 stsp switch (obj_type) {
5937 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5938 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5940 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5941 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5943 6458efa5 2020-11-24 stsp goto done;
5944 c42c9805 2020-11-24 stsp free(obj_id);
5945 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5946 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5948 6458efa5 2020-11-24 stsp goto done;
5949 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5950 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5951 c42c9805 2020-11-24 stsp goto done;
5953 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5954 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5955 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5956 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5957 c42c9805 2020-11-24 stsp goto done;
5961 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5967 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5968 c42c9805 2020-11-24 stsp if (err) {
5969 c42c9805 2020-11-24 stsp free(*commit_id);
5970 c42c9805 2020-11-24 stsp *commit_id = NULL;
5972 c42c9805 2020-11-24 stsp return err;
5975 c42c9805 2020-11-24 stsp static const struct got_error *
5976 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5977 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5979 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5980 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5981 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5983 c42c9805 2020-11-24 stsp *new_view = NULL;
5985 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5986 c42c9805 2020-11-24 stsp if (err) {
5987 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5988 c42c9805 2020-11-24 stsp return err;
5990 c42c9805 2020-11-24 stsp return NULL;
5993 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5994 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5995 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5996 6458efa5 2020-11-24 stsp goto done;
5999 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6000 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6003 6458efa5 2020-11-24 stsp view_close(log_view);
6005 6458efa5 2020-11-24 stsp *new_view = log_view;
6006 c42c9805 2020-11-24 stsp free(commit_id);
6007 6458efa5 2020-11-24 stsp return err;
6010 ce5b7c56 2019-07-09 stsp static void
6011 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6013 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6014 34ba6917 2020-11-30 stsp int i = 0;
6016 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6019 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6020 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6021 34ba6917 2020-11-30 stsp if (re == NULL)
6023 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6024 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6028 34ba6917 2020-11-30 stsp static void
6029 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6031 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6032 6458efa5 2020-11-24 stsp int n = 0;
6034 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6035 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6037 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6039 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6040 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6041 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6042 6458efa5 2020-11-24 stsp if (last) {
6043 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6044 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6049 6458efa5 2020-11-24 stsp static const struct got_error *
6050 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6052 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6054 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6055 6458efa5 2020-11-24 stsp return NULL;
6058 6458efa5 2020-11-24 stsp static int
6059 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6061 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6063 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6067 6458efa5 2020-11-24 stsp static const struct got_error *
6068 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6070 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6071 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6073 6458efa5 2020-11-24 stsp if (!view->searching) {
6074 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6075 6458efa5 2020-11-24 stsp return NULL;
6078 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6079 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6080 6458efa5 2020-11-24 stsp if (s->selected_entry)
6081 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6083 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6084 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6086 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6087 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6089 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6090 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6093 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6094 487cd7d2 2021-12-17 stsp re = s->selected_entry;
6095 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6096 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6098 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6101 6458efa5 2020-11-24 stsp while (1) {
6102 6458efa5 2020-11-24 stsp if (re == NULL) {
6103 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6104 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6105 6458efa5 2020-11-24 stsp return NULL;
6107 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6108 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6110 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6113 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6114 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6115 6458efa5 2020-11-24 stsp s->matched_entry = re;
6119 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6120 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6122 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6125 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6126 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6127 6458efa5 2020-11-24 stsp s->selected = 0;
6130 6458efa5 2020-11-24 stsp return NULL;
6133 6458efa5 2020-11-24 stsp static const struct got_error *
6134 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6136 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6137 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6138 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6139 6458efa5 2020-11-24 stsp char *line = NULL;
6140 6458efa5 2020-11-24 stsp wchar_t *wline;
6141 6458efa5 2020-11-24 stsp struct tog_color *tc;
6142 6458efa5 2020-11-24 stsp int width, n;
6143 6458efa5 2020-11-24 stsp int limit = view->nlines;
6145 6458efa5 2020-11-24 stsp werase(view->window);
6147 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6149 6458efa5 2020-11-24 stsp if (limit == 0)
6150 6458efa5 2020-11-24 stsp return NULL;
6152 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6154 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6155 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6156 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6158 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6159 6458efa5 2020-11-24 stsp if (err) {
6160 6458efa5 2020-11-24 stsp free(line);
6161 6458efa5 2020-11-24 stsp return err;
6163 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6164 6458efa5 2020-11-24 stsp wstandout(view->window);
6165 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6166 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6167 6458efa5 2020-11-24 stsp wstandend(view->window);
6168 6458efa5 2020-11-24 stsp free(wline);
6169 6458efa5 2020-11-24 stsp wline = NULL;
6170 6458efa5 2020-11-24 stsp free(line);
6171 6458efa5 2020-11-24 stsp line = NULL;
6172 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6173 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6174 6458efa5 2020-11-24 stsp if (--limit <= 0)
6175 6458efa5 2020-11-24 stsp return NULL;
6178 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6179 6458efa5 2020-11-24 stsp char *line = NULL;
6181 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6182 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6183 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6184 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6185 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6186 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6187 6458efa5 2020-11-24 stsp struct got_object_id *id;
6188 6458efa5 2020-11-24 stsp char *id_str;
6189 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6191 6458efa5 2020-11-24 stsp return err;
6192 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6193 6458efa5 2020-11-24 stsp if (err) {
6195 6458efa5 2020-11-24 stsp return err;
6197 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6198 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6199 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6201 6458efa5 2020-11-24 stsp free(id_str);
6202 6458efa5 2020-11-24 stsp return err;
6205 6458efa5 2020-11-24 stsp free(id_str);
6207 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6208 6458efa5 2020-11-24 stsp if (line == NULL)
6209 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6212 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6213 6458efa5 2020-11-24 stsp if (err) {
6214 6458efa5 2020-11-24 stsp free(line);
6215 6458efa5 2020-11-24 stsp return err;
6217 6458efa5 2020-11-24 stsp if (n == s->selected) {
6218 6458efa5 2020-11-24 stsp if (view->focussed)
6219 6458efa5 2020-11-24 stsp wstandout(view->window);
6220 6458efa5 2020-11-24 stsp s->selected_entry = re;
6222 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6224 6458efa5 2020-11-24 stsp wattr_on(view->window,
6225 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6226 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6228 6458efa5 2020-11-24 stsp wattr_off(view->window,
6229 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6230 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6231 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6232 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6233 6458efa5 2020-11-24 stsp wstandend(view->window);
6234 6458efa5 2020-11-24 stsp free(line);
6235 6458efa5 2020-11-24 stsp free(wline);
6236 6458efa5 2020-11-24 stsp wline = NULL;
6238 6458efa5 2020-11-24 stsp s->ndisplayed++;
6239 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6242 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6245 6458efa5 2020-11-24 stsp view_vborder(view);
6246 6458efa5 2020-11-24 stsp return err;
6249 6458efa5 2020-11-24 stsp static const struct got_error *
6250 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6251 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6253 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6254 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6255 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6257 c42c9805 2020-11-24 stsp *new_view = NULL;
6259 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6260 c42c9805 2020-11-24 stsp if (err) {
6261 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6262 c42c9805 2020-11-24 stsp return err;
6264 c42c9805 2020-11-24 stsp return NULL;
6268 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6269 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6270 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6271 c42c9805 2020-11-24 stsp goto done;
6274 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6275 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6277 c42c9805 2020-11-24 stsp goto done;
6279 c42c9805 2020-11-24 stsp *new_view = tree_view;
6281 c42c9805 2020-11-24 stsp free(commit_id);
6282 c42c9805 2020-11-24 stsp return err;
6284 c42c9805 2020-11-24 stsp static const struct got_error *
6285 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6287 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6288 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6289 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6290 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6291 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
6293 6458efa5 2020-11-24 stsp switch (ch) {
6295 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6298 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
6299 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6300 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
6301 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
6304 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
6305 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
6306 50617b77 2021-11-20 stsp &tog_refs, s->repo);
6309 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
6310 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
6312 6458efa5 2020-11-24 stsp case KEY_ENTER:
6313 6458efa5 2020-11-24 stsp case '\r':
6314 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6316 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6317 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6318 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6320 e78dc838 2020-12-04 stsp view->focussed = 0;
6321 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6322 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6323 6458efa5 2020-11-24 stsp err = view_close_child(view);
6325 6458efa5 2020-11-24 stsp return err;
6326 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6327 e78dc838 2020-12-04 stsp view->focus_child = 1;
6329 6458efa5 2020-11-24 stsp *new_view = log_view;
6332 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6334 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6335 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6336 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6338 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6340 e78dc838 2020-12-04 stsp view->focussed = 0;
6341 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6342 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6343 c42c9805 2020-11-24 stsp err = view_close_child(view);
6345 c42c9805 2020-11-24 stsp return err;
6346 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6347 e78dc838 2020-12-04 stsp view->focus_child = 1;
6349 c42c9805 2020-11-24 stsp *new_view = tree_view;
6351 e4526bf5 2021-09-03 naddy case 'g':
6352 e4526bf5 2021-09-03 naddy case KEY_HOME:
6353 e4526bf5 2021-09-03 naddy s->selected = 0;
6354 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6356 e4526bf5 2021-09-03 naddy case 'G':
6357 e4526bf5 2021-09-03 naddy case KEY_END:
6358 e4526bf5 2021-09-03 naddy s->selected = 0;
6359 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6360 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6361 e4526bf5 2021-09-03 naddy if (re == NULL)
6363 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6364 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6366 e4526bf5 2021-09-03 naddy if (n > 0)
6367 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6370 6458efa5 2020-11-24 stsp case KEY_UP:
6371 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6372 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6373 6458efa5 2020-11-24 stsp s->selected--;
6376 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6378 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6379 6458efa5 2020-11-24 stsp case CTRL('b'):
6380 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6381 34ba6917 2020-11-30 stsp s->selected = 0;
6382 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6385 6458efa5 2020-11-24 stsp case KEY_DOWN:
6386 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6387 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6388 6458efa5 2020-11-24 stsp s->selected++;
6391 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6392 6458efa5 2020-11-24 stsp /* can't scroll any further */
6394 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6396 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6397 6458efa5 2020-11-24 stsp case CTRL('f'):
6398 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6399 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6400 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6401 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6404 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6406 6458efa5 2020-11-24 stsp case CTRL('l'):
6407 8924d611 2020-12-26 stsp tog_free_refs();
6408 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
6411 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6412 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6414 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6415 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6416 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6422 6458efa5 2020-11-24 stsp return err;
6425 6458efa5 2020-11-24 stsp __dead static void
6426 6458efa5 2020-11-24 stsp usage_ref(void)
6429 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6430 6458efa5 2020-11-24 stsp getprogname());
6434 6458efa5 2020-11-24 stsp static const struct got_error *
6435 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6437 6458efa5 2020-11-24 stsp const struct got_error *error;
6438 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6439 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6440 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6442 6458efa5 2020-11-24 stsp struct tog_view *view;
6444 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6445 6458efa5 2020-11-24 stsp switch (ch) {
6447 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6448 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6449 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6453 e99e2d15 2020-11-24 naddy usage_ref();
6454 6458efa5 2020-11-24 stsp /* NOTREACHED */
6458 6458efa5 2020-11-24 stsp argc -= optind;
6459 6458efa5 2020-11-24 stsp argv += optind;
6461 6458efa5 2020-11-24 stsp if (argc > 1)
6462 e99e2d15 2020-11-24 naddy usage_ref();
6464 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6465 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6466 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6467 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6468 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6469 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6470 c156c7a4 2020-12-18 stsp goto done;
6471 6458efa5 2020-11-24 stsp if (worktree)
6472 6458efa5 2020-11-24 stsp repo_path =
6473 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6475 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6476 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6477 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6478 c156c7a4 2020-12-18 stsp goto done;
6482 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6483 6458efa5 2020-11-24 stsp if (error != NULL)
6484 6458efa5 2020-11-24 stsp goto done;
6486 6458efa5 2020-11-24 stsp init_curses();
6488 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6489 51a10b52 2020-12-26 stsp if (error)
6490 51a10b52 2020-12-26 stsp goto done;
6492 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6493 6458efa5 2020-11-24 stsp if (error)
6494 6458efa5 2020-11-24 stsp goto done;
6496 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6497 6458efa5 2020-11-24 stsp if (view == NULL) {
6498 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6499 6458efa5 2020-11-24 stsp goto done;
6502 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6503 6458efa5 2020-11-24 stsp if (error)
6504 6458efa5 2020-11-24 stsp goto done;
6506 6458efa5 2020-11-24 stsp if (worktree) {
6507 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6508 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6509 6458efa5 2020-11-24 stsp worktree = NULL;
6511 6458efa5 2020-11-24 stsp error = view_loop(view);
6513 6458efa5 2020-11-24 stsp free(repo_path);
6514 6458efa5 2020-11-24 stsp free(cwd);
6515 1d0f4054 2021-06-17 stsp if (repo) {
6516 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6517 1d0f4054 2021-06-17 stsp if (close_err)
6518 1d0f4054 2021-06-17 stsp error = close_err;
6520 51a10b52 2020-12-26 stsp tog_free_refs();
6521 6458efa5 2020-11-24 stsp return error;
6524 6458efa5 2020-11-24 stsp static void
6525 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6529 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6530 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6531 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
6532 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6534 6879ba42 2020-10-01 naddy fputc('\n', fp);
6537 4ed7e80c 2018-05-20 stsp __dead static void
6538 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6540 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6542 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6543 6879ba42 2020-10-01 naddy getprogname());
6544 6879ba42 2020-10-01 naddy if (hflag) {
6545 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6546 6879ba42 2020-10-01 naddy list_commands(fp);
6548 6879ba42 2020-10-01 naddy exit(status);
6551 c2301be8 2018-04-30 stsp static char **
6552 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6554 ee85c5e8 2020-02-29 stsp va_list ap;
6555 c2301be8 2018-04-30 stsp char **argv;
6558 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6560 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6561 c2301be8 2018-04-30 stsp if (argv == NULL)
6562 c2301be8 2018-04-30 stsp err(1, "calloc");
6563 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6564 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6565 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6566 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6569 ee85c5e8 2020-02-29 stsp va_end(ap);
6570 c2301be8 2018-04-30 stsp return argv;
6574 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6575 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6576 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6577 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6578 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6580 ee85c5e8 2020-02-29 stsp static const struct got_error *
6581 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6583 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6584 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6585 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6586 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6587 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6588 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6589 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6590 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6592 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6593 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6594 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6596 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6597 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6598 ee85c5e8 2020-02-29 stsp goto done;
6600 ee85c5e8 2020-02-29 stsp if (worktree)
6601 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6603 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6604 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6605 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6606 ee85c5e8 2020-02-29 stsp goto done;
6609 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6610 ee85c5e8 2020-02-29 stsp if (error != NULL)
6611 ee85c5e8 2020-02-29 stsp goto done;
6613 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6614 ee85c5e8 2020-02-29 stsp repo, worktree);
6615 ee85c5e8 2020-02-29 stsp if (error)
6616 ee85c5e8 2020-02-29 stsp goto done;
6618 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6619 84de9106 2020-12-26 stsp if (error)
6620 84de9106 2020-12-26 stsp goto done;
6621 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6622 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6623 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6624 ee85c5e8 2020-02-29 stsp if (error)
6625 ee85c5e8 2020-02-29 stsp goto done;
6627 ee85c5e8 2020-02-29 stsp if (worktree) {
6628 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6629 ee85c5e8 2020-02-29 stsp worktree = NULL;
6632 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6633 a44927cc 2022-04-07 stsp if (error)
6634 a44927cc 2022-04-07 stsp goto done;
6636 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6637 ee85c5e8 2020-02-29 stsp if (error) {
6638 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6639 ee85c5e8 2020-02-29 stsp goto done;
6640 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6641 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6642 6879ba42 2020-10-01 naddy usage(1, 1);
6643 ee85c5e8 2020-02-29 stsp /* not reached */
6646 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6647 1d0f4054 2021-06-17 stsp if (error == NULL)
6648 1d0f4054 2021-06-17 stsp error = close_err;
6649 ee85c5e8 2020-02-29 stsp repo = NULL;
6651 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6652 ee85c5e8 2020-02-29 stsp if (error)
6653 ee85c5e8 2020-02-29 stsp goto done;
6655 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6657 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6658 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6660 1d0f4054 2021-06-17 stsp if (repo) {
6661 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6662 1d0f4054 2021-06-17 stsp if (error == NULL)
6663 1d0f4054 2021-06-17 stsp error = close_err;
6665 a44927cc 2022-04-07 stsp if (commit)
6666 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6667 ee85c5e8 2020-02-29 stsp if (worktree)
6668 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6670 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6671 ee85c5e8 2020-02-29 stsp free(commit_id);
6672 ee85c5e8 2020-02-29 stsp free(cwd);
6673 ee85c5e8 2020-02-29 stsp free(repo_path);
6674 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6675 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6677 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6678 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6679 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6681 87670572 2020-12-26 naddy tog_free_refs();
6682 ee85c5e8 2020-02-29 stsp return error;
6686 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6688 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6689 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6690 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6691 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6692 3e166534 2022-02-16 naddy static const struct option longopts[] = {
6693 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6694 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6697 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6699 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6700 9f7d7167 2018-04-29 stsp switch (ch) {
6702 9f7d7167 2018-04-29 stsp hflag = 1;
6705 53ccebc2 2019-07-30 stsp Vflag = 1;
6708 6879ba42 2020-10-01 naddy usage(hflag, 1);
6709 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6713 9f7d7167 2018-04-29 stsp argc -= optind;
6714 9f7d7167 2018-04-29 stsp argv += optind;
6715 9814e6a3 2020-09-27 naddy optind = 1;
6716 c2301be8 2018-04-30 stsp optreset = 1;
6718 53ccebc2 2019-07-30 stsp if (Vflag) {
6719 53ccebc2 2019-07-30 stsp got_version_print_str();
6720 6879ba42 2020-10-01 naddy return 0;
6723 4010e238 2020-12-04 stsp #ifndef PROFILE
6724 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6725 4010e238 2020-12-04 stsp NULL) == -1)
6726 4010e238 2020-12-04 stsp err(1, "pledge");
6729 c2301be8 2018-04-30 stsp if (argc == 0) {
6730 f29d3e89 2018-06-23 stsp if (hflag)
6731 6879ba42 2020-10-01 naddy usage(hflag, 0);
6732 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6733 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6735 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6739 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6740 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6741 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6742 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6743 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6749 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6750 ee85c5e8 2020-02-29 stsp if (argc != 1)
6751 6879ba42 2020-10-01 naddy usage(0, 1);
6752 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6753 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6755 ee85c5e8 2020-02-29 stsp if (hflag)
6756 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6758 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6762 b46c1e04 2020-09-20 naddy putchar('\n');
6763 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6765 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6766 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6767 a2f4a359 2020-02-28 stsp free(cmd_argv);
6770 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6771 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);