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 60493ae3 2019-06-20 stsp int searching;
506 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
507 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
508 60493ae3 2019-06-20 stsp int search_next_done;
509 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
510 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
511 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
512 1803e47f 2019-06-22 stsp regex_t regex;
513 41605754 2020-11-12 stsp regmatch_t regmatch;
516 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
517 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
518 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
519 78756c87 2020-11-24 stsp struct got_repository *);
520 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
521 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
522 e78dc838 2020-12-04 stsp struct tog_view *, int);
523 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
524 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
525 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
527 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
528 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
529 78756c87 2020-11-24 stsp const char *, const char *, int);
530 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
531 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
532 e78dc838 2020-12-04 stsp struct tog_view *, int);
533 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
534 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
535 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
537 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
538 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
539 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
540 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
541 e78dc838 2020-12-04 stsp struct tog_view *, int);
542 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
543 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
544 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
546 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
547 4e97c21c 2020-12-06 stsp struct got_tree_object *, struct got_object_id *, const char *,
548 4e97c21c 2020-12-06 stsp struct got_repository *);
549 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
550 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
551 e78dc838 2020-12-04 stsp struct tog_view *, int);
552 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
553 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
554 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
556 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
557 6458efa5 2020-11-24 stsp struct got_repository *);
558 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
559 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
560 e78dc838 2020-12-04 stsp struct tog_view *, int);
561 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
562 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
563 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
565 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
566 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
567 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
569 25791caa 2018-10-24 stsp static void
570 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
572 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
575 83baff54 2019-08-12 stsp static void
576 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
578 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
581 61266923 2020-01-14 stsp static void
582 61266923 2020-01-14 stsp tog_sigcont(int signo)
584 61266923 2020-01-14 stsp tog_sigcont_received = 1;
587 e5a0f69f 2018-08-18 stsp static const struct got_error *
588 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
590 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
592 669b5ffa 2018-10-07 stsp if (view->child) {
593 669b5ffa 2018-10-07 stsp view_close(view->child);
594 669b5ffa 2018-10-07 stsp view->child = NULL;
596 e5a0f69f 2018-08-18 stsp if (view->close)
597 e5a0f69f 2018-08-18 stsp err = view->close(view);
598 ea5e7bb5 2018-08-01 stsp if (view->panel)
599 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
600 ea5e7bb5 2018-08-01 stsp if (view->window)
601 ea5e7bb5 2018-08-01 stsp delwin(view->window);
602 ea5e7bb5 2018-08-01 stsp free(view);
603 e5a0f69f 2018-08-18 stsp return err;
606 ea5e7bb5 2018-08-01 stsp static struct tog_view *
607 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
608 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
610 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
612 ea5e7bb5 2018-08-01 stsp if (view == NULL)
613 ea5e7bb5 2018-08-01 stsp return NULL;
615 d6b05b5b 2018-08-04 stsp view->type = type;
616 f7d12f7e 2018-08-01 stsp view->lines = LINES;
617 f7d12f7e 2018-08-01 stsp view->cols = COLS;
618 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
619 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
620 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
621 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
622 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
623 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
624 96a765a8 2018-08-04 stsp view_close(view);
625 ea5e7bb5 2018-08-01 stsp return NULL;
627 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
628 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
629 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
630 96a765a8 2018-08-04 stsp view_close(view);
631 ea5e7bb5 2018-08-01 stsp return NULL;
634 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
635 ea5e7bb5 2018-08-01 stsp return view;
639 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
641 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
643 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
646 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
648 5c60c32a 2018-10-18 stsp static const struct got_error *
649 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
651 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
653 5c60c32a 2018-10-18 stsp view->begin_y = 0;
654 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
655 5c60c32a 2018-10-18 stsp view->nlines = LINES;
656 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
657 5c60c32a 2018-10-18 stsp view->lines = LINES;
658 5c60c32a 2018-10-18 stsp view->cols = COLS;
659 5c60c32a 2018-10-18 stsp err = view_resize(view);
661 5c60c32a 2018-10-18 stsp return err;
663 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
664 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
666 5c60c32a 2018-10-18 stsp return NULL;
669 5c60c32a 2018-10-18 stsp static const struct got_error *
670 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
672 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
674 5c60c32a 2018-10-18 stsp view->begin_x = 0;
675 5c60c32a 2018-10-18 stsp view->begin_y = 0;
676 5c60c32a 2018-10-18 stsp view->nlines = LINES;
677 5c60c32a 2018-10-18 stsp view->ncols = COLS;
678 5c60c32a 2018-10-18 stsp view->lines = LINES;
679 5c60c32a 2018-10-18 stsp view->cols = COLS;
680 5c60c32a 2018-10-18 stsp err = view_resize(view);
682 5c60c32a 2018-10-18 stsp return err;
684 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
685 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
687 5c60c32a 2018-10-18 stsp return NULL;
691 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
693 5c60c32a 2018-10-18 stsp return view->parent == NULL;
696 4d8c2215 2018-08-19 stsp static const struct got_error *
697 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
699 f7d12f7e 2018-08-01 stsp int nlines, ncols;
701 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
702 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
704 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
706 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
707 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
709 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
711 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
712 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
713 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
714 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
715 25791caa 2018-10-24 stsp wclear(view->window);
717 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
718 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
719 0cf4efb1 2018-09-29 stsp view->lines = LINES;
720 0cf4efb1 2018-09-29 stsp view->cols = COLS;
722 6e3e5d9c 2018-10-18 stsp if (view->child) {
723 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
724 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
725 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
726 5c60c32a 2018-10-18 stsp if (view->child->focussed)
727 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
729 5c60c32a 2018-10-18 stsp show_panel(view->panel);
731 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
732 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
736 5c60c32a 2018-10-18 stsp return NULL;
739 669b5ffa 2018-10-07 stsp static const struct got_error *
740 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
742 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
744 669b5ffa 2018-10-07 stsp if (view->child == NULL)
745 669b5ffa 2018-10-07 stsp return NULL;
747 669b5ffa 2018-10-07 stsp err = view_close(view->child);
748 669b5ffa 2018-10-07 stsp view->child = NULL;
749 669b5ffa 2018-10-07 stsp return err;
752 72a9cb46 2020-12-03 stsp static void
753 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
755 669b5ffa 2018-10-07 stsp view->child = child;
756 669b5ffa 2018-10-07 stsp child->parent = view;
760 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
762 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
765 34bc9ec9 2019-02-22 stsp static void
766 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
768 25791caa 2018-10-24 stsp int cols, lines;
769 25791caa 2018-10-24 stsp struct winsize size;
771 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
772 25791caa 2018-10-24 stsp cols = 80; /* Default */
773 25791caa 2018-10-24 stsp lines = 24;
775 25791caa 2018-10-24 stsp cols = size.ws_col;
776 25791caa 2018-10-24 stsp lines = size.ws_row;
778 25791caa 2018-10-24 stsp resize_term(lines, cols);
781 2b49a8ae 2019-06-22 stsp static const struct got_error *
782 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
784 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
785 2b49a8ae 2019-06-22 stsp char pattern[1024];
788 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
789 2b49a8ae 2019-06-22 stsp return NULL;
791 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
792 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
794 2b49a8ae 2019-06-22 stsp nocbreak();
796 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
799 2b49a8ae 2019-06-22 stsp if (ret == ERR)
800 2b49a8ae 2019-06-22 stsp return NULL;
802 2b49a8ae 2019-06-22 stsp if (view->searching) {
803 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
804 2b49a8ae 2019-06-22 stsp view->searching = 0;
807 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
808 7c32bd05 2019-06-22 stsp err = view->search_start(view);
810 7c32bd05 2019-06-22 stsp regfree(&view->regex);
811 7c32bd05 2019-06-22 stsp return err;
813 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
814 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
815 2b49a8ae 2019-06-22 stsp view->search_next(view);
818 2b49a8ae 2019-06-22 stsp return NULL;
821 0cf4efb1 2018-09-29 stsp static const struct got_error *
822 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
823 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
825 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
826 669b5ffa 2018-10-07 stsp struct tog_view *v;
827 1a76625f 2018-10-22 stsp int ch, errcode;
829 e5a0f69f 2018-08-18 stsp *new = NULL;
831 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
832 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
833 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
834 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
836 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
837 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
838 82954512 2020-02-03 stsp if (errcode)
839 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
840 82954512 2020-02-03 stsp "pthread_mutex_unlock");
841 82954512 2020-02-03 stsp pthread_yield();
842 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
843 82954512 2020-02-03 stsp if (errcode)
844 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
845 82954512 2020-02-03 stsp "pthread_mutex_lock");
846 60493ae3 2019-06-20 stsp view->search_next(view);
847 60493ae3 2019-06-20 stsp return NULL;
850 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
851 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
852 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
853 1a76625f 2018-10-22 stsp if (errcode)
854 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
855 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
856 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
857 1a76625f 2018-10-22 stsp if (errcode)
858 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
859 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
861 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
862 25791caa 2018-10-24 stsp tog_resizeterm();
863 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
864 61266923 2020-01-14 stsp tog_sigcont_received = 0;
865 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
866 25791caa 2018-10-24 stsp err = view_resize(v);
868 25791caa 2018-10-24 stsp return err;
869 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
871 25791caa 2018-10-24 stsp return err;
872 cdfcfb03 2020-12-06 stsp if (v->child) {
873 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
875 cdfcfb03 2020-12-06 stsp return err;
876 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
877 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
879 cdfcfb03 2020-12-06 stsp return err;
884 e5a0f69f 2018-08-18 stsp switch (ch) {
888 1e37a5c2 2019-05-12 jcs if (view->child) {
889 e78dc838 2020-12-04 stsp view->focussed = 0;
890 e78dc838 2020-12-04 stsp view->child->focussed = 1;
891 e78dc838 2020-12-04 stsp view->focus_child = 1;
892 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
893 e78dc838 2020-12-04 stsp view->focussed = 0;
894 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
895 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
899 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
900 9970f7fc 2020-12-03 stsp view->dying = 1;
906 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
907 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
909 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
910 e78dc838 2020-12-04 stsp view->focussed = 0;
911 e78dc838 2020-12-04 stsp view->child->focussed = 1;
912 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
914 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
917 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
918 9970f7fc 2020-12-03 stsp KEY_RESIZE);
920 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
921 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
922 e78dc838 2020-12-04 stsp view->focussed = 1;
923 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
925 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
929 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
932 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
935 60493ae3 2019-06-20 stsp if (view->search_start)
936 2b49a8ae 2019-06-22 stsp view_search_start(view);
938 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
942 8f4ed634 2020-03-26 stsp if (view->search_next) {
943 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
944 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
945 60493ae3 2019-06-20 stsp view->search_next_done = 0;
946 60493ae3 2019-06-20 stsp view->search_next(view);
948 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
951 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
955 e5a0f69f 2018-08-18 stsp return err;
959 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
961 0cf4efb1 2018-09-29 stsp PANEL *panel;
962 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
964 669b5ffa 2018-10-07 stsp if (view->parent)
965 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
967 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
968 0cf4efb1 2018-09-29 stsp if (panel == NULL)
971 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
972 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
973 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
977 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
979 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
980 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
982 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
984 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
987 669b5ffa 2018-10-07 stsp return view->focussed;
990 bcbd79e2 2018-08-19 stsp static const struct got_error *
991 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
993 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
994 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
995 fb59748f 2020-12-05 stsp struct tog_view *new_view;
996 fd823528 2018-10-22 stsp int fast_refresh = 10;
997 1a76625f 2018-10-22 stsp int done = 0, errcode;
999 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1000 1a76625f 2018-10-22 stsp if (errcode)
1001 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1003 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1004 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1006 1004088d 2018-09-29 stsp view->focussed = 1;
1007 878940b7 2018-09-29 stsp err = view->show(view);
1009 0cf4efb1 2018-09-29 stsp return err;
1010 0cf4efb1 2018-09-29 stsp update_panels();
1011 0cf4efb1 2018-09-29 stsp doupdate();
1012 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1013 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1014 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1015 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1017 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1020 9970f7fc 2020-12-03 stsp if (view->dying) {
1021 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1023 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1024 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1026 e78dc838 2020-12-04 stsp else if (view->parent)
1027 669b5ffa 2018-10-07 stsp prev = view->parent;
1029 e78dc838 2020-12-04 stsp if (view->parent) {
1030 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1031 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1033 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1035 9970f7fc 2020-12-03 stsp err = view_close(view);
1037 e5a0f69f 2018-08-18 stsp goto done;
1039 e78dc838 2020-12-04 stsp view = NULL;
1040 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1041 e78dc838 2020-12-04 stsp if (v->focussed)
1044 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1045 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1047 e78dc838 2020-12-04 stsp view = prev;
1048 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1049 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1050 e78dc838 2020-12-04 stsp tog_view_list_head);
1052 e78dc838 2020-12-04 stsp if (view) {
1053 e78dc838 2020-12-04 stsp if (view->focus_child) {
1054 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1055 e78dc838 2020-12-04 stsp view = view->child;
1057 e78dc838 2020-12-04 stsp view->focussed = 1;
1061 bcbd79e2 2018-08-19 stsp if (new_view) {
1062 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1063 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1064 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1065 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1067 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1068 86c66b02 2018-10-18 stsp err = view_close(v);
1070 86c66b02 2018-10-18 stsp goto done;
1073 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1074 fed7eaa8 2018-10-24 stsp view = new_view;
1076 669b5ffa 2018-10-07 stsp if (view) {
1077 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1078 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1079 e78dc838 2020-12-04 stsp view = view->child;
1081 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1082 e78dc838 2020-12-04 stsp view = view->parent;
1084 e78dc838 2020-12-04 stsp show_panel(view->panel);
1085 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1086 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1087 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1088 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1090 1a76625f 2018-10-22 stsp goto done;
1092 669b5ffa 2018-10-07 stsp err = view->show(view);
1094 1a76625f 2018-10-22 stsp goto done;
1095 669b5ffa 2018-10-07 stsp if (view->child) {
1096 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1098 1a76625f 2018-10-22 stsp goto done;
1100 1a76625f 2018-10-22 stsp update_panels();
1101 1a76625f 2018-10-22 stsp doupdate();
1105 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1106 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1107 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1108 e5a0f69f 2018-08-18 stsp view_close(view);
1111 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1112 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1113 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1115 e5a0f69f 2018-08-18 stsp return err;
1118 4ed7e80c 2018-05-20 stsp __dead static void
1119 9f7d7167 2018-04-29 stsp usage_log(void)
1122 c70c5802 2018-08-01 stsp fprintf(stderr,
1123 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1124 9f7d7167 2018-04-29 stsp getprogname());
1128 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1129 80ddbec8 2018-04-29 stsp static const struct got_error *
1130 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1132 00dfcb92 2018-06-11 stsp char *vis = NULL;
1133 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1135 963b370f 2018-05-20 stsp *ws = NULL;
1136 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1137 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1138 00dfcb92 2018-06-11 stsp int vislen;
1139 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1140 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1142 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1143 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1145 00dfcb92 2018-06-11 stsp return err;
1146 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1147 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1148 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1149 a7f50699 2018-06-11 stsp goto done;
1153 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1154 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1155 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1156 a7f50699 2018-06-11 stsp goto done;
1159 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1160 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1162 00dfcb92 2018-06-11 stsp free(vis);
1163 963b370f 2018-05-20 stsp if (err) {
1164 963b370f 2018-05-20 stsp free(*ws);
1165 963b370f 2018-05-20 stsp *ws = NULL;
1166 963b370f 2018-05-20 stsp *wlen = 0;
1168 963b370f 2018-05-20 stsp return err;
1171 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1172 963b370f 2018-05-20 stsp static const struct got_error *
1173 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1174 27a741e5 2019-09-11 stsp int col_tab_align)
1176 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1177 963b370f 2018-05-20 stsp int cols = 0;
1178 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1179 963b370f 2018-05-20 stsp size_t wlen;
1182 963b370f 2018-05-20 stsp *wlinep = NULL;
1183 b700b5d6 2018-07-10 stsp *widthp = 0;
1185 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1187 963b370f 2018-05-20 stsp return err;
1189 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1190 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1193 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1194 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1199 27a741e5 2019-09-11 stsp while (i < wlen) {
1200 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1202 27a741e5 2019-09-11 stsp if (width == 0) {
1207 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1208 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1210 27a741e5 2019-09-11 stsp cols += width;
1212 27a741e5 2019-09-11 stsp } else if (width == -1) {
1213 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1214 27a741e5 2019-09-11 stsp width = TABSIZE -
1215 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1217 748d5cab 2020-12-14 naddy width = 1;
1218 748d5cab 2020-12-14 naddy wline[i] = L'.';
1220 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1222 748d5cab 2020-12-14 naddy cols += width;
1225 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1226 963b370f 2018-05-20 stsp goto done;
1229 963b370f 2018-05-20 stsp wline[i] = L'\0';
1230 b700b5d6 2018-07-10 stsp if (widthp)
1231 b700b5d6 2018-07-10 stsp *widthp = cols;
1234 963b370f 2018-05-20 stsp free(wline);
1236 963b370f 2018-05-20 stsp *wlinep = wline;
1237 963b370f 2018-05-20 stsp return err;
1240 8b473291 2019-02-21 stsp static const struct got_error*
1241 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1242 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1244 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1245 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1247 8b473291 2019-02-21 stsp const char *name;
1249 8b473291 2019-02-21 stsp *refs_str = NULL;
1251 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1252 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1253 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1256 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1257 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1259 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1260 8b473291 2019-02-21 stsp name += 5;
1261 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1263 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1264 8b473291 2019-02-21 stsp name += 6;
1265 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1266 8b473291 2019-02-21 stsp name += 8;
1267 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1268 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1271 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1274 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1275 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1276 5d844a1e 2019-08-13 stsp if (err) {
1277 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1278 48cae60d 2020-09-22 stsp free(ref_id);
1281 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1282 5d844a1e 2019-08-13 stsp err = NULL;
1283 5d844a1e 2019-08-13 stsp tag = NULL;
1286 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1287 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1288 48cae60d 2020-09-22 stsp free(ref_id);
1290 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1291 52b5abe1 2019-08-13 stsp if (cmp != 0)
1293 8b473291 2019-02-21 stsp s = *refs_str;
1294 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1295 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1296 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1298 8b473291 2019-02-21 stsp *refs_str = NULL;
1304 8b473291 2019-02-21 stsp return err;
1307 963b370f 2018-05-20 stsp static const struct got_error *
1308 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1309 27a741e5 2019-09-11 stsp int col_tab_align)
1311 e6b8b890 2020-12-29 naddy char *smallerthan;
1313 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1314 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1315 5813d178 2019-03-09 stsp author = smallerthan + 1;
1316 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1317 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1320 5813d178 2019-03-09 stsp static const struct got_error *
1321 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1322 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1323 8fdc79fe 2020-12-01 naddy int author_display_cols)
1325 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1326 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1327 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1328 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1329 5813d178 2019-03-09 stsp char *author = NULL;
1330 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1331 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1332 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1333 bb737323 2018-05-20 stsp int col, limit;
1334 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1335 ccb26ccd 2018-11-05 stsp struct tm tm;
1336 45d799e2 2018-12-23 stsp time_t committer_time;
1337 11b20872 2019-11-08 stsp struct tog_color *tc;
1339 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1340 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1341 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1342 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1343 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1344 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1346 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1347 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1349 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1350 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1352 11b20872 2019-11-08 stsp wattr_on(view->window,
1353 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1354 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1356 11b20872 2019-11-08 stsp wattr_off(view->window,
1357 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1358 27a741e5 2019-09-11 stsp col = limit;
1359 b39d25c7 2018-07-10 stsp if (col > avail)
1360 b39d25c7 2018-07-10 stsp goto done;
1362 6570a66d 2019-11-08 stsp if (avail >= 120) {
1363 6570a66d 2019-11-08 stsp char *id_str;
1364 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1366 6570a66d 2019-11-08 stsp goto done;
1367 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1369 11b20872 2019-11-08 stsp wattr_on(view->window,
1370 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1371 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1373 11b20872 2019-11-08 stsp wattr_off(view->window,
1374 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1375 6570a66d 2019-11-08 stsp free(id_str);
1377 6570a66d 2019-11-08 stsp if (col > avail)
1378 6570a66d 2019-11-08 stsp goto done;
1381 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1382 5813d178 2019-03-09 stsp if (author == NULL) {
1383 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1384 80ddbec8 2018-04-29 stsp goto done;
1386 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1388 bb737323 2018-05-20 stsp goto done;
1389 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1391 11b20872 2019-11-08 stsp wattr_on(view->window,
1392 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1393 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1395 11b20872 2019-11-08 stsp wattr_off(view->window,
1396 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1397 bb737323 2018-05-20 stsp col += author_width;
1398 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1399 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1401 bb737323 2018-05-20 stsp author_width++;
1403 9c2eaf34 2018-05-20 stsp if (col > avail)
1404 9c2eaf34 2018-05-20 stsp goto done;
1406 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1408 6d9fbc00 2018-04-29 stsp goto done;
1409 bb737323 2018-05-20 stsp logmsg = logmsg0;
1410 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1412 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1413 bb737323 2018-05-20 stsp if (newline)
1414 bb737323 2018-05-20 stsp *newline = '\0';
1415 bb737323 2018-05-20 stsp limit = avail - col;
1416 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1418 bb737323 2018-05-20 stsp goto done;
1419 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1420 bb737323 2018-05-20 stsp col += logmsg_width;
1421 27a741e5 2019-09-11 stsp while (col < avail) {
1422 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1426 80ddbec8 2018-04-29 stsp free(logmsg0);
1427 bb737323 2018-05-20 stsp free(wlogmsg);
1428 5813d178 2019-03-09 stsp free(author);
1429 bb737323 2018-05-20 stsp free(wauthor);
1430 80ddbec8 2018-04-29 stsp free(line);
1431 80ddbec8 2018-04-29 stsp return err;
1434 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1435 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1436 899d86c2 2018-05-10 stsp struct got_object_id *id)
1438 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1440 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1441 80ddbec8 2018-04-29 stsp if (entry == NULL)
1442 899d86c2 2018-05-10 stsp return NULL;
1444 899d86c2 2018-05-10 stsp entry->id = id;
1445 99db9666 2018-05-07 stsp entry->commit = commit;
1446 899d86c2 2018-05-10 stsp return entry;
1449 99db9666 2018-05-07 stsp static void
1450 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1452 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1454 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1455 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1456 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1457 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1458 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1459 99db9666 2018-05-07 stsp free(entry);
1462 99db9666 2018-05-07 stsp static void
1463 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1465 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1466 99db9666 2018-05-07 stsp pop_commit(commits);
1469 c4972b91 2018-05-07 stsp static const struct got_error *
1470 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1471 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1473 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1474 13add988 2019-10-15 stsp regmatch_t regmatch;
1475 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1477 13add988 2019-10-15 stsp *have_match = 0;
1479 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1481 13add988 2019-10-15 stsp return err;
1483 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1485 13add988 2019-10-15 stsp goto done;
1487 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1488 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1489 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1490 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1491 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1492 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1493 13add988 2019-10-15 stsp *have_match = 1;
1495 13add988 2019-10-15 stsp free(id_str);
1496 13add988 2019-10-15 stsp free(logmsg);
1497 13add988 2019-10-15 stsp return err;
1500 13add988 2019-10-15 stsp static const struct got_error *
1501 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1503 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1506 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1507 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1508 1a76625f 2018-10-22 stsp * while updating the display.
1511 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1512 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1513 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1514 1a76625f 2018-10-22 stsp int errcode;
1516 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1517 4e0d2870 2020-12-07 naddy NULL, NULL);
1518 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1521 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1524 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1525 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1526 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1530 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1531 1a76625f 2018-10-22 stsp if (errcode) {
1532 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1533 13add988 2019-10-15 stsp "pthread_mutex_lock");
1537 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1538 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1539 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1541 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1542 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1543 7c1452c1 2020-03-26 stsp int have_match;
1544 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1547 7c1452c1 2020-03-26 stsp if (have_match)
1548 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1551 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1552 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1553 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1554 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1557 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1559 9ba79e04 2018-06-11 stsp return err;
1562 2b779855 2020-12-05 naddy static void
1563 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1565 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1566 2b779855 2020-12-05 naddy int ncommits = 0;
1568 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1569 2b779855 2020-12-05 naddy while (entry) {
1570 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1571 2b779855 2020-12-05 naddy s->selected_entry = entry;
1574 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1575 2b779855 2020-12-05 naddy ncommits++;
1579 0553a4e3 2018-04-30 stsp static const struct got_error *
1580 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1582 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1583 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1584 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1585 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1586 60493ae3 2019-06-20 stsp int width;
1587 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1588 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1589 8b473291 2019-02-21 stsp char *refs_str = NULL;
1590 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1591 11b20872 2019-11-08 stsp struct tog_color *tc;
1592 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1594 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1595 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1596 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1597 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1599 ecb28ae0 2018-07-16 stsp return err;
1600 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1601 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1602 d2075bf3 2020-12-25 stsp if (refs) {
1603 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1604 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1606 d2075bf3 2020-12-25 stsp goto done;
1610 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1611 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1613 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed > 0) {
1614 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1615 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1616 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1617 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1618 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1619 8f4ed634 2020-03-26 stsp goto done;
1622 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1624 f9686aa5 2020-03-27 stsp if (view->searching) {
1625 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1626 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1627 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1628 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1629 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1630 f9686aa5 2020-03-27 stsp search_str = "searching...";
1633 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1634 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1635 f9686aa5 2020-03-27 stsp search_str ? search_str :
1636 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1637 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1638 8f4ed634 2020-03-26 stsp goto done;
1642 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1643 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1644 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1645 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1646 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1647 1a76625f 2018-10-22 stsp header = NULL;
1648 1a76625f 2018-10-22 stsp goto done;
1650 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1651 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1652 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1653 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1654 1a76625f 2018-10-22 stsp header = NULL;
1655 1a76625f 2018-10-22 stsp goto done;
1657 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1659 1a76625f 2018-10-22 stsp goto done;
1661 2814baeb 2018-08-01 stsp werase(view->window);
1663 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1664 a3404814 2018-09-02 stsp wstandout(view->window);
1665 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1667 11b20872 2019-11-08 stsp wattr_on(view->window,
1668 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1669 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1671 11b20872 2019-11-08 stsp wattr_off(view->window,
1672 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1673 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1674 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1677 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1678 a3404814 2018-09-02 stsp wstandend(view->window);
1679 ecb28ae0 2018-07-16 stsp free(wline);
1680 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1681 1a76625f 2018-10-22 stsp goto done;
1683 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1684 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1685 5813d178 2019-03-09 stsp ncommits = 0;
1686 5813d178 2019-03-09 stsp while (entry) {
1687 5813d178 2019-03-09 stsp char *author;
1688 5813d178 2019-03-09 stsp wchar_t *wauthor;
1689 5813d178 2019-03-09 stsp int width;
1690 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1692 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1693 5813d178 2019-03-09 stsp if (author == NULL) {
1694 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1695 5813d178 2019-03-09 stsp goto done;
1697 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1698 27a741e5 2019-09-11 stsp date_display_cols);
1699 5813d178 2019-03-09 stsp if (author_cols < width)
1700 5813d178 2019-03-09 stsp author_cols = width;
1701 5813d178 2019-03-09 stsp free(wauthor);
1702 5813d178 2019-03-09 stsp free(author);
1703 7ca04879 2019-10-19 stsp ncommits++;
1704 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1707 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1708 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1709 867c6645 2018-07-10 stsp ncommits = 0;
1710 899d86c2 2018-05-10 stsp while (entry) {
1711 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1713 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1714 2814baeb 2018-08-01 stsp wstandout(view->window);
1715 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1716 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1717 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1718 2814baeb 2018-08-01 stsp wstandend(view->window);
1720 60493ae3 2019-06-20 stsp goto done;
1721 0553a4e3 2018-04-30 stsp ncommits++;
1722 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1723 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1726 1a57306a 2018-09-02 stsp view_vborder(view);
1728 1a76625f 2018-10-22 stsp free(id_str);
1729 8b473291 2019-02-21 stsp free(refs_str);
1730 1a76625f 2018-10-22 stsp free(ncommits_str);
1731 1a76625f 2018-10-22 stsp free(header);
1732 80ddbec8 2018-04-29 stsp return err;
1735 07b55e75 2018-05-10 stsp static void
1736 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1738 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1739 07b55e75 2018-05-10 stsp int nscrolled = 0;
1741 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1742 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1745 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1746 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1747 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1748 07b55e75 2018-05-10 stsp if (entry) {
1749 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1750 07b55e75 2018-05-10 stsp nscrolled++;
1755 aa075928 2018-05-10 stsp static const struct got_error *
1756 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1758 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1759 5e224a3e 2019-02-22 stsp int errcode;
1761 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1763 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1764 ffe38506 2020-12-01 naddy if (ta->log_complete)
1767 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1768 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1769 7aafa0d1 2019-02-22 stsp if (errcode)
1770 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1771 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1774 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1775 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1777 7c1452c1 2020-03-26 stsp if (!wait)
1780 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1781 ffe38506 2020-12-01 naddy show_log_view(view);
1782 7c1452c1 2020-03-26 stsp update_panels();
1783 7c1452c1 2020-03-26 stsp doupdate();
1785 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1786 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1787 82954512 2020-02-03 stsp if (errcode)
1788 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1789 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1791 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1792 ffe38506 2020-12-01 naddy show_log_view(view);
1793 7c1452c1 2020-03-26 stsp update_panels();
1794 7c1452c1 2020-03-26 stsp doupdate();
1797 5e224a3e 2019-02-22 stsp return NULL;
1800 5e224a3e 2019-02-22 stsp static const struct got_error *
1801 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1803 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1804 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1805 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1806 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1808 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1809 5e224a3e 2019-02-22 stsp return NULL;
1811 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1812 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1813 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1815 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1817 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1818 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1820 5e224a3e 2019-02-22 stsp return err;
1824 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1825 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1828 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1830 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1831 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1833 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1834 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1836 dd0a52c1 2018-05-20 stsp return err;
1839 cd0acaa7 2018-05-20 stsp static const struct got_error *
1840 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1841 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1842 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1844 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1845 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1846 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1848 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1849 15a94983 2018-12-23 stsp if (diff_view == NULL)
1850 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1852 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1853 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1854 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1855 e5a0f69f 2018-08-18 stsp if (err == NULL)
1856 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1857 cd0acaa7 2018-05-20 stsp return err;
1860 80ddbec8 2018-04-29 stsp static const struct got_error *
1861 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1862 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1864 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1866 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1867 941e9f74 2019-05-21 stsp if (parent == NULL)
1868 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1870 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1871 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1872 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1873 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1874 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1875 941e9f74 2019-05-21 stsp s->tree = subtree;
1876 941e9f74 2019-05-21 stsp s->selected = 0;
1877 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1878 941e9f74 2019-05-21 stsp return NULL;
1881 941e9f74 2019-05-21 stsp static const struct got_error *
1882 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1883 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1885 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1886 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1887 941e9f74 2019-05-21 stsp const char *p;
1888 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1890 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1892 941e9f74 2019-05-21 stsp while (*p) {
1893 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1894 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1895 56e0773d 2019-11-28 stsp char *te_name;
1897 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1900 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1901 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1902 941e9f74 2019-05-21 stsp if (slash == NULL)
1903 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1905 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1906 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1907 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1910 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1911 56e0773d 2019-11-28 stsp if (te == NULL) {
1912 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1913 56e0773d 2019-11-28 stsp free(te_name);
1916 56e0773d 2019-11-28 stsp free(te_name);
1917 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1919 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1920 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1922 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1923 941e9f74 2019-05-21 stsp if (slash)
1924 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1926 941e9f74 2019-05-21 stsp subpath = strdup(path);
1927 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1928 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1932 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1937 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1938 941e9f74 2019-05-21 stsp free(tree_id);
1942 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1943 941e9f74 2019-05-21 stsp if (err) {
1944 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1947 941e9f74 2019-05-21 stsp if (slash == NULL)
1949 941e9f74 2019-05-21 stsp free(subpath);
1950 941e9f74 2019-05-21 stsp subpath = NULL;
1951 941e9f74 2019-05-21 stsp p = slash;
1954 941e9f74 2019-05-21 stsp free(subpath);
1955 1a76625f 2018-10-22 stsp return err;
1958 61266923 2020-01-14 stsp static const struct got_error *
1959 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1960 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1961 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
1963 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1964 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1965 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1966 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1968 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1969 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1971 55cccc34 2020-02-20 stsp return err;
1973 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1974 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1975 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1977 4e97c21c 2020-12-06 stsp err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1978 55cccc34 2020-02-20 stsp if (err) {
1979 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1980 55cccc34 2020-02-20 stsp return err;
1982 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1984 55cccc34 2020-02-20 stsp *new_view = tree_view;
1986 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1987 55cccc34 2020-02-20 stsp return NULL;
1989 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1992 55cccc34 2020-02-20 stsp static const struct got_error *
1993 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1995 61266923 2020-01-14 stsp sigset_t sigset;
1996 61266923 2020-01-14 stsp int errcode;
1998 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1999 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2001 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
2002 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2003 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2004 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2005 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2007 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2008 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2009 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2011 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2012 61266923 2020-01-14 stsp if (errcode)
2013 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2015 61266923 2020-01-14 stsp return NULL;
2018 1a76625f 2018-10-22 stsp static void *
2019 1a76625f 2018-10-22 stsp log_thread(void *arg)
2021 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2022 1a76625f 2018-10-22 stsp int errcode = 0;
2023 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2024 1a76625f 2018-10-22 stsp int done = 0;
2026 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2028 61266923 2020-01-14 stsp return (void *)err;
2030 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2031 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2032 1a76625f 2018-10-22 stsp if (err) {
2033 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2034 1a76625f 2018-10-22 stsp return (void *)err;
2035 1a76625f 2018-10-22 stsp err = NULL;
2037 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
2038 1a76625f 2018-10-22 stsp a->commits_needed--;
2040 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2041 3abe8080 2019-04-10 stsp if (errcode) {
2042 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2043 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2045 3abe8080 2019-04-10 stsp } else if (*a->quit)
2047 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2048 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2049 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2050 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2053 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2054 7c1452c1 2020-03-26 stsp if (errcode) {
2055 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2056 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2057 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2062 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2064 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2065 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2066 7c1452c1 2020-03-26 stsp &tog_mutex);
2067 7c1452c1 2020-03-26 stsp if (errcode)
2068 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2069 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2070 21355643 2020-12-06 stsp if (*a->quit)
2075 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2076 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2077 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2078 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2080 3abe8080 2019-04-10 stsp a->log_complete = 1;
2081 1a76625f 2018-10-22 stsp return (void *)err;
2084 1a76625f 2018-10-22 stsp static const struct got_error *
2085 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2087 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2088 1a76625f 2018-10-22 stsp int errcode;
2090 1a76625f 2018-10-22 stsp if (s->thread) {
2091 1a76625f 2018-10-22 stsp s->quit = 1;
2092 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2093 1a76625f 2018-10-22 stsp if (errcode)
2094 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2095 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2096 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2097 1a76625f 2018-10-22 stsp if (errcode)
2098 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2099 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2100 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2101 1a76625f 2018-10-22 stsp if (errcode)
2102 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2103 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2104 1a76625f 2018-10-22 stsp if (errcode)
2105 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2106 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2107 1a76625f 2018-10-22 stsp s->thread = NULL;
2110 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2111 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2112 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2115 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2116 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2117 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2120 9343a5fb 2018-06-23 stsp return err;
2123 9343a5fb 2018-06-23 stsp static const struct got_error *
2124 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2126 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2127 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2128 276b94a1 2020-11-13 naddy int errcode;
2130 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2132 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2133 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2134 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2136 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
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 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2141 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2142 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2143 1a76625f 2018-10-22 stsp free(s->start_id);
2144 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2145 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2146 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2147 1a76625f 2018-10-22 stsp return err;
2150 1a76625f 2018-10-22 stsp static const struct got_error *
2151 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2153 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2155 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2156 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2157 60493ae3 2019-06-20 stsp return NULL;
2160 60493ae3 2019-06-20 stsp static const struct got_error *
2161 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2163 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2164 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2165 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2167 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2168 f9686aa5 2020-03-27 stsp show_log_view(view);
2169 f9686aa5 2020-03-27 stsp update_panels();
2170 f9686aa5 2020-03-27 stsp doupdate();
2172 96e2b566 2019-07-08 stsp if (s->search_entry) {
2173 13add988 2019-10-15 stsp int errcode, ch;
2174 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2175 13add988 2019-10-15 stsp if (errcode)
2176 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2177 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2178 13add988 2019-10-15 stsp ch = wgetch(view->window);
2179 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2180 13add988 2019-10-15 stsp if (errcode)
2181 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2182 13add988 2019-10-15 stsp "pthread_mutex_lock");
2183 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2184 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2185 678cbce5 2019-07-28 stsp return NULL;
2187 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2188 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2190 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2191 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2192 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2193 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2194 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2196 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2197 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2199 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2200 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2202 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2205 60493ae3 2019-06-20 stsp while (1) {
2206 13add988 2019-10-15 stsp int have_match = 0;
2208 60493ae3 2019-06-20 stsp if (entry == NULL) {
2209 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2210 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2211 f9967bca 2020-03-27 stsp view->search_next_done =
2212 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2213 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2214 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2215 f801134a 2019-06-25 stsp return NULL;
2218 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2219 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2220 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2222 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2223 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2226 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2227 13add988 2019-10-15 stsp &view->regex);
2230 13add988 2019-10-15 stsp if (have_match) {
2231 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2232 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2236 96e2b566 2019-07-08 stsp s->search_entry = entry;
2237 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2238 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2240 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2243 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2244 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2245 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2246 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2248 60493ae3 2019-06-20 stsp return err;
2251 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2252 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2254 60493ae3 2019-06-20 stsp return err;
2259 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2261 60493ae3 2019-06-20 stsp return NULL;
2264 60493ae3 2019-06-20 stsp static const struct got_error *
2265 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2266 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2267 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2269 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2270 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2271 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2272 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2273 1a76625f 2018-10-22 stsp int errcode;
2275 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2276 f135c941 2020-02-20 stsp free(s->in_repo_path);
2277 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2278 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2279 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2282 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2283 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2284 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2286 fb2756b9 2018-08-04 stsp s->repo = repo;
2287 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2288 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2289 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2290 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2291 9cd7cbd1 2020-12-07 stsp goto done;
2294 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2295 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2296 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2297 5036bf37 2018-09-24 stsp goto done;
2299 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2301 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2302 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2303 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2304 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2306 11b20872 2019-11-08 stsp goto done;
2307 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2308 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2309 11b20872 2019-11-08 stsp if (err) {
2310 11b20872 2019-11-08 stsp free_colors(&s->colors);
2311 11b20872 2019-11-08 stsp goto done;
2313 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2314 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2315 11b20872 2019-11-08 stsp if (err) {
2316 11b20872 2019-11-08 stsp free_colors(&s->colors);
2317 11b20872 2019-11-08 stsp goto done;
2321 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2322 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2323 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2324 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2325 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2327 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2329 1a76625f 2018-10-22 stsp goto done;
2330 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2331 b672a97a 2020-01-27 stsp !s->log_branches);
2333 1a76625f 2018-10-22 stsp goto done;
2334 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2335 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2337 c5b78334 2020-01-12 stsp goto done;
2339 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2340 1a76625f 2018-10-22 stsp if (errcode) {
2341 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2342 1a76625f 2018-10-22 stsp goto done;
2344 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2345 7c1452c1 2020-03-26 stsp if (errcode) {
2346 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2347 7c1452c1 2020-03-26 stsp goto done;
2350 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2351 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2352 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2353 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2354 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2355 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2356 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2357 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2358 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2359 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2360 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2361 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2362 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2365 1a76625f 2018-10-22 stsp close_log_view(view);
2366 ba4f502b 2018-08-04 stsp return err;
2369 e5a0f69f 2018-08-18 stsp static const struct got_error *
2370 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2372 f2f6d207 2020-11-24 stsp const struct got_error *err;
2373 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2375 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2376 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2377 2b380cc8 2018-10-24 stsp &s->thread_args);
2378 2b380cc8 2018-10-24 stsp if (errcode)
2379 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2380 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2381 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2383 f2f6d207 2020-11-24 stsp return err;
2387 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2390 e5a0f69f 2018-08-18 stsp static const struct got_error *
2391 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2393 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2394 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2395 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2396 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2397 669b5ffa 2018-10-07 stsp int begin_x = 0;
2399 e5a0f69f 2018-08-18 stsp switch (ch) {
2401 1e37a5c2 2019-05-12 jcs s->quit = 1;
2404 1e37a5c2 2019-05-12 jcs case KEY_UP:
2407 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2409 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2410 1e37a5c2 2019-05-12 jcs s->selected--;
2412 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2413 2b779855 2020-12-05 naddy select_commit(s);
2415 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2416 a4292ac5 2019-05-12 jcs case CTRL('b'):
2417 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2419 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2420 1e37a5c2 2019-05-12 jcs s->selected = 0;
2422 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2423 2b779855 2020-12-05 naddy select_commit(s);
2426 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2429 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2431 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2432 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2433 1e37a5c2 2019-05-12 jcs s->selected++;
2435 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2439 2b779855 2020-12-05 naddy select_commit(s);
2441 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2442 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2443 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2444 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2445 1e37a5c2 2019-05-12 jcs if (first == NULL)
2447 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2450 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2451 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2452 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2453 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2454 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2455 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2457 2b779855 2020-12-05 naddy select_commit(s);
2460 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2461 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2462 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2463 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2464 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2465 2b779855 2020-12-05 naddy select_commit(s);
2466 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2467 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2468 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2469 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2470 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2473 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2476 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2478 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2479 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2480 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2481 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2482 78756c87 2020-11-24 stsp view, s->repo);
2485 e78dc838 2020-12-04 stsp view->focussed = 0;
2486 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2487 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2488 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2490 1e37a5c2 2019-05-12 jcs return err;
2491 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2492 e78dc838 2020-12-04 stsp view->focus_child = 1;
2494 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2497 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2499 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2500 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2501 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2502 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2506 e78dc838 2020-12-04 stsp view->focussed = 0;
2507 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2508 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2509 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2511 1e37a5c2 2019-05-12 jcs return err;
2512 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2513 e78dc838 2020-12-04 stsp view->focus_child = 1;
2515 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2517 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2518 21355643 2020-12-06 stsp case CTRL('l'):
2520 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2521 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2523 21355643 2020-12-06 stsp err = stop_log_thread(s);
2525 74cfe85e 2020-10-20 stsp return err;
2526 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2527 21355643 2020-12-06 stsp char *parent_path;
2528 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2530 21355643 2020-12-06 stsp return err;
2531 21355643 2020-12-06 stsp free(s->in_repo_path);
2532 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2533 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2534 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2535 21355643 2020-12-06 stsp struct got_object_id *start_id;
2536 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2537 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2538 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2540 21355643 2020-12-06 stsp return err;
2541 21355643 2020-12-06 stsp free(s->start_id);
2542 21355643 2020-12-06 stsp s->start_id = start_id;
2543 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2544 21355643 2020-12-06 stsp } else /* 'B' */
2545 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2547 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2548 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2550 21355643 2020-12-06 stsp return err;
2551 51a10b52 2020-12-26 stsp tog_free_refs();
2552 51a10b52 2020-12-26 stsp err = tog_load_refs(s->repo);
2554 ca51c541 2020-12-07 stsp return err;
2555 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2556 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2558 d01904d4 2019-06-25 stsp return err;
2559 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2560 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2562 b672a97a 2020-01-27 stsp return err;
2563 21355643 2020-12-06 stsp free_commits(&s->commits);
2564 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2565 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2566 21355643 2020-12-06 stsp s->selected_entry = NULL;
2567 21355643 2020-12-06 stsp s->selected = 0;
2568 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2569 21355643 2020-12-06 stsp s->quit = 0;
2570 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2573 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2574 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2575 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2576 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2577 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2578 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2579 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2580 6458efa5 2020-11-24 stsp if (err) {
2581 6458efa5 2020-11-24 stsp view_close(ref_view);
2582 6458efa5 2020-11-24 stsp return err;
2584 e78dc838 2020-12-04 stsp view->focussed = 0;
2585 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2586 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2587 6458efa5 2020-11-24 stsp err = view_close_child(view);
2589 6458efa5 2020-11-24 stsp return err;
2590 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2591 e78dc838 2020-12-04 stsp view->focus_child = 1;
2593 6458efa5 2020-11-24 stsp *new_view = ref_view;
2599 80ddbec8 2018-04-29 stsp return err;
2602 4ed7e80c 2018-05-20 stsp static const struct got_error *
2603 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2605 c2db6724 2019-01-04 stsp const struct got_error *error;
2607 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2608 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2609 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2611 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2612 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2614 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2615 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2617 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2618 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2620 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2621 c2db6724 2019-01-04 stsp if (error != NULL)
2622 c2db6724 2019-01-04 stsp return error;
2624 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2625 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2627 c2db6724 2019-01-04 stsp return NULL;
2630 a915003a 2019-02-05 stsp static void
2631 a915003a 2019-02-05 stsp init_curses(void)
2633 a915003a 2019-02-05 stsp initscr();
2635 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2638 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2639 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2640 a915003a 2019-02-05 stsp curs_set(0);
2641 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2642 6d17833f 2019-11-08 stsp start_color();
2643 6d17833f 2019-11-08 stsp use_default_colors();
2645 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2646 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2647 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2650 c2db6724 2019-01-04 stsp static const struct got_error *
2651 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2652 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2654 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2656 f135c941 2020-02-20 stsp if (argc == 0) {
2657 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2658 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2659 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2660 f135c941 2020-02-20 stsp return NULL;
2663 f135c941 2020-02-20 stsp if (worktree) {
2664 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2667 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2669 f135c941 2020-02-20 stsp return err;
2670 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2671 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2672 bfd61697 2020-11-14 stsp p) == -1) {
2673 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2674 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2678 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2680 f135c941 2020-02-20 stsp return err;
2683 f135c941 2020-02-20 stsp static const struct got_error *
2684 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2686 80ddbec8 2018-04-29 stsp const struct got_error *error;
2687 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2688 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2689 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2690 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2691 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2692 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2693 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2694 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2695 04cc582a 2018-08-01 stsp struct tog_view *view;
2697 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2698 80ddbec8 2018-04-29 stsp switch (ch) {
2700 b672a97a 2020-01-27 stsp log_branches = 1;
2703 80ddbec8 2018-04-29 stsp start_commit = optarg;
2706 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2707 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2708 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2712 17020d27 2019-03-07 stsp usage_log();
2713 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2717 80ddbec8 2018-04-29 stsp argc -= optind;
2718 80ddbec8 2018-04-29 stsp argv += optind;
2720 f135c941 2020-02-20 stsp if (argc > 1)
2721 f135c941 2020-02-20 stsp usage_log();
2723 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2724 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2725 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2726 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2727 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2728 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2729 c156c7a4 2020-12-18 stsp goto done;
2730 a1fbf39a 2019-08-11 stsp if (worktree)
2731 6962eb72 2020-02-20 stsp repo_path =
2732 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2734 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2735 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2736 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2737 c156c7a4 2020-12-18 stsp goto done;
2741 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2742 80ddbec8 2018-04-29 stsp if (error != NULL)
2743 ecb28ae0 2018-07-16 stsp goto done;
2745 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2746 f135c941 2020-02-20 stsp repo, worktree);
2747 f135c941 2020-02-20 stsp if (error)
2748 f135c941 2020-02-20 stsp goto done;
2750 f135c941 2020-02-20 stsp init_curses();
2752 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2753 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2754 c02c541e 2019-03-29 stsp if (error)
2755 c02c541e 2019-03-29 stsp goto done;
2757 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2758 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2759 87670572 2020-12-26 naddy error = tog_load_refs(repo);
2760 87670572 2020-12-26 naddy if (error)
2761 87670572 2020-12-26 naddy goto done;
2764 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2765 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2766 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2767 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2768 d8f38dc4 2020-12-05 stsp if (error)
2769 d8f38dc4 2020-12-05 stsp goto done;
2770 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2772 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2773 d8f38dc4 2020-12-05 stsp if (error == NULL)
2774 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2775 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2776 d8f38dc4 2020-12-05 stsp goto done;
2777 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2778 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2779 d8f38dc4 2020-12-05 stsp if (error)
2780 d8f38dc4 2020-12-05 stsp goto done;
2783 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2784 04cc582a 2018-08-01 stsp if (view == NULL) {
2785 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2786 04cc582a 2018-08-01 stsp goto done;
2788 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2789 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2790 ba4f502b 2018-08-04 stsp if (error)
2791 ba4f502b 2018-08-04 stsp goto done;
2792 2fc00ff4 2019-08-31 stsp if (worktree) {
2793 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2794 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2795 2fc00ff4 2019-08-31 stsp worktree = NULL;
2797 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2799 f135c941 2020-02-20 stsp free(in_repo_path);
2800 ecb28ae0 2018-07-16 stsp free(repo_path);
2801 ecb28ae0 2018-07-16 stsp free(cwd);
2802 899d86c2 2018-05-10 stsp free(start_id);
2803 d8f38dc4 2020-12-05 stsp free(label);
2805 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2807 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2808 ec142235 2019-03-07 stsp if (worktree)
2809 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2810 51a10b52 2020-12-26 stsp tog_free_refs();
2811 80ddbec8 2018-04-29 stsp return error;
2814 4ed7e80c 2018-05-20 stsp __dead static void
2815 9f7d7167 2018-04-29 stsp usage_diff(void)
2818 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2819 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2823 6d17833f 2019-11-08 stsp static int
2824 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2825 41605754 2020-11-12 stsp regmatch_t *regmatch)
2827 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2830 f26dddb7 2019-11-08 stsp struct tog_color *
2831 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2833 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2835 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2836 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2837 6d17833f 2019-11-08 stsp return tc;
2840 6d17833f 2019-11-08 stsp return NULL;
2843 4ed7e80c 2018-05-20 stsp static const struct got_error *
2844 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2845 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2847 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2848 41605754 2020-11-12 stsp wchar_t *wline;
2849 41605754 2020-11-12 stsp int width;
2852 41605754 2020-11-12 stsp *wtotal = 0;
2854 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2855 41605754 2020-11-12 stsp if (s == NULL)
2856 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2858 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2859 41605754 2020-11-12 stsp if (err) {
2861 41605754 2020-11-12 stsp return err;
2863 41605754 2020-11-12 stsp waddwstr(window, wline);
2864 41605754 2020-11-12 stsp free(wline);
2866 41605754 2020-11-12 stsp wlimit -= width;
2867 41605754 2020-11-12 stsp *wtotal += width;
2869 41605754 2020-11-12 stsp if (wlimit > 0) {
2870 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2871 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2872 41605754 2020-11-12 stsp if (s == NULL) {
2873 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2875 41605754 2020-11-12 stsp return err;
2877 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2878 41605754 2020-11-12 stsp if (err) {
2880 41605754 2020-11-12 stsp return err;
2882 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2883 41605754 2020-11-12 stsp waddwstr(window, wline);
2884 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2885 41605754 2020-11-12 stsp free(wline);
2887 41605754 2020-11-12 stsp wlimit -= width;
2888 41605754 2020-11-12 stsp *wtotal += width;
2891 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2892 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2893 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2895 41605754 2020-11-12 stsp return err;
2896 41605754 2020-11-12 stsp waddwstr(window, wline);
2897 41605754 2020-11-12 stsp free(wline);
2898 41605754 2020-11-12 stsp *wtotal += width;
2901 41605754 2020-11-12 stsp return NULL;
2904 41605754 2020-11-12 stsp static const struct got_error *
2905 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2907 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2908 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2909 61e69b96 2018-05-20 stsp const struct got_error *err;
2910 fe621944 2020-11-10 stsp int nprinted = 0;
2911 b304db33 2018-05-20 stsp char *line;
2912 826082fe 2020-12-10 stsp size_t linesize = 0;
2913 826082fe 2020-12-10 stsp ssize_t linelen;
2914 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2915 61e69b96 2018-05-20 stsp wchar_t *wline;
2916 e0b650dd 2018-05-20 stsp int width;
2917 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2918 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2919 fe621944 2020-11-10 stsp off_t line_offset;
2921 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2922 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2923 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2925 f7d12f7e 2018-08-01 stsp werase(view->window);
2927 a3404814 2018-09-02 stsp if (header) {
2928 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2929 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2930 135a2da0 2020-11-11 stsp header) == -1)
2931 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2932 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2933 135a2da0 2020-11-11 stsp free(line);
2935 a3404814 2018-09-02 stsp return err;
2937 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2938 a3404814 2018-09-02 stsp wstandout(view->window);
2939 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2940 e54cc94a 2020-11-11 stsp free(wline);
2941 e54cc94a 2020-11-11 stsp wline = NULL;
2942 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2943 a3404814 2018-09-02 stsp wstandend(view->window);
2944 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2945 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2947 a3404814 2018-09-02 stsp if (max_lines <= 1)
2948 a3404814 2018-09-02 stsp return NULL;
2949 a3404814 2018-09-02 stsp max_lines--;
2952 89f1a395 2020-12-01 naddy s->eof = 0;
2953 826082fe 2020-12-10 stsp line = NULL;
2954 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2955 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
2956 826082fe 2020-12-10 stsp if (linelen == -1) {
2957 826082fe 2020-12-10 stsp if (feof(s->f)) {
2958 826082fe 2020-12-10 stsp s->eof = 1;
2961 826082fe 2020-12-10 stsp free(line);
2962 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
2965 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
2967 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2968 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2969 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
2970 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2971 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2972 41605754 2020-11-12 stsp view->window, regmatch);
2973 41605754 2020-11-12 stsp if (err) {
2974 41605754 2020-11-12 stsp free(line);
2975 41605754 2020-11-12 stsp return err;
2978 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2979 41605754 2020-11-12 stsp if (err) {
2980 41605754 2020-11-12 stsp free(line);
2981 41605754 2020-11-12 stsp return err;
2983 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2984 41605754 2020-11-12 stsp free(wline);
2985 41605754 2020-11-12 stsp wline = NULL;
2988 6d17833f 2019-11-08 stsp wattr_off(view->window,
2989 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2990 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2991 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2992 fe621944 2020-11-10 stsp nprinted++;
2994 826082fe 2020-12-10 stsp free(line);
2995 fe621944 2020-11-10 stsp if (nprinted >= 1)
2996 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
2997 89f1a395 2020-12-01 naddy (nprinted - 1);
2999 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3001 1a57306a 2018-09-02 stsp view_vborder(view);
3003 89f1a395 2020-12-01 naddy if (s->eof) {
3004 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3005 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3006 c3e9aa98 2019-05-13 jcs nprinted++;
3009 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3011 c3e9aa98 2019-05-13 jcs return err;
3014 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3015 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3016 e54cc94a 2020-11-11 stsp free(wline);
3017 e54cc94a 2020-11-11 stsp wline = NULL;
3018 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3021 26ed57b2 2018-05-19 stsp return NULL;
3024 abd2672a 2018-12-23 stsp static char *
3025 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3027 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3028 09867e48 2019-08-13 stsp char *p, *s;
3030 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3031 09867e48 2019-08-13 stsp if (tm == NULL)
3032 09867e48 2019-08-13 stsp return NULL;
3033 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3034 09867e48 2019-08-13 stsp if (s == NULL)
3035 09867e48 2019-08-13 stsp return NULL;
3036 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3038 abd2672a 2018-12-23 stsp *p = '\0';
3042 4ed7e80c 2018-05-20 stsp static const struct got_error *
3043 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3044 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3046 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3047 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3048 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3049 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3051 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3052 0208f208 2020-05-05 stsp if (qid != NULL) {
3053 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3054 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3057 0208f208 2020-05-05 stsp return err;
3059 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3060 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3064 0208f208 2020-05-05 stsp if (tree_id1) {
3065 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3067 0208f208 2020-05-05 stsp goto done;
3070 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3071 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3073 0208f208 2020-05-05 stsp goto done;
3075 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3076 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3078 0208f208 2020-05-05 stsp if (tree1)
3079 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3080 0208f208 2020-05-05 stsp if (tree2)
3081 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3082 0208f208 2020-05-05 stsp return err;
3085 0208f208 2020-05-05 stsp static const struct got_error *
3086 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3090 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3091 fe621944 2020-11-10 stsp if (p == NULL)
3092 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3093 fe621944 2020-11-10 stsp *line_offsets = p;
3094 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3095 fe621944 2020-11-10 stsp (*nlines)++;
3096 fe621944 2020-11-10 stsp return NULL;
3099 fe621944 2020-11-10 stsp static const struct got_error *
3100 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3101 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3102 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3104 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3105 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3106 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3107 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3108 45d799e2 2018-12-23 stsp time_t committer_time;
3109 45d799e2 2018-12-23 stsp const char *author, *committer;
3110 8b473291 2019-02-21 stsp char *refs_str = NULL;
3111 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3112 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3113 fe621944 2020-11-10 stsp off_t outoff = 0;
3116 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3118 8b473291 2019-02-21 stsp if (refs) {
3119 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3121 8b473291 2019-02-21 stsp return err;
3124 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3126 abd2672a 2018-12-23 stsp return err;
3128 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3129 15a94983 2018-12-23 stsp if (err) {
3130 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3131 15a94983 2018-12-23 stsp goto done;
3134 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3136 fe621944 2020-11-10 stsp goto done;
3138 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3139 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3140 fe621944 2020-11-10 stsp if (n < 0) {
3141 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3142 abd2672a 2018-12-23 stsp goto done;
3144 fe621944 2020-11-10 stsp outoff += n;
3145 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3147 fe621944 2020-11-10 stsp goto done;
3149 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3150 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3151 fe621944 2020-11-10 stsp if (n < 0) {
3152 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3153 abd2672a 2018-12-23 stsp goto done;
3155 fe621944 2020-11-10 stsp outoff += n;
3156 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3158 fe621944 2020-11-10 stsp goto done;
3160 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3161 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3162 fe621944 2020-11-10 stsp if (datestr) {
3163 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3164 fe621944 2020-11-10 stsp if (n < 0) {
3165 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3166 fe621944 2020-11-10 stsp goto done;
3168 fe621944 2020-11-10 stsp outoff += n;
3169 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3171 fe621944 2020-11-10 stsp goto done;
3173 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3174 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3175 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3176 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3177 fe621944 2020-11-10 stsp if (n < 0) {
3178 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3179 fe621944 2020-11-10 stsp goto done;
3181 fe621944 2020-11-10 stsp outoff += n;
3182 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3184 fe621944 2020-11-10 stsp goto done;
3186 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3188 5943eee2 2019-08-13 stsp goto done;
3189 fe621944 2020-11-10 stsp s = logmsg;
3190 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3191 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3192 fe621944 2020-11-10 stsp if (n < 0) {
3193 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3194 fe621944 2020-11-10 stsp goto done;
3196 fe621944 2020-11-10 stsp outoff += n;
3197 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3199 fe621944 2020-11-10 stsp goto done;
3202 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3204 0208f208 2020-05-05 stsp goto done;
3205 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3206 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3207 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3208 fe621944 2020-11-10 stsp if (n < 0) {
3209 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3210 fe621944 2020-11-10 stsp goto done;
3212 fe621944 2020-11-10 stsp outoff += n;
3213 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3215 fe621944 2020-11-10 stsp goto done;
3216 0208f208 2020-05-05 stsp free((char *)pe->path);
3217 0208f208 2020-05-05 stsp free(pe->data);
3220 0208f208 2020-05-05 stsp fputc('\n', outfile);
3222 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3224 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3225 abd2672a 2018-12-23 stsp free(id_str);
3226 5943eee2 2019-08-13 stsp free(logmsg);
3227 8b473291 2019-02-21 stsp free(refs_str);
3228 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3229 7510f233 2020-08-09 stsp if (err) {
3230 ae6a6978 2020-08-09 stsp free(*line_offsets);
3231 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3232 ae6a6978 2020-08-09 stsp *nlines = 0;
3234 fe621944 2020-11-10 stsp return err;
3237 abd2672a 2018-12-23 stsp static const struct got_error *
3238 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3240 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3241 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3242 15a94983 2018-12-23 stsp int obj_type;
3244 fe621944 2020-11-10 stsp free(s->line_offsets);
3245 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3246 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3247 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3248 fe621944 2020-11-10 stsp s->nlines = 0;
3250 511a516b 2018-05-19 stsp f = got_opentemp();
3251 48ae06ee 2018-10-18 stsp if (f == NULL) {
3252 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3253 48ae06ee 2018-10-18 stsp goto done;
3255 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3256 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3257 fb43ecf1 2019-02-11 stsp goto done;
3261 15a94983 2018-12-23 stsp if (s->id1)
3262 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3264 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3266 15a94983 2018-12-23 stsp goto done;
3268 15a94983 2018-12-23 stsp switch (obj_type) {
3269 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3270 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3271 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3272 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3274 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3275 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3276 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3277 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3279 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3280 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3281 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3282 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3283 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3285 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3287 3ffacbe1 2020-02-02 tracey goto done;
3288 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3289 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3290 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3291 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3292 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3293 f44b1f58 2020-02-02 tracey if (err)
3294 f44b1f58 2020-02-02 tracey goto done;
3295 f44b1f58 2020-02-02 tracey } else {
3296 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3297 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3298 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3299 fe621944 2020-11-10 stsp err = write_commit_info(
3300 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3301 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3302 f44b1f58 2020-02-02 tracey if (err)
3303 f44b1f58 2020-02-02 tracey goto done;
3308 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3310 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3311 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3312 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3316 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3319 f44b1f58 2020-02-02 tracey if (err)
3320 f44b1f58 2020-02-02 tracey goto done;
3322 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3323 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3324 48ae06ee 2018-10-18 stsp return err;
3327 f5215bb9 2019-02-22 stsp static void
3328 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3330 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3331 f5215bb9 2019-02-22 stsp update_panels();
3332 f5215bb9 2019-02-22 stsp doupdate();
3335 f44b1f58 2020-02-02 tracey static const struct got_error *
3336 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3338 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3340 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3341 f44b1f58 2020-02-02 tracey return NULL;
3344 f44b1f58 2020-02-02 tracey static const struct got_error *
3345 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3347 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3348 f44b1f58 2020-02-02 tracey int lineno;
3349 826082fe 2020-12-10 stsp char *line = NULL;
3350 826082fe 2020-12-10 stsp size_t linesize = 0;
3351 826082fe 2020-12-10 stsp ssize_t linelen;
3353 f44b1f58 2020-02-02 tracey if (!view->searching) {
3354 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3355 f44b1f58 2020-02-02 tracey return NULL;
3358 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3359 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3360 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3362 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3363 f44b1f58 2020-02-02 tracey } else {
3364 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3365 f44b1f58 2020-02-02 tracey lineno = 1;
3367 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3370 f44b1f58 2020-02-02 tracey while (1) {
3371 f44b1f58 2020-02-02 tracey off_t offset;
3373 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3374 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3375 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3379 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3380 f44b1f58 2020-02-02 tracey lineno = 1;
3382 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3385 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3386 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3387 f44b1f58 2020-02-02 tracey free(line);
3388 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3390 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3391 826082fe 2020-12-10 stsp if (linelen != -1 &&
3392 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3393 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3394 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3397 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3398 f44b1f58 2020-02-02 tracey lineno++;
3400 f44b1f58 2020-02-02 tracey lineno--;
3402 826082fe 2020-12-10 stsp free(line);
3404 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3405 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3406 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3409 f44b1f58 2020-02-02 tracey return NULL;
3412 48ae06ee 2018-10-18 stsp static const struct got_error *
3413 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3414 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3415 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3416 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3418 48ae06ee 2018-10-18 stsp const struct got_error *err;
3419 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3421 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3422 15a94983 2018-12-23 stsp int type1, type2;
3423 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3425 15a94983 2018-12-23 stsp return err;
3426 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3428 15a94983 2018-12-23 stsp return err;
3430 15a94983 2018-12-23 stsp if (type1 != type2)
3431 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3433 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3434 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3435 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3436 f44b1f58 2020-02-02 tracey s->repo = repo;
3437 f44b1f58 2020-02-02 tracey s->id1 = id1;
3438 f44b1f58 2020-02-02 tracey s->id2 = id2;
3439 3dbaef42 2020-11-24 stsp s->label1 = label1;
3440 3dbaef42 2020-11-24 stsp s->label2 = label2;
3442 15a94983 2018-12-23 stsp if (id1) {
3443 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3444 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3445 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3447 5465d566 2020-02-01 tracey s->id1 = NULL;
3449 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3450 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3451 5465d566 2020-02-01 tracey free(s->id1);
3452 5465d566 2020-02-01 tracey s->id1 = NULL;
3453 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3455 5465d566 2020-02-01 tracey s->f = NULL;
3456 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3457 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3458 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3459 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3460 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3461 5465d566 2020-02-01 tracey s->log_view = log_view;
3462 5465d566 2020-02-01 tracey s->repo = repo;
3464 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3465 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3466 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3467 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3468 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3470 6d17833f 2019-11-08 stsp return err;
3471 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3472 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3473 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3474 6d17833f 2019-11-08 stsp if (err) {
3475 5465d566 2020-02-01 tracey free_colors(&s->colors);
3476 6d17833f 2019-11-08 stsp return err;
3478 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3479 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3480 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3481 6d17833f 2019-11-08 stsp if (err) {
3482 5465d566 2020-02-01 tracey free_colors(&s->colors);
3483 6d17833f 2019-11-08 stsp return err;
3486 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3487 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3488 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3489 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3490 6d17833f 2019-11-08 stsp if (err) {
3491 5465d566 2020-02-01 tracey free_colors(&s->colors);
3492 6d17833f 2019-11-08 stsp return err;
3495 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3496 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3497 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3498 11b20872 2019-11-08 stsp if (err) {
3499 5465d566 2020-02-01 tracey free_colors(&s->colors);
3500 11b20872 2019-11-08 stsp return err;
3503 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3504 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3505 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3506 11b20872 2019-11-08 stsp if (err) {
3507 5465d566 2020-02-01 tracey free_colors(&s->colors);
3508 11b20872 2019-11-08 stsp return err;
3512 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3513 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3514 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3516 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3517 fe621944 2020-11-10 stsp s->nlines = 0;
3518 5465d566 2020-02-01 tracey err = create_diff(s);
3519 48ae06ee 2018-10-18 stsp if (err) {
3520 5465d566 2020-02-01 tracey free(s->id1);
3521 5465d566 2020-02-01 tracey s->id1 = NULL;
3522 5465d566 2020-02-01 tracey free(s->id2);
3523 5465d566 2020-02-01 tracey s->id2 = NULL;
3524 78756c87 2020-11-24 stsp free_colors(&s->colors);
3525 48ae06ee 2018-10-18 stsp return err;
3528 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3529 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3530 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3531 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3532 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3534 5dc9f4bc 2018-08-04 stsp return NULL;
3537 e5a0f69f 2018-08-18 stsp static const struct got_error *
3538 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3540 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3541 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3543 5465d566 2020-02-01 tracey free(s->id1);
3544 5465d566 2020-02-01 tracey s->id1 = NULL;
3545 5465d566 2020-02-01 tracey free(s->id2);
3546 5465d566 2020-02-01 tracey s->id2 = NULL;
3547 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3548 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3549 5465d566 2020-02-01 tracey free_colors(&s->colors);
3550 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3551 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3552 fe621944 2020-11-10 stsp s->nlines = 0;
3553 e5a0f69f 2018-08-18 stsp return err;
3556 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3557 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3559 a3404814 2018-09-02 stsp const struct got_error *err;
3560 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3561 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3562 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3564 a3404814 2018-09-02 stsp if (s->id1) {
3565 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3567 a3404814 2018-09-02 stsp return err;
3568 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3570 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3572 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3574 a3404814 2018-09-02 stsp return err;
3575 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3577 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3578 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3579 a3404814 2018-09-02 stsp free(id_str1);
3580 a3404814 2018-09-02 stsp free(id_str2);
3581 a3404814 2018-09-02 stsp return err;
3583 a3404814 2018-09-02 stsp free(id_str1);
3584 a3404814 2018-09-02 stsp free(id_str2);
3586 89f1a395 2020-12-01 naddy return draw_file(view, header);
3589 15a087fe 2019-02-21 stsp static const struct got_error *
3590 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3591 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3593 d7a04538 2019-02-21 stsp const struct got_error *err;
3594 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3595 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3596 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3598 15a087fe 2019-02-21 stsp free(s->id2);
3599 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3600 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3601 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3603 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3605 d7a04538 2019-02-21 stsp return err;
3606 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3607 15a087fe 2019-02-21 stsp free(s->id1);
3608 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3609 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3610 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3611 15a087fe 2019-02-21 stsp return NULL;
3614 0cf4efb1 2018-09-29 stsp static const struct got_error *
3615 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3617 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3618 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3619 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3620 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3621 826082fe 2020-12-10 stsp char *line = NULL;
3622 826082fe 2020-12-10 stsp size_t linesize = 0;
3623 826082fe 2020-12-10 stsp ssize_t linelen;
3626 e5a0f69f 2018-08-18 stsp switch (ch) {
3629 3dbaef42 2020-11-24 stsp if (ch == 'a')
3630 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3631 3dbaef42 2020-11-24 stsp if (ch == 'w')
3632 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3633 64453f7e 2020-11-21 stsp wclear(view->window);
3634 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3635 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3636 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3637 64453f7e 2020-11-21 stsp err = create_diff(s);
3640 1e37a5c2 2019-05-12 jcs case KEY_UP:
3641 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3642 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3644 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3645 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3646 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3649 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3650 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3651 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3654 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3655 1e37a5c2 2019-05-12 jcs if (!s->eof)
3656 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3658 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3659 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3661 00ba99a7 2019-05-12 jcs if (s->eof)
3664 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3665 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3666 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3667 826082fe 2020-12-10 stsp if (linelen == -1) {
3668 826082fe 2020-12-10 stsp if (feof(s->f)) {
3669 826082fe 2020-12-10 stsp s->eof = 1;
3671 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3675 826082fe 2020-12-10 stsp free(line);
3678 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3679 1e37a5c2 2019-05-12 jcs s->diff_context--;
3680 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3681 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3682 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3683 27829c9e 2020-11-21 stsp s->nlines) {
3684 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3685 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3690 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3691 1e37a5c2 2019-05-12 jcs s->diff_context++;
3692 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3693 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3698 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3700 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3701 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3703 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3707 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3710 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3714 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3715 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3717 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3718 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3722 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3724 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3725 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3727 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3731 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3734 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3738 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3739 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3741 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3742 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3748 bcbd79e2 2018-08-19 stsp return err;
3751 4ed7e80c 2018-05-20 stsp static const struct got_error *
3752 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3754 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3755 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3756 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3757 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3758 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3759 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3760 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3761 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3762 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3763 3dbaef42 2020-11-24 stsp const char *errstr;
3764 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3766 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3767 26ed57b2 2018-05-19 stsp switch (ch) {
3769 64453f7e 2020-11-21 stsp force_text_diff = 1;
3772 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3774 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3775 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3777 09b5bff8 2020-02-23 naddy case 'r':
3778 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3779 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3780 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3782 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3785 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3788 17020d27 2019-03-07 stsp usage_diff();
3789 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3793 26ed57b2 2018-05-19 stsp argc -= optind;
3794 26ed57b2 2018-05-19 stsp argv += optind;
3796 26ed57b2 2018-05-19 stsp if (argc == 0) {
3797 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3798 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3799 15a94983 2018-12-23 stsp id_str1 = argv[0];
3800 15a94983 2018-12-23 stsp id_str2 = argv[1];
3802 26ed57b2 2018-05-19 stsp usage_diff();
3804 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3805 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3806 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3807 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3808 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3809 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3810 c156c7a4 2020-12-18 stsp goto done;
3811 a273ac94 2020-02-23 naddy if (worktree)
3812 a273ac94 2020-02-23 naddy repo_path =
3813 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3815 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3816 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3817 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3818 c156c7a4 2020-12-18 stsp goto done;
3822 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3823 eb6600df 2019-01-04 stsp if (error)
3824 eb6600df 2019-01-04 stsp goto done;
3826 a273ac94 2020-02-23 naddy init_curses();
3828 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3829 51a10b52 2020-12-26 stsp if (error)
3830 51a10b52 2020-12-26 stsp goto done;
3832 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
3833 26ed57b2 2018-05-19 stsp if (error)
3834 26ed57b2 2018-05-19 stsp goto done;
3836 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3837 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3838 26ed57b2 2018-05-19 stsp if (error)
3839 26ed57b2 2018-05-19 stsp goto done;
3841 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3842 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3843 26ed57b2 2018-05-19 stsp if (error)
3844 26ed57b2 2018-05-19 stsp goto done;
3846 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3847 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3848 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3849 ea5e7bb5 2018-08-01 stsp goto done;
3851 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3852 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3853 5dc9f4bc 2018-08-04 stsp if (error)
3854 5dc9f4bc 2018-08-04 stsp goto done;
3855 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3857 3dbaef42 2020-11-24 stsp free(label1);
3858 3dbaef42 2020-11-24 stsp free(label2);
3859 c02c541e 2019-03-29 stsp free(repo_path);
3860 a273ac94 2020-02-23 naddy free(cwd);
3862 921be706 2019-06-28 stsp got_repo_close(repo);
3863 a273ac94 2020-02-23 naddy if (worktree)
3864 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3865 51a10b52 2020-12-26 stsp tog_free_refs();
3866 26ed57b2 2018-05-19 stsp return error;
3869 4ed7e80c 2018-05-20 stsp __dead static void
3870 9f7d7167 2018-04-29 stsp usage_blame(void)
3873 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3874 9f7d7167 2018-04-29 stsp getprogname());
3878 84451b3e 2018-07-10 stsp struct tog_blame_line {
3879 84451b3e 2018-07-10 stsp int annotated;
3880 84451b3e 2018-07-10 stsp struct got_object_id *id;
3883 4ed7e80c 2018-05-20 stsp static const struct got_error *
3884 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3886 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3887 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3888 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3889 84451b3e 2018-07-10 stsp const struct got_error *err;
3890 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3891 826082fe 2020-12-10 stsp char *line = NULL;
3892 826082fe 2020-12-10 stsp size_t linesize = 0;
3893 826082fe 2020-12-10 stsp ssize_t linelen;
3894 84451b3e 2018-07-10 stsp wchar_t *wline;
3895 27a741e5 2019-09-11 stsp int width;
3896 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3897 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3898 ab089a2a 2018-07-12 stsp char *id_str;
3899 11b20872 2019-11-08 stsp struct tog_color *tc;
3901 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3903 ab089a2a 2018-07-12 stsp return err;
3905 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
3906 f7d12f7e 2018-08-01 stsp werase(view->window);
3908 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3909 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3910 ab089a2a 2018-07-12 stsp free(id_str);
3911 ab089a2a 2018-07-12 stsp return err;
3914 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3915 ab089a2a 2018-07-12 stsp free(line);
3916 2550e4c3 2018-07-13 stsp line = NULL;
3918 1cae65b4 2019-09-22 stsp return err;
3919 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3920 a3404814 2018-09-02 stsp wstandout(view->window);
3921 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3923 11b20872 2019-11-08 stsp wattr_on(view->window,
3924 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3925 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3927 11b20872 2019-11-08 stsp wattr_off(view->window,
3928 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3929 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3930 a3404814 2018-09-02 stsp wstandend(view->window);
3931 2550e4c3 2018-07-13 stsp free(wline);
3932 2550e4c3 2018-07-13 stsp wline = NULL;
3933 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3934 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3936 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3937 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3938 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3939 ab089a2a 2018-07-12 stsp free(id_str);
3940 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3942 ab089a2a 2018-07-12 stsp free(id_str);
3943 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3944 3f60a8ef 2018-07-10 stsp free(line);
3945 2550e4c3 2018-07-13 stsp line = NULL;
3947 3f60a8ef 2018-07-10 stsp return err;
3948 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3949 2550e4c3 2018-07-13 stsp free(wline);
3950 2550e4c3 2018-07-13 stsp wline = NULL;
3951 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3952 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3954 4f7c3e5e 2020-12-01 naddy s->eof = 0;
3955 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
3956 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
3957 826082fe 2020-12-10 stsp if (linelen == -1) {
3958 826082fe 2020-12-10 stsp if (feof(blame->f)) {
3959 826082fe 2020-12-10 stsp s->eof = 1;
3962 84451b3e 2018-07-10 stsp free(line);
3963 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
3965 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
3968 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3969 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3971 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
3972 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
3973 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3974 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3975 27a741e5 2019-09-11 stsp !(view->focussed &&
3976 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
3977 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3978 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3979 8d0fe45a 2019-08-12 stsp char *id_str;
3980 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3981 8d0fe45a 2019-08-12 stsp if (err) {
3982 8d0fe45a 2019-08-12 stsp free(line);
3983 8d0fe45a 2019-08-12 stsp return err;
3985 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3987 11b20872 2019-11-08 stsp wattr_on(view->window,
3988 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3989 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3991 11b20872 2019-11-08 stsp wattr_off(view->window,
3992 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3993 8d0fe45a 2019-08-12 stsp free(id_str);
3994 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3996 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3997 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4000 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4001 ee41ec32 2018-07-10 stsp prev_id = NULL;
4004 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4005 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4006 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4008 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4009 41605754 2020-11-12 stsp width = 9;
4010 41605754 2020-11-12 stsp wline = wcsdup(L"");
4011 41605754 2020-11-12 stsp if (wline == NULL) {
4012 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4013 41605754 2020-11-12 stsp free(line);
4014 41605754 2020-11-12 stsp return err;
4016 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4017 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4018 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4019 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4020 41605754 2020-11-12 stsp view->window, regmatch);
4021 41605754 2020-11-12 stsp if (err) {
4022 41605754 2020-11-12 stsp free(line);
4023 41605754 2020-11-12 stsp return err;
4025 41605754 2020-11-12 stsp width += 9;
4027 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4028 41605754 2020-11-12 stsp view->ncols - 9, 9);
4029 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4030 41605754 2020-11-12 stsp free(wline);
4031 41605754 2020-11-12 stsp wline = NULL;
4032 41605754 2020-11-12 stsp width += 9;
4035 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4036 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4037 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4038 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4040 826082fe 2020-12-10 stsp free(line);
4041 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4043 1a57306a 2018-09-02 stsp view_vborder(view);
4045 84451b3e 2018-07-10 stsp return NULL;
4048 84451b3e 2018-07-10 stsp static const struct got_error *
4049 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4051 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4052 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4053 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4054 1a76625f 2018-10-22 stsp int errcode;
4056 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4057 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4058 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4060 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4061 1a76625f 2018-10-22 stsp if (errcode)
4062 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4064 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4065 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4066 d68a0a7d 2018-07-10 stsp goto done;
4069 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4070 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4072 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4073 d68a0a7d 2018-07-10 stsp if (line->annotated)
4074 d68a0a7d 2018-07-10 stsp goto done;
4076 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4077 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4078 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4079 84451b3e 2018-07-10 stsp goto done;
4081 84451b3e 2018-07-10 stsp line->annotated = 1;
4083 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4084 1a76625f 2018-10-22 stsp if (errcode)
4085 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4086 84451b3e 2018-07-10 stsp return err;
4089 84451b3e 2018-07-10 stsp static void *
4090 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4092 18430de3 2018-07-10 stsp const struct got_error *err;
4093 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4094 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4095 1a76625f 2018-10-22 stsp int errcode;
4097 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4099 61266923 2020-01-14 stsp return (void *)err;
4101 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4102 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4103 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4104 fc06ba56 2019-08-22 stsp err = NULL;
4106 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4107 1a76625f 2018-10-22 stsp if (errcode)
4108 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4109 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4111 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4112 c9beca56 2018-07-22 stsp ta->repo = NULL;
4113 c9beca56 2018-07-22 stsp *ta->complete = 1;
4115 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4116 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4117 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4119 18430de3 2018-07-10 stsp return (void *)err;
4122 245d91c1 2018-07-12 stsp static struct got_object_id *
4123 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4124 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4126 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4128 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4129 8d0fe45a 2019-08-12 stsp return NULL;
4131 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4132 245d91c1 2018-07-12 stsp if (!line->annotated)
4133 245d91c1 2018-07-12 stsp return NULL;
4135 245d91c1 2018-07-12 stsp return line->id;
4138 b880a918 2018-07-10 stsp static const struct got_error *
4139 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4141 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4144 245d91c1 2018-07-12 stsp if (blame->thread) {
4145 1a76625f 2018-10-22 stsp int errcode;
4146 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4147 1a76625f 2018-10-22 stsp if (errcode)
4148 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4149 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4150 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4151 1a76625f 2018-10-22 stsp if (errcode)
4152 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4153 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4154 1a76625f 2018-10-22 stsp if (errcode)
4155 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4156 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4157 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4158 245d91c1 2018-07-12 stsp err = NULL;
4159 245d91c1 2018-07-12 stsp blame->thread = NULL;
4161 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4162 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4163 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4165 245d91c1 2018-07-12 stsp if (blame->f) {
4166 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4167 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4168 245d91c1 2018-07-12 stsp blame->f = NULL;
4170 57670559 2018-12-23 stsp if (blame->lines) {
4171 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4172 57670559 2018-12-23 stsp free(blame->lines[i].id);
4173 57670559 2018-12-23 stsp free(blame->lines);
4174 57670559 2018-12-23 stsp blame->lines = NULL;
4176 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4177 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4179 245d91c1 2018-07-12 stsp return err;
4182 245d91c1 2018-07-12 stsp static const struct got_error *
4183 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4185 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4186 fc06ba56 2019-08-22 stsp int *done = arg;
4187 fc06ba56 2019-08-22 stsp int errcode;
4189 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4190 fc06ba56 2019-08-22 stsp if (errcode)
4191 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4192 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4194 fc06ba56 2019-08-22 stsp if (*done)
4195 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4197 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4198 fc06ba56 2019-08-22 stsp if (errcode)
4199 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4200 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4202 fc06ba56 2019-08-22 stsp return err;
4205 fc06ba56 2019-08-22 stsp static const struct got_error *
4206 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4208 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4209 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4210 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4211 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4212 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4213 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4214 15a94983 2018-12-23 stsp int obj_type;
4216 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4217 a5388363 2020-12-01 naddy s->path);
4219 15a94983 2018-12-23 stsp return err;
4221 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4223 84451b3e 2018-07-10 stsp goto done;
4225 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4226 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4227 84451b3e 2018-07-10 stsp goto done;
4230 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4232 a70480e0 2018-06-23 stsp goto done;
4233 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4234 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4235 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4236 84451b3e 2018-07-10 stsp goto done;
4238 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4239 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4241 1fddf795 2021-01-20 stsp goto done;
4242 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4243 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4244 84451b3e 2018-07-10 stsp goto done;
4247 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4248 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4249 b02560ec 2019-08-19 stsp blame->nlines--;
4251 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4252 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4253 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4254 84451b3e 2018-07-10 stsp goto done;
4257 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4259 bd24772e 2018-07-11 stsp goto done;
4261 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4262 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4263 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4264 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4265 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4266 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4267 245d91c1 2018-07-12 stsp goto done;
4269 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4271 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4272 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4273 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4274 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4275 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4276 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4277 a5388363 2020-12-01 naddy s->blame_complete = 0;
4279 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4280 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4281 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4282 f5a09613 2020-12-13 naddy s->selected_line = 1;
4287 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4288 27d434c2 2018-09-15 stsp free(obj_id);
4290 245d91c1 2018-07-12 stsp stop_blame(blame);
4291 245d91c1 2018-07-12 stsp return err;
4294 245d91c1 2018-07-12 stsp static const struct got_error *
4295 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4296 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4298 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4299 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4301 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4303 c4843652 2019-08-12 stsp s->path = strdup(path);
4304 c4843652 2019-08-12 stsp if (s->path == NULL)
4305 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4307 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4308 c4843652 2019-08-12 stsp if (err) {
4309 c4843652 2019-08-12 stsp free(s->path);
4310 7cbe629d 2018-08-04 stsp return err;
4313 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4314 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4315 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4316 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4317 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4318 fb2756b9 2018-08-04 stsp s->repo = repo;
4319 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4320 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4322 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4323 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4324 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4325 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4327 11b20872 2019-11-08 stsp return err;
4330 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4331 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4332 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4333 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4334 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4336 a5388363 2020-12-01 naddy return run_blame(view);
4339 e5a0f69f 2018-08-18 stsp static const struct got_error *
4340 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4342 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4343 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4345 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4346 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4348 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4349 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4350 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4351 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4352 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4355 e5a0f69f 2018-08-18 stsp free(s->path);
4356 11b20872 2019-11-08 stsp free_colors(&s->colors);
4358 e5a0f69f 2018-08-18 stsp return err;
4361 7cbe629d 2018-08-04 stsp static const struct got_error *
4362 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4364 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4366 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4367 6c4c42e0 2019-06-24 stsp return NULL;
4370 6c4c42e0 2019-06-24 stsp static const struct got_error *
4371 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4373 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4374 6c4c42e0 2019-06-24 stsp int lineno;
4375 826082fe 2020-12-10 stsp char *line = NULL;
4376 826082fe 2020-12-10 stsp size_t linesize = 0;
4377 826082fe 2020-12-10 stsp ssize_t linelen;
4379 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4380 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4381 6c4c42e0 2019-06-24 stsp return NULL;
4384 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4385 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4386 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4388 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4390 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4391 6c4c42e0 2019-06-24 stsp lineno = 1;
4393 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4396 6c4c42e0 2019-06-24 stsp while (1) {
4397 6c4c42e0 2019-06-24 stsp off_t offset;
4399 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4400 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4401 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4406 6c4c42e0 2019-06-24 stsp lineno = 1;
4408 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4411 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4412 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4413 6c4c42e0 2019-06-24 stsp free(line);
4414 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4416 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4417 826082fe 2020-12-10 stsp if (linelen != -1 &&
4418 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4419 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4420 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4423 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4428 826082fe 2020-12-10 stsp free(line);
4430 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4431 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4432 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4435 6c4c42e0 2019-06-24 stsp return NULL;
4438 6c4c42e0 2019-06-24 stsp static const struct got_error *
4439 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4441 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4442 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4443 2b380cc8 2018-10-24 stsp int errcode;
4445 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4446 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4447 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4448 2b380cc8 2018-10-24 stsp if (errcode)
4449 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4451 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4454 51fe7530 2019-08-19 stsp if (s->blame_complete)
4455 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4457 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4459 669b5ffa 2018-10-07 stsp view_vborder(view);
4460 e5a0f69f 2018-08-18 stsp return err;
4463 e5a0f69f 2018-08-18 stsp static const struct got_error *
4464 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4466 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4467 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4468 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4469 669b5ffa 2018-10-07 stsp int begin_x = 0;
4471 e5a0f69f 2018-08-18 stsp switch (ch) {
4473 1e37a5c2 2019-05-12 jcs s->done = 1;
4476 1e37a5c2 2019-05-12 jcs case KEY_UP:
4477 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4478 1e37a5c2 2019-05-12 jcs s->selected_line--;
4479 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4480 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4481 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4483 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4484 ea025d1d 2020-02-22 naddy case CTRL('b'):
4485 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4486 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4489 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4490 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4491 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4493 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4496 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4497 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4498 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4499 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4500 1e37a5c2 2019-05-12 jcs s->selected_line++;
4501 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4502 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4503 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4506 1e37a5c2 2019-05-12 jcs case 'p': {
4507 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4508 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4509 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4510 1e37a5c2 2019-05-12 jcs if (id == NULL)
4512 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4513 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4514 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4515 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4516 1e37a5c2 2019-05-12 jcs int obj_type;
4517 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4518 1e37a5c2 2019-05-12 jcs s->repo, id);
4521 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4522 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4523 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4524 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4527 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4528 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4529 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4530 e5a0f69f 2018-08-18 stsp if (err) {
4531 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4532 1e37a5c2 2019-05-12 jcs err = NULL;
4533 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4536 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4538 1e37a5c2 2019-05-12 jcs free(blob_id);
4539 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4540 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4541 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4544 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4546 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4548 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4549 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4551 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4556 1e37a5c2 2019-05-12 jcs s->done = 1;
4557 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4558 1e37a5c2 2019-05-12 jcs s->done = 0;
4559 1e37a5c2 2019-05-12 jcs if (thread_err)
4561 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4562 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4563 a5388363 2020-12-01 naddy err = run_blame(view);
4568 1e37a5c2 2019-05-12 jcs case 'B': {
4569 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4570 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4571 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4573 1e37a5c2 2019-05-12 jcs s->done = 1;
4574 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4575 1e37a5c2 2019-05-12 jcs s->done = 0;
4576 1e37a5c2 2019-05-12 jcs if (thread_err)
4578 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4579 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4580 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4581 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4582 a5388363 2020-12-01 naddy err = run_blame(view);
4587 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4588 1e37a5c2 2019-05-12 jcs case '\r': {
4589 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4590 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4591 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4592 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4593 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4594 1e37a5c2 2019-05-12 jcs if (id == NULL)
4596 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4599 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4600 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4601 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4602 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4603 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4604 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4605 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4606 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4609 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4610 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4611 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4613 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4616 e78dc838 2020-12-04 stsp view->focussed = 0;
4617 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4618 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4619 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4622 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4623 e78dc838 2020-12-04 stsp view->focus_child = 1;
4625 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4630 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4631 ea025d1d 2020-02-22 naddy case CTRL('f'):
4633 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4634 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4635 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4638 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4639 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4640 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4641 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4644 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4645 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4646 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4647 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4649 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4650 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4651 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4653 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4654 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4655 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4656 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4662 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4665 a70480e0 2018-06-23 stsp static const struct got_error *
4666 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4668 a70480e0 2018-06-23 stsp const struct got_error *error;
4669 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4670 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4671 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4672 0587e10c 2020-07-23 stsp char *link_target = NULL;
4673 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4674 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4676 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4678 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4679 a70480e0 2018-06-23 stsp switch (ch) {
4681 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4684 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4685 69069811 2018-08-02 stsp if (repo_path == NULL)
4686 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4690 17020d27 2019-03-07 stsp usage_blame();
4691 a70480e0 2018-06-23 stsp /* NOTREACHED */
4695 a70480e0 2018-06-23 stsp argc -= optind;
4696 a70480e0 2018-06-23 stsp argv += optind;
4698 f135c941 2020-02-20 stsp if (argc != 1)
4699 a70480e0 2018-06-23 stsp usage_blame();
4701 69069811 2018-08-02 stsp if (repo_path == NULL) {
4702 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4703 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4704 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4705 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4706 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4707 c156c7a4 2020-12-18 stsp goto done;
4708 f135c941 2020-02-20 stsp if (worktree)
4709 eb41ed75 2019-02-05 stsp repo_path =
4710 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4712 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4713 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4714 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4715 c156c7a4 2020-12-18 stsp goto done;
4719 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4720 c02c541e 2019-03-29 stsp if (error != NULL)
4721 8e94dd5b 2019-01-04 stsp goto done;
4723 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4724 f135c941 2020-02-20 stsp worktree);
4725 c02c541e 2019-03-29 stsp if (error)
4726 92205607 2019-01-04 stsp goto done;
4728 f135c941 2020-02-20 stsp init_curses();
4730 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4731 51a10b52 2020-12-26 stsp if (error)
4732 51a10b52 2020-12-26 stsp goto done;
4734 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
4735 eb41ed75 2019-02-05 stsp if (error)
4736 69069811 2018-08-02 stsp goto done;
4738 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4739 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4740 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4741 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4742 a70480e0 2018-06-23 stsp if (error != NULL)
4743 66b4983c 2018-06-23 stsp goto done;
4744 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4745 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4747 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4748 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4750 a19e88aa 2018-06-23 stsp if (error != NULL)
4751 8b473291 2019-02-21 stsp goto done;
4753 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4754 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4755 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4756 e1cd8fed 2018-08-01 stsp goto done;
4759 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4760 0587e10c 2020-07-23 stsp commit_id, repo);
4761 7cbe629d 2018-08-04 stsp if (error)
4762 7cbe629d 2018-08-04 stsp goto done;
4764 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4765 78756c87 2020-11-24 stsp commit_id, repo);
4766 0587e10c 2020-07-23 stsp if (error)
4767 0587e10c 2020-07-23 stsp goto done;
4768 12314ad4 2019-08-31 stsp if (worktree) {
4769 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4770 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4771 12314ad4 2019-08-31 stsp worktree = NULL;
4773 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4775 69069811 2018-08-02 stsp free(repo_path);
4776 f135c941 2020-02-20 stsp free(in_repo_path);
4777 0587e10c 2020-07-23 stsp free(link_target);
4778 69069811 2018-08-02 stsp free(cwd);
4779 a70480e0 2018-06-23 stsp free(commit_id);
4780 eb41ed75 2019-02-05 stsp if (worktree)
4781 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4783 a70480e0 2018-06-23 stsp got_repo_close(repo);
4784 51a10b52 2020-12-26 stsp tog_free_refs();
4785 a70480e0 2018-06-23 stsp return error;
4788 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4789 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4791 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4792 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4793 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4794 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4795 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4796 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4797 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4799 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4801 f7d12f7e 2018-08-01 stsp werase(view->window);
4803 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4804 ffd1d5e5 2018-06-23 stsp return NULL;
4806 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4808 ffd1d5e5 2018-06-23 stsp return err;
4809 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4810 a3404814 2018-09-02 stsp wstandout(view->window);
4811 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4813 11b20872 2019-11-08 stsp wattr_on(view->window,
4814 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4815 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4817 11b20872 2019-11-08 stsp wattr_off(view->window,
4818 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4819 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4820 a3404814 2018-09-02 stsp wstandend(view->window);
4821 2550e4c3 2018-07-13 stsp free(wline);
4822 2550e4c3 2018-07-13 stsp wline = NULL;
4823 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4824 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4825 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4826 ffd1d5e5 2018-06-23 stsp return NULL;
4827 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4829 ce52c690 2018-06-23 stsp return err;
4830 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4831 2550e4c3 2018-07-13 stsp free(wline);
4832 2550e4c3 2018-07-13 stsp wline = NULL;
4833 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4834 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4835 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4836 ffd1d5e5 2018-06-23 stsp return NULL;
4837 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4838 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4839 a1eca9bb 2018-06-23 stsp return NULL;
4841 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4842 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4843 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4844 0cf4efb1 2018-09-29 stsp if (view->focussed)
4845 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4846 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4848 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4849 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4850 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4851 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4852 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4853 ffd1d5e5 2018-06-23 stsp return NULL;
4857 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4860 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4861 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4862 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4863 848d6979 2019-08-12 stsp const char *modestr = "";
4864 56e0773d 2019-11-28 stsp mode_t mode;
4866 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4867 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4869 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4870 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4871 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4873 638f9024 2019-05-13 stsp return got_error_from_errno(
4874 230a42bd 2019-05-11 jcs "got_object_id_str");
4876 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4877 63c5ca5d 2019-08-24 stsp modestr = "$";
4878 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4881 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4882 d86d3b18 2020-12-01 naddy te, s->repo);
4883 0d6c6ee3 2020-05-20 stsp if (err) {
4884 0d6c6ee3 2020-05-20 stsp free(id_str);
4885 0d6c6ee3 2020-05-20 stsp return err;
4887 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4888 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4889 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4891 848d6979 2019-08-12 stsp modestr = "@";
4893 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4894 848d6979 2019-08-12 stsp modestr = "/";
4895 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4896 848d6979 2019-08-12 stsp modestr = "*";
4897 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4898 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4899 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4900 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4901 1d13200f 2018-07-12 stsp free(id_str);
4902 0d6c6ee3 2020-05-20 stsp free(link_target);
4903 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4905 1d13200f 2018-07-12 stsp free(id_str);
4906 0d6c6ee3 2020-05-20 stsp free(link_target);
4907 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4908 ffd1d5e5 2018-06-23 stsp if (err) {
4909 ffd1d5e5 2018-06-23 stsp free(line);
4912 d86d3b18 2020-12-01 naddy if (n == s->selected) {
4913 0cf4efb1 2018-09-29 stsp if (view->focussed)
4914 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4915 d86d3b18 2020-12-01 naddy s->selected_entry = te;
4917 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
4919 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4920 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4921 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4923 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4924 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4925 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4926 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4927 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
4928 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4929 ffd1d5e5 2018-06-23 stsp free(line);
4930 2550e4c3 2018-07-13 stsp free(wline);
4931 2550e4c3 2018-07-13 stsp wline = NULL;
4933 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4934 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
4935 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4939 ffd1d5e5 2018-06-23 stsp return err;
4942 ffd1d5e5 2018-06-23 stsp static void
4943 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4945 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4946 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4947 fa86c4bf 2020-11-29 stsp int i = 0;
4949 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4952 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4953 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
4954 fa86c4bf 2020-11-29 stsp if (te == NULL) {
4955 fa86c4bf 2020-11-29 stsp if (!isroot)
4956 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
4959 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
4960 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
4964 fa86c4bf 2020-11-29 stsp static void
4965 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4967 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4968 ffd1d5e5 2018-06-23 stsp int n = 0;
4970 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
4971 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
4972 694d3271 2020-12-01 naddy s->first_displayed_entry);
4974 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
4976 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
4977 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4978 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
4979 768394f3 2019-01-24 stsp if (last) {
4980 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
4981 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
4986 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4987 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4988 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4990 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4991 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4992 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4994 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4995 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4996 56e0773d 2019-11-28 stsp + 1 /* slash */;
4998 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5000 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5001 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5002 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5004 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5005 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5006 d9765a41 2018-06-23 stsp while (pt) {
5007 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5008 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5009 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5010 cb2ebc8a 2018-06-23 stsp goto done;
5012 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5013 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5014 cb2ebc8a 2018-06-23 stsp goto done;
5016 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5019 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5020 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5021 ce52c690 2018-06-23 stsp goto done;
5025 ce52c690 2018-06-23 stsp if (err) {
5026 ce52c690 2018-06-23 stsp free(*path);
5027 ce52c690 2018-06-23 stsp *path = NULL;
5029 ce52c690 2018-06-23 stsp return err;
5032 ce52c690 2018-06-23 stsp static const struct got_error *
5033 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5034 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5035 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5037 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5038 ce52c690 2018-06-23 stsp char *path;
5039 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5041 a0de39f3 2019-08-09 stsp *new_view = NULL;
5043 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5045 ce52c690 2018-06-23 stsp return err;
5047 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5048 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5049 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5050 83ce39e3 2019-08-12 stsp goto done;
5053 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5054 e5a0f69f 2018-08-18 stsp if (err) {
5055 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5056 fc06ba56 2019-08-22 stsp err = NULL;
5057 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5059 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5061 83ce39e3 2019-08-12 stsp free(path);
5062 69efd4c4 2018-07-18 stsp return err;
5065 69efd4c4 2018-07-18 stsp static const struct got_error *
5066 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5067 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5069 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5070 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5071 69efd4c4 2018-07-18 stsp char *path;
5073 a0de39f3 2019-08-09 stsp *new_view = NULL;
5075 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5076 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5077 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5079 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5081 69efd4c4 2018-07-18 stsp return err;
5083 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5086 e5a0f69f 2018-08-18 stsp view_close(log_view);
5088 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5089 cb2ebc8a 2018-06-23 stsp free(path);
5090 cb2ebc8a 2018-06-23 stsp return err;
5093 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5094 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5095 4e97c21c 2020-12-06 stsp struct got_object_id *commit_id, const char *head_ref_name,
5096 4e97c21c 2020-12-06 stsp struct got_repository *repo)
5098 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5099 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5100 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5102 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5104 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5105 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5106 ffd1d5e5 2018-06-23 stsp goto done;
5108 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5109 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5110 ffd1d5e5 2018-06-23 stsp goto done;
5113 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5114 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5115 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5116 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5117 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5118 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5119 6484ec90 2018-09-29 stsp goto done;
5121 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5122 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5123 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5124 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5125 9cd7cbd1 2020-12-07 stsp goto done;
5128 fb2756b9 2018-08-04 stsp s->repo = repo;
5130 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5132 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5133 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5134 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5135 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5137 c0b01bdb 2019-11-08 stsp goto done;
5138 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5139 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5140 c0b01bdb 2019-11-08 stsp if (err) {
5141 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5142 c0b01bdb 2019-11-08 stsp goto done;
5144 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5145 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5146 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5147 c0b01bdb 2019-11-08 stsp if (err) {
5148 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5149 c0b01bdb 2019-11-08 stsp goto done;
5152 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5153 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5154 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5155 11b20872 2019-11-08 stsp if (err) {
5156 11b20872 2019-11-08 stsp free_colors(&s->colors);
5157 11b20872 2019-11-08 stsp goto done;
5160 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5161 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5162 c0b01bdb 2019-11-08 stsp if (err) {
5163 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5164 c0b01bdb 2019-11-08 stsp goto done;
5168 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5169 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5170 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5171 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5172 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5174 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5175 6484ec90 2018-09-29 stsp if (err) {
5176 fb2756b9 2018-08-04 stsp free(s->tree_label);
5177 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5179 ad80ab7b 2018-08-04 stsp return err;
5182 e5a0f69f 2018-08-18 stsp static const struct got_error *
5183 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5185 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5187 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5188 fb2756b9 2018-08-04 stsp free(s->tree_label);
5189 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5190 6484ec90 2018-09-29 stsp free(s->commit_id);
5191 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5192 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5193 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5194 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5195 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5196 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5197 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5198 ad80ab7b 2018-08-04 stsp free(parent);
5201 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5202 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5203 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5204 7c32bd05 2019-06-22 stsp return NULL;
5207 7c32bd05 2019-06-22 stsp static const struct got_error *
5208 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5210 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5212 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5213 7c32bd05 2019-06-22 stsp return NULL;
5216 7c32bd05 2019-06-22 stsp static int
5217 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5219 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5221 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5225 7c32bd05 2019-06-22 stsp static const struct got_error *
5226 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5228 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5229 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5231 7c32bd05 2019-06-22 stsp if (!view->searching) {
5232 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5233 7c32bd05 2019-06-22 stsp return NULL;
5236 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5237 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5238 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5239 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5240 56e0773d 2019-11-28 stsp s->selected_entry);
5242 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5244 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5245 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5247 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5248 56e0773d 2019-11-28 stsp s->selected_entry);
5251 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5252 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5254 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5257 7c32bd05 2019-06-22 stsp while (1) {
5258 56e0773d 2019-11-28 stsp if (te == NULL) {
5259 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5260 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5261 ac66afb8 2019-06-24 stsp return NULL;
5263 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5264 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5266 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5269 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5270 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5271 56e0773d 2019-11-28 stsp s->matched_entry = te;
5275 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5276 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5278 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5281 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5282 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5283 7c32bd05 2019-06-22 stsp s->selected = 0;
5286 e5a0f69f 2018-08-18 stsp return NULL;
5289 ad80ab7b 2018-08-04 stsp static const struct got_error *
5290 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5292 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5293 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5294 e5a0f69f 2018-08-18 stsp char *parent_path;
5296 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5298 e5a0f69f 2018-08-18 stsp return err;
5300 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5301 e5a0f69f 2018-08-18 stsp free(parent_path);
5303 669b5ffa 2018-10-07 stsp view_vborder(view);
5304 e5a0f69f 2018-08-18 stsp return err;
5307 e5a0f69f 2018-08-18 stsp static const struct got_error *
5308 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5310 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5311 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5312 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5313 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5315 e5a0f69f 2018-08-18 stsp switch (ch) {
5317 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5320 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5322 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5323 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5324 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5325 e78dc838 2020-12-04 stsp view->focussed = 0;
5326 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5327 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5328 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5330 1e37a5c2 2019-05-12 jcs return err;
5331 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5332 e78dc838 2020-12-04 stsp view->focus_child = 1;
5334 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5337 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5338 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5339 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5340 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5341 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5342 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5343 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5344 152c1c93 2020-11-29 stsp if (err) {
5345 152c1c93 2020-11-29 stsp view_close(ref_view);
5346 152c1c93 2020-11-29 stsp return err;
5348 e78dc838 2020-12-04 stsp view->focussed = 0;
5349 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5350 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5351 152c1c93 2020-11-29 stsp err = view_close_child(view);
5353 152c1c93 2020-11-29 stsp return err;
5354 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5355 e78dc838 2020-12-04 stsp view->focus_child = 1;
5357 152c1c93 2020-11-29 stsp *new_view = ref_view;
5360 1e37a5c2 2019-05-12 jcs case KEY_UP:
5361 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5362 1e37a5c2 2019-05-12 jcs s->selected--;
5365 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5367 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5368 ea025d1d 2020-02-22 naddy case CTRL('b'):
5369 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5370 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5371 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5372 fa86c4bf 2020-11-29 stsp s->selected = 0;
5374 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5375 fa86c4bf 2020-11-29 stsp s->selected = 0;
5377 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5380 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5381 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5382 1e37a5c2 2019-05-12 jcs s->selected++;
5385 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5387 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5389 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5391 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5392 ea025d1d 2020-02-22 naddy case CTRL('f'):
5393 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5395 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5396 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5397 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5400 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5402 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5404 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5405 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5406 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5407 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5408 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5410 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5411 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5413 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5414 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5415 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5416 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5417 1e37a5c2 2019-05-12 jcs s->selected_entry =
5418 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5419 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5420 1e37a5c2 2019-05-12 jcs free(parent);
5421 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5422 56e0773d 2019-11-28 stsp s->selected_entry))) {
5423 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5424 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5425 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5428 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5429 941e9f74 2019-05-21 stsp if (err) {
5430 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5433 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5434 56e0773d 2019-11-28 stsp s->selected_entry))) {
5435 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5436 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5437 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5439 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5440 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5441 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5444 e78dc838 2020-12-04 stsp view->focussed = 0;
5445 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5446 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5447 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5449 669b5ffa 2018-10-07 stsp return err;
5450 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5451 e78dc838 2020-12-04 stsp view->focus_child = 1;
5453 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5456 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5457 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5458 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5464 ffd1d5e5 2018-06-23 stsp return err;
5467 ffd1d5e5 2018-06-23 stsp __dead static void
5468 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5471 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5472 ffd1d5e5 2018-06-23 stsp getprogname());
5476 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5477 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5479 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5480 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5481 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5482 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5483 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5484 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5485 4e97c21c 2020-12-06 stsp char *label = NULL;
5486 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5487 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5488 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5489 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5491 5221c383 2018-08-01 stsp struct tog_view *view;
5493 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5494 ffd1d5e5 2018-06-23 stsp switch (ch) {
5496 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5499 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5500 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5501 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5505 e99e2d15 2020-11-24 naddy usage_tree();
5506 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5510 ffd1d5e5 2018-06-23 stsp argc -= optind;
5511 ffd1d5e5 2018-06-23 stsp argv += optind;
5513 55cccc34 2020-02-20 stsp if (argc > 1)
5514 e99e2d15 2020-11-24 naddy usage_tree();
5516 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5517 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5518 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5519 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5520 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5521 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5522 c156c7a4 2020-12-18 stsp goto done;
5523 55cccc34 2020-02-20 stsp if (worktree)
5524 52185f70 2019-02-05 stsp repo_path =
5525 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5527 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5528 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5529 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5530 c156c7a4 2020-12-18 stsp goto done;
5534 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5535 c02c541e 2019-03-29 stsp if (error != NULL)
5536 52185f70 2019-02-05 stsp goto done;
5538 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5539 55cccc34 2020-02-20 stsp repo, worktree);
5540 55cccc34 2020-02-20 stsp if (error)
5541 55cccc34 2020-02-20 stsp goto done;
5543 55cccc34 2020-02-20 stsp init_curses();
5545 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5546 c02c541e 2019-03-29 stsp if (error)
5547 52185f70 2019-02-05 stsp goto done;
5549 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
5550 51a10b52 2020-12-26 stsp if (error)
5551 51a10b52 2020-12-26 stsp goto done;
5553 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5554 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5555 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5556 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5557 4e97c21c 2020-12-06 stsp if (error)
5558 4e97c21c 2020-12-06 stsp goto done;
5559 4e97c21c 2020-12-06 stsp head_ref_name = label;
5561 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5562 4e97c21c 2020-12-06 stsp if (error == NULL)
5563 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5564 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5565 4e97c21c 2020-12-06 stsp goto done;
5566 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5567 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5568 4e97c21c 2020-12-06 stsp if (error)
5569 4e97c21c 2020-12-06 stsp goto done;
5572 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5573 55cccc34 2020-02-20 stsp if (error)
5574 ffd1d5e5 2018-06-23 stsp goto done;
5576 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5577 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5578 55cccc34 2020-02-20 stsp if (error)
5579 8b473291 2019-02-21 stsp goto done;
5581 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5582 5221c383 2018-08-01 stsp if (view == NULL) {
5583 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5584 5221c383 2018-08-01 stsp goto done;
5586 4e97c21c 2020-12-06 stsp error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5587 ad80ab7b 2018-08-04 stsp if (error)
5588 ad80ab7b 2018-08-04 stsp goto done;
5589 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5590 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5591 d91faf3b 2020-12-01 naddy in_repo_path);
5592 55cccc34 2020-02-20 stsp if (error)
5593 55cccc34 2020-02-20 stsp goto done;
5596 55cccc34 2020-02-20 stsp if (worktree) {
5597 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5598 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5599 55cccc34 2020-02-20 stsp worktree = NULL;
5601 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5603 52185f70 2019-02-05 stsp free(repo_path);
5604 e4a0e26d 2020-02-20 stsp free(cwd);
5605 ffd1d5e5 2018-06-23 stsp free(commit_id);
5606 4e97c21c 2020-12-06 stsp free(label);
5608 486cd271 2020-12-06 stsp got_ref_close(ref);
5609 ffd1d5e5 2018-06-23 stsp if (commit)
5610 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5612 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5614 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5615 51a10b52 2020-12-26 stsp tog_free_refs();
5616 ffd1d5e5 2018-06-23 stsp return error;
5619 6458efa5 2020-11-24 stsp static const struct got_error *
5620 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5622 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5623 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5625 6458efa5 2020-11-24 stsp s->nrefs = 0;
5626 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5627 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5630 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5631 6458efa5 2020-11-24 stsp if (re == NULL)
5632 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5634 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5635 8924d611 2020-12-26 stsp if (re->ref == NULL)
5636 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5637 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5638 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5641 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5642 6458efa5 2020-11-24 stsp return NULL;
5646 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5648 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5650 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5651 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5652 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5653 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5658 6458efa5 2020-11-24 stsp static const struct got_error *
5659 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5661 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5662 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5664 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5665 6458efa5 2020-11-24 stsp s->repo = repo;
5667 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5668 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5670 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5672 6458efa5 2020-11-24 stsp return err;
5674 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5675 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5676 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5677 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5679 6458efa5 2020-11-24 stsp goto done;
5681 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5682 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5683 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5685 6458efa5 2020-11-24 stsp goto done;
5687 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5688 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5689 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5691 6458efa5 2020-11-24 stsp goto done;
5694 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5695 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5696 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5697 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5698 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5701 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5702 6458efa5 2020-11-24 stsp return err;
5705 6458efa5 2020-11-24 stsp static const struct got_error *
5706 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5708 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5710 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5711 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5713 6458efa5 2020-11-24 stsp return NULL;
5716 6458efa5 2020-11-24 stsp static const struct got_error *
5717 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5718 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5720 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5721 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5722 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5723 6458efa5 2020-11-24 stsp int obj_type;
5725 c42c9805 2020-11-24 stsp *commit_id = NULL;
5727 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5729 6458efa5 2020-11-24 stsp return err;
5731 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5733 6458efa5 2020-11-24 stsp goto done;
5735 6458efa5 2020-11-24 stsp switch (obj_type) {
5736 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5737 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5739 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5740 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5742 6458efa5 2020-11-24 stsp goto done;
5743 c42c9805 2020-11-24 stsp free(obj_id);
5744 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5745 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5747 6458efa5 2020-11-24 stsp goto done;
5748 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5749 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5750 c42c9805 2020-11-24 stsp goto done;
5752 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5753 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5754 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5755 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5756 c42c9805 2020-11-24 stsp goto done;
5760 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5766 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5767 c42c9805 2020-11-24 stsp if (err) {
5768 c42c9805 2020-11-24 stsp free(*commit_id);
5769 c42c9805 2020-11-24 stsp *commit_id = NULL;
5771 c42c9805 2020-11-24 stsp return err;
5774 c42c9805 2020-11-24 stsp static const struct got_error *
5775 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5776 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5778 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5779 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5780 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5782 c42c9805 2020-11-24 stsp *new_view = NULL;
5784 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5785 c42c9805 2020-11-24 stsp if (err) {
5786 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5787 c42c9805 2020-11-24 stsp return err;
5789 c42c9805 2020-11-24 stsp return NULL;
5792 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5793 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5794 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5795 6458efa5 2020-11-24 stsp goto done;
5798 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5799 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5802 6458efa5 2020-11-24 stsp view_close(log_view);
5804 6458efa5 2020-11-24 stsp *new_view = log_view;
5805 c42c9805 2020-11-24 stsp free(commit_id);
5806 6458efa5 2020-11-24 stsp return err;
5809 ce5b7c56 2019-07-09 stsp static void
5810 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5812 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5813 34ba6917 2020-11-30 stsp int i = 0;
5815 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5818 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5819 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5820 34ba6917 2020-11-30 stsp if (re == NULL)
5822 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5823 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5827 34ba6917 2020-11-30 stsp static void
5828 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5830 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5831 6458efa5 2020-11-24 stsp int n = 0;
5833 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5834 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5836 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5838 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5839 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5840 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5841 6458efa5 2020-11-24 stsp if (last) {
5842 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5843 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5848 6458efa5 2020-11-24 stsp static const struct got_error *
5849 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5851 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5853 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5854 6458efa5 2020-11-24 stsp return NULL;
5857 6458efa5 2020-11-24 stsp static int
5858 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5860 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5862 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5866 6458efa5 2020-11-24 stsp static const struct got_error *
5867 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5869 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5870 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5872 6458efa5 2020-11-24 stsp if (!view->searching) {
5873 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5874 6458efa5 2020-11-24 stsp return NULL;
5877 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5878 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5879 6458efa5 2020-11-24 stsp if (s->selected_entry)
5880 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5882 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5883 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5885 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5886 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5888 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5889 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5892 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5893 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5895 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5898 6458efa5 2020-11-24 stsp while (1) {
5899 6458efa5 2020-11-24 stsp if (re == NULL) {
5900 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5901 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5902 6458efa5 2020-11-24 stsp return NULL;
5904 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5905 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5907 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5910 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5911 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5912 6458efa5 2020-11-24 stsp s->matched_entry = re;
5916 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5917 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5919 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5922 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5923 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5924 6458efa5 2020-11-24 stsp s->selected = 0;
5927 6458efa5 2020-11-24 stsp return NULL;
5930 6458efa5 2020-11-24 stsp static const struct got_error *
5931 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5933 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5934 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5935 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5936 6458efa5 2020-11-24 stsp char *line = NULL;
5937 6458efa5 2020-11-24 stsp wchar_t *wline;
5938 6458efa5 2020-11-24 stsp struct tog_color *tc;
5939 6458efa5 2020-11-24 stsp int width, n;
5940 6458efa5 2020-11-24 stsp int limit = view->nlines;
5942 6458efa5 2020-11-24 stsp werase(view->window);
5944 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5946 6458efa5 2020-11-24 stsp if (limit == 0)
5947 6458efa5 2020-11-24 stsp return NULL;
5949 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5951 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5952 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5953 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5955 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5956 6458efa5 2020-11-24 stsp if (err) {
5957 6458efa5 2020-11-24 stsp free(line);
5958 6458efa5 2020-11-24 stsp return err;
5960 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5961 6458efa5 2020-11-24 stsp wstandout(view->window);
5962 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5963 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5964 6458efa5 2020-11-24 stsp wstandend(view->window);
5965 6458efa5 2020-11-24 stsp free(wline);
5966 6458efa5 2020-11-24 stsp wline = NULL;
5967 6458efa5 2020-11-24 stsp free(line);
5968 6458efa5 2020-11-24 stsp line = NULL;
5969 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5970 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5971 6458efa5 2020-11-24 stsp if (--limit <= 0)
5972 6458efa5 2020-11-24 stsp return NULL;
5975 6458efa5 2020-11-24 stsp while (re && limit > 0) {
5976 6458efa5 2020-11-24 stsp char *line = NULL;
5978 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
5979 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
5980 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
5981 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
5982 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5983 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
5984 6458efa5 2020-11-24 stsp struct got_object_id *id;
5985 6458efa5 2020-11-24 stsp char *id_str;
5986 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
5988 6458efa5 2020-11-24 stsp return err;
5989 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
5990 6458efa5 2020-11-24 stsp if (err) {
5992 6458efa5 2020-11-24 stsp return err;
5994 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
5995 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
5996 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
5998 6458efa5 2020-11-24 stsp free(id_str);
5999 6458efa5 2020-11-24 stsp return err;
6002 6458efa5 2020-11-24 stsp free(id_str);
6004 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6005 6458efa5 2020-11-24 stsp if (line == NULL)
6006 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6009 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6010 6458efa5 2020-11-24 stsp if (err) {
6011 6458efa5 2020-11-24 stsp free(line);
6012 6458efa5 2020-11-24 stsp return err;
6014 6458efa5 2020-11-24 stsp if (n == s->selected) {
6015 6458efa5 2020-11-24 stsp if (view->focussed)
6016 6458efa5 2020-11-24 stsp wstandout(view->window);
6017 6458efa5 2020-11-24 stsp s->selected_entry = re;
6019 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6021 6458efa5 2020-11-24 stsp wattr_on(view->window,
6022 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6023 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6025 6458efa5 2020-11-24 stsp wattr_off(view->window,
6026 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6027 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6028 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6029 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6030 6458efa5 2020-11-24 stsp wstandend(view->window);
6031 6458efa5 2020-11-24 stsp free(line);
6032 6458efa5 2020-11-24 stsp free(wline);
6033 6458efa5 2020-11-24 stsp wline = NULL;
6035 6458efa5 2020-11-24 stsp s->ndisplayed++;
6036 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6039 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6042 6458efa5 2020-11-24 stsp view_vborder(view);
6043 6458efa5 2020-11-24 stsp return err;
6046 6458efa5 2020-11-24 stsp static const struct got_error *
6047 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6048 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6050 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6051 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
6052 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
6053 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6055 c42c9805 2020-11-24 stsp *new_view = NULL;
6057 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6058 c42c9805 2020-11-24 stsp if (err) {
6059 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6060 c42c9805 2020-11-24 stsp return err;
6062 c42c9805 2020-11-24 stsp return NULL;
6065 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6067 c42c9805 2020-11-24 stsp goto done;
6069 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6071 c42c9805 2020-11-24 stsp goto done;
6073 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6074 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6075 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6076 c42c9805 2020-11-24 stsp goto done;
6079 4e97c21c 2020-12-06 stsp err = open_tree_view(tree_view, tree, commit_id,
6080 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6082 c42c9805 2020-11-24 stsp goto done;
6084 c42c9805 2020-11-24 stsp *new_view = tree_view;
6086 c42c9805 2020-11-24 stsp free(commit_id);
6087 c42c9805 2020-11-24 stsp free(tree_id);
6088 c42c9805 2020-11-24 stsp if (err) {
6090 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6092 c42c9805 2020-11-24 stsp return err;
6094 c42c9805 2020-11-24 stsp static const struct got_error *
6095 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6097 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6098 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6099 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6100 34ba6917 2020-11-30 stsp int begin_x = 0;
6102 6458efa5 2020-11-24 stsp switch (ch) {
6104 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6106 6458efa5 2020-11-24 stsp case KEY_ENTER:
6107 6458efa5 2020-11-24 stsp case '\r':
6108 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6110 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6111 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6112 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6114 e78dc838 2020-12-04 stsp view->focussed = 0;
6115 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6116 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6117 6458efa5 2020-11-24 stsp err = view_close_child(view);
6119 6458efa5 2020-11-24 stsp return err;
6120 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6121 e78dc838 2020-12-04 stsp view->focus_child = 1;
6123 6458efa5 2020-11-24 stsp *new_view = log_view;
6126 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6128 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6129 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6130 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6132 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6134 e78dc838 2020-12-04 stsp view->focussed = 0;
6135 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6136 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6137 c42c9805 2020-11-24 stsp err = view_close_child(view);
6139 c42c9805 2020-11-24 stsp return err;
6140 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6141 e78dc838 2020-12-04 stsp view->focus_child = 1;
6143 c42c9805 2020-11-24 stsp *new_view = tree_view;
6146 6458efa5 2020-11-24 stsp case KEY_UP:
6147 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6148 6458efa5 2020-11-24 stsp s->selected--;
6151 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6153 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6154 6458efa5 2020-11-24 stsp case CTRL('b'):
6155 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6156 34ba6917 2020-11-30 stsp s->selected = 0;
6157 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6160 6458efa5 2020-11-24 stsp case KEY_DOWN:
6161 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6162 6458efa5 2020-11-24 stsp s->selected++;
6165 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6166 6458efa5 2020-11-24 stsp /* can't scroll any further */
6168 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6170 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6171 6458efa5 2020-11-24 stsp case CTRL('f'):
6172 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6173 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6174 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6175 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6178 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6180 6458efa5 2020-11-24 stsp case CTRL('l'):
6181 8924d611 2020-12-26 stsp tog_free_refs();
6182 8924d611 2020-12-26 stsp err = tog_load_refs(s->repo);
6185 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6186 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6188 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6189 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6190 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6196 6458efa5 2020-11-24 stsp return err;
6199 6458efa5 2020-11-24 stsp __dead static void
6200 6458efa5 2020-11-24 stsp usage_ref(void)
6203 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6204 6458efa5 2020-11-24 stsp getprogname());
6208 6458efa5 2020-11-24 stsp static const struct got_error *
6209 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6211 6458efa5 2020-11-24 stsp const struct got_error *error;
6212 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6213 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6214 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6216 6458efa5 2020-11-24 stsp struct tog_view *view;
6218 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6219 6458efa5 2020-11-24 stsp switch (ch) {
6221 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6222 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6223 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6227 e99e2d15 2020-11-24 naddy usage_ref();
6228 6458efa5 2020-11-24 stsp /* NOTREACHED */
6232 6458efa5 2020-11-24 stsp argc -= optind;
6233 6458efa5 2020-11-24 stsp argv += optind;
6235 6458efa5 2020-11-24 stsp if (argc > 1)
6236 e99e2d15 2020-11-24 naddy usage_ref();
6238 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6239 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6240 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6241 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6242 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6243 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6244 c156c7a4 2020-12-18 stsp goto done;
6245 6458efa5 2020-11-24 stsp if (worktree)
6246 6458efa5 2020-11-24 stsp repo_path =
6247 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6249 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6250 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6251 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6252 c156c7a4 2020-12-18 stsp goto done;
6256 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6257 6458efa5 2020-11-24 stsp if (error != NULL)
6258 6458efa5 2020-11-24 stsp goto done;
6260 6458efa5 2020-11-24 stsp init_curses();
6262 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6263 51a10b52 2020-12-26 stsp if (error)
6264 51a10b52 2020-12-26 stsp goto done;
6266 51a10b52 2020-12-26 stsp error = tog_load_refs(repo);
6267 6458efa5 2020-11-24 stsp if (error)
6268 6458efa5 2020-11-24 stsp goto done;
6270 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6271 6458efa5 2020-11-24 stsp if (view == NULL) {
6272 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6273 6458efa5 2020-11-24 stsp goto done;
6276 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6277 6458efa5 2020-11-24 stsp if (error)
6278 6458efa5 2020-11-24 stsp goto done;
6280 6458efa5 2020-11-24 stsp if (worktree) {
6281 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6282 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6283 6458efa5 2020-11-24 stsp worktree = NULL;
6285 6458efa5 2020-11-24 stsp error = view_loop(view);
6287 6458efa5 2020-11-24 stsp free(repo_path);
6288 6458efa5 2020-11-24 stsp free(cwd);
6290 6458efa5 2020-11-24 stsp got_repo_close(repo);
6291 51a10b52 2020-12-26 stsp tog_free_refs();
6292 6458efa5 2020-11-24 stsp return error;
6295 6458efa5 2020-11-24 stsp static void
6296 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6300 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6301 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6302 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6303 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6305 6879ba42 2020-10-01 naddy fputc('\n', fp);
6308 4ed7e80c 2018-05-20 stsp __dead static void
6309 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6311 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6313 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6314 6879ba42 2020-10-01 naddy getprogname());
6315 6879ba42 2020-10-01 naddy if (hflag) {
6316 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6317 6879ba42 2020-10-01 naddy list_commands(fp);
6319 6879ba42 2020-10-01 naddy exit(status);
6322 c2301be8 2018-04-30 stsp static char **
6323 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6325 ee85c5e8 2020-02-29 stsp va_list ap;
6326 c2301be8 2018-04-30 stsp char **argv;
6329 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6331 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6332 c2301be8 2018-04-30 stsp if (argv == NULL)
6333 c2301be8 2018-04-30 stsp err(1, "calloc");
6334 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6335 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6336 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6337 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6340 ee85c5e8 2020-02-29 stsp va_end(ap);
6341 c2301be8 2018-04-30 stsp return argv;
6345 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6346 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6347 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6348 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6349 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6351 ee85c5e8 2020-02-29 stsp static const struct got_error *
6352 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6354 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6355 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6356 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6357 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6358 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6359 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6360 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6362 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6363 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6364 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6366 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6367 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6368 ee85c5e8 2020-02-29 stsp goto done;
6370 ee85c5e8 2020-02-29 stsp if (worktree)
6371 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6373 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6374 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6375 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6376 ee85c5e8 2020-02-29 stsp goto done;
6379 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6380 ee85c5e8 2020-02-29 stsp if (error != NULL)
6381 ee85c5e8 2020-02-29 stsp goto done;
6383 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6384 ee85c5e8 2020-02-29 stsp repo, worktree);
6385 ee85c5e8 2020-02-29 stsp if (error)
6386 ee85c5e8 2020-02-29 stsp goto done;
6388 87670572 2020-12-26 naddy error = tog_load_refs(repo);
6389 84de9106 2020-12-26 stsp if (error)
6390 84de9106 2020-12-26 stsp goto done;
6391 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6392 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6393 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6394 ee85c5e8 2020-02-29 stsp if (error)
6395 ee85c5e8 2020-02-29 stsp goto done;
6397 ee85c5e8 2020-02-29 stsp if (worktree) {
6398 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6399 ee85c5e8 2020-02-29 stsp worktree = NULL;
6402 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6403 ee85c5e8 2020-02-29 stsp if (error) {
6404 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6405 ee85c5e8 2020-02-29 stsp goto done;
6406 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6407 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6408 6879ba42 2020-10-01 naddy usage(1, 1);
6409 ee85c5e8 2020-02-29 stsp /* not reached */
6412 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6413 ee85c5e8 2020-02-29 stsp repo = NULL;
6415 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6416 ee85c5e8 2020-02-29 stsp if (error)
6417 ee85c5e8 2020-02-29 stsp goto done;
6419 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6421 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6422 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6425 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6426 ee85c5e8 2020-02-29 stsp if (worktree)
6427 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6429 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6430 ee85c5e8 2020-02-29 stsp free(commit_id);
6431 ee85c5e8 2020-02-29 stsp free(cwd);
6432 ee85c5e8 2020-02-29 stsp free(repo_path);
6433 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6434 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6436 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6437 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6438 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6440 87670572 2020-12-26 naddy tog_free_refs();
6441 ee85c5e8 2020-02-29 stsp return error;
6445 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6447 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6448 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6449 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6450 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6451 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6452 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6453 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6456 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6458 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6459 9f7d7167 2018-04-29 stsp switch (ch) {
6461 9f7d7167 2018-04-29 stsp hflag = 1;
6464 53ccebc2 2019-07-30 stsp Vflag = 1;
6467 6879ba42 2020-10-01 naddy usage(hflag, 1);
6468 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6472 9f7d7167 2018-04-29 stsp argc -= optind;
6473 9f7d7167 2018-04-29 stsp argv += optind;
6474 9814e6a3 2020-09-27 naddy optind = 1;
6475 c2301be8 2018-04-30 stsp optreset = 1;
6477 53ccebc2 2019-07-30 stsp if (Vflag) {
6478 53ccebc2 2019-07-30 stsp got_version_print_str();
6479 6879ba42 2020-10-01 naddy return 0;
6482 4010e238 2020-12-04 stsp #ifndef PROFILE
6483 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6484 4010e238 2020-12-04 stsp NULL) == -1)
6485 4010e238 2020-12-04 stsp err(1, "pledge");
6488 c2301be8 2018-04-30 stsp if (argc == 0) {
6489 f29d3e89 2018-06-23 stsp if (hflag)
6490 6879ba42 2020-10-01 naddy usage(hflag, 0);
6491 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6492 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6494 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6498 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6499 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6500 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6501 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6502 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6508 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6509 ee85c5e8 2020-02-29 stsp if (argc != 1)
6510 6879ba42 2020-10-01 naddy usage(0, 1);
6511 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6512 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6514 ee85c5e8 2020-02-29 stsp if (hflag)
6515 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6517 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6521 b46c1e04 2020-09-20 naddy putchar('\n');
6522 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6524 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6525 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6526 a2f4a359 2020-02-28 stsp free(cmd_argv);
6529 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6530 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);