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 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
134 51a10b52 2020-12-26 stsp const struct got_error *err;
136 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
137 7f66531d 2021-11-16 stsp got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
140 51a10b52 2020-12-26 stsp return err;
142 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
146 51a10b52 2020-12-26 stsp static void
147 51a10b52 2020-12-26 stsp tog_free_refs(void)
149 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
150 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
151 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
153 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
156 51a10b52 2020-12-26 stsp static const struct got_error *
157 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
158 11b20872 2019-11-08 stsp int idx, short color)
160 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
161 11b20872 2019-11-08 stsp struct tog_color *tc;
162 11b20872 2019-11-08 stsp int regerr = 0;
164 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
165 11b20872 2019-11-08 stsp return NULL;
167 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
169 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
170 11b20872 2019-11-08 stsp if (tc == NULL)
171 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
172 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
173 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
174 11b20872 2019-11-08 stsp if (regerr) {
175 11b20872 2019-11-08 stsp static char regerr_msg[512];
176 11b20872 2019-11-08 stsp static char err_msg[512];
177 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
178 11b20872 2019-11-08 stsp sizeof(regerr_msg));
179 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
180 11b20872 2019-11-08 stsp regerr_msg);
181 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
183 11b20872 2019-11-08 stsp return err;
185 11b20872 2019-11-08 stsp tc->colorpair = idx;
186 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
187 11b20872 2019-11-08 stsp return NULL;
190 11b20872 2019-11-08 stsp static void
191 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
193 11b20872 2019-11-08 stsp struct tog_color *tc;
195 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
196 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
197 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
198 11b20872 2019-11-08 stsp regfree(&tc->regex);
203 11b20872 2019-11-08 stsp struct tog_color *
204 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
206 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
208 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
209 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
213 11b20872 2019-11-08 stsp return NULL;
217 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
219 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
220 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
221 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
222 11b20872 2019-11-08 stsp return COLOR_CYAN;
223 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
224 11b20872 2019-11-08 stsp return COLOR_YELLOW;
225 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
226 11b20872 2019-11-08 stsp return COLOR_GREEN;
227 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
228 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
229 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
230 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
231 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
232 91b8c405 2020-01-25 stsp return COLOR_CYAN;
233 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
234 11b20872 2019-11-08 stsp return COLOR_GREEN;
235 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
236 11b20872 2019-11-08 stsp return COLOR_GREEN;
237 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
238 11b20872 2019-11-08 stsp return COLOR_CYAN;
239 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
240 11b20872 2019-11-08 stsp return COLOR_YELLOW;
241 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
242 6458efa5 2020-11-24 stsp return COLOR_GREEN;
243 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
244 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
245 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
246 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
252 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
254 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
256 11b20872 2019-11-08 stsp if (val == NULL)
257 11b20872 2019-11-08 stsp return default_color_value(envvar);
259 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
260 11b20872 2019-11-08 stsp return COLOR_BLACK;
261 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
262 11b20872 2019-11-08 stsp return COLOR_RED;
263 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
266 11b20872 2019-11-08 stsp return COLOR_YELLOW;
267 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
268 11b20872 2019-11-08 stsp return COLOR_BLUE;
269 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
270 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
271 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
272 11b20872 2019-11-08 stsp return COLOR_CYAN;
273 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
274 11b20872 2019-11-08 stsp return COLOR_WHITE;
275 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
278 11b20872 2019-11-08 stsp return default_color_value(envvar);
282 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
283 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
284 3dbaef42 2020-11-24 stsp const char *label1, *label2;
286 15a087fe 2019-02-21 stsp int first_displayed_line;
287 15a087fe 2019-02-21 stsp int last_displayed_line;
289 15a087fe 2019-02-21 stsp int diff_context;
290 3dbaef42 2020-11-24 stsp int ignore_whitespace;
291 64453f7e 2020-11-21 stsp int force_text_diff;
292 15a087fe 2019-02-21 stsp struct got_repository *repo;
293 bddb1296 2019-11-08 stsp struct tog_colors colors;
294 fe621944 2020-11-10 stsp size_t nlines;
295 f44b1f58 2020-02-02 tracey off_t *line_offsets;
296 f44b1f58 2020-02-02 tracey int matched_line;
297 f44b1f58 2020-02-02 tracey int selected_line;
299 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
300 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
303 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
305 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
306 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
307 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
308 1a76625f 2018-10-22 stsp int commits_needed;
309 fb280deb 2021-08-30 stsp int load_all;
310 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
311 1a76625f 2018-10-22 stsp struct commit_queue *commits;
312 1a76625f 2018-10-22 stsp const char *in_repo_path;
313 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
314 1a76625f 2018-10-22 stsp struct got_repository *repo;
315 1a76625f 2018-10-22 stsp int log_complete;
316 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
317 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
318 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
319 13add988 2019-10-15 stsp int *searching;
320 13add988 2019-10-15 stsp int *search_next_done;
321 13add988 2019-10-15 stsp regex_t *regex;
324 1a76625f 2018-10-22 stsp struct tog_log_view_state {
325 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
326 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
327 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
328 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
329 b01e7d3b 2018-08-04 stsp int selected;
330 b01e7d3b 2018-08-04 stsp char *in_repo_path;
331 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
332 b672a97a 2020-01-27 stsp int log_branches;
333 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
334 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
335 1a76625f 2018-10-22 stsp sig_atomic_t quit;
336 1a76625f 2018-10-22 stsp pthread_t thread;
337 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
338 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
339 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
340 11b20872 2019-11-08 stsp struct tog_colors colors;
343 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
344 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
345 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
346 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
347 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
348 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
349 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
350 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
351 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
352 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
353 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
354 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
355 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
356 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
358 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
359 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
360 e9424729 2018-08-04 stsp int nlines;
362 e9424729 2018-08-04 stsp struct tog_view *view;
363 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
367 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
368 e9424729 2018-08-04 stsp const char *path;
369 e9424729 2018-08-04 stsp struct got_repository *repo;
370 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
371 e9424729 2018-08-04 stsp int *complete;
372 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
373 fc06ba56 2019-08-22 stsp void *cancel_arg;
376 e9424729 2018-08-04 stsp struct tog_blame {
378 be659d10 2020-11-18 stsp off_t filesize;
379 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
380 6fcac457 2018-11-19 stsp int nlines;
381 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
382 e9424729 2018-08-04 stsp pthread_t thread;
383 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
384 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
385 e9424729 2018-08-04 stsp const char *path;
388 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
389 7cbe629d 2018-08-04 stsp int first_displayed_line;
390 7cbe629d 2018-08-04 stsp int last_displayed_line;
391 7cbe629d 2018-08-04 stsp int selected_line;
392 7cbe629d 2018-08-04 stsp int blame_complete;
395 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
396 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
397 e5a0f69f 2018-08-18 stsp char *path;
398 7cbe629d 2018-08-04 stsp struct got_repository *repo;
399 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
400 e9424729 2018-08-04 stsp struct tog_blame blame;
401 6c4c42e0 2019-06-24 stsp int matched_line;
402 11b20872 2019-11-08 stsp struct tog_colors colors;
405 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
406 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
407 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
408 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
409 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
410 ad80ab7b 2018-08-04 stsp int selected;
413 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
415 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
416 ad80ab7b 2018-08-04 stsp char *tree_label;
417 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
418 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
419 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
420 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
421 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
422 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
423 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
424 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
425 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
426 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
427 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
428 6458efa5 2020-11-24 stsp struct tog_colors colors;
431 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
432 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
433 6458efa5 2020-11-24 stsp struct got_reference *ref;
437 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
439 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
440 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
441 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
442 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
443 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
444 7f66531d 2021-11-16 stsp int nrefs, ndisplayed, selected, show_ids, sort_by_date;
445 6458efa5 2020-11-24 stsp struct got_repository *repo;
446 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
447 bddb1296 2019-11-08 stsp struct tog_colors colors;
451 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
453 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
454 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
455 669b5ffa 2018-10-07 stsp * there is enough screen estate.
457 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
458 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
460 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
461 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
462 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
464 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
465 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
467 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
469 cc3c9aac 2018-08-01 stsp struct tog_view {
470 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
471 26ed57b2 2018-05-19 stsp WINDOW *window;
472 26ed57b2 2018-05-19 stsp PANEL *panel;
473 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
474 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
475 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
477 669b5ffa 2018-10-07 stsp struct tog_view *parent;
478 669b5ffa 2018-10-07 stsp struct tog_view *child;
481 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
482 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
483 e78dc838 2020-12-04 stsp * between parent and child.
484 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
485 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
486 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
487 e78dc838 2020-12-04 stsp * situations.
489 e78dc838 2020-12-04 stsp int focus_child;
491 5dc9f4bc 2018-08-04 stsp /* type-specific state */
492 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
494 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
495 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
496 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
497 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
498 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
501 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
502 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
503 e78dc838 2020-12-04 stsp struct tog_view *, int);
504 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
506 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
507 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
508 c0c4acc8 2021-01-24 stsp int search_started;
509 60493ae3 2019-06-20 stsp int searching;
510 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
511 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
512 60493ae3 2019-06-20 stsp int search_next_done;
513 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
514 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
515 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
516 1803e47f 2019-06-22 stsp regex_t regex;
517 41605754 2020-11-12 stsp regmatch_t regmatch;
520 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
521 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
522 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
523 78756c87 2020-11-24 stsp struct got_repository *);
524 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
525 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
526 e78dc838 2020-12-04 stsp struct tog_view *, int);
527 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
528 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
529 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
531 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
532 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
533 78756c87 2020-11-24 stsp const char *, const char *, int);
534 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
535 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
536 e78dc838 2020-12-04 stsp struct tog_view *, int);
537 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
538 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
539 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
541 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
542 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
543 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
544 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
545 e78dc838 2020-12-04 stsp struct tog_view *, int);
546 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
547 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
548 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
550 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
551 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
552 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
553 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
554 e78dc838 2020-12-04 stsp struct tog_view *, int);
555 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
556 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
557 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
559 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
560 6458efa5 2020-11-24 stsp struct got_repository *);
561 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
562 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
563 e78dc838 2020-12-04 stsp struct tog_view *, int);
564 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
565 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
566 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
568 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
569 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
570 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
572 25791caa 2018-10-24 stsp static void
573 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
575 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
578 83baff54 2019-08-12 stsp static void
579 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
581 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
584 61266923 2020-01-14 stsp static void
585 61266923 2020-01-14 stsp tog_sigcont(int signo)
587 61266923 2020-01-14 stsp tog_sigcont_received = 1;
590 e5a0f69f 2018-08-18 stsp static const struct got_error *
591 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
593 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
595 669b5ffa 2018-10-07 stsp if (view->child) {
596 669b5ffa 2018-10-07 stsp view_close(view->child);
597 669b5ffa 2018-10-07 stsp view->child = NULL;
599 e5a0f69f 2018-08-18 stsp if (view->close)
600 e5a0f69f 2018-08-18 stsp err = view->close(view);
601 ea5e7bb5 2018-08-01 stsp if (view->panel)
602 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
603 ea5e7bb5 2018-08-01 stsp if (view->window)
604 ea5e7bb5 2018-08-01 stsp delwin(view->window);
605 ea5e7bb5 2018-08-01 stsp free(view);
606 e5a0f69f 2018-08-18 stsp return err;
609 ea5e7bb5 2018-08-01 stsp static struct tog_view *
610 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
611 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
613 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
615 ea5e7bb5 2018-08-01 stsp if (view == NULL)
616 ea5e7bb5 2018-08-01 stsp return NULL;
618 d6b05b5b 2018-08-04 stsp view->type = type;
619 f7d12f7e 2018-08-01 stsp view->lines = LINES;
620 f7d12f7e 2018-08-01 stsp view->cols = COLS;
621 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
622 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
623 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
624 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
625 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
626 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
627 96a765a8 2018-08-04 stsp view_close(view);
628 ea5e7bb5 2018-08-01 stsp return NULL;
630 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
631 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
632 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
633 96a765a8 2018-08-04 stsp view_close(view);
634 ea5e7bb5 2018-08-01 stsp return NULL;
637 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
638 ea5e7bb5 2018-08-01 stsp return view;
642 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
644 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
646 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
649 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
651 5c60c32a 2018-10-18 stsp static const struct got_error *
652 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
654 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
656 5c60c32a 2018-10-18 stsp view->begin_y = 0;
657 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
658 5c60c32a 2018-10-18 stsp view->nlines = LINES;
659 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
660 5c60c32a 2018-10-18 stsp view->lines = LINES;
661 5c60c32a 2018-10-18 stsp view->cols = COLS;
662 5c60c32a 2018-10-18 stsp err = view_resize(view);
664 5c60c32a 2018-10-18 stsp return err;
666 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
667 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
669 5c60c32a 2018-10-18 stsp return NULL;
672 5c60c32a 2018-10-18 stsp static const struct got_error *
673 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
675 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
677 5c60c32a 2018-10-18 stsp view->begin_x = 0;
678 5c60c32a 2018-10-18 stsp view->begin_y = 0;
679 5c60c32a 2018-10-18 stsp view->nlines = LINES;
680 5c60c32a 2018-10-18 stsp view->ncols = COLS;
681 5c60c32a 2018-10-18 stsp view->lines = LINES;
682 5c60c32a 2018-10-18 stsp view->cols = COLS;
683 5c60c32a 2018-10-18 stsp err = view_resize(view);
685 5c60c32a 2018-10-18 stsp return err;
687 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
688 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
690 5c60c32a 2018-10-18 stsp return NULL;
694 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
696 5c60c32a 2018-10-18 stsp return view->parent == NULL;
699 4d8c2215 2018-08-19 stsp static const struct got_error *
700 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
702 f7d12f7e 2018-08-01 stsp int nlines, ncols;
704 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
705 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
707 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
709 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
710 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
712 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
714 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
715 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
716 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
717 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
718 25791caa 2018-10-24 stsp wclear(view->window);
720 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
721 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
722 0cf4efb1 2018-09-29 stsp view->lines = LINES;
723 0cf4efb1 2018-09-29 stsp view->cols = COLS;
725 6e3e5d9c 2018-10-18 stsp if (view->child) {
726 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
727 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
728 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
729 5c60c32a 2018-10-18 stsp if (view->child->focussed)
730 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
732 5c60c32a 2018-10-18 stsp show_panel(view->panel);
734 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
735 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
739 5c60c32a 2018-10-18 stsp return NULL;
742 669b5ffa 2018-10-07 stsp static const struct got_error *
743 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
745 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
747 669b5ffa 2018-10-07 stsp if (view->child == NULL)
748 669b5ffa 2018-10-07 stsp return NULL;
750 669b5ffa 2018-10-07 stsp err = view_close(view->child);
751 669b5ffa 2018-10-07 stsp view->child = NULL;
752 669b5ffa 2018-10-07 stsp return err;
755 72a9cb46 2020-12-03 stsp static void
756 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
758 669b5ffa 2018-10-07 stsp view->child = child;
759 669b5ffa 2018-10-07 stsp child->parent = view;
763 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
765 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
768 34bc9ec9 2019-02-22 stsp static void
769 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
771 25791caa 2018-10-24 stsp int cols, lines;
772 25791caa 2018-10-24 stsp struct winsize size;
774 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
775 25791caa 2018-10-24 stsp cols = 80; /* Default */
776 25791caa 2018-10-24 stsp lines = 24;
778 25791caa 2018-10-24 stsp cols = size.ws_col;
779 25791caa 2018-10-24 stsp lines = size.ws_row;
781 25791caa 2018-10-24 stsp resize_term(lines, cols);
784 2b49a8ae 2019-06-22 stsp static const struct got_error *
785 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
787 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
788 2b49a8ae 2019-06-22 stsp char pattern[1024];
791 c0c4acc8 2021-01-24 stsp if (view->search_started) {
792 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
793 c0c4acc8 2021-01-24 stsp view->searching = 0;
794 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
796 c0c4acc8 2021-01-24 stsp view->search_started = 0;
798 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
799 2b49a8ae 2019-06-22 stsp return NULL;
801 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
802 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
804 2b49a8ae 2019-06-22 stsp nocbreak();
806 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
809 2b49a8ae 2019-06-22 stsp if (ret == ERR)
810 2b49a8ae 2019-06-22 stsp return NULL;
812 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
813 7c32bd05 2019-06-22 stsp err = view->search_start(view);
815 7c32bd05 2019-06-22 stsp regfree(&view->regex);
816 7c32bd05 2019-06-22 stsp return err;
818 c0c4acc8 2021-01-24 stsp view->search_started = 1;
819 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
820 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
821 2b49a8ae 2019-06-22 stsp view->search_next(view);
824 2b49a8ae 2019-06-22 stsp return NULL;
827 0cf4efb1 2018-09-29 stsp static const struct got_error *
828 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
829 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
831 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
832 669b5ffa 2018-10-07 stsp struct tog_view *v;
833 1a76625f 2018-10-22 stsp int ch, errcode;
835 e5a0f69f 2018-08-18 stsp *new = NULL;
837 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
838 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
839 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
840 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
842 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
843 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
844 82954512 2020-02-03 stsp if (errcode)
845 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
846 82954512 2020-02-03 stsp "pthread_mutex_unlock");
847 3da8ef85 2021-09-21 stsp sched_yield();
848 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
849 82954512 2020-02-03 stsp if (errcode)
850 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
851 82954512 2020-02-03 stsp "pthread_mutex_lock");
852 60493ae3 2019-06-20 stsp view->search_next(view);
853 60493ae3 2019-06-20 stsp return NULL;
856 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
857 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
858 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
859 1a76625f 2018-10-22 stsp if (errcode)
860 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
861 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
862 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
863 1a76625f 2018-10-22 stsp if (errcode)
864 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
865 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
867 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
868 25791caa 2018-10-24 stsp tog_resizeterm();
869 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
870 61266923 2020-01-14 stsp tog_sigcont_received = 0;
871 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
872 25791caa 2018-10-24 stsp err = view_resize(v);
874 25791caa 2018-10-24 stsp return err;
875 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
877 25791caa 2018-10-24 stsp return err;
878 cdfcfb03 2020-12-06 stsp if (v->child) {
879 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
881 cdfcfb03 2020-12-06 stsp return err;
882 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
883 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
885 cdfcfb03 2020-12-06 stsp return err;
890 e5a0f69f 2018-08-18 stsp switch (ch) {
892 1e37a5c2 2019-05-12 jcs if (view->child) {
893 e78dc838 2020-12-04 stsp view->focussed = 0;
894 e78dc838 2020-12-04 stsp view->child->focussed = 1;
895 e78dc838 2020-12-04 stsp view->focus_child = 1;
896 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
897 e78dc838 2020-12-04 stsp view->focussed = 0;
898 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
899 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
903 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
904 9970f7fc 2020-12-03 stsp view->dying = 1;
910 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
911 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
913 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
914 e78dc838 2020-12-04 stsp view->focussed = 0;
915 e78dc838 2020-12-04 stsp view->child->focussed = 1;
916 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
918 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
921 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
922 9970f7fc 2020-12-03 stsp KEY_RESIZE);
924 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
925 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
926 e78dc838 2020-12-04 stsp view->focussed = 1;
927 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
929 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
933 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
936 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
939 60493ae3 2019-06-20 stsp if (view->search_start)
940 2b49a8ae 2019-06-22 stsp view_search_start(view);
942 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
946 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
947 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
948 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
949 60493ae3 2019-06-20 stsp view->search_next_done = 0;
950 60493ae3 2019-06-20 stsp view->search_next(view);
952 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
955 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
959 e5a0f69f 2018-08-18 stsp return err;
963 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
965 0cf4efb1 2018-09-29 stsp PANEL *panel;
966 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
968 669b5ffa 2018-10-07 stsp if (view->parent)
969 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
971 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
972 0cf4efb1 2018-09-29 stsp if (panel == NULL)
975 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
976 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
977 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
981 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
983 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
984 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
986 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
988 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
991 669b5ffa 2018-10-07 stsp return view->focussed;
994 bcbd79e2 2018-08-19 stsp static const struct got_error *
995 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
997 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
998 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
999 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1000 fd823528 2018-10-22 stsp int fast_refresh = 10;
1001 1a76625f 2018-10-22 stsp int done = 0, errcode;
1003 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1004 1a76625f 2018-10-22 stsp if (errcode)
1005 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1007 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1008 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1010 1004088d 2018-09-29 stsp view->focussed = 1;
1011 878940b7 2018-09-29 stsp err = view->show(view);
1013 0cf4efb1 2018-09-29 stsp return err;
1014 0cf4efb1 2018-09-29 stsp update_panels();
1015 0cf4efb1 2018-09-29 stsp doupdate();
1016 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1017 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1018 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1019 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1021 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1024 9970f7fc 2020-12-03 stsp if (view->dying) {
1025 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1027 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1028 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1030 e78dc838 2020-12-04 stsp else if (view->parent)
1031 669b5ffa 2018-10-07 stsp prev = view->parent;
1033 e78dc838 2020-12-04 stsp if (view->parent) {
1034 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1035 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1037 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1039 9970f7fc 2020-12-03 stsp err = view_close(view);
1041 e5a0f69f 2018-08-18 stsp goto done;
1043 e78dc838 2020-12-04 stsp view = NULL;
1044 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1045 e78dc838 2020-12-04 stsp if (v->focussed)
1048 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1049 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1051 e78dc838 2020-12-04 stsp view = prev;
1052 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1053 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1054 e78dc838 2020-12-04 stsp tog_view_list_head);
1056 e78dc838 2020-12-04 stsp if (view) {
1057 e78dc838 2020-12-04 stsp if (view->focus_child) {
1058 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1059 e78dc838 2020-12-04 stsp view = view->child;
1061 e78dc838 2020-12-04 stsp view->focussed = 1;
1065 bcbd79e2 2018-08-19 stsp if (new_view) {
1066 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1067 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1068 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1069 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1071 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1072 86c66b02 2018-10-18 stsp err = view_close(v);
1074 86c66b02 2018-10-18 stsp goto done;
1077 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1078 fed7eaa8 2018-10-24 stsp view = new_view;
1080 669b5ffa 2018-10-07 stsp if (view) {
1081 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1082 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1083 e78dc838 2020-12-04 stsp view = view->child;
1085 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1086 e78dc838 2020-12-04 stsp view = view->parent;
1088 e78dc838 2020-12-04 stsp show_panel(view->panel);
1089 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1090 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1091 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1092 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1094 1a76625f 2018-10-22 stsp goto done;
1096 669b5ffa 2018-10-07 stsp err = view->show(view);
1098 1a76625f 2018-10-22 stsp goto done;
1099 669b5ffa 2018-10-07 stsp if (view->child) {
1100 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1102 1a76625f 2018-10-22 stsp goto done;
1104 1a76625f 2018-10-22 stsp update_panels();
1105 1a76625f 2018-10-22 stsp doupdate();
1109 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1110 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1111 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1112 e5a0f69f 2018-08-18 stsp view_close(view);
1115 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1116 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1117 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1119 e5a0f69f 2018-08-18 stsp return err;
1122 4ed7e80c 2018-05-20 stsp __dead static void
1123 9f7d7167 2018-04-29 stsp usage_log(void)
1126 c70c5802 2018-08-01 stsp fprintf(stderr,
1127 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1128 9f7d7167 2018-04-29 stsp getprogname());
1132 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1133 80ddbec8 2018-04-29 stsp static const struct got_error *
1134 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1136 00dfcb92 2018-06-11 stsp char *vis = NULL;
1137 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1139 963b370f 2018-05-20 stsp *ws = NULL;
1140 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1141 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1142 00dfcb92 2018-06-11 stsp int vislen;
1143 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1144 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1146 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1147 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1149 00dfcb92 2018-06-11 stsp return err;
1150 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1151 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1152 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1153 a7f50699 2018-06-11 stsp goto done;
1157 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1158 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1159 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1160 a7f50699 2018-06-11 stsp goto done;
1163 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1164 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1166 00dfcb92 2018-06-11 stsp free(vis);
1167 963b370f 2018-05-20 stsp if (err) {
1168 963b370f 2018-05-20 stsp free(*ws);
1169 963b370f 2018-05-20 stsp *ws = NULL;
1170 963b370f 2018-05-20 stsp *wlen = 0;
1172 963b370f 2018-05-20 stsp return err;
1175 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1176 963b370f 2018-05-20 stsp static const struct got_error *
1177 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1178 27a741e5 2019-09-11 stsp int col_tab_align)
1180 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1181 963b370f 2018-05-20 stsp int cols = 0;
1182 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1183 963b370f 2018-05-20 stsp size_t wlen;
1186 963b370f 2018-05-20 stsp *wlinep = NULL;
1187 b700b5d6 2018-07-10 stsp *widthp = 0;
1189 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1191 963b370f 2018-05-20 stsp return err;
1193 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1194 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1197 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1198 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1203 27a741e5 2019-09-11 stsp while (i < wlen) {
1204 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1206 27a741e5 2019-09-11 stsp if (width == 0) {
1211 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1212 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1214 27a741e5 2019-09-11 stsp cols += width;
1216 27a741e5 2019-09-11 stsp } else if (width == -1) {
1217 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1218 27a741e5 2019-09-11 stsp width = TABSIZE -
1219 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1221 748d5cab 2020-12-14 naddy width = 1;
1222 748d5cab 2020-12-14 naddy wline[i] = L'.';
1224 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1226 748d5cab 2020-12-14 naddy cols += width;
1229 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1230 963b370f 2018-05-20 stsp goto done;
1233 963b370f 2018-05-20 stsp wline[i] = L'\0';
1234 b700b5d6 2018-07-10 stsp if (widthp)
1235 b700b5d6 2018-07-10 stsp *widthp = cols;
1238 963b370f 2018-05-20 stsp free(wline);
1240 963b370f 2018-05-20 stsp *wlinep = wline;
1241 963b370f 2018-05-20 stsp return err;
1244 8b473291 2019-02-21 stsp static const struct got_error*
1245 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1246 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1248 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1249 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1251 8b473291 2019-02-21 stsp const char *name;
1253 8b473291 2019-02-21 stsp *refs_str = NULL;
1255 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1256 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1257 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1260 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1261 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1263 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1264 8b473291 2019-02-21 stsp name += 5;
1265 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1267 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1268 8b473291 2019-02-21 stsp name += 6;
1269 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1270 8b473291 2019-02-21 stsp name += 8;
1271 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1272 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1275 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1278 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1279 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1280 5d844a1e 2019-08-13 stsp if (err) {
1281 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1282 48cae60d 2020-09-22 stsp free(ref_id);
1285 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1286 5d844a1e 2019-08-13 stsp err = NULL;
1287 5d844a1e 2019-08-13 stsp tag = NULL;
1290 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1291 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1292 48cae60d 2020-09-22 stsp free(ref_id);
1294 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1295 52b5abe1 2019-08-13 stsp if (cmp != 0)
1297 8b473291 2019-02-21 stsp s = *refs_str;
1298 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1299 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1302 8b473291 2019-02-21 stsp *refs_str = NULL;
1308 8b473291 2019-02-21 stsp return err;
1311 963b370f 2018-05-20 stsp static const struct got_error *
1312 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1313 27a741e5 2019-09-11 stsp int col_tab_align)
1315 e6b8b890 2020-12-29 naddy char *smallerthan;
1317 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1318 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1319 5813d178 2019-03-09 stsp author = smallerthan + 1;
1320 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1321 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1324 5813d178 2019-03-09 stsp static const struct got_error *
1325 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1326 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1327 8fdc79fe 2020-12-01 naddy int author_display_cols)
1329 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1330 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1331 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1332 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1333 5813d178 2019-03-09 stsp char *author = NULL;
1334 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1335 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1336 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1337 bb737323 2018-05-20 stsp int col, limit;
1338 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1339 ccb26ccd 2018-11-05 stsp struct tm tm;
1340 45d799e2 2018-12-23 stsp time_t committer_time;
1341 11b20872 2019-11-08 stsp struct tog_color *tc;
1343 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1344 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1345 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1346 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1347 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1349 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1350 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1352 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1353 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1355 11b20872 2019-11-08 stsp wattr_on(view->window,
1356 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1357 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1359 11b20872 2019-11-08 stsp wattr_off(view->window,
1360 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1361 27a741e5 2019-09-11 stsp col = limit;
1362 b39d25c7 2018-07-10 stsp if (col > avail)
1363 b39d25c7 2018-07-10 stsp goto done;
1365 6570a66d 2019-11-08 stsp if (avail >= 120) {
1366 6570a66d 2019-11-08 stsp char *id_str;
1367 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1369 6570a66d 2019-11-08 stsp goto done;
1370 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1372 11b20872 2019-11-08 stsp wattr_on(view->window,
1373 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1374 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1376 11b20872 2019-11-08 stsp wattr_off(view->window,
1377 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1378 6570a66d 2019-11-08 stsp free(id_str);
1380 6570a66d 2019-11-08 stsp if (col > avail)
1381 6570a66d 2019-11-08 stsp goto done;
1384 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1385 5813d178 2019-03-09 stsp if (author == NULL) {
1386 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1387 80ddbec8 2018-04-29 stsp goto done;
1389 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1391 bb737323 2018-05-20 stsp goto done;
1392 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1394 11b20872 2019-11-08 stsp wattr_on(view->window,
1395 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1396 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1398 11b20872 2019-11-08 stsp wattr_off(view->window,
1399 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1400 bb737323 2018-05-20 stsp col += author_width;
1401 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1402 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1404 bb737323 2018-05-20 stsp author_width++;
1406 9c2eaf34 2018-05-20 stsp if (col > avail)
1407 9c2eaf34 2018-05-20 stsp goto done;
1409 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1411 6d9fbc00 2018-04-29 stsp goto done;
1412 bb737323 2018-05-20 stsp logmsg = logmsg0;
1413 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1415 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1416 bb737323 2018-05-20 stsp if (newline)
1417 bb737323 2018-05-20 stsp *newline = '\0';
1418 bb737323 2018-05-20 stsp limit = avail - col;
1419 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1421 bb737323 2018-05-20 stsp goto done;
1422 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1423 bb737323 2018-05-20 stsp col += logmsg_width;
1424 27a741e5 2019-09-11 stsp while (col < avail) {
1425 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1429 80ddbec8 2018-04-29 stsp free(logmsg0);
1430 bb737323 2018-05-20 stsp free(wlogmsg);
1431 5813d178 2019-03-09 stsp free(author);
1432 bb737323 2018-05-20 stsp free(wauthor);
1433 80ddbec8 2018-04-29 stsp free(line);
1434 80ddbec8 2018-04-29 stsp return err;
1437 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1438 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1439 899d86c2 2018-05-10 stsp struct got_object_id *id)
1441 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1443 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1444 80ddbec8 2018-04-29 stsp if (entry == NULL)
1445 899d86c2 2018-05-10 stsp return NULL;
1447 899d86c2 2018-05-10 stsp entry->id = id;
1448 99db9666 2018-05-07 stsp entry->commit = commit;
1449 899d86c2 2018-05-10 stsp return entry;
1452 99db9666 2018-05-07 stsp static void
1453 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1455 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1457 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1458 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1459 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1460 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1461 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1462 99db9666 2018-05-07 stsp free(entry);
1465 99db9666 2018-05-07 stsp static void
1466 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1468 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1469 99db9666 2018-05-07 stsp pop_commit(commits);
1472 c4972b91 2018-05-07 stsp static const struct got_error *
1473 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1474 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1476 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1477 13add988 2019-10-15 stsp regmatch_t regmatch;
1478 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1480 13add988 2019-10-15 stsp *have_match = 0;
1482 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1484 13add988 2019-10-15 stsp return err;
1486 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1488 13add988 2019-10-15 stsp goto done;
1490 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1491 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1492 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1493 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1494 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1495 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1496 13add988 2019-10-15 stsp *have_match = 1;
1498 13add988 2019-10-15 stsp free(id_str);
1499 13add988 2019-10-15 stsp free(logmsg);
1500 13add988 2019-10-15 stsp return err;
1503 13add988 2019-10-15 stsp static const struct got_error *
1504 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1506 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1509 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1510 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1511 1a76625f 2018-10-22 stsp * while updating the display.
1514 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1515 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1516 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1517 1a76625f 2018-10-22 stsp int errcode;
1519 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1520 4e0d2870 2020-12-07 naddy NULL, NULL);
1521 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1524 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1527 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1528 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1529 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1533 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1534 1a76625f 2018-10-22 stsp if (errcode) {
1535 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1536 13add988 2019-10-15 stsp "pthread_mutex_lock");
1540 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1541 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1542 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1544 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1545 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1546 7c1452c1 2020-03-26 stsp int have_match;
1547 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1550 7c1452c1 2020-03-26 stsp if (have_match)
1551 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1554 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1555 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1556 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1557 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1560 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1562 9ba79e04 2018-06-11 stsp return err;
1565 2b779855 2020-12-05 naddy static void
1566 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1568 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1569 2b779855 2020-12-05 naddy int ncommits = 0;
1571 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1572 2b779855 2020-12-05 naddy while (entry) {
1573 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1574 2b779855 2020-12-05 naddy s->selected_entry = entry;
1577 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1578 2b779855 2020-12-05 naddy ncommits++;
1582 0553a4e3 2018-04-30 stsp static const struct got_error *
1583 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1585 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1586 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1587 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1588 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1589 60493ae3 2019-06-20 stsp int width;
1590 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1591 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1592 8b473291 2019-02-21 stsp char *refs_str = NULL;
1593 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1594 11b20872 2019-11-08 stsp struct tog_color *tc;
1595 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1597 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1598 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1599 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1600 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1602 ecb28ae0 2018-07-16 stsp return err;
1603 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1604 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1605 d2075bf3 2020-12-25 stsp if (refs) {
1606 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1607 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1609 d2075bf3 2020-12-25 stsp goto done;
1613 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1614 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1616 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1617 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1618 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1619 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1620 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1621 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1622 8f4ed634 2020-03-26 stsp goto done;
1625 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1627 f9686aa5 2020-03-27 stsp if (view->searching) {
1628 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1629 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1630 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1631 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1632 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1633 f9686aa5 2020-03-27 stsp search_str = "searching...";
1636 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1637 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1638 f9686aa5 2020-03-27 stsp search_str ? search_str :
1639 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1640 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1641 8f4ed634 2020-03-26 stsp goto done;
1645 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1646 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1647 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1648 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1649 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1650 1a76625f 2018-10-22 stsp header = NULL;
1651 1a76625f 2018-10-22 stsp goto done;
1653 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1654 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1655 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1656 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1657 1a76625f 2018-10-22 stsp header = NULL;
1658 1a76625f 2018-10-22 stsp goto done;
1660 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1662 1a76625f 2018-10-22 stsp goto done;
1664 2814baeb 2018-08-01 stsp werase(view->window);
1666 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1667 a3404814 2018-09-02 stsp wstandout(view->window);
1668 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1670 11b20872 2019-11-08 stsp wattr_on(view->window,
1671 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1672 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1674 11b20872 2019-11-08 stsp wattr_off(view->window,
1675 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1676 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1677 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1680 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1681 a3404814 2018-09-02 stsp wstandend(view->window);
1682 ecb28ae0 2018-07-16 stsp free(wline);
1683 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1684 1a76625f 2018-10-22 stsp goto done;
1686 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1687 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1688 5813d178 2019-03-09 stsp ncommits = 0;
1689 5813d178 2019-03-09 stsp while (entry) {
1690 5813d178 2019-03-09 stsp char *author;
1691 5813d178 2019-03-09 stsp wchar_t *wauthor;
1692 5813d178 2019-03-09 stsp int width;
1693 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1695 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1696 5813d178 2019-03-09 stsp if (author == NULL) {
1697 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1698 5813d178 2019-03-09 stsp goto done;
1700 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1701 27a741e5 2019-09-11 stsp date_display_cols);
1702 5813d178 2019-03-09 stsp if (author_cols < width)
1703 5813d178 2019-03-09 stsp author_cols = width;
1704 5813d178 2019-03-09 stsp free(wauthor);
1705 5813d178 2019-03-09 stsp free(author);
1706 7ca04879 2019-10-19 stsp ncommits++;
1707 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1710 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1711 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1712 867c6645 2018-07-10 stsp ncommits = 0;
1713 899d86c2 2018-05-10 stsp while (entry) {
1714 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1716 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1717 2814baeb 2018-08-01 stsp wstandout(view->window);
1718 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1719 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1720 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1721 2814baeb 2018-08-01 stsp wstandend(view->window);
1723 60493ae3 2019-06-20 stsp goto done;
1724 0553a4e3 2018-04-30 stsp ncommits++;
1725 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1726 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1729 1a57306a 2018-09-02 stsp view_vborder(view);
1731 1a76625f 2018-10-22 stsp free(id_str);
1732 8b473291 2019-02-21 stsp free(refs_str);
1733 1a76625f 2018-10-22 stsp free(ncommits_str);
1734 1a76625f 2018-10-22 stsp free(header);
1735 80ddbec8 2018-04-29 stsp return err;
1738 07b55e75 2018-05-10 stsp static void
1739 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1741 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1742 07b55e75 2018-05-10 stsp int nscrolled = 0;
1744 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1745 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1748 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1749 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1750 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1751 07b55e75 2018-05-10 stsp if (entry) {
1752 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1753 07b55e75 2018-05-10 stsp nscrolled++;
1758 aa075928 2018-05-10 stsp static const struct got_error *
1759 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1761 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1762 5e224a3e 2019-02-22 stsp int errcode;
1764 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1766 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1767 ffe38506 2020-12-01 naddy if (ta->log_complete)
1770 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1771 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1772 7aafa0d1 2019-02-22 stsp if (errcode)
1773 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1774 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1777 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1778 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1780 7c1452c1 2020-03-26 stsp if (!wait)
1783 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1784 ffe38506 2020-12-01 naddy show_log_view(view);
1785 7c1452c1 2020-03-26 stsp update_panels();
1786 7c1452c1 2020-03-26 stsp doupdate();
1788 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1789 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1790 82954512 2020-02-03 stsp if (errcode)
1791 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1792 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1794 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1795 ffe38506 2020-12-01 naddy show_log_view(view);
1796 7c1452c1 2020-03-26 stsp update_panels();
1797 7c1452c1 2020-03-26 stsp doupdate();
1800 5e224a3e 2019-02-22 stsp return NULL;
1803 5e224a3e 2019-02-22 stsp static const struct got_error *
1804 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1806 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1807 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1808 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1809 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1811 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1812 5e224a3e 2019-02-22 stsp return NULL;
1814 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1815 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1816 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1818 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1820 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1821 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1823 5e224a3e 2019-02-22 stsp return err;
1827 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1828 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1831 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1833 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1834 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1836 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1837 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1839 dd0a52c1 2018-05-20 stsp return err;
1842 cd0acaa7 2018-05-20 stsp static const struct got_error *
1843 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1844 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1845 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1847 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1848 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1849 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1851 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1852 15a94983 2018-12-23 stsp if (diff_view == NULL)
1853 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1855 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1856 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1857 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1858 e5a0f69f 2018-08-18 stsp if (err == NULL)
1859 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1860 cd0acaa7 2018-05-20 stsp return err;
1863 80ddbec8 2018-04-29 stsp static const struct got_error *
1864 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1865 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1867 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1869 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1870 941e9f74 2019-05-21 stsp if (parent == NULL)
1871 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1873 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1874 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1875 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1876 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1877 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1878 941e9f74 2019-05-21 stsp s->tree = subtree;
1879 941e9f74 2019-05-21 stsp s->selected = 0;
1880 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1881 941e9f74 2019-05-21 stsp return NULL;
1884 941e9f74 2019-05-21 stsp static const struct got_error *
1885 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1886 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1888 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1889 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1890 941e9f74 2019-05-21 stsp const char *p;
1891 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1893 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1895 941e9f74 2019-05-21 stsp while (*p) {
1896 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1897 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1898 56e0773d 2019-11-28 stsp char *te_name;
1900 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1903 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1904 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1905 941e9f74 2019-05-21 stsp if (slash == NULL)
1906 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1908 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1909 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1910 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1913 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1914 56e0773d 2019-11-28 stsp if (te == NULL) {
1915 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1916 56e0773d 2019-11-28 stsp free(te_name);
1919 56e0773d 2019-11-28 stsp free(te_name);
1920 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1922 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1923 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1925 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1926 941e9f74 2019-05-21 stsp if (slash)
1927 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1929 941e9f74 2019-05-21 stsp subpath = strdup(path);
1930 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1931 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1935 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1940 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1941 941e9f74 2019-05-21 stsp free(tree_id);
1945 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1946 941e9f74 2019-05-21 stsp if (err) {
1947 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1950 941e9f74 2019-05-21 stsp if (slash == NULL)
1952 941e9f74 2019-05-21 stsp free(subpath);
1953 941e9f74 2019-05-21 stsp subpath = NULL;
1954 941e9f74 2019-05-21 stsp p = slash;
1957 941e9f74 2019-05-21 stsp free(subpath);
1958 1a76625f 2018-10-22 stsp return err;
1961 61266923 2020-01-14 stsp static const struct got_error *
1962 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1963 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1964 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
1966 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1967 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1968 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1970 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1971 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1972 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1974 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1976 55cccc34 2020-02-20 stsp return err;
1977 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1979 55cccc34 2020-02-20 stsp *new_view = tree_view;
1981 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1982 55cccc34 2020-02-20 stsp return NULL;
1984 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1987 55cccc34 2020-02-20 stsp static const struct got_error *
1988 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1990 61266923 2020-01-14 stsp sigset_t sigset;
1991 61266923 2020-01-14 stsp int errcode;
1993 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1994 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1996 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1997 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1998 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1999 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2000 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2002 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2003 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2004 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2006 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2007 61266923 2020-01-14 stsp if (errcode)
2008 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2010 61266923 2020-01-14 stsp return NULL;
2013 1a76625f 2018-10-22 stsp static void *
2014 1a76625f 2018-10-22 stsp log_thread(void *arg)
2016 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2017 1a76625f 2018-10-22 stsp int errcode = 0;
2018 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2019 1a76625f 2018-10-22 stsp int done = 0;
2021 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2023 61266923 2020-01-14 stsp return (void *)err;
2025 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2026 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2027 1a76625f 2018-10-22 stsp if (err) {
2028 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2029 1a76625f 2018-10-22 stsp return (void *)err;
2030 1a76625f 2018-10-22 stsp err = NULL;
2032 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2033 1a76625f 2018-10-22 stsp a->commits_needed--;
2035 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2036 3abe8080 2019-04-10 stsp if (errcode) {
2037 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2038 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2040 3abe8080 2019-04-10 stsp } else if (*a->quit)
2042 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2043 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2044 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2045 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2048 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2049 7c1452c1 2020-03-26 stsp if (errcode) {
2050 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2051 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2052 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2057 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2059 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2060 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2061 7c1452c1 2020-03-26 stsp &tog_mutex);
2062 7c1452c1 2020-03-26 stsp if (errcode)
2063 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2064 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2065 21355643 2020-12-06 stsp if (*a->quit)
2070 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2071 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2072 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2073 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2075 3abe8080 2019-04-10 stsp a->log_complete = 1;
2076 1a76625f 2018-10-22 stsp return (void *)err;
2079 1a76625f 2018-10-22 stsp static const struct got_error *
2080 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2082 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2083 1a76625f 2018-10-22 stsp int errcode;
2085 1a76625f 2018-10-22 stsp if (s->thread) {
2086 1a76625f 2018-10-22 stsp s->quit = 1;
2087 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2088 1a76625f 2018-10-22 stsp if (errcode)
2089 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2090 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2091 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2092 1a76625f 2018-10-22 stsp if (errcode)
2093 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2094 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2095 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2096 1a76625f 2018-10-22 stsp if (errcode)
2097 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2098 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2099 1a76625f 2018-10-22 stsp if (errcode)
2100 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2101 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2102 1a76625f 2018-10-22 stsp s->thread = NULL;
2105 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2106 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2107 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2110 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2111 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2112 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2115 9343a5fb 2018-06-23 stsp return err;
2118 9343a5fb 2018-06-23 stsp static const struct got_error *
2119 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2121 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2122 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2123 276b94a1 2020-11-13 naddy int errcode;
2125 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2127 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2128 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2129 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2131 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2132 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2133 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2135 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2136 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2137 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2138 1a76625f 2018-10-22 stsp free(s->start_id);
2139 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2140 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2141 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2142 1a76625f 2018-10-22 stsp return err;
2145 1a76625f 2018-10-22 stsp static const struct got_error *
2146 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2148 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2150 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2151 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2152 60493ae3 2019-06-20 stsp return NULL;
2155 60493ae3 2019-06-20 stsp static const struct got_error *
2156 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2158 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2159 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2160 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2162 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2163 f9686aa5 2020-03-27 stsp show_log_view(view);
2164 f9686aa5 2020-03-27 stsp update_panels();
2165 f9686aa5 2020-03-27 stsp doupdate();
2167 96e2b566 2019-07-08 stsp if (s->search_entry) {
2168 13add988 2019-10-15 stsp int errcode, ch;
2169 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2170 13add988 2019-10-15 stsp if (errcode)
2171 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2172 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2173 13add988 2019-10-15 stsp ch = wgetch(view->window);
2174 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2175 13add988 2019-10-15 stsp if (errcode)
2176 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2177 13add988 2019-10-15 stsp "pthread_mutex_lock");
2178 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2179 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2180 678cbce5 2019-07-28 stsp return NULL;
2182 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2183 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2185 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2186 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2187 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2188 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2189 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2191 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2192 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2194 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2195 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2197 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2200 60493ae3 2019-06-20 stsp while (1) {
2201 13add988 2019-10-15 stsp int have_match = 0;
2203 60493ae3 2019-06-20 stsp if (entry == NULL) {
2204 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2205 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2206 f9967bca 2020-03-27 stsp view->search_next_done =
2207 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2208 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2209 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2210 f801134a 2019-06-25 stsp return NULL;
2213 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2214 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2215 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2217 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2218 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2221 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2222 13add988 2019-10-15 stsp &view->regex);
2225 13add988 2019-10-15 stsp if (have_match) {
2226 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2227 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2231 96e2b566 2019-07-08 stsp s->search_entry = entry;
2232 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2233 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2235 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2238 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2239 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2240 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2241 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2243 60493ae3 2019-06-20 stsp return err;
2246 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2247 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2249 60493ae3 2019-06-20 stsp return err;
2254 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2256 60493ae3 2019-06-20 stsp return NULL;
2259 60493ae3 2019-06-20 stsp static const struct got_error *
2260 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2261 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2262 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2264 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2265 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2266 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2267 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2268 1a76625f 2018-10-22 stsp int errcode;
2270 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2271 f135c941 2020-02-20 stsp free(s->in_repo_path);
2272 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2273 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2274 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2277 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2278 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2279 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2281 fb2756b9 2018-08-04 stsp s->repo = repo;
2282 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2283 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2284 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2285 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2286 9cd7cbd1 2020-12-07 stsp goto done;
2289 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2290 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2291 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2292 5036bf37 2018-09-24 stsp goto done;
2294 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2296 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2297 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2298 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2299 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2301 11b20872 2019-11-08 stsp goto done;
2302 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2303 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2304 11b20872 2019-11-08 stsp if (err) {
2305 11b20872 2019-11-08 stsp free_colors(&s->colors);
2306 11b20872 2019-11-08 stsp goto done;
2308 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2309 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2310 11b20872 2019-11-08 stsp if (err) {
2311 11b20872 2019-11-08 stsp free_colors(&s->colors);
2312 11b20872 2019-11-08 stsp goto done;
2316 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2317 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2318 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2319 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2320 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2322 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2324 1a76625f 2018-10-22 stsp goto done;
2325 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2326 b672a97a 2020-01-27 stsp !s->log_branches);
2328 1a76625f 2018-10-22 stsp goto done;
2329 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2330 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2332 c5b78334 2020-01-12 stsp goto done;
2334 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2335 1a76625f 2018-10-22 stsp if (errcode) {
2336 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2337 1a76625f 2018-10-22 stsp goto done;
2339 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2340 7c1452c1 2020-03-26 stsp if (errcode) {
2341 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2342 7c1452c1 2020-03-26 stsp goto done;
2345 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2346 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2347 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2348 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2349 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2350 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2351 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2352 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2353 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2354 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2355 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2356 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2357 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2360 1a76625f 2018-10-22 stsp close_log_view(view);
2361 ba4f502b 2018-08-04 stsp return err;
2364 e5a0f69f 2018-08-18 stsp static const struct got_error *
2365 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2367 f2f6d207 2020-11-24 stsp const struct got_error *err;
2368 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2370 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2371 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2372 2b380cc8 2018-10-24 stsp &s->thread_args);
2373 2b380cc8 2018-10-24 stsp if (errcode)
2374 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2375 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2376 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2378 f2f6d207 2020-11-24 stsp return err;
2382 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2385 e5a0f69f 2018-08-18 stsp static const struct got_error *
2386 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2388 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2389 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2390 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2391 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2392 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2393 f3bc9f1d 2021-09-05 naddy int begin_x = 0, n;
2395 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2396 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2397 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2398 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2399 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2400 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2401 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2402 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2403 fb280deb 2021-08-30 stsp select_commit(s);
2405 528dedf3 2021-08-30 stsp return NULL;
2408 528dedf3 2021-08-30 stsp switch (ch) {
2410 1e37a5c2 2019-05-12 jcs s->quit = 1;
2413 1e37a5c2 2019-05-12 jcs case KEY_UP:
2416 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2417 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2419 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2420 1e37a5c2 2019-05-12 jcs s->selected--;
2422 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2423 912a3f79 2021-08-30 j select_commit(s);
2426 912a3f79 2021-08-30 j case KEY_HOME:
2427 912a3f79 2021-08-30 j s->selected = 0;
2428 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2429 2b779855 2020-12-05 naddy select_commit(s);
2431 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2432 a4292ac5 2019-05-12 jcs case CTRL('b'):
2433 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2435 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2436 1e37a5c2 2019-05-12 jcs s->selected = 0;
2438 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2439 2b779855 2020-12-05 naddy select_commit(s);
2442 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2445 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2446 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2448 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2449 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2450 1e37a5c2 2019-05-12 jcs s->selected++;
2452 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2456 912a3f79 2021-08-30 j select_commit(s);
2459 912a3f79 2021-08-30 j case KEY_END: {
2460 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2461 912a3f79 2021-08-30 j * traverse them all. */
2462 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2463 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2464 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2467 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2468 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2469 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2470 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2472 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2473 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2475 f3bc9f1d 2021-09-05 naddy if (n > 0)
2476 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2477 2b779855 2020-12-05 naddy select_commit(s);
2480 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2481 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2482 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2483 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2484 1e37a5c2 2019-05-12 jcs if (first == NULL)
2486 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2489 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2490 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2491 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2492 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2493 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2494 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2496 2b779855 2020-12-05 naddy select_commit(s);
2499 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2500 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2501 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2502 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2503 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2504 2b779855 2020-12-05 naddy select_commit(s);
2505 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2506 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2507 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2508 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2509 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2512 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2515 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2517 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2518 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2519 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2520 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2521 78756c87 2020-11-24 stsp view, s->repo);
2524 e78dc838 2020-12-04 stsp view->focussed = 0;
2525 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2526 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2527 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2529 1e37a5c2 2019-05-12 jcs return err;
2530 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2531 e78dc838 2020-12-04 stsp view->focus_child = 1;
2533 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2536 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2538 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2539 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2540 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2541 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2545 e78dc838 2020-12-04 stsp view->focussed = 0;
2546 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2547 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2548 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2550 1e37a5c2 2019-05-12 jcs return err;
2551 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2552 e78dc838 2020-12-04 stsp view->focus_child = 1;
2554 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2556 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2557 21355643 2020-12-06 stsp case CTRL('l'):
2559 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2560 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2562 21355643 2020-12-06 stsp err = stop_log_thread(s);
2564 74cfe85e 2020-10-20 stsp return err;
2565 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2566 21355643 2020-12-06 stsp char *parent_path;
2567 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2569 21355643 2020-12-06 stsp return err;
2570 21355643 2020-12-06 stsp free(s->in_repo_path);
2571 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2572 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2573 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2574 21355643 2020-12-06 stsp struct got_object_id *start_id;
2575 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2576 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2577 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2579 21355643 2020-12-06 stsp return err;
2580 21355643 2020-12-06 stsp free(s->start_id);
2581 21355643 2020-12-06 stsp s->start_id = start_id;
2582 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2583 21355643 2020-12-06 stsp } else /* 'B' */
2584 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2586 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2587 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2589 21355643 2020-12-06 stsp return err;
2590 51a10b52 2020-12-26 stsp tog_free_refs();
2591 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
2593 ca51c541 2020-12-07 stsp return err;
2594 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2595 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2597 d01904d4 2019-06-25 stsp return err;
2598 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2599 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2601 b672a97a 2020-01-27 stsp return err;
2602 21355643 2020-12-06 stsp free_commits(&s->commits);
2603 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2604 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2605 21355643 2020-12-06 stsp s->selected_entry = NULL;
2606 21355643 2020-12-06 stsp s->selected = 0;
2607 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2608 21355643 2020-12-06 stsp s->quit = 0;
2609 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2612 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2613 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2614 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2615 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2616 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2617 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2618 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2619 6458efa5 2020-11-24 stsp if (err) {
2620 6458efa5 2020-11-24 stsp view_close(ref_view);
2621 6458efa5 2020-11-24 stsp return err;
2623 e78dc838 2020-12-04 stsp view->focussed = 0;
2624 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2625 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2626 6458efa5 2020-11-24 stsp err = view_close_child(view);
2628 6458efa5 2020-11-24 stsp return err;
2629 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2630 e78dc838 2020-12-04 stsp view->focus_child = 1;
2632 6458efa5 2020-11-24 stsp *new_view = ref_view;
2638 80ddbec8 2018-04-29 stsp return err;
2641 4ed7e80c 2018-05-20 stsp static const struct got_error *
2642 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2644 c2db6724 2019-01-04 stsp const struct got_error *error;
2646 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2647 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2648 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2650 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2651 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2653 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2654 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2656 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2657 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2659 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2660 c2db6724 2019-01-04 stsp if (error != NULL)
2661 c2db6724 2019-01-04 stsp return error;
2663 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2664 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2666 c2db6724 2019-01-04 stsp return NULL;
2669 a915003a 2019-02-05 stsp static void
2670 a915003a 2019-02-05 stsp init_curses(void)
2672 a915003a 2019-02-05 stsp initscr();
2674 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2677 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2678 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2679 a915003a 2019-02-05 stsp curs_set(0);
2680 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2681 6d17833f 2019-11-08 stsp start_color();
2682 6d17833f 2019-11-08 stsp use_default_colors();
2684 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2685 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2686 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2689 c2db6724 2019-01-04 stsp static const struct got_error *
2690 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2691 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2693 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2695 f135c941 2020-02-20 stsp if (argc == 0) {
2696 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2697 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2698 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2699 f135c941 2020-02-20 stsp return NULL;
2702 f135c941 2020-02-20 stsp if (worktree) {
2703 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2706 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2708 f135c941 2020-02-20 stsp return err;
2709 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2710 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2711 bfd61697 2020-11-14 stsp p) == -1) {
2712 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2713 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2717 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2719 f135c941 2020-02-20 stsp return err;
2722 f135c941 2020-02-20 stsp static const struct got_error *
2723 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2725 80ddbec8 2018-04-29 stsp const struct got_error *error;
2726 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2727 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2728 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2729 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2730 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2731 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2732 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2733 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2734 04cc582a 2018-08-01 stsp struct tog_view *view;
2736 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2737 80ddbec8 2018-04-29 stsp switch (ch) {
2739 b672a97a 2020-01-27 stsp log_branches = 1;
2742 80ddbec8 2018-04-29 stsp start_commit = optarg;
2745 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2746 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2747 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2751 17020d27 2019-03-07 stsp usage_log();
2752 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2756 80ddbec8 2018-04-29 stsp argc -= optind;
2757 80ddbec8 2018-04-29 stsp argv += optind;
2759 f135c941 2020-02-20 stsp if (argc > 1)
2760 f135c941 2020-02-20 stsp usage_log();
2762 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2763 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2764 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2765 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2766 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2767 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2768 c156c7a4 2020-12-18 stsp goto done;
2769 a1fbf39a 2019-08-11 stsp if (worktree)
2770 6962eb72 2020-02-20 stsp repo_path =
2771 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2773 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2774 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2775 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2776 c156c7a4 2020-12-18 stsp goto done;
2780 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2781 80ddbec8 2018-04-29 stsp if (error != NULL)
2782 ecb28ae0 2018-07-16 stsp goto done;
2784 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2785 f135c941 2020-02-20 stsp repo, worktree);
2786 f135c941 2020-02-20 stsp if (error)
2787 f135c941 2020-02-20 stsp goto done;
2789 f135c941 2020-02-20 stsp init_curses();
2791 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2792 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2793 c02c541e 2019-03-29 stsp if (error)
2794 c02c541e 2019-03-29 stsp goto done;
2796 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2797 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2798 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
2799 87670572 2020-12-26 naddy if (error)
2800 87670572 2020-12-26 naddy goto done;
2803 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2804 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2805 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2806 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2807 d8f38dc4 2020-12-05 stsp if (error)
2808 d8f38dc4 2020-12-05 stsp goto done;
2809 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2811 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2812 d8f38dc4 2020-12-05 stsp if (error == NULL)
2813 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2814 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2815 d8f38dc4 2020-12-05 stsp goto done;
2816 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2817 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2818 d8f38dc4 2020-12-05 stsp if (error)
2819 d8f38dc4 2020-12-05 stsp goto done;
2822 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2823 04cc582a 2018-08-01 stsp if (view == NULL) {
2824 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2825 04cc582a 2018-08-01 stsp goto done;
2827 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2828 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2829 ba4f502b 2018-08-04 stsp if (error)
2830 ba4f502b 2018-08-04 stsp goto done;
2831 2fc00ff4 2019-08-31 stsp if (worktree) {
2832 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2833 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2834 2fc00ff4 2019-08-31 stsp worktree = NULL;
2836 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2838 f135c941 2020-02-20 stsp free(in_repo_path);
2839 ecb28ae0 2018-07-16 stsp free(repo_path);
2840 ecb28ae0 2018-07-16 stsp free(cwd);
2841 899d86c2 2018-05-10 stsp free(start_id);
2842 d8f38dc4 2020-12-05 stsp free(label);
2844 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2845 1d0f4054 2021-06-17 stsp if (repo) {
2846 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2847 1d0f4054 2021-06-17 stsp if (error == NULL)
2848 1d0f4054 2021-06-17 stsp error = close_err;
2850 ec142235 2019-03-07 stsp if (worktree)
2851 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2852 51a10b52 2020-12-26 stsp tog_free_refs();
2853 80ddbec8 2018-04-29 stsp return error;
2856 4ed7e80c 2018-05-20 stsp __dead static void
2857 9f7d7167 2018-04-29 stsp usage_diff(void)
2860 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2861 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2865 6d17833f 2019-11-08 stsp static int
2866 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2867 41605754 2020-11-12 stsp regmatch_t *regmatch)
2869 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2872 f26dddb7 2019-11-08 stsp struct tog_color *
2873 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2875 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2877 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2878 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2879 6d17833f 2019-11-08 stsp return tc;
2882 6d17833f 2019-11-08 stsp return NULL;
2885 4ed7e80c 2018-05-20 stsp static const struct got_error *
2886 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2887 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2889 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2890 41605754 2020-11-12 stsp wchar_t *wline;
2891 41605754 2020-11-12 stsp int width;
2894 41605754 2020-11-12 stsp *wtotal = 0;
2896 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2897 41605754 2020-11-12 stsp if (s == NULL)
2898 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2900 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2901 41605754 2020-11-12 stsp if (err) {
2903 41605754 2020-11-12 stsp return err;
2905 41605754 2020-11-12 stsp waddwstr(window, wline);
2906 41605754 2020-11-12 stsp free(wline);
2908 41605754 2020-11-12 stsp wlimit -= width;
2909 41605754 2020-11-12 stsp *wtotal += width;
2911 41605754 2020-11-12 stsp if (wlimit > 0) {
2912 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2913 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2914 41605754 2020-11-12 stsp if (s == NULL) {
2915 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2917 41605754 2020-11-12 stsp return err;
2919 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2920 41605754 2020-11-12 stsp if (err) {
2922 41605754 2020-11-12 stsp return err;
2924 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2925 41605754 2020-11-12 stsp waddwstr(window, wline);
2926 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2927 41605754 2020-11-12 stsp free(wline);
2929 41605754 2020-11-12 stsp wlimit -= width;
2930 41605754 2020-11-12 stsp *wtotal += width;
2933 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2934 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2935 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2937 41605754 2020-11-12 stsp return err;
2938 41605754 2020-11-12 stsp waddwstr(window, wline);
2939 41605754 2020-11-12 stsp free(wline);
2940 41605754 2020-11-12 stsp *wtotal += width;
2943 41605754 2020-11-12 stsp return NULL;
2946 41605754 2020-11-12 stsp static const struct got_error *
2947 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2949 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2950 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2951 61e69b96 2018-05-20 stsp const struct got_error *err;
2952 fe621944 2020-11-10 stsp int nprinted = 0;
2953 b304db33 2018-05-20 stsp char *line;
2954 826082fe 2020-12-10 stsp size_t linesize = 0;
2955 826082fe 2020-12-10 stsp ssize_t linelen;
2956 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2957 61e69b96 2018-05-20 stsp wchar_t *wline;
2958 e0b650dd 2018-05-20 stsp int width;
2959 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2960 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2961 fe621944 2020-11-10 stsp off_t line_offset;
2963 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2964 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2965 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2967 f7d12f7e 2018-08-01 stsp werase(view->window);
2969 a3404814 2018-09-02 stsp if (header) {
2970 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2971 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2972 135a2da0 2020-11-11 stsp header) == -1)
2973 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2974 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2975 135a2da0 2020-11-11 stsp free(line);
2977 a3404814 2018-09-02 stsp return err;
2979 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2980 a3404814 2018-09-02 stsp wstandout(view->window);
2981 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2982 e54cc94a 2020-11-11 stsp free(wline);
2983 e54cc94a 2020-11-11 stsp wline = NULL;
2984 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2985 a3404814 2018-09-02 stsp wstandend(view->window);
2986 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2987 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2989 a3404814 2018-09-02 stsp if (max_lines <= 1)
2990 a3404814 2018-09-02 stsp return NULL;
2991 a3404814 2018-09-02 stsp max_lines--;
2994 89f1a395 2020-12-01 naddy s->eof = 0;
2995 826082fe 2020-12-10 stsp line = NULL;
2996 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2997 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
2998 826082fe 2020-12-10 stsp if (linelen == -1) {
2999 826082fe 2020-12-10 stsp if (feof(s->f)) {
3000 826082fe 2020-12-10 stsp s->eof = 1;
3003 826082fe 2020-12-10 stsp free(line);
3004 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3007 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3009 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3010 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3011 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3012 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3013 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3014 41605754 2020-11-12 stsp view->window, regmatch);
3015 41605754 2020-11-12 stsp if (err) {
3016 41605754 2020-11-12 stsp free(line);
3017 41605754 2020-11-12 stsp return err;
3020 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3021 41605754 2020-11-12 stsp if (err) {
3022 41605754 2020-11-12 stsp free(line);
3023 41605754 2020-11-12 stsp return err;
3025 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3026 41605754 2020-11-12 stsp free(wline);
3027 41605754 2020-11-12 stsp wline = NULL;
3030 6d17833f 2019-11-08 stsp wattr_off(view->window,
3031 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3032 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3033 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3034 fe621944 2020-11-10 stsp nprinted++;
3036 826082fe 2020-12-10 stsp free(line);
3037 fe621944 2020-11-10 stsp if (nprinted >= 1)
3038 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3039 89f1a395 2020-12-01 naddy (nprinted - 1);
3041 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3043 1a57306a 2018-09-02 stsp view_vborder(view);
3045 89f1a395 2020-12-01 naddy if (s->eof) {
3046 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3047 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3048 c3e9aa98 2019-05-13 jcs nprinted++;
3051 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3053 c3e9aa98 2019-05-13 jcs return err;
3056 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3057 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3058 e54cc94a 2020-11-11 stsp free(wline);
3059 e54cc94a 2020-11-11 stsp wline = NULL;
3060 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3063 26ed57b2 2018-05-19 stsp return NULL;
3066 abd2672a 2018-12-23 stsp static char *
3067 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3069 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3070 09867e48 2019-08-13 stsp char *p, *s;
3072 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3073 09867e48 2019-08-13 stsp if (tm == NULL)
3074 09867e48 2019-08-13 stsp return NULL;
3075 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3076 09867e48 2019-08-13 stsp if (s == NULL)
3077 09867e48 2019-08-13 stsp return NULL;
3078 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3080 abd2672a 2018-12-23 stsp *p = '\0';
3084 4ed7e80c 2018-05-20 stsp static const struct got_error *
3085 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3086 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3088 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3089 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3090 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3091 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3093 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3094 0208f208 2020-05-05 stsp if (qid != NULL) {
3095 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3096 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3099 0208f208 2020-05-05 stsp return err;
3101 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3102 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3103 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3104 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3105 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3107 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3111 0208f208 2020-05-05 stsp if (tree_id1) {
3112 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3114 0208f208 2020-05-05 stsp goto done;
3117 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3118 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3120 0208f208 2020-05-05 stsp goto done;
3122 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3123 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3125 0208f208 2020-05-05 stsp if (tree1)
3126 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3127 0208f208 2020-05-05 stsp if (tree2)
3128 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3129 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3130 0208f208 2020-05-05 stsp return err;
3133 0208f208 2020-05-05 stsp static const struct got_error *
3134 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3138 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3139 fe621944 2020-11-10 stsp if (p == NULL)
3140 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3141 fe621944 2020-11-10 stsp *line_offsets = p;
3142 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3143 fe621944 2020-11-10 stsp (*nlines)++;
3144 fe621944 2020-11-10 stsp return NULL;
3147 fe621944 2020-11-10 stsp static const struct got_error *
3148 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3149 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3150 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3152 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3153 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3154 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3155 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3156 45d799e2 2018-12-23 stsp time_t committer_time;
3157 45d799e2 2018-12-23 stsp const char *author, *committer;
3158 8b473291 2019-02-21 stsp char *refs_str = NULL;
3159 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3160 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3161 fe621944 2020-11-10 stsp off_t outoff = 0;
3164 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3166 8b473291 2019-02-21 stsp if (refs) {
3167 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3169 8b473291 2019-02-21 stsp return err;
3172 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3174 abd2672a 2018-12-23 stsp return err;
3176 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3177 15a94983 2018-12-23 stsp if (err) {
3178 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3179 15a94983 2018-12-23 stsp goto done;
3182 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3184 fe621944 2020-11-10 stsp goto done;
3186 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3187 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3188 fe621944 2020-11-10 stsp if (n < 0) {
3189 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3190 abd2672a 2018-12-23 stsp goto done;
3192 fe621944 2020-11-10 stsp outoff += n;
3193 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3195 fe621944 2020-11-10 stsp goto done;
3197 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3198 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3199 fe621944 2020-11-10 stsp if (n < 0) {
3200 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3201 abd2672a 2018-12-23 stsp goto done;
3203 fe621944 2020-11-10 stsp outoff += n;
3204 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3206 fe621944 2020-11-10 stsp goto done;
3208 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3209 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3210 fe621944 2020-11-10 stsp if (datestr) {
3211 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3212 fe621944 2020-11-10 stsp if (n < 0) {
3213 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3214 fe621944 2020-11-10 stsp goto done;
3216 fe621944 2020-11-10 stsp outoff += n;
3217 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3219 fe621944 2020-11-10 stsp goto done;
3221 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3222 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3223 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3224 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3225 fe621944 2020-11-10 stsp if (n < 0) {
3226 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3227 fe621944 2020-11-10 stsp goto done;
3229 fe621944 2020-11-10 stsp outoff += n;
3230 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3232 fe621944 2020-11-10 stsp goto done;
3234 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3235 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3236 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3237 9f98ca05 2021-09-24 stsp int pn = 1;
3238 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3239 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3240 9f98ca05 2021-09-24 stsp err = got_object_id_str(&id_str, qid->id);
3242 9f98ca05 2021-09-24 stsp goto done;
3243 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3244 9f98ca05 2021-09-24 stsp if (n < 0) {
3245 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3246 9f98ca05 2021-09-24 stsp goto done;
3248 9f98ca05 2021-09-24 stsp outoff += n;
3249 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3251 9f98ca05 2021-09-24 stsp goto done;
3252 9f98ca05 2021-09-24 stsp free(id_str);
3253 9f98ca05 2021-09-24 stsp id_str = NULL;
3257 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3259 5943eee2 2019-08-13 stsp goto done;
3260 fe621944 2020-11-10 stsp s = logmsg;
3261 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3262 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3263 fe621944 2020-11-10 stsp if (n < 0) {
3264 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3265 fe621944 2020-11-10 stsp goto done;
3267 fe621944 2020-11-10 stsp outoff += n;
3268 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3270 fe621944 2020-11-10 stsp goto done;
3273 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3275 0208f208 2020-05-05 stsp goto done;
3276 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3277 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3278 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3279 fe621944 2020-11-10 stsp if (n < 0) {
3280 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3281 fe621944 2020-11-10 stsp goto done;
3283 fe621944 2020-11-10 stsp outoff += n;
3284 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3286 fe621944 2020-11-10 stsp goto done;
3287 0208f208 2020-05-05 stsp free((char *)pe->path);
3288 0208f208 2020-05-05 stsp free(pe->data);
3291 0208f208 2020-05-05 stsp fputc('\n', outfile);
3293 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3295 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3296 abd2672a 2018-12-23 stsp free(id_str);
3297 5943eee2 2019-08-13 stsp free(logmsg);
3298 8b473291 2019-02-21 stsp free(refs_str);
3299 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3300 7510f233 2020-08-09 stsp if (err) {
3301 ae6a6978 2020-08-09 stsp free(*line_offsets);
3302 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3303 ae6a6978 2020-08-09 stsp *nlines = 0;
3305 fe621944 2020-11-10 stsp return err;
3308 abd2672a 2018-12-23 stsp static const struct got_error *
3309 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3311 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3312 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3313 15a94983 2018-12-23 stsp int obj_type;
3315 fe621944 2020-11-10 stsp free(s->line_offsets);
3316 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3317 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3318 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3319 fe621944 2020-11-10 stsp s->nlines = 0;
3321 511a516b 2018-05-19 stsp f = got_opentemp();
3322 48ae06ee 2018-10-18 stsp if (f == NULL) {
3323 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3324 48ae06ee 2018-10-18 stsp goto done;
3326 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3327 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3328 fb43ecf1 2019-02-11 stsp goto done;
3332 15a94983 2018-12-23 stsp if (s->id1)
3333 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3335 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3337 15a94983 2018-12-23 stsp goto done;
3339 15a94983 2018-12-23 stsp switch (obj_type) {
3340 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3341 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3342 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3343 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3345 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3346 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3347 67b631c9 2021-10-10 stsp s->id1, s->id2, NULL, "", "", s->diff_context,
3348 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3350 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3351 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3352 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3353 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3354 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3356 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3358 3ffacbe1 2020-02-02 tracey goto done;
3359 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3360 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3361 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3362 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3363 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3364 f44b1f58 2020-02-02 tracey if (err)
3365 f44b1f58 2020-02-02 tracey goto done;
3366 f44b1f58 2020-02-02 tracey } else {
3367 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3368 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3369 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3370 fe621944 2020-11-10 stsp err = write_commit_info(
3371 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3372 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3373 f44b1f58 2020-02-02 tracey if (err)
3374 f44b1f58 2020-02-02 tracey goto done;
3379 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3381 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3382 67b631c9 2021-10-10 stsp s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3383 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3387 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3390 f44b1f58 2020-02-02 tracey if (err)
3391 f44b1f58 2020-02-02 tracey goto done;
3393 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3394 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3395 48ae06ee 2018-10-18 stsp return err;
3398 f5215bb9 2019-02-22 stsp static void
3399 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3401 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3402 f5215bb9 2019-02-22 stsp update_panels();
3403 f5215bb9 2019-02-22 stsp doupdate();
3406 f44b1f58 2020-02-02 tracey static const struct got_error *
3407 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3409 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3411 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3412 f44b1f58 2020-02-02 tracey return NULL;
3415 f44b1f58 2020-02-02 tracey static const struct got_error *
3416 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3418 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3419 f44b1f58 2020-02-02 tracey int lineno;
3420 826082fe 2020-12-10 stsp char *line = NULL;
3421 826082fe 2020-12-10 stsp size_t linesize = 0;
3422 826082fe 2020-12-10 stsp ssize_t linelen;
3424 f44b1f58 2020-02-02 tracey if (!view->searching) {
3425 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3426 f44b1f58 2020-02-02 tracey return NULL;
3429 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3430 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3431 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3433 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3434 f44b1f58 2020-02-02 tracey } else {
3435 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3436 f44b1f58 2020-02-02 tracey lineno = 1;
3438 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3441 f44b1f58 2020-02-02 tracey while (1) {
3442 f44b1f58 2020-02-02 tracey off_t offset;
3444 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3445 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3446 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3450 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3451 f44b1f58 2020-02-02 tracey lineno = 1;
3453 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3456 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3457 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3458 f44b1f58 2020-02-02 tracey free(line);
3459 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3461 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3462 826082fe 2020-12-10 stsp if (linelen != -1 &&
3463 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3464 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3465 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3468 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3469 f44b1f58 2020-02-02 tracey lineno++;
3471 f44b1f58 2020-02-02 tracey lineno--;
3473 826082fe 2020-12-10 stsp free(line);
3475 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3476 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3477 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3480 f44b1f58 2020-02-02 tracey return NULL;
3483 48ae06ee 2018-10-18 stsp static const struct got_error *
3484 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3485 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3486 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3487 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3489 48ae06ee 2018-10-18 stsp const struct got_error *err;
3490 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3492 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3493 15a94983 2018-12-23 stsp int type1, type2;
3494 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3496 15a94983 2018-12-23 stsp return err;
3497 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3499 15a94983 2018-12-23 stsp return err;
3501 15a94983 2018-12-23 stsp if (type1 != type2)
3502 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3504 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3505 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3506 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3507 f44b1f58 2020-02-02 tracey s->repo = repo;
3508 f44b1f58 2020-02-02 tracey s->id1 = id1;
3509 f44b1f58 2020-02-02 tracey s->id2 = id2;
3510 3dbaef42 2020-11-24 stsp s->label1 = label1;
3511 3dbaef42 2020-11-24 stsp s->label2 = label2;
3513 15a94983 2018-12-23 stsp if (id1) {
3514 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3515 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3516 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3518 5465d566 2020-02-01 tracey s->id1 = NULL;
3520 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3521 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3522 5465d566 2020-02-01 tracey free(s->id1);
3523 5465d566 2020-02-01 tracey s->id1 = NULL;
3524 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3526 5465d566 2020-02-01 tracey s->f = NULL;
3527 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3528 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3529 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3530 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3531 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3532 5465d566 2020-02-01 tracey s->log_view = log_view;
3533 5465d566 2020-02-01 tracey s->repo = repo;
3535 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3536 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3537 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3538 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3539 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3541 6d17833f 2019-11-08 stsp return err;
3542 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3543 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3544 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3545 6d17833f 2019-11-08 stsp if (err) {
3546 5465d566 2020-02-01 tracey free_colors(&s->colors);
3547 6d17833f 2019-11-08 stsp return err;
3549 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3550 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3551 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3552 6d17833f 2019-11-08 stsp if (err) {
3553 5465d566 2020-02-01 tracey free_colors(&s->colors);
3554 6d17833f 2019-11-08 stsp return err;
3557 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3558 9f98ca05 2021-09-24 stsp "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3559 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3560 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3561 6d17833f 2019-11-08 stsp if (err) {
3562 5465d566 2020-02-01 tracey free_colors(&s->colors);
3563 6d17833f 2019-11-08 stsp return err;
3566 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3567 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3568 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3569 11b20872 2019-11-08 stsp if (err) {
3570 5465d566 2020-02-01 tracey free_colors(&s->colors);
3571 11b20872 2019-11-08 stsp return err;
3574 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3575 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3576 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3577 11b20872 2019-11-08 stsp if (err) {
3578 5465d566 2020-02-01 tracey free_colors(&s->colors);
3579 11b20872 2019-11-08 stsp return err;
3583 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3584 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3585 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3587 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3588 fe621944 2020-11-10 stsp s->nlines = 0;
3589 5465d566 2020-02-01 tracey err = create_diff(s);
3590 48ae06ee 2018-10-18 stsp if (err) {
3591 5465d566 2020-02-01 tracey free(s->id1);
3592 5465d566 2020-02-01 tracey s->id1 = NULL;
3593 5465d566 2020-02-01 tracey free(s->id2);
3594 5465d566 2020-02-01 tracey s->id2 = NULL;
3595 78756c87 2020-11-24 stsp free_colors(&s->colors);
3596 48ae06ee 2018-10-18 stsp return err;
3599 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3600 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3601 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3602 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3603 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3605 5dc9f4bc 2018-08-04 stsp return NULL;
3608 e5a0f69f 2018-08-18 stsp static const struct got_error *
3609 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3611 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3612 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3614 5465d566 2020-02-01 tracey free(s->id1);
3615 5465d566 2020-02-01 tracey s->id1 = NULL;
3616 5465d566 2020-02-01 tracey free(s->id2);
3617 5465d566 2020-02-01 tracey s->id2 = NULL;
3618 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3619 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3620 5465d566 2020-02-01 tracey free_colors(&s->colors);
3621 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3622 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3623 fe621944 2020-11-10 stsp s->nlines = 0;
3624 e5a0f69f 2018-08-18 stsp return err;
3627 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3628 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3630 a3404814 2018-09-02 stsp const struct got_error *err;
3631 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3632 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3633 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3635 a3404814 2018-09-02 stsp if (s->id1) {
3636 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3638 a3404814 2018-09-02 stsp return err;
3639 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3641 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3643 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3645 a3404814 2018-09-02 stsp return err;
3646 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3648 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3649 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3650 a3404814 2018-09-02 stsp free(id_str1);
3651 a3404814 2018-09-02 stsp free(id_str2);
3652 a3404814 2018-09-02 stsp return err;
3654 a3404814 2018-09-02 stsp free(id_str1);
3655 a3404814 2018-09-02 stsp free(id_str2);
3657 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3658 267bb3b8 2021-08-01 stsp free(header);
3659 267bb3b8 2021-08-01 stsp return err;
3662 15a087fe 2019-02-21 stsp static const struct got_error *
3663 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3664 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3666 d7a04538 2019-02-21 stsp const struct got_error *err;
3667 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3668 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3669 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3671 15a087fe 2019-02-21 stsp free(s->id2);
3672 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3673 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3674 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3676 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3678 d7a04538 2019-02-21 stsp return err;
3679 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3680 15a087fe 2019-02-21 stsp free(s->id1);
3681 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3682 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3683 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3684 15a087fe 2019-02-21 stsp return NULL;
3687 0cf4efb1 2018-09-29 stsp static const struct got_error *
3688 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3690 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3691 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3692 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3693 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3694 826082fe 2020-12-10 stsp char *line = NULL;
3695 826082fe 2020-12-10 stsp size_t linesize = 0;
3696 826082fe 2020-12-10 stsp ssize_t linelen;
3699 e5a0f69f 2018-08-18 stsp switch (ch) {
3702 3dbaef42 2020-11-24 stsp if (ch == 'a')
3703 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3704 3dbaef42 2020-11-24 stsp if (ch == 'w')
3705 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3706 64453f7e 2020-11-21 stsp wclear(view->window);
3707 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3708 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3709 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3710 64453f7e 2020-11-21 stsp err = create_diff(s);
3713 912a3f79 2021-08-30 j case KEY_HOME:
3714 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3717 912a3f79 2021-08-30 j case KEY_END:
3721 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3725 1e37a5c2 2019-05-12 jcs case KEY_UP:
3726 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3727 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3728 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3730 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3731 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3732 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3735 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3736 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3737 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3740 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3741 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3742 1e37a5c2 2019-05-12 jcs if (!s->eof)
3743 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3745 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3746 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3748 00ba99a7 2019-05-12 jcs if (s->eof)
3751 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3752 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3753 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3754 826082fe 2020-12-10 stsp if (linelen == -1) {
3755 826082fe 2020-12-10 stsp if (feof(s->f)) {
3756 826082fe 2020-12-10 stsp s->eof = 1;
3758 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3762 826082fe 2020-12-10 stsp free(line);
3765 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3766 1e37a5c2 2019-05-12 jcs s->diff_context--;
3767 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3768 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3769 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3770 27829c9e 2020-11-21 stsp s->nlines) {
3771 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3772 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3777 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3778 1e37a5c2 2019-05-12 jcs s->diff_context++;
3779 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3780 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3785 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3787 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3788 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3790 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3794 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3797 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3801 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3802 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3804 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3805 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3809 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3811 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3812 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3814 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3818 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3821 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3825 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3826 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3828 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3829 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3835 bcbd79e2 2018-08-19 stsp return err;
3838 4ed7e80c 2018-05-20 stsp static const struct got_error *
3839 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3841 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3842 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3843 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3844 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3845 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3846 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3847 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3848 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3849 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3850 3dbaef42 2020-11-24 stsp const char *errstr;
3851 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3853 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3854 26ed57b2 2018-05-19 stsp switch (ch) {
3856 64453f7e 2020-11-21 stsp force_text_diff = 1;
3859 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3861 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3862 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3864 09b5bff8 2020-02-23 naddy case 'r':
3865 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3866 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3867 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3869 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3872 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3875 17020d27 2019-03-07 stsp usage_diff();
3876 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3880 26ed57b2 2018-05-19 stsp argc -= optind;
3881 26ed57b2 2018-05-19 stsp argv += optind;
3883 26ed57b2 2018-05-19 stsp if (argc == 0) {
3884 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3885 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3886 15a94983 2018-12-23 stsp id_str1 = argv[0];
3887 15a94983 2018-12-23 stsp id_str2 = argv[1];
3889 26ed57b2 2018-05-19 stsp usage_diff();
3891 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3892 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3893 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3894 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3895 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3896 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3897 c156c7a4 2020-12-18 stsp goto done;
3898 a273ac94 2020-02-23 naddy if (worktree)
3899 a273ac94 2020-02-23 naddy repo_path =
3900 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3902 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3903 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3904 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3905 c156c7a4 2020-12-18 stsp goto done;
3909 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3910 eb6600df 2019-01-04 stsp if (error)
3911 eb6600df 2019-01-04 stsp goto done;
3913 a273ac94 2020-02-23 naddy init_curses();
3915 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3916 51a10b52 2020-12-26 stsp if (error)
3917 51a10b52 2020-12-26 stsp goto done;
3919 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3920 26ed57b2 2018-05-19 stsp if (error)
3921 26ed57b2 2018-05-19 stsp goto done;
3923 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3924 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3925 26ed57b2 2018-05-19 stsp if (error)
3926 26ed57b2 2018-05-19 stsp goto done;
3928 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3929 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3930 26ed57b2 2018-05-19 stsp if (error)
3931 26ed57b2 2018-05-19 stsp goto done;
3933 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3934 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3935 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3936 ea5e7bb5 2018-08-01 stsp goto done;
3938 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3939 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3940 5dc9f4bc 2018-08-04 stsp if (error)
3941 5dc9f4bc 2018-08-04 stsp goto done;
3942 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3944 3dbaef42 2020-11-24 stsp free(label1);
3945 3dbaef42 2020-11-24 stsp free(label2);
3946 c02c541e 2019-03-29 stsp free(repo_path);
3947 a273ac94 2020-02-23 naddy free(cwd);
3948 1d0f4054 2021-06-17 stsp if (repo) {
3949 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3950 1d0f4054 2021-06-17 stsp if (error == NULL)
3951 1d0f4054 2021-06-17 stsp error = close_err;
3953 a273ac94 2020-02-23 naddy if (worktree)
3954 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3955 51a10b52 2020-12-26 stsp tog_free_refs();
3956 26ed57b2 2018-05-19 stsp return error;
3959 4ed7e80c 2018-05-20 stsp __dead static void
3960 9f7d7167 2018-04-29 stsp usage_blame(void)
3963 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3964 9f7d7167 2018-04-29 stsp getprogname());
3968 84451b3e 2018-07-10 stsp struct tog_blame_line {
3969 84451b3e 2018-07-10 stsp int annotated;
3970 84451b3e 2018-07-10 stsp struct got_object_id *id;
3973 4ed7e80c 2018-05-20 stsp static const struct got_error *
3974 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3976 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3977 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3978 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3979 84451b3e 2018-07-10 stsp const struct got_error *err;
3980 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3981 826082fe 2020-12-10 stsp char *line = NULL;
3982 826082fe 2020-12-10 stsp size_t linesize = 0;
3983 826082fe 2020-12-10 stsp ssize_t linelen;
3984 84451b3e 2018-07-10 stsp wchar_t *wline;
3985 27a741e5 2019-09-11 stsp int width;
3986 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3987 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3988 ab089a2a 2018-07-12 stsp char *id_str;
3989 11b20872 2019-11-08 stsp struct tog_color *tc;
3991 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3993 ab089a2a 2018-07-12 stsp return err;
3995 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
3996 f7d12f7e 2018-08-01 stsp werase(view->window);
3998 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3999 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4000 ab089a2a 2018-07-12 stsp free(id_str);
4001 ab089a2a 2018-07-12 stsp return err;
4004 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4005 ab089a2a 2018-07-12 stsp free(line);
4006 2550e4c3 2018-07-13 stsp line = NULL;
4008 1cae65b4 2019-09-22 stsp return err;
4009 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4010 a3404814 2018-09-02 stsp wstandout(view->window);
4011 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4013 11b20872 2019-11-08 stsp wattr_on(view->window,
4014 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4015 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4017 11b20872 2019-11-08 stsp wattr_off(view->window,
4018 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4019 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4020 a3404814 2018-09-02 stsp wstandend(view->window);
4021 2550e4c3 2018-07-13 stsp free(wline);
4022 2550e4c3 2018-07-13 stsp wline = NULL;
4023 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4024 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4026 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4027 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4028 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4029 ab089a2a 2018-07-12 stsp free(id_str);
4030 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4032 ab089a2a 2018-07-12 stsp free(id_str);
4033 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4034 3f60a8ef 2018-07-10 stsp free(line);
4035 2550e4c3 2018-07-13 stsp line = NULL;
4037 3f60a8ef 2018-07-10 stsp return err;
4038 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4039 2550e4c3 2018-07-13 stsp free(wline);
4040 2550e4c3 2018-07-13 stsp wline = NULL;
4041 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4042 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4044 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4045 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4046 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4047 826082fe 2020-12-10 stsp if (linelen == -1) {
4048 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4049 826082fe 2020-12-10 stsp s->eof = 1;
4052 84451b3e 2018-07-10 stsp free(line);
4053 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4055 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4058 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4059 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4061 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4062 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4063 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4064 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4065 27a741e5 2019-09-11 stsp !(view->focussed &&
4066 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4067 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4068 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4069 8d0fe45a 2019-08-12 stsp char *id_str;
4070 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4071 8d0fe45a 2019-08-12 stsp if (err) {
4072 8d0fe45a 2019-08-12 stsp free(line);
4073 8d0fe45a 2019-08-12 stsp return err;
4075 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4077 11b20872 2019-11-08 stsp wattr_on(view->window,
4078 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4079 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4081 11b20872 2019-11-08 stsp wattr_off(view->window,
4082 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4083 8d0fe45a 2019-08-12 stsp free(id_str);
4084 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4086 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4087 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4090 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4091 ee41ec32 2018-07-10 stsp prev_id = NULL;
4094 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4095 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4096 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4098 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4099 41605754 2020-11-12 stsp width = 9;
4100 41605754 2020-11-12 stsp wline = wcsdup(L"");
4101 41605754 2020-11-12 stsp if (wline == NULL) {
4102 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4103 41605754 2020-11-12 stsp free(line);
4104 41605754 2020-11-12 stsp return err;
4106 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4107 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4108 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4109 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4110 41605754 2020-11-12 stsp view->window, regmatch);
4111 41605754 2020-11-12 stsp if (err) {
4112 41605754 2020-11-12 stsp free(line);
4113 41605754 2020-11-12 stsp return err;
4115 41605754 2020-11-12 stsp width += 9;
4117 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4118 41605754 2020-11-12 stsp view->ncols - 9, 9);
4119 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4120 41605754 2020-11-12 stsp free(wline);
4121 41605754 2020-11-12 stsp wline = NULL;
4122 41605754 2020-11-12 stsp width += 9;
4125 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4126 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4127 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4128 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4130 826082fe 2020-12-10 stsp free(line);
4131 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4133 1a57306a 2018-09-02 stsp view_vborder(view);
4135 84451b3e 2018-07-10 stsp return NULL;
4138 84451b3e 2018-07-10 stsp static const struct got_error *
4139 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4141 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4142 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4143 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4144 1a76625f 2018-10-22 stsp int errcode;
4146 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4147 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4148 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4150 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4151 1a76625f 2018-10-22 stsp if (errcode)
4152 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4154 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4155 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4156 d68a0a7d 2018-07-10 stsp goto done;
4159 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4160 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4162 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4163 d68a0a7d 2018-07-10 stsp if (line->annotated)
4164 d68a0a7d 2018-07-10 stsp goto done;
4166 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4167 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4168 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4169 84451b3e 2018-07-10 stsp goto done;
4171 84451b3e 2018-07-10 stsp line->annotated = 1;
4173 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4174 1a76625f 2018-10-22 stsp if (errcode)
4175 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4176 84451b3e 2018-07-10 stsp return err;
4179 84451b3e 2018-07-10 stsp static void *
4180 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4182 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4183 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4184 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4185 1a76625f 2018-10-22 stsp int errcode;
4187 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4189 61266923 2020-01-14 stsp return (void *)err;
4191 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4192 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4193 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4194 fc06ba56 2019-08-22 stsp err = NULL;
4196 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4197 1a76625f 2018-10-22 stsp if (errcode)
4198 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4199 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4201 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4202 1d0f4054 2021-06-17 stsp if (err == NULL)
4203 1d0f4054 2021-06-17 stsp err = close_err;
4204 c9beca56 2018-07-22 stsp ta->repo = NULL;
4205 c9beca56 2018-07-22 stsp *ta->complete = 1;
4207 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4208 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4209 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4211 18430de3 2018-07-10 stsp return (void *)err;
4214 245d91c1 2018-07-12 stsp static struct got_object_id *
4215 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4216 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4218 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4220 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4221 8d0fe45a 2019-08-12 stsp return NULL;
4223 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4224 245d91c1 2018-07-12 stsp if (!line->annotated)
4225 245d91c1 2018-07-12 stsp return NULL;
4227 245d91c1 2018-07-12 stsp return line->id;
4230 b880a918 2018-07-10 stsp static const struct got_error *
4231 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4233 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4236 245d91c1 2018-07-12 stsp if (blame->thread) {
4237 1a76625f 2018-10-22 stsp int errcode;
4238 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4239 1a76625f 2018-10-22 stsp if (errcode)
4240 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4241 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4242 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4243 1a76625f 2018-10-22 stsp if (errcode)
4244 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4245 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4246 1a76625f 2018-10-22 stsp if (errcode)
4247 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4248 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4249 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4250 245d91c1 2018-07-12 stsp err = NULL;
4251 245d91c1 2018-07-12 stsp blame->thread = NULL;
4253 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4254 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4255 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4256 1d0f4054 2021-06-17 stsp if (err == NULL)
4257 1d0f4054 2021-06-17 stsp err = close_err;
4258 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4260 245d91c1 2018-07-12 stsp if (blame->f) {
4261 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4262 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4263 245d91c1 2018-07-12 stsp blame->f = NULL;
4265 57670559 2018-12-23 stsp if (blame->lines) {
4266 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4267 57670559 2018-12-23 stsp free(blame->lines[i].id);
4268 57670559 2018-12-23 stsp free(blame->lines);
4269 57670559 2018-12-23 stsp blame->lines = NULL;
4271 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4272 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4274 245d91c1 2018-07-12 stsp return err;
4277 245d91c1 2018-07-12 stsp static const struct got_error *
4278 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4280 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4281 fc06ba56 2019-08-22 stsp int *done = arg;
4282 fc06ba56 2019-08-22 stsp int errcode;
4284 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4285 fc06ba56 2019-08-22 stsp if (errcode)
4286 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4287 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4289 fc06ba56 2019-08-22 stsp if (*done)
4290 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4292 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4293 fc06ba56 2019-08-22 stsp if (errcode)
4294 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4295 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4297 fc06ba56 2019-08-22 stsp return err;
4300 fc06ba56 2019-08-22 stsp static const struct got_error *
4301 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4303 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4304 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4305 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4306 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4307 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4308 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4309 15a94983 2018-12-23 stsp int obj_type;
4311 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4312 a5388363 2020-12-01 naddy s->path);
4314 15a94983 2018-12-23 stsp return err;
4316 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4318 84451b3e 2018-07-10 stsp goto done;
4320 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4321 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4322 84451b3e 2018-07-10 stsp goto done;
4325 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4327 a70480e0 2018-06-23 stsp goto done;
4328 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4329 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4330 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4331 84451b3e 2018-07-10 stsp goto done;
4333 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4334 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4336 1fddf795 2021-01-20 stsp goto done;
4337 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4338 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4339 84451b3e 2018-07-10 stsp goto done;
4342 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4343 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4344 b02560ec 2019-08-19 stsp blame->nlines--;
4346 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4347 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4348 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4349 84451b3e 2018-07-10 stsp goto done;
4352 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4354 bd24772e 2018-07-11 stsp goto done;
4356 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4357 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4358 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4359 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4360 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4361 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4362 245d91c1 2018-07-12 stsp goto done;
4364 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4366 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4367 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4368 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4369 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4370 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4371 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4372 a5388363 2020-12-01 naddy s->blame_complete = 0;
4374 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4375 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4376 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4377 f5a09613 2020-12-13 naddy s->selected_line = 1;
4382 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4383 27d434c2 2018-09-15 stsp free(obj_id);
4385 245d91c1 2018-07-12 stsp stop_blame(blame);
4386 245d91c1 2018-07-12 stsp return err;
4389 245d91c1 2018-07-12 stsp static const struct got_error *
4390 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4391 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4393 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4394 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4396 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4398 c4843652 2019-08-12 stsp s->path = strdup(path);
4399 c4843652 2019-08-12 stsp if (s->path == NULL)
4400 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4402 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4403 c4843652 2019-08-12 stsp if (err) {
4404 c4843652 2019-08-12 stsp free(s->path);
4405 7cbe629d 2018-08-04 stsp return err;
4408 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4409 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4410 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4411 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4412 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4413 fb2756b9 2018-08-04 stsp s->repo = repo;
4414 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4415 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4417 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4418 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4419 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4420 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4422 11b20872 2019-11-08 stsp return err;
4425 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4426 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4427 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4428 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4429 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4431 a5388363 2020-12-01 naddy return run_blame(view);
4434 e5a0f69f 2018-08-18 stsp static const struct got_error *
4435 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4437 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4438 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4440 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4441 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4443 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4444 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4445 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4446 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4447 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4450 e5a0f69f 2018-08-18 stsp free(s->path);
4451 11b20872 2019-11-08 stsp free_colors(&s->colors);
4453 e5a0f69f 2018-08-18 stsp return err;
4456 7cbe629d 2018-08-04 stsp static const struct got_error *
4457 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4459 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4461 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4462 6c4c42e0 2019-06-24 stsp return NULL;
4465 6c4c42e0 2019-06-24 stsp static const struct got_error *
4466 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4468 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4469 6c4c42e0 2019-06-24 stsp int lineno;
4470 826082fe 2020-12-10 stsp char *line = NULL;
4471 826082fe 2020-12-10 stsp size_t linesize = 0;
4472 826082fe 2020-12-10 stsp ssize_t linelen;
4474 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4475 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4476 6c4c42e0 2019-06-24 stsp return NULL;
4479 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4480 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4481 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4483 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4485 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4486 6c4c42e0 2019-06-24 stsp lineno = 1;
4488 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4491 6c4c42e0 2019-06-24 stsp while (1) {
4492 6c4c42e0 2019-06-24 stsp off_t offset;
4494 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4495 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4496 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4500 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4501 6c4c42e0 2019-06-24 stsp lineno = 1;
4503 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4506 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4507 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4508 6c4c42e0 2019-06-24 stsp free(line);
4509 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4511 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4512 826082fe 2020-12-10 stsp if (linelen != -1 &&
4513 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4514 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4515 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4518 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4523 826082fe 2020-12-10 stsp free(line);
4525 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4526 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4527 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4530 6c4c42e0 2019-06-24 stsp return NULL;
4533 6c4c42e0 2019-06-24 stsp static const struct got_error *
4534 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4536 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4537 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4538 2b380cc8 2018-10-24 stsp int errcode;
4540 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4541 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4542 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4543 2b380cc8 2018-10-24 stsp if (errcode)
4544 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4546 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4549 51fe7530 2019-08-19 stsp if (s->blame_complete)
4550 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4552 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4554 669b5ffa 2018-10-07 stsp view_vborder(view);
4555 e5a0f69f 2018-08-18 stsp return err;
4558 e5a0f69f 2018-08-18 stsp static const struct got_error *
4559 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4561 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4562 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4563 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4564 669b5ffa 2018-10-07 stsp int begin_x = 0;
4566 e5a0f69f 2018-08-18 stsp switch (ch) {
4568 1e37a5c2 2019-05-12 jcs s->done = 1;
4570 4deef56f 2021-09-02 naddy case 'g':
4571 4deef56f 2021-09-02 naddy case KEY_HOME:
4572 4deef56f 2021-09-02 naddy s->selected_line = 1;
4573 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4575 4deef56f 2021-09-02 naddy case 'G':
4576 4deef56f 2021-09-02 naddy case KEY_END:
4577 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4578 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4579 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4581 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4582 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4583 4deef56f 2021-09-02 naddy (view->nlines - 3);
4587 1e37a5c2 2019-05-12 jcs case KEY_UP:
4588 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4589 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4590 1e37a5c2 2019-05-12 jcs s->selected_line--;
4591 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4592 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4593 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4595 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4596 ea025d1d 2020-02-22 naddy case CTRL('b'):
4597 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4598 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4601 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4602 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4603 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4605 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4608 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4609 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4610 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4611 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4612 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4613 1e37a5c2 2019-05-12 jcs s->selected_line++;
4614 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4615 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4616 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4619 1e37a5c2 2019-05-12 jcs case 'p': {
4620 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4621 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4622 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4623 1e37a5c2 2019-05-12 jcs if (id == NULL)
4625 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4626 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4627 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4628 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4629 1e37a5c2 2019-05-12 jcs int obj_type;
4630 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4631 1e37a5c2 2019-05-12 jcs s->repo, id);
4634 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4635 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4636 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4637 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4640 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4641 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4642 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4643 e5a0f69f 2018-08-18 stsp if (err) {
4644 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4645 1e37a5c2 2019-05-12 jcs err = NULL;
4646 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4649 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4651 1e37a5c2 2019-05-12 jcs free(blob_id);
4652 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4653 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4654 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4657 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4659 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4661 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4662 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4664 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4669 1e37a5c2 2019-05-12 jcs s->done = 1;
4670 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4671 1e37a5c2 2019-05-12 jcs s->done = 0;
4672 1e37a5c2 2019-05-12 jcs if (thread_err)
4674 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4675 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4676 a5388363 2020-12-01 naddy err = run_blame(view);
4681 1e37a5c2 2019-05-12 jcs case 'B': {
4682 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4683 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4684 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4686 1e37a5c2 2019-05-12 jcs s->done = 1;
4687 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4688 1e37a5c2 2019-05-12 jcs s->done = 0;
4689 1e37a5c2 2019-05-12 jcs if (thread_err)
4691 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4692 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4693 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4694 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4695 a5388363 2020-12-01 naddy err = run_blame(view);
4700 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4701 1e37a5c2 2019-05-12 jcs case '\r': {
4702 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4703 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4704 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4705 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4706 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4707 1e37a5c2 2019-05-12 jcs if (id == NULL)
4709 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4712 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4713 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4714 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4715 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4716 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4717 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4718 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4719 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4722 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4723 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4724 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4726 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4729 e78dc838 2020-12-04 stsp view->focussed = 0;
4730 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4731 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4732 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4735 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4736 e78dc838 2020-12-04 stsp view->focus_child = 1;
4738 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4743 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4744 ea025d1d 2020-02-22 naddy case CTRL('f'):
4746 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4747 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4748 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4751 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4752 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4753 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4754 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4757 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4758 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4759 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4760 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4762 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4763 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4764 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4766 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4767 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4768 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4769 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4775 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4778 a70480e0 2018-06-23 stsp static const struct got_error *
4779 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4781 a70480e0 2018-06-23 stsp const struct got_error *error;
4782 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4783 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4784 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4785 0587e10c 2020-07-23 stsp char *link_target = NULL;
4786 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4787 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4789 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4791 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4792 a70480e0 2018-06-23 stsp switch (ch) {
4794 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4797 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4798 69069811 2018-08-02 stsp if (repo_path == NULL)
4799 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4803 17020d27 2019-03-07 stsp usage_blame();
4804 a70480e0 2018-06-23 stsp /* NOTREACHED */
4808 a70480e0 2018-06-23 stsp argc -= optind;
4809 a70480e0 2018-06-23 stsp argv += optind;
4811 f135c941 2020-02-20 stsp if (argc != 1)
4812 a70480e0 2018-06-23 stsp usage_blame();
4814 69069811 2018-08-02 stsp if (repo_path == NULL) {
4815 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4816 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4817 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4818 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4819 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4820 c156c7a4 2020-12-18 stsp goto done;
4821 f135c941 2020-02-20 stsp if (worktree)
4822 eb41ed75 2019-02-05 stsp repo_path =
4823 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4825 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4826 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4827 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4828 c156c7a4 2020-12-18 stsp goto done;
4832 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4833 c02c541e 2019-03-29 stsp if (error != NULL)
4834 8e94dd5b 2019-01-04 stsp goto done;
4836 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4837 f135c941 2020-02-20 stsp worktree);
4838 c02c541e 2019-03-29 stsp if (error)
4839 92205607 2019-01-04 stsp goto done;
4841 f135c941 2020-02-20 stsp init_curses();
4843 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4844 51a10b52 2020-12-26 stsp if (error)
4845 51a10b52 2020-12-26 stsp goto done;
4847 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4848 eb41ed75 2019-02-05 stsp if (error)
4849 69069811 2018-08-02 stsp goto done;
4851 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4852 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4853 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4854 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4855 a70480e0 2018-06-23 stsp if (error != NULL)
4856 66b4983c 2018-06-23 stsp goto done;
4857 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4858 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4860 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4861 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4863 a19e88aa 2018-06-23 stsp if (error != NULL)
4864 8b473291 2019-02-21 stsp goto done;
4866 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4867 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4868 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4869 e1cd8fed 2018-08-01 stsp goto done;
4872 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4873 0587e10c 2020-07-23 stsp commit_id, repo);
4874 7cbe629d 2018-08-04 stsp if (error)
4875 7cbe629d 2018-08-04 stsp goto done;
4877 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4878 78756c87 2020-11-24 stsp commit_id, repo);
4879 0587e10c 2020-07-23 stsp if (error)
4880 0587e10c 2020-07-23 stsp goto done;
4881 12314ad4 2019-08-31 stsp if (worktree) {
4882 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4883 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4884 12314ad4 2019-08-31 stsp worktree = NULL;
4886 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4888 69069811 2018-08-02 stsp free(repo_path);
4889 f135c941 2020-02-20 stsp free(in_repo_path);
4890 0587e10c 2020-07-23 stsp free(link_target);
4891 69069811 2018-08-02 stsp free(cwd);
4892 a70480e0 2018-06-23 stsp free(commit_id);
4893 eb41ed75 2019-02-05 stsp if (worktree)
4894 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4895 1d0f4054 2021-06-17 stsp if (repo) {
4896 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4897 1d0f4054 2021-06-17 stsp if (error == NULL)
4898 1d0f4054 2021-06-17 stsp error = close_err;
4900 51a10b52 2020-12-26 stsp tog_free_refs();
4901 a70480e0 2018-06-23 stsp return error;
4904 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4905 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4907 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4908 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4909 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4910 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4911 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4912 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4913 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4915 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4917 f7d12f7e 2018-08-01 stsp werase(view->window);
4919 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4920 ffd1d5e5 2018-06-23 stsp return NULL;
4922 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4924 ffd1d5e5 2018-06-23 stsp return err;
4925 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4926 a3404814 2018-09-02 stsp wstandout(view->window);
4927 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4929 11b20872 2019-11-08 stsp wattr_on(view->window,
4930 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4931 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4933 11b20872 2019-11-08 stsp wattr_off(view->window,
4934 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4935 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4936 a3404814 2018-09-02 stsp wstandend(view->window);
4937 2550e4c3 2018-07-13 stsp free(wline);
4938 2550e4c3 2018-07-13 stsp wline = NULL;
4939 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4940 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4941 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4942 ffd1d5e5 2018-06-23 stsp return NULL;
4943 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4945 ce52c690 2018-06-23 stsp return err;
4946 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4947 2550e4c3 2018-07-13 stsp free(wline);
4948 2550e4c3 2018-07-13 stsp wline = NULL;
4949 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4950 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4951 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4952 ffd1d5e5 2018-06-23 stsp return NULL;
4953 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4954 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4955 a1eca9bb 2018-06-23 stsp return NULL;
4957 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4958 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4959 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4960 0cf4efb1 2018-09-29 stsp if (view->focussed)
4961 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4962 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4964 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4965 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4966 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4967 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4968 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4969 ffd1d5e5 2018-06-23 stsp return NULL;
4973 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4976 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4977 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4978 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4979 848d6979 2019-08-12 stsp const char *modestr = "";
4980 56e0773d 2019-11-28 stsp mode_t mode;
4982 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4983 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4985 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4986 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4987 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4989 638f9024 2019-05-13 stsp return got_error_from_errno(
4990 230a42bd 2019-05-11 jcs "got_object_id_str");
4992 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4993 63c5ca5d 2019-08-24 stsp modestr = "$";
4994 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4997 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4998 d86d3b18 2020-12-01 naddy te, s->repo);
4999 0d6c6ee3 2020-05-20 stsp if (err) {
5000 0d6c6ee3 2020-05-20 stsp free(id_str);
5001 0d6c6ee3 2020-05-20 stsp return err;
5003 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5004 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5005 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5007 848d6979 2019-08-12 stsp modestr = "@";
5009 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5010 848d6979 2019-08-12 stsp modestr = "/";
5011 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5012 848d6979 2019-08-12 stsp modestr = "*";
5013 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5014 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5015 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5016 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5017 1d13200f 2018-07-12 stsp free(id_str);
5018 0d6c6ee3 2020-05-20 stsp free(link_target);
5019 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5021 1d13200f 2018-07-12 stsp free(id_str);
5022 0d6c6ee3 2020-05-20 stsp free(link_target);
5023 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5024 ffd1d5e5 2018-06-23 stsp if (err) {
5025 ffd1d5e5 2018-06-23 stsp free(line);
5028 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5029 0cf4efb1 2018-09-29 stsp if (view->focussed)
5030 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5031 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5033 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5035 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5036 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5037 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5039 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5040 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5041 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5042 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5043 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5044 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5045 ffd1d5e5 2018-06-23 stsp free(line);
5046 2550e4c3 2018-07-13 stsp free(wline);
5047 2550e4c3 2018-07-13 stsp wline = NULL;
5049 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5050 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5051 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5055 ffd1d5e5 2018-06-23 stsp return err;
5058 ffd1d5e5 2018-06-23 stsp static void
5059 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5061 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5062 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5063 fa86c4bf 2020-11-29 stsp int i = 0;
5065 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5068 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5069 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5070 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5071 fa86c4bf 2020-11-29 stsp if (!isroot)
5072 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5075 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5076 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5080 fa86c4bf 2020-11-29 stsp static void
5081 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5083 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5084 ffd1d5e5 2018-06-23 stsp int n = 0;
5086 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5087 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5088 694d3271 2020-12-01 naddy s->first_displayed_entry);
5090 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5092 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5093 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5094 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5095 768394f3 2019-01-24 stsp if (last) {
5096 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5097 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5102 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5103 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5104 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5106 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5107 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5108 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5110 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5111 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5112 56e0773d 2019-11-28 stsp + 1 /* slash */;
5114 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5116 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5117 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5118 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5120 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5121 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5122 d9765a41 2018-06-23 stsp while (pt) {
5123 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5124 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5125 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5126 cb2ebc8a 2018-06-23 stsp goto done;
5128 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5129 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5130 cb2ebc8a 2018-06-23 stsp goto done;
5132 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5135 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5136 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5137 ce52c690 2018-06-23 stsp goto done;
5141 ce52c690 2018-06-23 stsp if (err) {
5142 ce52c690 2018-06-23 stsp free(*path);
5143 ce52c690 2018-06-23 stsp *path = NULL;
5145 ce52c690 2018-06-23 stsp return err;
5148 ce52c690 2018-06-23 stsp static const struct got_error *
5149 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5150 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5151 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5153 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5154 ce52c690 2018-06-23 stsp char *path;
5155 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5157 a0de39f3 2019-08-09 stsp *new_view = NULL;
5159 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5161 ce52c690 2018-06-23 stsp return err;
5163 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5164 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5165 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5166 83ce39e3 2019-08-12 stsp goto done;
5169 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5170 e5a0f69f 2018-08-18 stsp if (err) {
5171 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5172 fc06ba56 2019-08-22 stsp err = NULL;
5173 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5175 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5177 83ce39e3 2019-08-12 stsp free(path);
5178 69efd4c4 2018-07-18 stsp return err;
5181 69efd4c4 2018-07-18 stsp static const struct got_error *
5182 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5183 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5185 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5186 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5187 69efd4c4 2018-07-18 stsp char *path;
5189 a0de39f3 2019-08-09 stsp *new_view = NULL;
5191 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5192 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5193 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5195 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5197 69efd4c4 2018-07-18 stsp return err;
5199 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5202 e5a0f69f 2018-08-18 stsp view_close(log_view);
5204 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5205 cb2ebc8a 2018-06-23 stsp free(path);
5206 cb2ebc8a 2018-06-23 stsp return err;
5209 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5210 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5211 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5213 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5214 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5215 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5216 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5218 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5219 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5221 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5222 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5223 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5225 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5227 bc573f3b 2021-07-10 stsp goto done;
5230 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5231 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5232 bc573f3b 2021-07-10 stsp * closed on demand.
5234 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5235 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5237 bc573f3b 2021-07-10 stsp goto done;
5238 bc573f3b 2021-07-10 stsp s->tree = s->root;
5240 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5241 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5242 ffd1d5e5 2018-06-23 stsp goto done;
5244 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5246 ffd1d5e5 2018-06-23 stsp goto done;
5249 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5250 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5251 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5252 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5253 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5254 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5255 9cd7cbd1 2020-12-07 stsp goto done;
5258 fb2756b9 2018-08-04 stsp s->repo = repo;
5260 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5261 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5262 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5263 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5265 c0b01bdb 2019-11-08 stsp goto done;
5266 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5267 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5269 c0b01bdb 2019-11-08 stsp goto done;
5270 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5271 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5272 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5274 c0b01bdb 2019-11-08 stsp goto done;
5276 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5277 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5278 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5280 11b20872 2019-11-08 stsp goto done;
5282 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5283 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5285 c0b01bdb 2019-11-08 stsp goto done;
5288 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5289 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5290 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5291 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5292 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5294 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5295 bc573f3b 2021-07-10 stsp if (commit)
5296 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5298 bc573f3b 2021-07-10 stsp close_tree_view(view);
5299 ad80ab7b 2018-08-04 stsp return err;
5302 e5a0f69f 2018-08-18 stsp static const struct got_error *
5303 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5305 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5307 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5308 fb2756b9 2018-08-04 stsp free(s->tree_label);
5309 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5310 6484ec90 2018-09-29 stsp free(s->commit_id);
5311 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5312 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5313 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5314 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5315 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5316 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5317 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5318 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5319 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5320 ad80ab7b 2018-08-04 stsp free(parent);
5323 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5324 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5325 bc573f3b 2021-07-10 stsp if (s->root)
5326 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5327 7c32bd05 2019-06-22 stsp return NULL;
5330 7c32bd05 2019-06-22 stsp static const struct got_error *
5331 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5333 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5335 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5336 7c32bd05 2019-06-22 stsp return NULL;
5339 7c32bd05 2019-06-22 stsp static int
5340 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5342 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5344 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5348 7c32bd05 2019-06-22 stsp static const struct got_error *
5349 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5351 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5352 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5354 7c32bd05 2019-06-22 stsp if (!view->searching) {
5355 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5356 7c32bd05 2019-06-22 stsp return NULL;
5359 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5360 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5361 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5362 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5363 56e0773d 2019-11-28 stsp s->selected_entry);
5365 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5367 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5368 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5370 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5371 56e0773d 2019-11-28 stsp s->selected_entry);
5374 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5375 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5377 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5380 7c32bd05 2019-06-22 stsp while (1) {
5381 56e0773d 2019-11-28 stsp if (te == NULL) {
5382 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5383 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5384 ac66afb8 2019-06-24 stsp return NULL;
5386 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5387 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5389 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5392 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5393 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5394 56e0773d 2019-11-28 stsp s->matched_entry = te;
5398 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5399 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5401 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5404 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5405 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5406 7c32bd05 2019-06-22 stsp s->selected = 0;
5409 e5a0f69f 2018-08-18 stsp return NULL;
5412 ad80ab7b 2018-08-04 stsp static const struct got_error *
5413 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5415 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5416 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5417 e5a0f69f 2018-08-18 stsp char *parent_path;
5419 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5421 e5a0f69f 2018-08-18 stsp return err;
5423 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5424 e5a0f69f 2018-08-18 stsp free(parent_path);
5426 669b5ffa 2018-10-07 stsp view_vborder(view);
5427 e5a0f69f 2018-08-18 stsp return err;
5430 e5a0f69f 2018-08-18 stsp static const struct got_error *
5431 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5433 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5434 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5435 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5436 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5437 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
5439 e5a0f69f 2018-08-18 stsp switch (ch) {
5441 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5444 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5446 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5447 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5448 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5449 e78dc838 2020-12-04 stsp view->focussed = 0;
5450 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5451 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5452 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5454 1e37a5c2 2019-05-12 jcs return err;
5455 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5456 e78dc838 2020-12-04 stsp view->focus_child = 1;
5458 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5461 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5462 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5463 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5464 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5465 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5466 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5467 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5468 152c1c93 2020-11-29 stsp if (err) {
5469 152c1c93 2020-11-29 stsp view_close(ref_view);
5470 152c1c93 2020-11-29 stsp return err;
5472 e78dc838 2020-12-04 stsp view->focussed = 0;
5473 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5474 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5475 152c1c93 2020-11-29 stsp err = view_close_child(view);
5477 152c1c93 2020-11-29 stsp return err;
5478 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5479 e78dc838 2020-12-04 stsp view->focus_child = 1;
5481 152c1c93 2020-11-29 stsp *new_view = ref_view;
5483 e4526bf5 2021-09-03 naddy case 'g':
5484 e4526bf5 2021-09-03 naddy case KEY_HOME:
5485 e4526bf5 2021-09-03 naddy s->selected = 0;
5486 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5487 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5488 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5490 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5492 e4526bf5 2021-09-03 naddy case 'G':
5493 e4526bf5 2021-09-03 naddy case KEY_END:
5494 e4526bf5 2021-09-03 naddy s->selected = 0;
5495 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5496 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5497 e4526bf5 2021-09-03 naddy if (te == NULL) {
5498 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5499 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5504 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5505 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5507 e4526bf5 2021-09-03 naddy if (n > 0)
5508 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5511 1e37a5c2 2019-05-12 jcs case KEY_UP:
5512 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5513 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5514 1e37a5c2 2019-05-12 jcs s->selected--;
5517 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5519 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5520 ea025d1d 2020-02-22 naddy case CTRL('b'):
5521 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5522 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5523 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5524 fa86c4bf 2020-11-29 stsp s->selected = 0;
5526 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5527 fa86c4bf 2020-11-29 stsp s->selected = 0;
5529 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5532 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5533 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5534 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5535 1e37a5c2 2019-05-12 jcs s->selected++;
5538 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5540 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5542 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5544 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5545 ea025d1d 2020-02-22 naddy case CTRL('f'):
5546 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5548 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5549 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5550 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5553 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5555 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5557 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5558 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5559 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5560 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5561 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5563 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5564 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5566 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5567 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5568 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5569 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5570 1e37a5c2 2019-05-12 jcs s->selected_entry =
5571 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5572 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5573 1e37a5c2 2019-05-12 jcs free(parent);
5574 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5575 56e0773d 2019-11-28 stsp s->selected_entry))) {
5576 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5577 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5578 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5581 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5582 941e9f74 2019-05-21 stsp if (err) {
5583 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5586 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5587 56e0773d 2019-11-28 stsp s->selected_entry))) {
5588 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5589 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5590 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5592 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5593 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5594 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5597 e78dc838 2020-12-04 stsp view->focussed = 0;
5598 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5599 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5600 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5602 669b5ffa 2018-10-07 stsp return err;
5603 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5604 e78dc838 2020-12-04 stsp view->focus_child = 1;
5606 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5609 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5610 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5611 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5617 ffd1d5e5 2018-06-23 stsp return err;
5620 ffd1d5e5 2018-06-23 stsp __dead static void
5621 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5624 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5625 ffd1d5e5 2018-06-23 stsp getprogname());
5629 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5630 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5632 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5633 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5634 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5635 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5636 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5637 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5638 4e97c21c 2020-12-06 stsp char *label = NULL;
5639 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5640 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5642 5221c383 2018-08-01 stsp struct tog_view *view;
5644 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5645 ffd1d5e5 2018-06-23 stsp switch (ch) {
5647 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5650 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5651 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5652 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5656 e99e2d15 2020-11-24 naddy usage_tree();
5657 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5661 ffd1d5e5 2018-06-23 stsp argc -= optind;
5662 ffd1d5e5 2018-06-23 stsp argv += optind;
5664 55cccc34 2020-02-20 stsp if (argc > 1)
5665 e99e2d15 2020-11-24 naddy usage_tree();
5667 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5668 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5669 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5670 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5671 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5672 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5673 c156c7a4 2020-12-18 stsp goto done;
5674 55cccc34 2020-02-20 stsp if (worktree)
5675 52185f70 2019-02-05 stsp repo_path =
5676 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5678 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5679 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5680 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5681 c156c7a4 2020-12-18 stsp goto done;
5685 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5686 c02c541e 2019-03-29 stsp if (error != NULL)
5687 52185f70 2019-02-05 stsp goto done;
5689 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5690 55cccc34 2020-02-20 stsp repo, worktree);
5691 55cccc34 2020-02-20 stsp if (error)
5692 55cccc34 2020-02-20 stsp goto done;
5694 55cccc34 2020-02-20 stsp init_curses();
5696 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5697 c02c541e 2019-03-29 stsp if (error)
5698 52185f70 2019-02-05 stsp goto done;
5700 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5701 51a10b52 2020-12-26 stsp if (error)
5702 51a10b52 2020-12-26 stsp goto done;
5704 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5705 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5706 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5707 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5708 4e97c21c 2020-12-06 stsp if (error)
5709 4e97c21c 2020-12-06 stsp goto done;
5710 4e97c21c 2020-12-06 stsp head_ref_name = label;
5712 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5713 4e97c21c 2020-12-06 stsp if (error == NULL)
5714 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5715 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5716 4e97c21c 2020-12-06 stsp goto done;
5717 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5718 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5719 4e97c21c 2020-12-06 stsp if (error)
5720 4e97c21c 2020-12-06 stsp goto done;
5723 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5724 5221c383 2018-08-01 stsp if (view == NULL) {
5725 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5726 5221c383 2018-08-01 stsp goto done;
5728 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5729 ad80ab7b 2018-08-04 stsp if (error)
5730 ad80ab7b 2018-08-04 stsp goto done;
5731 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5732 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5733 d91faf3b 2020-12-01 naddy in_repo_path);
5734 55cccc34 2020-02-20 stsp if (error)
5735 55cccc34 2020-02-20 stsp goto done;
5738 55cccc34 2020-02-20 stsp if (worktree) {
5739 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5740 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5741 55cccc34 2020-02-20 stsp worktree = NULL;
5743 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5745 52185f70 2019-02-05 stsp free(repo_path);
5746 e4a0e26d 2020-02-20 stsp free(cwd);
5747 ffd1d5e5 2018-06-23 stsp free(commit_id);
5748 4e97c21c 2020-12-06 stsp free(label);
5750 486cd271 2020-12-06 stsp got_ref_close(ref);
5751 1d0f4054 2021-06-17 stsp if (repo) {
5752 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5753 1d0f4054 2021-06-17 stsp if (error == NULL)
5754 1d0f4054 2021-06-17 stsp error = close_err;
5756 51a10b52 2020-12-26 stsp tog_free_refs();
5757 ffd1d5e5 2018-06-23 stsp return error;
5760 6458efa5 2020-11-24 stsp static const struct got_error *
5761 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5763 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5764 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5766 6458efa5 2020-11-24 stsp s->nrefs = 0;
5767 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5768 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5771 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5772 6458efa5 2020-11-24 stsp if (re == NULL)
5773 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5775 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5776 8924d611 2020-12-26 stsp if (re->ref == NULL)
5777 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5778 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5779 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5782 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5783 6458efa5 2020-11-24 stsp return NULL;
5787 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5789 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5791 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5792 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5793 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5794 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5799 6458efa5 2020-11-24 stsp static const struct got_error *
5800 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5802 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5803 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5805 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5806 6458efa5 2020-11-24 stsp s->repo = repo;
5808 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5809 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5811 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5813 6458efa5 2020-11-24 stsp return err;
5815 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5816 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5817 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5818 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5820 6458efa5 2020-11-24 stsp goto done;
5822 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5823 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5824 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5826 6458efa5 2020-11-24 stsp goto done;
5828 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5829 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5830 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5832 6458efa5 2020-11-24 stsp goto done;
5835 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5836 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5837 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5838 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5839 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5842 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5843 6458efa5 2020-11-24 stsp return err;
5846 6458efa5 2020-11-24 stsp static const struct got_error *
5847 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5849 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5851 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5852 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5854 6458efa5 2020-11-24 stsp return NULL;
5857 6458efa5 2020-11-24 stsp static const struct got_error *
5858 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5859 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5861 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5862 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5863 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5864 6458efa5 2020-11-24 stsp int obj_type;
5866 c42c9805 2020-11-24 stsp *commit_id = NULL;
5868 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5870 6458efa5 2020-11-24 stsp return err;
5872 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5874 6458efa5 2020-11-24 stsp goto done;
5876 6458efa5 2020-11-24 stsp switch (obj_type) {
5877 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5878 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5880 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5881 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5883 6458efa5 2020-11-24 stsp goto done;
5884 c42c9805 2020-11-24 stsp free(obj_id);
5885 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5886 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5888 6458efa5 2020-11-24 stsp goto done;
5889 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5890 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5891 c42c9805 2020-11-24 stsp goto done;
5893 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5894 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5895 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5896 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5897 c42c9805 2020-11-24 stsp goto done;
5901 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5907 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5908 c42c9805 2020-11-24 stsp if (err) {
5909 c42c9805 2020-11-24 stsp free(*commit_id);
5910 c42c9805 2020-11-24 stsp *commit_id = NULL;
5912 c42c9805 2020-11-24 stsp return err;
5915 c42c9805 2020-11-24 stsp static const struct got_error *
5916 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5917 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5919 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5920 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5921 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5923 c42c9805 2020-11-24 stsp *new_view = NULL;
5925 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5926 c42c9805 2020-11-24 stsp if (err) {
5927 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5928 c42c9805 2020-11-24 stsp return err;
5930 c42c9805 2020-11-24 stsp return NULL;
5933 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5934 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5935 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5936 6458efa5 2020-11-24 stsp goto done;
5939 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5940 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5943 6458efa5 2020-11-24 stsp view_close(log_view);
5945 6458efa5 2020-11-24 stsp *new_view = log_view;
5946 c42c9805 2020-11-24 stsp free(commit_id);
5947 6458efa5 2020-11-24 stsp return err;
5950 ce5b7c56 2019-07-09 stsp static void
5951 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5953 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5954 34ba6917 2020-11-30 stsp int i = 0;
5956 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5959 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5960 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5961 34ba6917 2020-11-30 stsp if (re == NULL)
5963 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5964 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5968 34ba6917 2020-11-30 stsp static void
5969 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5971 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5972 6458efa5 2020-11-24 stsp int n = 0;
5974 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5975 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5977 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5979 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5980 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5981 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5982 6458efa5 2020-11-24 stsp if (last) {
5983 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5984 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5989 6458efa5 2020-11-24 stsp static const struct got_error *
5990 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5992 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5994 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5995 6458efa5 2020-11-24 stsp return NULL;
5998 6458efa5 2020-11-24 stsp static int
5999 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6001 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6003 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6007 6458efa5 2020-11-24 stsp static const struct got_error *
6008 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6010 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6011 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6013 6458efa5 2020-11-24 stsp if (!view->searching) {
6014 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6015 6458efa5 2020-11-24 stsp return NULL;
6018 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6019 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6020 6458efa5 2020-11-24 stsp if (s->selected_entry)
6021 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6023 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6024 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6026 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6027 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6029 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6030 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6033 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6034 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6036 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6039 6458efa5 2020-11-24 stsp while (1) {
6040 6458efa5 2020-11-24 stsp if (re == NULL) {
6041 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6042 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6043 6458efa5 2020-11-24 stsp return NULL;
6045 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6046 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6048 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6051 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6052 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6053 6458efa5 2020-11-24 stsp s->matched_entry = re;
6057 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6058 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6060 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6063 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6064 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6065 6458efa5 2020-11-24 stsp s->selected = 0;
6068 6458efa5 2020-11-24 stsp return NULL;
6071 6458efa5 2020-11-24 stsp static const struct got_error *
6072 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6074 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6075 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6076 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6077 6458efa5 2020-11-24 stsp char *line = NULL;
6078 6458efa5 2020-11-24 stsp wchar_t *wline;
6079 6458efa5 2020-11-24 stsp struct tog_color *tc;
6080 6458efa5 2020-11-24 stsp int width, n;
6081 6458efa5 2020-11-24 stsp int limit = view->nlines;
6083 6458efa5 2020-11-24 stsp werase(view->window);
6085 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6087 6458efa5 2020-11-24 stsp if (limit == 0)
6088 6458efa5 2020-11-24 stsp return NULL;
6090 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6092 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6093 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6094 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6096 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6097 6458efa5 2020-11-24 stsp if (err) {
6098 6458efa5 2020-11-24 stsp free(line);
6099 6458efa5 2020-11-24 stsp return err;
6101 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6102 6458efa5 2020-11-24 stsp wstandout(view->window);
6103 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6104 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6105 6458efa5 2020-11-24 stsp wstandend(view->window);
6106 6458efa5 2020-11-24 stsp free(wline);
6107 6458efa5 2020-11-24 stsp wline = NULL;
6108 6458efa5 2020-11-24 stsp free(line);
6109 6458efa5 2020-11-24 stsp line = NULL;
6110 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6111 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6112 6458efa5 2020-11-24 stsp if (--limit <= 0)
6113 6458efa5 2020-11-24 stsp return NULL;
6116 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6117 6458efa5 2020-11-24 stsp char *line = NULL;
6119 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6120 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6121 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6122 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6123 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6124 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6125 6458efa5 2020-11-24 stsp struct got_object_id *id;
6126 6458efa5 2020-11-24 stsp char *id_str;
6127 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6129 6458efa5 2020-11-24 stsp return err;
6130 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6131 6458efa5 2020-11-24 stsp if (err) {
6133 6458efa5 2020-11-24 stsp return err;
6135 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6136 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6137 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6139 6458efa5 2020-11-24 stsp free(id_str);
6140 6458efa5 2020-11-24 stsp return err;
6143 6458efa5 2020-11-24 stsp free(id_str);
6145 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6146 6458efa5 2020-11-24 stsp if (line == NULL)
6147 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6150 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6151 6458efa5 2020-11-24 stsp if (err) {
6152 6458efa5 2020-11-24 stsp free(line);
6153 6458efa5 2020-11-24 stsp return err;
6155 6458efa5 2020-11-24 stsp if (n == s->selected) {
6156 6458efa5 2020-11-24 stsp if (view->focussed)
6157 6458efa5 2020-11-24 stsp wstandout(view->window);
6158 6458efa5 2020-11-24 stsp s->selected_entry = re;
6160 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6162 6458efa5 2020-11-24 stsp wattr_on(view->window,
6163 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6164 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6166 6458efa5 2020-11-24 stsp wattr_off(view->window,
6167 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6168 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6169 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6170 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6171 6458efa5 2020-11-24 stsp wstandend(view->window);
6172 6458efa5 2020-11-24 stsp free(line);
6173 6458efa5 2020-11-24 stsp free(wline);
6174 6458efa5 2020-11-24 stsp wline = NULL;
6176 6458efa5 2020-11-24 stsp s->ndisplayed++;
6177 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6180 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6183 6458efa5 2020-11-24 stsp view_vborder(view);
6184 6458efa5 2020-11-24 stsp return err;
6187 6458efa5 2020-11-24 stsp static const struct got_error *
6188 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6189 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6191 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6192 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6193 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6195 c42c9805 2020-11-24 stsp *new_view = NULL;
6197 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6198 c42c9805 2020-11-24 stsp if (err) {
6199 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6200 c42c9805 2020-11-24 stsp return err;
6202 c42c9805 2020-11-24 stsp return NULL;
6206 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6207 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6208 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6209 c42c9805 2020-11-24 stsp goto done;
6212 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6213 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6215 c42c9805 2020-11-24 stsp goto done;
6217 c42c9805 2020-11-24 stsp *new_view = tree_view;
6219 c42c9805 2020-11-24 stsp free(commit_id);
6220 c42c9805 2020-11-24 stsp return err;
6222 c42c9805 2020-11-24 stsp static const struct got_error *
6223 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6225 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6226 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6227 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6228 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6229 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
6231 6458efa5 2020-11-24 stsp switch (ch) {
6233 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6236 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
6237 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6238 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
6239 50617b77 2021-11-20 stsp got_ref_cmp_by_name, s->repo);
6242 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
6243 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
6244 50617b77 2021-11-20 stsp &tog_refs, s->repo);
6247 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
6248 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
6250 6458efa5 2020-11-24 stsp case KEY_ENTER:
6251 6458efa5 2020-11-24 stsp case '\r':
6252 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6254 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6255 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6256 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6258 e78dc838 2020-12-04 stsp view->focussed = 0;
6259 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6260 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6261 6458efa5 2020-11-24 stsp err = view_close_child(view);
6263 6458efa5 2020-11-24 stsp return err;
6264 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6265 e78dc838 2020-12-04 stsp view->focus_child = 1;
6267 6458efa5 2020-11-24 stsp *new_view = log_view;
6270 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6272 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6273 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6274 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6276 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6278 e78dc838 2020-12-04 stsp view->focussed = 0;
6279 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6280 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6281 c42c9805 2020-11-24 stsp err = view_close_child(view);
6283 c42c9805 2020-11-24 stsp return err;
6284 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6285 e78dc838 2020-12-04 stsp view->focus_child = 1;
6287 c42c9805 2020-11-24 stsp *new_view = tree_view;
6289 e4526bf5 2021-09-03 naddy case 'g':
6290 e4526bf5 2021-09-03 naddy case KEY_HOME:
6291 e4526bf5 2021-09-03 naddy s->selected = 0;
6292 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6294 e4526bf5 2021-09-03 naddy case 'G':
6295 e4526bf5 2021-09-03 naddy case KEY_END:
6296 e4526bf5 2021-09-03 naddy s->selected = 0;
6297 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6298 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6299 e4526bf5 2021-09-03 naddy if (re == NULL)
6301 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6302 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6304 e4526bf5 2021-09-03 naddy if (n > 0)
6305 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6308 6458efa5 2020-11-24 stsp case KEY_UP:
6309 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6310 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6311 6458efa5 2020-11-24 stsp s->selected--;
6314 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6316 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6317 6458efa5 2020-11-24 stsp case CTRL('b'):
6318 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6319 34ba6917 2020-11-30 stsp s->selected = 0;
6320 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6323 6458efa5 2020-11-24 stsp case KEY_DOWN:
6324 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6325 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6326 6458efa5 2020-11-24 stsp s->selected++;
6329 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6330 6458efa5 2020-11-24 stsp /* can't scroll any further */
6332 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6334 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6335 6458efa5 2020-11-24 stsp case CTRL('f'):
6336 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6337 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6338 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6339 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6342 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6344 6458efa5 2020-11-24 stsp case CTRL('l'):
6345 8924d611 2020-12-26 stsp tog_free_refs();
6346 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
6349 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6350 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6352 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6353 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6354 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6360 6458efa5 2020-11-24 stsp return err;
6363 6458efa5 2020-11-24 stsp __dead static void
6364 6458efa5 2020-11-24 stsp usage_ref(void)
6367 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6368 6458efa5 2020-11-24 stsp getprogname());
6372 6458efa5 2020-11-24 stsp static const struct got_error *
6373 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6375 6458efa5 2020-11-24 stsp const struct got_error *error;
6376 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6377 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6378 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6380 6458efa5 2020-11-24 stsp struct tog_view *view;
6382 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6383 6458efa5 2020-11-24 stsp switch (ch) {
6385 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6386 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6387 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6391 e99e2d15 2020-11-24 naddy usage_ref();
6392 6458efa5 2020-11-24 stsp /* NOTREACHED */
6396 6458efa5 2020-11-24 stsp argc -= optind;
6397 6458efa5 2020-11-24 stsp argv += optind;
6399 6458efa5 2020-11-24 stsp if (argc > 1)
6400 e99e2d15 2020-11-24 naddy usage_ref();
6402 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6403 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6404 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6405 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6406 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6407 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6408 c156c7a4 2020-12-18 stsp goto done;
6409 6458efa5 2020-11-24 stsp if (worktree)
6410 6458efa5 2020-11-24 stsp repo_path =
6411 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6413 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6414 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6415 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6416 c156c7a4 2020-12-18 stsp goto done;
6420 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6421 6458efa5 2020-11-24 stsp if (error != NULL)
6422 6458efa5 2020-11-24 stsp goto done;
6424 6458efa5 2020-11-24 stsp init_curses();
6426 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6427 51a10b52 2020-12-26 stsp if (error)
6428 51a10b52 2020-12-26 stsp goto done;
6430 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6431 6458efa5 2020-11-24 stsp if (error)
6432 6458efa5 2020-11-24 stsp goto done;
6434 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6435 6458efa5 2020-11-24 stsp if (view == NULL) {
6436 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6437 6458efa5 2020-11-24 stsp goto done;
6440 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6441 6458efa5 2020-11-24 stsp if (error)
6442 6458efa5 2020-11-24 stsp goto done;
6444 6458efa5 2020-11-24 stsp if (worktree) {
6445 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6446 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6447 6458efa5 2020-11-24 stsp worktree = NULL;
6449 6458efa5 2020-11-24 stsp error = view_loop(view);
6451 6458efa5 2020-11-24 stsp free(repo_path);
6452 6458efa5 2020-11-24 stsp free(cwd);
6453 1d0f4054 2021-06-17 stsp if (repo) {
6454 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6455 1d0f4054 2021-06-17 stsp if (close_err)
6456 1d0f4054 2021-06-17 stsp error = close_err;
6458 51a10b52 2020-12-26 stsp tog_free_refs();
6459 6458efa5 2020-11-24 stsp return error;
6462 6458efa5 2020-11-24 stsp static void
6463 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6467 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6468 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6469 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6470 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6472 6879ba42 2020-10-01 naddy fputc('\n', fp);
6475 4ed7e80c 2018-05-20 stsp __dead static void
6476 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6478 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6480 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6481 6879ba42 2020-10-01 naddy getprogname());
6482 6879ba42 2020-10-01 naddy if (hflag) {
6483 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6484 6879ba42 2020-10-01 naddy list_commands(fp);
6486 6879ba42 2020-10-01 naddy exit(status);
6489 c2301be8 2018-04-30 stsp static char **
6490 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6492 ee85c5e8 2020-02-29 stsp va_list ap;
6493 c2301be8 2018-04-30 stsp char **argv;
6496 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6498 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6499 c2301be8 2018-04-30 stsp if (argv == NULL)
6500 c2301be8 2018-04-30 stsp err(1, "calloc");
6501 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6502 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6503 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6504 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6507 ee85c5e8 2020-02-29 stsp va_end(ap);
6508 c2301be8 2018-04-30 stsp return argv;
6512 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6513 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6514 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6515 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6516 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6518 ee85c5e8 2020-02-29 stsp static const struct got_error *
6519 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6521 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6522 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6523 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6524 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6525 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6526 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6527 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6529 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6530 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6531 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6533 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6534 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6535 ee85c5e8 2020-02-29 stsp goto done;
6537 ee85c5e8 2020-02-29 stsp if (worktree)
6538 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6540 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6541 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6542 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6543 ee85c5e8 2020-02-29 stsp goto done;
6546 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6547 ee85c5e8 2020-02-29 stsp if (error != NULL)
6548 ee85c5e8 2020-02-29 stsp goto done;
6550 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6551 ee85c5e8 2020-02-29 stsp repo, worktree);
6552 ee85c5e8 2020-02-29 stsp if (error)
6553 ee85c5e8 2020-02-29 stsp goto done;
6555 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6556 84de9106 2020-12-26 stsp if (error)
6557 84de9106 2020-12-26 stsp goto done;
6558 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6559 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6560 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6561 ee85c5e8 2020-02-29 stsp if (error)
6562 ee85c5e8 2020-02-29 stsp goto done;
6564 ee85c5e8 2020-02-29 stsp if (worktree) {
6565 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6566 ee85c5e8 2020-02-29 stsp worktree = NULL;
6569 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6570 ee85c5e8 2020-02-29 stsp if (error) {
6571 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6572 ee85c5e8 2020-02-29 stsp goto done;
6573 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6574 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6575 6879ba42 2020-10-01 naddy usage(1, 1);
6576 ee85c5e8 2020-02-29 stsp /* not reached */
6579 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6580 1d0f4054 2021-06-17 stsp if (error == NULL)
6581 1d0f4054 2021-06-17 stsp error = close_err;
6582 ee85c5e8 2020-02-29 stsp repo = NULL;
6584 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6585 ee85c5e8 2020-02-29 stsp if (error)
6586 ee85c5e8 2020-02-29 stsp goto done;
6588 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6590 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6591 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6593 1d0f4054 2021-06-17 stsp if (repo) {
6594 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6595 1d0f4054 2021-06-17 stsp if (error == NULL)
6596 1d0f4054 2021-06-17 stsp error = close_err;
6598 ee85c5e8 2020-02-29 stsp if (worktree)
6599 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6601 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6602 ee85c5e8 2020-02-29 stsp free(commit_id);
6603 ee85c5e8 2020-02-29 stsp free(cwd);
6604 ee85c5e8 2020-02-29 stsp free(repo_path);
6605 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6606 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6608 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6609 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6610 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6612 87670572 2020-12-26 naddy tog_free_refs();
6613 ee85c5e8 2020-02-29 stsp return error;
6617 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6619 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6620 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6621 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6622 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6623 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6624 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6625 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6628 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6630 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6631 9f7d7167 2018-04-29 stsp switch (ch) {
6633 9f7d7167 2018-04-29 stsp hflag = 1;
6636 53ccebc2 2019-07-30 stsp Vflag = 1;
6639 6879ba42 2020-10-01 naddy usage(hflag, 1);
6640 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6644 9f7d7167 2018-04-29 stsp argc -= optind;
6645 9f7d7167 2018-04-29 stsp argv += optind;
6646 9814e6a3 2020-09-27 naddy optind = 1;
6647 c2301be8 2018-04-30 stsp optreset = 1;
6649 53ccebc2 2019-07-30 stsp if (Vflag) {
6650 53ccebc2 2019-07-30 stsp got_version_print_str();
6651 6879ba42 2020-10-01 naddy return 0;
6654 4010e238 2020-12-04 stsp #ifndef PROFILE
6655 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6656 4010e238 2020-12-04 stsp NULL) == -1)
6657 4010e238 2020-12-04 stsp err(1, "pledge");
6660 c2301be8 2018-04-30 stsp if (argc == 0) {
6661 f29d3e89 2018-06-23 stsp if (hflag)
6662 6879ba42 2020-10-01 naddy usage(hflag, 0);
6663 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6664 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6666 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6670 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6671 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6672 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6673 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6674 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6680 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6681 ee85c5e8 2020-02-29 stsp if (argc != 1)
6682 6879ba42 2020-10-01 naddy usage(0, 1);
6683 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6684 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6686 ee85c5e8 2020-02-29 stsp if (hflag)
6687 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6689 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6693 b46c1e04 2020-09-20 naddy putchar('\n');
6694 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6696 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6697 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6698 a2f4a359 2020-02-28 stsp free(cmd_argv);
6701 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6702 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);