2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
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>
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 9f7d7167 2018-04-29 stsp #include "got_error.h"
45 80ddbec8 2018-04-29 stsp #include "got_object.h"
46 80ddbec8 2018-04-29 stsp #include "got_reference.h"
47 80ddbec8 2018-04-29 stsp #include "got_repository.h"
48 80ddbec8 2018-04-29 stsp #include "got_diff.h"
49 511a516b 2018-05-19 stsp #include "got_opentemp.h"
50 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
51 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
52 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
53 a70480e0 2018-06-23 stsp #include "got_blame.h"
54 c2db6724 2019-01-04 stsp #include "got_privsep.h"
55 1dd54920 2019-05-11 stsp #include "got_path.h"
56 b7165be3 2019-02-05 stsp #include "got_worktree.h"
59 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 9f7d7167 2018-04-29 stsp #ifndef nitems
69 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 9f7d7167 2018-04-29 stsp struct tog_cmd {
73 c2301be8 2018-04-30 stsp const char *name;
74 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
75 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
78 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
82 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
83 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
88 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
89 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
92 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
93 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
94 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
95 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
96 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
99 d6b05b5b 2018-08-04 stsp enum tog_view_type {
100 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
102 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
103 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
104 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
107 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
109 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
110 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
111 ba4f502b 2018-08-04 stsp struct got_object_id *id;
112 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
115 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 ba4f502b 2018-08-04 stsp struct commit_queue {
117 ba4f502b 2018-08-04 stsp int ncommits;
118 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
121 f26dddb7 2019-11-08 stsp struct tog_color {
122 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
123 6d17833f 2019-11-08 stsp regex_t regex;
124 6d17833f 2019-11-08 stsp short colorpair;
126 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
128 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
129 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
131 11b20872 2019-11-08 stsp static const struct got_error *
132 51a10b52 2020-12-26 stsp tog_load_refs(struct got_repository *repo)
134 51a10b52 2020-12-26 stsp const struct got_error *err;
136 51a10b52 2020-12-26 stsp err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
138 51a10b52 2020-12-26 stsp return err;
140 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
144 51a10b52 2020-12-26 stsp static void
145 51a10b52 2020-12-26 stsp tog_free_refs(void)
147 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
148 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
149 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
151 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
154 51a10b52 2020-12-26 stsp static const struct got_error *
155 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
156 11b20872 2019-11-08 stsp int idx, short color)
158 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
159 11b20872 2019-11-08 stsp struct tog_color *tc;
160 11b20872 2019-11-08 stsp int regerr = 0;
162 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
163 11b20872 2019-11-08 stsp return NULL;
165 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
167 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
168 11b20872 2019-11-08 stsp if (tc == NULL)
169 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
170 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
171 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
172 11b20872 2019-11-08 stsp if (regerr) {
173 11b20872 2019-11-08 stsp static char regerr_msg[512];
174 11b20872 2019-11-08 stsp static char err_msg[512];
175 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
176 11b20872 2019-11-08 stsp sizeof(regerr_msg));
177 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
178 11b20872 2019-11-08 stsp regerr_msg);
179 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
181 11b20872 2019-11-08 stsp return err;
183 11b20872 2019-11-08 stsp tc->colorpair = idx;
184 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
185 11b20872 2019-11-08 stsp return NULL;
188 11b20872 2019-11-08 stsp static void
189 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
191 11b20872 2019-11-08 stsp struct tog_color *tc;
193 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
194 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
195 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
196 11b20872 2019-11-08 stsp regfree(&tc->regex);
201 11b20872 2019-11-08 stsp struct tog_color *
202 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
204 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
206 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
207 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
211 11b20872 2019-11-08 stsp return NULL;
215 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
217 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
218 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
219 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
220 11b20872 2019-11-08 stsp return COLOR_CYAN;
221 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
222 11b20872 2019-11-08 stsp return COLOR_YELLOW;
223 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
224 11b20872 2019-11-08 stsp return COLOR_GREEN;
225 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
226 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
227 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
228 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
229 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
230 91b8c405 2020-01-25 stsp return COLOR_CYAN;
231 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
232 11b20872 2019-11-08 stsp return COLOR_GREEN;
233 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
234 11b20872 2019-11-08 stsp return COLOR_GREEN;
235 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
236 11b20872 2019-11-08 stsp return COLOR_CYAN;
237 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
238 11b20872 2019-11-08 stsp return COLOR_YELLOW;
239 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
240 6458efa5 2020-11-24 stsp return COLOR_GREEN;
241 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
242 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
243 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
244 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
250 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
252 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
254 11b20872 2019-11-08 stsp if (val == NULL)
255 11b20872 2019-11-08 stsp return default_color_value(envvar);
257 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
258 11b20872 2019-11-08 stsp return COLOR_BLACK;
259 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
260 11b20872 2019-11-08 stsp return COLOR_RED;
261 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
262 11b20872 2019-11-08 stsp return COLOR_GREEN;
263 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
264 11b20872 2019-11-08 stsp return COLOR_YELLOW;
265 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
266 11b20872 2019-11-08 stsp return COLOR_BLUE;
267 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
268 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
270 11b20872 2019-11-08 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
272 11b20872 2019-11-08 stsp return COLOR_WHITE;
273 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
276 11b20872 2019-11-08 stsp return default_color_value(envvar);
280 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
281 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
282 3dbaef42 2020-11-24 stsp const char *label1, *label2;
284 15a087fe 2019-02-21 stsp int first_displayed_line;
285 15a087fe 2019-02-21 stsp int last_displayed_line;
287 15a087fe 2019-02-21 stsp int diff_context;
288 3dbaef42 2020-11-24 stsp int ignore_whitespace;
289 64453f7e 2020-11-21 stsp int force_text_diff;
290 15a087fe 2019-02-21 stsp struct got_repository *repo;
291 bddb1296 2019-11-08 stsp struct tog_colors colors;
292 fe621944 2020-11-10 stsp size_t nlines;
293 f44b1f58 2020-02-02 tracey off_t *line_offsets;
294 f44b1f58 2020-02-02 tracey int matched_line;
295 f44b1f58 2020-02-02 tracey int selected_line;
297 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
298 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
301 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
303 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
304 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
305 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
306 1a76625f 2018-10-22 stsp int commits_needed;
307 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
308 1a76625f 2018-10-22 stsp struct commit_queue *commits;
309 1a76625f 2018-10-22 stsp const char *in_repo_path;
310 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
311 1a76625f 2018-10-22 stsp struct got_repository *repo;
312 1a76625f 2018-10-22 stsp int log_complete;
313 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
314 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
315 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
316 13add988 2019-10-15 stsp int *searching;
317 13add988 2019-10-15 stsp int *search_next_done;
318 13add988 2019-10-15 stsp regex_t *regex;
321 1a76625f 2018-10-22 stsp struct tog_log_view_state {
322 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
323 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
324 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
325 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
326 b01e7d3b 2018-08-04 stsp int selected;
327 b01e7d3b 2018-08-04 stsp char *in_repo_path;
328 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
329 b672a97a 2020-01-27 stsp int log_branches;
330 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
331 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
332 1a76625f 2018-10-22 stsp sig_atomic_t quit;
333 1a76625f 2018-10-22 stsp pthread_t thread;
334 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
335 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
336 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
337 11b20872 2019-11-08 stsp struct tog_colors colors;
340 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
341 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
342 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
343 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
344 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
345 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
346 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
347 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
348 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
349 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
350 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
351 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
352 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
353 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
355 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
356 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
357 e9424729 2018-08-04 stsp int nlines;
359 e9424729 2018-08-04 stsp struct tog_view *view;
360 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
364 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
365 e9424729 2018-08-04 stsp const char *path;
366 e9424729 2018-08-04 stsp struct got_repository *repo;
367 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
368 e9424729 2018-08-04 stsp int *complete;
369 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
370 fc06ba56 2019-08-22 stsp void *cancel_arg;
373 e9424729 2018-08-04 stsp struct tog_blame {
375 be659d10 2020-11-18 stsp off_t filesize;
376 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
377 6fcac457 2018-11-19 stsp int nlines;
378 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
379 e9424729 2018-08-04 stsp pthread_t thread;
380 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
381 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
382 e9424729 2018-08-04 stsp const char *path;
385 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
386 7cbe629d 2018-08-04 stsp int first_displayed_line;
387 7cbe629d 2018-08-04 stsp int last_displayed_line;
388 7cbe629d 2018-08-04 stsp int selected_line;
389 7cbe629d 2018-08-04 stsp int blame_complete;
392 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
393 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
394 e5a0f69f 2018-08-18 stsp char *path;
395 7cbe629d 2018-08-04 stsp struct got_repository *repo;
396 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
397 e9424729 2018-08-04 stsp struct tog_blame blame;
398 6c4c42e0 2019-06-24 stsp int matched_line;
399 11b20872 2019-11-08 stsp struct tog_colors colors;
402 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
403 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
404 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
405 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
406 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
407 ad80ab7b 2018-08-04 stsp int selected;
410 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
412 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
413 ad80ab7b 2018-08-04 stsp char *tree_label;
414 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
415 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
416 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
417 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
418 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
419 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
420 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
421 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
422 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
423 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
424 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
425 6458efa5 2020-11-24 stsp struct tog_colors colors;
428 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
429 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
430 6458efa5 2020-11-24 stsp struct got_reference *ref;
434 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
436 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
437 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
438 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
439 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
440 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
441 6458efa5 2020-11-24 stsp int nrefs, ndisplayed, selected, show_ids;
442 6458efa5 2020-11-24 stsp struct got_repository *repo;
443 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
444 bddb1296 2019-11-08 stsp struct tog_colors colors;
448 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
450 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
451 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
452 669b5ffa 2018-10-07 stsp * there is enough screen estate.
454 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
455 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
457 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
458 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
459 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
461 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
462 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
464 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
466 cc3c9aac 2018-08-01 stsp struct tog_view {
467 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
468 26ed57b2 2018-05-19 stsp WINDOW *window;
469 26ed57b2 2018-05-19 stsp PANEL *panel;
470 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
471 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
472 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
474 669b5ffa 2018-10-07 stsp struct tog_view *parent;
475 669b5ffa 2018-10-07 stsp struct tog_view *child;
478 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
479 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
480 e78dc838 2020-12-04 stsp * between parent and child.
481 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
482 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
483 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
484 e78dc838 2020-12-04 stsp * situations.
486 e78dc838 2020-12-04 stsp int focus_child;
488 5dc9f4bc 2018-08-04 stsp /* type-specific state */
489 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
491 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
492 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
493 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
494 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
495 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
498 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
499 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
500 e78dc838 2020-12-04 stsp struct tog_view *, int);
501 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
503 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
504 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
505 c0c4acc8 2021-01-24 stsp int search_started;
506 60493ae3 2019-06-20 stsp int searching;
507 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
508 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
509 60493ae3 2019-06-20 stsp int search_next_done;
510 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
511 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
512 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
513 1803e47f 2019-06-22 stsp regex_t regex;
514 41605754 2020-11-12 stsp regmatch_t regmatch;
517 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
518 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
519 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
520 78756c87 2020-11-24 stsp struct got_repository *);
521 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
522 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
523 e78dc838 2020-12-04 stsp struct tog_view *, int);
524 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
525 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
526 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
528 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
529 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
530 78756c87 2020-11-24 stsp const char *, const char *, int);
531 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
532 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
533 e78dc838 2020-12-04 stsp struct tog_view *, int);
534 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
535 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
536 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
538 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
539 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
540 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
541 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
542 e78dc838 2020-12-04 stsp struct tog_view *, int);
543 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
544 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
545 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
547 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
548 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
549 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
550 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
551 e78dc838 2020-12-04 stsp struct tog_view *, int);
552 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
553 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
554 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
556 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
557 6458efa5 2020-11-24 stsp struct got_repository *);
558 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
559 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
560 e78dc838 2020-12-04 stsp struct tog_view *, int);
561 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
562 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
563 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
565 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
566 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
567 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
569 25791caa 2018-10-24 stsp static void
570 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
572 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
575 83baff54 2019-08-12 stsp static void
576 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
578 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
581 61266923 2020-01-14 stsp static void
582 61266923 2020-01-14 stsp tog_sigcont(int signo)
584 61266923 2020-01-14 stsp tog_sigcont_received = 1;
587 e5a0f69f 2018-08-18 stsp static const struct got_error *
588 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
590 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
592 669b5ffa 2018-10-07 stsp if (view->child) {
593 669b5ffa 2018-10-07 stsp view_close(view->child);
594 669b5ffa 2018-10-07 stsp view->child = NULL;
596 e5a0f69f 2018-08-18 stsp if (view->close)
597 e5a0f69f 2018-08-18 stsp err = view->close(view);
598 ea5e7bb5 2018-08-01 stsp if (view->panel)
599 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
600 ea5e7bb5 2018-08-01 stsp if (view->window)
601 ea5e7bb5 2018-08-01 stsp delwin(view->window);
602 ea5e7bb5 2018-08-01 stsp free(view);
603 e5a0f69f 2018-08-18 stsp return err;
606 ea5e7bb5 2018-08-01 stsp static struct tog_view *
607 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
608 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
610 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
612 ea5e7bb5 2018-08-01 stsp if (view == NULL)
613 ea5e7bb5 2018-08-01 stsp return NULL;
615 d6b05b5b 2018-08-04 stsp view->type = type;
616 f7d12f7e 2018-08-01 stsp view->lines = LINES;
617 f7d12f7e 2018-08-01 stsp view->cols = COLS;
618 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
619 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
620 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
621 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
622 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
623 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
624 96a765a8 2018-08-04 stsp view_close(view);
625 ea5e7bb5 2018-08-01 stsp return NULL;
627 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
628 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
629 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
630 96a765a8 2018-08-04 stsp view_close(view);
631 ea5e7bb5 2018-08-01 stsp return NULL;
634 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
635 ea5e7bb5 2018-08-01 stsp return view;
639 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
641 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
643 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
646 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
648 5c60c32a 2018-10-18 stsp static const struct got_error *
649 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
651 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
653 5c60c32a 2018-10-18 stsp view->begin_y = 0;
654 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
655 5c60c32a 2018-10-18 stsp view->nlines = LINES;
656 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
657 5c60c32a 2018-10-18 stsp view->lines = LINES;
658 5c60c32a 2018-10-18 stsp view->cols = COLS;
659 5c60c32a 2018-10-18 stsp err = view_resize(view);
661 5c60c32a 2018-10-18 stsp return err;
663 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
664 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
666 5c60c32a 2018-10-18 stsp return NULL;
669 5c60c32a 2018-10-18 stsp static const struct got_error *
670 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
672 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
674 5c60c32a 2018-10-18 stsp view->begin_x = 0;
675 5c60c32a 2018-10-18 stsp view->begin_y = 0;
676 5c60c32a 2018-10-18 stsp view->nlines = LINES;
677 5c60c32a 2018-10-18 stsp view->ncols = COLS;
678 5c60c32a 2018-10-18 stsp view->lines = LINES;
679 5c60c32a 2018-10-18 stsp view->cols = COLS;
680 5c60c32a 2018-10-18 stsp err = view_resize(view);
682 5c60c32a 2018-10-18 stsp return err;
684 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
685 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
687 5c60c32a 2018-10-18 stsp return NULL;
691 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
693 5c60c32a 2018-10-18 stsp return view->parent == NULL;
696 4d8c2215 2018-08-19 stsp static const struct got_error *
697 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
699 f7d12f7e 2018-08-01 stsp int nlines, ncols;
701 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
702 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
704 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
706 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
707 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
709 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
711 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
712 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
713 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
714 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
715 25791caa 2018-10-24 stsp wclear(view->window);
717 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
718 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
719 0cf4efb1 2018-09-29 stsp view->lines = LINES;
720 0cf4efb1 2018-09-29 stsp view->cols = COLS;
722 6e3e5d9c 2018-10-18 stsp if (view->child) {
723 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
724 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
725 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
726 5c60c32a 2018-10-18 stsp if (view->child->focussed)
727 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
729 5c60c32a 2018-10-18 stsp show_panel(view->panel);
731 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
732 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
736 5c60c32a 2018-10-18 stsp return NULL;
739 669b5ffa 2018-10-07 stsp static const struct got_error *
740 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
742 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
744 669b5ffa 2018-10-07 stsp if (view->child == NULL)
745 669b5ffa 2018-10-07 stsp return NULL;
747 669b5ffa 2018-10-07 stsp err = view_close(view->child);
748 669b5ffa 2018-10-07 stsp view->child = NULL;
749 669b5ffa 2018-10-07 stsp return err;
752 72a9cb46 2020-12-03 stsp static void
753 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
755 669b5ffa 2018-10-07 stsp view->child = child;
756 669b5ffa 2018-10-07 stsp child->parent = view;
760 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
762 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
765 34bc9ec9 2019-02-22 stsp static void
766 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
768 25791caa 2018-10-24 stsp int cols, lines;
769 25791caa 2018-10-24 stsp struct winsize size;
771 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
772 25791caa 2018-10-24 stsp cols = 80; /* Default */
773 25791caa 2018-10-24 stsp lines = 24;
775 25791caa 2018-10-24 stsp cols = size.ws_col;
776 25791caa 2018-10-24 stsp lines = size.ws_row;
778 25791caa 2018-10-24 stsp resize_term(lines, cols);
781 2b49a8ae 2019-06-22 stsp static const struct got_error *
782 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
784 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
785 2b49a8ae 2019-06-22 stsp char pattern[1024];
788 c0c4acc8 2021-01-24 stsp if (view->search_started) {
789 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
790 c0c4acc8 2021-01-24 stsp view->searching = 0;
791 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
793 c0c4acc8 2021-01-24 stsp view->search_started = 0;
795 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
796 2b49a8ae 2019-06-22 stsp return NULL;
798 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
799 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
801 2b49a8ae 2019-06-22 stsp nocbreak();
803 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
806 2b49a8ae 2019-06-22 stsp if (ret == ERR)
807 2b49a8ae 2019-06-22 stsp return NULL;
809 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
810 7c32bd05 2019-06-22 stsp err = view->search_start(view);
812 7c32bd05 2019-06-22 stsp regfree(&view->regex);
813 7c32bd05 2019-06-22 stsp return err;
815 c0c4acc8 2021-01-24 stsp view->search_started = 1;
816 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
817 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
818 2b49a8ae 2019-06-22 stsp view->search_next(view);
821 2b49a8ae 2019-06-22 stsp return NULL;
824 0cf4efb1 2018-09-29 stsp static const struct got_error *
825 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
826 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
828 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
829 669b5ffa 2018-10-07 stsp struct tog_view *v;
830 1a76625f 2018-10-22 stsp int ch, errcode;
832 e5a0f69f 2018-08-18 stsp *new = NULL;
834 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
835 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
836 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
837 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
839 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
840 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
841 82954512 2020-02-03 stsp if (errcode)
842 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
843 82954512 2020-02-03 stsp "pthread_mutex_unlock");
844 82954512 2020-02-03 stsp pthread_yield();
845 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
846 82954512 2020-02-03 stsp if (errcode)
847 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
848 82954512 2020-02-03 stsp "pthread_mutex_lock");
849 60493ae3 2019-06-20 stsp view->search_next(view);
850 60493ae3 2019-06-20 stsp return NULL;
853 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
854 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
855 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
856 1a76625f 2018-10-22 stsp if (errcode)
857 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
858 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
859 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
860 1a76625f 2018-10-22 stsp if (errcode)
861 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
862 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
864 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
865 25791caa 2018-10-24 stsp tog_resizeterm();
866 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
867 61266923 2020-01-14 stsp tog_sigcont_received = 0;
868 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
869 25791caa 2018-10-24 stsp err = view_resize(v);
871 25791caa 2018-10-24 stsp return err;
872 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
874 25791caa 2018-10-24 stsp return err;
875 cdfcfb03 2020-12-06 stsp if (v->child) {
876 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
878 cdfcfb03 2020-12-06 stsp return err;
879 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
880 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
882 cdfcfb03 2020-12-06 stsp return err;
887 e5a0f69f 2018-08-18 stsp switch (ch) {
891 1e37a5c2 2019-05-12 jcs if (view->child) {
892 e78dc838 2020-12-04 stsp view->focussed = 0;
893 e78dc838 2020-12-04 stsp view->child->focussed = 1;
894 e78dc838 2020-12-04 stsp view->focus_child = 1;
895 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
896 e78dc838 2020-12-04 stsp view->focussed = 0;
897 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
898 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
902 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
903 9970f7fc 2020-12-03 stsp view->dying = 1;
909 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
910 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
912 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
913 e78dc838 2020-12-04 stsp view->focussed = 0;
914 e78dc838 2020-12-04 stsp view->child->focussed = 1;
915 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
917 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
920 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
921 9970f7fc 2020-12-03 stsp KEY_RESIZE);
923 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
924 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
925 e78dc838 2020-12-04 stsp view->focussed = 1;
926 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
928 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
932 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
935 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
938 60493ae3 2019-06-20 stsp if (view->search_start)
939 2b49a8ae 2019-06-22 stsp view_search_start(view);
941 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
945 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
946 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
947 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
948 60493ae3 2019-06-20 stsp view->search_next_done = 0;
949 60493ae3 2019-06-20 stsp view->search_next(view);
951 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
954 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
958 e5a0f69f 2018-08-18 stsp return err;
962 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
964 0cf4efb1 2018-09-29 stsp PANEL *panel;
965 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
967 669b5ffa 2018-10-07 stsp if (view->parent)
968 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
970 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
971 0cf4efb1 2018-09-29 stsp if (panel == NULL)
974 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
975 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
976 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
980 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
982 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
983 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
985 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
987 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
990 669b5ffa 2018-10-07 stsp return view->focussed;
993 bcbd79e2 2018-08-19 stsp static const struct got_error *
994 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
996 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
997 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
998 fb59748f 2020-12-05 stsp struct tog_view *new_view;
999 fd823528 2018-10-22 stsp int fast_refresh = 10;
1000 1a76625f 2018-10-22 stsp int done = 0, errcode;
1002 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1003 1a76625f 2018-10-22 stsp if (errcode)
1004 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1006 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1007 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1009 1004088d 2018-09-29 stsp view->focussed = 1;
1010 878940b7 2018-09-29 stsp err = view->show(view);
1012 0cf4efb1 2018-09-29 stsp return err;
1013 0cf4efb1 2018-09-29 stsp update_panels();
1014 0cf4efb1 2018-09-29 stsp doupdate();
1015 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1016 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1017 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1018 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1020 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1023 9970f7fc 2020-12-03 stsp if (view->dying) {
1024 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1026 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1027 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1029 e78dc838 2020-12-04 stsp else if (view->parent)
1030 669b5ffa 2018-10-07 stsp prev = view->parent;
1032 e78dc838 2020-12-04 stsp if (view->parent) {
1033 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1034 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1036 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1038 9970f7fc 2020-12-03 stsp err = view_close(view);
1040 e5a0f69f 2018-08-18 stsp goto done;
1042 e78dc838 2020-12-04 stsp view = NULL;
1043 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1044 e78dc838 2020-12-04 stsp if (v->focussed)
1047 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1048 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1050 e78dc838 2020-12-04 stsp view = prev;
1051 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1052 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1053 e78dc838 2020-12-04 stsp tog_view_list_head);
1055 e78dc838 2020-12-04 stsp if (view) {
1056 e78dc838 2020-12-04 stsp if (view->focus_child) {
1057 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1058 e78dc838 2020-12-04 stsp view = view->child;
1060 e78dc838 2020-12-04 stsp view->focussed = 1;
1064 bcbd79e2 2018-08-19 stsp if (new_view) {
1065 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1066 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1067 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1068 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1070 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1071 86c66b02 2018-10-18 stsp err = view_close(v);
1073 86c66b02 2018-10-18 stsp goto done;
1076 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1077 fed7eaa8 2018-10-24 stsp view = new_view;
1079 669b5ffa 2018-10-07 stsp if (view) {
1080 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1081 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1082 e78dc838 2020-12-04 stsp view = view->child;
1084 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1085 e78dc838 2020-12-04 stsp view = view->parent;
1087 e78dc838 2020-12-04 stsp show_panel(view->panel);
1088 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1089 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1090 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1091 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1093 1a76625f 2018-10-22 stsp goto done;
1095 669b5ffa 2018-10-07 stsp err = view->show(view);
1097 1a76625f 2018-10-22 stsp goto done;
1098 669b5ffa 2018-10-07 stsp if (view->child) {
1099 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1101 1a76625f 2018-10-22 stsp goto done;
1103 1a76625f 2018-10-22 stsp update_panels();
1104 1a76625f 2018-10-22 stsp doupdate();
1108 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1109 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1110 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1111 e5a0f69f 2018-08-18 stsp view_close(view);
1114 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1115 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1116 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1118 e5a0f69f 2018-08-18 stsp return err;
1121 4ed7e80c 2018-05-20 stsp __dead static void
1122 9f7d7167 2018-04-29 stsp usage_log(void)
1125 c70c5802 2018-08-01 stsp fprintf(stderr,
1126 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1127 9f7d7167 2018-04-29 stsp getprogname());
1131 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1132 80ddbec8 2018-04-29 stsp static const struct got_error *
1133 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1135 00dfcb92 2018-06-11 stsp char *vis = NULL;
1136 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1138 963b370f 2018-05-20 stsp *ws = NULL;
1139 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1140 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1141 00dfcb92 2018-06-11 stsp int vislen;
1142 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1143 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1145 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1146 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1148 00dfcb92 2018-06-11 stsp return err;
1149 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1150 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1151 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1152 a7f50699 2018-06-11 stsp goto done;
1156 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1157 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1158 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1159 a7f50699 2018-06-11 stsp goto done;
1162 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1163 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1165 00dfcb92 2018-06-11 stsp free(vis);
1166 963b370f 2018-05-20 stsp if (err) {
1167 963b370f 2018-05-20 stsp free(*ws);
1168 963b370f 2018-05-20 stsp *ws = NULL;
1169 963b370f 2018-05-20 stsp *wlen = 0;
1171 963b370f 2018-05-20 stsp return err;
1174 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1175 963b370f 2018-05-20 stsp static const struct got_error *
1176 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1177 27a741e5 2019-09-11 stsp int col_tab_align)
1179 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1180 963b370f 2018-05-20 stsp int cols = 0;
1181 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1182 963b370f 2018-05-20 stsp size_t wlen;
1185 963b370f 2018-05-20 stsp *wlinep = NULL;
1186 b700b5d6 2018-07-10 stsp *widthp = 0;
1188 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1190 963b370f 2018-05-20 stsp return err;
1192 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1193 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1196 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1197 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1202 27a741e5 2019-09-11 stsp while (i < wlen) {
1203 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1205 27a741e5 2019-09-11 stsp if (width == 0) {
1210 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1211 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1213 27a741e5 2019-09-11 stsp cols += width;
1215 27a741e5 2019-09-11 stsp } else if (width == -1) {
1216 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1217 27a741e5 2019-09-11 stsp width = TABSIZE -
1218 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1220 748d5cab 2020-12-14 naddy width = 1;
1221 748d5cab 2020-12-14 naddy wline[i] = L'.';
1223 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1225 748d5cab 2020-12-14 naddy cols += width;
1228 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1229 963b370f 2018-05-20 stsp goto done;
1232 963b370f 2018-05-20 stsp wline[i] = L'\0';
1233 b700b5d6 2018-07-10 stsp if (widthp)
1234 b700b5d6 2018-07-10 stsp *widthp = cols;
1237 963b370f 2018-05-20 stsp free(wline);
1239 963b370f 2018-05-20 stsp *wlinep = wline;
1240 963b370f 2018-05-20 stsp return err;
1243 8b473291 2019-02-21 stsp static const struct got_error*
1244 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1245 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1247 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1248 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1250 8b473291 2019-02-21 stsp const char *name;
1252 8b473291 2019-02-21 stsp *refs_str = NULL;
1254 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1255 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1256 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1259 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1260 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1262 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1263 8b473291 2019-02-21 stsp name += 5;
1264 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1266 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1267 8b473291 2019-02-21 stsp name += 6;
1268 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1269 8b473291 2019-02-21 stsp name += 8;
1270 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1271 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1274 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1277 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1278 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1279 5d844a1e 2019-08-13 stsp if (err) {
1280 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1281 48cae60d 2020-09-22 stsp free(ref_id);
1284 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1285 5d844a1e 2019-08-13 stsp err = NULL;
1286 5d844a1e 2019-08-13 stsp tag = NULL;
1289 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1290 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1291 48cae60d 2020-09-22 stsp free(ref_id);
1293 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1294 52b5abe1 2019-08-13 stsp if (cmp != 0)
1296 8b473291 2019-02-21 stsp s = *refs_str;
1297 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1298 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1299 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1301 8b473291 2019-02-21 stsp *refs_str = NULL;
1307 8b473291 2019-02-21 stsp return err;
1310 963b370f 2018-05-20 stsp static const struct got_error *
1311 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1312 27a741e5 2019-09-11 stsp int col_tab_align)
1314 e6b8b890 2020-12-29 naddy char *smallerthan;
1316 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1317 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1318 5813d178 2019-03-09 stsp author = smallerthan + 1;
1319 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1320 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1323 5813d178 2019-03-09 stsp static const struct got_error *
1324 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1325 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1326 8fdc79fe 2020-12-01 naddy int author_display_cols)
1328 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1329 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1330 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1331 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1332 5813d178 2019-03-09 stsp char *author = NULL;
1333 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1334 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1335 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1336 bb737323 2018-05-20 stsp int col, limit;
1337 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1338 ccb26ccd 2018-11-05 stsp struct tm tm;
1339 45d799e2 2018-12-23 stsp time_t committer_time;
1340 11b20872 2019-11-08 stsp struct tog_color *tc;
1342 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1343 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1344 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1345 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1346 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1348 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1349 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1351 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1352 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1354 11b20872 2019-11-08 stsp wattr_on(view->window,
1355 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1356 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1358 11b20872 2019-11-08 stsp wattr_off(view->window,
1359 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1360 27a741e5 2019-09-11 stsp col = limit;
1361 b39d25c7 2018-07-10 stsp if (col > avail)
1362 b39d25c7 2018-07-10 stsp goto done;
1364 6570a66d 2019-11-08 stsp if (avail >= 120) {
1365 6570a66d 2019-11-08 stsp char *id_str;
1366 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1368 6570a66d 2019-11-08 stsp goto done;
1369 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1371 11b20872 2019-11-08 stsp wattr_on(view->window,
1372 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1373 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1375 11b20872 2019-11-08 stsp wattr_off(view->window,
1376 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1377 6570a66d 2019-11-08 stsp free(id_str);
1379 6570a66d 2019-11-08 stsp if (col > avail)
1380 6570a66d 2019-11-08 stsp goto done;
1383 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1384 5813d178 2019-03-09 stsp if (author == NULL) {
1385 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1386 80ddbec8 2018-04-29 stsp goto done;
1388 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1390 bb737323 2018-05-20 stsp goto done;
1391 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1393 11b20872 2019-11-08 stsp wattr_on(view->window,
1394 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1395 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1397 11b20872 2019-11-08 stsp wattr_off(view->window,
1398 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1399 bb737323 2018-05-20 stsp col += author_width;
1400 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1401 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1403 bb737323 2018-05-20 stsp author_width++;
1405 9c2eaf34 2018-05-20 stsp if (col > avail)
1406 9c2eaf34 2018-05-20 stsp goto done;
1408 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1410 6d9fbc00 2018-04-29 stsp goto done;
1411 bb737323 2018-05-20 stsp logmsg = logmsg0;
1412 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1414 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1415 bb737323 2018-05-20 stsp if (newline)
1416 bb737323 2018-05-20 stsp *newline = '\0';
1417 bb737323 2018-05-20 stsp limit = avail - col;
1418 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1420 bb737323 2018-05-20 stsp goto done;
1421 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1422 bb737323 2018-05-20 stsp col += logmsg_width;
1423 27a741e5 2019-09-11 stsp while (col < avail) {
1424 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1428 80ddbec8 2018-04-29 stsp free(logmsg0);
1429 bb737323 2018-05-20 stsp free(wlogmsg);
1430 5813d178 2019-03-09 stsp free(author);
1431 bb737323 2018-05-20 stsp free(wauthor);
1432 80ddbec8 2018-04-29 stsp free(line);
1433 80ddbec8 2018-04-29 stsp return err;
1436 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1437 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1438 899d86c2 2018-05-10 stsp struct got_object_id *id)
1440 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1442 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1443 80ddbec8 2018-04-29 stsp if (entry == NULL)
1444 899d86c2 2018-05-10 stsp return NULL;
1446 899d86c2 2018-05-10 stsp entry->id = id;
1447 99db9666 2018-05-07 stsp entry->commit = commit;
1448 899d86c2 2018-05-10 stsp return entry;
1451 99db9666 2018-05-07 stsp static void
1452 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1454 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1456 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1457 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1458 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1459 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1460 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1461 99db9666 2018-05-07 stsp free(entry);
1464 99db9666 2018-05-07 stsp static void
1465 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1467 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1468 99db9666 2018-05-07 stsp pop_commit(commits);
1471 c4972b91 2018-05-07 stsp static const struct got_error *
1472 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1473 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1475 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1476 13add988 2019-10-15 stsp regmatch_t regmatch;
1477 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1479 13add988 2019-10-15 stsp *have_match = 0;
1481 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1483 13add988 2019-10-15 stsp return err;
1485 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1487 13add988 2019-10-15 stsp goto done;
1489 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1490 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1491 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1492 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1493 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1494 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1495 13add988 2019-10-15 stsp *have_match = 1;
1497 13add988 2019-10-15 stsp free(id_str);
1498 13add988 2019-10-15 stsp free(logmsg);
1499 13add988 2019-10-15 stsp return err;
1502 13add988 2019-10-15 stsp static const struct got_error *
1503 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1505 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1508 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1509 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1510 1a76625f 2018-10-22 stsp * while updating the display.
1513 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1514 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1515 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1516 1a76625f 2018-10-22 stsp int errcode;
1518 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1519 4e0d2870 2020-12-07 naddy NULL, NULL);
1520 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1523 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1526 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1527 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1528 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1532 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1533 1a76625f 2018-10-22 stsp if (errcode) {
1534 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1535 13add988 2019-10-15 stsp "pthread_mutex_lock");
1539 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1540 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1541 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1543 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1544 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1545 7c1452c1 2020-03-26 stsp int have_match;
1546 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1549 7c1452c1 2020-03-26 stsp if (have_match)
1550 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1553 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1554 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1555 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1556 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1559 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1561 9ba79e04 2018-06-11 stsp return err;
1564 2b779855 2020-12-05 naddy static void
1565 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1567 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1568 2b779855 2020-12-05 naddy int ncommits = 0;
1570 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1571 2b779855 2020-12-05 naddy while (entry) {
1572 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1573 2b779855 2020-12-05 naddy s->selected_entry = entry;
1576 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1577 2b779855 2020-12-05 naddy ncommits++;
1581 0553a4e3 2018-04-30 stsp static const struct got_error *
1582 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1584 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1585 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1586 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1587 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1588 60493ae3 2019-06-20 stsp int width;
1589 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1590 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1591 8b473291 2019-02-21 stsp char *refs_str = NULL;
1592 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1593 11b20872 2019-11-08 stsp struct tog_color *tc;
1594 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1596 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1597 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1598 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1599 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1601 ecb28ae0 2018-07-16 stsp return err;
1602 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1603 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1604 d2075bf3 2020-12-25 stsp if (refs) {
1605 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1606 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1608 d2075bf3 2020-12-25 stsp goto done;
1612 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1613 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1615 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed > 0) {
1616 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1617 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1618 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1619 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1620 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1621 8f4ed634 2020-03-26 stsp goto done;
1624 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1626 f9686aa5 2020-03-27 stsp if (view->searching) {
1627 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1628 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1629 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1630 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1631 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1632 f9686aa5 2020-03-27 stsp search_str = "searching...";
1635 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1636 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1637 f9686aa5 2020-03-27 stsp search_str ? search_str :
1638 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1639 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1640 8f4ed634 2020-03-26 stsp goto done;
1644 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1645 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1646 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1647 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1648 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1649 1a76625f 2018-10-22 stsp header = NULL;
1650 1a76625f 2018-10-22 stsp goto done;
1652 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1653 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1654 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1655 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1656 1a76625f 2018-10-22 stsp header = NULL;
1657 1a76625f 2018-10-22 stsp goto done;
1659 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1661 1a76625f 2018-10-22 stsp goto done;
1663 2814baeb 2018-08-01 stsp werase(view->window);
1665 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1666 a3404814 2018-09-02 stsp wstandout(view->window);
1667 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1669 11b20872 2019-11-08 stsp wattr_on(view->window,
1670 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1671 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1673 11b20872 2019-11-08 stsp wattr_off(view->window,
1674 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1675 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1676 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1679 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1680 a3404814 2018-09-02 stsp wstandend(view->window);
1681 ecb28ae0 2018-07-16 stsp free(wline);
1682 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1683 1a76625f 2018-10-22 stsp goto done;
1685 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1686 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1687 5813d178 2019-03-09 stsp ncommits = 0;
1688 5813d178 2019-03-09 stsp while (entry) {
1689 5813d178 2019-03-09 stsp char *author;
1690 5813d178 2019-03-09 stsp wchar_t *wauthor;
1691 5813d178 2019-03-09 stsp int width;
1692 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1694 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1695 5813d178 2019-03-09 stsp if (author == NULL) {
1696 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1697 5813d178 2019-03-09 stsp goto done;
1699 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1700 27a741e5 2019-09-11 stsp date_display_cols);
1701 5813d178 2019-03-09 stsp if (author_cols < width)
1702 5813d178 2019-03-09 stsp author_cols = width;
1703 5813d178 2019-03-09 stsp free(wauthor);
1704 5813d178 2019-03-09 stsp free(author);
1705 7ca04879 2019-10-19 stsp ncommits++;
1706 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1709 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1710 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1711 867c6645 2018-07-10 stsp ncommits = 0;
1712 899d86c2 2018-05-10 stsp while (entry) {
1713 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1715 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1716 2814baeb 2018-08-01 stsp wstandout(view->window);
1717 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1718 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1719 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1720 2814baeb 2018-08-01 stsp wstandend(view->window);
1722 60493ae3 2019-06-20 stsp goto done;
1723 0553a4e3 2018-04-30 stsp ncommits++;
1724 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1725 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1728 1a57306a 2018-09-02 stsp view_vborder(view);
1730 1a76625f 2018-10-22 stsp free(id_str);
1731 8b473291 2019-02-21 stsp free(refs_str);
1732 1a76625f 2018-10-22 stsp free(ncommits_str);
1733 1a76625f 2018-10-22 stsp free(header);
1734 80ddbec8 2018-04-29 stsp return err;
1737 07b55e75 2018-05-10 stsp static void
1738 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1740 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1741 07b55e75 2018-05-10 stsp int nscrolled = 0;
1743 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1744 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1747 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1748 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1749 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1750 07b55e75 2018-05-10 stsp if (entry) {
1751 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1752 07b55e75 2018-05-10 stsp nscrolled++;
1757 aa075928 2018-05-10 stsp static const struct got_error *
1758 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1760 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1761 5e224a3e 2019-02-22 stsp int errcode;
1763 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1765 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1766 ffe38506 2020-12-01 naddy if (ta->log_complete)
1769 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1770 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1771 7aafa0d1 2019-02-22 stsp if (errcode)
1772 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1773 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1776 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1777 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1779 7c1452c1 2020-03-26 stsp if (!wait)
1782 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1783 ffe38506 2020-12-01 naddy show_log_view(view);
1784 7c1452c1 2020-03-26 stsp update_panels();
1785 7c1452c1 2020-03-26 stsp doupdate();
1787 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1788 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1789 82954512 2020-02-03 stsp if (errcode)
1790 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1791 7c1452c1 2020-03-26 stsp "pthread_cond_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();
1799 5e224a3e 2019-02-22 stsp return NULL;
1802 5e224a3e 2019-02-22 stsp static const struct got_error *
1803 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1805 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1806 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1807 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1808 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1810 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1811 5e224a3e 2019-02-22 stsp return NULL;
1813 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1814 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1815 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1817 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1819 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1820 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1822 5e224a3e 2019-02-22 stsp return err;
1826 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1827 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1830 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1832 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1833 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1835 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1836 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1838 dd0a52c1 2018-05-20 stsp return err;
1841 cd0acaa7 2018-05-20 stsp static const struct got_error *
1842 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1843 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1844 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1846 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1847 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1848 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1850 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1851 15a94983 2018-12-23 stsp if (diff_view == NULL)
1852 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1854 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1855 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1856 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1857 e5a0f69f 2018-08-18 stsp if (err == NULL)
1858 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1859 cd0acaa7 2018-05-20 stsp return err;
1862 80ddbec8 2018-04-29 stsp static const struct got_error *
1863 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1864 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1866 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1868 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1869 941e9f74 2019-05-21 stsp if (parent == NULL)
1870 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1872 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1873 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1874 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1875 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1876 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1877 941e9f74 2019-05-21 stsp s->tree = subtree;
1878 941e9f74 2019-05-21 stsp s->selected = 0;
1879 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1880 941e9f74 2019-05-21 stsp return NULL;
1883 941e9f74 2019-05-21 stsp static const struct got_error *
1884 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1885 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1887 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1888 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1889 941e9f74 2019-05-21 stsp const char *p;
1890 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1892 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1894 941e9f74 2019-05-21 stsp while (*p) {
1895 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1896 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1897 56e0773d 2019-11-28 stsp char *te_name;
1899 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1902 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1903 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1904 941e9f74 2019-05-21 stsp if (slash == NULL)
1905 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1907 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1908 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1909 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1912 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1913 56e0773d 2019-11-28 stsp if (te == NULL) {
1914 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1915 56e0773d 2019-11-28 stsp free(te_name);
1918 56e0773d 2019-11-28 stsp free(te_name);
1919 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1921 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1922 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1924 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1925 941e9f74 2019-05-21 stsp if (slash)
1926 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1928 941e9f74 2019-05-21 stsp subpath = strdup(path);
1929 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1930 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1934 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1939 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1940 941e9f74 2019-05-21 stsp free(tree_id);
1944 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1945 941e9f74 2019-05-21 stsp if (err) {
1946 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1949 941e9f74 2019-05-21 stsp if (slash == NULL)
1951 941e9f74 2019-05-21 stsp free(subpath);
1952 941e9f74 2019-05-21 stsp subpath = NULL;
1953 941e9f74 2019-05-21 stsp p = slash;
1956 941e9f74 2019-05-21 stsp free(subpath);
1957 1a76625f 2018-10-22 stsp return err;
1960 61266923 2020-01-14 stsp static const struct got_error *
1961 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1962 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1963 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
1965 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1966 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1967 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1969 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1970 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1971 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1973 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1975 55cccc34 2020-02-20 stsp return err;
1976 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1978 55cccc34 2020-02-20 stsp *new_view = tree_view;
1980 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1981 55cccc34 2020-02-20 stsp return NULL;
1983 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1986 55cccc34 2020-02-20 stsp static const struct got_error *
1987 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1989 61266923 2020-01-14 stsp sigset_t sigset;
1990 61266923 2020-01-14 stsp int errcode;
1992 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1993 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1995 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1996 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1997 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1998 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1999 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2001 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2002 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2003 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2005 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2006 61266923 2020-01-14 stsp if (errcode)
2007 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2009 61266923 2020-01-14 stsp return NULL;
2012 1a76625f 2018-10-22 stsp static void *
2013 1a76625f 2018-10-22 stsp log_thread(void *arg)
2015 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2016 1a76625f 2018-10-22 stsp int errcode = 0;
2017 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2018 1a76625f 2018-10-22 stsp int done = 0;
2020 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2022 61266923 2020-01-14 stsp return (void *)err;
2024 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2025 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2026 1a76625f 2018-10-22 stsp if (err) {
2027 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2028 1a76625f 2018-10-22 stsp return (void *)err;
2029 1a76625f 2018-10-22 stsp err = NULL;
2031 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
2032 1a76625f 2018-10-22 stsp a->commits_needed--;
2034 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2035 3abe8080 2019-04-10 stsp if (errcode) {
2036 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2037 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2039 3abe8080 2019-04-10 stsp } else if (*a->quit)
2041 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2042 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2043 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2044 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2047 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2048 7c1452c1 2020-03-26 stsp if (errcode) {
2049 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2050 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2051 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2056 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2058 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2059 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2060 7c1452c1 2020-03-26 stsp &tog_mutex);
2061 7c1452c1 2020-03-26 stsp if (errcode)
2062 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2063 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2064 21355643 2020-12-06 stsp if (*a->quit)
2069 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2070 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2071 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2072 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2074 3abe8080 2019-04-10 stsp a->log_complete = 1;
2075 1a76625f 2018-10-22 stsp return (void *)err;
2078 1a76625f 2018-10-22 stsp static const struct got_error *
2079 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2081 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2082 1a76625f 2018-10-22 stsp int errcode;
2084 1a76625f 2018-10-22 stsp if (s->thread) {
2085 1a76625f 2018-10-22 stsp s->quit = 1;
2086 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2087 1a76625f 2018-10-22 stsp if (errcode)
2088 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2089 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2090 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2091 1a76625f 2018-10-22 stsp if (errcode)
2092 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2093 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2094 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2095 1a76625f 2018-10-22 stsp if (errcode)
2096 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2097 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
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_mutex_lock");
2101 1a76625f 2018-10-22 stsp s->thread = NULL;
2104 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2105 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2106 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2109 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2110 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2111 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2114 9343a5fb 2018-06-23 stsp return err;
2117 9343a5fb 2018-06-23 stsp static const struct got_error *
2118 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2120 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2121 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2122 276b94a1 2020-11-13 naddy int errcode;
2124 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2126 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2127 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2128 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2130 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2131 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2132 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2134 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2135 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2136 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2137 1a76625f 2018-10-22 stsp free(s->start_id);
2138 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2139 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2140 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2141 1a76625f 2018-10-22 stsp return err;
2144 1a76625f 2018-10-22 stsp static const struct got_error *
2145 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2147 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2149 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2150 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2151 60493ae3 2019-06-20 stsp return NULL;
2154 60493ae3 2019-06-20 stsp static const struct got_error *
2155 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2157 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2158 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2159 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2161 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2162 f9686aa5 2020-03-27 stsp show_log_view(view);
2163 f9686aa5 2020-03-27 stsp update_panels();
2164 f9686aa5 2020-03-27 stsp doupdate();
2166 96e2b566 2019-07-08 stsp if (s->search_entry) {
2167 13add988 2019-10-15 stsp int errcode, ch;
2168 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2169 13add988 2019-10-15 stsp if (errcode)
2170 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2171 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2172 13add988 2019-10-15 stsp ch = wgetch(view->window);
2173 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2174 13add988 2019-10-15 stsp if (errcode)
2175 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2176 13add988 2019-10-15 stsp "pthread_mutex_lock");
2177 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2178 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2179 678cbce5 2019-07-28 stsp return NULL;
2181 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2182 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2184 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2185 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2186 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2187 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2188 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2190 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2191 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2193 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2194 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2196 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2199 60493ae3 2019-06-20 stsp while (1) {
2200 13add988 2019-10-15 stsp int have_match = 0;
2202 60493ae3 2019-06-20 stsp if (entry == NULL) {
2203 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2204 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2205 f9967bca 2020-03-27 stsp view->search_next_done =
2206 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2207 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2208 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2209 f801134a 2019-06-25 stsp return NULL;
2212 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2213 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2214 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2216 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2217 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2220 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2221 13add988 2019-10-15 stsp &view->regex);
2224 13add988 2019-10-15 stsp if (have_match) {
2225 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2226 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2230 96e2b566 2019-07-08 stsp s->search_entry = entry;
2231 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2232 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2234 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2237 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2238 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2239 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2240 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2242 60493ae3 2019-06-20 stsp return err;
2245 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2246 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2248 60493ae3 2019-06-20 stsp return err;
2253 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2255 60493ae3 2019-06-20 stsp return NULL;
2258 60493ae3 2019-06-20 stsp static const struct got_error *
2259 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2260 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2261 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2263 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2264 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2265 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2266 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2267 1a76625f 2018-10-22 stsp int errcode;
2269 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2270 f135c941 2020-02-20 stsp free(s->in_repo_path);
2271 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2272 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2273 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2276 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2277 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2278 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2280 fb2756b9 2018-08-04 stsp s->repo = repo;
2281 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2282 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2283 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2284 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2285 9cd7cbd1 2020-12-07 stsp goto done;
2288 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2289 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2290 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2291 5036bf37 2018-09-24 stsp goto done;
2293 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2295 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2296 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2297 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2298 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2300 11b20872 2019-11-08 stsp goto done;
2301 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2302 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2303 11b20872 2019-11-08 stsp if (err) {
2304 11b20872 2019-11-08 stsp free_colors(&s->colors);
2305 11b20872 2019-11-08 stsp goto done;
2307 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2308 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2309 11b20872 2019-11-08 stsp if (err) {
2310 11b20872 2019-11-08 stsp free_colors(&s->colors);
2311 11b20872 2019-11-08 stsp goto done;
2315 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2316 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2317 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2318 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2319 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2321 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2323 1a76625f 2018-10-22 stsp goto done;
2324 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2325 b672a97a 2020-01-27 stsp !s->log_branches);
2327 1a76625f 2018-10-22 stsp goto done;
2328 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2329 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2331 c5b78334 2020-01-12 stsp goto done;
2333 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2334 1a76625f 2018-10-22 stsp if (errcode) {
2335 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2336 1a76625f 2018-10-22 stsp goto done;
2338 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2339 7c1452c1 2020-03-26 stsp if (errcode) {
2340 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2341 7c1452c1 2020-03-26 stsp goto done;
2344 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2345 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2346 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2347 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2348 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2349 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2350 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2351 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2352 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2353 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2354 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2355 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2356 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2359 1a76625f 2018-10-22 stsp close_log_view(view);
2360 ba4f502b 2018-08-04 stsp return err;
2363 e5a0f69f 2018-08-18 stsp static const struct got_error *
2364 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2366 f2f6d207 2020-11-24 stsp const struct got_error *err;
2367 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2369 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2370 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2371 2b380cc8 2018-10-24 stsp &s->thread_args);
2372 2b380cc8 2018-10-24 stsp if (errcode)
2373 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2374 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2375 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2377 f2f6d207 2020-11-24 stsp return err;
2381 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2384 e5a0f69f 2018-08-18 stsp static const struct got_error *
2385 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2387 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2388 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2389 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2390 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2391 669b5ffa 2018-10-07 stsp int begin_x = 0;
2393 e5a0f69f 2018-08-18 stsp switch (ch) {
2395 1e37a5c2 2019-05-12 jcs s->quit = 1;
2398 1e37a5c2 2019-05-12 jcs case KEY_UP:
2401 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2403 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2404 1e37a5c2 2019-05-12 jcs s->selected--;
2406 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2407 2b779855 2020-12-05 naddy select_commit(s);
2409 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2410 a4292ac5 2019-05-12 jcs case CTRL('b'):
2411 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2413 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2414 1e37a5c2 2019-05-12 jcs s->selected = 0;
2416 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2417 2b779855 2020-12-05 naddy select_commit(s);
2420 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2423 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2425 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2426 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2427 1e37a5c2 2019-05-12 jcs s->selected++;
2429 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2433 2b779855 2020-12-05 naddy select_commit(s);
2435 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2436 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2437 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2438 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2439 1e37a5c2 2019-05-12 jcs if (first == NULL)
2441 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2444 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2445 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2446 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2447 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2448 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2449 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2451 2b779855 2020-12-05 naddy select_commit(s);
2454 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2455 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2456 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2457 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2458 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2459 2b779855 2020-12-05 naddy select_commit(s);
2460 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2461 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2462 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2463 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2464 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2467 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2470 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2472 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2473 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2474 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2475 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2476 78756c87 2020-11-24 stsp view, s->repo);
2479 e78dc838 2020-12-04 stsp view->focussed = 0;
2480 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2481 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2482 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2484 1e37a5c2 2019-05-12 jcs return err;
2485 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2486 e78dc838 2020-12-04 stsp view->focus_child = 1;
2488 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2491 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2493 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2494 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2495 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2496 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2500 e78dc838 2020-12-04 stsp view->focussed = 0;
2501 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2502 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2503 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2505 1e37a5c2 2019-05-12 jcs return err;
2506 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2507 e78dc838 2020-12-04 stsp view->focus_child = 1;
2509 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2511 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2512 21355643 2020-12-06 stsp case CTRL('l'):
2514 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2515 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2517 21355643 2020-12-06 stsp err = stop_log_thread(s);
2519 74cfe85e 2020-10-20 stsp return err;
2520 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2521 21355643 2020-12-06 stsp char *parent_path;
2522 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2524 21355643 2020-12-06 stsp return err;
2525 21355643 2020-12-06 stsp free(s->in_repo_path);
2526 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2527 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2528 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2529 21355643 2020-12-06 stsp struct got_object_id *start_id;
2530 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2531 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2532 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2534 21355643 2020-12-06 stsp return err;
2535 21355643 2020-12-06 stsp free(s->start_id);
2536 21355643 2020-12-06 stsp s->start_id = start_id;
2537 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2538 21355643 2020-12-06 stsp } else /* 'B' */
2539 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2541 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2542 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2544 21355643 2020-12-06 stsp return err;
2545 51a10b52 2020-12-26 stsp tog_free_refs();
2546 51a10b52 2020-12-26 stsp err = tog_load_refs(s->repo);
2548 ca51c541 2020-12-07 stsp return err;
2549 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2550 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2552 d01904d4 2019-06-25 stsp return err;
2553 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2554 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2556 b672a97a 2020-01-27 stsp return err;
2557 21355643 2020-12-06 stsp free_commits(&s->commits);
2558 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2559 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2560 21355643 2020-12-06 stsp s->selected_entry = NULL;
2561 21355643 2020-12-06 stsp s->selected = 0;
2562 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2563 21355643 2020-12-06 stsp s->quit = 0;
2564 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2567 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2568 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2569 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2570 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2571 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2572 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2573 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2574 6458efa5 2020-11-24 stsp if (err) {
2575 6458efa5 2020-11-24 stsp view_close(ref_view);
2576 6458efa5 2020-11-24 stsp return err;
2578 e78dc838 2020-12-04 stsp view->focussed = 0;
2579 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2580 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2581 6458efa5 2020-11-24 stsp err = view_close_child(view);
2583 6458efa5 2020-11-24 stsp return err;
2584 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2585 e78dc838 2020-12-04 stsp view->focus_child = 1;
2587 6458efa5 2020-11-24 stsp *new_view = ref_view;
2593 80ddbec8 2018-04-29 stsp return err;
2596 4ed7e80c 2018-05-20 stsp static const struct got_error *
2597 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2599 c2db6724 2019-01-04 stsp const struct got_error *error;
2601 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2602 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2603 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2605 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2606 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2608 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2609 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2611 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2612 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2614 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2615 c2db6724 2019-01-04 stsp if (error != NULL)
2616 c2db6724 2019-01-04 stsp return error;
2618 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2619 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2621 c2db6724 2019-01-04 stsp return NULL;
2624 a915003a 2019-02-05 stsp static void
2625 a915003a 2019-02-05 stsp init_curses(void)
2627 a915003a 2019-02-05 stsp initscr();
2629 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2632 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2633 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2634 a915003a 2019-02-05 stsp curs_set(0);
2635 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2636 6d17833f 2019-11-08 stsp start_color();
2637 6d17833f 2019-11-08 stsp use_default_colors();
2639 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2640 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2641 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2644 c2db6724 2019-01-04 stsp static const struct got_error *
2645 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2646 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2648 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2650 f135c941 2020-02-20 stsp if (argc == 0) {
2651 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2652 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2653 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2654 f135c941 2020-02-20 stsp return NULL;
2657 f135c941 2020-02-20 stsp if (worktree) {
2658 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2661 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2663 f135c941 2020-02-20 stsp return err;
2664 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2665 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2666 bfd61697 2020-11-14 stsp p) == -1) {
2667 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2668 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2672 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2674 f135c941 2020-02-20 stsp return err;
2677 f135c941 2020-02-20 stsp static const struct got_error *
2678 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2680 80ddbec8 2018-04-29 stsp const struct got_error *error;
2681 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2682 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2683 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2684 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2685 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2686 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2687 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2688 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2689 04cc582a 2018-08-01 stsp struct tog_view *view;
2691 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2692 80ddbec8 2018-04-29 stsp switch (ch) {
2694 b672a97a 2020-01-27 stsp log_branches = 1;
2697 80ddbec8 2018-04-29 stsp start_commit = optarg;
2700 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2701 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2702 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2706 17020d27 2019-03-07 stsp usage_log();
2707 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2711 80ddbec8 2018-04-29 stsp argc -= optind;
2712 80ddbec8 2018-04-29 stsp argv += optind;
2714 f135c941 2020-02-20 stsp if (argc > 1)
2715 f135c941 2020-02-20 stsp usage_log();
2717 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2718 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2719 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2720 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2721 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2722 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2723 c156c7a4 2020-12-18 stsp goto done;
2724 a1fbf39a 2019-08-11 stsp if (worktree)
2725 6962eb72 2020-02-20 stsp repo_path =
2726 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2728 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2729 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2730 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2731 c156c7a4 2020-12-18 stsp goto done;
2735 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2736 80ddbec8 2018-04-29 stsp if (error != NULL)
2737 ecb28ae0 2018-07-16 stsp goto done;
2739 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2740 f135c941 2020-02-20 stsp repo, worktree);
2741 f135c941 2020-02-20 stsp if (error)
2742 f135c941 2020-02-20 stsp goto done;
2744 f135c941 2020-02-20 stsp init_curses();
2746 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2747 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2748 c02c541e 2019-03-29 stsp if (error)
2749 c02c541e 2019-03-29 stsp goto done;
2751 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2752 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2753 87670572 2020-12-26 naddy error = tog_load_refs(repo);
2754 87670572 2020-12-26 naddy if (error)
2755 87670572 2020-12-26 naddy goto done;
2758 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2759 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2760 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2761 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2762 d8f38dc4 2020-12-05 stsp if (error)
2763 d8f38dc4 2020-12-05 stsp goto done;
2764 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2766 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2767 d8f38dc4 2020-12-05 stsp if (error == NULL)
2768 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2769 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2770 d8f38dc4 2020-12-05 stsp goto done;
2771 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2772 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2773 d8f38dc4 2020-12-05 stsp if (error)
2774 d8f38dc4 2020-12-05 stsp goto done;
2777 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2778 04cc582a 2018-08-01 stsp if (view == NULL) {
2779 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2780 04cc582a 2018-08-01 stsp goto done;
2782 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2783 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2784 ba4f502b 2018-08-04 stsp if (error)
2785 ba4f502b 2018-08-04 stsp goto done;
2786 2fc00ff4 2019-08-31 stsp if (worktree) {
2787 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2788 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2789 2fc00ff4 2019-08-31 stsp worktree = NULL;
2791 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2793 f135c941 2020-02-20 stsp free(in_repo_path);
2794 ecb28ae0 2018-07-16 stsp free(repo_path);
2795 ecb28ae0 2018-07-16 stsp free(cwd);
2796 899d86c2 2018-05-10 stsp free(start_id);
2797 d8f38dc4 2020-12-05 stsp free(label);
2799 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2800 1d0f4054 2021-06-17 stsp if (repo) {
2801 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2802 1d0f4054 2021-06-17 stsp if (error == NULL)
2803 1d0f4054 2021-06-17 stsp error = close_err;
2805 ec142235 2019-03-07 stsp if (worktree)
2806 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2807 51a10b52 2020-12-26 stsp tog_free_refs();
2808 80ddbec8 2018-04-29 stsp return error;
2811 4ed7e80c 2018-05-20 stsp __dead static void
2812 9f7d7167 2018-04-29 stsp usage_diff(void)
2815 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2816 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2820 6d17833f 2019-11-08 stsp static int
2821 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2822 41605754 2020-11-12 stsp regmatch_t *regmatch)
2824 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2827 f26dddb7 2019-11-08 stsp struct tog_color *
2828 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2830 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2832 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2833 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2834 6d17833f 2019-11-08 stsp return tc;
2837 6d17833f 2019-11-08 stsp return NULL;
2840 4ed7e80c 2018-05-20 stsp static const struct got_error *
2841 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2842 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2844 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2845 41605754 2020-11-12 stsp wchar_t *wline;
2846 41605754 2020-11-12 stsp int width;
2849 41605754 2020-11-12 stsp *wtotal = 0;
2851 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2852 41605754 2020-11-12 stsp if (s == NULL)
2853 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2855 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2856 41605754 2020-11-12 stsp if (err) {
2858 41605754 2020-11-12 stsp return err;
2860 41605754 2020-11-12 stsp waddwstr(window, wline);
2861 41605754 2020-11-12 stsp free(wline);
2863 41605754 2020-11-12 stsp wlimit -= width;
2864 41605754 2020-11-12 stsp *wtotal += width;
2866 41605754 2020-11-12 stsp if (wlimit > 0) {
2867 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2868 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2869 41605754 2020-11-12 stsp if (s == NULL) {
2870 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2872 41605754 2020-11-12 stsp return err;
2874 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2875 41605754 2020-11-12 stsp if (err) {
2877 41605754 2020-11-12 stsp return err;
2879 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2880 41605754 2020-11-12 stsp waddwstr(window, wline);
2881 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2882 41605754 2020-11-12 stsp free(wline);
2884 41605754 2020-11-12 stsp wlimit -= width;
2885 41605754 2020-11-12 stsp *wtotal += width;
2888 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2889 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2890 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2892 41605754 2020-11-12 stsp return err;
2893 41605754 2020-11-12 stsp waddwstr(window, wline);
2894 41605754 2020-11-12 stsp free(wline);
2895 41605754 2020-11-12 stsp *wtotal += width;
2898 41605754 2020-11-12 stsp return NULL;
2901 41605754 2020-11-12 stsp static const struct got_error *
2902 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2904 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2905 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2906 61e69b96 2018-05-20 stsp const struct got_error *err;
2907 fe621944 2020-11-10 stsp int nprinted = 0;
2908 b304db33 2018-05-20 stsp char *line;
2909 826082fe 2020-12-10 stsp size_t linesize = 0;
2910 826082fe 2020-12-10 stsp ssize_t linelen;
2911 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2912 61e69b96 2018-05-20 stsp wchar_t *wline;
2913 e0b650dd 2018-05-20 stsp int width;
2914 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2915 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2916 fe621944 2020-11-10 stsp off_t line_offset;
2918 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2919 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2920 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2922 f7d12f7e 2018-08-01 stsp werase(view->window);
2924 a3404814 2018-09-02 stsp if (header) {
2925 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2926 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2927 135a2da0 2020-11-11 stsp header) == -1)
2928 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2929 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2930 135a2da0 2020-11-11 stsp free(line);
2932 a3404814 2018-09-02 stsp return err;
2934 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2935 a3404814 2018-09-02 stsp wstandout(view->window);
2936 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2937 e54cc94a 2020-11-11 stsp free(wline);
2938 e54cc94a 2020-11-11 stsp wline = NULL;
2939 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2940 a3404814 2018-09-02 stsp wstandend(view->window);
2941 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2942 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2944 a3404814 2018-09-02 stsp if (max_lines <= 1)
2945 a3404814 2018-09-02 stsp return NULL;
2946 a3404814 2018-09-02 stsp max_lines--;
2949 89f1a395 2020-12-01 naddy s->eof = 0;
2950 826082fe 2020-12-10 stsp line = NULL;
2951 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2952 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
2953 826082fe 2020-12-10 stsp if (linelen == -1) {
2954 826082fe 2020-12-10 stsp if (feof(s->f)) {
2955 826082fe 2020-12-10 stsp s->eof = 1;
2958 826082fe 2020-12-10 stsp free(line);
2959 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
2962 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
2964 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2965 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2966 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
2967 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2968 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2969 41605754 2020-11-12 stsp view->window, regmatch);
2970 41605754 2020-11-12 stsp if (err) {
2971 41605754 2020-11-12 stsp free(line);
2972 41605754 2020-11-12 stsp return err;
2975 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2976 41605754 2020-11-12 stsp if (err) {
2977 41605754 2020-11-12 stsp free(line);
2978 41605754 2020-11-12 stsp return err;
2980 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2981 41605754 2020-11-12 stsp free(wline);
2982 41605754 2020-11-12 stsp wline = NULL;
2985 6d17833f 2019-11-08 stsp wattr_off(view->window,
2986 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2987 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2988 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2989 fe621944 2020-11-10 stsp nprinted++;
2991 826082fe 2020-12-10 stsp free(line);
2992 fe621944 2020-11-10 stsp if (nprinted >= 1)
2993 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
2994 89f1a395 2020-12-01 naddy (nprinted - 1);
2996 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
2998 1a57306a 2018-09-02 stsp view_vborder(view);
3000 89f1a395 2020-12-01 naddy if (s->eof) {
3001 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3002 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3003 c3e9aa98 2019-05-13 jcs nprinted++;
3006 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3008 c3e9aa98 2019-05-13 jcs return err;
3011 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3012 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3013 e54cc94a 2020-11-11 stsp free(wline);
3014 e54cc94a 2020-11-11 stsp wline = NULL;
3015 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3018 26ed57b2 2018-05-19 stsp return NULL;
3021 abd2672a 2018-12-23 stsp static char *
3022 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3024 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3025 09867e48 2019-08-13 stsp char *p, *s;
3027 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3028 09867e48 2019-08-13 stsp if (tm == NULL)
3029 09867e48 2019-08-13 stsp return NULL;
3030 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3031 09867e48 2019-08-13 stsp if (s == NULL)
3032 09867e48 2019-08-13 stsp return NULL;
3033 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3035 abd2672a 2018-12-23 stsp *p = '\0';
3039 4ed7e80c 2018-05-20 stsp static const struct got_error *
3040 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3041 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3043 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3044 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3045 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3046 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3048 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3049 0208f208 2020-05-05 stsp if (qid != NULL) {
3050 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3051 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3054 0208f208 2020-05-05 stsp return err;
3056 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3057 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3058 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3059 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3060 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3062 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3066 0208f208 2020-05-05 stsp if (tree_id1) {
3067 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3069 0208f208 2020-05-05 stsp goto done;
3072 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3073 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3075 0208f208 2020-05-05 stsp goto done;
3077 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3078 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3080 0208f208 2020-05-05 stsp if (tree1)
3081 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3082 0208f208 2020-05-05 stsp if (tree2)
3083 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3084 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3085 0208f208 2020-05-05 stsp return err;
3088 0208f208 2020-05-05 stsp static const struct got_error *
3089 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3093 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3094 fe621944 2020-11-10 stsp if (p == NULL)
3095 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");