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 61266923 2020-01-14 stsp #include <signal.h>
28 9f7d7167 2018-04-29 stsp #include <stdlib.h>
29 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
30 26ed57b2 2018-05-19 stsp #include <stdio.h>
31 9f7d7167 2018-04-29 stsp #include <getopt.h>
32 9f7d7167 2018-04-29 stsp #include <string.h>
33 9f7d7167 2018-04-29 stsp #include <err.h>
34 80ddbec8 2018-04-29 stsp #include <unistd.h>
35 26ed57b2 2018-05-19 stsp #include <limits.h>
36 61e69b96 2018-05-20 stsp #include <wchar.h>
37 788c352e 2018-06-16 stsp #include <time.h>
38 84451b3e 2018-07-10 stsp #include <pthread.h>
39 5036bf37 2018-09-24 stsp #include <libgen.h>
40 60493ae3 2019-06-20 stsp #include <regex.h>
41 3da8ef85 2021-09-21 stsp #include <sched.h>
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 9f7d7167 2018-04-29 stsp #include "got_error.h"
45 80ddbec8 2018-04-29 stsp #include "got_object.h"
46 80ddbec8 2018-04-29 stsp #include "got_reference.h"
47 80ddbec8 2018-04-29 stsp #include "got_repository.h"
48 80ddbec8 2018-04-29 stsp #include "got_diff.h"
49 511a516b 2018-05-19 stsp #include "got_opentemp.h"
50 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
51 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
52 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
53 a70480e0 2018-06-23 stsp #include "got_blame.h"
54 c2db6724 2019-01-04 stsp #include "got_privsep.h"
55 1dd54920 2019-05-11 stsp #include "got_path.h"
56 b7165be3 2019-02-05 stsp #include "got_worktree.h"
59 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 9f7d7167 2018-04-29 stsp #ifndef nitems
69 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 9f7d7167 2018-04-29 stsp struct tog_cmd {
73 c2301be8 2018-04-30 stsp const char *name;
74 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
75 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
78 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
82 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
83 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
88 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
89 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
92 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
93 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
94 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
95 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
96 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
99 d6b05b5b 2018-08-04 stsp enum tog_view_type {
100 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
102 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
103 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
104 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
107 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
109 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
110 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
111 ba4f502b 2018-08-04 stsp struct got_object_id *id;
112 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
115 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 ba4f502b 2018-08-04 stsp struct commit_queue {
117 ba4f502b 2018-08-04 stsp int ncommits;
118 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
121 f26dddb7 2019-11-08 stsp struct tog_color {
122 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
123 6d17833f 2019-11-08 stsp regex_t regex;
124 6d17833f 2019-11-08 stsp short colorpair;
126 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
128 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
131 11b20872 2019-11-08 stsp static const struct got_error *
132 51a10b52 2020-12-26 stsp tog_load_refs(struct got_repository *repo)
134 51a10b52 2020-12-26 stsp const struct got_error *err;
136 51a10b52 2020-12-26 stsp err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
138 51a10b52 2020-12-26 stsp return err;
140 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
144 51a10b52 2020-12-26 stsp static void
145 51a10b52 2020-12-26 stsp tog_free_refs(void)
147 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
148 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
149 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
151 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
154 51a10b52 2020-12-26 stsp static const struct got_error *
155 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
156 11b20872 2019-11-08 stsp int idx, short color)
158 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
159 11b20872 2019-11-08 stsp struct tog_color *tc;
160 11b20872 2019-11-08 stsp int regerr = 0;
162 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
163 11b20872 2019-11-08 stsp return NULL;
165 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
167 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
168 11b20872 2019-11-08 stsp if (tc == NULL)
169 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
170 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
171 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
172 11b20872 2019-11-08 stsp if (regerr) {
173 11b20872 2019-11-08 stsp static char regerr_msg[512];
174 11b20872 2019-11-08 stsp static char err_msg[512];
175 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
176 11b20872 2019-11-08 stsp sizeof(regerr_msg));
177 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
178 11b20872 2019-11-08 stsp regerr_msg);
179 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
181 11b20872 2019-11-08 stsp return err;
183 11b20872 2019-11-08 stsp tc->colorpair = idx;
184 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
185 11b20872 2019-11-08 stsp return NULL;
188 11b20872 2019-11-08 stsp static void
189 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
191 11b20872 2019-11-08 stsp struct tog_color *tc;
193 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
194 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
195 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
196 11b20872 2019-11-08 stsp regfree(&tc->regex);
201 11b20872 2019-11-08 stsp struct tog_color *
202 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
204 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
206 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
207 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
211 11b20872 2019-11-08 stsp return NULL;
215 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
217 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
218 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
219 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
220 11b20872 2019-11-08 stsp return COLOR_CYAN;
221 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
222 11b20872 2019-11-08 stsp return COLOR_YELLOW;
223 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
224 11b20872 2019-11-08 stsp return COLOR_GREEN;
225 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
226 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
227 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
228 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
229 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
230 91b8c405 2020-01-25 stsp return COLOR_CYAN;
231 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
232 11b20872 2019-11-08 stsp return COLOR_GREEN;
233 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
234 11b20872 2019-11-08 stsp return COLOR_GREEN;
235 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
236 11b20872 2019-11-08 stsp return COLOR_CYAN;
237 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
238 11b20872 2019-11-08 stsp return COLOR_YELLOW;
239 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
240 6458efa5 2020-11-24 stsp return COLOR_GREEN;
241 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
242 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
243 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
244 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
250 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
252 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
254 11b20872 2019-11-08 stsp if (val == NULL)
255 11b20872 2019-11-08 stsp return default_color_value(envvar);
257 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
258 11b20872 2019-11-08 stsp return COLOR_BLACK;
259 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
260 11b20872 2019-11-08 stsp return COLOR_RED;
261 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
262 11b20872 2019-11-08 stsp return COLOR_GREEN;
263 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
264 11b20872 2019-11-08 stsp return COLOR_YELLOW;
265 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
266 11b20872 2019-11-08 stsp return COLOR_BLUE;
267 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
268 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
270 11b20872 2019-11-08 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
272 11b20872 2019-11-08 stsp return COLOR_WHITE;
273 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
276 11b20872 2019-11-08 stsp return default_color_value(envvar);
280 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
281 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
282 3dbaef42 2020-11-24 stsp const char *label1, *label2;
284 15a087fe 2019-02-21 stsp int first_displayed_line;
285 15a087fe 2019-02-21 stsp int last_displayed_line;
287 15a087fe 2019-02-21 stsp int diff_context;
288 3dbaef42 2020-11-24 stsp int ignore_whitespace;
289 64453f7e 2020-11-21 stsp int force_text_diff;
290 15a087fe 2019-02-21 stsp struct got_repository *repo;
291 bddb1296 2019-11-08 stsp struct tog_colors colors;
292 fe621944 2020-11-10 stsp size_t nlines;
293 f44b1f58 2020-02-02 tracey off_t *line_offsets;
294 f44b1f58 2020-02-02 tracey int matched_line;
295 f44b1f58 2020-02-02 tracey int selected_line;
297 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
298 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
301 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
303 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
304 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
305 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
306 1a76625f 2018-10-22 stsp int commits_needed;
307 fb280deb 2021-08-30 stsp int load_all;
308 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
309 1a76625f 2018-10-22 stsp struct commit_queue *commits;
310 1a76625f 2018-10-22 stsp const char *in_repo_path;
311 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
312 1a76625f 2018-10-22 stsp struct got_repository *repo;
313 1a76625f 2018-10-22 stsp int log_complete;
314 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
315 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
316 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
317 13add988 2019-10-15 stsp int *searching;
318 13add988 2019-10-15 stsp int *search_next_done;
319 13add988 2019-10-15 stsp regex_t *regex;
322 1a76625f 2018-10-22 stsp struct tog_log_view_state {
323 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
324 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
325 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
326 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
327 b01e7d3b 2018-08-04 stsp int selected;
328 b01e7d3b 2018-08-04 stsp char *in_repo_path;
329 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
330 b672a97a 2020-01-27 stsp int log_branches;
331 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
332 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
333 1a76625f 2018-10-22 stsp sig_atomic_t quit;
334 1a76625f 2018-10-22 stsp pthread_t thread;
335 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
336 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
337 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
338 11b20872 2019-11-08 stsp struct tog_colors colors;
341 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
342 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
343 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
344 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
345 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
346 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
347 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
348 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
349 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
350 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
351 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
352 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
353 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
354 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
356 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
357 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
358 e9424729 2018-08-04 stsp int nlines;
360 e9424729 2018-08-04 stsp struct tog_view *view;
361 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
365 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
366 e9424729 2018-08-04 stsp const char *path;
367 e9424729 2018-08-04 stsp struct got_repository *repo;
368 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
369 e9424729 2018-08-04 stsp int *complete;
370 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
371 fc06ba56 2019-08-22 stsp void *cancel_arg;
374 e9424729 2018-08-04 stsp struct tog_blame {
376 be659d10 2020-11-18 stsp off_t filesize;
377 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
378 6fcac457 2018-11-19 stsp int nlines;
379 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
380 e9424729 2018-08-04 stsp pthread_t thread;
381 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
382 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
383 e9424729 2018-08-04 stsp const char *path;
386 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
387 7cbe629d 2018-08-04 stsp int first_displayed_line;
388 7cbe629d 2018-08-04 stsp int last_displayed_line;
389 7cbe629d 2018-08-04 stsp int selected_line;
390 7cbe629d 2018-08-04 stsp int blame_complete;
393 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
394 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
395 e5a0f69f 2018-08-18 stsp char *path;
396 7cbe629d 2018-08-04 stsp struct got_repository *repo;
397 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
398 e9424729 2018-08-04 stsp struct tog_blame blame;
399 6c4c42e0 2019-06-24 stsp int matched_line;
400 11b20872 2019-11-08 stsp struct tog_colors colors;
403 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
404 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
405 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
406 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
407 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
408 ad80ab7b 2018-08-04 stsp int selected;
411 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
413 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
414 ad80ab7b 2018-08-04 stsp char *tree_label;
415 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
416 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
417 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
418 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
419 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
420 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
421 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
422 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
423 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
424 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
425 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
426 6458efa5 2020-11-24 stsp struct tog_colors colors;
429 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
430 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
431 6458efa5 2020-11-24 stsp struct got_reference *ref;
435 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
437 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
438 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
439 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
440 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
441 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
442 6458efa5 2020-11-24 stsp int nrefs, ndisplayed, selected, show_ids;
443 6458efa5 2020-11-24 stsp struct got_repository *repo;
444 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
445 bddb1296 2019-11-08 stsp struct tog_colors colors;
449 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
451 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
452 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
453 669b5ffa 2018-10-07 stsp * there is enough screen estate.
455 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
456 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
458 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
459 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
460 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
462 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
463 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
465 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
467 cc3c9aac 2018-08-01 stsp struct tog_view {
468 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
469 26ed57b2 2018-05-19 stsp WINDOW *window;
470 26ed57b2 2018-05-19 stsp PANEL *panel;
471 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
472 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
473 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
475 669b5ffa 2018-10-07 stsp struct tog_view *parent;
476 669b5ffa 2018-10-07 stsp struct tog_view *child;
479 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
480 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
481 e78dc838 2020-12-04 stsp * between parent and child.
482 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
483 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
484 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
485 e78dc838 2020-12-04 stsp * situations.
487 e78dc838 2020-12-04 stsp int focus_child;
489 5dc9f4bc 2018-08-04 stsp /* type-specific state */
490 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
492 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
493 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
494 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
495 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
496 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
499 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
500 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
501 e78dc838 2020-12-04 stsp struct tog_view *, int);
502 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
504 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
505 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
506 c0c4acc8 2021-01-24 stsp int search_started;
507 60493ae3 2019-06-20 stsp int searching;
508 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
509 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
510 60493ae3 2019-06-20 stsp int search_next_done;
511 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
512 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
513 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
514 1803e47f 2019-06-22 stsp regex_t regex;
515 41605754 2020-11-12 stsp regmatch_t regmatch;
518 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
519 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
520 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
521 78756c87 2020-11-24 stsp struct got_repository *);
522 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
523 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
524 e78dc838 2020-12-04 stsp struct tog_view *, int);
525 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
526 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
527 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
529 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
530 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
531 78756c87 2020-11-24 stsp const char *, const char *, int);
532 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
533 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
534 e78dc838 2020-12-04 stsp struct tog_view *, int);
535 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
536 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
537 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
539 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
540 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
541 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
542 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
543 e78dc838 2020-12-04 stsp struct tog_view *, int);
544 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
545 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
546 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
548 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
549 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
550 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
551 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
552 e78dc838 2020-12-04 stsp struct tog_view *, int);
553 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
554 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
555 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
557 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
558 6458efa5 2020-11-24 stsp struct got_repository *);
559 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
560 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
561 e78dc838 2020-12-04 stsp struct tog_view *, int);
562 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
563 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
564 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
566 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
567 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
568 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
570 25791caa 2018-10-24 stsp static void
571 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
573 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
576 83baff54 2019-08-12 stsp static void
577 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
579 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
582 61266923 2020-01-14 stsp static void
583 61266923 2020-01-14 stsp tog_sigcont(int signo)
585 61266923 2020-01-14 stsp tog_sigcont_received = 1;
588 e5a0f69f 2018-08-18 stsp static const struct got_error *
589 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
591 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
593 669b5ffa 2018-10-07 stsp if (view->child) {
594 669b5ffa 2018-10-07 stsp view_close(view->child);
595 669b5ffa 2018-10-07 stsp view->child = NULL;
597 e5a0f69f 2018-08-18 stsp if (view->close)
598 e5a0f69f 2018-08-18 stsp err = view->close(view);
599 ea5e7bb5 2018-08-01 stsp if (view->panel)
600 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
601 ea5e7bb5 2018-08-01 stsp if (view->window)
602 ea5e7bb5 2018-08-01 stsp delwin(view->window);
603 ea5e7bb5 2018-08-01 stsp free(view);
604 e5a0f69f 2018-08-18 stsp return err;
607 ea5e7bb5 2018-08-01 stsp static struct tog_view *
608 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
609 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
611 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
613 ea5e7bb5 2018-08-01 stsp if (view == NULL)
614 ea5e7bb5 2018-08-01 stsp return NULL;
616 d6b05b5b 2018-08-04 stsp view->type = type;
617 f7d12f7e 2018-08-01 stsp view->lines = LINES;
618 f7d12f7e 2018-08-01 stsp view->cols = COLS;
619 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
620 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
621 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
622 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
623 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
624 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
625 96a765a8 2018-08-04 stsp view_close(view);
626 ea5e7bb5 2018-08-01 stsp return NULL;
628 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
629 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
630 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
631 96a765a8 2018-08-04 stsp view_close(view);
632 ea5e7bb5 2018-08-01 stsp return NULL;
635 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
636 ea5e7bb5 2018-08-01 stsp return view;
640 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
642 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
644 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
647 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
649 5c60c32a 2018-10-18 stsp static const struct got_error *
650 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
652 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
654 5c60c32a 2018-10-18 stsp view->begin_y = 0;
655 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
656 5c60c32a 2018-10-18 stsp view->nlines = LINES;
657 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
658 5c60c32a 2018-10-18 stsp view->lines = LINES;
659 5c60c32a 2018-10-18 stsp view->cols = COLS;
660 5c60c32a 2018-10-18 stsp err = view_resize(view);
662 5c60c32a 2018-10-18 stsp return err;
664 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
665 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
667 5c60c32a 2018-10-18 stsp return NULL;
670 5c60c32a 2018-10-18 stsp static const struct got_error *
671 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
673 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
675 5c60c32a 2018-10-18 stsp view->begin_x = 0;
676 5c60c32a 2018-10-18 stsp view->begin_y = 0;
677 5c60c32a 2018-10-18 stsp view->nlines = LINES;
678 5c60c32a 2018-10-18 stsp view->ncols = COLS;
679 5c60c32a 2018-10-18 stsp view->lines = LINES;
680 5c60c32a 2018-10-18 stsp view->cols = COLS;
681 5c60c32a 2018-10-18 stsp err = view_resize(view);
683 5c60c32a 2018-10-18 stsp return err;
685 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
686 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
688 5c60c32a 2018-10-18 stsp return NULL;
692 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
694 5c60c32a 2018-10-18 stsp return view->parent == NULL;
697 4d8c2215 2018-08-19 stsp static const struct got_error *
698 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
700 f7d12f7e 2018-08-01 stsp int nlines, ncols;
702 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
703 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
705 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
707 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
708 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
710 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
712 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
713 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
714 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
715 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
716 25791caa 2018-10-24 stsp wclear(view->window);
718 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
719 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
720 0cf4efb1 2018-09-29 stsp view->lines = LINES;
721 0cf4efb1 2018-09-29 stsp view->cols = COLS;
723 6e3e5d9c 2018-10-18 stsp if (view->child) {
724 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
725 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
726 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
727 5c60c32a 2018-10-18 stsp if (view->child->focussed)
728 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
730 5c60c32a 2018-10-18 stsp show_panel(view->panel);
732 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
733 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
737 5c60c32a 2018-10-18 stsp return NULL;
740 669b5ffa 2018-10-07 stsp static const struct got_error *
741 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
743 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
745 669b5ffa 2018-10-07 stsp if (view->child == NULL)
746 669b5ffa 2018-10-07 stsp return NULL;
748 669b5ffa 2018-10-07 stsp err = view_close(view->child);
749 669b5ffa 2018-10-07 stsp view->child = NULL;
750 669b5ffa 2018-10-07 stsp return err;
753 72a9cb46 2020-12-03 stsp static void
754 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
756 669b5ffa 2018-10-07 stsp view->child = child;
757 669b5ffa 2018-10-07 stsp child->parent = view;
761 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
763 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
766 34bc9ec9 2019-02-22 stsp static void
767 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
769 25791caa 2018-10-24 stsp int cols, lines;
770 25791caa 2018-10-24 stsp struct winsize size;
772 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
773 25791caa 2018-10-24 stsp cols = 80; /* Default */
774 25791caa 2018-10-24 stsp lines = 24;
776 25791caa 2018-10-24 stsp cols = size.ws_col;
777 25791caa 2018-10-24 stsp lines = size.ws_row;
779 25791caa 2018-10-24 stsp resize_term(lines, cols);
782 2b49a8ae 2019-06-22 stsp static const struct got_error *
783 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
785 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
786 2b49a8ae 2019-06-22 stsp char pattern[1024];
789 c0c4acc8 2021-01-24 stsp if (view->search_started) {
790 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
791 c0c4acc8 2021-01-24 stsp view->searching = 0;
792 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
794 c0c4acc8 2021-01-24 stsp view->search_started = 0;
796 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
797 2b49a8ae 2019-06-22 stsp return NULL;
799 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
800 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
802 2b49a8ae 2019-06-22 stsp nocbreak();
804 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
807 2b49a8ae 2019-06-22 stsp if (ret == ERR)
808 2b49a8ae 2019-06-22 stsp return NULL;
810 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
811 7c32bd05 2019-06-22 stsp err = view->search_start(view);
813 7c32bd05 2019-06-22 stsp regfree(&view->regex);
814 7c32bd05 2019-06-22 stsp return err;
816 c0c4acc8 2021-01-24 stsp view->search_started = 1;
817 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
818 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
819 2b49a8ae 2019-06-22 stsp view->search_next(view);
822 2b49a8ae 2019-06-22 stsp return NULL;
825 0cf4efb1 2018-09-29 stsp static const struct got_error *
826 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
827 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
829 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
830 669b5ffa 2018-10-07 stsp struct tog_view *v;
831 1a76625f 2018-10-22 stsp int ch, errcode;
833 e5a0f69f 2018-08-18 stsp *new = NULL;
835 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
836 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
837 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
838 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
840 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
841 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
842 82954512 2020-02-03 stsp if (errcode)
843 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
844 82954512 2020-02-03 stsp "pthread_mutex_unlock");
845 3da8ef85 2021-09-21 stsp sched_yield();
846 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
847 82954512 2020-02-03 stsp if (errcode)
848 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
849 82954512 2020-02-03 stsp "pthread_mutex_lock");
850 60493ae3 2019-06-20 stsp view->search_next(view);
851 60493ae3 2019-06-20 stsp return NULL;
854 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
855 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
856 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
857 1a76625f 2018-10-22 stsp if (errcode)
858 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
860 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
861 1a76625f 2018-10-22 stsp if (errcode)
862 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
863 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
865 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
866 25791caa 2018-10-24 stsp tog_resizeterm();
867 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
868 61266923 2020-01-14 stsp tog_sigcont_received = 0;
869 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
870 25791caa 2018-10-24 stsp err = view_resize(v);
872 25791caa 2018-10-24 stsp return err;
873 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
875 25791caa 2018-10-24 stsp return err;
876 cdfcfb03 2020-12-06 stsp if (v->child) {
877 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
879 cdfcfb03 2020-12-06 stsp return err;
880 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
881 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
883 cdfcfb03 2020-12-06 stsp return err;
888 e5a0f69f 2018-08-18 stsp switch (ch) {
890 1e37a5c2 2019-05-12 jcs if (view->child) {
891 e78dc838 2020-12-04 stsp view->focussed = 0;
892 e78dc838 2020-12-04 stsp view->child->focussed = 1;
893 e78dc838 2020-12-04 stsp view->focus_child = 1;
894 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
895 e78dc838 2020-12-04 stsp view->focussed = 0;
896 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
897 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
901 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
902 9970f7fc 2020-12-03 stsp view->dying = 1;
908 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
909 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
911 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
912 e78dc838 2020-12-04 stsp view->focussed = 0;
913 e78dc838 2020-12-04 stsp view->child->focussed = 1;
914 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
916 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
919 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
920 9970f7fc 2020-12-03 stsp KEY_RESIZE);
922 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
923 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
924 e78dc838 2020-12-04 stsp view->focussed = 1;
925 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
927 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
931 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
934 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
937 60493ae3 2019-06-20 stsp if (view->search_start)
938 2b49a8ae 2019-06-22 stsp view_search_start(view);
940 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
944 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
945 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
946 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
947 60493ae3 2019-06-20 stsp view->search_next_done = 0;
948 60493ae3 2019-06-20 stsp view->search_next(view);
950 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
953 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
957 e5a0f69f 2018-08-18 stsp return err;
961 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
963 0cf4efb1 2018-09-29 stsp PANEL *panel;
964 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
966 669b5ffa 2018-10-07 stsp if (view->parent)
967 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
969 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
970 0cf4efb1 2018-09-29 stsp if (panel == NULL)
973 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
974 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
975 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
979 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
981 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
982 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
984 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
986 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
989 669b5ffa 2018-10-07 stsp return view->focussed;
992 bcbd79e2 2018-08-19 stsp static const struct got_error *
993 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
995 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
996 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
997 fb59748f 2020-12-05 stsp struct tog_view *new_view;
998 fd823528 2018-10-22 stsp int fast_refresh = 10;
999 1a76625f 2018-10-22 stsp int done = 0, errcode;
1001 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1002 1a76625f 2018-10-22 stsp if (errcode)
1003 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1005 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1006 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1008 1004088d 2018-09-29 stsp view->focussed = 1;
1009 878940b7 2018-09-29 stsp err = view->show(view);
1011 0cf4efb1 2018-09-29 stsp return err;
1012 0cf4efb1 2018-09-29 stsp update_panels();
1013 0cf4efb1 2018-09-29 stsp doupdate();
1014 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1015 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1016 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1017 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1019 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1022 9970f7fc 2020-12-03 stsp if (view->dying) {
1023 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1025 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1026 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1028 e78dc838 2020-12-04 stsp else if (view->parent)
1029 669b5ffa 2018-10-07 stsp prev = view->parent;
1031 e78dc838 2020-12-04 stsp if (view->parent) {
1032 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1033 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1035 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1037 9970f7fc 2020-12-03 stsp err = view_close(view);
1039 e5a0f69f 2018-08-18 stsp goto done;
1041 e78dc838 2020-12-04 stsp view = NULL;
1042 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1043 e78dc838 2020-12-04 stsp if (v->focussed)
1046 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1047 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1049 e78dc838 2020-12-04 stsp view = prev;
1050 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1051 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1052 e78dc838 2020-12-04 stsp tog_view_list_head);
1054 e78dc838 2020-12-04 stsp if (view) {
1055 e78dc838 2020-12-04 stsp if (view->focus_child) {
1056 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1057 e78dc838 2020-12-04 stsp view = view->child;
1059 e78dc838 2020-12-04 stsp view->focussed = 1;
1063 bcbd79e2 2018-08-19 stsp if (new_view) {
1064 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1065 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1066 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1067 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1069 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1070 86c66b02 2018-10-18 stsp err = view_close(v);
1072 86c66b02 2018-10-18 stsp goto done;
1075 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1076 fed7eaa8 2018-10-24 stsp view = new_view;
1078 669b5ffa 2018-10-07 stsp if (view) {
1079 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1080 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1081 e78dc838 2020-12-04 stsp view = view->child;
1083 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1084 e78dc838 2020-12-04 stsp view = view->parent;
1086 e78dc838 2020-12-04 stsp show_panel(view->panel);
1087 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1088 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1089 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1090 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1092 1a76625f 2018-10-22 stsp goto done;
1094 669b5ffa 2018-10-07 stsp err = view->show(view);
1096 1a76625f 2018-10-22 stsp goto done;
1097 669b5ffa 2018-10-07 stsp if (view->child) {
1098 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1100 1a76625f 2018-10-22 stsp goto done;
1102 1a76625f 2018-10-22 stsp update_panels();
1103 1a76625f 2018-10-22 stsp doupdate();
1107 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1108 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1109 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1110 e5a0f69f 2018-08-18 stsp view_close(view);
1113 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1114 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1115 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1117 e5a0f69f 2018-08-18 stsp return err;
1120 4ed7e80c 2018-05-20 stsp __dead static void
1121 9f7d7167 2018-04-29 stsp usage_log(void)
1124 c70c5802 2018-08-01 stsp fprintf(stderr,
1125 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1126 9f7d7167 2018-04-29 stsp getprogname());
1130 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1131 80ddbec8 2018-04-29 stsp static const struct got_error *
1132 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1134 00dfcb92 2018-06-11 stsp char *vis = NULL;
1135 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1137 963b370f 2018-05-20 stsp *ws = NULL;
1138 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1139 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1140 00dfcb92 2018-06-11 stsp int vislen;
1141 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1142 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1144 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1145 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1147 00dfcb92 2018-06-11 stsp return err;
1148 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1149 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1150 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1151 a7f50699 2018-06-11 stsp goto done;
1155 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1156 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1158 a7f50699 2018-06-11 stsp goto done;
1161 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1162 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1164 00dfcb92 2018-06-11 stsp free(vis);
1165 963b370f 2018-05-20 stsp if (err) {
1166 963b370f 2018-05-20 stsp free(*ws);
1167 963b370f 2018-05-20 stsp *ws = NULL;
1168 963b370f 2018-05-20 stsp *wlen = 0;
1170 963b370f 2018-05-20 stsp return err;
1173 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1174 963b370f 2018-05-20 stsp static const struct got_error *
1175 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1176 27a741e5 2019-09-11 stsp int col_tab_align)
1178 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1179 963b370f 2018-05-20 stsp int cols = 0;
1180 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1181 963b370f 2018-05-20 stsp size_t wlen;
1184 963b370f 2018-05-20 stsp *wlinep = NULL;
1185 b700b5d6 2018-07-10 stsp *widthp = 0;
1187 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1189 963b370f 2018-05-20 stsp return err;
1191 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1192 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1195 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1196 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1201 27a741e5 2019-09-11 stsp while (i < wlen) {
1202 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1204 27a741e5 2019-09-11 stsp if (width == 0) {
1209 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1210 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1212 27a741e5 2019-09-11 stsp cols += width;
1214 27a741e5 2019-09-11 stsp } else if (width == -1) {
1215 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1216 27a741e5 2019-09-11 stsp width = TABSIZE -
1217 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1219 748d5cab 2020-12-14 naddy width = 1;
1220 748d5cab 2020-12-14 naddy wline[i] = L'.';
1222 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1224 748d5cab 2020-12-14 naddy cols += width;
1227 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1228 963b370f 2018-05-20 stsp goto done;
1231 963b370f 2018-05-20 stsp wline[i] = L'\0';
1232 b700b5d6 2018-07-10 stsp if (widthp)
1233 b700b5d6 2018-07-10 stsp *widthp = cols;
1236 963b370f 2018-05-20 stsp free(wline);
1238 963b370f 2018-05-20 stsp *wlinep = wline;
1239 963b370f 2018-05-20 stsp return err;
1242 8b473291 2019-02-21 stsp static const struct got_error*
1243 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1244 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1246 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1247 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1249 8b473291 2019-02-21 stsp const char *name;
1251 8b473291 2019-02-21 stsp *refs_str = NULL;
1253 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1254 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1255 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1258 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1259 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1261 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1262 8b473291 2019-02-21 stsp name += 5;
1263 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1265 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1266 8b473291 2019-02-21 stsp name += 6;
1267 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1268 8b473291 2019-02-21 stsp name += 8;
1269 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1270 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1273 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1276 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1277 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1278 5d844a1e 2019-08-13 stsp if (err) {
1279 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1280 48cae60d 2020-09-22 stsp free(ref_id);
1283 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1284 5d844a1e 2019-08-13 stsp err = NULL;
1285 5d844a1e 2019-08-13 stsp tag = NULL;
1288 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1289 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1290 48cae60d 2020-09-22 stsp free(ref_id);
1292 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1293 52b5abe1 2019-08-13 stsp if (cmp != 0)
1295 8b473291 2019-02-21 stsp s = *refs_str;
1296 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1297 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1298 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1300 8b473291 2019-02-21 stsp *refs_str = NULL;
1306 8b473291 2019-02-21 stsp return err;
1309 963b370f 2018-05-20 stsp static const struct got_error *
1310 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1311 27a741e5 2019-09-11 stsp int col_tab_align)
1313 e6b8b890 2020-12-29 naddy char *smallerthan;
1315 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1316 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1317 5813d178 2019-03-09 stsp author = smallerthan + 1;
1318 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1319 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1322 5813d178 2019-03-09 stsp static const struct got_error *
1323 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1324 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1325 8fdc79fe 2020-12-01 naddy int author_display_cols)
1327 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1328 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1329 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1330 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1331 5813d178 2019-03-09 stsp char *author = NULL;
1332 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1333 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1334 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1335 bb737323 2018-05-20 stsp int col, limit;
1336 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1337 ccb26ccd 2018-11-05 stsp struct tm tm;
1338 45d799e2 2018-12-23 stsp time_t committer_time;
1339 11b20872 2019-11-08 stsp struct tog_color *tc;
1341 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1342 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1343 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1344 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1345 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1347 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1348 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1350 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1351 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1353 11b20872 2019-11-08 stsp wattr_on(view->window,
1354 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1355 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1357 11b20872 2019-11-08 stsp wattr_off(view->window,
1358 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1359 27a741e5 2019-09-11 stsp col = limit;
1360 b39d25c7 2018-07-10 stsp if (col > avail)
1361 b39d25c7 2018-07-10 stsp goto done;
1363 6570a66d 2019-11-08 stsp if (avail >= 120) {
1364 6570a66d 2019-11-08 stsp char *id_str;
1365 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1367 6570a66d 2019-11-08 stsp goto done;
1368 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1370 11b20872 2019-11-08 stsp wattr_on(view->window,
1371 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1372 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1374 11b20872 2019-11-08 stsp wattr_off(view->window,
1375 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1376 6570a66d 2019-11-08 stsp free(id_str);
1378 6570a66d 2019-11-08 stsp if (col > avail)
1379 6570a66d 2019-11-08 stsp goto done;
1382 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1383 5813d178 2019-03-09 stsp if (author == NULL) {
1384 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1385 80ddbec8 2018-04-29 stsp goto done;
1387 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1389 bb737323 2018-05-20 stsp goto done;
1390 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1392 11b20872 2019-11-08 stsp wattr_on(view->window,
1393 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1394 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1396 11b20872 2019-11-08 stsp wattr_off(view->window,
1397 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1398 bb737323 2018-05-20 stsp col += author_width;
1399 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1400 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1402 bb737323 2018-05-20 stsp author_width++;
1404 9c2eaf34 2018-05-20 stsp if (col > avail)
1405 9c2eaf34 2018-05-20 stsp goto done;
1407 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1409 6d9fbc00 2018-04-29 stsp goto done;
1410 bb737323 2018-05-20 stsp logmsg = logmsg0;
1411 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1413 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1414 bb737323 2018-05-20 stsp if (newline)
1415 bb737323 2018-05-20 stsp *newline = '\0';
1416 bb737323 2018-05-20 stsp limit = avail - col;
1417 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1419 bb737323 2018-05-20 stsp goto done;
1420 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1421 bb737323 2018-05-20 stsp col += logmsg_width;
1422 27a741e5 2019-09-11 stsp while (col < avail) {
1423 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1427 80ddbec8 2018-04-29 stsp free(logmsg0);
1428 bb737323 2018-05-20 stsp free(wlogmsg);
1429 5813d178 2019-03-09 stsp free(author);
1430 bb737323 2018-05-20 stsp free(wauthor);
1431 80ddbec8 2018-04-29 stsp free(line);
1432 80ddbec8 2018-04-29 stsp return err;
1435 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1436 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1437 899d86c2 2018-05-10 stsp struct got_object_id *id)
1439 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1441 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1442 80ddbec8 2018-04-29 stsp if (entry == NULL)
1443 899d86c2 2018-05-10 stsp return NULL;
1445 899d86c2 2018-05-10 stsp entry->id = id;
1446 99db9666 2018-05-07 stsp entry->commit = commit;
1447 899d86c2 2018-05-10 stsp return entry;
1450 99db9666 2018-05-07 stsp static void
1451 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1453 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1455 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1456 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1457 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1458 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1459 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1460 99db9666 2018-05-07 stsp free(entry);
1463 99db9666 2018-05-07 stsp static void
1464 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1466 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1467 99db9666 2018-05-07 stsp pop_commit(commits);
1470 c4972b91 2018-05-07 stsp static const struct got_error *
1471 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1472 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1474 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1475 13add988 2019-10-15 stsp regmatch_t regmatch;
1476 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1478 13add988 2019-10-15 stsp *have_match = 0;
1480 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1482 13add988 2019-10-15 stsp return err;
1484 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1486 13add988 2019-10-15 stsp goto done;
1488 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1489 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1490 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1491 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1492 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1493 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1494 13add988 2019-10-15 stsp *have_match = 1;
1496 13add988 2019-10-15 stsp free(id_str);
1497 13add988 2019-10-15 stsp free(logmsg);
1498 13add988 2019-10-15 stsp return err;
1501 13add988 2019-10-15 stsp static const struct got_error *
1502 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1504 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1507 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1508 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1509 1a76625f 2018-10-22 stsp * while updating the display.
1512 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1513 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1514 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1515 1a76625f 2018-10-22 stsp int errcode;
1517 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1518 4e0d2870 2020-12-07 naddy NULL, NULL);
1519 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1522 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1525 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1526 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1527 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1531 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1532 1a76625f 2018-10-22 stsp if (errcode) {
1533 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1534 13add988 2019-10-15 stsp "pthread_mutex_lock");
1538 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1539 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1540 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1542 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1543 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1544 7c1452c1 2020-03-26 stsp int have_match;
1545 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1548 7c1452c1 2020-03-26 stsp if (have_match)
1549 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1552 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1553 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1554 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1555 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1558 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1560 9ba79e04 2018-06-11 stsp return err;
1563 2b779855 2020-12-05 naddy static void
1564 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1566 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1567 2b779855 2020-12-05 naddy int ncommits = 0;
1569 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1570 2b779855 2020-12-05 naddy while (entry) {
1571 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1572 2b779855 2020-12-05 naddy s->selected_entry = entry;
1575 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1576 2b779855 2020-12-05 naddy ncommits++;
1580 0553a4e3 2018-04-30 stsp static const struct got_error *
1581 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1583 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1584 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1585 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1586 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1587 60493ae3 2019-06-20 stsp int width;
1588 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1589 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1590 8b473291 2019-02-21 stsp char *refs_str = NULL;
1591 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1592 11b20872 2019-11-08 stsp struct tog_color *tc;
1593 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1595 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1596 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1597 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1598 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1600 ecb28ae0 2018-07-16 stsp return err;
1601 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1602 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1603 d2075bf3 2020-12-25 stsp if (refs) {
1604 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1605 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1607 d2075bf3 2020-12-25 stsp goto done;
1611 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1612 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1614 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1615 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1616 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1617 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1618 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1619 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1620 8f4ed634 2020-03-26 stsp goto done;
1623 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1625 f9686aa5 2020-03-27 stsp if (view->searching) {
1626 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1627 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1628 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1629 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1630 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1631 f9686aa5 2020-03-27 stsp search_str = "searching...";
1634 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1635 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1636 f9686aa5 2020-03-27 stsp search_str ? search_str :
1637 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1638 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1639 8f4ed634 2020-03-26 stsp goto done;
1643 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1644 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1645 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1646 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1647 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1648 1a76625f 2018-10-22 stsp header = NULL;
1649 1a76625f 2018-10-22 stsp goto done;
1651 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1652 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1653 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1654 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1655 1a76625f 2018-10-22 stsp header = NULL;
1656 1a76625f 2018-10-22 stsp goto done;
1658 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1660 1a76625f 2018-10-22 stsp goto done;
1662 2814baeb 2018-08-01 stsp werase(view->window);
1664 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1665 a3404814 2018-09-02 stsp wstandout(view->window);
1666 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1668 11b20872 2019-11-08 stsp wattr_on(view->window,
1669 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1670 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1672 11b20872 2019-11-08 stsp wattr_off(view->window,
1673 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1674 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1675 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1678 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1679 a3404814 2018-09-02 stsp wstandend(view->window);
1680 ecb28ae0 2018-07-16 stsp free(wline);
1681 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1682 1a76625f 2018-10-22 stsp goto done;
1684 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1685 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1686 5813d178 2019-03-09 stsp ncommits = 0;
1687 5813d178 2019-03-09 stsp while (entry) {
1688 5813d178 2019-03-09 stsp char *author;
1689 5813d178 2019-03-09 stsp wchar_t *wauthor;
1690 5813d178 2019-03-09 stsp int width;
1691 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1693 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1694 5813d178 2019-03-09 stsp if (author == NULL) {
1695 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1696 5813d178 2019-03-09 stsp goto done;
1698 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1699 27a741e5 2019-09-11 stsp date_display_cols);
1700 5813d178 2019-03-09 stsp if (author_cols < width)
1701 5813d178 2019-03-09 stsp author_cols = width;
1702 5813d178 2019-03-09 stsp free(wauthor);
1703 5813d178 2019-03-09 stsp free(author);
1704 7ca04879 2019-10-19 stsp ncommits++;
1705 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1708 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1709 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1710 867c6645 2018-07-10 stsp ncommits = 0;
1711 899d86c2 2018-05-10 stsp while (entry) {
1712 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1714 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1715 2814baeb 2018-08-01 stsp wstandout(view->window);
1716 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1717 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1718 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1719 2814baeb 2018-08-01 stsp wstandend(view->window);
1721 60493ae3 2019-06-20 stsp goto done;
1722 0553a4e3 2018-04-30 stsp ncommits++;
1723 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1724 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1727 1a57306a 2018-09-02 stsp view_vborder(view);
1729 1a76625f 2018-10-22 stsp free(id_str);
1730 8b473291 2019-02-21 stsp free(refs_str);
1731 1a76625f 2018-10-22 stsp free(ncommits_str);
1732 1a76625f 2018-10-22 stsp free(header);
1733 80ddbec8 2018-04-29 stsp return err;
1736 07b55e75 2018-05-10 stsp static void
1737 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1739 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1740 07b55e75 2018-05-10 stsp int nscrolled = 0;
1742 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1743 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1746 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1747 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1748 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1749 07b55e75 2018-05-10 stsp if (entry) {
1750 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1751 07b55e75 2018-05-10 stsp nscrolled++;
1756 aa075928 2018-05-10 stsp static const struct got_error *
1757 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1759 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1760 5e224a3e 2019-02-22 stsp int errcode;
1762 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1764 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1765 ffe38506 2020-12-01 naddy if (ta->log_complete)
1768 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1769 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1770 7aafa0d1 2019-02-22 stsp if (errcode)
1771 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1772 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1775 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1776 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1778 7c1452c1 2020-03-26 stsp if (!wait)
1781 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1782 ffe38506 2020-12-01 naddy show_log_view(view);
1783 7c1452c1 2020-03-26 stsp update_panels();
1784 7c1452c1 2020-03-26 stsp doupdate();
1786 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1787 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1788 82954512 2020-02-03 stsp if (errcode)
1789 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1790 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1792 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1793 ffe38506 2020-12-01 naddy show_log_view(view);
1794 7c1452c1 2020-03-26 stsp update_panels();
1795 7c1452c1 2020-03-26 stsp doupdate();
1798 5e224a3e 2019-02-22 stsp return NULL;
1801 5e224a3e 2019-02-22 stsp static const struct got_error *
1802 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1804 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1805 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1806 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1807 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1809 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1810 5e224a3e 2019-02-22 stsp return NULL;
1812 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1813 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1814 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1816 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1818 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1819 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1821 5e224a3e 2019-02-22 stsp return err;
1825 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1826 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1829 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1831 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1832 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1834 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1835 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1837 dd0a52c1 2018-05-20 stsp return err;
1840 cd0acaa7 2018-05-20 stsp static const struct got_error *
1841 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1842 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1843 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1845 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1846 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1847 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1849 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1850 15a94983 2018-12-23 stsp if (diff_view == NULL)
1851 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1853 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1854 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1855 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1856 e5a0f69f 2018-08-18 stsp if (err == NULL)
1857 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1858 cd0acaa7 2018-05-20 stsp return err;
1861 80ddbec8 2018-04-29 stsp static const struct got_error *
1862 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1863 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1865 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1867 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1868 941e9f74 2019-05-21 stsp if (parent == NULL)
1869 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1871 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1872 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1873 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1874 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1875 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1876 941e9f74 2019-05-21 stsp s->tree = subtree;
1877 941e9f74 2019-05-21 stsp s->selected = 0;
1878 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1879 941e9f74 2019-05-21 stsp return NULL;
1882 941e9f74 2019-05-21 stsp static const struct got_error *
1883 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1884 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1886 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1887 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1888 941e9f74 2019-05-21 stsp const char *p;
1889 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1891 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1893 941e9f74 2019-05-21 stsp while (*p) {
1894 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1895 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1896 56e0773d 2019-11-28 stsp char *te_name;
1898 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1901 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1902 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1903 941e9f74 2019-05-21 stsp if (slash == NULL)
1904 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1906 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1907 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1908 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1911 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1912 56e0773d 2019-11-28 stsp if (te == NULL) {
1913 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1914 56e0773d 2019-11-28 stsp free(te_name);
1917 56e0773d 2019-11-28 stsp free(te_name);
1918 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1920 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1921 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1923 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1924 941e9f74 2019-05-21 stsp if (slash)
1925 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1927 941e9f74 2019-05-21 stsp subpath = strdup(path);
1928 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1929 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1933 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1938 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1939 941e9f74 2019-05-21 stsp free(tree_id);
1943 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1944 941e9f74 2019-05-21 stsp if (err) {
1945 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1948 941e9f74 2019-05-21 stsp if (slash == NULL)
1950 941e9f74 2019-05-21 stsp free(subpath);
1951 941e9f74 2019-05-21 stsp subpath = NULL;
1952 941e9f74 2019-05-21 stsp p = slash;
1955 941e9f74 2019-05-21 stsp free(subpath);
1956 1a76625f 2018-10-22 stsp return err;
1959 61266923 2020-01-14 stsp static const struct got_error *
1960 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1961 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1962 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
1964 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1965 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1966 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1968 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1969 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1970 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1972 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1974 55cccc34 2020-02-20 stsp return err;
1975 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1977 55cccc34 2020-02-20 stsp *new_view = tree_view;
1979 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1980 55cccc34 2020-02-20 stsp return NULL;
1982 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1985 55cccc34 2020-02-20 stsp static const struct got_error *
1986 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1988 61266923 2020-01-14 stsp sigset_t sigset;
1989 61266923 2020-01-14 stsp int errcode;
1991 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1992 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1994 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1995 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1996 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1997 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1998 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2000 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2001 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2002 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2004 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2005 61266923 2020-01-14 stsp if (errcode)
2006 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2008 61266923 2020-01-14 stsp return NULL;
2011 1a76625f 2018-10-22 stsp static void *
2012 1a76625f 2018-10-22 stsp log_thread(void *arg)
2014 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2015 1a76625f 2018-10-22 stsp int errcode = 0;
2016 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2017 1a76625f 2018-10-22 stsp int done = 0;
2019 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2021 61266923 2020-01-14 stsp return (void *)err;
2023 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2024 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2025 1a76625f 2018-10-22 stsp if (err) {
2026 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2027 1a76625f 2018-10-22 stsp return (void *)err;
2028 1a76625f 2018-10-22 stsp err = NULL;
2030 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2031 1a76625f 2018-10-22 stsp a->commits_needed--;
2033 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2034 3abe8080 2019-04-10 stsp if (errcode) {
2035 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2036 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2038 3abe8080 2019-04-10 stsp } else if (*a->quit)
2040 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2041 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2042 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2043 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2046 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2047 7c1452c1 2020-03-26 stsp if (errcode) {
2048 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2049 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2050 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2055 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2057 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2058 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2059 7c1452c1 2020-03-26 stsp &tog_mutex);
2060 7c1452c1 2020-03-26 stsp if (errcode)
2061 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2062 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2063 21355643 2020-12-06 stsp if (*a->quit)
2068 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2069 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2070 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2071 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2073 3abe8080 2019-04-10 stsp a->log_complete = 1;
2074 1a76625f 2018-10-22 stsp return (void *)err;
2077 1a76625f 2018-10-22 stsp static const struct got_error *
2078 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2080 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2081 1a76625f 2018-10-22 stsp int errcode;
2083 1a76625f 2018-10-22 stsp if (s->thread) {
2084 1a76625f 2018-10-22 stsp s->quit = 1;
2085 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2086 1a76625f 2018-10-22 stsp if (errcode)
2087 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2088 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2090 1a76625f 2018-10-22 stsp if (errcode)
2091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2092 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2093 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2094 1a76625f 2018-10-22 stsp if (errcode)
2095 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2096 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2097 1a76625f 2018-10-22 stsp if (errcode)
2098 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2099 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2100 1a76625f 2018-10-22 stsp s->thread = NULL;
2103 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2104 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2105 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2108 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2109 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2110 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2113 9343a5fb 2018-06-23 stsp return err;
2116 9343a5fb 2018-06-23 stsp static const struct got_error *
2117 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2119 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2120 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2121 276b94a1 2020-11-13 naddy int errcode;
2123 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2125 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2126 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2127 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2129 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2130 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2131 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2133 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2134 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2135 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2136 1a76625f 2018-10-22 stsp free(s->start_id);
2137 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2138 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2139 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2140 1a76625f 2018-10-22 stsp return err;
2143 1a76625f 2018-10-22 stsp static const struct got_error *
2144 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2146 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2148 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2149 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2150 60493ae3 2019-06-20 stsp return NULL;
2153 60493ae3 2019-06-20 stsp static const struct got_error *
2154 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2156 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2157 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2158 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2160 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2161 f9686aa5 2020-03-27 stsp show_log_view(view);
2162 f9686aa5 2020-03-27 stsp update_panels();
2163 f9686aa5 2020-03-27 stsp doupdate();
2165 96e2b566 2019-07-08 stsp if (s->search_entry) {
2166 13add988 2019-10-15 stsp int errcode, ch;
2167 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2168 13add988 2019-10-15 stsp if (errcode)
2169 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2170 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2171 13add988 2019-10-15 stsp ch = wgetch(view->window);
2172 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2173 13add988 2019-10-15 stsp if (errcode)
2174 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2175 13add988 2019-10-15 stsp "pthread_mutex_lock");
2176 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2177 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2178 678cbce5 2019-07-28 stsp return NULL;
2180 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2181 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2183 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2184 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2185 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2186 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2187 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2189 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2190 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2192 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2193 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2195 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2198 60493ae3 2019-06-20 stsp while (1) {
2199 13add988 2019-10-15 stsp int have_match = 0;
2201 60493ae3 2019-06-20 stsp if (entry == NULL) {
2202 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2203 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2204 f9967bca 2020-03-27 stsp view->search_next_done =
2205 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2206 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2207 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2208 f801134a 2019-06-25 stsp return NULL;
2211 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2212 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2213 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2215 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2216 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2219 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2220 13add988 2019-10-15 stsp &view->regex);
2223 13add988 2019-10-15 stsp if (have_match) {
2224 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2225 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2229 96e2b566 2019-07-08 stsp s->search_entry = entry;
2230 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2231 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2233 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2236 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2237 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2238 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2239 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2241 60493ae3 2019-06-20 stsp return err;
2244 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2245 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2247 60493ae3 2019-06-20 stsp return err;
2252 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2254 60493ae3 2019-06-20 stsp return NULL;
2257 60493ae3 2019-06-20 stsp static const struct got_error *
2258 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2259 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2260 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2262 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2263 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2264 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2265 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2266 1a76625f 2018-10-22 stsp int errcode;
2268 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2269 f135c941 2020-02-20 stsp free(s->in_repo_path);
2270 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2271 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2272 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2275 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2276 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2277 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2279 fb2756b9 2018-08-04 stsp s->repo = repo;
2280 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2281 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2282 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2283 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2284 9cd7cbd1 2020-12-07 stsp goto done;
2287 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2288 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2289 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2290 5036bf37 2018-09-24 stsp goto done;
2292 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2294 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2295 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2296 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2297 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2299 11b20872 2019-11-08 stsp goto done;
2300 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2301 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2302 11b20872 2019-11-08 stsp if (err) {
2303 11b20872 2019-11-08 stsp free_colors(&s->colors);
2304 11b20872 2019-11-08 stsp goto done;
2306 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2307 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2308 11b20872 2019-11-08 stsp if (err) {
2309 11b20872 2019-11-08 stsp free_colors(&s->colors);
2310 11b20872 2019-11-08 stsp goto done;
2314 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2315 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2316 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2317 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2318 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2320 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2322 1a76625f 2018-10-22 stsp goto done;
2323 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2324 b672a97a 2020-01-27 stsp !s->log_branches);
2326 1a76625f 2018-10-22 stsp goto done;
2327 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2328 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2330 c5b78334 2020-01-12 stsp goto done;
2332 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2333 1a76625f 2018-10-22 stsp if (errcode) {
2334 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2335 1a76625f 2018-10-22 stsp goto done;
2337 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2338 7c1452c1 2020-03-26 stsp if (errcode) {
2339 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2340 7c1452c1 2020-03-26 stsp goto done;
2343 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2344 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2345 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2346 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2347 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2348 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2349 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2350 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2351 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2352 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2353 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2354 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2355 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2358 1a76625f 2018-10-22 stsp close_log_view(view);
2359 ba4f502b 2018-08-04 stsp return err;
2362 e5a0f69f 2018-08-18 stsp static const struct got_error *
2363 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2365 f2f6d207 2020-11-24 stsp const struct got_error *err;
2366 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2368 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2369 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2370 2b380cc8 2018-10-24 stsp &s->thread_args);
2371 2b380cc8 2018-10-24 stsp if (errcode)
2372 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2373 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2374 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2376 f2f6d207 2020-11-24 stsp return err;
2380 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2383 e5a0f69f 2018-08-18 stsp static const struct got_error *
2384 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2386 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2387 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2388 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2389 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2390 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2391 f3bc9f1d 2021-09-05 naddy int begin_x = 0, n;
2393 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2394 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2395 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2396 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2397 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2398 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2399 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2400 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2401 fb280deb 2021-08-30 stsp select_commit(s);
2403 528dedf3 2021-08-30 stsp return NULL;
2406 528dedf3 2021-08-30 stsp switch (ch) {
2408 1e37a5c2 2019-05-12 jcs s->quit = 1;
2411 1e37a5c2 2019-05-12 jcs case KEY_UP:
2414 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2416 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2417 1e37a5c2 2019-05-12 jcs s->selected--;
2419 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2420 912a3f79 2021-08-30 j select_commit(s);
2423 912a3f79 2021-08-30 j case KEY_HOME:
2424 912a3f79 2021-08-30 j s->selected = 0;
2425 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2426 2b779855 2020-12-05 naddy select_commit(s);
2428 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2429 a4292ac5 2019-05-12 jcs case CTRL('b'):
2430 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2432 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2433 1e37a5c2 2019-05-12 jcs s->selected = 0;
2435 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2436 2b779855 2020-12-05 naddy select_commit(s);
2439 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2442 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2444 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2445 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2446 1e37a5c2 2019-05-12 jcs s->selected++;
2448 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2452 912a3f79 2021-08-30 j select_commit(s);
2455 912a3f79 2021-08-30 j case KEY_END: {
2456 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2457 912a3f79 2021-08-30 j * traverse them all. */
2458 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2459 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2460 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2463 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2464 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2465 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2466 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2468 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2469 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2471 f3bc9f1d 2021-09-05 naddy if (n > 0)
2472 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2473 2b779855 2020-12-05 naddy select_commit(s);
2476 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2477 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2478 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2479 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2480 1e37a5c2 2019-05-12 jcs if (first == NULL)
2482 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2485 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2486 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2487 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2488 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2489 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2490 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2492 2b779855 2020-12-05 naddy select_commit(s);
2495 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2496 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2497 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2498 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2499 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2500 2b779855 2020-12-05 naddy select_commit(s);
2501 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2502 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2503 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2504 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2505 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2508 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2511 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2513 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2514 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2515 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2516 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2517 78756c87 2020-11-24 stsp view, s->repo);
2520 e78dc838 2020-12-04 stsp view->focussed = 0;
2521 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2522 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2523 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2525 1e37a5c2 2019-05-12 jcs return err;
2526 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2527 e78dc838 2020-12-04 stsp view->focus_child = 1;
2529 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2532 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2534 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2535 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2536 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2537 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2541 e78dc838 2020-12-04 stsp view->focussed = 0;
2542 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2543 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2544 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2546 1e37a5c2 2019-05-12 jcs return err;
2547 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2548 e78dc838 2020-12-04 stsp view->focus_child = 1;
2550 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2552 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2553 21355643 2020-12-06 stsp case CTRL('l'):
2555 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2556 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2558 21355643 2020-12-06 stsp err = stop_log_thread(s);
2560 74cfe85e 2020-10-20 stsp return err;
2561 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2562 21355643 2020-12-06 stsp char *parent_path;
2563 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2565 21355643 2020-12-06 stsp return err;
2566 21355643 2020-12-06 stsp free(s->in_repo_path);
2567 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2568 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2569 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2570 21355643 2020-12-06 stsp struct got_object_id *start_id;
2571 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2572 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2573 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2575 21355643 2020-12-06 stsp return err;
2576 21355643 2020-12-06 stsp free(s->start_id);
2577 21355643 2020-12-06 stsp s->start_id = start_id;
2578 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2579 21355643 2020-12-06 stsp } else /* 'B' */
2580 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2582 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2583 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2585 21355643 2020-12-06 stsp return err;
2586 51a10b52 2020-12-26 stsp tog_free_refs();
2587 51a10b52 2020-12-26 stsp err = tog_load_refs(s->repo);
2589 ca51c541 2020-12-07 stsp return err;
2590 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2591 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2593 d01904d4 2019-06-25 stsp return err;
2594 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2595 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2597 b672a97a 2020-01-27 stsp return err;
2598 21355643 2020-12-06 stsp free_commits(&s->commits);
2599 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2600 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2601 21355643 2020-12-06 stsp s->selected_entry = NULL;
2602 21355643 2020-12-06 stsp s->selected = 0;
2603 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2604 21355643 2020-12-06 stsp s->quit = 0;
2605 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2608 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2609 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2610 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2611 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2612 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2613 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2614 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2615 6458efa5 2020-11-24 stsp if (err) {
2616 6458efa5 2020-11-24 stsp view_close(ref_view);
2617 6458efa5 2020-11-24 stsp return err;
2619 e78dc838 2020-12-04 stsp view->focussed = 0;
2620 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2621 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2622 6458efa5 2020-11-24 stsp err = view_close_child(view);
2624 6458efa5 2020-11-24 stsp return err;
2625 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2626 e78dc838 2020-12-04 stsp view->focus_child = 1;
2628 6458efa5 2020-11-24 stsp *new_view = ref_view;
2634 80ddbec8 2018-04-29 stsp return err;
2637 4ed7e80c 2018-05-20 stsp static const struct got_error *
2638 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2640 c2db6724 2019-01-04 stsp const struct got_error *error;
2642 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2643 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2644 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2646 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2647 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2649 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2650 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2652 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2653 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2655 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2656 c2db6724 2019-01-04 stsp if (error != NULL)
2657 c2db6724 2019-01-04 stsp return error;
2659 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2660 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2662 c2db6724 2019-01-04 stsp return NULL;
2665 a915003a 2019-02-05 stsp static void
2666 a915003a 2019-02-05 stsp init_curses(void)
2668 a915003a 2019-02-05 stsp initscr();
2670 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2673 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2674 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2675 a915003a 2019-02-05 stsp curs_set(0);
2676 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2677 6d17833f 2019-11-08 stsp start_color();
2678 6d17833f 2019-11-08 stsp use_default_colors();
2680 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2681 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2682 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2685 c2db6724 2019-01-04 stsp static const struct got_error *
2686 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2687 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2689 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2691 f135c941 2020-02-20 stsp if (argc == 0) {
2692 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2693 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2694 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2695 f135c941 2020-02-20 stsp return NULL;
2698 f135c941 2020-02-20 stsp if (worktree) {
2699 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2702 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2704 f135c941 2020-02-20 stsp return err;
2705 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2706 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2707 bfd61697 2020-11-14 stsp p) == -1) {
2708 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2709 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2713 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2715 f135c941 2020-02-20 stsp return err;
2718 f135c941 2020-02-20 stsp static const struct got_error *
2719 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2721 80ddbec8 2018-04-29 stsp const struct got_error *error;
2722 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2723 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2724 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2725 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2726 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2727 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2728 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2729 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2730 04cc582a 2018-08-01 stsp struct tog_view *view;
2732 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2733 80ddbec8 2018-04-29 stsp switch (ch) {
2735 b672a97a 2020-01-27 stsp log_branches = 1;
2738 80ddbec8 2018-04-29 stsp start_commit = optarg;
2741 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2742 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2743 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2747 17020d27 2019-03-07 stsp usage_log();
2748 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2752 80ddbec8 2018-04-29 stsp argc -= optind;
2753 80ddbec8 2018-04-29 stsp argv += optind;
2755 f135c941 2020-02-20 stsp if (argc > 1)
2756 f135c941 2020-02-20 stsp usage_log();
2758 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2759 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2760 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2761 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2762 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2763 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2764 c156c7a4 2020-12-18 stsp goto done;
2765 a1fbf39a 2019-08-11 stsp if (worktree)
2766 6962eb72 2020-02-20 stsp repo_path =
2767 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2769 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2770 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2771 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2772 c156c7a4 2020-12-18 stsp goto done;
2776 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2777 80ddbec8 2018-04-29 stsp if (error != NULL)
2778 ecb28ae0 2018-07-16 stsp goto done;
2780 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2781 f135c941 2020-02-20 stsp repo, worktree);
2782 f135c941 2020-02-20 stsp if (error)
2783 f135c941 2020-02-20 stsp goto done;
2785 f135c941 2020-02-20 stsp init_curses();
2787 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2788 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2789 c02c541e 2019-03-29 stsp if (error)
2790 c02c541e 2019-03-29 stsp goto done;
2792 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2793 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2794 87670572 2020-12-26 naddy error = tog_load_refs(repo);
2795 87670572 2020-12-26 naddy if (error)
2796 87670572 2020-12-26 naddy goto done;
2799 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2800 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2801 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2802 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2803 d8f38dc4 2020-12-05 stsp if (error)
2804 d8f38dc4 2020-12-05 stsp goto done;
2805 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2807 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2808 d8f38dc4 2020-12-05 stsp if (error == NULL)
2809 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2810 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2811 d8f38dc4 2020-12-05 stsp goto done;
2812 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2813 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2814 d8f38dc4 2020-12-05 stsp if (error)
2815 d8f38dc4 2020-12-05 stsp goto done;
2818 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2819 04cc582a 2018-08-01 stsp if (view == NULL) {
2820 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2821 04cc582a 2018-08-01 stsp goto done;
2823 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2824 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2825 ba4f502b 2018-08-04 stsp if (error)
2826 ba4f502b 2018-08-04 stsp goto done;
2827 2fc00ff4 2019-08-31 stsp if (worktree) {
2828 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2829 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2830 2fc00ff4 2019-08-31 stsp worktree = NULL;
2832 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2834 f135c941 2020-02-20 stsp free(in_repo_path);
2835 ecb28ae0 2018-07-16 stsp free(repo_path);
2836 ecb28ae0 2018-07-16 stsp free(cwd);
2837 899d86c2 2018-05-10 stsp free(start_id);
2838 d8f38dc4 2020-12-05 stsp free(label);
2840 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2841 1d0f4054 2021-06-17 stsp if (repo) {
2842 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2843 1d0f4054 2021-06-17 stsp if (error == NULL)
2844 1d0f4054 2021-06-17 stsp error = close_err;
2846 ec142235 2019-03-07 stsp if (worktree)
2847 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2848 51a10b52 2020-12-26 stsp tog_free_refs();
2849 80ddbec8 2018-04-29 stsp return error;
2852 4ed7e80c 2018-05-20 stsp __dead static void
2853 9f7d7167 2018-04-29 stsp usage_diff(void)
2856 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2857 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2861 6d17833f 2019-11-08 stsp static int
2862 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2863 41605754 2020-11-12 stsp regmatch_t *regmatch)
2865 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2868 f26dddb7 2019-11-08 stsp struct tog_color *
2869 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2871 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2873 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2874 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2875 6d17833f 2019-11-08 stsp return tc;
2878 6d17833f 2019-11-08 stsp return NULL;
2881 4ed7e80c 2018-05-20 stsp static const struct got_error *
2882 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2883 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2885 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2886 41605754 2020-11-12 stsp wchar_t *wline;
2887 41605754 2020-11-12 stsp int width;
2890 41605754 2020-11-12 stsp *wtotal = 0;
2892 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2893 41605754 2020-11-12 stsp if (s == NULL)
2894 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2896 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2897 41605754 2020-11-12 stsp if (err) {
2899 41605754 2020-11-12 stsp return err;
2901 41605754 2020-11-12 stsp waddwstr(window, wline);
2902 41605754 2020-11-12 stsp free(wline);
2904 41605754 2020-11-12 stsp wlimit -= width;
2905 41605754 2020-11-12 stsp *wtotal += width;
2907 41605754 2020-11-12 stsp if (wlimit > 0) {
2908 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2909 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2910 41605754 2020-11-12 stsp if (s == NULL) {
2911 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2913 41605754 2020-11-12 stsp return err;
2915 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2916 41605754 2020-11-12 stsp if (err) {
2918 41605754 2020-11-12 stsp return err;
2920 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2921 41605754 2020-11-12 stsp waddwstr(window, wline);
2922 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2923 41605754 2020-11-12 stsp free(wline);
2925 41605754 2020-11-12 stsp wlimit -= width;
2926 41605754 2020-11-12 stsp *wtotal += width;
2929 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2930 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2931 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2933 41605754 2020-11-12 stsp return err;
2934 41605754 2020-11-12 stsp waddwstr(window, wline);
2935 41605754 2020-11-12 stsp free(wline);
2936 41605754 2020-11-12 stsp *wtotal += width;
2939 41605754 2020-11-12 stsp return NULL;
2942 41605754 2020-11-12 stsp static const struct got_error *
2943 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2945 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2946 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2947 61e69b96 2018-05-20 stsp const struct got_error *err;
2948 fe621944 2020-11-10 stsp int nprinted = 0;
2949 b304db33 2018-05-20 stsp char *line;
2950 826082fe 2020-12-10 stsp size_t linesize = 0;
2951 826082fe 2020-12-10 stsp ssize_t linelen;
2952 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2953 61e69b96 2018-05-20 stsp wchar_t *wline;
2954 e0b650dd 2018-05-20 stsp int width;
2955 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2956 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2957 fe621944 2020-11-10 stsp off_t line_offset;
2959 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2960 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2961 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2963 f7d12f7e 2018-08-01 stsp werase(view->window);
2965 a3404814 2018-09-02 stsp if (header) {
2966 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2967 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2968 135a2da0 2020-11-11 stsp header) == -1)
2969 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2970 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2971 135a2da0 2020-11-11 stsp free(line);
2973 a3404814 2018-09-02 stsp return err;
2975 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2976 a3404814 2018-09-02 stsp wstandout(view->window);
2977 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2978 e54cc94a 2020-11-11 stsp free(wline);
2979 e54cc94a 2020-11-11 stsp wline = NULL;
2980 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2981 a3404814 2018-09-02 stsp wstandend(view->window);
2982 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2983 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2985 a3404814 2018-09-02 stsp if (max_lines <= 1)
2986 a3404814 2018-09-02 stsp return NULL;
2987 a3404814 2018-09-02 stsp max_lines--;
2990 89f1a395 2020-12-01 naddy s->eof = 0;
2991 826082fe 2020-12-10 stsp line = NULL;
2992 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2993 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
2994 826082fe 2020-12-10 stsp if (linelen == -1) {
2995 826082fe 2020-12-10 stsp if (feof(s->f)) {
2996 826082fe 2020-12-10 stsp s->eof = 1;
2999 826082fe 2020-12-10 stsp free(line);
3000 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3003 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3005 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3006 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3007 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3008 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3009 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3010 41605754 2020-11-12 stsp view->window, regmatch);
3011 41605754 2020-11-12 stsp if (err) {
3012 41605754 2020-11-12 stsp free(line);
3013 41605754 2020-11-12 stsp return err;
3016 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3017 41605754 2020-11-12 stsp if (err) {
3018 41605754 2020-11-12 stsp free(line);
3019 41605754 2020-11-12 stsp return err;
3021 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3022 41605754 2020-11-12 stsp free(wline);
3023 41605754 2020-11-12 stsp wline = NULL;
3026 6d17833f 2019-11-08 stsp wattr_off(view->window,
3027 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3028 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3029 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3030 fe621944 2020-11-10 stsp nprinted++;
3032 826082fe 2020-12-10 stsp free(line);
3033 fe621944 2020-11-10 stsp if (nprinted >= 1)
3034 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3035 89f1a395 2020-12-01 naddy (nprinted - 1);
3037 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3039 1a57306a 2018-09-02 stsp view_vborder(view);
3041 89f1a395 2020-12-01 naddy if (s->eof) {
3042 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3043 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3044 c3e9aa98 2019-05-13 jcs nprinted++;
3047 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3049 c3e9aa98 2019-05-13 jcs return err;
3052 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3053 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3054 e54cc94a 2020-11-11 stsp free(wline);
3055 e54cc94a 2020-11-11 stsp wline = NULL;
3056 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3059 26ed57b2 2018-05-19 stsp return NULL;
3062 abd2672a 2018-12-23 stsp static char *
3063 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3065 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3066 09867e48 2019-08-13 stsp char *p, *s;
3068 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3069 09867e48 2019-08-13 stsp if (tm == NULL)
3070 09867e48 2019-08-13 stsp return NULL;
3071 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3072 09867e48 2019-08-13 stsp if (s == NULL)
3073 09867e48 2019-08-13 stsp return NULL;
3074 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3076 abd2672a 2018-12-23 stsp *p = '\0';
3080 4ed7e80c 2018-05-20 stsp static const struct got_error *
3081 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3082 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3084 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3085 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3086 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3087 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3089 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3090 0208f208 2020-05-05 stsp if (qid != NULL) {
3091 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3092 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3095 0208f208 2020-05-05 stsp return err;
3097 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3098 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3099 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3100 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3101 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3103 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3107 0208f208 2020-05-05 stsp if (tree_id1) {
3108 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3110 0208f208 2020-05-05 stsp goto done;
3113 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3114 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3116 0208f208 2020-05-05 stsp goto done;
3118 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3119 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3121 0208f208 2020-05-05 stsp if (tree1)
3122 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3123 0208f208 2020-05-05 stsp if (tree2)
3124 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3125 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3126 0208f208 2020-05-05 stsp return err;
3129 0208f208 2020-05-05 stsp static const struct got_error *
3130 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3134 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3135 fe621944 2020-11-10 stsp if (p == NULL)
3136 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3137 fe621944 2020-11-10 stsp *line_offsets = p;
3138 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3139 fe621944 2020-11-10 stsp (*nlines)++;
3140 fe621944 2020-11-10 stsp return NULL;
3143 fe621944 2020-11-10 stsp static const struct got_error *
3144 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3145 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3146 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3148 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3149 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3150 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3151 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3152 45d799e2 2018-12-23 stsp time_t committer_time;
3153 45d799e2 2018-12-23 stsp const char *author, *committer;
3154 8b473291 2019-02-21 stsp char *refs_str = NULL;
3155 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3156 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3157 fe621944 2020-11-10 stsp off_t outoff = 0;
3160 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3162 8b473291 2019-02-21 stsp if (refs) {
3163 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3165 8b473291 2019-02-21 stsp return err;
3168 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3170 abd2672a 2018-12-23 stsp return err;
3172 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3173 15a94983 2018-12-23 stsp if (err) {
3174 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3175 15a94983 2018-12-23 stsp goto done;
3178 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3180 fe621944 2020-11-10 stsp goto done;
3182 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3183 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3184 fe621944 2020-11-10 stsp if (n < 0) {
3185 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3186 abd2672a 2018-12-23 stsp goto done;
3188 fe621944 2020-11-10 stsp outoff += n;
3189 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3191 fe621944 2020-11-10 stsp goto done;
3193 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3194 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3195 fe621944 2020-11-10 stsp if (n < 0) {
3196 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3197 abd2672a 2018-12-23 stsp goto done;
3199 fe621944 2020-11-10 stsp outoff += n;
3200 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3202 fe621944 2020-11-10 stsp goto done;
3204 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3205 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3206 fe621944 2020-11-10 stsp if (datestr) {
3207 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3208 fe621944 2020-11-10 stsp if (n < 0) {
3209 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3210 fe621944 2020-11-10 stsp goto done;
3212 fe621944 2020-11-10 stsp outoff += n;
3213 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3215 fe621944 2020-11-10 stsp goto done;
3217 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3218 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3219 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3220 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3221 fe621944 2020-11-10 stsp if (n < 0) {
3222 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3223 fe621944 2020-11-10 stsp goto done;
3225 fe621944 2020-11-10 stsp outoff += n;
3226 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3228 fe621944 2020-11-10 stsp goto done;
3230 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3231 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3232 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3233 9f98ca05 2021-09-24 stsp int pn = 1;
3234 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3235 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3236 9f98ca05 2021-09-24 stsp err = got_object_id_str(&id_str, qid->id);
3238 9f98ca05 2021-09-24 stsp goto done;
3239 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3240 9f98ca05 2021-09-24 stsp if (n < 0) {
3241 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3242 9f98ca05 2021-09-24 stsp goto done;
3244 9f98ca05 2021-09-24 stsp outoff += n;
3245 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3247 9f98ca05 2021-09-24 stsp goto done;
3248 9f98ca05 2021-09-24 stsp free(id_str);
3249 9f98ca05 2021-09-24 stsp id_str = NULL;
3253 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3255 5943eee2 2019-08-13 stsp goto done;
3256 fe621944 2020-11-10 stsp s = logmsg;
3257 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3258 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3259 fe621944 2020-11-10 stsp if (n < 0) {
3260 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3261 fe621944 2020-11-10 stsp goto done;
3263 fe621944 2020-11-10 stsp outoff += n;
3264 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3266 fe621944 2020-11-10 stsp goto done;
3269 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3271 0208f208 2020-05-05 stsp goto done;
3272 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3273 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3274 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3275 fe621944 2020-11-10 stsp if (n < 0) {
3276 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3277 fe621944 2020-11-10 stsp goto done;
3279 fe621944 2020-11-10 stsp outoff += n;
3280 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3282 fe621944 2020-11-10 stsp goto done;
3283 0208f208 2020-05-05 stsp free((char *)pe->path);
3284 0208f208 2020-05-05 stsp free(pe->data);
3287 0208f208 2020-05-05 stsp fputc('\n', outfile);
3289 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3291 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3292 abd2672a 2018-12-23 stsp free(id_str);
3293 5943eee2 2019-08-13 stsp free(logmsg);
3294 8b473291 2019-02-21 stsp free(refs_str);
3295 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3296 7510f233 2020-08-09 stsp if (err) {
3297 ae6a6978 2020-08-09 stsp free(*line_offsets);
3298 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3299 ae6a6978 2020-08-09 stsp *nlines = 0;
3301 fe621944 2020-11-10 stsp return err;
3304 abd2672a 2018-12-23 stsp static const struct got_error *
3305 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3307 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3308 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3309 15a94983 2018-12-23 stsp int obj_type;
3311 fe621944 2020-11-10 stsp free(s->line_offsets);
3312 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3313 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3314 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3315 fe621944 2020-11-10 stsp s->nlines = 0;
3317 511a516b 2018-05-19 stsp f = got_opentemp();
3318 48ae06ee 2018-10-18 stsp if (f == NULL) {
3319 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3320 48ae06ee 2018-10-18 stsp goto done;
3322 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3323 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3324 fb43ecf1 2019-02-11 stsp goto done;
3328 15a94983 2018-12-23 stsp if (s->id1)
3329 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3331 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3333 15a94983 2018-12-23 stsp goto done;
3335 15a94983 2018-12-23 stsp switch (obj_type) {
3336 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3337 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3338 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3339 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3341 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3342 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3343 67b631c9 2021-10-10 stsp s->id1, s->id2, NULL, "", "", s->diff_context,
3344 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3346 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3347 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3348 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3349 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3350 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3352 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3354 3ffacbe1 2020-02-02 tracey goto done;
3355 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3356 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3357 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3358 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3359 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3360 f44b1f58 2020-02-02 tracey if (err)
3361 f44b1f58 2020-02-02 tracey goto done;
3362 f44b1f58 2020-02-02 tracey } else {
3363 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3364 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3365 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3366 fe621944 2020-11-10 stsp err = write_commit_info(
3367 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3368 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3369 f44b1f58 2020-02-02 tracey if (err)
3370 f44b1f58 2020-02-02 tracey goto done;
3375 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3377 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3378 67b631c9 2021-10-10 stsp s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3379 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3383 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3386 f44b1f58 2020-02-02 tracey if (err)
3387 f44b1f58 2020-02-02 tracey goto done;
3389 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3390 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3391 48ae06ee 2018-10-18 stsp return err;
3394 f5215bb9 2019-02-22 stsp static void
3395 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3397 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3398 f5215bb9 2019-02-22 stsp update_panels();
3399 f5215bb9 2019-02-22 stsp doupdate();
3402 f44b1f58 2020-02-02 tracey static const struct got_error *
3403 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3405 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3407 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3408 f44b1f58 2020-02-02 tracey return NULL;
3411 f44b1f58 2020-02-02 tracey static const struct got_error *
3412 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3414 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3415 f44b1f58 2020-02-02 tracey int lineno;
3416 826082fe 2020-12-10 stsp char *line = NULL;
3417 826082fe 2020-12-10 stsp size_t linesize = 0;
3418 826082fe 2020-12-10 stsp ssize_t linelen;
3420 f44b1f58 2020-02-02 tracey if (!view->searching) {
3421 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3422 f44b1f58 2020-02-02 tracey return NULL;
3425 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3426 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3427 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3429 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3430 f44b1f58 2020-02-02 tracey } else {
3431 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3432 f44b1f58 2020-02-02 tracey lineno = 1;
3434 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3437 f44b1f58 2020-02-02 tracey while (1) {
3438 f44b1f58 2020-02-02 tracey off_t offset;
3440 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3441 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3442 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3446 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3447 f44b1f58 2020-02-02 tracey lineno = 1;
3449 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3452 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3453 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3454 f44b1f58 2020-02-02 tracey free(line);
3455 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3457 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3458 826082fe 2020-12-10 stsp if (linelen != -1 &&
3459 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3460 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3461 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3464 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3465 f44b1f58 2020-02-02 tracey lineno++;
3467 f44b1f58 2020-02-02 tracey lineno--;
3469 826082fe 2020-12-10 stsp free(line);
3471 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3472 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3473 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3476 f44b1f58 2020-02-02 tracey return NULL;
3479 48ae06ee 2018-10-18 stsp static const struct got_error *
3480 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3481 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3482 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3483 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3485 48ae06ee 2018-10-18 stsp const struct got_error *err;
3486 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3488 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3489 15a94983 2018-12-23 stsp int type1, type2;
3490 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3492 15a94983 2018-12-23 stsp return err;
3493 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3495 15a94983 2018-12-23 stsp return err;
3497 15a94983 2018-12-23 stsp if (type1 != type2)
3498 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3500 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3501 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3502 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3503 f44b1f58 2020-02-02 tracey s->repo = repo;
3504 f44b1f58 2020-02-02 tracey s->id1 = id1;
3505 f44b1f58 2020-02-02 tracey s->id2 = id2;
3506 3dbaef42 2020-11-24 stsp s->label1 = label1;
3507 3dbaef42 2020-11-24 stsp s->label2 = label2;
3509 15a94983 2018-12-23 stsp if (id1) {
3510 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3511 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3512 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3514 5465d566 2020-02-01 tracey s->id1 = NULL;
3516 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3517 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3518 5465d566 2020-02-01 tracey free(s->id1);
3519 5465d566 2020-02-01 tracey s->id1 = NULL;
3520 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3522 5465d566 2020-02-01 tracey s->f = NULL;
3523 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3524 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3525 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3526 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3527 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3528 5465d566 2020-02-01 tracey s->log_view = log_view;
3529 5465d566 2020-02-01 tracey s->repo = repo;
3531 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3532 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3533 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3534 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3535 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3537 6d17833f 2019-11-08 stsp return err;
3538 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3539 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3540 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3541 6d17833f 2019-11-08 stsp if (err) {
3542 5465d566 2020-02-01 tracey free_colors(&s->colors);
3543 6d17833f 2019-11-08 stsp return err;
3545 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3546 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3547 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3548 6d17833f 2019-11-08 stsp if (err) {
3549 5465d566 2020-02-01 tracey free_colors(&s->colors);
3550 6d17833f 2019-11-08 stsp return err;
3553 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3554 9f98ca05 2021-09-24 stsp "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3555 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3556 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3557 6d17833f 2019-11-08 stsp if (err) {
3558 5465d566 2020-02-01 tracey free_colors(&s->colors);
3559 6d17833f 2019-11-08 stsp return err;
3562 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3563 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3564 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3565 11b20872 2019-11-08 stsp if (err) {
3566 5465d566 2020-02-01 tracey free_colors(&s->colors);
3567 11b20872 2019-11-08 stsp return err;
3570 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3571 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3572 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3573 11b20872 2019-11-08 stsp if (err) {
3574 5465d566 2020-02-01 tracey free_colors(&s->colors);
3575 11b20872 2019-11-08 stsp return err;
3579 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3580 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3581 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3583 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3584 fe621944 2020-11-10 stsp s->nlines = 0;
3585 5465d566 2020-02-01 tracey err = create_diff(s);
3586 48ae06ee 2018-10-18 stsp if (err) {
3587 5465d566 2020-02-01 tracey free(s->id1);
3588 5465d566 2020-02-01 tracey s->id1 = NULL;
3589 5465d566 2020-02-01 tracey free(s->id2);
3590 5465d566 2020-02-01 tracey s->id2 = NULL;
3591 78756c87 2020-11-24 stsp free_colors(&s->colors);
3592 48ae06ee 2018-10-18 stsp return err;
3595 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3596 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3597 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3598 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3599 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3601 5dc9f4bc 2018-08-04 stsp return NULL;
3604 e5a0f69f 2018-08-18 stsp static const struct got_error *
3605 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3607 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3608 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3610 5465d566 2020-02-01 tracey free(s->id1);
3611 5465d566 2020-02-01 tracey s->id1 = NULL;
3612 5465d566 2020-02-01 tracey free(s->id2);
3613 5465d566 2020-02-01 tracey s->id2 = NULL;
3614 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3615 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3616 5465d566 2020-02-01 tracey free_colors(&s->colors);
3617 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3618 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3619 fe621944 2020-11-10 stsp s->nlines = 0;
3620 e5a0f69f 2018-08-18 stsp return err;
3623 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3624 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3626 a3404814 2018-09-02 stsp const struct got_error *err;
3627 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3628 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3629 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3631 a3404814 2018-09-02 stsp if (s->id1) {
3632 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3634 a3404814 2018-09-02 stsp return err;
3635 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3637 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3639 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3641 a3404814 2018-09-02 stsp return err;
3642 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3644 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3645 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3646 a3404814 2018-09-02 stsp free(id_str1);
3647 a3404814 2018-09-02 stsp free(id_str2);
3648 a3404814 2018-09-02 stsp return err;
3650 a3404814 2018-09-02 stsp free(id_str1);
3651 a3404814 2018-09-02 stsp free(id_str2);
3653 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3654 267bb3b8 2021-08-01 stsp free(header);
3655 267bb3b8 2021-08-01 stsp return err;
3658 15a087fe 2019-02-21 stsp static const struct got_error *
3659 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3660 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3662 d7a04538 2019-02-21 stsp const struct got_error *err;
3663 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3664 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3665 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3667 15a087fe 2019-02-21 stsp free(s->id2);
3668 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3669 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3670 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3672 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3674 d7a04538 2019-02-21 stsp return err;
3675 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3676 15a087fe 2019-02-21 stsp free(s->id1);
3677 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3678 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3679 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3680 15a087fe 2019-02-21 stsp return NULL;
3683 0cf4efb1 2018-09-29 stsp static const struct got_error *
3684 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3686 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3687 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3688 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3689 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3690 826082fe 2020-12-10 stsp char *line = NULL;
3691 826082fe 2020-12-10 stsp size_t linesize = 0;
3692 826082fe 2020-12-10 stsp ssize_t linelen;
3695 e5a0f69f 2018-08-18 stsp switch (ch) {
3698 3dbaef42 2020-11-24 stsp if (ch == 'a')
3699 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3700 3dbaef42 2020-11-24 stsp if (ch == 'w')
3701 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3702 64453f7e 2020-11-21 stsp wclear(view->window);
3703 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3704 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3705 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3706 64453f7e 2020-11-21 stsp err = create_diff(s);
3709 912a3f79 2021-08-30 j case KEY_HOME:
3710 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3713 912a3f79 2021-08-30 j case KEY_END:
3717 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3721 1e37a5c2 2019-05-12 jcs case KEY_UP:
3722 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3723 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3725 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3726 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3727 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3730 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3731 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3732 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3735 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3736 1e37a5c2 2019-05-12 jcs if (!s->eof)
3737 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3739 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3740 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3742 00ba99a7 2019-05-12 jcs if (s->eof)
3745 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3746 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3747 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3748 826082fe 2020-12-10 stsp if (linelen == -1) {
3749 826082fe 2020-12-10 stsp if (feof(s->f)) {
3750 826082fe 2020-12-10 stsp s->eof = 1;
3752 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3756 826082fe 2020-12-10 stsp free(line);
3759 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3760 1e37a5c2 2019-05-12 jcs s->diff_context--;
3761 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3762 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3763 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3764 27829c9e 2020-11-21 stsp s->nlines) {
3765 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3766 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3771 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3772 1e37a5c2 2019-05-12 jcs s->diff_context++;
3773 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3774 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3779 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3781 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3782 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3784 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3788 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3791 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3795 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3796 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3798 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3799 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3803 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3805 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3806 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3808 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3812 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3815 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3819 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3820 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3822 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3823 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3829 bcbd79e2 2018-08-19 stsp return err;
3832 4ed7e80c 2018-05-20 stsp static const struct got_error *
3833 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3835 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3836 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3837 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3838 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3839 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3840 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3841 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3842 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3843 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3844 3dbaef42 2020-11-24 stsp const char *errstr;
3845 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3847 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3848 26ed57b2 2018-05-19 stsp switch (ch) {
3850 64453f7e 2020-11-21 stsp force_text_diff = 1;
3853 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3855 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3856 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3858 09b5bff8 2020-02-23 naddy case 'r':
3859 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3860 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3861 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3863 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3866 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3869 17020d27 2019-03-07 stsp usage_diff();
3870 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3874 26ed57b2 2018-05-19 stsp argc -= optind;
3875 26ed57b2 2018-05-19 stsp argv += optind;
3877 26ed57b2 2018-05-19 stsp if (argc == 0) {
3878 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3879 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3880 15a94983 2018-12-23 stsp id_str1 = argv[0];
3881 15a94983 2018-12-23 stsp id_str2 = argv[1];
3883 26ed57b2 2018-05-19 stsp usage_diff();
3885 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3886 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3887 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3888 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3889 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3890 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3891 c156c7a4 2020-12-18 stsp goto done;
3892 a273ac94 2020-02-23 naddy if (worktree)
3893 a273ac94 2020-02-23 naddy repo_path =
3894 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3896 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3897 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3898 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3899 c156c7a4 2020-12-18 stsp goto done;
3903 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3904 eb6600df 2019-01-04 stsp if (error)
3905 eb6600df 2019-01-04 stsp goto done;
3907 a273ac94 2020-02-23 naddy init_curses();
3909 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3910 51a10b52 2020-12-26 stsp if (error)
3911 51a10b52 2020-12-26 stsp goto done;
3913 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
3914 26ed57b2 2018-05-19 stsp if (error)
3915 26ed57b2 2018-05-19 stsp goto done;
3917 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3918 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3919 26ed57b2 2018-05-19 stsp if (error)
3920 26ed57b2 2018-05-19 stsp goto done;
3922 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3923 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3924 26ed57b2 2018-05-19 stsp if (error)
3925 26ed57b2 2018-05-19 stsp goto done;
3927 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3928 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3929 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3930 ea5e7bb5 2018-08-01 stsp goto done;
3932 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3933 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3934 5dc9f4bc 2018-08-04 stsp if (error)
3935 5dc9f4bc 2018-08-04 stsp goto done;
3936 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3938 3dbaef42 2020-11-24 stsp free(label1);
3939 3dbaef42 2020-11-24 stsp free(label2);
3940 c02c541e 2019-03-29 stsp free(repo_path);
3941 a273ac94 2020-02-23 naddy free(cwd);
3942 1d0f4054 2021-06-17 stsp if (repo) {
3943 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3944 1d0f4054 2021-06-17 stsp if (error == NULL)
3945 1d0f4054 2021-06-17 stsp error = close_err;
3947 a273ac94 2020-02-23 naddy if (worktree)
3948 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3949 51a10b52 2020-12-26 stsp tog_free_refs();
3950 26ed57b2 2018-05-19 stsp return error;
3953 4ed7e80c 2018-05-20 stsp __dead static void
3954 9f7d7167 2018-04-29 stsp usage_blame(void)
3957 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3958 9f7d7167 2018-04-29 stsp getprogname());
3962 84451b3e 2018-07-10 stsp struct tog_blame_line {
3963 84451b3e 2018-07-10 stsp int annotated;
3964 84451b3e 2018-07-10 stsp struct got_object_id *id;
3967 4ed7e80c 2018-05-20 stsp static const struct got_error *
3968 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3970 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3971 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3972 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3973 84451b3e 2018-07-10 stsp const struct got_error *err;
3974 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3975 826082fe 2020-12-10 stsp char *line = NULL;
3976 826082fe 2020-12-10 stsp size_t linesize = 0;
3977 826082fe 2020-12-10 stsp ssize_t linelen;
3978 84451b3e 2018-07-10 stsp wchar_t *wline;
3979 27a741e5 2019-09-11 stsp int width;
3980 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3981 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3982 ab089a2a 2018-07-12 stsp char *id_str;
3983 11b20872 2019-11-08 stsp struct tog_color *tc;
3985 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3987 ab089a2a 2018-07-12 stsp return err;
3989 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
3990 f7d12f7e 2018-08-01 stsp werase(view->window);
3992 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3993 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3994 ab089a2a 2018-07-12 stsp free(id_str);
3995 ab089a2a 2018-07-12 stsp return err;
3998 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3999 ab089a2a 2018-07-12 stsp free(line);
4000 2550e4c3 2018-07-13 stsp line = NULL;
4002 1cae65b4 2019-09-22 stsp return err;
4003 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4004 a3404814 2018-09-02 stsp wstandout(view->window);
4005 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4007 11b20872 2019-11-08 stsp wattr_on(view->window,
4008 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4009 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4011 11b20872 2019-11-08 stsp wattr_off(view->window,
4012 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4013 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4014 a3404814 2018-09-02 stsp wstandend(view->window);
4015 2550e4c3 2018-07-13 stsp free(wline);
4016 2550e4c3 2018-07-13 stsp wline = NULL;
4017 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4018 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4020 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4021 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4022 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4023 ab089a2a 2018-07-12 stsp free(id_str);
4024 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4026 ab089a2a 2018-07-12 stsp free(id_str);
4027 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4028 3f60a8ef 2018-07-10 stsp free(line);
4029 2550e4c3 2018-07-13 stsp line = NULL;
4031 3f60a8ef 2018-07-10 stsp return err;
4032 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4033 2550e4c3 2018-07-13 stsp free(wline);
4034 2550e4c3 2018-07-13 stsp wline = NULL;
4035 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4036 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4038 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4039 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4040 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4041 826082fe 2020-12-10 stsp if (linelen == -1) {
4042 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4043 826082fe 2020-12-10 stsp s->eof = 1;
4046 84451b3e 2018-07-10 stsp free(line);
4047 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4049 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4052 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4053 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4055 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4056 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4057 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4058 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4059 27a741e5 2019-09-11 stsp !(view->focussed &&
4060 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4061 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4062 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4063 8d0fe45a 2019-08-12 stsp char *id_str;
4064 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4065 8d0fe45a 2019-08-12 stsp if (err) {
4066 8d0fe45a 2019-08-12 stsp free(line);
4067 8d0fe45a 2019-08-12 stsp return err;
4069 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4071 11b20872 2019-11-08 stsp wattr_on(view->window,
4072 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4073 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4075 11b20872 2019-11-08 stsp wattr_off(view->window,
4076 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4077 8d0fe45a 2019-08-12 stsp free(id_str);
4078 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4080 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4081 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4084 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4085 ee41ec32 2018-07-10 stsp prev_id = NULL;
4088 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4089 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4090 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4092 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4093 41605754 2020-11-12 stsp width = 9;
4094 41605754 2020-11-12 stsp wline = wcsdup(L"");
4095 41605754 2020-11-12 stsp if (wline == NULL) {
4096 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4097 41605754 2020-11-12 stsp free(line);
4098 41605754 2020-11-12 stsp return err;
4100 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4101 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4102 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4103 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4104 41605754 2020-11-12 stsp view->window, regmatch);
4105 41605754 2020-11-12 stsp if (err) {
4106 41605754 2020-11-12 stsp free(line);
4107 41605754 2020-11-12 stsp return err;
4109 41605754 2020-11-12 stsp width += 9;
4111 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4112 41605754 2020-11-12 stsp view->ncols - 9, 9);
4113 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4114 41605754 2020-11-12 stsp free(wline);
4115 41605754 2020-11-12 stsp wline = NULL;
4116 41605754 2020-11-12 stsp width += 9;
4119 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4120 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4121 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4122 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4124 826082fe 2020-12-10 stsp free(line);
4125 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4127 1a57306a 2018-09-02 stsp view_vborder(view);
4129 84451b3e 2018-07-10 stsp return NULL;
4132 84451b3e 2018-07-10 stsp static const struct got_error *
4133 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4135 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4136 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4137 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4138 1a76625f 2018-10-22 stsp int errcode;
4140 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4141 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4142 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4144 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4145 1a76625f 2018-10-22 stsp if (errcode)
4146 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4148 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4149 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4150 d68a0a7d 2018-07-10 stsp goto done;
4153 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4154 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4156 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4157 d68a0a7d 2018-07-10 stsp if (line->annotated)
4158 d68a0a7d 2018-07-10 stsp goto done;
4160 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4161 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4162 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4163 84451b3e 2018-07-10 stsp goto done;
4165 84451b3e 2018-07-10 stsp line->annotated = 1;
4167 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4168 1a76625f 2018-10-22 stsp if (errcode)
4169 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4170 84451b3e 2018-07-10 stsp return err;
4173 84451b3e 2018-07-10 stsp static void *
4174 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4176 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4177 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4178 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4179 1a76625f 2018-10-22 stsp int errcode;
4181 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4183 61266923 2020-01-14 stsp return (void *)err;
4185 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4186 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4187 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4188 fc06ba56 2019-08-22 stsp err = NULL;
4190 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4191 1a76625f 2018-10-22 stsp if (errcode)
4192 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4193 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4195 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4196 1d0f4054 2021-06-17 stsp if (err == NULL)
4197 1d0f4054 2021-06-17 stsp err = close_err;
4198 c9beca56 2018-07-22 stsp ta->repo = NULL;
4199 c9beca56 2018-07-22 stsp *ta->complete = 1;
4201 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4202 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4203 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4205 18430de3 2018-07-10 stsp return (void *)err;
4208 245d91c1 2018-07-12 stsp static struct got_object_id *
4209 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4210 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4212 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4214 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4215 8d0fe45a 2019-08-12 stsp return NULL;
4217 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4218 245d91c1 2018-07-12 stsp if (!line->annotated)
4219 245d91c1 2018-07-12 stsp return NULL;
4221 245d91c1 2018-07-12 stsp return line->id;
4224 b880a918 2018-07-10 stsp static const struct got_error *
4225 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4227 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4230 245d91c1 2018-07-12 stsp if (blame->thread) {
4231 1a76625f 2018-10-22 stsp int errcode;
4232 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4233 1a76625f 2018-10-22 stsp if (errcode)
4234 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4235 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4236 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4237 1a76625f 2018-10-22 stsp if (errcode)
4238 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4239 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4240 1a76625f 2018-10-22 stsp if (errcode)
4241 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4242 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4243 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4244 245d91c1 2018-07-12 stsp err = NULL;
4245 245d91c1 2018-07-12 stsp blame->thread = NULL;
4247 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4248 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4249 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4250 1d0f4054 2021-06-17 stsp if (err == NULL)
4251 1d0f4054 2021-06-17 stsp err = close_err;
4252 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4254 245d91c1 2018-07-12 stsp if (blame->f) {
4255 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4256 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4257 245d91c1 2018-07-12 stsp blame->f = NULL;
4259 57670559 2018-12-23 stsp if (blame->lines) {
4260 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4261 57670559 2018-12-23 stsp free(blame->lines[i].id);
4262 57670559 2018-12-23 stsp free(blame->lines);
4263 57670559 2018-12-23 stsp blame->lines = NULL;
4265 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4266 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4268 245d91c1 2018-07-12 stsp return err;
4271 245d91c1 2018-07-12 stsp static const struct got_error *
4272 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4274 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4275 fc06ba56 2019-08-22 stsp int *done = arg;
4276 fc06ba56 2019-08-22 stsp int errcode;
4278 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4279 fc06ba56 2019-08-22 stsp if (errcode)
4280 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4281 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4283 fc06ba56 2019-08-22 stsp if (*done)
4284 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4286 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4287 fc06ba56 2019-08-22 stsp if (errcode)
4288 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4289 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4291 fc06ba56 2019-08-22 stsp return err;
4294 fc06ba56 2019-08-22 stsp static const struct got_error *
4295 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4297 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4298 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4299 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4300 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4301 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4302 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4303 15a94983 2018-12-23 stsp int obj_type;
4305 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4306 a5388363 2020-12-01 naddy s->path);
4308 15a94983 2018-12-23 stsp return err;
4310 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4312 84451b3e 2018-07-10 stsp goto done;
4314 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4315 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4316 84451b3e 2018-07-10 stsp goto done;
4319 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4321 a70480e0 2018-06-23 stsp goto done;
4322 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4323 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4324 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4325 84451b3e 2018-07-10 stsp goto done;
4327 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4328 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4330 1fddf795 2021-01-20 stsp goto done;
4331 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4332 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4333 84451b3e 2018-07-10 stsp goto done;
4336 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4337 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4338 b02560ec 2019-08-19 stsp blame->nlines--;
4340 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4341 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4342 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4343 84451b3e 2018-07-10 stsp goto done;
4346 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4348 bd24772e 2018-07-11 stsp goto done;
4350 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4351 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4352 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4353 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4354 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4355 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4356 245d91c1 2018-07-12 stsp goto done;
4358 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4360 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4361 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4362 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4363 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4364 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4365 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4366 a5388363 2020-12-01 naddy s->blame_complete = 0;
4368 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4369 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4370 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4371 f5a09613 2020-12-13 naddy s->selected_line = 1;
4376 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4377 27d434c2 2018-09-15 stsp free(obj_id);
4379 245d91c1 2018-07-12 stsp stop_blame(blame);
4380 245d91c1 2018-07-12 stsp return err;
4383 245d91c1 2018-07-12 stsp static const struct got_error *
4384 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4385 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4387 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4388 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4390 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4392 c4843652 2019-08-12 stsp s->path = strdup(path);
4393 c4843652 2019-08-12 stsp if (s->path == NULL)
4394 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4396 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4397 c4843652 2019-08-12 stsp if (err) {
4398 c4843652 2019-08-12 stsp free(s->path);
4399 7cbe629d 2018-08-04 stsp return err;
4402 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4403 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4404 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4405 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4406 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4407 fb2756b9 2018-08-04 stsp s->repo = repo;
4408 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4409 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4411 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4412 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4413 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4414 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4416 11b20872 2019-11-08 stsp return err;
4419 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4420 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4421 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4422 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4423 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4425 a5388363 2020-12-01 naddy return run_blame(view);
4428 e5a0f69f 2018-08-18 stsp static const struct got_error *
4429 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4431 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4432 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4434 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4435 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4437 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4438 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4439 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4440 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4441 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4444 e5a0f69f 2018-08-18 stsp free(s->path);
4445 11b20872 2019-11-08 stsp free_colors(&s->colors);
4447 e5a0f69f 2018-08-18 stsp return err;
4450 7cbe629d 2018-08-04 stsp static const struct got_error *
4451 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4453 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4455 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4456 6c4c42e0 2019-06-24 stsp return NULL;
4459 6c4c42e0 2019-06-24 stsp static const struct got_error *
4460 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4462 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4463 6c4c42e0 2019-06-24 stsp int lineno;
4464 826082fe 2020-12-10 stsp char *line = NULL;
4465 826082fe 2020-12-10 stsp size_t linesize = 0;
4466 826082fe 2020-12-10 stsp ssize_t linelen;
4468 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4469 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4470 6c4c42e0 2019-06-24 stsp return NULL;
4473 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4474 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4475 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4477 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4479 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4480 6c4c42e0 2019-06-24 stsp lineno = 1;
4482 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4485 6c4c42e0 2019-06-24 stsp while (1) {
4486 6c4c42e0 2019-06-24 stsp off_t offset;
4488 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4489 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4490 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4494 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4495 6c4c42e0 2019-06-24 stsp lineno = 1;
4497 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4500 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4501 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4502 6c4c42e0 2019-06-24 stsp free(line);
4503 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4505 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4506 826082fe 2020-12-10 stsp if (linelen != -1 &&
4507 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4508 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4509 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4512 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4517 826082fe 2020-12-10 stsp free(line);
4519 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4520 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4521 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4524 6c4c42e0 2019-06-24 stsp return NULL;
4527 6c4c42e0 2019-06-24 stsp static const struct got_error *
4528 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4530 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4531 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4532 2b380cc8 2018-10-24 stsp int errcode;
4534 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4535 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4536 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4537 2b380cc8 2018-10-24 stsp if (errcode)
4538 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4540 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4543 51fe7530 2019-08-19 stsp if (s->blame_complete)
4544 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4546 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4548 669b5ffa 2018-10-07 stsp view_vborder(view);
4549 e5a0f69f 2018-08-18 stsp return err;
4552 e5a0f69f 2018-08-18 stsp static const struct got_error *
4553 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4555 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4556 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4557 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4558 669b5ffa 2018-10-07 stsp int begin_x = 0;
4560 e5a0f69f 2018-08-18 stsp switch (ch) {
4562 1e37a5c2 2019-05-12 jcs s->done = 1;
4564 4deef56f 2021-09-02 naddy case 'g':
4565 4deef56f 2021-09-02 naddy case KEY_HOME:
4566 4deef56f 2021-09-02 naddy s->selected_line = 1;
4567 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4569 4deef56f 2021-09-02 naddy case 'G':
4570 4deef56f 2021-09-02 naddy case KEY_END:
4571 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4572 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4573 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4575 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4576 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4577 4deef56f 2021-09-02 naddy (view->nlines - 3);
4581 1e37a5c2 2019-05-12 jcs case KEY_UP:
4582 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4583 1e37a5c2 2019-05-12 jcs s->selected_line--;
4584 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4585 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4586 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4588 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4589 ea025d1d 2020-02-22 naddy case CTRL('b'):
4590 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4591 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4594 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4595 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4596 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4598 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4601 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4602 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4603 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4604 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4605 1e37a5c2 2019-05-12 jcs s->selected_line++;
4606 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4607 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4608 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4611 1e37a5c2 2019-05-12 jcs case 'p': {
4612 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4613 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4614 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4615 1e37a5c2 2019-05-12 jcs if (id == NULL)
4617 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4618 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4619 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4620 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4621 1e37a5c2 2019-05-12 jcs int obj_type;
4622 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4623 1e37a5c2 2019-05-12 jcs s->repo, id);
4626 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4627 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4628 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4629 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4632 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4633 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4634 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4635 e5a0f69f 2018-08-18 stsp if (err) {
4636 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4637 1e37a5c2 2019-05-12 jcs err = NULL;
4638 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4641 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4643 1e37a5c2 2019-05-12 jcs free(blob_id);
4644 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4645 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4646 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4649 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4651 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4653 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4654 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4656 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4661 1e37a5c2 2019-05-12 jcs s->done = 1;
4662 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4663 1e37a5c2 2019-05-12 jcs s->done = 0;
4664 1e37a5c2 2019-05-12 jcs if (thread_err)
4666 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4667 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4668 a5388363 2020-12-01 naddy err = run_blame(view);
4673 1e37a5c2 2019-05-12 jcs case 'B': {
4674 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4675 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4676 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4678 1e37a5c2 2019-05-12 jcs s->done = 1;
4679 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4680 1e37a5c2 2019-05-12 jcs s->done = 0;
4681 1e37a5c2 2019-05-12 jcs if (thread_err)
4683 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4684 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4685 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4686 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4687 a5388363 2020-12-01 naddy err = run_blame(view);
4692 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4693 1e37a5c2 2019-05-12 jcs case '\r': {
4694 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4695 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4696 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4697 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4698 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4699 1e37a5c2 2019-05-12 jcs if (id == NULL)
4701 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4704 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4705 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4706 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4707 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4708 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4709 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4710 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4711 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4714 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4715 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4716 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4718 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4721 e78dc838 2020-12-04 stsp view->focussed = 0;
4722 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4723 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4724 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4727 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4728 e78dc838 2020-12-04 stsp view->focus_child = 1;
4730 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4735 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4736 ea025d1d 2020-02-22 naddy case CTRL('f'):
4738 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4739 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4740 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4743 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4744 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4745 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4746 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4749 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4750 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4751 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4752 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4754 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4755 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4756 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4758 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4759 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4760 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4761 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4767 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4770 a70480e0 2018-06-23 stsp static const struct got_error *
4771 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4773 a70480e0 2018-06-23 stsp const struct got_error *error;
4774 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4775 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4776 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4777 0587e10c 2020-07-23 stsp char *link_target = NULL;
4778 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4779 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4781 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4783 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4784 a70480e0 2018-06-23 stsp switch (ch) {
4786 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4789 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4790 69069811 2018-08-02 stsp if (repo_path == NULL)
4791 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4795 17020d27 2019-03-07 stsp usage_blame();
4796 a70480e0 2018-06-23 stsp /* NOTREACHED */
4800 a70480e0 2018-06-23 stsp argc -= optind;
4801 a70480e0 2018-06-23 stsp argv += optind;
4803 f135c941 2020-02-20 stsp if (argc != 1)
4804 a70480e0 2018-06-23 stsp usage_blame();
4806 69069811 2018-08-02 stsp if (repo_path == NULL) {
4807 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4808 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4809 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4810 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4811 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4812 c156c7a4 2020-12-18 stsp goto done;
4813 f135c941 2020-02-20 stsp if (worktree)
4814 eb41ed75 2019-02-05 stsp repo_path =
4815 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4817 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4818 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4819 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4820 c156c7a4 2020-12-18 stsp goto done;
4824 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4825 c02c541e 2019-03-29 stsp if (error != NULL)
4826 8e94dd5b 2019-01-04 stsp goto done;
4828 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4829 f135c941 2020-02-20 stsp worktree);
4830 c02c541e 2019-03-29 stsp if (error)
4831 92205607 2019-01-04 stsp goto done;
4833 f135c941 2020-02-20 stsp init_curses();
4835 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4836 51a10b52 2020-12-26 stsp if (error)
4837 51a10b52 2020-12-26 stsp goto done;
4839 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
4840 eb41ed75 2019-02-05 stsp if (error)
4841 69069811 2018-08-02 stsp goto done;
4843 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4844 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4845 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4846 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4847 a70480e0 2018-06-23 stsp if (error != NULL)
4848 66b4983c 2018-06-23 stsp goto done;
4849 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4850 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4852 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4853 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4855 a19e88aa 2018-06-23 stsp if (error != NULL)
4856 8b473291 2019-02-21 stsp goto done;
4858 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4859 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4860 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4861 e1cd8fed 2018-08-01 stsp goto done;
4864 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4865 0587e10c 2020-07-23 stsp commit_id, repo);
4866 7cbe629d 2018-08-04 stsp if (error)
4867 7cbe629d 2018-08-04 stsp goto done;
4869 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4870 78756c87 2020-11-24 stsp commit_id, repo);
4871 0587e10c 2020-07-23 stsp if (error)
4872 0587e10c 2020-07-23 stsp goto done;
4873 12314ad4 2019-08-31 stsp if (worktree) {
4874 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4875 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4876 12314ad4 2019-08-31 stsp worktree = NULL;
4878 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4880 69069811 2018-08-02 stsp free(repo_path);
4881 f135c941 2020-02-20 stsp free(in_repo_path);
4882 0587e10c 2020-07-23 stsp free(link_target);
4883 69069811 2018-08-02 stsp free(cwd);
4884 a70480e0 2018-06-23 stsp free(commit_id);
4885 eb41ed75 2019-02-05 stsp if (worktree)
4886 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4887 1d0f4054 2021-06-17 stsp if (repo) {
4888 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4889 1d0f4054 2021-06-17 stsp if (error == NULL)
4890 1d0f4054 2021-06-17 stsp error = close_err;
4892 51a10b52 2020-12-26 stsp tog_free_refs();
4893 a70480e0 2018-06-23 stsp return error;
4896 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4897 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4899 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4900 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4901 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4902 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4903 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4904 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4905 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4907 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4909 f7d12f7e 2018-08-01 stsp werase(view->window);
4911 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4912 ffd1d5e5 2018-06-23 stsp return NULL;
4914 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4916 ffd1d5e5 2018-06-23 stsp return err;
4917 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4918 a3404814 2018-09-02 stsp wstandout(view->window);
4919 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4921 11b20872 2019-11-08 stsp wattr_on(view->window,
4922 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4923 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4925 11b20872 2019-11-08 stsp wattr_off(view->window,
4926 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4927 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4928 a3404814 2018-09-02 stsp wstandend(view->window);
4929 2550e4c3 2018-07-13 stsp free(wline);
4930 2550e4c3 2018-07-13 stsp wline = NULL;
4931 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4932 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4933 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4934 ffd1d5e5 2018-06-23 stsp return NULL;
4935 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4937 ce52c690 2018-06-23 stsp return err;
4938 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4939 2550e4c3 2018-07-13 stsp free(wline);
4940 2550e4c3 2018-07-13 stsp wline = NULL;
4941 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4942 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4943 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4944 ffd1d5e5 2018-06-23 stsp return NULL;
4945 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4946 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4947 a1eca9bb 2018-06-23 stsp return NULL;
4949 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4950 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4951 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4952 0cf4efb1 2018-09-29 stsp if (view->focussed)
4953 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4954 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4956 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4957 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4958 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4959 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4960 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4961 ffd1d5e5 2018-06-23 stsp return NULL;
4965 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4968 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4969 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4970 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4971 848d6979 2019-08-12 stsp const char *modestr = "";
4972 56e0773d 2019-11-28 stsp mode_t mode;
4974 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4975 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4977 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4978 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4979 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4981 638f9024 2019-05-13 stsp return got_error_from_errno(
4982 230a42bd 2019-05-11 jcs "got_object_id_str");
4984 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4985 63c5ca5d 2019-08-24 stsp modestr = "$";
4986 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4989 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4990 d86d3b18 2020-12-01 naddy te, s->repo);
4991 0d6c6ee3 2020-05-20 stsp if (err) {
4992 0d6c6ee3 2020-05-20 stsp free(id_str);
4993 0d6c6ee3 2020-05-20 stsp return err;
4995 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4996 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4997 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4999 848d6979 2019-08-12 stsp modestr = "@";
5001 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5002 848d6979 2019-08-12 stsp modestr = "/";
5003 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5004 848d6979 2019-08-12 stsp modestr = "*";
5005 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5006 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5007 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5008 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5009 1d13200f 2018-07-12 stsp free(id_str);
5010 0d6c6ee3 2020-05-20 stsp free(link_target);
5011 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5013 1d13200f 2018-07-12 stsp free(id_str);
5014 0d6c6ee3 2020-05-20 stsp free(link_target);
5015 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5016 ffd1d5e5 2018-06-23 stsp if (err) {
5017 ffd1d5e5 2018-06-23 stsp free(line);
5020 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5021 0cf4efb1 2018-09-29 stsp if (view->focussed)
5022 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5023 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5025 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5027 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5028 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5029 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5031 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5032 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5033 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5034 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5035 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5036 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5037 ffd1d5e5 2018-06-23 stsp free(line);
5038 2550e4c3 2018-07-13 stsp free(wline);
5039 2550e4c3 2018-07-13 stsp wline = NULL;
5041 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5042 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5043 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5047 ffd1d5e5 2018-06-23 stsp return err;
5050 ffd1d5e5 2018-06-23 stsp static void
5051 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5053 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5054 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5055 fa86c4bf 2020-11-29 stsp int i = 0;
5057 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5060 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5061 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5062 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5063 fa86c4bf 2020-11-29 stsp if (!isroot)
5064 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5067 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5068 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5072 fa86c4bf 2020-11-29 stsp static void
5073 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5075 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5076 ffd1d5e5 2018-06-23 stsp int n = 0;
5078 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5079 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5080 694d3271 2020-12-01 naddy s->first_displayed_entry);
5082 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5084 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5085 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5086 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5087 768394f3 2019-01-24 stsp if (last) {
5088 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5089 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5094 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5095 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5096 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5098 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5099 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5100 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5102 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5103 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5104 56e0773d 2019-11-28 stsp + 1 /* slash */;
5106 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5108 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5109 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5110 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5112 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5113 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5114 d9765a41 2018-06-23 stsp while (pt) {
5115 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5116 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5117 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5118 cb2ebc8a 2018-06-23 stsp goto done;
5120 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5121 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5122 cb2ebc8a 2018-06-23 stsp goto done;
5124 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5127 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5128 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5129 ce52c690 2018-06-23 stsp goto done;
5133 ce52c690 2018-06-23 stsp if (err) {
5134 ce52c690 2018-06-23 stsp free(*path);
5135 ce52c690 2018-06-23 stsp *path = NULL;
5137 ce52c690 2018-06-23 stsp return err;
5140 ce52c690 2018-06-23 stsp static const struct got_error *
5141 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5142 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5143 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5145 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5146 ce52c690 2018-06-23 stsp char *path;
5147 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5149 a0de39f3 2019-08-09 stsp *new_view = NULL;
5151 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5153 ce52c690 2018-06-23 stsp return err;
5155 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5156 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5157 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5158 83ce39e3 2019-08-12 stsp goto done;
5161 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5162 e5a0f69f 2018-08-18 stsp if (err) {
5163 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5164 fc06ba56 2019-08-22 stsp err = NULL;
5165 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5167 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5169 83ce39e3 2019-08-12 stsp free(path);
5170 69efd4c4 2018-07-18 stsp return err;
5173 69efd4c4 2018-07-18 stsp static const struct got_error *
5174 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5175 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5177 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5178 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5179 69efd4c4 2018-07-18 stsp char *path;
5181 a0de39f3 2019-08-09 stsp *new_view = NULL;
5183 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5184 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5185 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5187 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5189 69efd4c4 2018-07-18 stsp return err;
5191 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5194 e5a0f69f 2018-08-18 stsp view_close(log_view);
5196 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5197 cb2ebc8a 2018-06-23 stsp free(path);
5198 cb2ebc8a 2018-06-23 stsp return err;
5201 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5202 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5203 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5205 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5206 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5207 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5208 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5210 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5211 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5213 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5214 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5215 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5217 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5219 bc573f3b 2021-07-10 stsp goto done;
5222 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5223 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5224 bc573f3b 2021-07-10 stsp * closed on demand.
5226 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5227 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5229 bc573f3b 2021-07-10 stsp goto done;
5230 bc573f3b 2021-07-10 stsp s->tree = s->root;
5232 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5233 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5234 ffd1d5e5 2018-06-23 stsp goto done;
5236 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5237 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5238 ffd1d5e5 2018-06-23 stsp goto done;
5241 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5242 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5243 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5244 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5245 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5246 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5247 9cd7cbd1 2020-12-07 stsp goto done;
5250 fb2756b9 2018-08-04 stsp s->repo = repo;
5252 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5253 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5254 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5255 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5257 c0b01bdb 2019-11-08 stsp goto done;
5258 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5259 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5261 c0b01bdb 2019-11-08 stsp goto done;
5262 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5263 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5264 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5266 c0b01bdb 2019-11-08 stsp goto done;
5268 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5269 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5270 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5272 11b20872 2019-11-08 stsp goto done;
5274 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5275 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5277 c0b01bdb 2019-11-08 stsp goto done;
5280 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5281 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5282 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5283 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5284 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5286 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5287 bc573f3b 2021-07-10 stsp if (commit)
5288 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5290 bc573f3b 2021-07-10 stsp close_tree_view(view);
5291 ad80ab7b 2018-08-04 stsp return err;
5294 e5a0f69f 2018-08-18 stsp static const struct got_error *
5295 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5297 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5299 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5300 fb2756b9 2018-08-04 stsp free(s->tree_label);
5301 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5302 6484ec90 2018-09-29 stsp free(s->commit_id);
5303 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5304 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5305 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5306 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5307 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5308 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5309 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5310 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5311 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5312 ad80ab7b 2018-08-04 stsp free(parent);
5315 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5316 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5317 bc573f3b 2021-07-10 stsp if (s->root)
5318 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5319 7c32bd05 2019-06-22 stsp return NULL;
5322 7c32bd05 2019-06-22 stsp static const struct got_error *
5323 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5325 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5327 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5328 7c32bd05 2019-06-22 stsp return NULL;
5331 7c32bd05 2019-06-22 stsp static int
5332 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5334 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5336 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5340 7c32bd05 2019-06-22 stsp static const struct got_error *
5341 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5343 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5344 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5346 7c32bd05 2019-06-22 stsp if (!view->searching) {
5347 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5348 7c32bd05 2019-06-22 stsp return NULL;
5351 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5352 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5353 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5354 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5355 56e0773d 2019-11-28 stsp s->selected_entry);
5357 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5359 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5360 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5362 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5363 56e0773d 2019-11-28 stsp s->selected_entry);
5366 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5367 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5369 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5372 7c32bd05 2019-06-22 stsp while (1) {
5373 56e0773d 2019-11-28 stsp if (te == NULL) {
5374 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5375 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5376 ac66afb8 2019-06-24 stsp return NULL;
5378 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5379 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5381 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5384 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5385 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5386 56e0773d 2019-11-28 stsp s->matched_entry = te;
5390 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5391 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5393 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5396 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5397 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5398 7c32bd05 2019-06-22 stsp s->selected = 0;
5401 e5a0f69f 2018-08-18 stsp return NULL;
5404 ad80ab7b 2018-08-04 stsp static const struct got_error *
5405 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5407 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5408 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5409 e5a0f69f 2018-08-18 stsp char *parent_path;
5411 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5413 e5a0f69f 2018-08-18 stsp return err;
5415 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5416 e5a0f69f 2018-08-18 stsp free(parent_path);
5418 669b5ffa 2018-10-07 stsp view_vborder(view);
5419 e5a0f69f 2018-08-18 stsp return err;
5422 e5a0f69f 2018-08-18 stsp static const struct got_error *
5423 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5425 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5426 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5427 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5428 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5429 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
5431 e5a0f69f 2018-08-18 stsp switch (ch) {
5433 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5436 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5438 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5439 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5440 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5441 e78dc838 2020-12-04 stsp view->focussed = 0;
5442 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5443 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5444 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5446 1e37a5c2 2019-05-12 jcs return err;
5447 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5448 e78dc838 2020-12-04 stsp view->focus_child = 1;
5450 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5453 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5454 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5455 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5456 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5457 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5458 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5459 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5460 152c1c93 2020-11-29 stsp if (err) {
5461 152c1c93 2020-11-29 stsp view_close(ref_view);
5462 152c1c93 2020-11-29 stsp return err;
5464 e78dc838 2020-12-04 stsp view->focussed = 0;
5465 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5466 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5467 152c1c93 2020-11-29 stsp err = view_close_child(view);
5469 152c1c93 2020-11-29 stsp return err;
5470 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5471 e78dc838 2020-12-04 stsp view->focus_child = 1;
5473 152c1c93 2020-11-29 stsp *new_view = ref_view;
5475 e4526bf5 2021-09-03 naddy case 'g':
5476 e4526bf5 2021-09-03 naddy case KEY_HOME:
5477 e4526bf5 2021-09-03 naddy s->selected = 0;
5478 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5479 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5480 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5482 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5484 e4526bf5 2021-09-03 naddy case 'G':
5485 e4526bf5 2021-09-03 naddy case KEY_END:
5486 e4526bf5 2021-09-03 naddy s->selected = 0;
5487 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5488 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5489 e4526bf5 2021-09-03 naddy if (te == NULL) {
5490 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5491 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5496 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5497 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5499 e4526bf5 2021-09-03 naddy if (n > 0)
5500 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5503 1e37a5c2 2019-05-12 jcs case KEY_UP:
5504 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5505 1e37a5c2 2019-05-12 jcs s->selected--;
5508 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5510 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5511 ea025d1d 2020-02-22 naddy case CTRL('b'):
5512 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5513 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5514 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5515 fa86c4bf 2020-11-29 stsp s->selected = 0;
5517 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5518 fa86c4bf 2020-11-29 stsp s->selected = 0;
5520 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5523 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5524 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5525 1e37a5c2 2019-05-12 jcs s->selected++;
5528 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5530 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5532 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5534 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5535 ea025d1d 2020-02-22 naddy case CTRL('f'):
5536 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5538 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5539 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5540 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5543 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5545 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5547 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5548 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5549 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5550 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5551 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5553 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5554 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5556 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5557 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5558 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5559 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5560 1e37a5c2 2019-05-12 jcs s->selected_entry =
5561 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5562 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5563 1e37a5c2 2019-05-12 jcs free(parent);
5564 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5565 56e0773d 2019-11-28 stsp s->selected_entry))) {
5566 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5567 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5568 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5571 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5572 941e9f74 2019-05-21 stsp if (err) {
5573 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5576 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5577 56e0773d 2019-11-28 stsp s->selected_entry))) {
5578 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5579 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5580 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5582 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5583 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5584 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5587 e78dc838 2020-12-04 stsp view->focussed = 0;
5588 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5589 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5590 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5592 669b5ffa 2018-10-07 stsp return err;
5593 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5594 e78dc838 2020-12-04 stsp view->focus_child = 1;
5596 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5599 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5600 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5601 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5607 ffd1d5e5 2018-06-23 stsp return err;
5610 ffd1d5e5 2018-06-23 stsp __dead static void
5611 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5614 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5615 ffd1d5e5 2018-06-23 stsp getprogname());
5619 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5620 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5622 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5623 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5624 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5625 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5626 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5627 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5628 4e97c21c 2020-12-06 stsp char *label = NULL;
5629 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5630 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5632 5221c383 2018-08-01 stsp struct tog_view *view;
5634 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5635 ffd1d5e5 2018-06-23 stsp switch (ch) {
5637 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5640 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5641 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5642 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5646 e99e2d15 2020-11-24 naddy usage_tree();
5647 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5651 ffd1d5e5 2018-06-23 stsp argc -= optind;
5652 ffd1d5e5 2018-06-23 stsp argv += optind;
5654 55cccc34 2020-02-20 stsp if (argc > 1)
5655 e99e2d15 2020-11-24 naddy usage_tree();
5657 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5658 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5659 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5660 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5661 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5662 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5663 c156c7a4 2020-12-18 stsp goto done;
5664 55cccc34 2020-02-20 stsp if (worktree)
5665 52185f70 2019-02-05 stsp repo_path =
5666 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5668 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5669 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5670 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5671 c156c7a4 2020-12-18 stsp goto done;
5675 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5676 c02c541e 2019-03-29 stsp if (error != NULL)
5677 52185f70 2019-02-05 stsp goto done;
5679 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5680 55cccc34 2020-02-20 stsp repo, worktree);
5681 55cccc34 2020-02-20 stsp if (error)
5682 55cccc34 2020-02-20 stsp goto done;
5684 55cccc34 2020-02-20 stsp init_curses();
5686 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5687 c02c541e 2019-03-29 stsp if (error)
5688 52185f70 2019-02-05 stsp goto done;
5690 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
5691 51a10b52 2020-12-26 stsp if (error)
5692 51a10b52 2020-12-26 stsp goto done;
5694 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5695 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5696 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5697 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5698 4e97c21c 2020-12-06 stsp if (error)
5699 4e97c21c 2020-12-06 stsp goto done;
5700 4e97c21c 2020-12-06 stsp head_ref_name = label;
5702 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5703 4e97c21c 2020-12-06 stsp if (error == NULL)
5704 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5705 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5706 4e97c21c 2020-12-06 stsp goto done;
5707 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5708 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5709 4e97c21c 2020-12-06 stsp if (error)
5710 4e97c21c 2020-12-06 stsp goto done;
5713 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5714 5221c383 2018-08-01 stsp if (view == NULL) {
5715 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5716 5221c383 2018-08-01 stsp goto done;
5718 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5719 ad80ab7b 2018-08-04 stsp if (error)
5720 ad80ab7b 2018-08-04 stsp goto done;
5721 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5722 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5723 d91faf3b 2020-12-01 naddy in_repo_path);
5724 55cccc34 2020-02-20 stsp if (error)
5725 55cccc34 2020-02-20 stsp goto done;
5728 55cccc34 2020-02-20 stsp if (worktree) {
5729 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5730 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5731 55cccc34 2020-02-20 stsp worktree = NULL;
5733 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5735 52185f70 2019-02-05 stsp free(repo_path);
5736 e4a0e26d 2020-02-20 stsp free(cwd);
5737 ffd1d5e5 2018-06-23 stsp free(commit_id);
5738 4e97c21c 2020-12-06 stsp free(label);
5740 486cd271 2020-12-06 stsp got_ref_close(ref);
5741 1d0f4054 2021-06-17 stsp if (repo) {
5742 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5743 1d0f4054 2021-06-17 stsp if (error == NULL)
5744 1d0f4054 2021-06-17 stsp error = close_err;
5746 51a10b52 2020-12-26 stsp tog_free_refs();
5747 ffd1d5e5 2018-06-23 stsp return error;
5750 6458efa5 2020-11-24 stsp static const struct got_error *
5751 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5753 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5754 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5756 6458efa5 2020-11-24 stsp s->nrefs = 0;
5757 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5758 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5761 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5762 6458efa5 2020-11-24 stsp if (re == NULL)
5763 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5765 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5766 8924d611 2020-12-26 stsp if (re->ref == NULL)
5767 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5768 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5769 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5772 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5773 6458efa5 2020-11-24 stsp return NULL;
5777 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5779 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5781 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5782 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5783 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5784 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5789 6458efa5 2020-11-24 stsp static const struct got_error *
5790 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5792 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5793 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5795 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5796 6458efa5 2020-11-24 stsp s->repo = repo;
5798 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5799 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5801 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5803 6458efa5 2020-11-24 stsp return err;
5805 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5806 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5807 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5808 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5810 6458efa5 2020-11-24 stsp goto done;
5812 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5813 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5814 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5816 6458efa5 2020-11-24 stsp goto done;
5818 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5819 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5820 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5822 6458efa5 2020-11-24 stsp goto done;
5825 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5826 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5827 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5828 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5829 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5832 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5833 6458efa5 2020-11-24 stsp return err;
5836 6458efa5 2020-11-24 stsp static const struct got_error *
5837 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5839 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5841 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5842 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5844 6458efa5 2020-11-24 stsp return NULL;
5847 6458efa5 2020-11-24 stsp static const struct got_error *
5848 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5849 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5851 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5852 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5853 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5854 6458efa5 2020-11-24 stsp int obj_type;
5856 c42c9805 2020-11-24 stsp *commit_id = NULL;
5858 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5860 6458efa5 2020-11-24 stsp return err;
5862 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5864 6458efa5 2020-11-24 stsp goto done;
5866 6458efa5 2020-11-24 stsp switch (obj_type) {
5867 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5868 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5870 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5871 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5873 6458efa5 2020-11-24 stsp goto done;
5874 c42c9805 2020-11-24 stsp free(obj_id);
5875 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5876 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5878 6458efa5 2020-11-24 stsp goto done;
5879 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5880 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5881 c42c9805 2020-11-24 stsp goto done;
5883 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5884 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5885 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5886 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5887 c42c9805 2020-11-24 stsp goto done;
5891 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5897 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5898 c42c9805 2020-11-24 stsp if (err) {
5899 c42c9805 2020-11-24 stsp free(*commit_id);
5900 c42c9805 2020-11-24 stsp *commit_id = NULL;
5902 c42c9805 2020-11-24 stsp return err;
5905 c42c9805 2020-11-24 stsp static const struct got_error *
5906 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5907 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5909 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5910 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5911 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5913 c42c9805 2020-11-24 stsp *new_view = NULL;
5915 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5916 c42c9805 2020-11-24 stsp if (err) {
5917 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5918 c42c9805 2020-11-24 stsp return err;
5920 c42c9805 2020-11-24 stsp return NULL;
5923 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5924 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5925 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5926 6458efa5 2020-11-24 stsp goto done;
5929 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5930 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5933 6458efa5 2020-11-24 stsp view_close(log_view);
5935 6458efa5 2020-11-24 stsp *new_view = log_view;
5936 c42c9805 2020-11-24 stsp free(commit_id);
5937 6458efa5 2020-11-24 stsp return err;
5940 ce5b7c56 2019-07-09 stsp static void
5941 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5943 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5944 34ba6917 2020-11-30 stsp int i = 0;
5946 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5949 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5950 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5951 34ba6917 2020-11-30 stsp if (re == NULL)
5953 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5954 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5958 34ba6917 2020-11-30 stsp static void
5959 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5961 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5962 6458efa5 2020-11-24 stsp int n = 0;
5964 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5965 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5967 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5969 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5970 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5971 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5972 6458efa5 2020-11-24 stsp if (last) {
5973 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5974 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5979 6458efa5 2020-11-24 stsp static const struct got_error *
5980 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5982 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5984 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5985 6458efa5 2020-11-24 stsp return NULL;
5988 6458efa5 2020-11-24 stsp static int
5989 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5991 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5993 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5997 6458efa5 2020-11-24 stsp static const struct got_error *
5998 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6000 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6001 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6003 6458efa5 2020-11-24 stsp if (!view->searching) {
6004 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6005 6458efa5 2020-11-24 stsp return NULL;
6008 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6009 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6010 6458efa5 2020-11-24 stsp if (s->selected_entry)
6011 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6013 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6014 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6016 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6017 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6019 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6020 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6023 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6024 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6026 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6029 6458efa5 2020-11-24 stsp while (1) {
6030 6458efa5 2020-11-24 stsp if (re == NULL) {
6031 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6032 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6033 6458efa5 2020-11-24 stsp return NULL;
6035 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6036 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6038 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6041 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6042 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6043 6458efa5 2020-11-24 stsp s->matched_entry = re;
6047 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6048 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6050 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6053 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6054 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6055 6458efa5 2020-11-24 stsp s->selected = 0;
6058 6458efa5 2020-11-24 stsp return NULL;
6061 6458efa5 2020-11-24 stsp static const struct got_error *
6062 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6064 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6065 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6066 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6067 6458efa5 2020-11-24 stsp char *line = NULL;
6068 6458efa5 2020-11-24 stsp wchar_t *wline;
6069 6458efa5 2020-11-24 stsp struct tog_color *tc;
6070 6458efa5 2020-11-24 stsp int width, n;
6071 6458efa5 2020-11-24 stsp int limit = view->nlines;
6073 6458efa5 2020-11-24 stsp werase(view->window);
6075 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6077 6458efa5 2020-11-24 stsp if (limit == 0)
6078 6458efa5 2020-11-24 stsp return NULL;
6080 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6082 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6083 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6084 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6086 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6087 6458efa5 2020-11-24 stsp if (err) {
6088 6458efa5 2020-11-24 stsp free(line);
6089 6458efa5 2020-11-24 stsp return err;
6091 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6092 6458efa5 2020-11-24 stsp wstandout(view->window);
6093 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6094 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6095 6458efa5 2020-11-24 stsp wstandend(view->window);
6096 6458efa5 2020-11-24 stsp free(wline);
6097 6458efa5 2020-11-24 stsp wline = NULL;
6098 6458efa5 2020-11-24 stsp free(line);
6099 6458efa5 2020-11-24 stsp line = NULL;
6100 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6101 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6102 6458efa5 2020-11-24 stsp if (--limit <= 0)
6103 6458efa5 2020-11-24 stsp return NULL;
6106 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6107 6458efa5 2020-11-24 stsp char *line = NULL;
6109 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6110 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6111 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6112 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6113 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6114 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6115 6458efa5 2020-11-24 stsp struct got_object_id *id;
6116 6458efa5 2020-11-24 stsp char *id_str;
6117 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6119 6458efa5 2020-11-24 stsp return err;
6120 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6121 6458efa5 2020-11-24 stsp if (err) {
6123 6458efa5 2020-11-24 stsp return err;
6125 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6126 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6127 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6129 6458efa5 2020-11-24 stsp free(id_str);
6130 6458efa5 2020-11-24 stsp return err;
6133 6458efa5 2020-11-24 stsp free(id_str);
6135 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6136 6458efa5 2020-11-24 stsp if (line == NULL)
6137 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6140 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6141 6458efa5 2020-11-24 stsp if (err) {
6142 6458efa5 2020-11-24 stsp free(line);
6143 6458efa5 2020-11-24 stsp return err;
6145 6458efa5 2020-11-24 stsp if (n == s->selected) {
6146 6458efa5 2020-11-24 stsp if (view->focussed)
6147 6458efa5 2020-11-24 stsp wstandout(view->window);
6148 6458efa5 2020-11-24 stsp s->selected_entry = re;
6150 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6152 6458efa5 2020-11-24 stsp wattr_on(view->window,
6153 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6154 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6156 6458efa5 2020-11-24 stsp wattr_off(view->window,
6157 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6158 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6159 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6160 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6161 6458efa5 2020-11-24 stsp wstandend(view->window);
6162 6458efa5 2020-11-24 stsp free(line);
6163 6458efa5 2020-11-24 stsp free(wline);
6164 6458efa5 2020-11-24 stsp wline = NULL;
6166 6458efa5 2020-11-24 stsp s->ndisplayed++;
6167 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6170 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6173 6458efa5 2020-11-24 stsp view_vborder(view);
6174 6458efa5 2020-11-24 stsp return err;
6177 6458efa5 2020-11-24 stsp static const struct got_error *
6178 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6179 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6181 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6182 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6183 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6185 c42c9805 2020-11-24 stsp *new_view = NULL;
6187 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6188 c42c9805 2020-11-24 stsp if (err) {
6189 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6190 c42c9805 2020-11-24 stsp return err;
6192 c42c9805 2020-11-24 stsp return NULL;
6196 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6197 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6198 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6199 c42c9805 2020-11-24 stsp goto done;
6202 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6203 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6205 c42c9805 2020-11-24 stsp goto done;
6207 c42c9805 2020-11-24 stsp *new_view = tree_view;
6209 c42c9805 2020-11-24 stsp free(commit_id);
6210 c42c9805 2020-11-24 stsp return err;
6212 c42c9805 2020-11-24 stsp static const struct got_error *
6213 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6215 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6216 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6217 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6218 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6219 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
6221 6458efa5 2020-11-24 stsp switch (ch) {
6223 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6225 6458efa5 2020-11-24 stsp case KEY_ENTER:
6226 6458efa5 2020-11-24 stsp case '\r':
6227 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6229 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6230 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6231 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6233 e78dc838 2020-12-04 stsp view->focussed = 0;
6234 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6235 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6236 6458efa5 2020-11-24 stsp err = view_close_child(view);
6238 6458efa5 2020-11-24 stsp return err;
6239 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6240 e78dc838 2020-12-04 stsp view->focus_child = 1;
6242 6458efa5 2020-11-24 stsp *new_view = log_view;
6245 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6247 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6248 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6249 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6251 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6253 e78dc838 2020-12-04 stsp view->focussed = 0;
6254 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6255 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6256 c42c9805 2020-11-24 stsp err = view_close_child(view);
6258 c42c9805 2020-11-24 stsp return err;
6259 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6260 e78dc838 2020-12-04 stsp view->focus_child = 1;
6262 c42c9805 2020-11-24 stsp *new_view = tree_view;
6264 e4526bf5 2021-09-03 naddy case 'g':
6265 e4526bf5 2021-09-03 naddy case KEY_HOME:
6266 e4526bf5 2021-09-03 naddy s->selected = 0;
6267 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6269 e4526bf5 2021-09-03 naddy case 'G':
6270 e4526bf5 2021-09-03 naddy case KEY_END:
6271 e4526bf5 2021-09-03 naddy s->selected = 0;
6272 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6273 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6274 e4526bf5 2021-09-03 naddy if (re == NULL)
6276 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6277 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6279 e4526bf5 2021-09-03 naddy if (n > 0)
6280 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6283 6458efa5 2020-11-24 stsp case KEY_UP:
6284 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6285 6458efa5 2020-11-24 stsp s->selected--;
6288 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6290 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6291 6458efa5 2020-11-24 stsp case CTRL('b'):
6292 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6293 34ba6917 2020-11-30 stsp s->selected = 0;
6294 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6297 6458efa5 2020-11-24 stsp case KEY_DOWN:
6298 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6299 6458efa5 2020-11-24 stsp s->selected++;
6302 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6303 6458efa5 2020-11-24 stsp /* can't scroll any further */
6305 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6307 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6308 6458efa5 2020-11-24 stsp case CTRL('f'):
6309 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6310 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6311 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6312 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6315 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6317 6458efa5 2020-11-24 stsp case CTRL('l'):
6318 8924d611 2020-12-26 stsp tog_free_refs();
6319 8924d611 2020-12-26 stsp err = tog_load_refs(s->repo);
6322 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6323 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6325 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6326 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6327 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6333 6458efa5 2020-11-24 stsp return err;
6336 6458efa5 2020-11-24 stsp __dead static void
6337 6458efa5 2020-11-24 stsp usage_ref(void)
6340 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6341 6458efa5 2020-11-24 stsp getprogname());
6345 6458efa5 2020-11-24 stsp static const struct got_error *
6346 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6348 6458efa5 2020-11-24 stsp const struct got_error *error;
6349 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6350 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6351 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6353 6458efa5 2020-11-24 stsp struct tog_view *view;
6355 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6356 6458efa5 2020-11-24 stsp switch (ch) {
6358 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6359 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6360 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6364 e99e2d15 2020-11-24 naddy usage_ref();
6365 6458efa5 2020-11-24 stsp /* NOTREACHED */
6369 6458efa5 2020-11-24 stsp argc -= optind;
6370 6458efa5 2020-11-24 stsp argv += optind;
6372 6458efa5 2020-11-24 stsp if (argc > 1)
6373 e99e2d15 2020-11-24 naddy usage_ref();
6375 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6376 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6377 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6378 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6379 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6380 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6381 c156c7a4 2020-12-18 stsp goto done;
6382 6458efa5 2020-11-24 stsp if (worktree)
6383 6458efa5 2020-11-24 stsp repo_path =
6384 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6386 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6387 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6388 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6389 c156c7a4 2020-12-18 stsp goto done;
6393 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6394 6458efa5 2020-11-24 stsp if (error != NULL)
6395 6458efa5 2020-11-24 stsp goto done;
6397 6458efa5 2020-11-24 stsp init_curses();
6399 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6400 51a10b52 2020-12-26 stsp if (error)
6401 51a10b52 2020-12-26 stsp goto done;
6403 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
6404 6458efa5 2020-11-24 stsp if (error)
6405 6458efa5 2020-11-24 stsp goto done;
6407 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6408 6458efa5 2020-11-24 stsp if (view == NULL) {
6409 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6410 6458efa5 2020-11-24 stsp goto done;
6413 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6414 6458efa5 2020-11-24 stsp if (error)
6415 6458efa5 2020-11-24 stsp goto done;
6417 6458efa5 2020-11-24 stsp if (worktree) {
6418 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6419 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6420 6458efa5 2020-11-24 stsp worktree = NULL;
6422 6458efa5 2020-11-24 stsp error = view_loop(view);
6424 6458efa5 2020-11-24 stsp free(repo_path);
6425 6458efa5 2020-11-24 stsp free(cwd);
6426 1d0f4054 2021-06-17 stsp if (repo) {
6427 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6428 1d0f4054 2021-06-17 stsp if (close_err)
6429 1d0f4054 2021-06-17 stsp error = close_err;
6431 51a10b52 2020-12-26 stsp tog_free_refs();
6432 6458efa5 2020-11-24 stsp return error;
6435 6458efa5 2020-11-24 stsp static void
6436 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6440 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6441 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6442 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6443 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6445 6879ba42 2020-10-01 naddy fputc('\n', fp);
6448 4ed7e80c 2018-05-20 stsp __dead static void
6449 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6451 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6453 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6454 6879ba42 2020-10-01 naddy getprogname());
6455 6879ba42 2020-10-01 naddy if (hflag) {
6456 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6457 6879ba42 2020-10-01 naddy list_commands(fp);
6459 6879ba42 2020-10-01 naddy exit(status);
6462 c2301be8 2018-04-30 stsp static char **
6463 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6465 ee85c5e8 2020-02-29 stsp va_list ap;
6466 c2301be8 2018-04-30 stsp char **argv;
6469 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6471 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6472 c2301be8 2018-04-30 stsp if (argv == NULL)
6473 c2301be8 2018-04-30 stsp err(1, "calloc");
6474 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6475 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6476 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6477 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6480 ee85c5e8 2020-02-29 stsp va_end(ap);
6481 c2301be8 2018-04-30 stsp return argv;
6485 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6486 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6487 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6488 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6489 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6491 ee85c5e8 2020-02-29 stsp static const struct got_error *
6492 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6494 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6495 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6496 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6497 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6498 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6499 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6500 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6502 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6503 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6504 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6506 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6507 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6508 ee85c5e8 2020-02-29 stsp goto done;
6510 ee85c5e8 2020-02-29 stsp if (worktree)
6511 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6513 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6514 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6515 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6516 ee85c5e8 2020-02-29 stsp goto done;
6519 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6520 ee85c5e8 2020-02-29 stsp if (error != NULL)
6521 ee85c5e8 2020-02-29 stsp goto done;
6523 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6524 ee85c5e8 2020-02-29 stsp repo, worktree);
6525 ee85c5e8 2020-02-29 stsp if (error)
6526 ee85c5e8 2020-02-29 stsp goto done;
6528 87670572 2020-12-26 naddy error = tog_load_refs(repo);
6529 84de9106 2020-12-26 stsp if (error)
6530 84de9106 2020-12-26 stsp goto done;
6531 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6532 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6533 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6534 ee85c5e8 2020-02-29 stsp if (error)
6535 ee85c5e8 2020-02-29 stsp goto done;
6537 ee85c5e8 2020-02-29 stsp if (worktree) {
6538 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6539 ee85c5e8 2020-02-29 stsp worktree = NULL;
6542 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6543 ee85c5e8 2020-02-29 stsp if (error) {
6544 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6545 ee85c5e8 2020-02-29 stsp goto done;
6546 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6547 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6548 6879ba42 2020-10-01 naddy usage(1, 1);
6549 ee85c5e8 2020-02-29 stsp /* not reached */
6552 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6553 1d0f4054 2021-06-17 stsp if (error == NULL)
6554 1d0f4054 2021-06-17 stsp error = close_err;
6555 ee85c5e8 2020-02-29 stsp repo = NULL;
6557 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6558 ee85c5e8 2020-02-29 stsp if (error)
6559 ee85c5e8 2020-02-29 stsp goto done;
6561 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6563 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6564 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6566 1d0f4054 2021-06-17 stsp if (repo) {
6567 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6568 1d0f4054 2021-06-17 stsp if (error == NULL)
6569 1d0f4054 2021-06-17 stsp error = close_err;
6571 ee85c5e8 2020-02-29 stsp if (worktree)
6572 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6574 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6575 ee85c5e8 2020-02-29 stsp free(commit_id);
6576 ee85c5e8 2020-02-29 stsp free(cwd);
6577 ee85c5e8 2020-02-29 stsp free(repo_path);
6578 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6579 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6581 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6582 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6583 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6585 87670572 2020-12-26 naddy tog_free_refs();
6586 ee85c5e8 2020-02-29 stsp return error;
6590 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6592 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6593 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6594 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6595 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6596 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6597 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6598 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6601 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6603 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6604 9f7d7167 2018-04-29 stsp switch (ch) {
6606 9f7d7167 2018-04-29 stsp hflag = 1;
6609 53ccebc2 2019-07-30 stsp Vflag = 1;
6612 6879ba42 2020-10-01 naddy usage(hflag, 1);
6613 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6617 9f7d7167 2018-04-29 stsp argc -= optind;
6618 9f7d7167 2018-04-29 stsp argv += optind;
6619 9814e6a3 2020-09-27 naddy optind = 1;
6620 c2301be8 2018-04-30 stsp optreset = 1;
6622 53ccebc2 2019-07-30 stsp if (Vflag) {
6623 53ccebc2 2019-07-30 stsp got_version_print_str();
6624 6879ba42 2020-10-01 naddy return 0;
6627 4010e238 2020-12-04 stsp #ifndef PROFILE
6628 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6629 4010e238 2020-12-04 stsp NULL) == -1)
6630 4010e238 2020-12-04 stsp err(1, "pledge");
6633 c2301be8 2018-04-30 stsp if (argc == 0) {
6634 f29d3e89 2018-06-23 stsp if (hflag)
6635 6879ba42 2020-10-01 naddy usage(hflag, 0);
6636 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6637 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6639 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6643 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6644 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6645 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6646 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6647 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6653 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6654 ee85c5e8 2020-02-29 stsp if (argc != 1)
6655 6879ba42 2020-10-01 naddy usage(0, 1);
6656 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6657 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6659 ee85c5e8 2020-02-29 stsp if (hflag)
6660 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6662 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6666 b46c1e04 2020-09-20 naddy putchar('\n');
6667 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6669 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6670 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6671 a2f4a359 2020-02-28 stsp free(cmd_argv);
6674 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6675 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);