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 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
123 6d17833f 2019-11-08 stsp regex_t regex;
124 6d17833f 2019-11-08 stsp short colorpair;
126 f26dddb7 2019-11-08 stsp SIMPLEQ_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 11b20872 2019-11-08 stsp SIMPLEQ_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 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
194 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
195 11b20872 2019-11-08 stsp SIMPLEQ_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 11b20872 2019-11-08 stsp SIMPLEQ_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 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
415 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
416 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
417 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
418 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
419 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
420 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
421 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
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 4e97c21c 2020-12-06 stsp struct got_tree_object *, struct got_object_id *, const char *,
549 4e97c21c 2020-12-06 stsp struct got_repository *);
550 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
551 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
552 e78dc838 2020-12-04 stsp struct tog_view *, int);
553 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
554 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
555 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
557 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
558 6458efa5 2020-11-24 stsp struct got_repository *);
559 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
560 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
561 e78dc838 2020-12-04 stsp struct tog_view *, int);
562 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
563 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
564 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
566 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
567 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
568 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
570 25791caa 2018-10-24 stsp static void
571 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
573 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
576 83baff54 2019-08-12 stsp static void
577 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
579 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
582 61266923 2020-01-14 stsp static void
583 61266923 2020-01-14 stsp tog_sigcont(int signo)
585 61266923 2020-01-14 stsp tog_sigcont_received = 1;
588 e5a0f69f 2018-08-18 stsp static const struct got_error *
589 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
591 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
593 669b5ffa 2018-10-07 stsp if (view->child) {
594 669b5ffa 2018-10-07 stsp view_close(view->child);
595 669b5ffa 2018-10-07 stsp view->child = NULL;
597 e5a0f69f 2018-08-18 stsp if (view->close)
598 e5a0f69f 2018-08-18 stsp err = view->close(view);
599 ea5e7bb5 2018-08-01 stsp if (view->panel)
600 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
601 ea5e7bb5 2018-08-01 stsp if (view->window)
602 ea5e7bb5 2018-08-01 stsp delwin(view->window);
603 ea5e7bb5 2018-08-01 stsp free(view);
604 e5a0f69f 2018-08-18 stsp return err;
607 ea5e7bb5 2018-08-01 stsp static struct tog_view *
608 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
609 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
611 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
613 ea5e7bb5 2018-08-01 stsp if (view == NULL)
614 ea5e7bb5 2018-08-01 stsp return NULL;
616 d6b05b5b 2018-08-04 stsp view->type = type;
617 f7d12f7e 2018-08-01 stsp view->lines = LINES;
618 f7d12f7e 2018-08-01 stsp view->cols = COLS;
619 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
620 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
621 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
622 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
623 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
624 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
625 96a765a8 2018-08-04 stsp view_close(view);
626 ea5e7bb5 2018-08-01 stsp return NULL;
628 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
629 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
630 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
631 96a765a8 2018-08-04 stsp view_close(view);
632 ea5e7bb5 2018-08-01 stsp return NULL;
635 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
636 ea5e7bb5 2018-08-01 stsp return view;
640 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
642 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
644 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
647 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
649 5c60c32a 2018-10-18 stsp static const struct got_error *
650 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
652 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
654 5c60c32a 2018-10-18 stsp view->begin_y = 0;
655 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
656 5c60c32a 2018-10-18 stsp view->nlines = LINES;
657 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
658 5c60c32a 2018-10-18 stsp view->lines = LINES;
659 5c60c32a 2018-10-18 stsp view->cols = COLS;
660 5c60c32a 2018-10-18 stsp err = view_resize(view);
662 5c60c32a 2018-10-18 stsp return err;
664 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
665 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
667 5c60c32a 2018-10-18 stsp return NULL;
670 5c60c32a 2018-10-18 stsp static const struct got_error *
671 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
673 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
675 5c60c32a 2018-10-18 stsp view->begin_x = 0;
676 5c60c32a 2018-10-18 stsp view->begin_y = 0;
677 5c60c32a 2018-10-18 stsp view->nlines = LINES;
678 5c60c32a 2018-10-18 stsp view->ncols = COLS;
679 5c60c32a 2018-10-18 stsp view->lines = LINES;
680 5c60c32a 2018-10-18 stsp view->cols = COLS;
681 5c60c32a 2018-10-18 stsp err = view_resize(view);
683 5c60c32a 2018-10-18 stsp return err;
685 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
686 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
688 5c60c32a 2018-10-18 stsp return NULL;
692 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
694 5c60c32a 2018-10-18 stsp return view->parent == NULL;
697 4d8c2215 2018-08-19 stsp static const struct got_error *
698 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
700 f7d12f7e 2018-08-01 stsp int nlines, ncols;
702 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
703 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
705 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
707 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
708 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
710 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
712 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
713 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
714 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
715 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
716 25791caa 2018-10-24 stsp wclear(view->window);
718 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
719 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
720 0cf4efb1 2018-09-29 stsp view->lines = LINES;
721 0cf4efb1 2018-09-29 stsp view->cols = COLS;
723 6e3e5d9c 2018-10-18 stsp if (view->child) {
724 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
725 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
726 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
727 5c60c32a 2018-10-18 stsp if (view->child->focussed)
728 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
730 5c60c32a 2018-10-18 stsp show_panel(view->panel);
732 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
733 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
737 5c60c32a 2018-10-18 stsp return NULL;
740 669b5ffa 2018-10-07 stsp static const struct got_error *
741 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
743 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
745 669b5ffa 2018-10-07 stsp if (view->child == NULL)
746 669b5ffa 2018-10-07 stsp return NULL;
748 669b5ffa 2018-10-07 stsp err = view_close(view->child);
749 669b5ffa 2018-10-07 stsp view->child = NULL;
750 669b5ffa 2018-10-07 stsp return err;
753 72a9cb46 2020-12-03 stsp static void
754 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
756 669b5ffa 2018-10-07 stsp view->child = child;
757 669b5ffa 2018-10-07 stsp child->parent = view;
761 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
763 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
766 34bc9ec9 2019-02-22 stsp static void
767 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
769 25791caa 2018-10-24 stsp int cols, lines;
770 25791caa 2018-10-24 stsp struct winsize size;
772 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
773 25791caa 2018-10-24 stsp cols = 80; /* Default */
774 25791caa 2018-10-24 stsp lines = 24;
776 25791caa 2018-10-24 stsp cols = size.ws_col;
777 25791caa 2018-10-24 stsp lines = size.ws_row;
779 25791caa 2018-10-24 stsp resize_term(lines, cols);
782 2b49a8ae 2019-06-22 stsp static const struct got_error *
783 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
785 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
786 2b49a8ae 2019-06-22 stsp char pattern[1024];
789 c0c4acc8 2021-01-24 stsp if (view->search_started) {
790 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
791 c0c4acc8 2021-01-24 stsp view->searching = 0;
792 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
794 c0c4acc8 2021-01-24 stsp view->search_started = 0;
796 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
797 2b49a8ae 2019-06-22 stsp return NULL;
799 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
800 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
802 2b49a8ae 2019-06-22 stsp nocbreak();
804 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
807 2b49a8ae 2019-06-22 stsp if (ret == ERR)
808 2b49a8ae 2019-06-22 stsp return NULL;
810 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
811 7c32bd05 2019-06-22 stsp err = view->search_start(view);
813 7c32bd05 2019-06-22 stsp regfree(&view->regex);
814 7c32bd05 2019-06-22 stsp return err;
816 c0c4acc8 2021-01-24 stsp view->search_started = 1;
817 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
818 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
819 2b49a8ae 2019-06-22 stsp view->search_next(view);
822 2b49a8ae 2019-06-22 stsp return NULL;
825 0cf4efb1 2018-09-29 stsp static const struct got_error *
826 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
827 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
829 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
830 669b5ffa 2018-10-07 stsp struct tog_view *v;
831 1a76625f 2018-10-22 stsp int ch, errcode;
833 e5a0f69f 2018-08-18 stsp *new = NULL;
835 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
836 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
837 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
838 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
840 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
841 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
842 82954512 2020-02-03 stsp if (errcode)
843 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
844 82954512 2020-02-03 stsp "pthread_mutex_unlock");
845 82954512 2020-02-03 stsp pthread_yield();
846 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
847 82954512 2020-02-03 stsp if (errcode)
848 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
849 82954512 2020-02-03 stsp "pthread_mutex_lock");
850 60493ae3 2019-06-20 stsp view->search_next(view);
851 60493ae3 2019-06-20 stsp return NULL;
854 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
855 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
856 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
857 1a76625f 2018-10-22 stsp if (errcode)
858 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
860 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
861 1a76625f 2018-10-22 stsp if (errcode)
862 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
863 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
865 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
866 25791caa 2018-10-24 stsp tog_resizeterm();
867 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
868 61266923 2020-01-14 stsp tog_sigcont_received = 0;
869 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
870 25791caa 2018-10-24 stsp err = view_resize(v);
872 25791caa 2018-10-24 stsp return err;
873 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
875 25791caa 2018-10-24 stsp return err;
876 cdfcfb03 2020-12-06 stsp if (v->child) {
877 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
879 cdfcfb03 2020-12-06 stsp return err;
880 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
881 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
883 cdfcfb03 2020-12-06 stsp return err;
888 e5a0f69f 2018-08-18 stsp switch (ch) {
892 1e37a5c2 2019-05-12 jcs if (view->child) {
893 e78dc838 2020-12-04 stsp view->focussed = 0;
894 e78dc838 2020-12-04 stsp view->child->focussed = 1;
895 e78dc838 2020-12-04 stsp view->focus_child = 1;
896 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
897 e78dc838 2020-12-04 stsp view->focussed = 0;
898 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
899 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
903 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
904 9970f7fc 2020-12-03 stsp view->dying = 1;
910 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
911 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
913 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
914 e78dc838 2020-12-04 stsp view->focussed = 0;
915 e78dc838 2020-12-04 stsp view->child->focussed = 1;
916 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
918 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
921 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
922 9970f7fc 2020-12-03 stsp KEY_RESIZE);
924 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
925 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
926 e78dc838 2020-12-04 stsp view->focussed = 1;
927 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
929 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
933 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
936 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
939 60493ae3 2019-06-20 stsp if (view->search_start)
940 2b49a8ae 2019-06-22 stsp view_search_start(view);
942 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
946 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
947 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
948 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
949 60493ae3 2019-06-20 stsp view->search_next_done = 0;
950 60493ae3 2019-06-20 stsp view->search_next(view);
952 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
955 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
959 e5a0f69f 2018-08-18 stsp return err;
963 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
965 0cf4efb1 2018-09-29 stsp PANEL *panel;
966 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
968 669b5ffa 2018-10-07 stsp if (view->parent)
969 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
971 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
972 0cf4efb1 2018-09-29 stsp if (panel == NULL)
975 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
976 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
977 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
981 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
983 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
984 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
986 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
988 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
991 669b5ffa 2018-10-07 stsp return view->focussed;
994 bcbd79e2 2018-08-19 stsp static const struct got_error *
995 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
997 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
998 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
999 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1000 fd823528 2018-10-22 stsp int fast_refresh = 10;
1001 1a76625f 2018-10-22 stsp int done = 0, errcode;
1003 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1004 1a76625f 2018-10-22 stsp if (errcode)
1005 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1007 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1008 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1010 1004088d 2018-09-29 stsp view->focussed = 1;
1011 878940b7 2018-09-29 stsp err = view->show(view);
1013 0cf4efb1 2018-09-29 stsp return err;
1014 0cf4efb1 2018-09-29 stsp update_panels();
1015 0cf4efb1 2018-09-29 stsp doupdate();
1016 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1017 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1018 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1019 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1021 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1024 9970f7fc 2020-12-03 stsp if (view->dying) {
1025 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1027 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1028 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1030 e78dc838 2020-12-04 stsp else if (view->parent)
1031 669b5ffa 2018-10-07 stsp prev = view->parent;
1033 e78dc838 2020-12-04 stsp if (view->parent) {
1034 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1035 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1037 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1039 9970f7fc 2020-12-03 stsp err = view_close(view);
1041 e5a0f69f 2018-08-18 stsp goto done;
1043 e78dc838 2020-12-04 stsp view = NULL;
1044 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1045 e78dc838 2020-12-04 stsp if (v->focussed)
1048 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1049 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1051 e78dc838 2020-12-04 stsp view = prev;
1052 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1053 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1054 e78dc838 2020-12-04 stsp tog_view_list_head);
1056 e78dc838 2020-12-04 stsp if (view) {
1057 e78dc838 2020-12-04 stsp if (view->focus_child) {
1058 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1059 e78dc838 2020-12-04 stsp view = view->child;
1061 e78dc838 2020-12-04 stsp view->focussed = 1;
1065 bcbd79e2 2018-08-19 stsp if (new_view) {
1066 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1067 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1068 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1069 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1071 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1072 86c66b02 2018-10-18 stsp err = view_close(v);
1074 86c66b02 2018-10-18 stsp goto done;
1077 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1078 fed7eaa8 2018-10-24 stsp view = new_view;
1080 669b5ffa 2018-10-07 stsp if (view) {
1081 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1082 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1083 e78dc838 2020-12-04 stsp view = view->child;
1085 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1086 e78dc838 2020-12-04 stsp view = view->parent;
1088 e78dc838 2020-12-04 stsp show_panel(view->panel);
1089 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1090 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1091 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1092 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1094 1a76625f 2018-10-22 stsp goto done;
1096 669b5ffa 2018-10-07 stsp err = view->show(view);
1098 1a76625f 2018-10-22 stsp goto done;
1099 669b5ffa 2018-10-07 stsp if (view->child) {
1100 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1102 1a76625f 2018-10-22 stsp goto done;
1104 1a76625f 2018-10-22 stsp update_panels();
1105 1a76625f 2018-10-22 stsp doupdate();
1109 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1110 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1111 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1112 e5a0f69f 2018-08-18 stsp view_close(view);
1115 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1116 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1117 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1119 e5a0f69f 2018-08-18 stsp return err;
1122 4ed7e80c 2018-05-20 stsp __dead static void
1123 9f7d7167 2018-04-29 stsp usage_log(void)
1126 c70c5802 2018-08-01 stsp fprintf(stderr,
1127 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1128 9f7d7167 2018-04-29 stsp getprogname());
1132 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1133 80ddbec8 2018-04-29 stsp static const struct got_error *
1134 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1136 00dfcb92 2018-06-11 stsp char *vis = NULL;
1137 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1139 963b370f 2018-05-20 stsp *ws = NULL;
1140 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1141 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1142 00dfcb92 2018-06-11 stsp int vislen;
1143 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1144 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1146 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1147 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1149 00dfcb92 2018-06-11 stsp return err;
1150 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1151 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1152 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1153 a7f50699 2018-06-11 stsp goto done;
1157 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1158 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1159 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1160 a7f50699 2018-06-11 stsp goto done;
1163 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1164 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1166 00dfcb92 2018-06-11 stsp free(vis);
1167 963b370f 2018-05-20 stsp if (err) {
1168 963b370f 2018-05-20 stsp free(*ws);
1169 963b370f 2018-05-20 stsp *ws = NULL;
1170 963b370f 2018-05-20 stsp *wlen = 0;
1172 963b370f 2018-05-20 stsp return err;
1175 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1176 963b370f 2018-05-20 stsp static const struct got_error *
1177 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1178 27a741e5 2019-09-11 stsp int col_tab_align)
1180 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1181 963b370f 2018-05-20 stsp int cols = 0;
1182 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1183 963b370f 2018-05-20 stsp size_t wlen;
1186 963b370f 2018-05-20 stsp *wlinep = NULL;
1187 b700b5d6 2018-07-10 stsp *widthp = 0;
1189 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1191 963b370f 2018-05-20 stsp return err;
1193 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1194 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1197 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1198 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1203 27a741e5 2019-09-11 stsp while (i < wlen) {
1204 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1206 27a741e5 2019-09-11 stsp if (width == 0) {
1211 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1212 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1214 27a741e5 2019-09-11 stsp cols += width;
1216 27a741e5 2019-09-11 stsp } else if (width == -1) {
1217 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1218 27a741e5 2019-09-11 stsp width = TABSIZE -
1219 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1221 748d5cab 2020-12-14 naddy width = 1;
1222 748d5cab 2020-12-14 naddy wline[i] = L'.';
1224 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1226 748d5cab 2020-12-14 naddy cols += width;
1229 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1230 963b370f 2018-05-20 stsp goto done;
1233 963b370f 2018-05-20 stsp wline[i] = L'\0';
1234 b700b5d6 2018-07-10 stsp if (widthp)
1235 b700b5d6 2018-07-10 stsp *widthp = cols;
1238 963b370f 2018-05-20 stsp free(wline);
1240 963b370f 2018-05-20 stsp *wlinep = wline;
1241 963b370f 2018-05-20 stsp return err;
1244 8b473291 2019-02-21 stsp static const struct got_error*
1245 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1246 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1248 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1249 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1251 8b473291 2019-02-21 stsp const char *name;
1253 8b473291 2019-02-21 stsp *refs_str = NULL;
1255 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1256 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1257 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1260 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1261 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1263 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1264 8b473291 2019-02-21 stsp name += 5;
1265 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1267 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1268 8b473291 2019-02-21 stsp name += 6;
1269 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1270 8b473291 2019-02-21 stsp name += 8;
1271 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1272 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1275 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1278 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1279 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1280 5d844a1e 2019-08-13 stsp if (err) {
1281 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1282 48cae60d 2020-09-22 stsp free(ref_id);
1285 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1286 5d844a1e 2019-08-13 stsp err = NULL;
1287 5d844a1e 2019-08-13 stsp tag = NULL;
1290 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1291 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1292 48cae60d 2020-09-22 stsp free(ref_id);
1294 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1295 52b5abe1 2019-08-13 stsp if (cmp != 0)
1297 8b473291 2019-02-21 stsp s = *refs_str;
1298 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1299 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1302 8b473291 2019-02-21 stsp *refs_str = NULL;
1308 8b473291 2019-02-21 stsp return err;
1311 963b370f 2018-05-20 stsp static const struct got_error *
1312 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1313 27a741e5 2019-09-11 stsp int col_tab_align)
1315 e6b8b890 2020-12-29 naddy char *smallerthan;
1317 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1318 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1319 5813d178 2019-03-09 stsp author = smallerthan + 1;
1320 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1321 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1324 5813d178 2019-03-09 stsp static const struct got_error *
1325 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1326 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1327 8fdc79fe 2020-12-01 naddy int author_display_cols)
1329 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1330 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1331 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1332 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1333 5813d178 2019-03-09 stsp char *author = NULL;
1334 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1335 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1336 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1337 bb737323 2018-05-20 stsp int col, limit;
1338 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1339 ccb26ccd 2018-11-05 stsp struct tm tm;
1340 45d799e2 2018-12-23 stsp time_t committer_time;
1341 11b20872 2019-11-08 stsp struct tog_color *tc;
1343 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1344 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1345 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1346 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1347 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1348 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1350 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1351 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1353 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1354 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1356 11b20872 2019-11-08 stsp wattr_on(view->window,
1357 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1358 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1360 11b20872 2019-11-08 stsp wattr_off(view->window,
1361 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1362 27a741e5 2019-09-11 stsp col = limit;
1363 b39d25c7 2018-07-10 stsp if (col > avail)
1364 b39d25c7 2018-07-10 stsp goto done;
1366 6570a66d 2019-11-08 stsp if (avail >= 120) {
1367 6570a66d 2019-11-08 stsp char *id_str;
1368 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1370 6570a66d 2019-11-08 stsp goto done;
1371 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1373 11b20872 2019-11-08 stsp wattr_on(view->window,
1374 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1375 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1377 11b20872 2019-11-08 stsp wattr_off(view->window,
1378 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1379 6570a66d 2019-11-08 stsp free(id_str);
1381 6570a66d 2019-11-08 stsp if (col > avail)
1382 6570a66d 2019-11-08 stsp goto done;
1385 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1386 5813d178 2019-03-09 stsp if (author == NULL) {
1387 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1388 80ddbec8 2018-04-29 stsp goto done;
1390 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1392 bb737323 2018-05-20 stsp goto done;
1393 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1395 11b20872 2019-11-08 stsp wattr_on(view->window,
1396 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1397 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1399 11b20872 2019-11-08 stsp wattr_off(view->window,
1400 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1401 bb737323 2018-05-20 stsp col += author_width;
1402 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1403 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1405 bb737323 2018-05-20 stsp author_width++;
1407 9c2eaf34 2018-05-20 stsp if (col > avail)
1408 9c2eaf34 2018-05-20 stsp goto done;
1410 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1412 6d9fbc00 2018-04-29 stsp goto done;
1413 bb737323 2018-05-20 stsp logmsg = logmsg0;
1414 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1416 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1417 bb737323 2018-05-20 stsp if (newline)
1418 bb737323 2018-05-20 stsp *newline = '\0';
1419 bb737323 2018-05-20 stsp limit = avail - col;
1420 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1422 bb737323 2018-05-20 stsp goto done;
1423 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1424 bb737323 2018-05-20 stsp col += logmsg_width;
1425 27a741e5 2019-09-11 stsp while (col < avail) {
1426 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1430 80ddbec8 2018-04-29 stsp free(logmsg0);
1431 bb737323 2018-05-20 stsp free(wlogmsg);
1432 5813d178 2019-03-09 stsp free(author);
1433 bb737323 2018-05-20 stsp free(wauthor);
1434 80ddbec8 2018-04-29 stsp free(line);
1435 80ddbec8 2018-04-29 stsp return err;
1438 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1439 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1440 899d86c2 2018-05-10 stsp struct got_object_id *id)
1442 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1444 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1445 80ddbec8 2018-04-29 stsp if (entry == NULL)
1446 899d86c2 2018-05-10 stsp return NULL;
1448 899d86c2 2018-05-10 stsp entry->id = id;
1449 99db9666 2018-05-07 stsp entry->commit = commit;
1450 899d86c2 2018-05-10 stsp return entry;
1453 99db9666 2018-05-07 stsp static void
1454 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1456 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1458 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1459 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1460 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1461 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1462 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1463 99db9666 2018-05-07 stsp free(entry);
1466 99db9666 2018-05-07 stsp static void
1467 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1469 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1470 99db9666 2018-05-07 stsp pop_commit(commits);
1473 c4972b91 2018-05-07 stsp static const struct got_error *
1474 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1475 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1477 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1478 13add988 2019-10-15 stsp regmatch_t regmatch;
1479 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1481 13add988 2019-10-15 stsp *have_match = 0;
1483 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1485 13add988 2019-10-15 stsp return err;
1487 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1489 13add988 2019-10-15 stsp goto done;
1491 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1492 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1493 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1494 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1495 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1496 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1497 13add988 2019-10-15 stsp *have_match = 1;
1499 13add988 2019-10-15 stsp free(id_str);
1500 13add988 2019-10-15 stsp free(logmsg);
1501 13add988 2019-10-15 stsp return err;
1504 13add988 2019-10-15 stsp static const struct got_error *
1505 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1507 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1510 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1511 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1512 1a76625f 2018-10-22 stsp * while updating the display.
1515 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1516 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1517 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1518 1a76625f 2018-10-22 stsp int errcode;
1520 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1521 4e0d2870 2020-12-07 naddy NULL, NULL);
1522 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1525 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1528 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1529 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1530 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1534 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1535 1a76625f 2018-10-22 stsp if (errcode) {
1536 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1537 13add988 2019-10-15 stsp "pthread_mutex_lock");
1541 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1542 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1543 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1545 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1546 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1547 7c1452c1 2020-03-26 stsp int have_match;
1548 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1551 7c1452c1 2020-03-26 stsp if (have_match)
1552 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1555 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1556 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1557 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1558 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1561 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1563 9ba79e04 2018-06-11 stsp return err;
1566 2b779855 2020-12-05 naddy static void
1567 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1569 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1570 2b779855 2020-12-05 naddy int ncommits = 0;
1572 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1573 2b779855 2020-12-05 naddy while (entry) {
1574 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1575 2b779855 2020-12-05 naddy s->selected_entry = entry;
1578 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1579 2b779855 2020-12-05 naddy ncommits++;
1583 0553a4e3 2018-04-30 stsp static const struct got_error *
1584 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1586 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1587 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1588 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1589 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1590 60493ae3 2019-06-20 stsp int width;
1591 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1592 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1593 8b473291 2019-02-21 stsp char *refs_str = NULL;
1594 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1595 11b20872 2019-11-08 stsp struct tog_color *tc;
1596 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1598 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1599 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1600 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1601 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1603 ecb28ae0 2018-07-16 stsp return err;
1604 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1605 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1606 d2075bf3 2020-12-25 stsp if (refs) {
1607 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1608 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1610 d2075bf3 2020-12-25 stsp goto done;
1614 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1615 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1617 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed > 0) {
1618 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1619 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1620 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1621 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1622 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1623 8f4ed634 2020-03-26 stsp goto done;
1626 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1628 f9686aa5 2020-03-27 stsp if (view->searching) {
1629 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1630 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1631 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1632 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1633 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1634 f9686aa5 2020-03-27 stsp search_str = "searching...";
1637 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1638 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1639 f9686aa5 2020-03-27 stsp search_str ? search_str :
1640 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1641 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1642 8f4ed634 2020-03-26 stsp goto done;
1646 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1647 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1648 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1649 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1650 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1651 1a76625f 2018-10-22 stsp header = NULL;
1652 1a76625f 2018-10-22 stsp goto done;
1654 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1655 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1656 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1657 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1658 1a76625f 2018-10-22 stsp header = NULL;
1659 1a76625f 2018-10-22 stsp goto done;
1661 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1663 1a76625f 2018-10-22 stsp goto done;
1665 2814baeb 2018-08-01 stsp werase(view->window);
1667 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1668 a3404814 2018-09-02 stsp wstandout(view->window);
1669 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1671 11b20872 2019-11-08 stsp wattr_on(view->window,
1672 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1673 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1675 11b20872 2019-11-08 stsp wattr_off(view->window,
1676 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1677 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1678 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1681 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1682 a3404814 2018-09-02 stsp wstandend(view->window);
1683 ecb28ae0 2018-07-16 stsp free(wline);
1684 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1685 1a76625f 2018-10-22 stsp goto done;
1687 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1688 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1689 5813d178 2019-03-09 stsp ncommits = 0;
1690 5813d178 2019-03-09 stsp while (entry) {
1691 5813d178 2019-03-09 stsp char *author;
1692 5813d178 2019-03-09 stsp wchar_t *wauthor;
1693 5813d178 2019-03-09 stsp int width;
1694 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1696 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1697 5813d178 2019-03-09 stsp if (author == NULL) {
1698 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1699 5813d178 2019-03-09 stsp goto done;
1701 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1702 27a741e5 2019-09-11 stsp date_display_cols);
1703 5813d178 2019-03-09 stsp if (author_cols < width)
1704 5813d178 2019-03-09 stsp author_cols = width;
1705 5813d178 2019-03-09 stsp free(wauthor);
1706 5813d178 2019-03-09 stsp free(author);
1707 7ca04879 2019-10-19 stsp ncommits++;
1708 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1711 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1712 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1713 867c6645 2018-07-10 stsp ncommits = 0;
1714 899d86c2 2018-05-10 stsp while (entry) {
1715 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1717 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1718 2814baeb 2018-08-01 stsp wstandout(view->window);
1719 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1720 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1721 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1722 2814baeb 2018-08-01 stsp wstandend(view->window);
1724 60493ae3 2019-06-20 stsp goto done;
1725 0553a4e3 2018-04-30 stsp ncommits++;
1726 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1727 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1730 1a57306a 2018-09-02 stsp view_vborder(view);
1732 1a76625f 2018-10-22 stsp free(id_str);
1733 8b473291 2019-02-21 stsp free(refs_str);
1734 1a76625f 2018-10-22 stsp free(ncommits_str);
1735 1a76625f 2018-10-22 stsp free(header);
1736 80ddbec8 2018-04-29 stsp return err;
1739 07b55e75 2018-05-10 stsp static void
1740 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1742 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1743 07b55e75 2018-05-10 stsp int nscrolled = 0;
1745 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1746 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1749 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1750 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1751 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1752 07b55e75 2018-05-10 stsp if (entry) {
1753 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1754 07b55e75 2018-05-10 stsp nscrolled++;
1759 aa075928 2018-05-10 stsp static const struct got_error *
1760 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1762 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1763 5e224a3e 2019-02-22 stsp int errcode;
1765 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1767 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1768 ffe38506 2020-12-01 naddy if (ta->log_complete)
1771 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1772 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1773 7aafa0d1 2019-02-22 stsp if (errcode)
1774 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1775 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1778 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1779 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1781 7c1452c1 2020-03-26 stsp if (!wait)
1784 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1785 ffe38506 2020-12-01 naddy show_log_view(view);
1786 7c1452c1 2020-03-26 stsp update_panels();
1787 7c1452c1 2020-03-26 stsp doupdate();
1789 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1790 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1791 82954512 2020-02-03 stsp if (errcode)
1792 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1793 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1795 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1796 ffe38506 2020-12-01 naddy show_log_view(view);
1797 7c1452c1 2020-03-26 stsp update_panels();
1798 7c1452c1 2020-03-26 stsp doupdate();
1801 5e224a3e 2019-02-22 stsp return NULL;
1804 5e224a3e 2019-02-22 stsp static const struct got_error *
1805 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1807 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1808 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1809 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1810 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1812 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1813 5e224a3e 2019-02-22 stsp return NULL;
1815 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1816 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1817 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1819 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1821 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1822 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1824 5e224a3e 2019-02-22 stsp return err;
1828 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1829 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1832 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1834 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1835 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1837 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1838 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1840 dd0a52c1 2018-05-20 stsp return err;
1843 cd0acaa7 2018-05-20 stsp static const struct got_error *
1844 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1845 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1846 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1848 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1849 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1850 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1852 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1853 15a94983 2018-12-23 stsp if (diff_view == NULL)
1854 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1856 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1857 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1858 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1859 e5a0f69f 2018-08-18 stsp if (err == NULL)
1860 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1861 cd0acaa7 2018-05-20 stsp return err;
1864 80ddbec8 2018-04-29 stsp static const struct got_error *
1865 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1866 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1868 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1870 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1871 941e9f74 2019-05-21 stsp if (parent == NULL)
1872 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1874 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1875 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1876 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1877 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1878 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1879 941e9f74 2019-05-21 stsp s->tree = subtree;
1880 941e9f74 2019-05-21 stsp s->selected = 0;
1881 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1882 941e9f74 2019-05-21 stsp return NULL;
1885 941e9f74 2019-05-21 stsp static const struct got_error *
1886 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1887 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1889 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1890 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1891 941e9f74 2019-05-21 stsp const char *p;
1892 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1894 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1896 941e9f74 2019-05-21 stsp while (*p) {
1897 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1898 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1899 56e0773d 2019-11-28 stsp char *te_name;
1901 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1904 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1905 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1906 941e9f74 2019-05-21 stsp if (slash == NULL)
1907 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1909 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1910 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1911 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1914 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1915 56e0773d 2019-11-28 stsp if (te == NULL) {
1916 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1917 56e0773d 2019-11-28 stsp free(te_name);
1920 56e0773d 2019-11-28 stsp free(te_name);
1921 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1923 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1924 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1926 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1927 941e9f74 2019-05-21 stsp if (slash)
1928 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1930 941e9f74 2019-05-21 stsp subpath = strdup(path);
1931 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1932 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1936 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1941 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1942 941e9f74 2019-05-21 stsp free(tree_id);
1946 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1947 941e9f74 2019-05-21 stsp if (err) {
1948 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1951 941e9f74 2019-05-21 stsp if (slash == NULL)
1953 941e9f74 2019-05-21 stsp free(subpath);
1954 941e9f74 2019-05-21 stsp subpath = NULL;
1955 941e9f74 2019-05-21 stsp p = slash;
1958 941e9f74 2019-05-21 stsp free(subpath);
1959 1a76625f 2018-10-22 stsp return err;
1962 61266923 2020-01-14 stsp static const struct got_error *
1963 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1964 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1965 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
1967 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1968 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1969 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1970 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1972 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1973 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1975 55cccc34 2020-02-20 stsp return err;
1977 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1978 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1979 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1981 4e97c21c 2020-12-06 stsp err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1982 55cccc34 2020-02-20 stsp if (err) {
1983 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1984 55cccc34 2020-02-20 stsp return err;
1986 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1988 55cccc34 2020-02-20 stsp *new_view = tree_view;
1990 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1991 55cccc34 2020-02-20 stsp return NULL;
1993 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1996 55cccc34 2020-02-20 stsp static const struct got_error *
1997 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1999 61266923 2020-01-14 stsp sigset_t sigset;
2000 61266923 2020-01-14 stsp int errcode;
2002 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2003 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2005 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
2006 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2007 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2008 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2009 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2011 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2012 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2013 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2015 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2016 61266923 2020-01-14 stsp if (errcode)
2017 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2019 61266923 2020-01-14 stsp return NULL;
2022 1a76625f 2018-10-22 stsp static void *
2023 1a76625f 2018-10-22 stsp log_thread(void *arg)
2025 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2026 1a76625f 2018-10-22 stsp int errcode = 0;
2027 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2028 1a76625f 2018-10-22 stsp int done = 0;
2030 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2032 61266923 2020-01-14 stsp return (void *)err;
2034 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2035 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2036 1a76625f 2018-10-22 stsp if (err) {
2037 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2038 1a76625f 2018-10-22 stsp return (void *)err;
2039 1a76625f 2018-10-22 stsp err = NULL;
2041 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
2042 1a76625f 2018-10-22 stsp a->commits_needed--;
2044 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2045 3abe8080 2019-04-10 stsp if (errcode) {
2046 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2047 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2049 3abe8080 2019-04-10 stsp } else if (*a->quit)
2051 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2052 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2053 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2054 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2057 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2058 7c1452c1 2020-03-26 stsp if (errcode) {
2059 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2060 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2061 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2066 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2068 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2069 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2070 7c1452c1 2020-03-26 stsp &tog_mutex);
2071 7c1452c1 2020-03-26 stsp if (errcode)
2072 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2073 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2074 21355643 2020-12-06 stsp if (*a->quit)
2079 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2080 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2081 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2082 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2084 3abe8080 2019-04-10 stsp a->log_complete = 1;
2085 1a76625f 2018-10-22 stsp return (void *)err;
2088 1a76625f 2018-10-22 stsp static const struct got_error *
2089 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2091 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2092 1a76625f 2018-10-22 stsp int errcode;
2094 1a76625f 2018-10-22 stsp if (s->thread) {
2095 1a76625f 2018-10-22 stsp s->quit = 1;
2096 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2097 1a76625f 2018-10-22 stsp if (errcode)
2098 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2099 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2100 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2101 1a76625f 2018-10-22 stsp if (errcode)
2102 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2103 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2104 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2105 1a76625f 2018-10-22 stsp if (errcode)
2106 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2107 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2108 1a76625f 2018-10-22 stsp if (errcode)
2109 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2110 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2111 1a76625f 2018-10-22 stsp s->thread = NULL;
2114 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2115 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2116 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2119 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2120 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2121 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2124 9343a5fb 2018-06-23 stsp return err;
2127 9343a5fb 2018-06-23 stsp static const struct got_error *
2128 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2130 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2131 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2132 276b94a1 2020-11-13 naddy int errcode;
2134 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2136 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2137 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2138 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2140 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2141 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2142 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2144 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2145 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2146 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2147 1a76625f 2018-10-22 stsp free(s->start_id);
2148 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2149 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2150 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2151 1a76625f 2018-10-22 stsp return err;
2154 1a76625f 2018-10-22 stsp static const struct got_error *
2155 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2157 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2159 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2160 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2161 60493ae3 2019-06-20 stsp return NULL;
2164 60493ae3 2019-06-20 stsp static const struct got_error *
2165 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2167 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2168 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2169 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2171 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2172 f9686aa5 2020-03-27 stsp show_log_view(view);
2173 f9686aa5 2020-03-27 stsp update_panels();
2174 f9686aa5 2020-03-27 stsp doupdate();
2176 96e2b566 2019-07-08 stsp if (s->search_entry) {
2177 13add988 2019-10-15 stsp int errcode, ch;
2178 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2179 13add988 2019-10-15 stsp if (errcode)
2180 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2181 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2182 13add988 2019-10-15 stsp ch = wgetch(view->window);
2183 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2184 13add988 2019-10-15 stsp if (errcode)
2185 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2186 13add988 2019-10-15 stsp "pthread_mutex_lock");
2187 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2188 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2189 678cbce5 2019-07-28 stsp return NULL;
2191 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2192 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2194 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2195 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2196 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2197 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2198 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2200 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2201 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2203 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2204 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2206 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2209 60493ae3 2019-06-20 stsp while (1) {
2210 13add988 2019-10-15 stsp int have_match = 0;
2212 60493ae3 2019-06-20 stsp if (entry == NULL) {
2213 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2214 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2215 f9967bca 2020-03-27 stsp view->search_next_done =
2216 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2217 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2218 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2219 f801134a 2019-06-25 stsp return NULL;
2222 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2223 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2224 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2226 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2227 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2230 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2231 13add988 2019-10-15 stsp &view->regex);
2234 13add988 2019-10-15 stsp if (have_match) {
2235 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2236 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2240 96e2b566 2019-07-08 stsp s->search_entry = entry;
2241 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2242 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2244 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2247 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2248 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2249 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2250 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2252 60493ae3 2019-06-20 stsp return err;
2255 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2256 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2258 60493ae3 2019-06-20 stsp return err;
2263 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2265 60493ae3 2019-06-20 stsp return NULL;
2268 60493ae3 2019-06-20 stsp static const struct got_error *
2269 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2270 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2271 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2273 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2274 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2275 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2276 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2277 1a76625f 2018-10-22 stsp int errcode;
2279 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2280 f135c941 2020-02-20 stsp free(s->in_repo_path);
2281 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2282 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2283 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2286 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2287 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2288 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2290 fb2756b9 2018-08-04 stsp s->repo = repo;
2291 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2292 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2293 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2294 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2295 9cd7cbd1 2020-12-07 stsp goto done;
2298 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2299 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2300 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2301 5036bf37 2018-09-24 stsp goto done;
2303 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2305 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2306 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2307 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2308 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2310 11b20872 2019-11-08 stsp goto done;
2311 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2312 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2313 11b20872 2019-11-08 stsp if (err) {
2314 11b20872 2019-11-08 stsp free_colors(&s->colors);
2315 11b20872 2019-11-08 stsp goto done;
2317 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2318 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2319 11b20872 2019-11-08 stsp if (err) {
2320 11b20872 2019-11-08 stsp free_colors(&s->colors);
2321 11b20872 2019-11-08 stsp goto done;
2325 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2326 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2327 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2328 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2329 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2331 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2333 1a76625f 2018-10-22 stsp goto done;
2334 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2335 b672a97a 2020-01-27 stsp !s->log_branches);
2337 1a76625f 2018-10-22 stsp goto done;
2338 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2339 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2341 c5b78334 2020-01-12 stsp goto done;
2343 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2344 1a76625f 2018-10-22 stsp if (errcode) {
2345 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2346 1a76625f 2018-10-22 stsp goto done;
2348 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2349 7c1452c1 2020-03-26 stsp if (errcode) {
2350 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2351 7c1452c1 2020-03-26 stsp goto done;
2354 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2355 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2356 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2357 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2358 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2359 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2360 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2361 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2362 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2363 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2364 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2365 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2366 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2369 1a76625f 2018-10-22 stsp close_log_view(view);
2370 ba4f502b 2018-08-04 stsp return err;
2373 e5a0f69f 2018-08-18 stsp static const struct got_error *
2374 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2376 f2f6d207 2020-11-24 stsp const struct got_error *err;
2377 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2379 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2380 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2381 2b380cc8 2018-10-24 stsp &s->thread_args);
2382 2b380cc8 2018-10-24 stsp if (errcode)
2383 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2384 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2385 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2387 f2f6d207 2020-11-24 stsp return err;
2391 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2394 e5a0f69f 2018-08-18 stsp static const struct got_error *
2395 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2397 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2398 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2399 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2400 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2401 669b5ffa 2018-10-07 stsp int begin_x = 0;
2403 e5a0f69f 2018-08-18 stsp switch (ch) {
2405 1e37a5c2 2019-05-12 jcs s->quit = 1;
2408 1e37a5c2 2019-05-12 jcs case KEY_UP:
2411 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2413 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2414 1e37a5c2 2019-05-12 jcs s->selected--;
2416 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2417 2b779855 2020-12-05 naddy select_commit(s);
2419 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2420 a4292ac5 2019-05-12 jcs case CTRL('b'):
2421 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2423 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2424 1e37a5c2 2019-05-12 jcs s->selected = 0;
2426 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2427 2b779855 2020-12-05 naddy select_commit(s);
2430 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2433 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2435 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2436 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2437 1e37a5c2 2019-05-12 jcs s->selected++;
2439 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2443 2b779855 2020-12-05 naddy select_commit(s);
2445 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2446 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2447 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2448 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2449 1e37a5c2 2019-05-12 jcs if (first == NULL)
2451 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2454 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2455 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2456 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2457 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2458 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2459 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2461 2b779855 2020-12-05 naddy select_commit(s);
2464 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2465 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2466 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2467 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2468 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2469 2b779855 2020-12-05 naddy select_commit(s);
2470 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2471 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2472 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2473 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2474 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2477 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2480 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2482 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2483 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2484 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2485 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2486 78756c87 2020-11-24 stsp view, s->repo);
2489 e78dc838 2020-12-04 stsp view->focussed = 0;
2490 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2491 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2492 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2494 1e37a5c2 2019-05-12 jcs return err;
2495 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2496 e78dc838 2020-12-04 stsp view->focus_child = 1;
2498 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2501 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2503 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2504 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2505 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2506 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2510 e78dc838 2020-12-04 stsp view->focussed = 0;
2511 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2512 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2513 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2515 1e37a5c2 2019-05-12 jcs return err;
2516 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2517 e78dc838 2020-12-04 stsp view->focus_child = 1;
2519 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2521 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2522 21355643 2020-12-06 stsp case CTRL('l'):
2524 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2525 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2527 21355643 2020-12-06 stsp err = stop_log_thread(s);
2529 74cfe85e 2020-10-20 stsp return err;
2530 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2531 21355643 2020-12-06 stsp char *parent_path;
2532 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2534 21355643 2020-12-06 stsp return err;
2535 21355643 2020-12-06 stsp free(s->in_repo_path);
2536 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2537 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2538 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2539 21355643 2020-12-06 stsp struct got_object_id *start_id;
2540 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2541 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2542 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2544 21355643 2020-12-06 stsp return err;
2545 21355643 2020-12-06 stsp free(s->start_id);
2546 21355643 2020-12-06 stsp s->start_id = start_id;
2547 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2548 21355643 2020-12-06 stsp } else /* 'B' */
2549 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2551 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2552 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2554 21355643 2020-12-06 stsp return err;
2555 51a10b52 2020-12-26 stsp tog_free_refs();
2556 51a10b52 2020-12-26 stsp err = tog_load_refs(s->repo);
2558 ca51c541 2020-12-07 stsp return err;
2559 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2560 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2562 d01904d4 2019-06-25 stsp return err;
2563 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2564 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2566 b672a97a 2020-01-27 stsp return err;
2567 21355643 2020-12-06 stsp free_commits(&s->commits);
2568 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2569 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2570 21355643 2020-12-06 stsp s->selected_entry = NULL;
2571 21355643 2020-12-06 stsp s->selected = 0;
2572 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2573 21355643 2020-12-06 stsp s->quit = 0;
2574 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2577 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2578 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2579 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2580 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2581 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2582 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2583 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2584 6458efa5 2020-11-24 stsp if (err) {
2585 6458efa5 2020-11-24 stsp view_close(ref_view);
2586 6458efa5 2020-11-24 stsp return err;
2588 e78dc838 2020-12-04 stsp view->focussed = 0;
2589 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2590 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2591 6458efa5 2020-11-24 stsp err = view_close_child(view);
2593 6458efa5 2020-11-24 stsp return err;
2594 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2595 e78dc838 2020-12-04 stsp view->focus_child = 1;
2597 6458efa5 2020-11-24 stsp *new_view = ref_view;
2603 80ddbec8 2018-04-29 stsp return err;
2606 4ed7e80c 2018-05-20 stsp static const struct got_error *
2607 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2609 c2db6724 2019-01-04 stsp const struct got_error *error;
2611 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2612 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2613 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2615 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2616 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2618 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2619 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2621 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2622 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2624 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2625 c2db6724 2019-01-04 stsp if (error != NULL)
2626 c2db6724 2019-01-04 stsp return error;
2628 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2629 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2631 c2db6724 2019-01-04 stsp return NULL;
2634 a915003a 2019-02-05 stsp static void
2635 a915003a 2019-02-05 stsp init_curses(void)
2637 a915003a 2019-02-05 stsp initscr();
2639 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2642 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2643 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2644 a915003a 2019-02-05 stsp curs_set(0);
2645 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2646 6d17833f 2019-11-08 stsp start_color();
2647 6d17833f 2019-11-08 stsp use_default_colors();
2649 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2650 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2651 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2654 c2db6724 2019-01-04 stsp static const struct got_error *
2655 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2656 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2658 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2660 f135c941 2020-02-20 stsp if (argc == 0) {
2661 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2662 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2663 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2664 f135c941 2020-02-20 stsp return NULL;
2667 f135c941 2020-02-20 stsp if (worktree) {
2668 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2671 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2673 f135c941 2020-02-20 stsp return err;
2674 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2675 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2676 bfd61697 2020-11-14 stsp p) == -1) {
2677 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2678 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2682 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2684 f135c941 2020-02-20 stsp return err;
2687 f135c941 2020-02-20 stsp static const struct got_error *
2688 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2690 80ddbec8 2018-04-29 stsp const struct got_error *error;
2691 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2692 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2693 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2694 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2695 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2696 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2697 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2698 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2699 04cc582a 2018-08-01 stsp struct tog_view *view;
2701 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2702 80ddbec8 2018-04-29 stsp switch (ch) {
2704 b672a97a 2020-01-27 stsp log_branches = 1;
2707 80ddbec8 2018-04-29 stsp start_commit = optarg;
2710 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2711 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2712 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2716 17020d27 2019-03-07 stsp usage_log();
2717 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2721 80ddbec8 2018-04-29 stsp argc -= optind;
2722 80ddbec8 2018-04-29 stsp argv += optind;
2724 f135c941 2020-02-20 stsp if (argc > 1)
2725 f135c941 2020-02-20 stsp usage_log();
2727 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2728 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2729 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2730 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2731 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2732 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2733 c156c7a4 2020-12-18 stsp goto done;
2734 a1fbf39a 2019-08-11 stsp if (worktree)
2735 6962eb72 2020-02-20 stsp repo_path =
2736 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2738 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2739 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2740 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2741 c156c7a4 2020-12-18 stsp goto done;
2745 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2746 80ddbec8 2018-04-29 stsp if (error != NULL)
2747 ecb28ae0 2018-07-16 stsp goto done;
2749 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2750 f135c941 2020-02-20 stsp repo, worktree);
2751 f135c941 2020-02-20 stsp if (error)
2752 f135c941 2020-02-20 stsp goto done;
2754 f135c941 2020-02-20 stsp init_curses();
2756 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2757 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2758 c02c541e 2019-03-29 stsp if (error)
2759 c02c541e 2019-03-29 stsp goto done;
2761 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2762 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2763 87670572 2020-12-26 naddy error = tog_load_refs(repo);
2764 87670572 2020-12-26 naddy if (error)
2765 87670572 2020-12-26 naddy goto done;
2768 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2769 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2770 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2771 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2772 d8f38dc4 2020-12-05 stsp if (error)
2773 d8f38dc4 2020-12-05 stsp goto done;
2774 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2776 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2777 d8f38dc4 2020-12-05 stsp if (error == NULL)
2778 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2779 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2780 d8f38dc4 2020-12-05 stsp goto done;
2781 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2782 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2783 d8f38dc4 2020-12-05 stsp if (error)
2784 d8f38dc4 2020-12-05 stsp goto done;
2787 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2788 04cc582a 2018-08-01 stsp if (view == NULL) {
2789 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2790 04cc582a 2018-08-01 stsp goto done;
2792 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2793 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2794 ba4f502b 2018-08-04 stsp if (error)
2795 ba4f502b 2018-08-04 stsp goto done;
2796 2fc00ff4 2019-08-31 stsp if (worktree) {
2797 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2798 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2799 2fc00ff4 2019-08-31 stsp worktree = NULL;
2801 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2803 f135c941 2020-02-20 stsp free(in_repo_path);
2804 ecb28ae0 2018-07-16 stsp free(repo_path);
2805 ecb28ae0 2018-07-16 stsp free(cwd);
2806 899d86c2 2018-05-10 stsp free(start_id);
2807 d8f38dc4 2020-12-05 stsp free(label);
2809 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2811 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2812 ec142235 2019-03-07 stsp if (worktree)
2813 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2814 51a10b52 2020-12-26 stsp tog_free_refs();
2815 80ddbec8 2018-04-29 stsp return error;
2818 4ed7e80c 2018-05-20 stsp __dead static void
2819 9f7d7167 2018-04-29 stsp usage_diff(void)
2822 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2823 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2827 6d17833f 2019-11-08 stsp static int
2828 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2829 41605754 2020-11-12 stsp regmatch_t *regmatch)
2831 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2834 f26dddb7 2019-11-08 stsp struct tog_color *
2835 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2837 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2839 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2840 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2841 6d17833f 2019-11-08 stsp return tc;
2844 6d17833f 2019-11-08 stsp return NULL;
2847 4ed7e80c 2018-05-20 stsp static const struct got_error *
2848 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2849 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2851 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2852 41605754 2020-11-12 stsp wchar_t *wline;
2853 41605754 2020-11-12 stsp int width;
2856 41605754 2020-11-12 stsp *wtotal = 0;
2858 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2859 41605754 2020-11-12 stsp if (s == NULL)
2860 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2862 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2863 41605754 2020-11-12 stsp if (err) {
2865 41605754 2020-11-12 stsp return err;
2867 41605754 2020-11-12 stsp waddwstr(window, wline);
2868 41605754 2020-11-12 stsp free(wline);
2870 41605754 2020-11-12 stsp wlimit -= width;
2871 41605754 2020-11-12 stsp *wtotal += width;
2873 41605754 2020-11-12 stsp if (wlimit > 0) {
2874 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2875 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2876 41605754 2020-11-12 stsp if (s == NULL) {
2877 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2879 41605754 2020-11-12 stsp return err;
2881 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2882 41605754 2020-11-12 stsp if (err) {
2884 41605754 2020-11-12 stsp return err;
2886 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2887 41605754 2020-11-12 stsp waddwstr(window, wline);
2888 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2889 41605754 2020-11-12 stsp free(wline);
2891 41605754 2020-11-12 stsp wlimit -= width;
2892 41605754 2020-11-12 stsp *wtotal += width;
2895 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2896 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2897 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2899 41605754 2020-11-12 stsp return err;
2900 41605754 2020-11-12 stsp waddwstr(window, wline);
2901 41605754 2020-11-12 stsp free(wline);
2902 41605754 2020-11-12 stsp *wtotal += width;
2905 41605754 2020-11-12 stsp return NULL;
2908 41605754 2020-11-12 stsp static const struct got_error *
2909 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2911 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2912 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2913 61e69b96 2018-05-20 stsp const struct got_error *err;
2914 fe621944 2020-11-10 stsp int nprinted = 0;
2915 b304db33 2018-05-20 stsp char *line;
2916 826082fe 2020-12-10 stsp size_t linesize = 0;
2917 826082fe 2020-12-10 stsp ssize_t linelen;
2918 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2919 61e69b96 2018-05-20 stsp wchar_t *wline;
2920 e0b650dd 2018-05-20 stsp int width;
2921 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2922 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2923 fe621944 2020-11-10 stsp off_t line_offset;
2925 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2926 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2927 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2929 f7d12f7e 2018-08-01 stsp werase(view->window);
2931 a3404814 2018-09-02 stsp if (header) {
2932 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2933 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2934 135a2da0 2020-11-11 stsp header) == -1)
2935 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2936 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2937 135a2da0 2020-11-11 stsp free(line);
2939 a3404814 2018-09-02 stsp return err;
2941 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2942 a3404814 2018-09-02 stsp wstandout(view->window);
2943 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2944 e54cc94a 2020-11-11 stsp free(wline);
2945 e54cc94a 2020-11-11 stsp wline = NULL;
2946 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2947 a3404814 2018-09-02 stsp wstandend(view->window);
2948 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2949 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2951 a3404814 2018-09-02 stsp if (max_lines <= 1)
2952 a3404814 2018-09-02 stsp return NULL;
2953 a3404814 2018-09-02 stsp max_lines--;
2956 89f1a395 2020-12-01 naddy s->eof = 0;
2957 826082fe 2020-12-10 stsp line = NULL;
2958 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2959 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
2960 826082fe 2020-12-10 stsp if (linelen == -1) {
2961 826082fe 2020-12-10 stsp if (feof(s->f)) {
2962 826082fe 2020-12-10 stsp s->eof = 1;
2965 826082fe 2020-12-10 stsp free(line);
2966 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
2969 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
2971 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2972 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2973 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
2974 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2975 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2976 41605754 2020-11-12 stsp view->window, regmatch);
2977 41605754 2020-11-12 stsp if (err) {
2978 41605754 2020-11-12 stsp free(line);
2979 41605754 2020-11-12 stsp return err;
2982 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2983 41605754 2020-11-12 stsp if (err) {
2984 41605754 2020-11-12 stsp free(line);
2985 41605754 2020-11-12 stsp return err;
2987 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2988 41605754 2020-11-12 stsp free(wline);
2989 41605754 2020-11-12 stsp wline = NULL;
2992 6d17833f 2019-11-08 stsp wattr_off(view->window,
2993 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2994 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2995 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2996 fe621944 2020-11-10 stsp nprinted++;
2998 826082fe 2020-12-10 stsp free(line);
2999 fe621944 2020-11-10 stsp if (nprinted >= 1)
3000 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3001 89f1a395 2020-12-01 naddy (nprinted - 1);
3003 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3005 1a57306a 2018-09-02 stsp view_vborder(view);
3007 89f1a395 2020-12-01 naddy if (s->eof) {
3008 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3009 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3010 c3e9aa98 2019-05-13 jcs nprinted++;
3013 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3015 c3e9aa98 2019-05-13 jcs return err;
3018 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3019 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3020 e54cc94a 2020-11-11 stsp free(wline);
3021 e54cc94a 2020-11-11 stsp wline = NULL;
3022 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3025 26ed57b2 2018-05-19 stsp return NULL;
3028 abd2672a 2018-12-23 stsp static char *
3029 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3031 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3032 09867e48 2019-08-13 stsp char *p, *s;
3034 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3035 09867e48 2019-08-13 stsp if (tm == NULL)
3036 09867e48 2019-08-13 stsp return NULL;
3037 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3038 09867e48 2019-08-13 stsp if (s == NULL)
3039 09867e48 2019-08-13 stsp return NULL;
3040 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3042 abd2672a 2018-12-23 stsp *p = '\0';
3046 4ed7e80c 2018-05-20 stsp static const struct got_error *
3047 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3048 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3050 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3051 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3052 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3053 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3055 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3056 0208f208 2020-05-05 stsp if (qid != NULL) {
3057 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3058 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3061 0208f208 2020-05-05 stsp return err;
3063 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3064 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3068 0208f208 2020-05-05 stsp if (tree_id1) {
3069 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3071 0208f208 2020-05-05 stsp goto done;
3074 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3075 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3077 0208f208 2020-05-05 stsp goto done;
3079 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3080 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3082 0208f208 2020-05-05 stsp if (tree1)
3083 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3084 0208f208 2020-05-05 stsp if (tree2)
3085 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3086 0208f208 2020-05-05 stsp return err;
3089 0208f208 2020-05-05 stsp static const struct got_error *
3090 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3094 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3095 fe621944 2020-11-10 stsp if (p == NULL)
3096 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3097 fe621944 2020-11-10 stsp *line_offsets = p;
3098 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3099 fe621944 2020-11-10 stsp (*nlines)++;
3100 fe621944 2020-11-10 stsp return NULL;
3103 fe621944 2020-11-10 stsp static const struct got_error *
3104 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3105 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3106 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3108 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3109 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3110 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3111 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3112 45d799e2 2018-12-23 stsp time_t committer_time;
3113 45d799e2 2018-12-23 stsp const char *author, *committer;
3114 8b473291 2019-02-21 stsp char *refs_str = NULL;
3115 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3116 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3117 fe621944 2020-11-10 stsp off_t outoff = 0;
3120 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3122 8b473291 2019-02-21 stsp if (refs) {
3123 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3125 8b473291 2019-02-21 stsp return err;
3128 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3130 abd2672a 2018-12-23 stsp return err;
3132 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3133 15a94983 2018-12-23 stsp if (err) {
3134 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3135 15a94983 2018-12-23 stsp goto done;
3138 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3140 fe621944 2020-11-10 stsp goto done;
3142 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3143 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3144 fe621944 2020-11-10 stsp if (n < 0) {
3145 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3146 abd2672a 2018-12-23 stsp goto done;
3148 fe621944 2020-11-10 stsp outoff += n;
3149 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3151 fe621944 2020-11-10 stsp goto done;
3153 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3154 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3155 fe621944 2020-11-10 stsp if (n < 0) {
3156 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3157 abd2672a 2018-12-23 stsp goto done;
3159 fe621944 2020-11-10 stsp outoff += n;
3160 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3162 fe621944 2020-11-10 stsp goto done;
3164 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3165 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3166 fe621944 2020-11-10 stsp if (datestr) {
3167 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3168 fe621944 2020-11-10 stsp if (n < 0) {
3169 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3170 fe621944 2020-11-10 stsp goto done;
3172 fe621944 2020-11-10 stsp outoff += n;
3173 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3175 fe621944 2020-11-10 stsp goto done;
3177 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3178 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3179 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3180 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3181 fe621944 2020-11-10 stsp if (n < 0) {
3182 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3183 fe621944 2020-11-10 stsp goto done;
3185 fe621944 2020-11-10 stsp outoff += n;
3186 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3188 fe621944 2020-11-10 stsp goto done;
3190 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3192 5943eee2 2019-08-13 stsp goto done;
3193 fe621944 2020-11-10 stsp s = logmsg;
3194 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3195 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3196 fe621944 2020-11-10 stsp if (n < 0) {
3197 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3198 fe621944 2020-11-10 stsp goto done;
3200 fe621944 2020-11-10 stsp outoff += n;
3201 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3203 fe621944 2020-11-10 stsp goto done;
3206 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3208 0208f208 2020-05-05 stsp goto done;
3209 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3210 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3211 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3212 fe621944 2020-11-10 stsp if (n < 0) {
3213 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3214 fe621944 2020-11-10 stsp goto done;
3216 fe621944 2020-11-10 stsp outoff += n;
3217 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3219 fe621944 2020-11-10 stsp goto done;
3220 0208f208 2020-05-05 stsp free((char *)pe->path);
3221 0208f208 2020-05-05 stsp free(pe->data);
3224 0208f208 2020-05-05 stsp fputc('\n', outfile);
3226 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3228 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3229 abd2672a 2018-12-23 stsp free(id_str);
3230 5943eee2 2019-08-13 stsp free(logmsg);
3231 8b473291 2019-02-21 stsp free(refs_str);
3232 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3233 7510f233 2020-08-09 stsp if (err) {
3234 ae6a6978 2020-08-09 stsp free(*line_offsets);
3235 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3236 ae6a6978 2020-08-09 stsp *nlines = 0;
3238 fe621944 2020-11-10 stsp return err;
3241 abd2672a 2018-12-23 stsp static const struct got_error *
3242 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3244 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3245 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3246 15a94983 2018-12-23 stsp int obj_type;
3248 fe621944 2020-11-10 stsp free(s->line_offsets);
3249 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3250 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3251 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3252 fe621944 2020-11-10 stsp s->nlines = 0;
3254 511a516b 2018-05-19 stsp f = got_opentemp();
3255 48ae06ee 2018-10-18 stsp if (f == NULL) {
3256 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3257 48ae06ee 2018-10-18 stsp goto done;
3259 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3260 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3261 fb43ecf1 2019-02-11 stsp goto done;
3265 15a94983 2018-12-23 stsp if (s->id1)
3266 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3268 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3270 15a94983 2018-12-23 stsp goto done;
3272 15a94983 2018-12-23 stsp switch (obj_type) {
3273 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3274 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3275 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3276 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3278 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3279 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3280 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3281 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3283 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3284 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3285 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3286 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3287 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3289 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3291 3ffacbe1 2020-02-02 tracey goto done;
3292 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3293 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3294 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3295 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3296 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3297 f44b1f58 2020-02-02 tracey if (err)
3298 f44b1f58 2020-02-02 tracey goto done;
3299 f44b1f58 2020-02-02 tracey } else {
3300 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3301 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3302 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3303 fe621944 2020-11-10 stsp err = write_commit_info(
3304 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3305 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3306 f44b1f58 2020-02-02 tracey if (err)
3307 f44b1f58 2020-02-02 tracey goto done;
3312 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3314 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3315 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3316 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3320 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3323 f44b1f58 2020-02-02 tracey if (err)
3324 f44b1f58 2020-02-02 tracey goto done;
3326 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3327 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3328 48ae06ee 2018-10-18 stsp return err;
3331 f5215bb9 2019-02-22 stsp static void
3332 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3334 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3335 f5215bb9 2019-02-22 stsp update_panels();
3336 f5215bb9 2019-02-22 stsp doupdate();
3339 f44b1f58 2020-02-02 tracey static const struct got_error *
3340 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3342 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3344 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3345 f44b1f58 2020-02-02 tracey return NULL;
3348 f44b1f58 2020-02-02 tracey static const struct got_error *
3349 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3351 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3352 f44b1f58 2020-02-02 tracey int lineno;
3353 826082fe 2020-12-10 stsp char *line = NULL;
3354 826082fe 2020-12-10 stsp size_t linesize = 0;
3355 826082fe 2020-12-10 stsp ssize_t linelen;
3357 f44b1f58 2020-02-02 tracey if (!view->searching) {
3358 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3359 f44b1f58 2020-02-02 tracey return NULL;
3362 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3363 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3364 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3366 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3367 f44b1f58 2020-02-02 tracey } else {
3368 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3369 f44b1f58 2020-02-02 tracey lineno = 1;
3371 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3374 f44b1f58 2020-02-02 tracey while (1) {
3375 f44b1f58 2020-02-02 tracey off_t offset;
3377 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3378 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3379 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3383 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3384 f44b1f58 2020-02-02 tracey lineno = 1;
3386 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3389 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3390 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3391 f44b1f58 2020-02-02 tracey free(line);
3392 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3394 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3395 826082fe 2020-12-10 stsp if (linelen != -1 &&
3396 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3397 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3398 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3401 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3402 f44b1f58 2020-02-02 tracey lineno++;
3404 f44b1f58 2020-02-02 tracey lineno--;
3406 826082fe 2020-12-10 stsp free(line);
3408 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3409 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3410 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3413 f44b1f58 2020-02-02 tracey return NULL;
3416 48ae06ee 2018-10-18 stsp static const struct got_error *
3417 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3418 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3419 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3420 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3422 48ae06ee 2018-10-18 stsp const struct got_error *err;
3423 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3425 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3426 15a94983 2018-12-23 stsp int type1, type2;
3427 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3429 15a94983 2018-12-23 stsp return err;
3430 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3432 15a94983 2018-12-23 stsp return err;
3434 15a94983 2018-12-23 stsp if (type1 != type2)
3435 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3437 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3438 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3439 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3440 f44b1f58 2020-02-02 tracey s->repo = repo;
3441 f44b1f58 2020-02-02 tracey s->id1 = id1;
3442 f44b1f58 2020-02-02 tracey s->id2 = id2;
3443 3dbaef42 2020-11-24 stsp s->label1 = label1;
3444 3dbaef42 2020-11-24 stsp s->label2 = label2;
3446 15a94983 2018-12-23 stsp if (id1) {
3447 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3448 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3449 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3451 5465d566 2020-02-01 tracey s->id1 = NULL;
3453 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3454 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3455 5465d566 2020-02-01 tracey free(s->id1);
3456 5465d566 2020-02-01 tracey s->id1 = NULL;
3457 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3459 5465d566 2020-02-01 tracey s->f = NULL;
3460 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3461 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3462 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3463 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3464 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3465 5465d566 2020-02-01 tracey s->log_view = log_view;
3466 5465d566 2020-02-01 tracey s->repo = repo;
3468 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3469 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3470 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3471 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3472 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3474 6d17833f 2019-11-08 stsp return err;
3475 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3476 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3477 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3478 6d17833f 2019-11-08 stsp if (err) {
3479 5465d566 2020-02-01 tracey free_colors(&s->colors);
3480 6d17833f 2019-11-08 stsp return err;
3482 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3483 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3484 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3485 6d17833f 2019-11-08 stsp if (err) {
3486 5465d566 2020-02-01 tracey free_colors(&s->colors);
3487 6d17833f 2019-11-08 stsp return err;
3490 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3491 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3492 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3493 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3494 6d17833f 2019-11-08 stsp if (err) {
3495 5465d566 2020-02-01 tracey free_colors(&s->colors);
3496 6d17833f 2019-11-08 stsp return err;
3499 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3500 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3501 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3502 11b20872 2019-11-08 stsp if (err) {
3503 5465d566 2020-02-01 tracey free_colors(&s->colors);
3504 11b20872 2019-11-08 stsp return err;
3507 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3508 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3509 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3510 11b20872 2019-11-08 stsp if (err) {
3511 5465d566 2020-02-01 tracey free_colors(&s->colors);
3512 11b20872 2019-11-08 stsp return err;
3516 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3517 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3518 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3520 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3521 fe621944 2020-11-10 stsp s->nlines = 0;
3522 5465d566 2020-02-01 tracey err = create_diff(s);
3523 48ae06ee 2018-10-18 stsp if (err) {
3524 5465d566 2020-02-01 tracey free(s->id1);
3525 5465d566 2020-02-01 tracey s->id1 = NULL;
3526 5465d566 2020-02-01 tracey free(s->id2);
3527 5465d566 2020-02-01 tracey s->id2 = NULL;
3528 78756c87 2020-11-24 stsp free_colors(&s->colors);
3529 48ae06ee 2018-10-18 stsp return err;
3532 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3533 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3534 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3535 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3536 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3538 5dc9f4bc 2018-08-04 stsp return NULL;
3541 e5a0f69f 2018-08-18 stsp static const struct got_error *
3542 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3544 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3545 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3547 5465d566 2020-02-01 tracey free(s->id1);
3548 5465d566 2020-02-01 tracey s->id1 = NULL;
3549 5465d566 2020-02-01 tracey free(s->id2);
3550 5465d566 2020-02-01 tracey s->id2 = NULL;
3551 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3552 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3553 5465d566 2020-02-01 tracey free_colors(&s->colors);
3554 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3555 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3556 fe621944 2020-11-10 stsp s->nlines = 0;
3557 e5a0f69f 2018-08-18 stsp return err;
3560 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3561 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3563 a3404814 2018-09-02 stsp const struct got_error *err;
3564 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3565 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3566 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3568 a3404814 2018-09-02 stsp if (s->id1) {
3569 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3571 a3404814 2018-09-02 stsp return err;
3572 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3574 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3576 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3578 a3404814 2018-09-02 stsp return err;
3579 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3581 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3582 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3583 a3404814 2018-09-02 stsp free(id_str1);
3584 a3404814 2018-09-02 stsp free(id_str2);
3585 a3404814 2018-09-02 stsp return err;
3587 a3404814 2018-09-02 stsp free(id_str1);
3588 a3404814 2018-09-02 stsp free(id_str2);
3590 89f1a395 2020-12-01 naddy return draw_file(view, header);
3593 15a087fe 2019-02-21 stsp static const struct got_error *
3594 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3595 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3597 d7a04538 2019-02-21 stsp const struct got_error *err;
3598 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3599 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3600 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3602 15a087fe 2019-02-21 stsp free(s->id2);
3603 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3604 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3605 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3607 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3609 d7a04538 2019-02-21 stsp return err;
3610 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3611 15a087fe 2019-02-21 stsp free(s->id1);
3612 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3613 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3614 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3615 15a087fe 2019-02-21 stsp return NULL;
3618 0cf4efb1 2018-09-29 stsp static const struct got_error *
3619 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3621 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3622 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3623 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3624 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3625 826082fe 2020-12-10 stsp char *line = NULL;
3626 826082fe 2020-12-10 stsp size_t linesize = 0;
3627 826082fe 2020-12-10 stsp ssize_t linelen;
3630 e5a0f69f 2018-08-18 stsp switch (ch) {
3633 3dbaef42 2020-11-24 stsp if (ch == 'a')
3634 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3635 3dbaef42 2020-11-24 stsp if (ch == 'w')
3636 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3637 64453f7e 2020-11-21 stsp wclear(view->window);
3638 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3639 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3640 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3641 64453f7e 2020-11-21 stsp err = create_diff(s);
3644 1e37a5c2 2019-05-12 jcs case KEY_UP:
3645 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3646 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3648 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3649 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3650 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3653 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3654 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3655 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3658 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3659 1e37a5c2 2019-05-12 jcs if (!s->eof)
3660 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3662 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3663 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3665 00ba99a7 2019-05-12 jcs if (s->eof)
3668 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3669 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3670 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3671 826082fe 2020-12-10 stsp if (linelen == -1) {
3672 826082fe 2020-12-10 stsp if (feof(s->f)) {
3673 826082fe 2020-12-10 stsp s->eof = 1;
3675 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3679 826082fe 2020-12-10 stsp free(line);
3682 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3683 1e37a5c2 2019-05-12 jcs s->diff_context--;
3684 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3685 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3686 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3687 27829c9e 2020-11-21 stsp s->nlines) {
3688 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3689 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3694 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3695 1e37a5c2 2019-05-12 jcs s->diff_context++;
3696 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3697 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3702 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3704 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3705 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3707 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3711 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3714 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3718 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3719 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3721 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3722 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3726 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3728 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3729 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3731 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3735 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3738 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3742 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3743 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3745 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3746 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3752 bcbd79e2 2018-08-19 stsp return err;
3755 4ed7e80c 2018-05-20 stsp static const struct got_error *
3756 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3758 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3759 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3760 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3761 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3762 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3763 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3764 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3765 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3766 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3767 3dbaef42 2020-11-24 stsp const char *errstr;
3768 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3770 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3771 26ed57b2 2018-05-19 stsp switch (ch) {
3773 64453f7e 2020-11-21 stsp force_text_diff = 1;
3776 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3778 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3779 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3781 09b5bff8 2020-02-23 naddy case 'r':
3782 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3783 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3784 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3786 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3789 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3792 17020d27 2019-03-07 stsp usage_diff();
3793 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3797 26ed57b2 2018-05-19 stsp argc -= optind;
3798 26ed57b2 2018-05-19 stsp argv += optind;
3800 26ed57b2 2018-05-19 stsp if (argc == 0) {
3801 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3802 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3803 15a94983 2018-12-23 stsp id_str1 = argv[0];
3804 15a94983 2018-12-23 stsp id_str2 = argv[1];
3806 26ed57b2 2018-05-19 stsp usage_diff();
3808 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3809 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3810 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3811 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3812 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3813 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3814 c156c7a4 2020-12-18 stsp goto done;
3815 a273ac94 2020-02-23 naddy if (worktree)
3816 a273ac94 2020-02-23 naddy repo_path =
3817 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3819 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3820 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3821 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3822 c156c7a4 2020-12-18 stsp goto done;
3826 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3827 eb6600df 2019-01-04 stsp if (error)
3828 eb6600df 2019-01-04 stsp goto done;
3830 a273ac94 2020-02-23 naddy init_curses();
3832 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3833 51a10b52 2020-12-26 stsp if (error)
3834 51a10b52 2020-12-26 stsp goto done;
3836 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
3837 26ed57b2 2018-05-19 stsp if (error)
3838 26ed57b2 2018-05-19 stsp goto done;
3840 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3841 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3842 26ed57b2 2018-05-19 stsp if (error)
3843 26ed57b2 2018-05-19 stsp goto done;
3845 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3846 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3847 26ed57b2 2018-05-19 stsp if (error)
3848 26ed57b2 2018-05-19 stsp goto done;
3850 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3851 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3852 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3853 ea5e7bb5 2018-08-01 stsp goto done;
3855 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3856 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3857 5dc9f4bc 2018-08-04 stsp if (error)
3858 5dc9f4bc 2018-08-04 stsp goto done;
3859 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3861 3dbaef42 2020-11-24 stsp free(label1);
3862 3dbaef42 2020-11-24 stsp free(label2);
3863 c02c541e 2019-03-29 stsp free(repo_path);
3864 a273ac94 2020-02-23 naddy free(cwd);
3866 921be706 2019-06-28 stsp got_repo_close(repo);
3867 a273ac94 2020-02-23 naddy if (worktree)
3868 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3869 51a10b52 2020-12-26 stsp tog_free_refs();
3870 26ed57b2 2018-05-19 stsp return error;
3873 4ed7e80c 2018-05-20 stsp __dead static void
3874 9f7d7167 2018-04-29 stsp usage_blame(void)
3877 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3878 9f7d7167 2018-04-29 stsp getprogname());
3882 84451b3e 2018-07-10 stsp struct tog_blame_line {
3883 84451b3e 2018-07-10 stsp int annotated;
3884 84451b3e 2018-07-10 stsp struct got_object_id *id;
3887 4ed7e80c 2018-05-20 stsp static const struct got_error *
3888 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3890 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3891 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3892 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3893 84451b3e 2018-07-10 stsp const struct got_error *err;
3894 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3895 826082fe 2020-12-10 stsp char *line = NULL;
3896 826082fe 2020-12-10 stsp size_t linesize = 0;
3897 826082fe 2020-12-10 stsp ssize_t linelen;
3898 84451b3e 2018-07-10 stsp wchar_t *wline;
3899 27a741e5 2019-09-11 stsp int width;
3900 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3901 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3902 ab089a2a 2018-07-12 stsp char *id_str;
3903 11b20872 2019-11-08 stsp struct tog_color *tc;
3905 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3907 ab089a2a 2018-07-12 stsp return err;
3909 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
3910 f7d12f7e 2018-08-01 stsp werase(view->window);
3912 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3913 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3914 ab089a2a 2018-07-12 stsp free(id_str);
3915 ab089a2a 2018-07-12 stsp return err;
3918 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3919 ab089a2a 2018-07-12 stsp free(line);
3920 2550e4c3 2018-07-13 stsp line = NULL;
3922 1cae65b4 2019-09-22 stsp return err;
3923 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3924 a3404814 2018-09-02 stsp wstandout(view->window);
3925 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3927 11b20872 2019-11-08 stsp wattr_on(view->window,
3928 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3929 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3931 11b20872 2019-11-08 stsp wattr_off(view->window,
3932 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3933 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3934 a3404814 2018-09-02 stsp wstandend(view->window);
3935 2550e4c3 2018-07-13 stsp free(wline);
3936 2550e4c3 2018-07-13 stsp wline = NULL;
3937 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3938 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3940 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3941 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3942 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3943 ab089a2a 2018-07-12 stsp free(id_str);
3944 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3946 ab089a2a 2018-07-12 stsp free(id_str);
3947 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3948 3f60a8ef 2018-07-10 stsp free(line);
3949 2550e4c3 2018-07-13 stsp line = NULL;
3951 3f60a8ef 2018-07-10 stsp return err;
3952 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3953 2550e4c3 2018-07-13 stsp free(wline);
3954 2550e4c3 2018-07-13 stsp wline = NULL;
3955 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3956 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3958 4f7c3e5e 2020-12-01 naddy s->eof = 0;
3959 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
3960 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
3961 826082fe 2020-12-10 stsp if (linelen == -1) {
3962 826082fe 2020-12-10 stsp if (feof(blame->f)) {
3963 826082fe 2020-12-10 stsp s->eof = 1;
3966 84451b3e 2018-07-10 stsp free(line);
3967 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
3969 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
3972 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3973 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3975 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
3976 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
3977 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3978 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3979 27a741e5 2019-09-11 stsp !(view->focussed &&
3980 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
3981 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3982 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3983 8d0fe45a 2019-08-12 stsp char *id_str;
3984 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3985 8d0fe45a 2019-08-12 stsp if (err) {
3986 8d0fe45a 2019-08-12 stsp free(line);
3987 8d0fe45a 2019-08-12 stsp return err;
3989 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3991 11b20872 2019-11-08 stsp wattr_on(view->window,
3992 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3993 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3995 11b20872 2019-11-08 stsp wattr_off(view->window,
3996 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3997 8d0fe45a 2019-08-12 stsp free(id_str);
3998 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4000 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4001 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4004 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4005 ee41ec32 2018-07-10 stsp prev_id = NULL;
4008 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4009 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4010 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4012 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4013 41605754 2020-11-12 stsp width = 9;
4014 41605754 2020-11-12 stsp wline = wcsdup(L"");
4015 41605754 2020-11-12 stsp if (wline == NULL) {
4016 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4017 41605754 2020-11-12 stsp free(line);
4018 41605754 2020-11-12 stsp return err;
4020 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4021 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4022 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4023 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4024 41605754 2020-11-12 stsp view->window, regmatch);
4025 41605754 2020-11-12 stsp if (err) {
4026 41605754 2020-11-12 stsp free(line);
4027 41605754 2020-11-12 stsp return err;
4029 41605754 2020-11-12 stsp width += 9;
4031 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4032 41605754 2020-11-12 stsp view->ncols - 9, 9);
4033 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4034 41605754 2020-11-12 stsp free(wline);
4035 41605754 2020-11-12 stsp wline = NULL;
4036 41605754 2020-11-12 stsp width += 9;
4039 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4040 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4041 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4042 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4044 826082fe 2020-12-10 stsp free(line);
4045 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4047 1a57306a 2018-09-02 stsp view_vborder(view);
4049 84451b3e 2018-07-10 stsp return NULL;
4052 84451b3e 2018-07-10 stsp static const struct got_error *
4053 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4055 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4056 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4057 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4058 1a76625f 2018-10-22 stsp int errcode;
4060 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4061 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4062 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4064 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4065 1a76625f 2018-10-22 stsp if (errcode)
4066 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4068 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4069 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4070 d68a0a7d 2018-07-10 stsp goto done;
4073 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4074 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4076 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4077 d68a0a7d 2018-07-10 stsp if (line->annotated)
4078 d68a0a7d 2018-07-10 stsp goto done;
4080 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4081 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4082 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4083 84451b3e 2018-07-10 stsp goto done;
4085 84451b3e 2018-07-10 stsp line->annotated = 1;
4087 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4088 1a76625f 2018-10-22 stsp if (errcode)
4089 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4090 84451b3e 2018-07-10 stsp return err;
4093 84451b3e 2018-07-10 stsp static void *
4094 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4096 18430de3 2018-07-10 stsp const struct got_error *err;
4097 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4098 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4099 1a76625f 2018-10-22 stsp int errcode;
4101 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4103 61266923 2020-01-14 stsp return (void *)err;
4105 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4106 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4107 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4108 fc06ba56 2019-08-22 stsp err = NULL;
4110 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4111 1a76625f 2018-10-22 stsp if (errcode)
4112 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4113 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4115 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4116 c9beca56 2018-07-22 stsp ta->repo = NULL;
4117 c9beca56 2018-07-22 stsp *ta->complete = 1;
4119 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4120 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4121 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4123 18430de3 2018-07-10 stsp return (void *)err;
4126 245d91c1 2018-07-12 stsp static struct got_object_id *
4127 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4128 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4130 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4132 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4133 8d0fe45a 2019-08-12 stsp return NULL;
4135 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4136 245d91c1 2018-07-12 stsp if (!line->annotated)
4137 245d91c1 2018-07-12 stsp return NULL;
4139 245d91c1 2018-07-12 stsp return line->id;
4142 b880a918 2018-07-10 stsp static const struct got_error *
4143 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4145 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4148 245d91c1 2018-07-12 stsp if (blame->thread) {
4149 1a76625f 2018-10-22 stsp int errcode;
4150 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4151 1a76625f 2018-10-22 stsp if (errcode)
4152 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4153 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4154 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4155 1a76625f 2018-10-22 stsp if (errcode)
4156 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4157 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4158 1a76625f 2018-10-22 stsp if (errcode)
4159 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4160 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4161 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4162 245d91c1 2018-07-12 stsp err = NULL;
4163 245d91c1 2018-07-12 stsp blame->thread = NULL;
4165 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4166 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4167 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4169 245d91c1 2018-07-12 stsp if (blame->f) {
4170 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4171 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4172 245d91c1 2018-07-12 stsp blame->f = NULL;
4174 57670559 2018-12-23 stsp if (blame->lines) {
4175 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4176 57670559 2018-12-23 stsp free(blame->lines[i].id);
4177 57670559 2018-12-23 stsp free(blame->lines);
4178 57670559 2018-12-23 stsp blame->lines = NULL;
4180 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4181 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4183 245d91c1 2018-07-12 stsp return err;
4186 245d91c1 2018-07-12 stsp static const struct got_error *
4187 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4189 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4190 fc06ba56 2019-08-22 stsp int *done = arg;
4191 fc06ba56 2019-08-22 stsp int errcode;
4193 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4194 fc06ba56 2019-08-22 stsp if (errcode)
4195 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4196 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4198 fc06ba56 2019-08-22 stsp if (*done)
4199 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4201 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4202 fc06ba56 2019-08-22 stsp if (errcode)
4203 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4204 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4206 fc06ba56 2019-08-22 stsp return err;
4209 fc06ba56 2019-08-22 stsp static const struct got_error *
4210 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4212 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4213 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4214 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4215 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4216 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4217 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4218 15a94983 2018-12-23 stsp int obj_type;
4220 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4221 a5388363 2020-12-01 naddy s->path);
4223 15a94983 2018-12-23 stsp return err;
4225 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4227 84451b3e 2018-07-10 stsp goto done;
4229 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4230 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4231 84451b3e 2018-07-10 stsp goto done;
4234 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4236 a70480e0 2018-06-23 stsp goto done;
4237 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4238 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4239 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4240 84451b3e 2018-07-10 stsp goto done;
4242 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4243 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4245 1fddf795 2021-01-20 stsp goto done;
4246 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4247 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4248 84451b3e 2018-07-10 stsp goto done;
4251 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4252 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4253 b02560ec 2019-08-19 stsp blame->nlines--;
4255 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4256 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4257 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4258 84451b3e 2018-07-10 stsp goto done;
4261 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4263 bd24772e 2018-07-11 stsp goto done;
4265 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4266 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4267 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4268 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4269 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4270 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4271 245d91c1 2018-07-12 stsp goto done;
4273 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4275 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4276 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4277 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4278 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4279 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4280 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4281 a5388363 2020-12-01 naddy s->blame_complete = 0;
4283 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4284 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4285 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4286 f5a09613 2020-12-13 naddy s->selected_line = 1;
4291 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4292 27d434c2 2018-09-15 stsp free(obj_id);
4294 245d91c1 2018-07-12 stsp stop_blame(blame);
4295 245d91c1 2018-07-12 stsp return err;
4298 245d91c1 2018-07-12 stsp static const struct got_error *
4299 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4300 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4302 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4303 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4305 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4307 c4843652 2019-08-12 stsp s->path = strdup(path);
4308 c4843652 2019-08-12 stsp if (s->path == NULL)
4309 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4311 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4312 c4843652 2019-08-12 stsp if (err) {
4313 c4843652 2019-08-12 stsp free(s->path);
4314 7cbe629d 2018-08-04 stsp return err;
4317 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4318 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4319 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4320 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4321 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4322 fb2756b9 2018-08-04 stsp s->repo = repo;
4323 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4324 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4326 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4327 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4328 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4329 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4331 11b20872 2019-11-08 stsp return err;
4334 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4335 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4336 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4337 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4338 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4340 a5388363 2020-12-01 naddy return run_blame(view);
4343 e5a0f69f 2018-08-18 stsp static const struct got_error *
4344 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4346 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4347 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4349 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4350 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4352 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4353 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4354 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4355 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4356 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4359 e5a0f69f 2018-08-18 stsp free(s->path);
4360 11b20872 2019-11-08 stsp free_colors(&s->colors);
4362 e5a0f69f 2018-08-18 stsp return err;
4365 7cbe629d 2018-08-04 stsp static const struct got_error *
4366 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4368 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4370 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4371 6c4c42e0 2019-06-24 stsp return NULL;
4374 6c4c42e0 2019-06-24 stsp static const struct got_error *
4375 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4377 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4378 6c4c42e0 2019-06-24 stsp int lineno;
4379 826082fe 2020-12-10 stsp char *line = NULL;
4380 826082fe 2020-12-10 stsp size_t linesize = 0;
4381 826082fe 2020-12-10 stsp ssize_t linelen;
4383 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4384 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4385 6c4c42e0 2019-06-24 stsp return NULL;
4388 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4389 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4390 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4392 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4394 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4395 6c4c42e0 2019-06-24 stsp lineno = 1;
4397 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4400 6c4c42e0 2019-06-24 stsp while (1) {
4401 6c4c42e0 2019-06-24 stsp off_t offset;
4403 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4404 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4405 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4409 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4410 6c4c42e0 2019-06-24 stsp lineno = 1;
4412 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4415 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4416 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4417 6c4c42e0 2019-06-24 stsp free(line);
4418 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4420 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4421 826082fe 2020-12-10 stsp if (linelen != -1 &&
4422 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4423 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4424 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4427 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4432 826082fe 2020-12-10 stsp free(line);
4434 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4435 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4436 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4439 6c4c42e0 2019-06-24 stsp return NULL;
4442 6c4c42e0 2019-06-24 stsp static const struct got_error *
4443 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4445 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4446 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4447 2b380cc8 2018-10-24 stsp int errcode;
4449 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4450 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4451 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4452 2b380cc8 2018-10-24 stsp if (errcode)
4453 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4455 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4458 51fe7530 2019-08-19 stsp if (s->blame_complete)
4459 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4461 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4463 669b5ffa 2018-10-07 stsp view_vborder(view);
4464 e5a0f69f 2018-08-18 stsp return err;
4467 e5a0f69f 2018-08-18 stsp static const struct got_error *
4468 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4470 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4471 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4472 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4473 669b5ffa 2018-10-07 stsp int begin_x = 0;
4475 e5a0f69f 2018-08-18 stsp switch (ch) {
4477 1e37a5c2 2019-05-12 jcs s->done = 1;
4480 1e37a5c2 2019-05-12 jcs case KEY_UP:
4481 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4482 1e37a5c2 2019-05-12 jcs s->selected_line--;
4483 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4484 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4485 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4487 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4488 ea025d1d 2020-02-22 naddy case CTRL('b'):
4489 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4490 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4493 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4494 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4495 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4497 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4500 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4501 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4502 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4503 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4504 1e37a5c2 2019-05-12 jcs s->selected_line++;
4505 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4506 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4507 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4510 1e37a5c2 2019-05-12 jcs case 'p': {
4511 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4512 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4513 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4514 1e37a5c2 2019-05-12 jcs if (id == NULL)
4516 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4517 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4518 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4519 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4520 1e37a5c2 2019-05-12 jcs int obj_type;
4521 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4522 1e37a5c2 2019-05-12 jcs s->repo, id);
4525 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4526 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4527 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4528 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4531 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4532 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4533 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4534 e5a0f69f 2018-08-18 stsp if (err) {
4535 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4536 1e37a5c2 2019-05-12 jcs err = NULL;
4537 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4540 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4542 1e37a5c2 2019-05-12 jcs free(blob_id);
4543 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4544 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4545 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4548 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4550 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4552 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4553 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4555 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4560 1e37a5c2 2019-05-12 jcs s->done = 1;
4561 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4562 1e37a5c2 2019-05-12 jcs s->done = 0;
4563 1e37a5c2 2019-05-12 jcs if (thread_err)
4565 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4566 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4567 a5388363 2020-12-01 naddy err = run_blame(view);
4572 1e37a5c2 2019-05-12 jcs case 'B': {
4573 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4574 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4575 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4577 1e37a5c2 2019-05-12 jcs s->done = 1;
4578 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4579 1e37a5c2 2019-05-12 jcs s->done = 0;
4580 1e37a5c2 2019-05-12 jcs if (thread_err)
4582 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4583 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4584 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4585 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4586 a5388363 2020-12-01 naddy err = run_blame(view);
4591 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4592 1e37a5c2 2019-05-12 jcs case '\r': {
4593 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4594 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4595 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4596 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4597 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4598 1e37a5c2 2019-05-12 jcs if (id == NULL)
4600 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4603 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4604 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4605 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4606 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4607 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4608 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4609 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4610 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4613 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4614 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4615 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4617 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4620 e78dc838 2020-12-04 stsp view->focussed = 0;
4621 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4622 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4623 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4626 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4627 e78dc838 2020-12-04 stsp view->focus_child = 1;
4629 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4634 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4635 ea025d1d 2020-02-22 naddy case CTRL('f'):
4637 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4638 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4639 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4642 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4643 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4644 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4645 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4648 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4649 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4650 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4651 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4653 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4654 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4655 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4657 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4658 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4659 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4660 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4666 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4669 a70480e0 2018-06-23 stsp static const struct got_error *
4670 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4672 a70480e0 2018-06-23 stsp const struct got_error *error;
4673 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4674 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4675 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4676 0587e10c 2020-07-23 stsp char *link_target = NULL;
4677 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4678 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4680 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4682 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4683 a70480e0 2018-06-23 stsp switch (ch) {
4685 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4688 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4689 69069811 2018-08-02 stsp if (repo_path == NULL)
4690 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4694 17020d27 2019-03-07 stsp usage_blame();
4695 a70480e0 2018-06-23 stsp /* NOTREACHED */
4699 a70480e0 2018-06-23 stsp argc -= optind;
4700 a70480e0 2018-06-23 stsp argv += optind;
4702 f135c941 2020-02-20 stsp if (argc != 1)
4703 a70480e0 2018-06-23 stsp usage_blame();
4705 69069811 2018-08-02 stsp if (repo_path == NULL) {
4706 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4707 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4708 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4709 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4710 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4711 c156c7a4 2020-12-18 stsp goto done;
4712 f135c941 2020-02-20 stsp if (worktree)
4713 eb41ed75 2019-02-05 stsp repo_path =
4714 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4716 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4717 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4718 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4719 c156c7a4 2020-12-18 stsp goto done;
4723 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4724 c02c541e 2019-03-29 stsp if (error != NULL)
4725 8e94dd5b 2019-01-04 stsp goto done;
4727 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4728 f135c941 2020-02-20 stsp worktree);
4729 c02c541e 2019-03-29 stsp if (error)
4730 92205607 2019-01-04 stsp goto done;
4732 f135c941 2020-02-20 stsp init_curses();
4734 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4735 51a10b52 2020-12-26 stsp if (error)
4736 51a10b52 2020-12-26 stsp goto done;
4738 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
4739 eb41ed75 2019-02-05 stsp if (error)
4740 69069811 2018-08-02 stsp goto done;
4742 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4743 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4744 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4745 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4746 a70480e0 2018-06-23 stsp if (error != NULL)
4747 66b4983c 2018-06-23 stsp goto done;
4748 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4749 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4751 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4752 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4754 a19e88aa 2018-06-23 stsp if (error != NULL)
4755 8b473291 2019-02-21 stsp goto done;
4757 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4758 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4759 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4760 e1cd8fed 2018-08-01 stsp goto done;
4763 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4764 0587e10c 2020-07-23 stsp commit_id, repo);
4765 7cbe629d 2018-08-04 stsp if (error)
4766 7cbe629d 2018-08-04 stsp goto done;
4768 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4769 78756c87 2020-11-24 stsp commit_id, repo);
4770 0587e10c 2020-07-23 stsp if (error)
4771 0587e10c 2020-07-23 stsp goto done;
4772 12314ad4 2019-08-31 stsp if (worktree) {
4773 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4774 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4775 12314ad4 2019-08-31 stsp worktree = NULL;
4777 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4779 69069811 2018-08-02 stsp free(repo_path);
4780 f135c941 2020-02-20 stsp free(in_repo_path);
4781 0587e10c 2020-07-23 stsp free(link_target);
4782 69069811 2018-08-02 stsp free(cwd);
4783 a70480e0 2018-06-23 stsp free(commit_id);
4784 eb41ed75 2019-02-05 stsp if (worktree)
4785 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4787 a70480e0 2018-06-23 stsp got_repo_close(repo);
4788 51a10b52 2020-12-26 stsp tog_free_refs();
4789 a70480e0 2018-06-23 stsp return error;
4792 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4793 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4795 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4796 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4797 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4798 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4799 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4800 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4801 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4803 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4805 f7d12f7e 2018-08-01 stsp werase(view->window);
4807 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4808 ffd1d5e5 2018-06-23 stsp return NULL;
4810 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4812 ffd1d5e5 2018-06-23 stsp return err;
4813 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4814 a3404814 2018-09-02 stsp wstandout(view->window);
4815 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4817 11b20872 2019-11-08 stsp wattr_on(view->window,
4818 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4819 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4821 11b20872 2019-11-08 stsp wattr_off(view->window,
4822 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4823 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4824 a3404814 2018-09-02 stsp wstandend(view->window);
4825 2550e4c3 2018-07-13 stsp free(wline);
4826 2550e4c3 2018-07-13 stsp wline = NULL;
4827 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4828 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4829 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4830 ffd1d5e5 2018-06-23 stsp return NULL;
4831 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4833 ce52c690 2018-06-23 stsp return err;
4834 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4835 2550e4c3 2018-07-13 stsp free(wline);
4836 2550e4c3 2018-07-13 stsp wline = NULL;
4837 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4838 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4839 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4840 ffd1d5e5 2018-06-23 stsp return NULL;
4841 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4842 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4843 a1eca9bb 2018-06-23 stsp return NULL;
4845 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4846 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4847 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4848 0cf4efb1 2018-09-29 stsp if (view->focussed)
4849 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4850 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4852 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4853 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4854 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4855 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4856 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4857 ffd1d5e5 2018-06-23 stsp return NULL;
4861 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4864 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4865 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4866 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4867 848d6979 2019-08-12 stsp const char *modestr = "";
4868 56e0773d 2019-11-28 stsp mode_t mode;
4870 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4871 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4873 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4874 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4875 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4877 638f9024 2019-05-13 stsp return got_error_from_errno(
4878 230a42bd 2019-05-11 jcs "got_object_id_str");
4880 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4881 63c5ca5d 2019-08-24 stsp modestr = "$";
4882 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4885 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4886 d86d3b18 2020-12-01 naddy te, s->repo);
4887 0d6c6ee3 2020-05-20 stsp if (err) {
4888 0d6c6ee3 2020-05-20 stsp free(id_str);
4889 0d6c6ee3 2020-05-20 stsp return err;
4891 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4892 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4893 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4895 848d6979 2019-08-12 stsp modestr = "@";
4897 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4898 848d6979 2019-08-12 stsp modestr = "/";
4899 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4900 848d6979 2019-08-12 stsp modestr = "*";
4901 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4902 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4903 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4904 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4905 1d13200f 2018-07-12 stsp free(id_str);
4906 0d6c6ee3 2020-05-20 stsp free(link_target);
4907 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4909 1d13200f 2018-07-12 stsp free(id_str);
4910 0d6c6ee3 2020-05-20 stsp free(link_target);
4911 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4912 ffd1d5e5 2018-06-23 stsp if (err) {
4913 ffd1d5e5 2018-06-23 stsp free(line);
4916 d86d3b18 2020-12-01 naddy if (n == s->selected) {
4917 0cf4efb1 2018-09-29 stsp if (view->focussed)
4918 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4919 d86d3b18 2020-12-01 naddy s->selected_entry = te;
4921 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
4923 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4924 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4925 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4927 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4928 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4929 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4930 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4931 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
4932 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4933 ffd1d5e5 2018-06-23 stsp free(line);
4934 2550e4c3 2018-07-13 stsp free(wline);
4935 2550e4c3 2018-07-13 stsp wline = NULL;
4937 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4938 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
4939 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4943 ffd1d5e5 2018-06-23 stsp return err;
4946 ffd1d5e5 2018-06-23 stsp static void
4947 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4949 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4950 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4951 fa86c4bf 2020-11-29 stsp int i = 0;
4953 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4956 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4957 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
4958 fa86c4bf 2020-11-29 stsp if (te == NULL) {
4959 fa86c4bf 2020-11-29 stsp if (!isroot)
4960 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
4963 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
4964 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
4968 fa86c4bf 2020-11-29 stsp static void
4969 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4971 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4972 ffd1d5e5 2018-06-23 stsp int n = 0;
4974 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
4975 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
4976 694d3271 2020-12-01 naddy s->first_displayed_entry);
4978 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
4980 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
4981 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4982 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
4983 768394f3 2019-01-24 stsp if (last) {
4984 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
4985 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
4990 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4991 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4992 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4994 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4995 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4996 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4998 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4999 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5000 56e0773d 2019-11-28 stsp + 1 /* slash */;
5002 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5004 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5005 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5006 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5008 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5009 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5010 d9765a41 2018-06-23 stsp while (pt) {
5011 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5012 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5013 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5014 cb2ebc8a 2018-06-23 stsp goto done;
5016 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5017 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5018 cb2ebc8a 2018-06-23 stsp goto done;
5020 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5023 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5024 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5025 ce52c690 2018-06-23 stsp goto done;
5029 ce52c690 2018-06-23 stsp if (err) {
5030 ce52c690 2018-06-23 stsp free(*path);
5031 ce52c690 2018-06-23 stsp *path = NULL;
5033 ce52c690 2018-06-23 stsp return err;
5036 ce52c690 2018-06-23 stsp static const struct got_error *
5037 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5038 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5039 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5041 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5042 ce52c690 2018-06-23 stsp char *path;
5043 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5045 a0de39f3 2019-08-09 stsp *new_view = NULL;
5047 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5049 ce52c690 2018-06-23 stsp return err;
5051 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5052 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5053 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5054 83ce39e3 2019-08-12 stsp goto done;
5057 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5058 e5a0f69f 2018-08-18 stsp if (err) {
5059 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5060 fc06ba56 2019-08-22 stsp err = NULL;
5061 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5063 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5065 83ce39e3 2019-08-12 stsp free(path);
5066 69efd4c4 2018-07-18 stsp return err;
5069 69efd4c4 2018-07-18 stsp static const struct got_error *
5070 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5071 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5073 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5074 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5075 69efd4c4 2018-07-18 stsp char *path;
5077 a0de39f3 2019-08-09 stsp *new_view = NULL;
5079 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5080 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5081 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5083 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5085 69efd4c4 2018-07-18 stsp return err;
5087 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5090 e5a0f69f 2018-08-18 stsp view_close(log_view);
5092 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5093 cb2ebc8a 2018-06-23 stsp free(path);
5094 cb2ebc8a 2018-06-23 stsp return err;
5097 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5098 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5099 4e97c21c 2020-12-06 stsp struct got_object_id *commit_id, const char *head_ref_name,
5100 4e97c21c 2020-12-06 stsp struct got_repository *repo)
5102 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5103 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5104 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5106 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5108 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5109 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5110 ffd1d5e5 2018-06-23 stsp goto done;
5112 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5113 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5114 ffd1d5e5 2018-06-23 stsp goto done;
5117 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5118 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5119 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5120 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5121 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5122 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5123 6484ec90 2018-09-29 stsp goto done;
5125 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5126 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5127 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5128 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5129 9cd7cbd1 2020-12-07 stsp goto done;
5132 fb2756b9 2018-08-04 stsp s->repo = repo;
5134 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5136 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5137 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5138 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5139 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5141 c0b01bdb 2019-11-08 stsp goto done;
5142 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5143 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5144 c0b01bdb 2019-11-08 stsp if (err) {
5145 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5146 c0b01bdb 2019-11-08 stsp goto done;
5148 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5149 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5150 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5151 c0b01bdb 2019-11-08 stsp if (err) {
5152 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5153 c0b01bdb 2019-11-08 stsp goto done;
5156 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5157 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5158 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5159 11b20872 2019-11-08 stsp if (err) {
5160 11b20872 2019-11-08 stsp free_colors(&s->colors);
5161 11b20872 2019-11-08 stsp goto done;
5164 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5165 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5166 c0b01bdb 2019-11-08 stsp if (err) {
5167 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5168 c0b01bdb 2019-11-08 stsp goto done;
5172 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5173 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5174 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5175 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5176 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5178 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5179 6484ec90 2018-09-29 stsp if (err) {
5180 fb2756b9 2018-08-04 stsp free(s->tree_label);
5181 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5183 ad80ab7b 2018-08-04 stsp return err;
5186 e5a0f69f 2018-08-18 stsp static const struct got_error *
5187 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5189 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5191 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5192 fb2756b9 2018-08-04 stsp free(s->tree_label);
5193 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5194 6484ec90 2018-09-29 stsp free(s->commit_id);
5195 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5196 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5197 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5198 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5199 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5200 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5201 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5202 ad80ab7b 2018-08-04 stsp free(parent);
5205 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5206 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5207 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5208 7c32bd05 2019-06-22 stsp return NULL;
5211 7c32bd05 2019-06-22 stsp static const struct got_error *
5212 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5214 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5216 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5217 7c32bd05 2019-06-22 stsp return NULL;
5220 7c32bd05 2019-06-22 stsp static int
5221 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5223 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5225 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5229 7c32bd05 2019-06-22 stsp static const struct got_error *
5230 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5232 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5233 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5235 7c32bd05 2019-06-22 stsp if (!view->searching) {
5236 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5237 7c32bd05 2019-06-22 stsp return NULL;
5240 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5241 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5242 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5243 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5244 56e0773d 2019-11-28 stsp s->selected_entry);
5246 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5248 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5249 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5251 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5252 56e0773d 2019-11-28 stsp s->selected_entry);
5255 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5256 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5258 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5261 7c32bd05 2019-06-22 stsp while (1) {
5262 56e0773d 2019-11-28 stsp if (te == NULL) {
5263 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5264 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5265 ac66afb8 2019-06-24 stsp return NULL;
5267 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5268 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5270 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5273 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5274 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5275 56e0773d 2019-11-28 stsp s->matched_entry = te;
5279 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5280 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5282 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5285 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5286 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5287 7c32bd05 2019-06-22 stsp s->selected = 0;
5290 e5a0f69f 2018-08-18 stsp return NULL;
5293 ad80ab7b 2018-08-04 stsp static const struct got_error *
5294 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5296 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5297 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5298 e5a0f69f 2018-08-18 stsp char *parent_path;
5300 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5302 e5a0f69f 2018-08-18 stsp return err;
5304 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5305 e5a0f69f 2018-08-18 stsp free(parent_path);
5307 669b5ffa 2018-10-07 stsp view_vborder(view);
5308 e5a0f69f 2018-08-18 stsp return err;
5311 e5a0f69f 2018-08-18 stsp static const struct got_error *
5312 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5314 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5315 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5316 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5317 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5319 e5a0f69f 2018-08-18 stsp switch (ch) {
5321 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5324 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5326 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5327 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5328 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5329 e78dc838 2020-12-04 stsp view->focussed = 0;
5330 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5331 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5332 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5334 1e37a5c2 2019-05-12 jcs return err;
5335 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5336 e78dc838 2020-12-04 stsp view->focus_child = 1;
5338 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5341 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5342 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5343 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5344 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5345 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5346 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5347 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5348 152c1c93 2020-11-29 stsp if (err) {
5349 152c1c93 2020-11-29 stsp view_close(ref_view);
5350 152c1c93 2020-11-29 stsp return err;
5352 e78dc838 2020-12-04 stsp view->focussed = 0;
5353 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5354 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5355 152c1c93 2020-11-29 stsp err = view_close_child(view);
5357 152c1c93 2020-11-29 stsp return err;
5358 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5359 e78dc838 2020-12-04 stsp view->focus_child = 1;
5361 152c1c93 2020-11-29 stsp *new_view = ref_view;
5364 1e37a5c2 2019-05-12 jcs case KEY_UP:
5365 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5366 1e37a5c2 2019-05-12 jcs s->selected--;
5369 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5371 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5372 ea025d1d 2020-02-22 naddy case CTRL('b'):
5373 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5374 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5375 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5376 fa86c4bf 2020-11-29 stsp s->selected = 0;
5378 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5379 fa86c4bf 2020-11-29 stsp s->selected = 0;
5381 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5384 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5385 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5386 1e37a5c2 2019-05-12 jcs s->selected++;
5389 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5391 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5393 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5395 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5396 ea025d1d 2020-02-22 naddy case CTRL('f'):
5397 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5399 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5400 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5401 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5404 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5406 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5408 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5409 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5410 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5411 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5412 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5414 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5415 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5417 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5418 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5419 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5420 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5421 1e37a5c2 2019-05-12 jcs s->selected_entry =
5422 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5423 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5424 1e37a5c2 2019-05-12 jcs free(parent);
5425 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5426 56e0773d 2019-11-28 stsp s->selected_entry))) {
5427 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5428 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5429 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5432 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5433 941e9f74 2019-05-21 stsp if (err) {
5434 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5437 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5438 56e0773d 2019-11-28 stsp s->selected_entry))) {
5439 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5440 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5441 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5443 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5444 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5445 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5448 e78dc838 2020-12-04 stsp view->focussed = 0;
5449 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5450 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5451 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5453 669b5ffa 2018-10-07 stsp return err;
5454 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5455 e78dc838 2020-12-04 stsp view->focus_child = 1;
5457 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5460 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5461 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5462 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5468 ffd1d5e5 2018-06-23 stsp return err;
5471 ffd1d5e5 2018-06-23 stsp __dead static void
5472 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5475 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5476 ffd1d5e5 2018-06-23 stsp getprogname());
5480 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5481 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5483 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5484 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5485 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5486 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5487 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5488 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5489 4e97c21c 2020-12-06 stsp char *label = NULL;
5490 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5491 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5492 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5493 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5495 5221c383 2018-08-01 stsp struct tog_view *view;
5497 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5498 ffd1d5e5 2018-06-23 stsp switch (ch) {
5500 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5503 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5504 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5505 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5509 e99e2d15 2020-11-24 naddy usage_tree();
5510 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5514 ffd1d5e5 2018-06-23 stsp argc -= optind;
5515 ffd1d5e5 2018-06-23 stsp argv += optind;
5517 55cccc34 2020-02-20 stsp if (argc > 1)
5518 e99e2d15 2020-11-24 naddy usage_tree();
5520 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5521 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5522 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5523 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5524 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5525 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5526 c156c7a4 2020-12-18 stsp goto done;
5527 55cccc34 2020-02-20 stsp if (worktree)
5528 52185f70 2019-02-05 stsp repo_path =
5529 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5531 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5532 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5533 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5534 c156c7a4 2020-12-18 stsp goto done;
5538 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5539 c02c541e 2019-03-29 stsp if (error != NULL)
5540 52185f70 2019-02-05 stsp goto done;
5542 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5543 55cccc34 2020-02-20 stsp repo, worktree);
5544 55cccc34 2020-02-20 stsp if (error)
5545 55cccc34 2020-02-20 stsp goto done;
5547 55cccc34 2020-02-20 stsp init_curses();
5549 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5550 c02c541e 2019-03-29 stsp if (error)
5551 52185f70 2019-02-05 stsp goto done;
5553 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
5554 51a10b52 2020-12-26 stsp if (error)
5555 51a10b52 2020-12-26 stsp goto done;
5557 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5558 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5559 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5560 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5561 4e97c21c 2020-12-06 stsp if (error)
5562 4e97c21c 2020-12-06 stsp goto done;
5563 4e97c21c 2020-12-06 stsp head_ref_name = label;
5565 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5566 4e97c21c 2020-12-06 stsp if (error == NULL)
5567 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5568 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5569 4e97c21c 2020-12-06 stsp goto done;
5570 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5571 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5572 4e97c21c 2020-12-06 stsp if (error)
5573 4e97c21c 2020-12-06 stsp goto done;
5576 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5577 55cccc34 2020-02-20 stsp if (error)
5578 ffd1d5e5 2018-06-23 stsp goto done;
5580 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5581 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5582 55cccc34 2020-02-20 stsp if (error)
5583 8b473291 2019-02-21 stsp goto done;
5585 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5586 5221c383 2018-08-01 stsp if (view == NULL) {
5587 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5588 5221c383 2018-08-01 stsp goto done;
5590 4e97c21c 2020-12-06 stsp error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5591 ad80ab7b 2018-08-04 stsp if (error)
5592 ad80ab7b 2018-08-04 stsp goto done;
5593 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5594 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5595 d91faf3b 2020-12-01 naddy in_repo_path);
5596 55cccc34 2020-02-20 stsp if (error)
5597 55cccc34 2020-02-20 stsp goto done;
5600 55cccc34 2020-02-20 stsp if (worktree) {
5601 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5602 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5603 55cccc34 2020-02-20 stsp worktree = NULL;
5605 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5607 52185f70 2019-02-05 stsp free(repo_path);
5608 e4a0e26d 2020-02-20 stsp free(cwd);
5609 ffd1d5e5 2018-06-23 stsp free(commit_id);
5610 4e97c21c 2020-12-06 stsp free(label);
5612 486cd271 2020-12-06 stsp got_ref_close(ref);
5613 ffd1d5e5 2018-06-23 stsp if (commit)
5614 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5616 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5618 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5619 51a10b52 2020-12-26 stsp tog_free_refs();
5620 ffd1d5e5 2018-06-23 stsp return error;
5623 6458efa5 2020-11-24 stsp static const struct got_error *
5624 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5626 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5627 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5629 6458efa5 2020-11-24 stsp s->nrefs = 0;
5630 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5631 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5634 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5635 6458efa5 2020-11-24 stsp if (re == NULL)
5636 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5638 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5639 8924d611 2020-12-26 stsp if (re->ref == NULL)
5640 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5641 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5642 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5645 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5646 6458efa5 2020-11-24 stsp return NULL;
5650 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5652 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5654 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5655 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5656 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5657 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5662 6458efa5 2020-11-24 stsp static const struct got_error *
5663 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5665 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5666 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5668 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5669 6458efa5 2020-11-24 stsp s->repo = repo;
5671 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5672 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5674 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5676 6458efa5 2020-11-24 stsp return err;
5678 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5679 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5680 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5681 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5683 6458efa5 2020-11-24 stsp goto done;
5685 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5686 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5687 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5689 6458efa5 2020-11-24 stsp goto done;
5691 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5692 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5693 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5695 6458efa5 2020-11-24 stsp goto done;
5698 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5699 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5700 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5701 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5702 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5705 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5706 6458efa5 2020-11-24 stsp return err;
5709 6458efa5 2020-11-24 stsp static const struct got_error *
5710 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5712 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5714 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5715 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5717 6458efa5 2020-11-24 stsp return NULL;
5720 6458efa5 2020-11-24 stsp static const struct got_error *
5721 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5722 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5724 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5725 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5726 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5727 6458efa5 2020-11-24 stsp int obj_type;
5729 c42c9805 2020-11-24 stsp *commit_id = NULL;
5731 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5733 6458efa5 2020-11-24 stsp return err;
5735 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5737 6458efa5 2020-11-24 stsp goto done;
5739 6458efa5 2020-11-24 stsp switch (obj_type) {
5740 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5741 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5743 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5744 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5746 6458efa5 2020-11-24 stsp goto done;
5747 c42c9805 2020-11-24 stsp free(obj_id);
5748 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5749 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5751 6458efa5 2020-11-24 stsp goto done;
5752 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5753 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5754 c42c9805 2020-11-24 stsp goto done;
5756 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5757 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5758 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5759 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5760 c42c9805 2020-11-24 stsp goto done;
5764 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5770 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5771 c42c9805 2020-11-24 stsp if (err) {
5772 c42c9805 2020-11-24 stsp free(*commit_id);
5773 c42c9805 2020-11-24 stsp *commit_id = NULL;
5775 c42c9805 2020-11-24 stsp return err;
5778 c42c9805 2020-11-24 stsp static const struct got_error *
5779 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5780 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5782 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5783 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5784 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5786 c42c9805 2020-11-24 stsp *new_view = NULL;
5788 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5789 c42c9805 2020-11-24 stsp if (err) {
5790 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5791 c42c9805 2020-11-24 stsp return err;
5793 c42c9805 2020-11-24 stsp return NULL;
5796 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5797 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5798 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5799 6458efa5 2020-11-24 stsp goto done;
5802 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5803 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5806 6458efa5 2020-11-24 stsp view_close(log_view);
5808 6458efa5 2020-11-24 stsp *new_view = log_view;
5809 c42c9805 2020-11-24 stsp free(commit_id);
5810 6458efa5 2020-11-24 stsp return err;
5813 ce5b7c56 2019-07-09 stsp static void
5814 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5816 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5817 34ba6917 2020-11-30 stsp int i = 0;
5819 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5822 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5823 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5824 34ba6917 2020-11-30 stsp if (re == NULL)
5826 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5827 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5831 34ba6917 2020-11-30 stsp static void
5832 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5834 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5835 6458efa5 2020-11-24 stsp int n = 0;
5837 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5838 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5840 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5842 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5843 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5844 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5845 6458efa5 2020-11-24 stsp if (last) {
5846 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5847 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5852 6458efa5 2020-11-24 stsp static const struct got_error *
5853 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5855 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5857 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5858 6458efa5 2020-11-24 stsp return NULL;
5861 6458efa5 2020-11-24 stsp static int
5862 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5864 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5866 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5870 6458efa5 2020-11-24 stsp static const struct got_error *
5871 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5873 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5874 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5876 6458efa5 2020-11-24 stsp if (!view->searching) {
5877 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5878 6458efa5 2020-11-24 stsp return NULL;
5881 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5882 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5883 6458efa5 2020-11-24 stsp if (s->selected_entry)
5884 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5886 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5887 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5889 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5890 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5892 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5893 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5896 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5897 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5899 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5902 6458efa5 2020-11-24 stsp while (1) {
5903 6458efa5 2020-11-24 stsp if (re == NULL) {
5904 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5905 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5906 6458efa5 2020-11-24 stsp return NULL;
5908 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5909 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5911 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5914 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5915 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5916 6458efa5 2020-11-24 stsp s->matched_entry = re;
5920 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5921 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5923 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5926 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5927 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5928 6458efa5 2020-11-24 stsp s->selected = 0;
5931 6458efa5 2020-11-24 stsp return NULL;
5934 6458efa5 2020-11-24 stsp static const struct got_error *
5935 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5937 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5938 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5939 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5940 6458efa5 2020-11-24 stsp char *line = NULL;
5941 6458efa5 2020-11-24 stsp wchar_t *wline;
5942 6458efa5 2020-11-24 stsp struct tog_color *tc;
5943 6458efa5 2020-11-24 stsp int width, n;
5944 6458efa5 2020-11-24 stsp int limit = view->nlines;
5946 6458efa5 2020-11-24 stsp werase(view->window);
5948 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5950 6458efa5 2020-11-24 stsp if (limit == 0)
5951 6458efa5 2020-11-24 stsp return NULL;
5953 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5955 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5956 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5957 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5959 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5960 6458efa5 2020-11-24 stsp if (err) {
5961 6458efa5 2020-11-24 stsp free(line);
5962 6458efa5 2020-11-24 stsp return err;
5964 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5965 6458efa5 2020-11-24 stsp wstandout(view->window);
5966 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5967 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5968 6458efa5 2020-11-24 stsp wstandend(view->window);
5969 6458efa5 2020-11-24 stsp free(wline);
5970 6458efa5 2020-11-24 stsp wline = NULL;
5971 6458efa5 2020-11-24 stsp free(line);
5972 6458efa5 2020-11-24 stsp line = NULL;
5973 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5974 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5975 6458efa5 2020-11-24 stsp if (--limit <= 0)
5976 6458efa5 2020-11-24 stsp return NULL;
5979 6458efa5 2020-11-24 stsp while (re && limit > 0) {
5980 6458efa5 2020-11-24 stsp char *line = NULL;
5982 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
5983 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
5984 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
5985 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
5986 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5987 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
5988 6458efa5 2020-11-24 stsp struct got_object_id *id;
5989 6458efa5 2020-11-24 stsp char *id_str;
5990 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
5992 6458efa5 2020-11-24 stsp return err;
5993 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
5994 6458efa5 2020-11-24 stsp if (err) {
5996 6458efa5 2020-11-24 stsp return err;
5998 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
5999 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6000 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6002 6458efa5 2020-11-24 stsp free(id_str);
6003 6458efa5 2020-11-24 stsp return err;
6006 6458efa5 2020-11-24 stsp free(id_str);
6008 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6009 6458efa5 2020-11-24 stsp if (line == NULL)
6010 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6013 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6014 6458efa5 2020-11-24 stsp if (err) {
6015 6458efa5 2020-11-24 stsp free(line);
6016 6458efa5 2020-11-24 stsp return err;
6018 6458efa5 2020-11-24 stsp if (n == s->selected) {
6019 6458efa5 2020-11-24 stsp if (view->focussed)
6020 6458efa5 2020-11-24 stsp wstandout(view->window);
6021 6458efa5 2020-11-24 stsp s->selected_entry = re;
6023 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6025 6458efa5 2020-11-24 stsp wattr_on(view->window,
6026 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6027 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6029 6458efa5 2020-11-24 stsp wattr_off(view->window,
6030 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6031 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6032 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6033 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6034 6458efa5 2020-11-24 stsp wstandend(view->window);
6035 6458efa5 2020-11-24 stsp free(line);
6036 6458efa5 2020-11-24 stsp free(wline);
6037 6458efa5 2020-11-24 stsp wline = NULL;
6039 6458efa5 2020-11-24 stsp s->ndisplayed++;
6040 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6043 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6046 6458efa5 2020-11-24 stsp view_vborder(view);
6047 6458efa5 2020-11-24 stsp return err;
6050 6458efa5 2020-11-24 stsp static const struct got_error *
6051 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6052 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6054 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6055 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
6056 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
6057 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6059 c42c9805 2020-11-24 stsp *new_view = NULL;
6061 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6062 c42c9805 2020-11-24 stsp if (err) {
6063 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6064 c42c9805 2020-11-24 stsp return err;
6066 c42c9805 2020-11-24 stsp return NULL;
6069 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6071 c42c9805 2020-11-24 stsp goto done;
6073 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6075 c42c9805 2020-11-24 stsp goto done;
6077 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6078 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6079 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6080 c42c9805 2020-11-24 stsp goto done;
6083 4e97c21c 2020-12-06 stsp err = open_tree_view(tree_view, tree, commit_id,
6084 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6086 c42c9805 2020-11-24 stsp goto done;
6088 c42c9805 2020-11-24 stsp *new_view = tree_view;
6090 c42c9805 2020-11-24 stsp free(commit_id);
6091 c42c9805 2020-11-24 stsp free(tree_id);
6092 c42c9805 2020-11-24 stsp if (err) {
6094 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6096 c42c9805 2020-11-24 stsp return err;
6098 c42c9805 2020-11-24 stsp static const struct got_error *
6099 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6101 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6102 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6103 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6104 34ba6917 2020-11-30 stsp int begin_x = 0;
6106 6458efa5 2020-11-24 stsp switch (ch) {
6108 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6110 6458efa5 2020-11-24 stsp case KEY_ENTER:
6111 6458efa5 2020-11-24 stsp case '\r':
6112 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6114 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6115 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6116 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6118 e78dc838 2020-12-04 stsp view->focussed = 0;
6119 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6120 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6121 6458efa5 2020-11-24 stsp err = view_close_child(view);
6123 6458efa5 2020-11-24 stsp return err;
6124 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6125 e78dc838 2020-12-04 stsp view->focus_child = 1;
6127 6458efa5 2020-11-24 stsp *new_view = log_view;
6130 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6132 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6133 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6134 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6136 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6138 e78dc838 2020-12-04 stsp view->focussed = 0;
6139 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6140 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6141 c42c9805 2020-11-24 stsp err = view_close_child(view);
6143 c42c9805 2020-11-24 stsp return err;
6144 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6145 e78dc838 2020-12-04 stsp view->focus_child = 1;
6147 c42c9805 2020-11-24 stsp *new_view = tree_view;
6150 6458efa5 2020-11-24 stsp case KEY_UP:
6151 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6152 6458efa5 2020-11-24 stsp s->selected--;
6155 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6157 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6158 6458efa5 2020-11-24 stsp case CTRL('b'):
6159 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6160 34ba6917 2020-11-30 stsp s->selected = 0;
6161 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6164 6458efa5 2020-11-24 stsp case KEY_DOWN:
6165 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6166 6458efa5 2020-11-24 stsp s->selected++;
6169 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6170 6458efa5 2020-11-24 stsp /* can't scroll any further */
6172 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6174 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6175 6458efa5 2020-11-24 stsp case CTRL('f'):
6176 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6177 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6178 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6179 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6182 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6184 6458efa5 2020-11-24 stsp case CTRL('l'):
6185 8924d611 2020-12-26 stsp tog_free_refs();
6186 8924d611 2020-12-26 stsp err = tog_load_refs(s->repo);
6189 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6190 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6192 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6193 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6194 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6200 6458efa5 2020-11-24 stsp return err;
6203 6458efa5 2020-11-24 stsp __dead static void
6204 6458efa5 2020-11-24 stsp usage_ref(void)
6207 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6208 6458efa5 2020-11-24 stsp getprogname());
6212 6458efa5 2020-11-24 stsp static const struct got_error *
6213 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6215 6458efa5 2020-11-24 stsp const struct got_error *error;
6216 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6217 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6218 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6220 6458efa5 2020-11-24 stsp struct tog_view *view;
6222 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6223 6458efa5 2020-11-24 stsp switch (ch) {
6225 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6226 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6227 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6231 e99e2d15 2020-11-24 naddy usage_ref();
6232 6458efa5 2020-11-24 stsp /* NOTREACHED */
6236 6458efa5 2020-11-24 stsp argc -= optind;
6237 6458efa5 2020-11-24 stsp argv += optind;
6239 6458efa5 2020-11-24 stsp if (argc > 1)
6240 e99e2d15 2020-11-24 naddy usage_ref();
6242 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6243 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6244 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6245 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6246 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6247 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6248 c156c7a4 2020-12-18 stsp goto done;
6249 6458efa5 2020-11-24 stsp if (worktree)
6250 6458efa5 2020-11-24 stsp repo_path =
6251 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6253 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6254 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6255 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6256 c156c7a4 2020-12-18 stsp goto done;
6260 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6261 6458efa5 2020-11-24 stsp if (error != NULL)
6262 6458efa5 2020-11-24 stsp goto done;
6264 6458efa5 2020-11-24 stsp init_curses();
6266 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6267 51a10b52 2020-12-26 stsp if (error)
6268 51a10b52 2020-12-26 stsp goto done;
6270 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
6271 6458efa5 2020-11-24 stsp if (error)
6272 6458efa5 2020-11-24 stsp goto done;
6274 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6275 6458efa5 2020-11-24 stsp if (view == NULL) {
6276 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6277 6458efa5 2020-11-24 stsp goto done;
6280 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6281 6458efa5 2020-11-24 stsp if (error)
6282 6458efa5 2020-11-24 stsp goto done;
6284 6458efa5 2020-11-24 stsp if (worktree) {
6285 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6286 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6287 6458efa5 2020-11-24 stsp worktree = NULL;
6289 6458efa5 2020-11-24 stsp error = view_loop(view);
6291 6458efa5 2020-11-24 stsp free(repo_path);
6292 6458efa5 2020-11-24 stsp free(cwd);
6294 6458efa5 2020-11-24 stsp got_repo_close(repo);
6295 51a10b52 2020-12-26 stsp tog_free_refs();
6296 6458efa5 2020-11-24 stsp return error;
6299 6458efa5 2020-11-24 stsp static void
6300 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6304 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6305 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6306 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6307 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6309 6879ba42 2020-10-01 naddy fputc('\n', fp);
6312 4ed7e80c 2018-05-20 stsp __dead static void
6313 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6315 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6317 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6318 6879ba42 2020-10-01 naddy getprogname());
6319 6879ba42 2020-10-01 naddy if (hflag) {
6320 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6321 6879ba42 2020-10-01 naddy list_commands(fp);
6323 6879ba42 2020-10-01 naddy exit(status);
6326 c2301be8 2018-04-30 stsp static char **
6327 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6329 ee85c5e8 2020-02-29 stsp va_list ap;
6330 c2301be8 2018-04-30 stsp char **argv;
6333 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6335 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6336 c2301be8 2018-04-30 stsp if (argv == NULL)
6337 c2301be8 2018-04-30 stsp err(1, "calloc");
6338 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6339 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6340 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6341 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6344 ee85c5e8 2020-02-29 stsp va_end(ap);
6345 c2301be8 2018-04-30 stsp return argv;
6349 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6350 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6351 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6352 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6353 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6355 ee85c5e8 2020-02-29 stsp static const struct got_error *
6356 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6358 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6359 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6360 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6361 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6362 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6363 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6364 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6366 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6367 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6368 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6370 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6371 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6372 ee85c5e8 2020-02-29 stsp goto done;
6374 ee85c5e8 2020-02-29 stsp if (worktree)
6375 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6377 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6378 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6379 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6380 ee85c5e8 2020-02-29 stsp goto done;
6383 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6384 ee85c5e8 2020-02-29 stsp if (error != NULL)
6385 ee85c5e8 2020-02-29 stsp goto done;
6387 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6388 ee85c5e8 2020-02-29 stsp repo, worktree);
6389 ee85c5e8 2020-02-29 stsp if (error)
6390 ee85c5e8 2020-02-29 stsp goto done;
6392 87670572 2020-12-26 naddy error = tog_load_refs(repo);
6393 84de9106 2020-12-26 stsp if (error)
6394 84de9106 2020-12-26 stsp goto done;
6395 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6396 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6397 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6398 ee85c5e8 2020-02-29 stsp if (error)
6399 ee85c5e8 2020-02-29 stsp goto done;
6401 ee85c5e8 2020-02-29 stsp if (worktree) {
6402 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6403 ee85c5e8 2020-02-29 stsp worktree = NULL;
6406 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6407 ee85c5e8 2020-02-29 stsp if (error) {
6408 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6409 ee85c5e8 2020-02-29 stsp goto done;
6410 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6411 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6412 6879ba42 2020-10-01 naddy usage(1, 1);
6413 ee85c5e8 2020-02-29 stsp /* not reached */
6416 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6417 ee85c5e8 2020-02-29 stsp repo = NULL;
6419 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6420 ee85c5e8 2020-02-29 stsp if (error)
6421 ee85c5e8 2020-02-29 stsp goto done;
6423 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6425 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6426 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6429 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6430 ee85c5e8 2020-02-29 stsp if (worktree)
6431 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6433 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6434 ee85c5e8 2020-02-29 stsp free(commit_id);
6435 ee85c5e8 2020-02-29 stsp free(cwd);
6436 ee85c5e8 2020-02-29 stsp free(repo_path);
6437 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6438 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6440 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6441 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6442 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6444 87670572 2020-12-26 naddy tog_free_refs();
6445 ee85c5e8 2020-02-29 stsp return error;
6449 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6451 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6452 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6453 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6454 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6455 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6456 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6457 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6460 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6462 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6463 9f7d7167 2018-04-29 stsp switch (ch) {
6465 9f7d7167 2018-04-29 stsp hflag = 1;
6468 53ccebc2 2019-07-30 stsp Vflag = 1;
6471 6879ba42 2020-10-01 naddy usage(hflag, 1);
6472 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6476 9f7d7167 2018-04-29 stsp argc -= optind;
6477 9f7d7167 2018-04-29 stsp argv += optind;
6478 9814e6a3 2020-09-27 naddy optind = 1;
6479 c2301be8 2018-04-30 stsp optreset = 1;
6481 53ccebc2 2019-07-30 stsp if (Vflag) {
6482 53ccebc2 2019-07-30 stsp got_version_print_str();
6483 6879ba42 2020-10-01 naddy return 0;
6486 4010e238 2020-12-04 stsp #ifndef PROFILE
6487 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6488 4010e238 2020-12-04 stsp NULL) == -1)
6489 4010e238 2020-12-04 stsp err(1, "pledge");
6492 c2301be8 2018-04-30 stsp if (argc == 0) {
6493 f29d3e89 2018-06-23 stsp if (hflag)
6494 6879ba42 2020-10-01 naddy usage(hflag, 0);
6495 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6496 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6498 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6502 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6503 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6504 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6505 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6506 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6512 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6513 ee85c5e8 2020-02-29 stsp if (argc != 1)
6514 6879ba42 2020-10-01 naddy usage(0, 1);
6515 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6516 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6518 ee85c5e8 2020-02-29 stsp if (hflag)
6519 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6521 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6525 b46c1e04 2020-09-20 naddy putchar('\n');
6526 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6528 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6529 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6530 a2f4a359 2020-02-28 stsp free(cmd_argv);
6533 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6534 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);