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 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
18 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 def2f970 2021-09-28 thomas #if defined(__FreeBSD__)
23 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 9f7d7167 2018-04-29 stsp #include <curses.h>
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
44 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
46 53ccebc2 2019-07-30 stsp #include "got_version.h"
47 9f7d7167 2018-04-29 stsp #include "got_error.h"
48 80ddbec8 2018-04-29 stsp #include "got_object.h"
49 80ddbec8 2018-04-29 stsp #include "got_reference.h"
50 80ddbec8 2018-04-29 stsp #include "got_repository.h"
51 80ddbec8 2018-04-29 stsp #include "got_diff.h"
52 511a516b 2018-05-19 stsp #include "got_opentemp.h"
53 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
54 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
55 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
56 a70480e0 2018-06-23 stsp #include "got_blame.h"
57 c2db6724 2019-01-04 stsp #include "got_privsep.h"
58 1dd54920 2019-05-11 stsp #include "got_path.h"
59 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 dd038bc6 2021-09-21 thomas.ad //#define update_panels() (0)
62 dd038bc6 2021-09-21 thomas.ad //#define doupdate() (0)
65 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
69 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
72 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
73 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
76 9f7d7167 2018-04-29 stsp #ifndef nitems
77 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 9f7d7167 2018-04-29 stsp struct tog_cmd {
81 c2301be8 2018-04-30 stsp const char *name;
82 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
89 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
90 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
91 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
96 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
97 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
99 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
100 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
101 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
102 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
103 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
104 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
107 d6b05b5b 2018-08-04 stsp enum tog_view_type {
108 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
109 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
110 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
111 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
112 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
115 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
117 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
118 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
119 ba4f502b 2018-08-04 stsp struct got_object_id *id;
120 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
123 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 ba4f502b 2018-08-04 stsp struct commit_queue {
125 ba4f502b 2018-08-04 stsp int ncommits;
126 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
129 f26dddb7 2019-11-08 stsp struct tog_color {
130 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
131 6d17833f 2019-11-08 stsp regex_t regex;
132 6d17833f 2019-11-08 stsp short colorpair;
134 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
136 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 11b20872 2019-11-08 stsp static const struct got_error *
140 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
142 51a10b52 2020-12-26 stsp const struct got_error *err;
144 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
145 3bfadbd4 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
148 51a10b52 2020-12-26 stsp return err;
150 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
154 51a10b52 2020-12-26 stsp static void
155 51a10b52 2020-12-26 stsp tog_free_refs(void)
157 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
158 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
159 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
161 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
164 51a10b52 2020-12-26 stsp static const struct got_error *
165 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
166 11b20872 2019-11-08 stsp int idx, short color)
168 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
169 11b20872 2019-11-08 stsp struct tog_color *tc;
170 11b20872 2019-11-08 stsp int regerr = 0;
172 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
173 11b20872 2019-11-08 stsp return NULL;
175 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
177 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
178 11b20872 2019-11-08 stsp if (tc == NULL)
179 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
180 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
181 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
182 11b20872 2019-11-08 stsp if (regerr) {
183 11b20872 2019-11-08 stsp static char regerr_msg[512];
184 11b20872 2019-11-08 stsp static char err_msg[512];
185 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
186 11b20872 2019-11-08 stsp sizeof(regerr_msg));
187 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
188 11b20872 2019-11-08 stsp regerr_msg);
189 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
191 11b20872 2019-11-08 stsp return err;
193 11b20872 2019-11-08 stsp tc->colorpair = idx;
194 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
195 11b20872 2019-11-08 stsp return NULL;
198 11b20872 2019-11-08 stsp static void
199 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
201 11b20872 2019-11-08 stsp struct tog_color *tc;
203 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
204 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
205 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
206 11b20872 2019-11-08 stsp regfree(&tc->regex);
211 11b20872 2019-11-08 stsp struct tog_color *
212 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
214 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
216 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
217 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
221 11b20872 2019-11-08 stsp return NULL;
225 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
227 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
228 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
229 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
230 11b20872 2019-11-08 stsp return COLOR_CYAN;
231 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
232 11b20872 2019-11-08 stsp return COLOR_YELLOW;
233 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
234 11b20872 2019-11-08 stsp return COLOR_GREEN;
235 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
236 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
237 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
238 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
239 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
240 91b8c405 2020-01-25 stsp return COLOR_CYAN;
241 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
242 11b20872 2019-11-08 stsp return COLOR_GREEN;
243 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
244 11b20872 2019-11-08 stsp return COLOR_GREEN;
245 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
246 11b20872 2019-11-08 stsp return COLOR_CYAN;
247 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
248 11b20872 2019-11-08 stsp return COLOR_YELLOW;
249 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
250 6458efa5 2020-11-24 stsp return COLOR_GREEN;
251 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
252 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
253 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
254 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
260 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
262 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
264 11b20872 2019-11-08 stsp if (val == NULL)
265 11b20872 2019-11-08 stsp return default_color_value(envvar);
267 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
268 11b20872 2019-11-08 stsp return COLOR_BLACK;
269 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
270 11b20872 2019-11-08 stsp return COLOR_RED;
271 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
274 11b20872 2019-11-08 stsp return COLOR_YELLOW;
275 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
276 11b20872 2019-11-08 stsp return COLOR_BLUE;
277 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
278 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
279 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
280 11b20872 2019-11-08 stsp return COLOR_CYAN;
281 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
282 11b20872 2019-11-08 stsp return COLOR_WHITE;
283 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
286 11b20872 2019-11-08 stsp return default_color_value(envvar);
290 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
291 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
292 3dbaef42 2020-11-24 stsp const char *label1, *label2;
294 15a087fe 2019-02-21 stsp int first_displayed_line;
295 15a087fe 2019-02-21 stsp int last_displayed_line;
297 15a087fe 2019-02-21 stsp int diff_context;
298 3dbaef42 2020-11-24 stsp int ignore_whitespace;
299 64453f7e 2020-11-21 stsp int force_text_diff;
300 15a087fe 2019-02-21 stsp struct got_repository *repo;
301 bddb1296 2019-11-08 stsp struct tog_colors colors;
302 fe621944 2020-11-10 stsp size_t nlines;
303 f44b1f58 2020-02-02 tracey off_t *line_offsets;
304 f44b1f58 2020-02-02 tracey int matched_line;
305 f44b1f58 2020-02-02 tracey int selected_line;
307 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
308 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
311 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
313 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
314 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
315 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
316 1a76625f 2018-10-22 stsp int commits_needed;
317 fb280deb 2021-08-30 stsp int load_all;
318 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
319 1a76625f 2018-10-22 stsp struct commit_queue *commits;
320 1a76625f 2018-10-22 stsp const char *in_repo_path;
321 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
322 1a76625f 2018-10-22 stsp struct got_repository *repo;
323 1a76625f 2018-10-22 stsp int log_complete;
324 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
325 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
326 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
327 13add988 2019-10-15 stsp int *searching;
328 13add988 2019-10-15 stsp int *search_next_done;
329 13add988 2019-10-15 stsp regex_t *regex;
332 1a76625f 2018-10-22 stsp struct tog_log_view_state {
333 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
334 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
335 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
336 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
337 b01e7d3b 2018-08-04 stsp int selected;
338 b01e7d3b 2018-08-04 stsp char *in_repo_path;
339 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
340 b672a97a 2020-01-27 stsp int log_branches;
341 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
342 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
343 1a76625f 2018-10-22 stsp sig_atomic_t quit;
344 1a76625f 2018-10-22 stsp pthread_t thread;
345 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
346 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
347 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
348 11b20872 2019-11-08 stsp struct tog_colors colors;
351 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
352 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
353 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
354 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
355 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
356 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
357 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
358 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
359 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
360 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
361 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
362 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
363 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
364 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
366 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
367 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
368 e9424729 2018-08-04 stsp int nlines;
370 e9424729 2018-08-04 stsp struct tog_view *view;
371 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
375 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
376 e9424729 2018-08-04 stsp const char *path;
377 e9424729 2018-08-04 stsp struct got_repository *repo;
378 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
379 e9424729 2018-08-04 stsp int *complete;
380 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
381 fc06ba56 2019-08-22 stsp void *cancel_arg;
384 e9424729 2018-08-04 stsp struct tog_blame {
386 be659d10 2020-11-18 stsp off_t filesize;
387 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
388 6fcac457 2018-11-19 stsp int nlines;
389 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
390 e9424729 2018-08-04 stsp pthread_t thread;
391 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
392 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
393 e9424729 2018-08-04 stsp const char *path;
396 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
397 7cbe629d 2018-08-04 stsp int first_displayed_line;
398 7cbe629d 2018-08-04 stsp int last_displayed_line;
399 7cbe629d 2018-08-04 stsp int selected_line;
400 7cbe629d 2018-08-04 stsp int blame_complete;
403 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
404 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
405 e5a0f69f 2018-08-18 stsp char *path;
406 7cbe629d 2018-08-04 stsp struct got_repository *repo;
407 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
408 e9424729 2018-08-04 stsp struct tog_blame blame;
409 6c4c42e0 2019-06-24 stsp int matched_line;
410 11b20872 2019-11-08 stsp struct tog_colors colors;
413 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
414 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
415 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
416 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
417 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
418 ad80ab7b 2018-08-04 stsp int selected;
421 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
423 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
424 ad80ab7b 2018-08-04 stsp char *tree_label;
425 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
426 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
427 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
428 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
429 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
430 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
431 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
432 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
433 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
434 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
435 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
436 6458efa5 2020-11-24 stsp struct tog_colors colors;
439 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
440 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
441 6458efa5 2020-11-24 stsp struct got_reference *ref;
445 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
447 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
448 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
449 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
450 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
451 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
452 3bfadbd4 2021-11-20 thomas int nrefs, ndisplayed, selected, show_ids, sort_by_date;
453 6458efa5 2020-11-24 stsp struct got_repository *repo;
454 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
455 bddb1296 2019-11-08 stsp struct tog_colors colors;
459 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
461 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
462 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
463 669b5ffa 2018-10-07 stsp * there is enough screen estate.
465 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
466 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
468 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
469 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
470 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
472 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
473 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
475 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
477 cc3c9aac 2018-08-01 stsp struct tog_view {
478 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
479 26ed57b2 2018-05-19 stsp WINDOW *window;
480 26ed57b2 2018-05-19 stsp PANEL *panel;
481 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
482 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
483 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
485 669b5ffa 2018-10-07 stsp struct tog_view *parent;
486 669b5ffa 2018-10-07 stsp struct tog_view *child;
489 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
490 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
491 e78dc838 2020-12-04 stsp * between parent and child.
492 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
493 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
494 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
495 e78dc838 2020-12-04 stsp * situations.
497 e78dc838 2020-12-04 stsp int focus_child;
499 5dc9f4bc 2018-08-04 stsp /* type-specific state */
500 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
502 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
503 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
504 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
505 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
506 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
509 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
510 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
511 e78dc838 2020-12-04 stsp struct tog_view *, int);
512 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
514 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
515 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
516 c0c4acc8 2021-01-24 stsp int search_started;
517 60493ae3 2019-06-20 stsp int searching;
518 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
519 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
520 60493ae3 2019-06-20 stsp int search_next_done;
521 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
522 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
523 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
524 1803e47f 2019-06-22 stsp regex_t regex;
525 41605754 2020-11-12 stsp regmatch_t regmatch;
528 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
529 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
530 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
531 78756c87 2020-11-24 stsp struct got_repository *);
532 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
533 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
534 e78dc838 2020-12-04 stsp struct tog_view *, int);
535 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
536 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
537 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
539 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
540 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
541 78756c87 2020-11-24 stsp const char *, const char *, int);
542 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
543 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
544 e78dc838 2020-12-04 stsp struct tog_view *, int);
545 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
546 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
547 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
549 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
550 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
551 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
552 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
553 e78dc838 2020-12-04 stsp struct tog_view *, int);
554 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
555 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
556 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
558 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
559 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
560 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
561 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
562 e78dc838 2020-12-04 stsp struct tog_view *, int);
563 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
564 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
565 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
567 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
568 6458efa5 2020-11-24 stsp struct got_repository *);
569 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
570 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
571 e78dc838 2020-12-04 stsp struct tog_view *, int);
572 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
573 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
574 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
576 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
577 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
578 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
580 25791caa 2018-10-24 stsp static void
581 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
583 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
586 83baff54 2019-08-12 stsp static void
587 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
589 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
592 61266923 2020-01-14 stsp static void
593 61266923 2020-01-14 stsp tog_sigcont(int signo)
595 61266923 2020-01-14 stsp tog_sigcont_received = 1;
598 e5a0f69f 2018-08-18 stsp static const struct got_error *
599 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
601 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
603 669b5ffa 2018-10-07 stsp if (view->child) {
604 669b5ffa 2018-10-07 stsp view_close(view->child);
605 669b5ffa 2018-10-07 stsp view->child = NULL;
607 e5a0f69f 2018-08-18 stsp if (view->close)
608 e5a0f69f 2018-08-18 stsp err = view->close(view);
609 ea5e7bb5 2018-08-01 stsp if (view->panel)
610 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
611 ea5e7bb5 2018-08-01 stsp if (view->window)
612 ea5e7bb5 2018-08-01 stsp delwin(view->window);
613 ea5e7bb5 2018-08-01 stsp free(view);
614 e5a0f69f 2018-08-18 stsp return err;
617 ea5e7bb5 2018-08-01 stsp static struct tog_view *
618 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
619 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
621 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
623 ea5e7bb5 2018-08-01 stsp if (view == NULL)
624 ea5e7bb5 2018-08-01 stsp return NULL;
626 d6b05b5b 2018-08-04 stsp view->type = type;
627 f7d12f7e 2018-08-01 stsp view->lines = LINES;
628 f7d12f7e 2018-08-01 stsp view->cols = COLS;
629 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
630 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
631 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
632 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
633 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
634 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
635 96a765a8 2018-08-04 stsp view_close(view);
636 ea5e7bb5 2018-08-01 stsp return NULL;
638 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
639 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
640 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
641 96a765a8 2018-08-04 stsp view_close(view);
642 ea5e7bb5 2018-08-01 stsp return NULL;
645 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
646 ea5e7bb5 2018-08-01 stsp return view;
650 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
652 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
654 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
657 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
659 5c60c32a 2018-10-18 stsp static const struct got_error *
660 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
662 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
664 5c60c32a 2018-10-18 stsp view->begin_y = 0;
665 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
666 5c60c32a 2018-10-18 stsp view->nlines = LINES;
667 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
668 5c60c32a 2018-10-18 stsp view->lines = LINES;
669 5c60c32a 2018-10-18 stsp view->cols = COLS;
670 5c60c32a 2018-10-18 stsp err = view_resize(view);
672 5c60c32a 2018-10-18 stsp return err;
674 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
675 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
677 5c60c32a 2018-10-18 stsp return NULL;
680 5c60c32a 2018-10-18 stsp static const struct got_error *
681 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
683 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
685 5c60c32a 2018-10-18 stsp view->begin_x = 0;
686 5c60c32a 2018-10-18 stsp view->begin_y = 0;
687 5c60c32a 2018-10-18 stsp view->nlines = LINES;
688 5c60c32a 2018-10-18 stsp view->ncols = COLS;
689 5c60c32a 2018-10-18 stsp view->lines = LINES;
690 5c60c32a 2018-10-18 stsp view->cols = COLS;
691 5c60c32a 2018-10-18 stsp err = view_resize(view);
693 5c60c32a 2018-10-18 stsp return err;
695 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
696 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
698 5c60c32a 2018-10-18 stsp return NULL;
702 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
704 5c60c32a 2018-10-18 stsp return view->parent == NULL;
707 4d8c2215 2018-08-19 stsp static const struct got_error *
708 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
710 f7d12f7e 2018-08-01 stsp int nlines, ncols;
712 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
713 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
715 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
717 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
718 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
720 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
722 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
723 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
724 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
725 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
726 25791caa 2018-10-24 stsp wclear(view->window);
728 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
729 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
730 0cf4efb1 2018-09-29 stsp view->lines = LINES;
731 0cf4efb1 2018-09-29 stsp view->cols = COLS;
733 6e3e5d9c 2018-10-18 stsp if (view->child) {
734 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
735 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
736 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
737 5c60c32a 2018-10-18 stsp if (view->child->focussed)
738 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
740 5c60c32a 2018-10-18 stsp show_panel(view->panel);
742 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
743 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
747 5c60c32a 2018-10-18 stsp return NULL;
750 669b5ffa 2018-10-07 stsp static const struct got_error *
751 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
753 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
755 669b5ffa 2018-10-07 stsp if (view->child == NULL)
756 669b5ffa 2018-10-07 stsp return NULL;
758 669b5ffa 2018-10-07 stsp err = view_close(view->child);
759 669b5ffa 2018-10-07 stsp view->child = NULL;
760 669b5ffa 2018-10-07 stsp return err;
763 72a9cb46 2020-12-03 stsp static void
764 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
766 669b5ffa 2018-10-07 stsp view->child = child;
767 669b5ffa 2018-10-07 stsp child->parent = view;
771 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
773 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
776 34bc9ec9 2019-02-22 stsp static void
777 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
779 25791caa 2018-10-24 stsp int cols, lines;
780 25791caa 2018-10-24 stsp struct winsize size;
782 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
783 25791caa 2018-10-24 stsp cols = 80; /* Default */
784 25791caa 2018-10-24 stsp lines = 24;
786 25791caa 2018-10-24 stsp cols = size.ws_col;
787 25791caa 2018-10-24 stsp lines = size.ws_row;
789 25791caa 2018-10-24 stsp resize_term(lines, cols);
792 2b49a8ae 2019-06-22 stsp static const struct got_error *
793 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
795 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
796 2b49a8ae 2019-06-22 stsp char pattern[1024];
799 c0c4acc8 2021-01-24 stsp if (view->search_started) {
800 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
801 c0c4acc8 2021-01-24 stsp view->searching = 0;
802 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
804 c0c4acc8 2021-01-24 stsp view->search_started = 0;
806 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
807 2b49a8ae 2019-06-22 stsp return NULL;
809 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
810 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
812 2b49a8ae 2019-06-22 stsp nocbreak();
814 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
817 2b49a8ae 2019-06-22 stsp if (ret == ERR)
818 2b49a8ae 2019-06-22 stsp return NULL;
820 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
821 7c32bd05 2019-06-22 stsp err = view->search_start(view);
823 7c32bd05 2019-06-22 stsp regfree(&view->regex);
824 7c32bd05 2019-06-22 stsp return err;
826 c0c4acc8 2021-01-24 stsp view->search_started = 1;
827 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
828 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
829 2b49a8ae 2019-06-22 stsp view->search_next(view);
832 2b49a8ae 2019-06-22 stsp return NULL;
835 0cf4efb1 2018-09-29 stsp static const struct got_error *
836 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
837 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
839 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
840 669b5ffa 2018-10-07 stsp struct tog_view *v;
841 1a76625f 2018-10-22 stsp int ch, errcode;
843 e5a0f69f 2018-08-18 stsp *new = NULL;
845 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
846 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
847 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
848 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
850 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
851 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
852 82954512 2020-02-03 stsp if (errcode)
853 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
854 82954512 2020-02-03 stsp "pthread_mutex_unlock");
855 3da8ef85 2021-09-21 stsp sched_yield();
856 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
857 82954512 2020-02-03 stsp if (errcode)
858 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
859 82954512 2020-02-03 stsp "pthread_mutex_lock");
860 60493ae3 2019-06-20 stsp view->search_next(view);
861 60493ae3 2019-06-20 stsp return NULL;
864 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
865 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
866 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
867 1a76625f 2018-10-22 stsp if (errcode)
868 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
869 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
870 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
871 1a76625f 2018-10-22 stsp if (errcode)
872 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
873 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
875 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
876 25791caa 2018-10-24 stsp tog_resizeterm();
877 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
878 61266923 2020-01-14 stsp tog_sigcont_received = 0;
879 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
880 25791caa 2018-10-24 stsp err = view_resize(v);
882 25791caa 2018-10-24 stsp return err;
883 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
885 25791caa 2018-10-24 stsp return err;
886 cdfcfb03 2020-12-06 stsp if (v->child) {
887 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
889 cdfcfb03 2020-12-06 stsp return err;
890 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
891 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
893 cdfcfb03 2020-12-06 stsp return err;
898 e5a0f69f 2018-08-18 stsp switch (ch) {
900 1e37a5c2 2019-05-12 jcs if (view->child) {
901 e78dc838 2020-12-04 stsp view->focussed = 0;
902 e78dc838 2020-12-04 stsp view->child->focussed = 1;
903 e78dc838 2020-12-04 stsp view->focus_child = 1;
904 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
905 e78dc838 2020-12-04 stsp view->focussed = 0;
906 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
907 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
911 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
912 9970f7fc 2020-12-03 stsp view->dying = 1;
918 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
919 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
921 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
922 e78dc838 2020-12-04 stsp view->focussed = 0;
923 e78dc838 2020-12-04 stsp view->child->focussed = 1;
924 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
926 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
929 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
930 9970f7fc 2020-12-03 stsp KEY_RESIZE);
932 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
933 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
934 e78dc838 2020-12-04 stsp view->focussed = 1;
935 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
937 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
941 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
944 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
947 60493ae3 2019-06-20 stsp if (view->search_start)
948 2b49a8ae 2019-06-22 stsp view_search_start(view);
950 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
954 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
955 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
956 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
957 60493ae3 2019-06-20 stsp view->search_next_done = 0;
958 60493ae3 2019-06-20 stsp view->search_next(view);
960 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
963 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
967 e5a0f69f 2018-08-18 stsp return err;
971 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
973 0cf4efb1 2018-09-29 stsp PANEL *panel;
974 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
976 669b5ffa 2018-10-07 stsp if (view->parent)
977 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
979 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
980 0cf4efb1 2018-09-29 stsp if (panel == NULL)
983 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
984 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
985 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
989 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
991 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
992 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
994 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
996 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
999 669b5ffa 2018-10-07 stsp return view->focussed;
1002 bcbd79e2 2018-08-19 stsp static const struct got_error *
1003 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1005 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1006 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1007 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1008 fd823528 2018-10-22 stsp int fast_refresh = 10;
1009 1a76625f 2018-10-22 stsp int done = 0, errcode;
1011 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1012 1a76625f 2018-10-22 stsp if (errcode)
1013 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1015 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1016 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1018 1004088d 2018-09-29 stsp view->focussed = 1;
1019 878940b7 2018-09-29 stsp err = view->show(view);
1021 0cf4efb1 2018-09-29 stsp return err;
1022 0cf4efb1 2018-09-29 stsp update_panels();
1023 0cf4efb1 2018-09-29 stsp doupdate();
1024 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1025 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1026 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1027 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1029 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1032 9970f7fc 2020-12-03 stsp if (view->dying) {
1033 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1035 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1036 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1038 e78dc838 2020-12-04 stsp else if (view->parent)
1039 669b5ffa 2018-10-07 stsp prev = view->parent;
1041 e78dc838 2020-12-04 stsp if (view->parent) {
1042 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1043 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1045 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1047 9970f7fc 2020-12-03 stsp err = view_close(view);
1049 e5a0f69f 2018-08-18 stsp goto done;
1051 e78dc838 2020-12-04 stsp view = NULL;
1052 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1053 e78dc838 2020-12-04 stsp if (v->focussed)
1056 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1057 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1059 e78dc838 2020-12-04 stsp view = prev;
1060 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1061 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1062 e78dc838 2020-12-04 stsp tog_view_list_head);
1064 e78dc838 2020-12-04 stsp if (view) {
1065 e78dc838 2020-12-04 stsp if (view->focus_child) {
1066 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1067 e78dc838 2020-12-04 stsp view = view->child;
1069 e78dc838 2020-12-04 stsp view->focussed = 1;
1073 bcbd79e2 2018-08-19 stsp if (new_view) {
1074 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1075 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1076 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1077 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1079 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1080 86c66b02 2018-10-18 stsp err = view_close(v);
1082 86c66b02 2018-10-18 stsp goto done;
1085 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1086 fed7eaa8 2018-10-24 stsp view = new_view;
1088 669b5ffa 2018-10-07 stsp if (view) {
1089 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1090 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1091 e78dc838 2020-12-04 stsp view = view->child;
1093 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1094 e78dc838 2020-12-04 stsp view = view->parent;
1096 e78dc838 2020-12-04 stsp show_panel(view->panel);
1097 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1098 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1099 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1100 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1102 1a76625f 2018-10-22 stsp goto done;
1104 669b5ffa 2018-10-07 stsp err = view->show(view);
1106 1a76625f 2018-10-22 stsp goto done;
1107 669b5ffa 2018-10-07 stsp if (view->child) {
1108 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1110 1a76625f 2018-10-22 stsp goto done;
1112 1a76625f 2018-10-22 stsp update_panels();
1113 1a76625f 2018-10-22 stsp doupdate();
1117 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1118 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1119 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1120 e5a0f69f 2018-08-18 stsp view_close(view);
1123 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1124 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1125 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1127 e5a0f69f 2018-08-18 stsp return err;
1130 4ed7e80c 2018-05-20 stsp __dead static void
1131 9f7d7167 2018-04-29 stsp usage_log(void)
1134 c70c5802 2018-08-01 stsp fprintf(stderr,
1135 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1136 9f7d7167 2018-04-29 stsp getprogname());
1140 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1141 80ddbec8 2018-04-29 stsp static const struct got_error *
1142 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1144 00dfcb92 2018-06-11 stsp char *vis = NULL;
1145 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1147 963b370f 2018-05-20 stsp *ws = NULL;
1148 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1149 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1150 00dfcb92 2018-06-11 stsp int vislen;
1151 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1152 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1154 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1155 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1157 00dfcb92 2018-06-11 stsp return err;
1158 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1159 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1160 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1161 a7f50699 2018-06-11 stsp goto done;
1165 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1166 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1167 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1168 a7f50699 2018-06-11 stsp goto done;
1171 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1172 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1174 00dfcb92 2018-06-11 stsp free(vis);
1175 963b370f 2018-05-20 stsp if (err) {
1176 963b370f 2018-05-20 stsp free(*ws);
1177 963b370f 2018-05-20 stsp *ws = NULL;
1178 963b370f 2018-05-20 stsp *wlen = 0;
1180 963b370f 2018-05-20 stsp return err;
1183 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1184 963b370f 2018-05-20 stsp static const struct got_error *
1185 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1186 27a741e5 2019-09-11 stsp int col_tab_align)
1188 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1189 963b370f 2018-05-20 stsp int cols = 0;
1190 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1191 963b370f 2018-05-20 stsp size_t wlen;
1194 963b370f 2018-05-20 stsp *wlinep = NULL;
1195 b700b5d6 2018-07-10 stsp *widthp = 0;
1197 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1199 963b370f 2018-05-20 stsp return err;
1201 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1202 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1205 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1206 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1211 27a741e5 2019-09-11 stsp while (i < wlen) {
1212 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1214 27a741e5 2019-09-11 stsp if (width == 0) {
1219 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1220 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1222 27a741e5 2019-09-11 stsp cols += width;
1224 27a741e5 2019-09-11 stsp } else if (width == -1) {
1225 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1226 27a741e5 2019-09-11 stsp width = TABSIZE -
1227 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1229 748d5cab 2020-12-14 naddy width = 1;
1230 748d5cab 2020-12-14 naddy wline[i] = L'.';
1232 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1234 748d5cab 2020-12-14 naddy cols += width;
1237 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1238 963b370f 2018-05-20 stsp goto done;
1241 963b370f 2018-05-20 stsp wline[i] = L'\0';
1242 b700b5d6 2018-07-10 stsp if (widthp)
1243 b700b5d6 2018-07-10 stsp *widthp = cols;
1246 963b370f 2018-05-20 stsp free(wline);
1248 963b370f 2018-05-20 stsp *wlinep = wline;
1249 963b370f 2018-05-20 stsp return err;
1252 8b473291 2019-02-21 stsp static const struct got_error*
1253 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1254 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1256 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1257 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1259 8b473291 2019-02-21 stsp const char *name;
1261 8b473291 2019-02-21 stsp *refs_str = NULL;
1263 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1264 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1265 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1268 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1269 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1271 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1272 8b473291 2019-02-21 stsp name += 5;
1273 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1275 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1276 8b473291 2019-02-21 stsp name += 6;
1277 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1278 8b473291 2019-02-21 stsp name += 8;
1279 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1280 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1283 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1286 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1287 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1288 5d844a1e 2019-08-13 stsp if (err) {
1289 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1290 48cae60d 2020-09-22 stsp free(ref_id);
1293 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1294 5d844a1e 2019-08-13 stsp err = NULL;
1295 5d844a1e 2019-08-13 stsp tag = NULL;
1298 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1299 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1300 48cae60d 2020-09-22 stsp free(ref_id);
1302 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1303 52b5abe1 2019-08-13 stsp if (cmp != 0)
1305 8b473291 2019-02-21 stsp s = *refs_str;
1306 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1307 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1308 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1310 8b473291 2019-02-21 stsp *refs_str = NULL;
1316 8b473291 2019-02-21 stsp return err;
1319 963b370f 2018-05-20 stsp static const struct got_error *
1320 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1321 27a741e5 2019-09-11 stsp int col_tab_align)
1323 e6b8b890 2020-12-29 naddy char *smallerthan;
1325 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1326 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1327 5813d178 2019-03-09 stsp author = smallerthan + 1;
1328 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1329 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1332 5813d178 2019-03-09 stsp static const struct got_error *
1333 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1334 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1335 8fdc79fe 2020-12-01 naddy int author_display_cols)
1337 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1338 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1339 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1340 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1341 5813d178 2019-03-09 stsp char *author = NULL;
1342 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1343 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1344 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1345 bb737323 2018-05-20 stsp int col, limit;
1346 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1347 ccb26ccd 2018-11-05 stsp struct tm tm;
1348 45d799e2 2018-12-23 stsp time_t committer_time;
1349 11b20872 2019-11-08 stsp struct tog_color *tc;
1351 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1352 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1353 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1354 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1355 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1357 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1358 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1360 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1361 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1363 11b20872 2019-11-08 stsp wattr_on(view->window,
1364 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1365 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1367 11b20872 2019-11-08 stsp wattr_off(view->window,
1368 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1369 27a741e5 2019-09-11 stsp col = limit;
1370 b39d25c7 2018-07-10 stsp if (col > avail)
1371 b39d25c7 2018-07-10 stsp goto done;
1373 6570a66d 2019-11-08 stsp if (avail >= 120) {
1374 6570a66d 2019-11-08 stsp char *id_str;
1375 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1377 6570a66d 2019-11-08 stsp goto done;
1378 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1380 11b20872 2019-11-08 stsp wattr_on(view->window,
1381 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1382 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1384 11b20872 2019-11-08 stsp wattr_off(view->window,
1385 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1386 6570a66d 2019-11-08 stsp free(id_str);
1388 6570a66d 2019-11-08 stsp if (col > avail)
1389 6570a66d 2019-11-08 stsp goto done;
1392 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1393 5813d178 2019-03-09 stsp if (author == NULL) {
1394 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1395 80ddbec8 2018-04-29 stsp goto done;
1397 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1399 bb737323 2018-05-20 stsp goto done;
1400 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1402 11b20872 2019-11-08 stsp wattr_on(view->window,
1403 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1404 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1406 11b20872 2019-11-08 stsp wattr_off(view->window,
1407 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1408 bb737323 2018-05-20 stsp col += author_width;
1409 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1410 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1412 bb737323 2018-05-20 stsp author_width++;
1414 9c2eaf34 2018-05-20 stsp if (col > avail)
1415 9c2eaf34 2018-05-20 stsp goto done;
1417 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1419 6d9fbc00 2018-04-29 stsp goto done;
1420 bb737323 2018-05-20 stsp logmsg = logmsg0;
1421 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1423 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1424 bb737323 2018-05-20 stsp if (newline)
1425 bb737323 2018-05-20 stsp *newline = '\0';
1426 bb737323 2018-05-20 stsp limit = avail - col;
1427 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1429 bb737323 2018-05-20 stsp goto done;
1430 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1431 bb737323 2018-05-20 stsp col += logmsg_width;
1432 27a741e5 2019-09-11 stsp while (col < avail) {
1433 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1437 80ddbec8 2018-04-29 stsp free(logmsg0);
1438 bb737323 2018-05-20 stsp free(wlogmsg);
1439 5813d178 2019-03-09 stsp free(author);
1440 bb737323 2018-05-20 stsp free(wauthor);
1441 80ddbec8 2018-04-29 stsp free(line);
1442 80ddbec8 2018-04-29 stsp return err;
1445 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1446 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1447 899d86c2 2018-05-10 stsp struct got_object_id *id)
1449 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1451 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1452 80ddbec8 2018-04-29 stsp if (entry == NULL)
1453 899d86c2 2018-05-10 stsp return NULL;
1455 899d86c2 2018-05-10 stsp entry->id = id;
1456 99db9666 2018-05-07 stsp entry->commit = commit;
1457 899d86c2 2018-05-10 stsp return entry;
1460 99db9666 2018-05-07 stsp static void
1461 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1463 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1465 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1466 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1467 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1468 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1469 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1470 99db9666 2018-05-07 stsp free(entry);
1473 99db9666 2018-05-07 stsp static void
1474 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1476 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1477 99db9666 2018-05-07 stsp pop_commit(commits);
1480 c4972b91 2018-05-07 stsp static const struct got_error *
1481 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1482 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1484 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1485 13add988 2019-10-15 stsp regmatch_t regmatch;
1486 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1488 13add988 2019-10-15 stsp *have_match = 0;
1490 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1492 13add988 2019-10-15 stsp return err;
1494 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1496 13add988 2019-10-15 stsp goto done;
1498 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1499 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1500 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1501 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1502 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1503 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1504 13add988 2019-10-15 stsp *have_match = 1;
1506 13add988 2019-10-15 stsp free(id_str);
1507 13add988 2019-10-15 stsp free(logmsg);
1508 13add988 2019-10-15 stsp return err;
1511 13add988 2019-10-15 stsp static const struct got_error *
1512 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1514 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1517 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1518 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1519 1a76625f 2018-10-22 stsp * while updating the display.
1522 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1523 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1524 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1525 1a76625f 2018-10-22 stsp int errcode;
1527 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1528 4e0d2870 2020-12-07 naddy NULL, NULL);
1529 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1532 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1535 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1536 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1537 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1541 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1542 1a76625f 2018-10-22 stsp if (errcode) {
1543 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1544 13add988 2019-10-15 stsp "pthread_mutex_lock");
1548 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1549 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1550 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1552 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1553 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1554 7c1452c1 2020-03-26 stsp int have_match;
1555 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1558 7c1452c1 2020-03-26 stsp if (have_match)
1559 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1562 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1563 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1564 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1565 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1568 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1570 9ba79e04 2018-06-11 stsp return err;
1573 2b779855 2020-12-05 naddy static void
1574 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1576 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1577 2b779855 2020-12-05 naddy int ncommits = 0;
1579 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1580 2b779855 2020-12-05 naddy while (entry) {
1581 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1582 2b779855 2020-12-05 naddy s->selected_entry = entry;
1585 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1586 2b779855 2020-12-05 naddy ncommits++;
1590 0553a4e3 2018-04-30 stsp static const struct got_error *
1591 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1593 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1594 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1595 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1596 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1597 60493ae3 2019-06-20 stsp int width;
1598 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1599 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1600 8b473291 2019-02-21 stsp char *refs_str = NULL;
1601 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1602 11b20872 2019-11-08 stsp struct tog_color *tc;
1603 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1605 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1606 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1607 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1608 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1610 ecb28ae0 2018-07-16 stsp return err;
1611 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1612 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1613 d2075bf3 2020-12-25 stsp if (refs) {
1614 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1615 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1617 d2075bf3 2020-12-25 stsp goto done;
1621 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1622 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1624 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1625 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1626 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1627 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1628 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1629 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1630 8f4ed634 2020-03-26 stsp goto done;
1633 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1635 f9686aa5 2020-03-27 stsp if (view->searching) {
1636 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1637 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1638 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1639 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1640 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1641 f9686aa5 2020-03-27 stsp search_str = "searching...";
1644 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1645 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1646 f9686aa5 2020-03-27 stsp search_str ? search_str :
1647 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1648 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1649 8f4ed634 2020-03-26 stsp goto done;
1653 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1654 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1655 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1656 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1657 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1658 1a76625f 2018-10-22 stsp header = NULL;
1659 1a76625f 2018-10-22 stsp goto done;
1661 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1662 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1663 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1664 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1665 1a76625f 2018-10-22 stsp header = NULL;
1666 1a76625f 2018-10-22 stsp goto done;
1668 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1670 1a76625f 2018-10-22 stsp goto done;
1672 2814baeb 2018-08-01 stsp werase(view->window);
1674 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1675 a3404814 2018-09-02 stsp wstandout(view->window);
1676 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1678 11b20872 2019-11-08 stsp wattr_on(view->window,
1679 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1680 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1682 11b20872 2019-11-08 stsp wattr_off(view->window,
1683 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1684 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1685 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1688 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1689 a3404814 2018-09-02 stsp wstandend(view->window);
1690 ecb28ae0 2018-07-16 stsp free(wline);
1691 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1692 1a76625f 2018-10-22 stsp goto done;
1694 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1695 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1696 5813d178 2019-03-09 stsp ncommits = 0;
1697 5813d178 2019-03-09 stsp while (entry) {
1698 5813d178 2019-03-09 stsp char *author;
1699 5813d178 2019-03-09 stsp wchar_t *wauthor;
1700 5813d178 2019-03-09 stsp int width;
1701 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1703 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1704 5813d178 2019-03-09 stsp if (author == NULL) {
1705 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1706 5813d178 2019-03-09 stsp goto done;
1708 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1709 27a741e5 2019-09-11 stsp date_display_cols);
1710 5813d178 2019-03-09 stsp if (author_cols < width)
1711 5813d178 2019-03-09 stsp author_cols = width;
1712 5813d178 2019-03-09 stsp free(wauthor);
1713 5813d178 2019-03-09 stsp free(author);
1714 7ca04879 2019-10-19 stsp ncommits++;
1715 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1718 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1719 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1720 867c6645 2018-07-10 stsp ncommits = 0;
1721 899d86c2 2018-05-10 stsp while (entry) {
1722 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1724 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1725 2814baeb 2018-08-01 stsp wstandout(view->window);
1726 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1727 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1728 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1729 2814baeb 2018-08-01 stsp wstandend(view->window);
1731 60493ae3 2019-06-20 stsp goto done;
1732 0553a4e3 2018-04-30 stsp ncommits++;
1733 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1734 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1737 1a57306a 2018-09-02 stsp view_vborder(view);
1738 dd038bc6 2021-09-21 thomas.ad update_panels();
1739 dd038bc6 2021-09-21 thomas.ad doupdate();
1741 1a76625f 2018-10-22 stsp free(id_str);
1742 8b473291 2019-02-21 stsp free(refs_str);
1743 1a76625f 2018-10-22 stsp free(ncommits_str);
1744 1a76625f 2018-10-22 stsp free(header);
1745 80ddbec8 2018-04-29 stsp return err;
1748 07b55e75 2018-05-10 stsp static void
1749 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1751 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1752 07b55e75 2018-05-10 stsp int nscrolled = 0;
1754 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1755 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1758 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1759 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1760 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1761 07b55e75 2018-05-10 stsp if (entry) {
1762 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1763 07b55e75 2018-05-10 stsp nscrolled++;
1768 aa075928 2018-05-10 stsp static const struct got_error *
1769 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1771 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1772 5e224a3e 2019-02-22 stsp int errcode;
1774 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1776 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1777 ffe38506 2020-12-01 naddy if (ta->log_complete)
1780 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1781 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1782 7aafa0d1 2019-02-22 stsp if (errcode)
1783 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1784 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1787 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1788 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1790 7c1452c1 2020-03-26 stsp if (!wait)
1793 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1794 ffe38506 2020-12-01 naddy show_log_view(view);
1795 7c1452c1 2020-03-26 stsp update_panels();
1796 7c1452c1 2020-03-26 stsp doupdate();
1798 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1799 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1800 82954512 2020-02-03 stsp if (errcode)
1801 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1802 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1804 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1805 ffe38506 2020-12-01 naddy show_log_view(view);
1806 7c1452c1 2020-03-26 stsp update_panels();
1807 7c1452c1 2020-03-26 stsp doupdate();
1810 5e224a3e 2019-02-22 stsp return NULL;
1813 5e224a3e 2019-02-22 stsp static const struct got_error *
1814 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1816 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1817 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1818 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1819 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1821 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1822 5e224a3e 2019-02-22 stsp return NULL;
1824 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1825 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1826 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1828 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1830 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1831 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1833 5e224a3e 2019-02-22 stsp return err;
1837 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1838 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1841 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1843 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1844 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1846 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1847 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1849 dd0a52c1 2018-05-20 stsp return err;
1852 cd0acaa7 2018-05-20 stsp static const struct got_error *
1853 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1854 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1855 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1857 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1858 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1859 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1861 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1862 15a94983 2018-12-23 stsp if (diff_view == NULL)
1863 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1865 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1866 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1867 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1868 e5a0f69f 2018-08-18 stsp if (err == NULL)
1869 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1870 cd0acaa7 2018-05-20 stsp return err;
1873 80ddbec8 2018-04-29 stsp static const struct got_error *
1874 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1875 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1877 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1879 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1880 941e9f74 2019-05-21 stsp if (parent == NULL)
1881 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1883 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1884 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1885 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1886 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1887 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1888 941e9f74 2019-05-21 stsp s->tree = subtree;
1889 941e9f74 2019-05-21 stsp s->selected = 0;
1890 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1891 941e9f74 2019-05-21 stsp return NULL;
1894 941e9f74 2019-05-21 stsp static const struct got_error *
1895 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1896 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1898 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1899 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1900 941e9f74 2019-05-21 stsp const char *p;
1901 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1903 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1905 941e9f74 2019-05-21 stsp while (*p) {
1906 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1907 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1908 56e0773d 2019-11-28 stsp char *te_name;
1910 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1913 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1914 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1915 941e9f74 2019-05-21 stsp if (slash == NULL)
1916 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1918 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1919 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1920 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1923 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1924 56e0773d 2019-11-28 stsp if (te == NULL) {
1925 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1926 56e0773d 2019-11-28 stsp free(te_name);
1929 56e0773d 2019-11-28 stsp free(te_name);
1930 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1932 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1933 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1935 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1936 941e9f74 2019-05-21 stsp if (slash)
1937 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1939 941e9f74 2019-05-21 stsp subpath = strdup(path);
1940 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1941 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1945 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1950 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1951 941e9f74 2019-05-21 stsp free(tree_id);
1955 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1956 941e9f74 2019-05-21 stsp if (err) {
1957 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1960 941e9f74 2019-05-21 stsp if (slash == NULL)
1962 941e9f74 2019-05-21 stsp free(subpath);
1963 941e9f74 2019-05-21 stsp subpath = NULL;
1964 941e9f74 2019-05-21 stsp p = slash;
1967 941e9f74 2019-05-21 stsp free(subpath);
1968 1a76625f 2018-10-22 stsp return err;
1971 61266923 2020-01-14 stsp static const struct got_error *
1972 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1973 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1974 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
1976 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1977 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1978 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1980 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1981 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1982 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1984 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1986 55cccc34 2020-02-20 stsp return err;
1987 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1989 55cccc34 2020-02-20 stsp *new_view = tree_view;
1991 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1992 55cccc34 2020-02-20 stsp return NULL;
1994 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1997 55cccc34 2020-02-20 stsp static const struct got_error *
1998 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2000 61266923 2020-01-14 stsp sigset_t sigset;
2001 61266923 2020-01-14 stsp int errcode;
2003 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2004 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2006 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
2007 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2008 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2009 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2010 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2012 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2013 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2014 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2016 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2017 61266923 2020-01-14 stsp if (errcode)
2018 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2020 61266923 2020-01-14 stsp return NULL;
2023 1a76625f 2018-10-22 stsp static void *
2024 1a76625f 2018-10-22 stsp log_thread(void *arg)
2026 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2027 1a76625f 2018-10-22 stsp int errcode = 0;
2028 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2029 1a76625f 2018-10-22 stsp int done = 0;
2031 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2033 61266923 2020-01-14 stsp return (void *)err;
2035 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2036 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2037 1a76625f 2018-10-22 stsp if (err) {
2038 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2039 1a76625f 2018-10-22 stsp return (void *)err;
2040 1a76625f 2018-10-22 stsp err = NULL;
2042 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2043 1a76625f 2018-10-22 stsp a->commits_needed--;
2045 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2046 3abe8080 2019-04-10 stsp if (errcode) {
2047 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2048 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2050 3abe8080 2019-04-10 stsp } else if (*a->quit)
2052 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2053 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2054 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2055 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2058 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2059 7c1452c1 2020-03-26 stsp if (errcode) {
2060 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2061 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2062 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2067 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2069 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2070 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2071 7c1452c1 2020-03-26 stsp &tog_mutex);
2072 7c1452c1 2020-03-26 stsp if (errcode)
2073 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2074 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2075 21355643 2020-12-06 stsp if (*a->quit)
2080 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2081 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2082 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2083 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2085 3abe8080 2019-04-10 stsp a->log_complete = 1;
2086 1a76625f 2018-10-22 stsp return (void *)err;
2089 1a76625f 2018-10-22 stsp static const struct got_error *
2090 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2092 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2093 1a76625f 2018-10-22 stsp int errcode;
2095 1a76625f 2018-10-22 stsp if (s->thread) {
2096 1a76625f 2018-10-22 stsp s->quit = 1;
2097 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2098 1a76625f 2018-10-22 stsp if (errcode)
2099 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2100 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2101 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2102 1a76625f 2018-10-22 stsp if (errcode)
2103 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2104 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2105 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2106 1a76625f 2018-10-22 stsp if (errcode)
2107 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2108 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2109 1a76625f 2018-10-22 stsp if (errcode)
2110 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2111 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2112 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2115 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2116 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2117 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2120 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2121 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2122 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2125 9343a5fb 2018-06-23 stsp return err;
2128 9343a5fb 2018-06-23 stsp static const struct got_error *
2129 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2131 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2132 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2133 276b94a1 2020-11-13 naddy int errcode;
2135 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2137 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2138 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2139 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2141 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2142 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2143 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2145 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2146 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2147 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2148 1a76625f 2018-10-22 stsp free(s->start_id);
2149 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2150 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2151 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2152 1a76625f 2018-10-22 stsp return err;
2155 1a76625f 2018-10-22 stsp static const struct got_error *
2156 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2158 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2160 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2161 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2162 60493ae3 2019-06-20 stsp return NULL;
2165 60493ae3 2019-06-20 stsp static const struct got_error *
2166 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2168 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2169 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2170 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2172 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2173 f9686aa5 2020-03-27 stsp show_log_view(view);
2174 f9686aa5 2020-03-27 stsp update_panels();
2175 f9686aa5 2020-03-27 stsp doupdate();
2177 96e2b566 2019-07-08 stsp if (s->search_entry) {
2178 13add988 2019-10-15 stsp int errcode, ch;
2179 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2180 13add988 2019-10-15 stsp if (errcode)
2181 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2182 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2183 13add988 2019-10-15 stsp ch = wgetch(view->window);
2184 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2185 13add988 2019-10-15 stsp if (errcode)
2186 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2187 13add988 2019-10-15 stsp "pthread_mutex_lock");
2188 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2189 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2190 678cbce5 2019-07-28 stsp return NULL;
2192 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2193 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2195 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2196 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2197 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2198 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2199 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2201 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2202 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2204 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2207 60493ae3 2019-06-20 stsp while (1) {
2208 13add988 2019-10-15 stsp int have_match = 0;
2210 60493ae3 2019-06-20 stsp if (entry == NULL) {
2211 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2212 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2213 f9967bca 2020-03-27 stsp view->search_next_done =
2214 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2215 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2216 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2217 f801134a 2019-06-25 stsp return NULL;
2220 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2221 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2222 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2224 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2225 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2228 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2229 13add988 2019-10-15 stsp &view->regex);
2232 13add988 2019-10-15 stsp if (have_match) {
2233 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2234 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2238 96e2b566 2019-07-08 stsp s->search_entry = entry;
2239 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2240 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2242 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2245 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2246 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2247 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2248 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2250 60493ae3 2019-06-20 stsp return err;
2253 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2254 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2256 60493ae3 2019-06-20 stsp return err;
2261 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2263 60493ae3 2019-06-20 stsp return NULL;
2266 60493ae3 2019-06-20 stsp static const struct got_error *
2267 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2268 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2269 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2271 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2272 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2273 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2274 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2275 1a76625f 2018-10-22 stsp int errcode;
2277 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2278 f135c941 2020-02-20 stsp free(s->in_repo_path);
2279 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2280 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2281 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2284 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2285 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2286 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2288 fb2756b9 2018-08-04 stsp s->repo = repo;
2289 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2290 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2291 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2292 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2293 9cd7cbd1 2020-12-07 stsp goto done;
2296 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2297 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2298 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2299 5036bf37 2018-09-24 stsp goto done;
2301 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2303 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2304 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2305 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2306 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2308 11b20872 2019-11-08 stsp goto done;
2309 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2310 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2311 11b20872 2019-11-08 stsp if (err) {
2312 11b20872 2019-11-08 stsp free_colors(&s->colors);
2313 11b20872 2019-11-08 stsp goto done;
2315 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2316 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2317 11b20872 2019-11-08 stsp if (err) {
2318 11b20872 2019-11-08 stsp free_colors(&s->colors);
2319 11b20872 2019-11-08 stsp goto done;
2323 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2324 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2325 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2326 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2327 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2329 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2331 1a76625f 2018-10-22 stsp goto done;
2332 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2333 b672a97a 2020-01-27 stsp !s->log_branches);
2335 1a76625f 2018-10-22 stsp goto done;
2336 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2337 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2339 c5b78334 2020-01-12 stsp goto done;
2341 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2342 1a76625f 2018-10-22 stsp if (errcode) {
2343 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2344 1a76625f 2018-10-22 stsp goto done;
2346 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2347 7c1452c1 2020-03-26 stsp if (errcode) {
2348 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2349 7c1452c1 2020-03-26 stsp goto done;
2352 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2353 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2354 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2355 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2356 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2357 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2358 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2359 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2360 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2361 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2362 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2363 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2364 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2367 1a76625f 2018-10-22 stsp close_log_view(view);
2368 ba4f502b 2018-08-04 stsp return err;
2371 e5a0f69f 2018-08-18 stsp static const struct got_error *
2372 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2374 f2f6d207 2020-11-24 stsp const struct got_error *err;
2375 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2377 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2378 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2379 2b380cc8 2018-10-24 stsp &s->thread_args);
2380 2b380cc8 2018-10-24 stsp if (errcode)
2381 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2382 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2383 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2385 f2f6d207 2020-11-24 stsp return err;
2389 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2392 e5a0f69f 2018-08-18 stsp static const struct got_error *
2393 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2395 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2396 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2397 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2398 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2399 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2400 f3bc9f1d 2021-09-05 naddy int begin_x = 0, n;
2402 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2403 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2404 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2405 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2406 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2407 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2408 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2409 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2410 fb280deb 2021-08-30 stsp select_commit(s);
2412 528dedf3 2021-08-30 stsp return NULL;
2415 528dedf3 2021-08-30 stsp switch (ch) {
2417 1e37a5c2 2019-05-12 jcs s->quit = 1;
2420 1e37a5c2 2019-05-12 jcs case KEY_UP:
2423 f7140bf5 2021-10-17 thomas case CTRL('p'):
2424 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2426 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2427 1e37a5c2 2019-05-12 jcs s->selected--;
2429 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2430 912a3f79 2021-08-30 j select_commit(s);
2433 912a3f79 2021-08-30 j case KEY_HOME:
2434 912a3f79 2021-08-30 j s->selected = 0;
2435 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2436 2b779855 2020-12-05 naddy select_commit(s);
2438 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2439 a4292ac5 2019-05-12 jcs case CTRL('b'):
2440 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2442 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2443 1e37a5c2 2019-05-12 jcs s->selected = 0;
2445 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2446 2b779855 2020-12-05 naddy select_commit(s);
2449 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2452 f7140bf5 2021-10-17 thomas case CTRL('n'):
2453 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2455 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2456 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2457 1e37a5c2 2019-05-12 jcs s->selected++;
2459 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2463 912a3f79 2021-08-30 j select_commit(s);
2466 912a3f79 2021-08-30 j case KEY_END: {
2467 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2468 912a3f79 2021-08-30 j * traverse them all. */
2469 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2470 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2471 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2474 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2475 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2476 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2477 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2479 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2480 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2482 f3bc9f1d 2021-09-05 naddy if (n > 0)
2483 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2484 2b779855 2020-12-05 naddy select_commit(s);
2487 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2488 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2489 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2490 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2491 1e37a5c2 2019-05-12 jcs if (first == NULL)
2493 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2496 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2497 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2498 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2499 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2500 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2501 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2503 2b779855 2020-12-05 naddy select_commit(s);
2506 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2507 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2508 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2509 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2510 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2511 2b779855 2020-12-05 naddy select_commit(s);
2512 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2513 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2514 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2515 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2516 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2519 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2522 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2524 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2525 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2526 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2527 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2528 78756c87 2020-11-24 stsp view, s->repo);
2531 e78dc838 2020-12-04 stsp view->focussed = 0;
2532 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2533 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2534 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2536 1e37a5c2 2019-05-12 jcs return err;
2537 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2538 e78dc838 2020-12-04 stsp view->focus_child = 1;
2540 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2543 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2545 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2546 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2547 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2548 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2552 e78dc838 2020-12-04 stsp view->focussed = 0;
2553 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2554 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2555 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2557 1e37a5c2 2019-05-12 jcs return err;
2558 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2559 e78dc838 2020-12-04 stsp view->focus_child = 1;
2561 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2563 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2564 21355643 2020-12-06 stsp case CTRL('l'):
2566 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2567 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2569 21355643 2020-12-06 stsp err = stop_log_thread(s);
2571 74cfe85e 2020-10-20 stsp return err;
2572 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2573 21355643 2020-12-06 stsp char *parent_path;
2574 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2576 21355643 2020-12-06 stsp return err;
2577 21355643 2020-12-06 stsp free(s->in_repo_path);
2578 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2579 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2580 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2581 21355643 2020-12-06 stsp struct got_object_id *start_id;
2582 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2583 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2584 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2586 21355643 2020-12-06 stsp return err;
2587 21355643 2020-12-06 stsp free(s->start_id);
2588 21355643 2020-12-06 stsp s->start_id = start_id;
2589 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2590 21355643 2020-12-06 stsp } else /* 'B' */
2591 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2593 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2594 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2596 21355643 2020-12-06 stsp return err;
2597 51a10b52 2020-12-26 stsp tog_free_refs();
2598 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2600 ca51c541 2020-12-07 stsp return err;
2601 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2602 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2604 d01904d4 2019-06-25 stsp return err;
2605 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2606 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2608 b672a97a 2020-01-27 stsp return err;
2609 21355643 2020-12-06 stsp free_commits(&s->commits);
2610 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2611 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2612 21355643 2020-12-06 stsp s->selected_entry = NULL;
2613 21355643 2020-12-06 stsp s->selected = 0;
2614 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2615 21355643 2020-12-06 stsp s->quit = 0;
2616 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2619 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2620 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2621 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2622 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2623 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2624 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2625 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2626 6458efa5 2020-11-24 stsp if (err) {
2627 6458efa5 2020-11-24 stsp view_close(ref_view);
2628 6458efa5 2020-11-24 stsp return err;
2630 e78dc838 2020-12-04 stsp view->focussed = 0;
2631 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2632 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2633 6458efa5 2020-11-24 stsp err = view_close_child(view);
2635 6458efa5 2020-11-24 stsp return err;
2636 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2637 e78dc838 2020-12-04 stsp view->focus_child = 1;
2639 6458efa5 2020-11-24 stsp *new_view = ref_view;
2645 80ddbec8 2018-04-29 stsp return err;
2648 4ed7e80c 2018-05-20 stsp static const struct got_error *
2649 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2651 c2db6724 2019-01-04 stsp const struct got_error *error;
2653 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2654 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2655 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2657 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2658 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2660 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2661 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2663 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2664 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2666 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2667 c2db6724 2019-01-04 stsp if (error != NULL)
2668 c2db6724 2019-01-04 stsp return error;
2670 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2671 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2673 c2db6724 2019-01-04 stsp return NULL;
2676 a915003a 2019-02-05 stsp static void
2677 a915003a 2019-02-05 stsp init_curses(void)
2679 a915003a 2019-02-05 stsp initscr();
2681 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2684 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2685 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2686 a915003a 2019-02-05 stsp curs_set(0);
2687 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2688 6d17833f 2019-11-08 stsp start_color();
2689 6d17833f 2019-11-08 stsp use_default_colors();
2691 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2692 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2693 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2696 c2db6724 2019-01-04 stsp static const struct got_error *
2697 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2698 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2700 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2702 f135c941 2020-02-20 stsp if (argc == 0) {
2703 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2704 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2705 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2706 f135c941 2020-02-20 stsp return NULL;
2709 f135c941 2020-02-20 stsp if (worktree) {
2710 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2713 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2715 f135c941 2020-02-20 stsp return err;
2716 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2717 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2718 bfd61697 2020-11-14 stsp p) == -1) {
2719 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2720 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2724 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2726 f135c941 2020-02-20 stsp return err;
2729 f135c941 2020-02-20 stsp static const struct got_error *
2730 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2732 80ddbec8 2018-04-29 stsp const struct got_error *error;
2733 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2734 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2735 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2736 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2737 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2738 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2739 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2740 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2741 04cc582a 2018-08-01 stsp struct tog_view *view;
2743 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2744 80ddbec8 2018-04-29 stsp switch (ch) {
2746 b672a97a 2020-01-27 stsp log_branches = 1;
2749 80ddbec8 2018-04-29 stsp start_commit = optarg;
2752 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2753 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2754 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2758 17020d27 2019-03-07 stsp usage_log();
2759 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2763 80ddbec8 2018-04-29 stsp argc -= optind;
2764 80ddbec8 2018-04-29 stsp argv += optind;
2766 f135c941 2020-02-20 stsp if (argc > 1)
2767 f135c941 2020-02-20 stsp usage_log();
2769 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2770 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2771 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2772 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2773 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2774 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2775 c156c7a4 2020-12-18 stsp goto done;
2776 a1fbf39a 2019-08-11 stsp if (worktree)
2777 6962eb72 2020-02-20 stsp repo_path =
2778 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2780 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2781 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2782 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2783 c156c7a4 2020-12-18 stsp goto done;
2787 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2788 80ddbec8 2018-04-29 stsp if (error != NULL)
2789 ecb28ae0 2018-07-16 stsp goto done;
2791 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2792 f135c941 2020-02-20 stsp repo, worktree);
2793 f135c941 2020-02-20 stsp if (error)
2794 f135c941 2020-02-20 stsp goto done;
2796 f135c941 2020-02-20 stsp init_curses();
2798 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2799 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2800 c02c541e 2019-03-29 stsp if (error)
2801 c02c541e 2019-03-29 stsp goto done;
2803 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2804 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2805 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
2806 87670572 2020-12-26 naddy if (error)
2807 87670572 2020-12-26 naddy goto done;
2810 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2811 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2812 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2813 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2814 d8f38dc4 2020-12-05 stsp if (error)
2815 d8f38dc4 2020-12-05 stsp goto done;
2816 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2818 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2819 d8f38dc4 2020-12-05 stsp if (error == NULL)
2820 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2821 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2822 d8f38dc4 2020-12-05 stsp goto done;
2823 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2824 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2825 d8f38dc4 2020-12-05 stsp if (error)
2826 d8f38dc4 2020-12-05 stsp goto done;
2829 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2830 04cc582a 2018-08-01 stsp if (view == NULL) {
2831 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2832 04cc582a 2018-08-01 stsp goto done;
2834 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2835 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2836 ba4f502b 2018-08-04 stsp if (error)
2837 ba4f502b 2018-08-04 stsp goto done;
2838 2fc00ff4 2019-08-31 stsp if (worktree) {
2839 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2840 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2841 2fc00ff4 2019-08-31 stsp worktree = NULL;
2843 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2845 f135c941 2020-02-20 stsp free(in_repo_path);
2846 ecb28ae0 2018-07-16 stsp free(repo_path);
2847 ecb28ae0 2018-07-16 stsp free(cwd);
2848 899d86c2 2018-05-10 stsp free(start_id);
2849 d8f38dc4 2020-12-05 stsp free(label);
2851 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2852 1d0f4054 2021-06-17 stsp if (repo) {
2853 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2854 1d0f4054 2021-06-17 stsp if (error == NULL)
2855 1d0f4054 2021-06-17 stsp error = close_err;
2857 ec142235 2019-03-07 stsp if (worktree)
2858 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2859 51a10b52 2020-12-26 stsp tog_free_refs();
2860 80ddbec8 2018-04-29 stsp return error;
2863 4ed7e80c 2018-05-20 stsp __dead static void
2864 9f7d7167 2018-04-29 stsp usage_diff(void)
2867 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2868 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2872 6d17833f 2019-11-08 stsp static int
2873 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2874 41605754 2020-11-12 stsp regmatch_t *regmatch)
2876 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2879 f26dddb7 2019-11-08 stsp struct tog_color *
2880 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2882 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2884 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2885 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2886 6d17833f 2019-11-08 stsp return tc;
2889 6d17833f 2019-11-08 stsp return NULL;
2892 4ed7e80c 2018-05-20 stsp static const struct got_error *
2893 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2894 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2896 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2897 41605754 2020-11-12 stsp wchar_t *wline;
2898 41605754 2020-11-12 stsp int width;
2901 41605754 2020-11-12 stsp *wtotal = 0;
2903 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2904 41605754 2020-11-12 stsp if (s == NULL)
2905 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2907 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2908 41605754 2020-11-12 stsp if (err) {
2910 41605754 2020-11-12 stsp return err;
2912 41605754 2020-11-12 stsp waddwstr(window, wline);
2913 41605754 2020-11-12 stsp free(wline);
2915 41605754 2020-11-12 stsp wlimit -= width;
2916 41605754 2020-11-12 stsp *wtotal += width;
2918 41605754 2020-11-12 stsp if (wlimit > 0) {
2919 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2920 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2921 41605754 2020-11-12 stsp if (s == NULL) {
2922 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2924 41605754 2020-11-12 stsp return err;
2926 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2927 41605754 2020-11-12 stsp if (err) {
2929 41605754 2020-11-12 stsp return err;
2931 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2932 41605754 2020-11-12 stsp waddwstr(window, wline);
2933 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2934 41605754 2020-11-12 stsp free(wline);
2936 41605754 2020-11-12 stsp wlimit -= width;
2937 41605754 2020-11-12 stsp *wtotal += width;
2940 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2941 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2942 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2944 41605754 2020-11-12 stsp return err;
2945 41605754 2020-11-12 stsp waddwstr(window, wline);
2946 41605754 2020-11-12 stsp free(wline);
2947 41605754 2020-11-12 stsp *wtotal += width;
2950 41605754 2020-11-12 stsp return NULL;
2953 41605754 2020-11-12 stsp static const struct got_error *
2954 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2956 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2957 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2958 61e69b96 2018-05-20 stsp const struct got_error *err;
2959 fe621944 2020-11-10 stsp int nprinted = 0;
2960 b304db33 2018-05-20 stsp char *line;
2961 826082fe 2020-12-10 stsp size_t linesize = 0;
2962 826082fe 2020-12-10 stsp ssize_t linelen;
2963 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2964 61e69b96 2018-05-20 stsp wchar_t *wline;
2965 e0b650dd 2018-05-20 stsp int width;
2966 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2967 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2968 fe621944 2020-11-10 stsp off_t line_offset;
2970 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2971 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2972 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2974 f7d12f7e 2018-08-01 stsp werase(view->window);
2976 a3404814 2018-09-02 stsp if (header) {
2977 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2978 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2979 135a2da0 2020-11-11 stsp header) == -1)
2980 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2981 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2982 135a2da0 2020-11-11 stsp free(line);
2984 a3404814 2018-09-02 stsp return err;
2986 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2987 a3404814 2018-09-02 stsp wstandout(view->window);
2988 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2989 e54cc94a 2020-11-11 stsp free(wline);
2990 e54cc94a 2020-11-11 stsp wline = NULL;
2991 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2992 a3404814 2018-09-02 stsp wstandend(view->window);
2993 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2994 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2996 a3404814 2018-09-02 stsp if (max_lines <= 1)
2997 a3404814 2018-09-02 stsp return NULL;
2998 a3404814 2018-09-02 stsp max_lines--;
3001 89f1a395 2020-12-01 naddy s->eof = 0;
3002 826082fe 2020-12-10 stsp line = NULL;
3003 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3004 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3005 826082fe 2020-12-10 stsp if (linelen == -1) {
3006 826082fe 2020-12-10 stsp if (feof(s->f)) {
3007 826082fe 2020-12-10 stsp s->eof = 1;
3010 826082fe 2020-12-10 stsp free(line);
3011 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3014 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3016 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3017 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3018 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3019 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3020 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3021 41605754 2020-11-12 stsp view->window, regmatch);
3022 41605754 2020-11-12 stsp if (err) {
3023 41605754 2020-11-12 stsp free(line);
3024 41605754 2020-11-12 stsp return err;
3027 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3028 41605754 2020-11-12 stsp if (err) {
3029 41605754 2020-11-12 stsp free(line);
3030 41605754 2020-11-12 stsp return err;
3032 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3033 41605754 2020-11-12 stsp free(wline);
3034 41605754 2020-11-12 stsp wline = NULL;
3037 6d17833f 2019-11-08 stsp wattr_off(view->window,
3038 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3039 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3040 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3041 fe621944 2020-11-10 stsp nprinted++;
3043 826082fe 2020-12-10 stsp free(line);
3044 fe621944 2020-11-10 stsp if (nprinted >= 1)
3045 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3046 89f1a395 2020-12-01 naddy (nprinted - 1);
3048 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3050 1a57306a 2018-09-02 stsp view_vborder(view);
3052 89f1a395 2020-12-01 naddy if (s->eof) {
3053 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3054 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3055 c3e9aa98 2019-05-13 jcs nprinted++;
3058 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3060 c3e9aa98 2019-05-13 jcs return err;
3063 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3064 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3065 e54cc94a 2020-11-11 stsp free(wline);
3066 e54cc94a 2020-11-11 stsp wline = NULL;
3067 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3070 26ed57b2 2018-05-19 stsp return NULL;
3073 abd2672a 2018-12-23 stsp static char *
3074 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3076 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3077 09867e48 2019-08-13 stsp char *p, *s;
3079 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3080 09867e48 2019-08-13 stsp if (tm == NULL)
3081 09867e48 2019-08-13 stsp return NULL;
3082 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3083 09867e48 2019-08-13 stsp if (s == NULL)
3084 09867e48 2019-08-13 stsp return NULL;
3085 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3087 abd2672a 2018-12-23 stsp *p = '\0';
3091 4ed7e80c 2018-05-20 stsp static const struct got_error *
3092 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3093 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3095 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3096 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3097 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3098 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3100 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3101 0208f208 2020-05-05 stsp if (qid != NULL) {
3102 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3103 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3106 0208f208 2020-05-05 stsp return err;
3108 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3109 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3110 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3111 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3112 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3114 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3118 0208f208 2020-05-05 stsp if (tree_id1) {
3119 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3121 0208f208 2020-05-05 stsp goto done;
3124 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3125 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3127 0208f208 2020-05-05 stsp goto done;
3129 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3130 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3132 0208f208 2020-05-05 stsp if (tree1)
3133 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3134 0208f208 2020-05-05 stsp if (tree2)
3135 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3136 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3137 0208f208 2020-05-05 stsp return err;
3140 0208f208 2020-05-05 stsp static const struct got_error *
3141 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3145 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3146 fe621944 2020-11-10 stsp if (p == NULL)
3147 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3148 fe621944 2020-11-10 stsp *line_offsets = p;
3149 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3150 fe621944 2020-11-10 stsp (*nlines)++;
3151 fe621944 2020-11-10 stsp return NULL;
3154 fe621944 2020-11-10 stsp static const struct got_error *
3155 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3156 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3157 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3159 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3160 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3161 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3162 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3163 45d799e2 2018-12-23 stsp time_t committer_time;
3164 45d799e2 2018-12-23 stsp const char *author, *committer;
3165 8b473291 2019-02-21 stsp char *refs_str = NULL;
3166 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3167 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3168 fe621944 2020-11-10 stsp off_t outoff = 0;
3171 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3173 8b473291 2019-02-21 stsp if (refs) {
3174 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3176 8b473291 2019-02-21 stsp return err;
3179 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3181 abd2672a 2018-12-23 stsp return err;
3183 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3184 15a94983 2018-12-23 stsp if (err) {
3185 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3186 15a94983 2018-12-23 stsp goto done;
3189 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3191 fe621944 2020-11-10 stsp goto done;
3193 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3194 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3195 fe621944 2020-11-10 stsp if (n < 0) {
3196 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3197 abd2672a 2018-12-23 stsp goto done;
3199 fe621944 2020-11-10 stsp outoff += n;
3200 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3202 fe621944 2020-11-10 stsp goto done;
3204 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3205 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3206 fe621944 2020-11-10 stsp if (n < 0) {
3207 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3208 abd2672a 2018-12-23 stsp goto done;
3210 fe621944 2020-11-10 stsp outoff += n;
3211 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3213 fe621944 2020-11-10 stsp goto done;
3215 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3216 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3217 fe621944 2020-11-10 stsp if (datestr) {
3218 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3219 fe621944 2020-11-10 stsp if (n < 0) {
3220 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3221 fe621944 2020-11-10 stsp goto done;
3223 fe621944 2020-11-10 stsp outoff += n;
3224 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3226 fe621944 2020-11-10 stsp goto done;
3228 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3229 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3230 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3231 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3232 fe621944 2020-11-10 stsp if (n < 0) {
3233 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3234 fe621944 2020-11-10 stsp goto done;
3236 fe621944 2020-11-10 stsp outoff += n;
3237 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3239 fe621944 2020-11-10 stsp goto done;
3241 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3242 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3243 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3244 ac4dc263 2021-09-24 thomas int pn = 1;
3245 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3246 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3247 ac4dc263 2021-09-24 thomas err = got_object_id_str(&id_str, qid->id);
3248 ac4dc263 2021-09-24 thomas if (err)
3249 ac4dc263 2021-09-24 thomas goto done;
3250 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3251 ac4dc263 2021-09-24 thomas if (n < 0) {
3252 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3253 ac4dc263 2021-09-24 thomas goto done;
3255 ac4dc263 2021-09-24 thomas outoff += n;
3256 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3257 ac4dc263 2021-09-24 thomas if (err)
3258 ac4dc263 2021-09-24 thomas goto done;
3259 ac4dc263 2021-09-24 thomas free(id_str);
3260 ac4dc263 2021-09-24 thomas id_str = NULL;
3264 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3266 5943eee2 2019-08-13 stsp goto done;
3267 fe621944 2020-11-10 stsp s = logmsg;
3268 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3269 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3270 fe621944 2020-11-10 stsp if (n < 0) {
3271 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3272 fe621944 2020-11-10 stsp goto done;
3274 fe621944 2020-11-10 stsp outoff += n;
3275 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3277 fe621944 2020-11-10 stsp goto done;
3280 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3282 0208f208 2020-05-05 stsp goto done;
3283 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3284 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3285 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3286 fe621944 2020-11-10 stsp if (n < 0) {
3287 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3288 fe621944 2020-11-10 stsp goto done;
3290 fe621944 2020-11-10 stsp outoff += n;
3291 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3293 fe621944 2020-11-10 stsp goto done;
3294 0208f208 2020-05-05 stsp free((char *)pe->path);
3295 0208f208 2020-05-05 stsp free(pe->data);
3298 0208f208 2020-05-05 stsp fputc('\n', outfile);
3300 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3302 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3303 abd2672a 2018-12-23 stsp free(id_str);
3304 5943eee2 2019-08-13 stsp free(logmsg);
3305 8b473291 2019-02-21 stsp free(refs_str);
3306 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3307 7510f233 2020-08-09 stsp if (err) {
3308 ae6a6978 2020-08-09 stsp free(*line_offsets);
3309 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3310 ae6a6978 2020-08-09 stsp *nlines = 0;
3312 fe621944 2020-11-10 stsp return err;
3315 abd2672a 2018-12-23 stsp static const struct got_error *
3316 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3318 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3319 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3320 15a94983 2018-12-23 stsp int obj_type;
3322 fe621944 2020-11-10 stsp free(s->line_offsets);
3323 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3324 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3325 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3326 fe621944 2020-11-10 stsp s->nlines = 0;
3328 511a516b 2018-05-19 stsp f = got_opentemp();
3329 48ae06ee 2018-10-18 stsp if (f == NULL) {
3330 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3331 48ae06ee 2018-10-18 stsp goto done;
3333 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3334 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3335 fb43ecf1 2019-02-11 stsp goto done;
3339 15a94983 2018-12-23 stsp if (s->id1)
3340 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3342 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3344 15a94983 2018-12-23 stsp goto done;
3346 15a94983 2018-12-23 stsp switch (obj_type) {
3347 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3348 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3349 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3350 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3352 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3353 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3354 cc8021af 2021-10-12 thomas s->id1, s->id2, NULL, "", "", s->diff_context,
3355 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3357 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3358 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3359 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3360 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3361 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3363 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3365 3ffacbe1 2020-02-02 tracey goto done;
3366 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3367 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3368 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3369 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3370 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3371 f44b1f58 2020-02-02 tracey if (err)
3372 f44b1f58 2020-02-02 tracey goto done;
3373 f44b1f58 2020-02-02 tracey } else {
3374 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3375 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3376 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3377 fe621944 2020-11-10 stsp err = write_commit_info(
3378 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3379 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3380 f44b1f58 2020-02-02 tracey if (err)
3381 f44b1f58 2020-02-02 tracey goto done;
3386 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3388 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3389 cc8021af 2021-10-12 thomas s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3390 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3394 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3397 f44b1f58 2020-02-02 tracey if (err)
3398 f44b1f58 2020-02-02 tracey goto done;
3400 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3401 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3402 48ae06ee 2018-10-18 stsp return err;
3405 f5215bb9 2019-02-22 stsp static void
3406 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3408 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3409 f5215bb9 2019-02-22 stsp update_panels();
3410 f5215bb9 2019-02-22 stsp doupdate();
3413 f44b1f58 2020-02-02 tracey static const struct got_error *
3414 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3416 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3418 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3419 f44b1f58 2020-02-02 tracey return NULL;
3422 f44b1f58 2020-02-02 tracey static const struct got_error *
3423 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3425 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3426 f44b1f58 2020-02-02 tracey int lineno;
3427 826082fe 2020-12-10 stsp char *line = NULL;
3428 826082fe 2020-12-10 stsp size_t linesize = 0;
3429 826082fe 2020-12-10 stsp ssize_t linelen;
3431 f44b1f58 2020-02-02 tracey if (!view->searching) {
3432 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3433 f44b1f58 2020-02-02 tracey return NULL;
3436 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3437 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3438 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3440 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3442 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3444 f44b1f58 2020-02-02 tracey while (1) {
3445 f44b1f58 2020-02-02 tracey off_t offset;
3447 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3448 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3449 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3453 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3454 f44b1f58 2020-02-02 tracey lineno = 1;
3456 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3459 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3460 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3461 f44b1f58 2020-02-02 tracey free(line);
3462 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3464 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3465 826082fe 2020-12-10 stsp if (linelen != -1 &&
3466 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3467 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3468 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3471 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3472 f44b1f58 2020-02-02 tracey lineno++;
3474 f44b1f58 2020-02-02 tracey lineno--;
3476 826082fe 2020-12-10 stsp free(line);
3478 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3479 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3480 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3483 f44b1f58 2020-02-02 tracey return NULL;
3486 48ae06ee 2018-10-18 stsp static const struct got_error *
3487 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3488 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3489 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3490 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3492 48ae06ee 2018-10-18 stsp const struct got_error *err;
3493 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3495 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3496 15a94983 2018-12-23 stsp int type1, type2;
3497 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3499 15a94983 2018-12-23 stsp return err;
3500 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3502 15a94983 2018-12-23 stsp return err;
3504 15a94983 2018-12-23 stsp if (type1 != type2)
3505 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3507 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3508 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3509 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3510 f44b1f58 2020-02-02 tracey s->repo = repo;
3511 f44b1f58 2020-02-02 tracey s->id1 = id1;
3512 f44b1f58 2020-02-02 tracey s->id2 = id2;
3513 3dbaef42 2020-11-24 stsp s->label1 = label1;
3514 3dbaef42 2020-11-24 stsp s->label2 = label2;
3516 15a94983 2018-12-23 stsp if (id1) {
3517 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3518 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3519 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3521 5465d566 2020-02-01 tracey s->id1 = NULL;
3523 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3524 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3525 5465d566 2020-02-01 tracey free(s->id1);
3526 5465d566 2020-02-01 tracey s->id1 = NULL;
3527 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3529 5465d566 2020-02-01 tracey s->f = NULL;
3530 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3531 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3532 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3533 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3534 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3535 5465d566 2020-02-01 tracey s->log_view = log_view;
3536 5465d566 2020-02-01 tracey s->repo = repo;
3538 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3539 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3540 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3541 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3542 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3544 6d17833f 2019-11-08 stsp return err;
3545 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3546 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3547 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3548 6d17833f 2019-11-08 stsp if (err) {
3549 5465d566 2020-02-01 tracey free_colors(&s->colors);
3550 6d17833f 2019-11-08 stsp return err;
3552 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3553 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3554 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3555 6d17833f 2019-11-08 stsp if (err) {
3556 5465d566 2020-02-01 tracey free_colors(&s->colors);
3557 6d17833f 2019-11-08 stsp return err;
3560 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3561 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3562 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3563 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3564 6d17833f 2019-11-08 stsp if (err) {
3565 5465d566 2020-02-01 tracey free_colors(&s->colors);
3566 6d17833f 2019-11-08 stsp return err;
3569 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3570 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3571 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3572 11b20872 2019-11-08 stsp if (err) {
3573 5465d566 2020-02-01 tracey free_colors(&s->colors);
3574 11b20872 2019-11-08 stsp return err;
3577 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3578 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3579 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3580 11b20872 2019-11-08 stsp if (err) {
3581 5465d566 2020-02-01 tracey free_colors(&s->colors);
3582 11b20872 2019-11-08 stsp return err;
3586 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3587 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3588 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3590 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3591 fe621944 2020-11-10 stsp s->nlines = 0;
3592 5465d566 2020-02-01 tracey err = create_diff(s);
3593 48ae06ee 2018-10-18 stsp if (err) {
3594 5465d566 2020-02-01 tracey free(s->id1);
3595 5465d566 2020-02-01 tracey s->id1 = NULL;
3596 5465d566 2020-02-01 tracey free(s->id2);
3597 5465d566 2020-02-01 tracey s->id2 = NULL;
3598 78756c87 2020-11-24 stsp free_colors(&s->colors);
3599 48ae06ee 2018-10-18 stsp return err;
3602 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3603 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3604 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3605 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3606 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3608 5dc9f4bc 2018-08-04 stsp return NULL;
3611 e5a0f69f 2018-08-18 stsp static const struct got_error *
3612 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3614 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3615 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3617 5465d566 2020-02-01 tracey free(s->id1);
3618 5465d566 2020-02-01 tracey s->id1 = NULL;
3619 5465d566 2020-02-01 tracey free(s->id2);
3620 5465d566 2020-02-01 tracey s->id2 = NULL;
3621 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3622 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3623 5465d566 2020-02-01 tracey free_colors(&s->colors);
3624 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3625 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3626 fe621944 2020-11-10 stsp s->nlines = 0;
3627 e5a0f69f 2018-08-18 stsp return err;
3630 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3631 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3633 a3404814 2018-09-02 stsp const struct got_error *err;
3634 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3635 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3636 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3638 a3404814 2018-09-02 stsp if (s->id1) {
3639 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3641 a3404814 2018-09-02 stsp return err;
3642 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3644 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3646 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3648 a3404814 2018-09-02 stsp return err;
3649 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3651 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3652 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3653 a3404814 2018-09-02 stsp free(id_str1);
3654 a3404814 2018-09-02 stsp free(id_str2);
3655 a3404814 2018-09-02 stsp return err;
3657 a3404814 2018-09-02 stsp free(id_str1);
3658 a3404814 2018-09-02 stsp free(id_str2);
3660 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3661 267bb3b8 2021-08-01 stsp free(header);
3662 267bb3b8 2021-08-01 stsp return err;
3665 15a087fe 2019-02-21 stsp static const struct got_error *
3666 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3667 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3669 d7a04538 2019-02-21 stsp const struct got_error *err;
3670 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3671 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3672 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3674 15a087fe 2019-02-21 stsp free(s->id2);
3675 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3676 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3677 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3679 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3681 d7a04538 2019-02-21 stsp return err;
3682 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3683 15a087fe 2019-02-21 stsp free(s->id1);
3684 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3685 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3686 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3687 15a087fe 2019-02-21 stsp return NULL;
3690 0cf4efb1 2018-09-29 stsp static const struct got_error *
3691 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3693 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3694 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3695 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3696 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3697 826082fe 2020-12-10 stsp char *line = NULL;
3698 826082fe 2020-12-10 stsp size_t linesize = 0;
3699 826082fe 2020-12-10 stsp ssize_t linelen;
3702 e5a0f69f 2018-08-18 stsp switch (ch) {
3705 3dbaef42 2020-11-24 stsp if (ch == 'a')
3706 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3707 3dbaef42 2020-11-24 stsp if (ch == 'w')
3708 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3709 64453f7e 2020-11-21 stsp wclear(view->window);
3710 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3711 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3712 aa61903a 2021-12-31 thomas s->matched_line = 0;
3713 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3714 64453f7e 2020-11-21 stsp err = create_diff(s);
3717 912a3f79 2021-08-30 j case KEY_HOME:
3718 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3721 912a3f79 2021-08-30 j case KEY_END:
3725 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3729 1e37a5c2 2019-05-12 jcs case KEY_UP:
3730 f7140bf5 2021-10-17 thomas case CTRL('p'):
3731 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3732 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3734 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3735 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3736 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3739 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3740 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3741 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3744 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3745 f7140bf5 2021-10-17 thomas case CTRL('n'):
3746 1e37a5c2 2019-05-12 jcs if (!s->eof)
3747 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3749 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3750 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3752 00ba99a7 2019-05-12 jcs if (s->eof)
3755 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3756 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3757 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3758 826082fe 2020-12-10 stsp if (linelen == -1) {
3759 826082fe 2020-12-10 stsp if (feof(s->f)) {
3760 826082fe 2020-12-10 stsp s->eof = 1;
3762 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3766 826082fe 2020-12-10 stsp free(line);
3769 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3770 1e37a5c2 2019-05-12 jcs s->diff_context--;
3771 aa61903a 2021-12-31 thomas s->matched_line = 0;
3772 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3773 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3774 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3775 27829c9e 2020-11-21 stsp s->nlines) {
3776 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3777 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3782 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3783 1e37a5c2 2019-05-12 jcs s->diff_context++;
3784 aa61903a 2021-12-31 thomas s->matched_line = 0;
3785 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3786 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3791 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3793 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3794 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3796 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3800 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3803 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3807 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3808 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3809 aa61903a 2021-12-31 thomas s->matched_line = 0;
3811 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3812 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3816 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3818 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3819 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3821 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3825 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3828 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3832 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3833 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3834 aa61903a 2021-12-31 thomas s->matched_line = 0;
3836 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3837 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3843 bcbd79e2 2018-08-19 stsp return err;
3846 4ed7e80c 2018-05-20 stsp static const struct got_error *
3847 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3849 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3850 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3851 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3852 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3853 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3854 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3855 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3856 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3857 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3858 3dbaef42 2020-11-24 stsp const char *errstr;
3859 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3861 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3862 26ed57b2 2018-05-19 stsp switch (ch) {
3864 64453f7e 2020-11-21 stsp force_text_diff = 1;
3867 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3869 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3870 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3872 09b5bff8 2020-02-23 naddy case 'r':
3873 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3874 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3875 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3877 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3880 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3883 17020d27 2019-03-07 stsp usage_diff();
3884 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3888 26ed57b2 2018-05-19 stsp argc -= optind;
3889 26ed57b2 2018-05-19 stsp argv += optind;
3891 26ed57b2 2018-05-19 stsp if (argc == 0) {
3892 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3893 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3894 15a94983 2018-12-23 stsp id_str1 = argv[0];
3895 15a94983 2018-12-23 stsp id_str2 = argv[1];
3897 26ed57b2 2018-05-19 stsp usage_diff();
3899 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3900 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3901 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3902 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3903 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3904 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3905 c156c7a4 2020-12-18 stsp goto done;
3906 a273ac94 2020-02-23 naddy if (worktree)
3907 a273ac94 2020-02-23 naddy repo_path =
3908 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3910 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3911 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3912 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3913 c156c7a4 2020-12-18 stsp goto done;
3917 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3918 eb6600df 2019-01-04 stsp if (error)
3919 eb6600df 2019-01-04 stsp goto done;
3921 a273ac94 2020-02-23 naddy init_curses();
3923 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3924 51a10b52 2020-12-26 stsp if (error)
3925 51a10b52 2020-12-26 stsp goto done;
3927 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3928 26ed57b2 2018-05-19 stsp if (error)
3929 26ed57b2 2018-05-19 stsp goto done;
3931 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3932 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3933 26ed57b2 2018-05-19 stsp if (error)
3934 26ed57b2 2018-05-19 stsp goto done;
3936 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3937 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3938 26ed57b2 2018-05-19 stsp if (error)
3939 26ed57b2 2018-05-19 stsp goto done;
3941 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3942 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3943 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3944 ea5e7bb5 2018-08-01 stsp goto done;
3946 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3947 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3948 5dc9f4bc 2018-08-04 stsp if (error)
3949 5dc9f4bc 2018-08-04 stsp goto done;
3950 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3952 3dbaef42 2020-11-24 stsp free(label1);
3953 3dbaef42 2020-11-24 stsp free(label2);
3954 c02c541e 2019-03-29 stsp free(repo_path);
3955 a273ac94 2020-02-23 naddy free(cwd);
3956 1d0f4054 2021-06-17 stsp if (repo) {
3957 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3958 1d0f4054 2021-06-17 stsp if (error == NULL)
3959 1d0f4054 2021-06-17 stsp error = close_err;
3961 a273ac94 2020-02-23 naddy if (worktree)
3962 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3963 51a10b52 2020-12-26 stsp tog_free_refs();
3964 26ed57b2 2018-05-19 stsp return error;
3967 4ed7e80c 2018-05-20 stsp __dead static void
3968 9f7d7167 2018-04-29 stsp usage_blame(void)
3971 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3972 9f7d7167 2018-04-29 stsp getprogname());
3976 84451b3e 2018-07-10 stsp struct tog_blame_line {
3977 84451b3e 2018-07-10 stsp int annotated;
3978 84451b3e 2018-07-10 stsp struct got_object_id *id;
3981 4ed7e80c 2018-05-20 stsp static const struct got_error *
3982 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3984 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3985 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3986 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3987 84451b3e 2018-07-10 stsp const struct got_error *err;
3988 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3989 826082fe 2020-12-10 stsp char *line = NULL;
3990 826082fe 2020-12-10 stsp size_t linesize = 0;
3991 826082fe 2020-12-10 stsp ssize_t linelen;
3992 84451b3e 2018-07-10 stsp wchar_t *wline;
3993 27a741e5 2019-09-11 stsp int width;
3994 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3995 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3996 ab089a2a 2018-07-12 stsp char *id_str;
3997 11b20872 2019-11-08 stsp struct tog_color *tc;
3999 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
4001 ab089a2a 2018-07-12 stsp return err;
4003 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4004 f7d12f7e 2018-08-01 stsp werase(view->window);
4006 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4007 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4008 ab089a2a 2018-07-12 stsp free(id_str);
4009 ab089a2a 2018-07-12 stsp return err;
4012 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4013 ab089a2a 2018-07-12 stsp free(line);
4014 2550e4c3 2018-07-13 stsp line = NULL;
4016 1cae65b4 2019-09-22 stsp return err;
4017 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4018 a3404814 2018-09-02 stsp wstandout(view->window);
4019 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4021 11b20872 2019-11-08 stsp wattr_on(view->window,
4022 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4023 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4025 11b20872 2019-11-08 stsp wattr_off(view->window,
4026 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4027 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4028 a3404814 2018-09-02 stsp wstandend(view->window);
4029 2550e4c3 2018-07-13 stsp free(wline);
4030 2550e4c3 2018-07-13 stsp wline = NULL;
4031 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4032 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4034 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4035 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4036 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4037 ab089a2a 2018-07-12 stsp free(id_str);
4038 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4040 ab089a2a 2018-07-12 stsp free(id_str);
4041 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4042 3f60a8ef 2018-07-10 stsp free(line);
4043 2550e4c3 2018-07-13 stsp line = NULL;
4045 3f60a8ef 2018-07-10 stsp return err;
4046 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4047 2550e4c3 2018-07-13 stsp free(wline);
4048 2550e4c3 2018-07-13 stsp wline = NULL;
4049 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4050 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4052 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4053 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4054 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4055 826082fe 2020-12-10 stsp if (linelen == -1) {
4056 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4057 826082fe 2020-12-10 stsp s->eof = 1;
4060 84451b3e 2018-07-10 stsp free(line);
4061 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4063 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4066 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4067 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4069 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4070 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4071 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4072 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4073 27a741e5 2019-09-11 stsp !(view->focussed &&
4074 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4075 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4076 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4077 8d0fe45a 2019-08-12 stsp char *id_str;
4078 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4079 8d0fe45a 2019-08-12 stsp if (err) {
4080 8d0fe45a 2019-08-12 stsp free(line);
4081 8d0fe45a 2019-08-12 stsp return err;
4083 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4085 11b20872 2019-11-08 stsp wattr_on(view->window,
4086 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4087 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4089 11b20872 2019-11-08 stsp wattr_off(view->window,
4090 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4091 8d0fe45a 2019-08-12 stsp free(id_str);
4092 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4094 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4095 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4098 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4099 ee41ec32 2018-07-10 stsp prev_id = NULL;
4102 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4103 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4104 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4106 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4107 41605754 2020-11-12 stsp width = 9;
4108 41605754 2020-11-12 stsp wline = wcsdup(L"");
4109 41605754 2020-11-12 stsp if (wline == NULL) {
4110 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4111 41605754 2020-11-12 stsp free(line);
4112 41605754 2020-11-12 stsp return err;
4114 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4115 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4116 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4117 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4118 41605754 2020-11-12 stsp view->window, regmatch);
4119 41605754 2020-11-12 stsp if (err) {
4120 41605754 2020-11-12 stsp free(line);
4121 41605754 2020-11-12 stsp return err;
4123 41605754 2020-11-12 stsp width += 9;
4125 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4126 41605754 2020-11-12 stsp view->ncols - 9, 9);
4127 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4128 41605754 2020-11-12 stsp free(wline);
4129 41605754 2020-11-12 stsp wline = NULL;
4130 41605754 2020-11-12 stsp width += 9;
4133 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4134 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4135 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4136 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4138 826082fe 2020-12-10 stsp free(line);
4139 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4141 1a57306a 2018-09-02 stsp view_vborder(view);
4143 84451b3e 2018-07-10 stsp return NULL;
4146 84451b3e 2018-07-10 stsp static const struct got_error *
4147 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4149 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4150 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4151 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4152 1a76625f 2018-10-22 stsp int errcode;
4154 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4155 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4156 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4158 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4159 1a76625f 2018-10-22 stsp if (errcode)
4160 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4162 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4163 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4164 d68a0a7d 2018-07-10 stsp goto done;
4167 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4168 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4170 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4171 d68a0a7d 2018-07-10 stsp if (line->annotated)
4172 d68a0a7d 2018-07-10 stsp goto done;
4174 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4175 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4176 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4177 84451b3e 2018-07-10 stsp goto done;
4179 84451b3e 2018-07-10 stsp line->annotated = 1;
4181 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4182 1a76625f 2018-10-22 stsp if (errcode)
4183 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4184 84451b3e 2018-07-10 stsp return err;
4187 84451b3e 2018-07-10 stsp static void *
4188 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4190 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4191 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4192 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4193 1a76625f 2018-10-22 stsp int errcode;
4195 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4197 61266923 2020-01-14 stsp return (void *)err;
4199 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4200 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4201 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4202 fc06ba56 2019-08-22 stsp err = NULL;
4204 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4205 1a76625f 2018-10-22 stsp if (errcode)
4206 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4207 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4209 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4210 1d0f4054 2021-06-17 stsp if (err == NULL)
4211 1d0f4054 2021-06-17 stsp err = close_err;
4212 c9beca56 2018-07-22 stsp ta->repo = NULL;
4213 c9beca56 2018-07-22 stsp *ta->complete = 1;
4215 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4216 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4217 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4219 18430de3 2018-07-10 stsp return (void *)err;
4222 245d91c1 2018-07-12 stsp static struct got_object_id *
4223 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4224 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4226 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4228 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4229 8d0fe45a 2019-08-12 stsp return NULL;
4231 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4232 245d91c1 2018-07-12 stsp if (!line->annotated)
4233 245d91c1 2018-07-12 stsp return NULL;
4235 245d91c1 2018-07-12 stsp return line->id;
4238 b880a918 2018-07-10 stsp static const struct got_error *
4239 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4241 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4244 245d91c1 2018-07-12 stsp if (blame->thread) {
4245 1a76625f 2018-10-22 stsp int errcode;
4246 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4247 1a76625f 2018-10-22 stsp if (errcode)
4248 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4249 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4250 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4251 1a76625f 2018-10-22 stsp if (errcode)
4252 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4253 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4254 1a76625f 2018-10-22 stsp if (errcode)
4255 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4256 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4257 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4258 245d91c1 2018-07-12 stsp err = NULL;
4259 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4261 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4262 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4263 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4264 1d0f4054 2021-06-17 stsp if (err == NULL)
4265 1d0f4054 2021-06-17 stsp err = close_err;
4266 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4268 245d91c1 2018-07-12 stsp if (blame->f) {
4269 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4270 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4271 245d91c1 2018-07-12 stsp blame->f = NULL;
4273 57670559 2018-12-23 stsp if (blame->lines) {
4274 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4275 57670559 2018-12-23 stsp free(blame->lines[i].id);
4276 57670559 2018-12-23 stsp free(blame->lines);
4277 57670559 2018-12-23 stsp blame->lines = NULL;
4279 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4280 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4282 245d91c1 2018-07-12 stsp return err;
4285 245d91c1 2018-07-12 stsp static const struct got_error *
4286 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4288 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4289 fc06ba56 2019-08-22 stsp int *done = arg;
4290 fc06ba56 2019-08-22 stsp int errcode;
4292 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&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_unlock");
4297 fc06ba56 2019-08-22 stsp if (*done)
4298 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4300 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4301 fc06ba56 2019-08-22 stsp if (errcode)
4302 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4303 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4305 fc06ba56 2019-08-22 stsp return err;
4308 fc06ba56 2019-08-22 stsp static const struct got_error *
4309 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4311 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4312 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4313 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4314 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4315 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4316 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4317 15a94983 2018-12-23 stsp int obj_type;
4319 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4320 a5388363 2020-12-01 naddy s->path);
4322 15a94983 2018-12-23 stsp return err;
4324 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4326 84451b3e 2018-07-10 stsp goto done;
4328 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4329 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4330 84451b3e 2018-07-10 stsp goto done;
4333 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4335 a70480e0 2018-06-23 stsp goto done;
4336 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4337 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4338 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4339 84451b3e 2018-07-10 stsp goto done;
4341 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4342 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4344 1fddf795 2021-01-20 stsp goto done;
4345 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4346 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4347 84451b3e 2018-07-10 stsp goto done;
4350 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4351 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4352 b02560ec 2019-08-19 stsp blame->nlines--;
4354 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4355 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4356 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4357 84451b3e 2018-07-10 stsp goto done;
4360 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4362 bd24772e 2018-07-11 stsp goto done;
4364 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4365 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4366 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4367 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4368 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4369 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4370 245d91c1 2018-07-12 stsp goto done;
4372 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4374 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4375 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4376 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4377 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4378 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4379 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4380 a5388363 2020-12-01 naddy s->blame_complete = 0;
4382 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4383 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4384 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4385 f5a09613 2020-12-13 naddy s->selected_line = 1;
4387 aa61903a 2021-12-31 thomas s->matched_line = 0;
4391 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4392 27d434c2 2018-09-15 stsp free(obj_id);
4394 245d91c1 2018-07-12 stsp stop_blame(blame);
4395 245d91c1 2018-07-12 stsp return err;
4398 245d91c1 2018-07-12 stsp static const struct got_error *
4399 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4400 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4402 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4403 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4405 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4407 c4843652 2019-08-12 stsp s->path = strdup(path);
4408 c4843652 2019-08-12 stsp if (s->path == NULL)
4409 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4411 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4412 c4843652 2019-08-12 stsp if (err) {
4413 c4843652 2019-08-12 stsp free(s->path);
4414 7cbe629d 2018-08-04 stsp return err;
4417 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4418 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4419 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4420 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4421 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4422 fb2756b9 2018-08-04 stsp s->repo = repo;
4423 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4424 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4426 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4427 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4428 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4429 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4431 11b20872 2019-11-08 stsp return err;
4434 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4435 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4436 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4437 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4438 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4440 a5388363 2020-12-01 naddy return run_blame(view);
4443 e5a0f69f 2018-08-18 stsp static const struct got_error *
4444 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4446 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4447 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4449 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4450 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4452 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4453 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4454 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4455 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4456 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4459 e5a0f69f 2018-08-18 stsp free(s->path);
4460 11b20872 2019-11-08 stsp free_colors(&s->colors);
4462 e5a0f69f 2018-08-18 stsp return err;
4465 7cbe629d 2018-08-04 stsp static const struct got_error *
4466 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4468 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4470 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4471 6c4c42e0 2019-06-24 stsp return NULL;
4474 6c4c42e0 2019-06-24 stsp static const struct got_error *
4475 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4477 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4478 6c4c42e0 2019-06-24 stsp int lineno;
4479 826082fe 2020-12-10 stsp char *line = NULL;
4480 826082fe 2020-12-10 stsp size_t linesize = 0;
4481 826082fe 2020-12-10 stsp ssize_t linelen;
4483 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4484 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4485 6c4c42e0 2019-06-24 stsp return NULL;
4488 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4489 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4490 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4492 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4494 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4496 6c4c42e0 2019-06-24 stsp while (1) {
4497 6c4c42e0 2019-06-24 stsp off_t offset;
4499 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4500 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4501 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4505 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4506 6c4c42e0 2019-06-24 stsp lineno = 1;
4508 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4511 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4512 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4513 6c4c42e0 2019-06-24 stsp free(line);
4514 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4516 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4517 826082fe 2020-12-10 stsp if (linelen != -1 &&
4518 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4519 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4520 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4523 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4528 826082fe 2020-12-10 stsp free(line);
4530 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4531 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4532 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4535 6c4c42e0 2019-06-24 stsp return NULL;
4538 6c4c42e0 2019-06-24 stsp static const struct got_error *
4539 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4541 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4542 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4543 2b380cc8 2018-10-24 stsp int errcode;
4545 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4546 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4547 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4548 2b380cc8 2018-10-24 stsp if (errcode)
4549 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4551 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4554 51fe7530 2019-08-19 stsp if (s->blame_complete)
4555 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4557 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4559 669b5ffa 2018-10-07 stsp view_vborder(view);
4560 e5a0f69f 2018-08-18 stsp return err;
4563 e5a0f69f 2018-08-18 stsp static const struct got_error *
4564 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4566 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4567 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4568 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4569 669b5ffa 2018-10-07 stsp int begin_x = 0;
4571 e5a0f69f 2018-08-18 stsp switch (ch) {
4573 1e37a5c2 2019-05-12 jcs s->done = 1;
4575 4deef56f 2021-09-02 naddy case 'g':
4576 4deef56f 2021-09-02 naddy case KEY_HOME:
4577 4deef56f 2021-09-02 naddy s->selected_line = 1;
4578 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4580 4deef56f 2021-09-02 naddy case 'G':
4581 4deef56f 2021-09-02 naddy case KEY_END:
4582 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4583 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4584 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4586 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4587 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4588 4deef56f 2021-09-02 naddy (view->nlines - 3);
4592 1e37a5c2 2019-05-12 jcs case KEY_UP:
4593 f7140bf5 2021-10-17 thomas case CTRL('p'):
4594 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4595 1e37a5c2 2019-05-12 jcs s->selected_line--;
4596 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4597 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4598 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4600 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4601 ea025d1d 2020-02-22 naddy case CTRL('b'):
4602 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4603 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4606 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4607 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4608 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4610 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4613 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4614 f7140bf5 2021-10-17 thomas case CTRL('n'):
4615 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4616 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4617 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4618 1e37a5c2 2019-05-12 jcs s->selected_line++;
4619 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4620 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4621 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4624 1e37a5c2 2019-05-12 jcs case 'p': {
4625 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4626 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4627 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4628 1e37a5c2 2019-05-12 jcs if (id == NULL)
4630 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4631 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4632 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4633 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4634 1e37a5c2 2019-05-12 jcs int obj_type;
4635 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4636 1e37a5c2 2019-05-12 jcs s->repo, id);
4639 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4640 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4641 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4642 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4645 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4646 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4647 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4648 e5a0f69f 2018-08-18 stsp if (err) {
4649 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4650 1e37a5c2 2019-05-12 jcs err = NULL;
4651 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4654 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4656 1e37a5c2 2019-05-12 jcs free(blob_id);
4657 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4658 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4659 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4662 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4664 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4666 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4667 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4669 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4674 1e37a5c2 2019-05-12 jcs s->done = 1;
4675 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4676 1e37a5c2 2019-05-12 jcs s->done = 0;
4677 1e37a5c2 2019-05-12 jcs if (thread_err)
4679 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4680 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4681 a5388363 2020-12-01 naddy err = run_blame(view);
4686 1e37a5c2 2019-05-12 jcs case 'B': {
4687 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4688 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4689 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4691 1e37a5c2 2019-05-12 jcs s->done = 1;
4692 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4693 1e37a5c2 2019-05-12 jcs s->done = 0;
4694 1e37a5c2 2019-05-12 jcs if (thread_err)
4696 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4697 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4698 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4699 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4700 a5388363 2020-12-01 naddy err = run_blame(view);
4705 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4706 1e37a5c2 2019-05-12 jcs case '\r': {
4707 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4708 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4709 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4710 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4711 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4712 1e37a5c2 2019-05-12 jcs if (id == NULL)
4714 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4717 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4718 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4719 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4720 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4721 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4722 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4723 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4724 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4727 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4728 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4729 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4731 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4734 e78dc838 2020-12-04 stsp view->focussed = 0;
4735 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4736 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4737 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4740 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4741 e78dc838 2020-12-04 stsp view->focus_child = 1;
4743 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4748 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4749 ea025d1d 2020-02-22 naddy case CTRL('f'):
4751 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4752 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4753 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4756 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4757 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4758 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4759 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4762 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4763 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4764 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4765 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4767 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4768 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4769 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4771 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4772 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4773 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4774 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4780 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4783 a70480e0 2018-06-23 stsp static const struct got_error *
4784 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4786 a70480e0 2018-06-23 stsp const struct got_error *error;
4787 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4788 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4789 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4790 0587e10c 2020-07-23 stsp char *link_target = NULL;
4791 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4792 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4794 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4796 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4797 a70480e0 2018-06-23 stsp switch (ch) {
4799 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4802 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4803 69069811 2018-08-02 stsp if (repo_path == NULL)
4804 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4808 17020d27 2019-03-07 stsp usage_blame();
4809 a70480e0 2018-06-23 stsp /* NOTREACHED */
4813 a70480e0 2018-06-23 stsp argc -= optind;
4814 a70480e0 2018-06-23 stsp argv += optind;
4816 f135c941 2020-02-20 stsp if (argc != 1)
4817 a70480e0 2018-06-23 stsp usage_blame();
4819 69069811 2018-08-02 stsp if (repo_path == NULL) {
4820 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4821 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4822 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4823 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4824 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4825 c156c7a4 2020-12-18 stsp goto done;
4826 f135c941 2020-02-20 stsp if (worktree)
4827 eb41ed75 2019-02-05 stsp repo_path =
4828 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4830 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4831 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4832 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4833 c156c7a4 2020-12-18 stsp goto done;
4837 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4838 c02c541e 2019-03-29 stsp if (error != NULL)
4839 8e94dd5b 2019-01-04 stsp goto done;
4841 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4842 f135c941 2020-02-20 stsp worktree);
4843 c02c541e 2019-03-29 stsp if (error)
4844 92205607 2019-01-04 stsp goto done;
4846 f135c941 2020-02-20 stsp init_curses();
4848 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4849 51a10b52 2020-12-26 stsp if (error)
4850 51a10b52 2020-12-26 stsp goto done;
4852 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4853 eb41ed75 2019-02-05 stsp if (error)
4854 69069811 2018-08-02 stsp goto done;
4856 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4857 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4858 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4859 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4860 a70480e0 2018-06-23 stsp if (error != NULL)
4861 66b4983c 2018-06-23 stsp goto done;
4862 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4863 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4865 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4866 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4868 a19e88aa 2018-06-23 stsp if (error != NULL)
4869 8b473291 2019-02-21 stsp goto done;
4871 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4872 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4873 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4874 e1cd8fed 2018-08-01 stsp goto done;
4877 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4878 0587e10c 2020-07-23 stsp commit_id, repo);
4879 7cbe629d 2018-08-04 stsp if (error)
4880 7cbe629d 2018-08-04 stsp goto done;
4882 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4883 78756c87 2020-11-24 stsp commit_id, repo);
4884 0587e10c 2020-07-23 stsp if (error)
4885 0587e10c 2020-07-23 stsp goto done;
4886 12314ad4 2019-08-31 stsp if (worktree) {
4887 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4888 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4889 12314ad4 2019-08-31 stsp worktree = NULL;
4891 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4893 69069811 2018-08-02 stsp free(repo_path);
4894 f135c941 2020-02-20 stsp free(in_repo_path);
4895 0587e10c 2020-07-23 stsp free(link_target);
4896 69069811 2018-08-02 stsp free(cwd);
4897 a70480e0 2018-06-23 stsp free(commit_id);
4898 eb41ed75 2019-02-05 stsp if (worktree)
4899 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4900 1d0f4054 2021-06-17 stsp if (repo) {
4901 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4902 1d0f4054 2021-06-17 stsp if (error == NULL)
4903 1d0f4054 2021-06-17 stsp error = close_err;
4905 51a10b52 2020-12-26 stsp tog_free_refs();
4906 a70480e0 2018-06-23 stsp return error;
4909 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4910 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4912 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4913 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4914 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4915 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4916 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4917 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4918 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4920 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4922 f7d12f7e 2018-08-01 stsp werase(view->window);
4924 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4925 ffd1d5e5 2018-06-23 stsp return NULL;
4927 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4929 ffd1d5e5 2018-06-23 stsp return err;
4930 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4931 a3404814 2018-09-02 stsp wstandout(view->window);
4932 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4934 11b20872 2019-11-08 stsp wattr_on(view->window,
4935 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4936 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4938 11b20872 2019-11-08 stsp wattr_off(view->window,
4939 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4940 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4941 a3404814 2018-09-02 stsp wstandend(view->window);
4942 2550e4c3 2018-07-13 stsp free(wline);
4943 2550e4c3 2018-07-13 stsp wline = NULL;
4944 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4945 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4946 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4947 ffd1d5e5 2018-06-23 stsp return NULL;
4948 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4950 ce52c690 2018-06-23 stsp return err;
4951 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4952 2550e4c3 2018-07-13 stsp free(wline);
4953 2550e4c3 2018-07-13 stsp wline = NULL;
4954 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4955 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4956 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4957 ffd1d5e5 2018-06-23 stsp return NULL;
4958 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4959 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4960 a1eca9bb 2018-06-23 stsp return NULL;
4962 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4963 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4964 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4965 0cf4efb1 2018-09-29 stsp if (view->focussed)
4966 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4967 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4969 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4970 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4971 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4972 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4973 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4974 ffd1d5e5 2018-06-23 stsp return NULL;
4978 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4981 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4982 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4983 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4984 848d6979 2019-08-12 stsp const char *modestr = "";
4985 56e0773d 2019-11-28 stsp mode_t mode;
4987 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4988 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4990 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4991 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4992 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4994 638f9024 2019-05-13 stsp return got_error_from_errno(
4995 230a42bd 2019-05-11 jcs "got_object_id_str");
4997 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4998 63c5ca5d 2019-08-24 stsp modestr = "$";
4999 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5002 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5003 d86d3b18 2020-12-01 naddy te, s->repo);
5004 0d6c6ee3 2020-05-20 stsp if (err) {
5005 0d6c6ee3 2020-05-20 stsp free(id_str);
5006 0d6c6ee3 2020-05-20 stsp return err;
5008 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5009 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5010 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5012 848d6979 2019-08-12 stsp modestr = "@";
5014 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5015 848d6979 2019-08-12 stsp modestr = "/";
5016 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5017 848d6979 2019-08-12 stsp modestr = "*";
5018 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5019 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5020 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5021 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5022 1d13200f 2018-07-12 stsp free(id_str);
5023 0d6c6ee3 2020-05-20 stsp free(link_target);
5024 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5026 1d13200f 2018-07-12 stsp free(id_str);
5027 0d6c6ee3 2020-05-20 stsp free(link_target);
5028 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5029 ffd1d5e5 2018-06-23 stsp if (err) {
5030 ffd1d5e5 2018-06-23 stsp free(line);
5033 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5034 0cf4efb1 2018-09-29 stsp if (view->focussed)
5035 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5036 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5038 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5040 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5041 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5042 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5044 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5045 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5046 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5047 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5048 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5049 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5050 ffd1d5e5 2018-06-23 stsp free(line);
5051 2550e4c3 2018-07-13 stsp free(wline);
5052 2550e4c3 2018-07-13 stsp wline = NULL;
5054 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5055 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5056 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5060 ffd1d5e5 2018-06-23 stsp return err;
5063 ffd1d5e5 2018-06-23 stsp static void
5064 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5066 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5067 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5068 fa86c4bf 2020-11-29 stsp int i = 0;
5070 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5073 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5074 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5075 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5076 fa86c4bf 2020-11-29 stsp if (!isroot)
5077 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5080 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5081 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5085 fa86c4bf 2020-11-29 stsp static void
5086 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5088 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5089 ffd1d5e5 2018-06-23 stsp int n = 0;
5091 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5092 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5093 694d3271 2020-12-01 naddy s->first_displayed_entry);
5095 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5097 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5098 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5099 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5100 768394f3 2019-01-24 stsp if (last) {
5101 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5102 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5107 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5108 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5109 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5111 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5112 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5113 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5115 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5116 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5117 56e0773d 2019-11-28 stsp + 1 /* slash */;
5119 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5121 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5122 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5123 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5125 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5126 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5127 d9765a41 2018-06-23 stsp while (pt) {
5128 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5129 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5130 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5131 cb2ebc8a 2018-06-23 stsp goto done;
5133 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5134 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5135 cb2ebc8a 2018-06-23 stsp goto done;
5137 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5140 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5141 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5142 ce52c690 2018-06-23 stsp goto done;
5146 ce52c690 2018-06-23 stsp if (err) {
5147 ce52c690 2018-06-23 stsp free(*path);
5148 ce52c690 2018-06-23 stsp *path = NULL;
5150 ce52c690 2018-06-23 stsp return err;
5153 ce52c690 2018-06-23 stsp static const struct got_error *
5154 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5155 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5156 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5158 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5159 ce52c690 2018-06-23 stsp char *path;
5160 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5162 a0de39f3 2019-08-09 stsp *new_view = NULL;
5164 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5166 ce52c690 2018-06-23 stsp return err;
5168 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5169 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5170 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5171 83ce39e3 2019-08-12 stsp goto done;
5174 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5175 e5a0f69f 2018-08-18 stsp if (err) {
5176 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5177 fc06ba56 2019-08-22 stsp err = NULL;
5178 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5180 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5182 83ce39e3 2019-08-12 stsp free(path);
5183 69efd4c4 2018-07-18 stsp return err;
5186 69efd4c4 2018-07-18 stsp static const struct got_error *
5187 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5188 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5190 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5191 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5192 69efd4c4 2018-07-18 stsp char *path;
5194 a0de39f3 2019-08-09 stsp *new_view = NULL;
5196 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5197 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5198 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5200 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5202 69efd4c4 2018-07-18 stsp return err;
5204 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5207 e5a0f69f 2018-08-18 stsp view_close(log_view);
5209 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5210 cb2ebc8a 2018-06-23 stsp free(path);
5211 cb2ebc8a 2018-06-23 stsp return err;
5214 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5215 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5216 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5218 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5219 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5220 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5221 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5223 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5224 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5226 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5227 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5228 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5230 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5232 bc573f3b 2021-07-10 stsp goto done;
5235 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5236 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5237 bc573f3b 2021-07-10 stsp * closed on demand.
5239 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5240 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5242 bc573f3b 2021-07-10 stsp goto done;
5243 bc573f3b 2021-07-10 stsp s->tree = s->root;
5245 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5246 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5247 ffd1d5e5 2018-06-23 stsp goto done;
5249 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5250 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5251 ffd1d5e5 2018-06-23 stsp goto done;
5254 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5255 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5256 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5257 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5258 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5259 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5260 9cd7cbd1 2020-12-07 stsp goto done;
5263 fb2756b9 2018-08-04 stsp s->repo = repo;
5265 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5266 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5267 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5268 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5270 c0b01bdb 2019-11-08 stsp goto done;
5271 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5272 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5274 c0b01bdb 2019-11-08 stsp goto done;
5275 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5276 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5277 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5279 c0b01bdb 2019-11-08 stsp goto done;
5281 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5282 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5283 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5285 11b20872 2019-11-08 stsp goto done;
5287 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5288 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5290 c0b01bdb 2019-11-08 stsp goto done;
5293 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5294 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5295 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5296 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5297 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5299 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5300 bc573f3b 2021-07-10 stsp if (commit)
5301 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5303 bc573f3b 2021-07-10 stsp close_tree_view(view);
5304 ad80ab7b 2018-08-04 stsp return err;
5307 e5a0f69f 2018-08-18 stsp static const struct got_error *
5308 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5310 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5312 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5313 fb2756b9 2018-08-04 stsp free(s->tree_label);
5314 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5315 6484ec90 2018-09-29 stsp free(s->commit_id);
5316 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5317 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5318 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5319 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5320 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5321 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5322 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5323 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5324 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5325 ad80ab7b 2018-08-04 stsp free(parent);
5328 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5329 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5330 bc573f3b 2021-07-10 stsp if (s->root)
5331 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5332 7c32bd05 2019-06-22 stsp return NULL;
5335 7c32bd05 2019-06-22 stsp static const struct got_error *
5336 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5338 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5340 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5341 7c32bd05 2019-06-22 stsp return NULL;
5344 7c32bd05 2019-06-22 stsp static int
5345 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5347 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5349 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5353 7c32bd05 2019-06-22 stsp static const struct got_error *
5354 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5356 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5357 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5359 7c32bd05 2019-06-22 stsp if (!view->searching) {
5360 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5361 7c32bd05 2019-06-22 stsp return NULL;
5364 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5365 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5366 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5367 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5368 56e0773d 2019-11-28 stsp s->selected_entry);
5370 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5372 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5373 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5375 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5376 56e0773d 2019-11-28 stsp s->selected_entry);
5379 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5380 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5381 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5382 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5384 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5387 7c32bd05 2019-06-22 stsp while (1) {
5388 56e0773d 2019-11-28 stsp if (te == NULL) {
5389 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5390 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5391 ac66afb8 2019-06-24 stsp return NULL;
5393 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5394 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5396 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5399 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5400 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5401 56e0773d 2019-11-28 stsp s->matched_entry = te;
5405 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5406 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5408 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5411 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5412 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5413 7c32bd05 2019-06-22 stsp s->selected = 0;
5416 e5a0f69f 2018-08-18 stsp return NULL;
5419 ad80ab7b 2018-08-04 stsp static const struct got_error *
5420 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5422 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5423 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5424 e5a0f69f 2018-08-18 stsp char *parent_path;
5426 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5428 e5a0f69f 2018-08-18 stsp return err;
5430 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5431 e5a0f69f 2018-08-18 stsp free(parent_path);
5433 669b5ffa 2018-10-07 stsp view_vborder(view);
5434 e5a0f69f 2018-08-18 stsp return err;
5437 e5a0f69f 2018-08-18 stsp static const struct got_error *
5438 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5440 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5441 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5442 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5443 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5444 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
5446 e5a0f69f 2018-08-18 stsp switch (ch) {
5448 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5451 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5453 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5454 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5455 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5456 e78dc838 2020-12-04 stsp view->focussed = 0;
5457 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5458 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5459 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5461 1e37a5c2 2019-05-12 jcs return err;
5462 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5463 e78dc838 2020-12-04 stsp view->focus_child = 1;
5465 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5468 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5469 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5470 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5471 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5472 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5473 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5474 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5475 152c1c93 2020-11-29 stsp if (err) {
5476 152c1c93 2020-11-29 stsp view_close(ref_view);
5477 152c1c93 2020-11-29 stsp return err;
5479 e78dc838 2020-12-04 stsp view->focussed = 0;
5480 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5481 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5482 152c1c93 2020-11-29 stsp err = view_close_child(view);
5484 152c1c93 2020-11-29 stsp return err;
5485 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5486 e78dc838 2020-12-04 stsp view->focus_child = 1;
5488 152c1c93 2020-11-29 stsp *new_view = ref_view;
5490 e4526bf5 2021-09-03 naddy case 'g':
5491 e4526bf5 2021-09-03 naddy case KEY_HOME:
5492 e4526bf5 2021-09-03 naddy s->selected = 0;
5493 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5494 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5495 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5497 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5499 e4526bf5 2021-09-03 naddy case 'G':
5500 e4526bf5 2021-09-03 naddy case KEY_END:
5501 e4526bf5 2021-09-03 naddy s->selected = 0;
5502 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5503 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5504 e4526bf5 2021-09-03 naddy if (te == NULL) {
5505 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5506 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5511 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5512 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5514 e4526bf5 2021-09-03 naddy if (n > 0)
5515 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5518 1e37a5c2 2019-05-12 jcs case KEY_UP:
5519 f7140bf5 2021-10-17 thomas case CTRL('p'):
5520 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5521 1e37a5c2 2019-05-12 jcs s->selected--;
5524 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5526 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5527 ea025d1d 2020-02-22 naddy case CTRL('b'):
5528 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5529 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5530 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5531 fa86c4bf 2020-11-29 stsp s->selected = 0;
5533 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5534 fa86c4bf 2020-11-29 stsp s->selected = 0;
5536 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5539 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5540 f7140bf5 2021-10-17 thomas case CTRL('n'):
5541 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5542 1e37a5c2 2019-05-12 jcs s->selected++;
5545 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5547 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5549 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5551 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5552 ea025d1d 2020-02-22 naddy case CTRL('f'):
5553 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5555 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5556 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5557 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5560 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5562 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5564 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5565 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5566 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5567 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5568 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5570 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5571 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5573 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5574 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5575 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5576 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5577 1e37a5c2 2019-05-12 jcs s->selected_entry =
5578 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5579 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5580 1e37a5c2 2019-05-12 jcs free(parent);
5581 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5582 56e0773d 2019-11-28 stsp s->selected_entry))) {
5583 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5584 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5585 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5588 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5589 941e9f74 2019-05-21 stsp if (err) {
5590 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5593 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5594 56e0773d 2019-11-28 stsp s->selected_entry))) {
5595 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5596 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5597 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5599 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5600 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5601 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5604 e78dc838 2020-12-04 stsp view->focussed = 0;
5605 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5606 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5607 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5609 669b5ffa 2018-10-07 stsp return err;
5610 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5611 e78dc838 2020-12-04 stsp view->focus_child = 1;
5613 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5616 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5617 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5618 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5624 ffd1d5e5 2018-06-23 stsp return err;
5627 ffd1d5e5 2018-06-23 stsp __dead static void
5628 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5631 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5632 ffd1d5e5 2018-06-23 stsp getprogname());
5636 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5637 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5639 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5640 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5641 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5642 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5643 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5644 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5645 4e97c21c 2020-12-06 stsp char *label = NULL;
5646 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5647 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5649 5221c383 2018-08-01 stsp struct tog_view *view;
5651 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5652 ffd1d5e5 2018-06-23 stsp switch (ch) {
5654 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5657 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5658 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5659 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5663 e99e2d15 2020-11-24 naddy usage_tree();
5664 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5668 ffd1d5e5 2018-06-23 stsp argc -= optind;
5669 ffd1d5e5 2018-06-23 stsp argv += optind;
5671 55cccc34 2020-02-20 stsp if (argc > 1)
5672 e99e2d15 2020-11-24 naddy usage_tree();
5674 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5675 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5676 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5677 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5678 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5679 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5680 c156c7a4 2020-12-18 stsp goto done;
5681 55cccc34 2020-02-20 stsp if (worktree)
5682 52185f70 2019-02-05 stsp repo_path =
5683 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5685 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5686 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5687 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5688 c156c7a4 2020-12-18 stsp goto done;
5692 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5693 c02c541e 2019-03-29 stsp if (error != NULL)
5694 52185f70 2019-02-05 stsp goto done;
5696 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5697 55cccc34 2020-02-20 stsp repo, worktree);
5698 55cccc34 2020-02-20 stsp if (error)
5699 55cccc34 2020-02-20 stsp goto done;
5701 55cccc34 2020-02-20 stsp init_curses();
5703 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5704 c02c541e 2019-03-29 stsp if (error)
5705 52185f70 2019-02-05 stsp goto done;
5707 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5708 51a10b52 2020-12-26 stsp if (error)
5709 51a10b52 2020-12-26 stsp goto done;
5711 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5712 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5713 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5714 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5715 4e97c21c 2020-12-06 stsp if (error)
5716 4e97c21c 2020-12-06 stsp goto done;
5717 4e97c21c 2020-12-06 stsp head_ref_name = label;
5719 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5720 4e97c21c 2020-12-06 stsp if (error == NULL)
5721 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5722 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5723 4e97c21c 2020-12-06 stsp goto done;
5724 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5725 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5726 4e97c21c 2020-12-06 stsp if (error)
5727 4e97c21c 2020-12-06 stsp goto done;
5730 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5731 5221c383 2018-08-01 stsp if (view == NULL) {
5732 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5733 5221c383 2018-08-01 stsp goto done;
5735 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5736 ad80ab7b 2018-08-04 stsp if (error)
5737 ad80ab7b 2018-08-04 stsp goto done;
5738 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5739 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5740 d91faf3b 2020-12-01 naddy in_repo_path);
5741 55cccc34 2020-02-20 stsp if (error)
5742 55cccc34 2020-02-20 stsp goto done;
5745 55cccc34 2020-02-20 stsp if (worktree) {
5746 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5747 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5748 55cccc34 2020-02-20 stsp worktree = NULL;
5750 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5752 52185f70 2019-02-05 stsp free(repo_path);
5753 e4a0e26d 2020-02-20 stsp free(cwd);
5754 ffd1d5e5 2018-06-23 stsp free(commit_id);
5755 4e97c21c 2020-12-06 stsp free(label);
5757 486cd271 2020-12-06 stsp got_ref_close(ref);
5758 1d0f4054 2021-06-17 stsp if (repo) {
5759 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5760 1d0f4054 2021-06-17 stsp if (error == NULL)
5761 1d0f4054 2021-06-17 stsp error = close_err;
5763 51a10b52 2020-12-26 stsp tog_free_refs();
5764 ffd1d5e5 2018-06-23 stsp return error;
5767 6458efa5 2020-11-24 stsp static const struct got_error *
5768 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5770 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5771 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5773 6458efa5 2020-11-24 stsp s->nrefs = 0;
5774 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5775 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5778 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5779 6458efa5 2020-11-24 stsp if (re == NULL)
5780 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5782 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5783 8924d611 2020-12-26 stsp if (re->ref == NULL)
5784 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5785 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5786 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5789 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5790 6458efa5 2020-11-24 stsp return NULL;
5794 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5796 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5798 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5799 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5800 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5801 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5806 6458efa5 2020-11-24 stsp static const struct got_error *
5807 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5809 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5810 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5812 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5813 6458efa5 2020-11-24 stsp s->repo = repo;
5815 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5816 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5818 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5820 6458efa5 2020-11-24 stsp return err;
5822 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5823 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5824 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5825 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5827 6458efa5 2020-11-24 stsp goto done;
5829 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5830 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5831 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5833 6458efa5 2020-11-24 stsp goto done;
5835 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5836 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5837 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5839 6458efa5 2020-11-24 stsp goto done;
5842 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5843 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5844 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5845 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5846 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5849 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5850 6458efa5 2020-11-24 stsp return err;
5853 6458efa5 2020-11-24 stsp static const struct got_error *
5854 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5856 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5858 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5859 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5861 6458efa5 2020-11-24 stsp return NULL;
5864 6458efa5 2020-11-24 stsp static const struct got_error *
5865 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5866 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5868 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5869 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5870 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5871 6458efa5 2020-11-24 stsp int obj_type;
5873 c42c9805 2020-11-24 stsp *commit_id = NULL;
5875 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5877 6458efa5 2020-11-24 stsp return err;
5879 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5881 6458efa5 2020-11-24 stsp goto done;
5883 6458efa5 2020-11-24 stsp switch (obj_type) {
5884 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5885 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5887 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5888 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5890 6458efa5 2020-11-24 stsp goto done;
5891 c42c9805 2020-11-24 stsp free(obj_id);
5892 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5893 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5895 6458efa5 2020-11-24 stsp goto done;
5896 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5897 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5898 c42c9805 2020-11-24 stsp goto done;
5900 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5901 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5902 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5903 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5904 c42c9805 2020-11-24 stsp goto done;
5908 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5914 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5915 c42c9805 2020-11-24 stsp if (err) {
5916 c42c9805 2020-11-24 stsp free(*commit_id);
5917 c42c9805 2020-11-24 stsp *commit_id = NULL;
5919 c42c9805 2020-11-24 stsp return err;
5922 c42c9805 2020-11-24 stsp static const struct got_error *
5923 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5924 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5926 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5927 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5928 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5930 c42c9805 2020-11-24 stsp *new_view = NULL;
5932 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5933 c42c9805 2020-11-24 stsp if (err) {
5934 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5935 c42c9805 2020-11-24 stsp return err;
5937 c42c9805 2020-11-24 stsp return NULL;
5940 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5941 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5942 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5943 6458efa5 2020-11-24 stsp goto done;
5946 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5947 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5950 6458efa5 2020-11-24 stsp view_close(log_view);
5952 6458efa5 2020-11-24 stsp *new_view = log_view;
5953 c42c9805 2020-11-24 stsp free(commit_id);
5954 6458efa5 2020-11-24 stsp return err;
5957 ce5b7c56 2019-07-09 stsp static void
5958 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5960 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5961 34ba6917 2020-11-30 stsp int i = 0;
5963 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5966 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5967 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5968 34ba6917 2020-11-30 stsp if (re == NULL)
5970 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5971 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5975 34ba6917 2020-11-30 stsp static void
5976 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5978 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5979 6458efa5 2020-11-24 stsp int n = 0;
5981 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5982 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5984 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5986 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5987 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5988 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5989 6458efa5 2020-11-24 stsp if (last) {
5990 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5991 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5996 6458efa5 2020-11-24 stsp static const struct got_error *
5997 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5999 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6001 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6002 6458efa5 2020-11-24 stsp return NULL;
6005 6458efa5 2020-11-24 stsp static int
6006 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6008 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6010 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6014 6458efa5 2020-11-24 stsp static const struct got_error *
6015 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6017 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6018 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6020 6458efa5 2020-11-24 stsp if (!view->searching) {
6021 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6022 6458efa5 2020-11-24 stsp return NULL;
6025 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6026 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6027 6458efa5 2020-11-24 stsp if (s->selected_entry)
6028 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6030 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6031 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6033 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6034 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6036 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6037 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6040 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6041 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6042 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6043 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6045 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6048 6458efa5 2020-11-24 stsp while (1) {
6049 6458efa5 2020-11-24 stsp if (re == NULL) {
6050 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6051 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6052 6458efa5 2020-11-24 stsp return NULL;
6054 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6055 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6057 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6060 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6061 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6062 6458efa5 2020-11-24 stsp s->matched_entry = re;
6066 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6067 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6069 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6072 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6073 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6074 6458efa5 2020-11-24 stsp s->selected = 0;
6077 6458efa5 2020-11-24 stsp return NULL;
6080 6458efa5 2020-11-24 stsp static const struct got_error *
6081 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6083 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6084 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6085 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6086 6458efa5 2020-11-24 stsp char *line = NULL;
6087 6458efa5 2020-11-24 stsp wchar_t *wline;
6088 6458efa5 2020-11-24 stsp struct tog_color *tc;
6089 6458efa5 2020-11-24 stsp int width, n;
6090 6458efa5 2020-11-24 stsp int limit = view->nlines;
6092 6458efa5 2020-11-24 stsp werase(view->window);
6094 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6096 6458efa5 2020-11-24 stsp if (limit == 0)
6097 6458efa5 2020-11-24 stsp return NULL;
6099 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6101 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6102 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6103 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6105 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6106 6458efa5 2020-11-24 stsp if (err) {
6107 6458efa5 2020-11-24 stsp free(line);
6108 6458efa5 2020-11-24 stsp return err;
6110 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6111 6458efa5 2020-11-24 stsp wstandout(view->window);
6112 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6113 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6114 6458efa5 2020-11-24 stsp wstandend(view->window);
6115 6458efa5 2020-11-24 stsp free(wline);
6116 6458efa5 2020-11-24 stsp wline = NULL;
6117 6458efa5 2020-11-24 stsp free(line);
6118 6458efa5 2020-11-24 stsp line = NULL;
6119 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6120 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6121 6458efa5 2020-11-24 stsp if (--limit <= 0)
6122 6458efa5 2020-11-24 stsp return NULL;
6125 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6126 6458efa5 2020-11-24 stsp char *line = NULL;
6128 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6129 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6130 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6131 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6132 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6133 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6134 6458efa5 2020-11-24 stsp struct got_object_id *id;
6135 6458efa5 2020-11-24 stsp char *id_str;
6136 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6138 6458efa5 2020-11-24 stsp return err;
6139 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6140 6458efa5 2020-11-24 stsp if (err) {
6142 6458efa5 2020-11-24 stsp return err;
6144 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6145 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6146 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6148 6458efa5 2020-11-24 stsp free(id_str);
6149 6458efa5 2020-11-24 stsp return err;
6152 6458efa5 2020-11-24 stsp free(id_str);
6154 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6155 6458efa5 2020-11-24 stsp if (line == NULL)
6156 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6159 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6160 6458efa5 2020-11-24 stsp if (err) {
6161 6458efa5 2020-11-24 stsp free(line);
6162 6458efa5 2020-11-24 stsp return err;
6164 6458efa5 2020-11-24 stsp if (n == s->selected) {
6165 6458efa5 2020-11-24 stsp if (view->focussed)
6166 6458efa5 2020-11-24 stsp wstandout(view->window);
6167 6458efa5 2020-11-24 stsp s->selected_entry = re;
6169 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6171 6458efa5 2020-11-24 stsp wattr_on(view->window,
6172 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6173 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6175 6458efa5 2020-11-24 stsp wattr_off(view->window,
6176 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6177 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6178 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6179 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6180 6458efa5 2020-11-24 stsp wstandend(view->window);
6181 6458efa5 2020-11-24 stsp free(line);
6182 6458efa5 2020-11-24 stsp free(wline);
6183 6458efa5 2020-11-24 stsp wline = NULL;
6185 6458efa5 2020-11-24 stsp s->ndisplayed++;
6186 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6189 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6192 6458efa5 2020-11-24 stsp view_vborder(view);
6193 6458efa5 2020-11-24 stsp return err;
6196 6458efa5 2020-11-24 stsp static const struct got_error *
6197 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6198 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6200 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6201 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6202 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6204 c42c9805 2020-11-24 stsp *new_view = NULL;
6206 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6207 c42c9805 2020-11-24 stsp if (err) {
6208 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6209 c42c9805 2020-11-24 stsp return err;
6211 c42c9805 2020-11-24 stsp return NULL;
6215 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6216 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6217 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6218 c42c9805 2020-11-24 stsp goto done;
6221 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6222 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6224 c42c9805 2020-11-24 stsp goto done;
6226 c42c9805 2020-11-24 stsp *new_view = tree_view;
6228 c42c9805 2020-11-24 stsp free(commit_id);
6229 c42c9805 2020-11-24 stsp return err;
6231 c42c9805 2020-11-24 stsp static const struct got_error *
6232 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6234 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6235 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6236 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6237 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6238 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
6240 6458efa5 2020-11-24 stsp switch (ch) {
6242 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6244 98182bd0 2021-11-20 thomas case 'o':
6245 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6246 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6247 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6248 c591440f 2021-11-20 thomas got_ref_cmp_by_name, s->repo);
6249 c591440f 2021-11-20 thomas if (err)
6251 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6252 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6253 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6254 3bfadbd4 2021-11-20 thomas if (err)
6256 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6257 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6259 6458efa5 2020-11-24 stsp case KEY_ENTER:
6260 6458efa5 2020-11-24 stsp case '\r':
6261 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6263 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6264 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6265 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6267 e78dc838 2020-12-04 stsp view->focussed = 0;
6268 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6269 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6270 6458efa5 2020-11-24 stsp err = view_close_child(view);
6272 6458efa5 2020-11-24 stsp return err;
6273 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6274 e78dc838 2020-12-04 stsp view->focus_child = 1;
6276 6458efa5 2020-11-24 stsp *new_view = log_view;
6279 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6281 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6282 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6283 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6285 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6287 e78dc838 2020-12-04 stsp view->focussed = 0;
6288 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6289 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6290 c42c9805 2020-11-24 stsp err = view_close_child(view);
6292 c42c9805 2020-11-24 stsp return err;
6293 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6294 e78dc838 2020-12-04 stsp view->focus_child = 1;
6296 c42c9805 2020-11-24 stsp *new_view = tree_view;
6298 e4526bf5 2021-09-03 naddy case 'g':
6299 e4526bf5 2021-09-03 naddy case KEY_HOME:
6300 e4526bf5 2021-09-03 naddy s->selected = 0;
6301 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6303 e4526bf5 2021-09-03 naddy case 'G':
6304 e4526bf5 2021-09-03 naddy case KEY_END:
6305 e4526bf5 2021-09-03 naddy s->selected = 0;
6306 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6307 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6308 e4526bf5 2021-09-03 naddy if (re == NULL)
6310 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6311 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6313 e4526bf5 2021-09-03 naddy if (n > 0)
6314 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6317 6458efa5 2020-11-24 stsp case KEY_UP:
6318 f7140bf5 2021-10-17 thomas case CTRL('p'):
6319 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6320 6458efa5 2020-11-24 stsp s->selected--;
6323 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6325 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6326 6458efa5 2020-11-24 stsp case CTRL('b'):
6327 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6328 34ba6917 2020-11-30 stsp s->selected = 0;
6329 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6332 6458efa5 2020-11-24 stsp case KEY_DOWN:
6333 f7140bf5 2021-10-17 thomas case CTRL('n'):
6334 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6335 6458efa5 2020-11-24 stsp s->selected++;
6338 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6339 6458efa5 2020-11-24 stsp /* can't scroll any further */
6341 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6343 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6344 6458efa5 2020-11-24 stsp case CTRL('f'):
6345 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6346 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6347 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6348 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6351 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6353 6458efa5 2020-11-24 stsp case CTRL('l'):
6354 8924d611 2020-12-26 stsp tog_free_refs();
6355 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6358 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6359 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6361 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6362 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6363 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6369 6458efa5 2020-11-24 stsp return err;
6372 6458efa5 2020-11-24 stsp __dead static void
6373 6458efa5 2020-11-24 stsp usage_ref(void)
6376 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6377 6458efa5 2020-11-24 stsp getprogname());
6381 6458efa5 2020-11-24 stsp static const struct got_error *
6382 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6384 6458efa5 2020-11-24 stsp const struct got_error *error;
6385 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6386 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6387 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6389 6458efa5 2020-11-24 stsp struct tog_view *view;
6391 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6392 6458efa5 2020-11-24 stsp switch (ch) {
6394 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6395 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6396 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6400 e99e2d15 2020-11-24 naddy usage_ref();
6401 6458efa5 2020-11-24 stsp /* NOTREACHED */
6405 6458efa5 2020-11-24 stsp argc -= optind;
6406 6458efa5 2020-11-24 stsp argv += optind;
6408 6458efa5 2020-11-24 stsp if (argc > 1)
6409 e99e2d15 2020-11-24 naddy usage_ref();
6411 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6412 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6413 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6414 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6415 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6416 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6417 c156c7a4 2020-12-18 stsp goto done;
6418 6458efa5 2020-11-24 stsp if (worktree)
6419 6458efa5 2020-11-24 stsp repo_path =
6420 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6422 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6423 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6424 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6425 c156c7a4 2020-12-18 stsp goto done;
6429 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6430 6458efa5 2020-11-24 stsp if (error != NULL)
6431 6458efa5 2020-11-24 stsp goto done;
6433 6458efa5 2020-11-24 stsp init_curses();
6435 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6436 51a10b52 2020-12-26 stsp if (error)
6437 51a10b52 2020-12-26 stsp goto done;
6439 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6440 6458efa5 2020-11-24 stsp if (error)
6441 6458efa5 2020-11-24 stsp goto done;
6443 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6444 6458efa5 2020-11-24 stsp if (view == NULL) {
6445 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6446 6458efa5 2020-11-24 stsp goto done;
6449 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6450 6458efa5 2020-11-24 stsp if (error)
6451 6458efa5 2020-11-24 stsp goto done;
6453 6458efa5 2020-11-24 stsp if (worktree) {
6454 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6455 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6456 6458efa5 2020-11-24 stsp worktree = NULL;
6458 6458efa5 2020-11-24 stsp error = view_loop(view);
6460 6458efa5 2020-11-24 stsp free(repo_path);
6461 6458efa5 2020-11-24 stsp free(cwd);
6462 1d0f4054 2021-06-17 stsp if (repo) {
6463 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6464 1d0f4054 2021-06-17 stsp if (close_err)
6465 1d0f4054 2021-06-17 stsp error = close_err;
6467 51a10b52 2020-12-26 stsp tog_free_refs();
6468 6458efa5 2020-11-24 stsp return error;
6471 6458efa5 2020-11-24 stsp static void
6472 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6476 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6477 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6478 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6479 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6481 6879ba42 2020-10-01 naddy fputc('\n', fp);
6484 4ed7e80c 2018-05-20 stsp __dead static void
6485 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6487 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6489 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6490 6879ba42 2020-10-01 naddy getprogname());
6491 6879ba42 2020-10-01 naddy if (hflag) {
6492 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6493 6879ba42 2020-10-01 naddy list_commands(fp);
6495 6879ba42 2020-10-01 naddy exit(status);
6498 c2301be8 2018-04-30 stsp static char **
6499 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6501 ee85c5e8 2020-02-29 stsp va_list ap;
6502 c2301be8 2018-04-30 stsp char **argv;
6505 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6507 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6508 c2301be8 2018-04-30 stsp if (argv == NULL)
6509 c2301be8 2018-04-30 stsp err(1, "calloc");
6510 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6511 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6512 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6513 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6516 ee85c5e8 2020-02-29 stsp va_end(ap);
6517 c2301be8 2018-04-30 stsp return argv;
6521 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6522 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6523 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6524 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6525 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6527 ee85c5e8 2020-02-29 stsp static const struct got_error *
6528 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6530 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6531 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6532 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6533 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6534 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6535 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6536 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6538 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6539 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6540 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6542 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6543 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6544 ee85c5e8 2020-02-29 stsp goto done;
6546 ee85c5e8 2020-02-29 stsp if (worktree)
6547 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6549 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6550 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6551 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6552 ee85c5e8 2020-02-29 stsp goto done;
6555 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6556 ee85c5e8 2020-02-29 stsp if (error != NULL)
6557 ee85c5e8 2020-02-29 stsp goto done;
6559 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6560 ee85c5e8 2020-02-29 stsp repo, worktree);
6561 ee85c5e8 2020-02-29 stsp if (error)
6562 ee85c5e8 2020-02-29 stsp goto done;
6564 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6565 84de9106 2020-12-26 stsp if (error)
6566 84de9106 2020-12-26 stsp goto done;
6567 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6568 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6569 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6570 ee85c5e8 2020-02-29 stsp if (error)
6571 ee85c5e8 2020-02-29 stsp goto done;
6573 ee85c5e8 2020-02-29 stsp if (worktree) {
6574 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6575 ee85c5e8 2020-02-29 stsp worktree = NULL;
6578 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6579 ee85c5e8 2020-02-29 stsp if (error) {
6580 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6581 ee85c5e8 2020-02-29 stsp goto done;
6582 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6583 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6584 6879ba42 2020-10-01 naddy usage(1, 1);
6585 ee85c5e8 2020-02-29 stsp /* not reached */
6588 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6589 1d0f4054 2021-06-17 stsp if (error == NULL)
6590 1d0f4054 2021-06-17 stsp error = close_err;
6591 ee85c5e8 2020-02-29 stsp repo = NULL;
6593 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6594 ee85c5e8 2020-02-29 stsp if (error)
6595 ee85c5e8 2020-02-29 stsp goto done;
6597 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6599 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6600 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6602 1d0f4054 2021-06-17 stsp if (repo) {
6603 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6604 1d0f4054 2021-06-17 stsp if (error == NULL)
6605 1d0f4054 2021-06-17 stsp error = close_err;
6607 ee85c5e8 2020-02-29 stsp if (worktree)
6608 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6610 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6611 ee85c5e8 2020-02-29 stsp free(commit_id);
6612 ee85c5e8 2020-02-29 stsp free(cwd);
6613 ee85c5e8 2020-02-29 stsp free(repo_path);
6614 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6615 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6617 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6618 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6619 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6621 87670572 2020-12-26 naddy tog_free_refs();
6622 ee85c5e8 2020-02-29 stsp return error;
6626 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6628 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6629 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6630 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6631 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6632 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6633 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6634 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6637 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6639 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6640 9f7d7167 2018-04-29 stsp switch (ch) {
6642 9f7d7167 2018-04-29 stsp hflag = 1;
6645 53ccebc2 2019-07-30 stsp Vflag = 1;
6648 6879ba42 2020-10-01 naddy usage(hflag, 1);
6649 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6653 9f7d7167 2018-04-29 stsp argc -= optind;
6654 9f7d7167 2018-04-29 stsp argv += optind;
6655 9814e6a3 2020-09-27 naddy optind = 1;
6656 c2301be8 2018-04-30 stsp optreset = 1;
6658 53ccebc2 2019-07-30 stsp if (Vflag) {
6659 53ccebc2 2019-07-30 stsp got_version_print_str();
6660 6879ba42 2020-10-01 naddy return 0;
6663 4010e238 2020-12-04 stsp #ifndef PROFILE
6664 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6665 4010e238 2020-12-04 stsp NULL) == -1)
6666 4010e238 2020-12-04 stsp err(1, "pledge");
6669 c2301be8 2018-04-30 stsp if (argc == 0) {
6670 f29d3e89 2018-06-23 stsp if (hflag)
6671 6879ba42 2020-10-01 naddy usage(hflag, 0);
6672 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6673 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6675 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6679 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6680 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6681 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6682 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6683 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6689 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6690 ee85c5e8 2020-02-29 stsp if (argc != 1)
6691 6879ba42 2020-10-01 naddy usage(0, 1);
6692 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6693 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6695 ee85c5e8 2020-02-29 stsp if (hflag)
6696 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6698 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6702 b46c1e04 2020-09-20 naddy putchar('\n');
6703 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6705 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6706 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6707 a2f4a359 2020-02-28 stsp free(cmd_argv);
6710 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6711 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);